2016-01-17 18:08:28 +08:00
|
|
|
// Integration tests - test rclone by doing real transactions to a
|
|
|
|
// storage provider to and from the local disk.
|
|
|
|
//
|
|
|
|
// By default it will use a local fs, however you can provide a
|
|
|
|
// -remote option to use a different remote. The test_all.go script
|
|
|
|
// is a wrapper to call this for all the test remotes.
|
|
|
|
//
|
|
|
|
// FIXME not safe for concurrent running of tests until fs.Config is
|
|
|
|
// no longer a global
|
|
|
|
//
|
|
|
|
// NB When writing tests
|
|
|
|
//
|
|
|
|
// Make sure every series of writes to the remote has a
|
|
|
|
// fstest.CheckItems() before use. This make sure the directory
|
|
|
|
// listing is now consistent and stops cascading errors.
|
|
|
|
//
|
2018-01-13 00:30:54 +08:00
|
|
|
// Call accounting.Stats.ResetCounters() before every fs.Sync() as it
|
|
|
|
// uses the error count internally.
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
package operations_test
|
2014-08-02 00:58:39 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-02-13 18:48:26 +08:00
|
|
|
"errors"
|
2017-01-11 22:59:53 +08:00
|
|
|
"fmt"
|
2017-02-13 18:48:26 +08:00
|
|
|
"io"
|
2014-08-02 00:58:39 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-01-12 00:05:41 +08:00
|
|
|
_ "github.com/ncw/rclone/backend/all" // import all backends
|
2014-08-02 00:58:39 +08:00
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-13 00:30:54 +08:00
|
|
|
"github.com/ncw/rclone/fs/accounting"
|
|
|
|
"github.com/ncw/rclone/fs/filter"
|
|
|
|
"github.com/ncw/rclone/fs/hash"
|
|
|
|
"github.com/ncw/rclone/fs/list"
|
|
|
|
"github.com/ncw/rclone/fs/operations"
|
2014-08-02 00:58:39 +08:00
|
|
|
"github.com/ncw/rclone/fstest"
|
2016-06-25 21:28:26 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-06-30 00:59:31 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-02 00:58:39 +08:00
|
|
|
)
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Some times used in the tests
|
|
|
|
var (
|
|
|
|
t1 = fstest.Time("2001-02-03T04:05:06.499999999Z")
|
|
|
|
t2 = fstest.Time("2011-12-25T12:59:59.123456789Z")
|
|
|
|
t3 = fstest.Time("2011-12-30T12:59:59.000000000Z")
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.TestMain(m)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMkdir(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
2018-01-13 00:30:54 +08:00
|
|
|
|
|
|
|
err := operations.Mkdir(r.Fremote, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListing(t, r.Fremote, []fstest.Item{})
|
|
|
|
|
|
|
|
err = operations.Mkdir(r.Fremote, "")
|
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLsd(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("sub dir/hello world", "hello world", t1)
|
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.ListDir(r.Fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
res := buf.String()
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Contains(t, res, "sub dir\n")
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLs(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.List(r.Fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
res := buf.String()
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Contains(t, res, " 0 empty space\n")
|
|
|
|
assert.Contains(t, res, " 60 potato2\n")
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2018-10-20 00:41:14 +08:00
|
|
|
func TestLsWithFilesFrom(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
|
|
|
|
|
|
|
// Set the --files-from equivalent
|
|
|
|
f, err := filter.NewFilter(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.AddFile("potato2"))
|
|
|
|
require.NoError(t, f.AddFile("notfound"))
|
|
|
|
|
|
|
|
// Monkey patch the active filter
|
|
|
|
oldFilter := filter.Active
|
|
|
|
filter.Active = f
|
|
|
|
defer func() {
|
|
|
|
filter.Active = oldFilter
|
|
|
|
}()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = operations.List(r.Fremote, &buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, " 60 potato2\n", buf.String())
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestLsLong(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.ListLong(r.Fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
res := buf.String()
|
2015-08-17 06:24:34 +08:00
|
|
|
lines := strings.Split(strings.Trim(res, "\n"), "\n")
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, 2, len(lines))
|
2015-08-17 06:24:34 +08:00
|
|
|
|
|
|
|
timeFormat := "2006-01-02 15:04:05.000000000"
|
2017-10-29 20:23:10 +08:00
|
|
|
precision := r.Fremote.Precision()
|
2015-09-23 02:04:12 +08:00
|
|
|
location := time.Now().Location()
|
2015-08-17 06:24:34 +08:00
|
|
|
checkTime := func(m, filename string, expected time.Time) {
|
2015-09-23 02:04:12 +08:00
|
|
|
modTime, err := time.ParseInLocation(timeFormat, m, location) // parse as localtime
|
2015-08-17 06:24:34 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error parsing %q: %v", m, err)
|
|
|
|
} else {
|
|
|
|
dt, ok := fstest.CheckTimeEqualWithPrecision(expected, modTime, precision)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: Modification time difference too big |%s| > %s (%s vs %s) (precision %s)", filename, dt, precision, modTime, expected, precision)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m1 := regexp.MustCompile(`(?m)^ 0 (\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{9}) empty space$`)
|
|
|
|
if ms := m1.FindStringSubmatch(res); ms == nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("empty space missing: %q", res)
|
2015-08-17 06:24:34 +08:00
|
|
|
} else {
|
|
|
|
checkTime(ms[1], "empty space", t2.Local())
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2015-08-17 06:24:34 +08:00
|
|
|
|
|
|
|
m2 := regexp.MustCompile(`(?m)^ 60 (\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{9}) potato2$`)
|
|
|
|
if ms := m2.FindStringSubmatch(res); ms == nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("potato2 missing: %q", res)
|
2015-08-17 06:24:34 +08:00
|
|
|
} else {
|
|
|
|
checkTime(ms[1], "potato2", t1.Local())
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 22:09:31 +08:00
|
|
|
func TestHashSums(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2017-05-26 22:09:31 +08:00
|
|
|
// MD5 Sum
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.Md5sum(r.Fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
res := buf.String()
|
2015-08-17 06:24:34 +08:00
|
|
|
if !strings.Contains(res, "d41d8cd98f00b204e9800998ecf8427e empty space\n") &&
|
2016-01-16 17:44:46 +08:00
|
|
|
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
|
2015-08-17 06:24:34 +08:00
|
|
|
!strings.Contains(res, " empty space\n") {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("empty space missing: %q", res)
|
|
|
|
}
|
2017-06-07 06:21:47 +08:00
|
|
|
if !strings.Contains(res, "d6548b156ea68a4e003e786df99eee76 potato2\n") &&
|
2016-01-16 17:44:46 +08:00
|
|
|
!strings.Contains(res, " UNSUPPORTED potato2\n") &&
|
2015-08-17 06:24:34 +08:00
|
|
|
!strings.Contains(res, " potato2\n") {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("potato2 missing: %q", res)
|
|
|
|
}
|
|
|
|
|
2017-05-26 22:09:31 +08:00
|
|
|
// SHA1 Sum
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2017-06-07 06:21:47 +08:00
|
|
|
buf.Reset()
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.Sha1sum(r.Fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2017-05-26 22:09:31 +08:00
|
|
|
res = buf.String()
|
2016-01-17 18:08:28 +08:00
|
|
|
if !strings.Contains(res, "da39a3ee5e6b4b0d3255bfef95601890afd80709 empty space\n") &&
|
|
|
|
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
|
|
|
|
!strings.Contains(res, " empty space\n") {
|
|
|
|
t.Errorf("empty space missing: %q", res)
|
|
|
|
}
|
|
|
|
if !strings.Contains(res, "9dc7f7d3279715991a22853f5981df582b7f9f6d potato2\n") &&
|
|
|
|
!strings.Contains(res, " UNSUPPORTED potato2\n") &&
|
|
|
|
!strings.Contains(res, " potato2\n") {
|
|
|
|
t.Errorf("potato2 missing: %q", res)
|
|
|
|
}
|
2017-05-26 22:09:31 +08:00
|
|
|
|
|
|
|
// Dropbox Hash Sum
|
|
|
|
|
2017-06-07 06:21:47 +08:00
|
|
|
buf.Reset()
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.DropboxHashSum(r.Fremote, &buf)
|
2017-05-26 22:09:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
res = buf.String()
|
|
|
|
if !strings.Contains(res, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 empty space\n") &&
|
2017-06-07 06:21:47 +08:00
|
|
|
!strings.Contains(res, " UNSUPPORTED empty space\n") &&
|
|
|
|
!strings.Contains(res, " empty space\n") {
|
2017-05-26 22:09:31 +08:00
|
|
|
t.Errorf("empty space missing: %q", res)
|
|
|
|
}
|
|
|
|
if !strings.Contains(res, "a979481df794fed9c3990a6a422e0b1044ac802c15fab13af9c687f8bdbee01a potato2\n") &&
|
2017-06-07 06:21:47 +08:00
|
|
|
!strings.Contains(res, " UNSUPPORTED potato2\n") &&
|
|
|
|
!strings.Contains(res, " potato2\n") {
|
2017-05-26 22:09:31 +08:00
|
|
|
t.Errorf("potato2 missing: %q", res)
|
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
}
|
|
|
|
|
2015-10-03 02:48:48 +08:00
|
|
|
func TestCount(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
2016-06-03 04:02:44 +08:00
|
|
|
file3 := r.WriteBoth("sub dir/potato3", "hello", t2)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2, file3)
|
2016-06-03 04:02:44 +08:00
|
|
|
|
|
|
|
// Check the MaxDepth too
|
|
|
|
fs.Config.MaxDepth = 1
|
|
|
|
defer func() { fs.Config.MaxDepth = -1 }()
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
objects, size, err := operations.Count(r.Fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), objects)
|
|
|
|
assert.Equal(t, int64(60), size)
|
2015-10-03 02:48:48 +08:00
|
|
|
}
|
|
|
|
|
2015-12-03 06:25:32 +08:00
|
|
|
func TestDelete(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2015-12-03 06:25:32 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("small", "1234567890", t2) // 10 bytes
|
|
|
|
file2 := r.WriteObject("medium", "------------------------------------------------------------", t1) // 60 bytes
|
|
|
|
file3 := r.WriteObject("large", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 100 bytes
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2, file3)
|
2015-12-03 06:25:32 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
filter.Active.Opt.MaxSize = 60
|
2015-12-03 06:25:32 +08:00
|
|
|
defer func() {
|
2018-01-13 00:30:54 +08:00
|
|
|
filter.Active.Opt.MaxSize = -1
|
2015-12-03 06:25:32 +08:00
|
|
|
}()
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.Delete(r.Fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file3)
|
2015-12-03 06:25:32 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 01:07:04 +08:00
|
|
|
func testCheck(t *testing.T, checkFunction func(fdst, fsrc fs.Fs, oneway bool) error) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-01-17 18:08:28 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
|
2018-05-30 01:07:04 +08:00
|
|
|
check := func(i int, wantErrors int64, oneway bool) {
|
2017-10-29 20:23:10 +08:00
|
|
|
fs.Debugf(r.Fremote, "%d: Starting check test", i)
|
2018-01-13 00:30:54 +08:00
|
|
|
oldErrors := accounting.Stats.GetErrors()
|
2018-05-30 01:07:04 +08:00
|
|
|
err := checkFunction(r.Fremote, r.Flocal, oneway)
|
2018-01-13 00:30:54 +08:00
|
|
|
gotErrors := accounting.Stats.GetErrors() - oldErrors
|
2016-01-17 18:08:28 +08:00
|
|
|
if wantErrors == 0 && err != nil {
|
|
|
|
t.Errorf("%d: Got error when not expecting one: %v", i, err)
|
|
|
|
}
|
|
|
|
if wantErrors != 0 && err == nil {
|
|
|
|
t.Errorf("%d: No error when expecting one", i)
|
|
|
|
}
|
|
|
|
if wantErrors != gotErrors {
|
|
|
|
t.Errorf("%d: Expecting %d errors but got %d", i, wantErrors, gotErrors)
|
|
|
|
}
|
2017-10-29 20:23:10 +08:00
|
|
|
fs.Debugf(r.Fremote, "%d: Ending check test", i)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
file1 := r.WriteBoth("rutabaga", "is tasty", t3)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1)
|
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
2018-05-30 01:07:04 +08:00
|
|
|
check(1, 0, false)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
file2 := r.WriteFile("potato2", "------------------------------------------------------------", t1)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1, file2)
|
2018-05-30 01:07:04 +08:00
|
|
|
check(2, 1, false)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
file3 := r.WriteObject("empty space", "", t2)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file3)
|
2018-05-30 01:07:04 +08:00
|
|
|
check(3, 2, false)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
2016-04-07 21:56:27 +08:00
|
|
|
file2r := file2
|
|
|
|
if fs.Config.SizeOnly {
|
|
|
|
file2r = r.WriteObject("potato2", "--Some-Differences-But-Size-Only-Is-Enabled-----------------", t1)
|
|
|
|
} else {
|
|
|
|
r.WriteObject("potato2", "------------------------------------------------------------", t1)
|
|
|
|
}
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2r, file3)
|
2018-05-30 01:07:04 +08:00
|
|
|
check(4, 1, false)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
r.WriteFile("empty space", "", t2)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1, file2, file3)
|
2018-05-30 01:07:04 +08:00
|
|
|
check(5, 0, false)
|
|
|
|
|
|
|
|
file4 := r.WriteObject("remotepotato", "------------------------------------------------------------", t1)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2r, file3, file4)
|
|
|
|
check(6, 1, false)
|
|
|
|
check(7, 0, true)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-03-06 00:10:51 +08:00
|
|
|
|
2017-02-13 18:48:26 +08:00
|
|
|
func TestCheck(t *testing.T) {
|
2018-01-13 00:30:54 +08:00
|
|
|
testCheck(t, operations.Check)
|
2017-02-13 18:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckDownload(t *testing.T) {
|
2018-01-13 00:30:54 +08:00
|
|
|
testCheck(t, operations.CheckDownload)
|
2017-02-13 18:48:26 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:56:27 +08:00
|
|
|
func TestCheckSizeOnly(t *testing.T) {
|
|
|
|
fs.Config.SizeOnly = true
|
|
|
|
defer func() { fs.Config.SizeOnly = false }()
|
|
|
|
TestCheck(t)
|
|
|
|
}
|
|
|
|
|
2016-08-19 05:43:02 +08:00
|
|
|
func TestCat(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-08-19 05:43:02 +08:00
|
|
|
defer r.Finalise()
|
2017-02-08 16:09:41 +08:00
|
|
|
file1 := r.WriteBoth("file1", "ABCDEFGHIJ", t1)
|
|
|
|
file2 := r.WriteBoth("file2", "012345678", t2)
|
2016-08-19 05:43:02 +08:00
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
2016-08-19 05:43:02 +08:00
|
|
|
|
2017-02-08 16:09:41 +08:00
|
|
|
for _, test := range []struct {
|
|
|
|
offset int64
|
|
|
|
count int64
|
|
|
|
a string
|
|
|
|
b string
|
|
|
|
}{
|
|
|
|
{0, -1, "ABCDEFGHIJ", "012345678"},
|
|
|
|
{0, 5, "ABCDE", "01234"},
|
|
|
|
{-3, -1, "HIJ", "678"},
|
2017-06-10 20:48:00 +08:00
|
|
|
{1, 3, "BCD", "123"},
|
2017-02-08 16:09:41 +08:00
|
|
|
} {
|
|
|
|
var buf bytes.Buffer
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.Cat(r.Fremote, &buf, test.offset, test.count)
|
2017-02-08 16:09:41 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
res := buf.String()
|
2016-08-19 05:43:02 +08:00
|
|
|
|
2017-02-08 16:09:41 +08:00
|
|
|
if res != test.a+test.b && res != test.b+test.a {
|
|
|
|
t.Errorf("Incorrect output from Cat(%d,%d): %q", test.offset, test.count, res)
|
|
|
|
}
|
2016-08-19 05:43:02 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 19:49:31 +08:00
|
|
|
|
2017-08-04 03:42:35 +08:00
|
|
|
func TestRcat(t *testing.T) {
|
2017-09-11 14:26:53 +08:00
|
|
|
checkSumBefore := fs.Config.CheckSum
|
|
|
|
defer func() { fs.Config.CheckSum = checkSumBefore }()
|
2017-08-04 03:42:35 +08:00
|
|
|
|
2017-09-16 00:08:14 +08:00
|
|
|
check := func(withChecksum bool) {
|
|
|
|
fs.Config.CheckSum = withChecksum
|
|
|
|
prefix := "no_checksum_"
|
|
|
|
if withChecksum {
|
|
|
|
prefix = "with_checksum_"
|
|
|
|
}
|
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2017-09-11 14:26:53 +08:00
|
|
|
defer r.Finalise()
|
2017-08-04 03:42:35 +08:00
|
|
|
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckListing(t, r.Fremote, []fstest.Item{})
|
2017-08-04 03:42:35 +08:00
|
|
|
|
2017-09-11 14:26:53 +08:00
|
|
|
data1 := "this is some really nice test data"
|
2017-09-16 00:08:14 +08:00
|
|
|
path1 := prefix + "small_file_from_pipe"
|
2017-09-11 14:25:34 +08:00
|
|
|
|
2017-09-11 14:26:53 +08:00
|
|
|
data2 := string(make([]byte, fs.Config.StreamingUploadCutoff+1))
|
2017-09-16 00:08:14 +08:00
|
|
|
path2 := prefix + "big_file_from_pipe"
|
2017-09-11 14:25:34 +08:00
|
|
|
|
2017-09-11 14:26:53 +08:00
|
|
|
in := ioutil.NopCloser(strings.NewReader(data1))
|
2018-01-13 00:30:54 +08:00
|
|
|
_, err := operations.Rcat(r.Fremote, path1, in, t1)
|
2017-09-11 14:26:53 +08:00
|
|
|
require.NoError(t, err)
|
2017-08-04 03:42:35 +08:00
|
|
|
|
2017-09-11 14:26:53 +08:00
|
|
|
in = ioutil.NopCloser(strings.NewReader(data2))
|
2018-01-13 00:30:54 +08:00
|
|
|
_, err = operations.Rcat(r.Fremote, path2, in, t2)
|
2017-09-11 14:26:53 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
file1 := fstest.NewItem(path1, data1, t1)
|
|
|
|
file2 := fstest.NewItem(path2, data2, t2)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
2017-09-11 14:26:53 +08:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:08:14 +08:00
|
|
|
check(true)
|
|
|
|
check(false)
|
2017-08-04 03:42:35 +08:00
|
|
|
}
|
|
|
|
|
2018-10-27 06:47:23 +08:00
|
|
|
func TestPurge(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
r.Mkdir(r.Fremote)
|
|
|
|
|
|
|
|
// Make some files and dirs
|
|
|
|
r.ForceMkdir(r.Fremote)
|
|
|
|
file1 := r.WriteObject("A1/B1/C1/one", "aaa", t1)
|
|
|
|
//..and dirs we expect to delete
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2/C2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B1/C3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3/C4"))
|
|
|
|
//..and one more file at the end
|
|
|
|
file2 := r.WriteObject("A1/two", "bbb", t2)
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{
|
|
|
|
file1, file2,
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A1/B1",
|
|
|
|
"A1/B1/C1",
|
|
|
|
"A2",
|
|
|
|
"A1/B2",
|
|
|
|
"A1/B2/C2",
|
|
|
|
"A1/B1/C3",
|
|
|
|
"A3",
|
|
|
|
"A3/B3",
|
|
|
|
"A3/B3/C4",
|
|
|
|
},
|
|
|
|
fs.GetModifyWindow(r.Fremote),
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, operations.Purge(r.Fremote, "A1/B1"))
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{
|
|
|
|
file2,
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A2",
|
|
|
|
"A1/B2",
|
|
|
|
"A1/B2/C2",
|
|
|
|
"A3",
|
|
|
|
"A3/B3",
|
|
|
|
"A3/B3/C4",
|
|
|
|
},
|
|
|
|
fs.GetModifyWindow(r.Fremote),
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, operations.Purge(r.Fremote, ""))
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{},
|
|
|
|
[]string{},
|
|
|
|
fs.GetModifyWindow(r.Fremote),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-13 18:23:54 +08:00
|
|
|
func TestRmdirsNoLeaveRoot(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-11-27 19:49:31 +08:00
|
|
|
defer r.Finalise()
|
2017-10-29 20:23:10 +08:00
|
|
|
r.Mkdir(r.Fremote)
|
2016-11-28 20:23:24 +08:00
|
|
|
|
2016-11-27 19:49:31 +08:00
|
|
|
// Make some files and dirs we expect to keep
|
2017-10-29 20:23:10 +08:00
|
|
|
r.ForceMkdir(r.Fremote)
|
2016-11-27 19:49:31 +08:00
|
|
|
file1 := r.WriteObject("A1/B1/C1/one", "aaa", t1)
|
|
|
|
//..and dirs we expect to delete
|
2018-01-13 00:30:54 +08:00
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2/C2"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B1/C3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3/C4"))
|
2017-04-27 01:41:01 +08:00
|
|
|
//..and one more file at the end
|
|
|
|
file2 := r.WriteObject("A1/two", "bbb", t2)
|
2016-11-27 19:49:31 +08:00
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
2017-10-29 20:23:10 +08:00
|
|
|
r.Fremote,
|
2016-11-27 19:49:31 +08:00
|
|
|
[]fstest.Item{
|
|
|
|
file1, file2,
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A1/B1",
|
|
|
|
"A1/B1/C1",
|
|
|
|
"A2",
|
|
|
|
"A1/B2",
|
|
|
|
"A1/B2/C2",
|
|
|
|
"A1/B1/C3",
|
|
|
|
"A3",
|
|
|
|
"A3/B3",
|
|
|
|
"A3/B3/C4",
|
2017-08-09 22:51:27 +08:00
|
|
|
},
|
2018-06-04 02:45:34 +08:00
|
|
|
fs.GetModifyWindow(r.Fremote),
|
2016-11-27 19:49:31 +08:00
|
|
|
)
|
|
|
|
|
2018-10-27 06:47:23 +08:00
|
|
|
require.NoError(t, operations.Rmdirs(r.Fremote, "A3/B3/C4", false))
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{
|
|
|
|
file1, file2,
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A1/B1",
|
|
|
|
"A1/B1/C1",
|
|
|
|
"A2",
|
|
|
|
"A1/B2",
|
|
|
|
"A1/B2/C2",
|
|
|
|
"A1/B1/C3",
|
|
|
|
"A3",
|
|
|
|
"A3/B3",
|
|
|
|
},
|
|
|
|
fs.GetModifyWindow(r.Fremote),
|
|
|
|
)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
require.NoError(t, operations.Rmdirs(r.Fremote, "", false))
|
2016-11-27 19:49:31 +08:00
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
2017-10-29 20:23:10 +08:00
|
|
|
r.Fremote,
|
2016-11-27 19:49:31 +08:00
|
|
|
[]fstest.Item{
|
|
|
|
file1, file2,
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A1/B1",
|
|
|
|
"A1/B1/C1",
|
|
|
|
},
|
2018-06-04 02:45:34 +08:00
|
|
|
fs.GetModifyWindow(r.Fremote),
|
2016-11-27 19:49:31 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
2016-10-24 00:34:17 +08:00
|
|
|
|
2017-12-13 18:23:54 +08:00
|
|
|
func TestRmdirsLeaveRoot(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
r.Mkdir(r.Fremote)
|
|
|
|
|
|
|
|
r.ForceMkdir(r.Fremote)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B1"))
|
|
|
|
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B1/C1"))
|
2017-12-13 18:23:54 +08:00
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
"A1/B1",
|
|
|
|
"A1/B1/C1",
|
|
|
|
},
|
2018-06-04 02:45:34 +08:00
|
|
|
fs.GetModifyWindow(r.Fremote),
|
2017-12-13 18:23:54 +08:00
|
|
|
)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
require.NoError(t, operations.Rmdirs(r.Fremote, "A1", true))
|
2017-12-13 18:23:54 +08:00
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(
|
|
|
|
t,
|
|
|
|
r.Fremote,
|
|
|
|
[]fstest.Item{},
|
|
|
|
[]string{
|
|
|
|
"A1",
|
|
|
|
},
|
2018-06-04 02:45:34 +08:00
|
|
|
fs.GetModifyWindow(r.Fremote),
|
2017-12-13 18:23:54 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-30 23:45:41 +08:00
|
|
|
func TestRcatSize(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
const body = "------------------------------------------------------------"
|
|
|
|
file1 := r.WriteFile("potato1", body, t1)
|
|
|
|
file2 := r.WriteFile("potato2", body, t2)
|
|
|
|
// Test with known length
|
|
|
|
bodyReader := ioutil.NopCloser(strings.NewReader(body))
|
|
|
|
obj, err := operations.RcatSize(r.Fremote, file1.Path, bodyReader, int64(len(body)), file1.ModTime)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(len(body)), obj.Size())
|
|
|
|
assert.Equal(t, file1.Path, obj.Remote())
|
|
|
|
|
|
|
|
// Test with unknown length
|
|
|
|
bodyReader = ioutil.NopCloser(strings.NewReader(body)) // reset Reader
|
|
|
|
ioutil.NopCloser(strings.NewReader(body))
|
|
|
|
obj, err = operations.RcatSize(r.Fremote, file2.Path, bodyReader, -1, file2.ModTime)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(len(body)), obj.Size())
|
|
|
|
assert.Equal(t, file2.Path, obj.Remote())
|
|
|
|
|
|
|
|
// Check files exist
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
|
|
|
}
|
|
|
|
|
2016-10-24 00:34:17 +08:00
|
|
|
func TestMoveFile(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-10-24 00:34:17 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
file1 := r.WriteFile("file1", "file1 contents", t1)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
2016-10-24 00:34:17 +08:00
|
|
|
|
|
|
|
file2 := file1
|
|
|
|
file2.Path = "sub/file2"
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.MoveFile(r.Fremote, r.Flocal, file2.Path, file1.Path)
|
2016-10-24 00:34:17 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2016-10-24 00:34:17 +08:00
|
|
|
|
|
|
|
r.WriteFile("file1", "file1 contents", t1)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
2016-10-24 00:34:17 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.MoveFile(r.Fremote, r.Flocal, file2.Path, file1.Path)
|
2016-10-24 00:34:17 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2017-05-27 23:30:26 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.MoveFile(r.Fremote, r.Fremote, file2.Path, file2.Path)
|
2017-05-27 23:30:26 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2016-10-24 00:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFile(t *testing.T) {
|
2017-10-29 20:23:10 +08:00
|
|
|
r := fstest.NewRun(t)
|
2016-10-24 00:34:17 +08:00
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
file1 := r.WriteFile("file1", "file1 contents", t1)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
2016-10-24 00:34:17 +08:00
|
|
|
|
|
|
|
file2 := file1
|
|
|
|
file2.Path = "sub/file2"
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err := operations.CopyFile(r.Fremote, r.Flocal, file2.Path, file1.Path)
|
2016-10-24 00:34:17 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2016-10-24 00:34:17 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.CopyFile(r.Fremote, r.Flocal, file2.Path, file1.Path)
|
2016-10-24 00:34:17 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2017-05-27 23:30:26 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
err = operations.CopyFile(r.Fremote, r.Fremote, file2.Path, file2.Path)
|
2017-05-27 23:30:26 +08:00
|
|
|
require.NoError(t, err)
|
2017-10-29 20:23:10 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
2016-10-24 00:34:17 +08:00
|
|
|
}
|
2017-01-11 22:59:53 +08:00
|
|
|
|
|
|
|
// testFsInfo is for unit testing fs.Info
|
|
|
|
type testFsInfo struct {
|
|
|
|
name string
|
|
|
|
root string
|
|
|
|
stringVal string
|
|
|
|
precision time.Duration
|
2018-01-13 00:30:54 +08:00
|
|
|
hashes hash.Set
|
2017-01-14 01:21:47 +08:00
|
|
|
features fs.Features
|
2017-01-11 22:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the remote (as passed into NewFs)
|
|
|
|
func (i *testFsInfo) Name() string { return i.name }
|
|
|
|
|
|
|
|
// Root of the remote (as passed into NewFs)
|
|
|
|
func (i *testFsInfo) Root() string { return i.root }
|
|
|
|
|
|
|
|
// String returns a description of the FS
|
|
|
|
func (i *testFsInfo) String() string { return i.stringVal }
|
|
|
|
|
|
|
|
// Precision of the ModTimes in this Fs
|
|
|
|
func (i *testFsInfo) Precision() time.Duration { return i.precision }
|
|
|
|
|
|
|
|
// Returns the supported hash types of the filesystem
|
2018-01-13 00:30:54 +08:00
|
|
|
func (i *testFsInfo) Hashes() hash.Set { return i.hashes }
|
2017-01-11 22:59:53 +08:00
|
|
|
|
2017-01-14 01:21:47 +08:00
|
|
|
// Returns the supported hash types of the filesystem
|
|
|
|
func (i *testFsInfo) Features() *fs.Features { return &i.features }
|
|
|
|
|
2017-01-11 22:59:53 +08:00
|
|
|
func TestSameConfig(t *testing.T) {
|
|
|
|
a := &testFsInfo{name: "name", root: "root"}
|
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
root string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"name", "root", true},
|
|
|
|
{"name", "rooty", true},
|
|
|
|
{"namey", "root", false},
|
|
|
|
{"namey", "roott", false},
|
|
|
|
} {
|
|
|
|
b := &testFsInfo{name: test.name, root: test.root}
|
2018-01-13 00:30:54 +08:00
|
|
|
actual := operations.SameConfig(a, b)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2018-01-13 00:30:54 +08:00
|
|
|
actual = operations.SameConfig(b, a)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSame(t *testing.T) {
|
|
|
|
a := &testFsInfo{name: "name", root: "root"}
|
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
root string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"name", "root", true},
|
|
|
|
{"name", "rooty", false},
|
|
|
|
{"namey", "root", false},
|
|
|
|
{"namey", "roott", false},
|
|
|
|
} {
|
|
|
|
b := &testFsInfo{name: test.name, root: test.root}
|
2018-01-13 00:30:54 +08:00
|
|
|
actual := operations.Same(a, b)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual)
|
2018-01-13 00:30:54 +08:00
|
|
|
actual = operations.Same(b, a)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverlapping(t *testing.T) {
|
|
|
|
a := &testFsInfo{name: "name", root: "root"}
|
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
|
|
|
root string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{"name", "root", true},
|
|
|
|
{"namey", "root", false},
|
|
|
|
{"name", "rooty", false},
|
|
|
|
{"namey", "rooty", false},
|
|
|
|
{"name", "roo", false},
|
|
|
|
{"name", "root/toot", true},
|
|
|
|
{"name", "root/toot/", true},
|
|
|
|
{"name", "", true},
|
|
|
|
{"name", "/", true},
|
|
|
|
} {
|
|
|
|
b := &testFsInfo{name: test.name, root: test.root}
|
|
|
|
what := fmt.Sprintf("(%q,%q) vs (%q,%q)", a.name, a.root, b.name, b.root)
|
2018-01-13 00:30:54 +08:00
|
|
|
actual := operations.Overlapping(a, b)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual, what)
|
2018-01-13 00:30:54 +08:00
|
|
|
actual = operations.Overlapping(b, a)
|
2017-01-11 22:59:53 +08:00
|
|
|
assert.Equal(t, test.expected, actual, what)
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 19:00:05 +08:00
|
|
|
|
2017-02-13 18:48:26 +08:00
|
|
|
type errorReader struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (er errorReader) Read(p []byte) (n int, err error) {
|
|
|
|
return 0, er.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckEqualReaders(t *testing.T) {
|
|
|
|
b65a := make([]byte, 65*1024)
|
|
|
|
b65b := make([]byte, 65*1024)
|
|
|
|
b65b[len(b65b)-1] = 1
|
|
|
|
b66 := make([]byte, 66*1024)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err := operations.CheckEqualReaders(bytes.NewBuffer(b65a), bytes.NewBuffer(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, differ, false)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b65a), bytes.NewBuffer(b65b))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b65a), bytes.NewBuffer(b66))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b66), bytes.NewBuffer(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
|
|
|
myErr := errors.New("sentinel")
|
|
|
|
wrap := func(b []byte) io.Reader {
|
|
|
|
r := bytes.NewBuffer(b)
|
|
|
|
e := errorReader{myErr}
|
|
|
|
return io.MultiReader(r, e)
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(wrap(b65a), bytes.NewBuffer(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(wrap(b65a), bytes.NewBuffer(b65b))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(wrap(b65a), bytes.NewBuffer(b66))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(wrap(b66), bytes.NewBuffer(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b65a), wrap(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b65a), wrap(b65b))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b65a), wrap(b66))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
differ, err = operations.CheckEqualReaders(bytes.NewBuffer(b66), wrap(b65a))
|
2017-02-13 18:48:26 +08:00
|
|
|
assert.Equal(t, myErr, err)
|
|
|
|
assert.Equal(t, differ, true)
|
|
|
|
}
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
func TestListFormat(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("a", "a", t1)
|
|
|
|
file2 := r.WriteObject("subdir/b", "b", t1)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
items, _ := list.DirSorted(r.Fremote, true, "")
|
|
|
|
var list operations.ListFormat
|
2018-01-06 22:39:31 +08:00
|
|
|
list.AddPath()
|
|
|
|
list.SetDirSlash(false)
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "subdir", list.Format(items[1]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
list.SetDirSlash(true)
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "subdir/", list.Format(items[1]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
list.SetOutput(nil)
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "", list.Format(items[1]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
list.AppendOutput(func() string { return "a" })
|
|
|
|
list.AppendOutput(func() string { return "b" })
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "ab", list.Format(items[1]))
|
2018-01-06 22:39:31 +08:00
|
|
|
list.SetSeparator(":::")
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "a:::b", list.Format(items[1]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddModTime()
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, items[0].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[0]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
2018-05-13 16:18:08 +08:00
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddID()
|
2018-05-13 17:55:18 +08:00
|
|
|
_ = list.Format(items[0]) // Can't really check anything - at least it didn't panic!
|
2018-05-13 16:18:08 +08:00
|
|
|
|
2018-05-13 17:37:25 +08:00
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddMimeType()
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Contains(t, list.Format(items[0]), "/")
|
|
|
|
assert.Equal(t, "inode/directory", list.Format(items[1]))
|
2018-05-13 17:37:25 +08:00
|
|
|
|
2018-06-03 17:42:34 +08:00
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddPath()
|
|
|
|
list.SetAbsolute(true)
|
|
|
|
assert.Equal(t, "/a", list.Format(items[0]))
|
|
|
|
list.SetAbsolute(false)
|
|
|
|
assert.Equal(t, "a", list.Format(items[0]))
|
|
|
|
|
2018-01-06 22:39:31 +08:00
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddSize()
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "1", list.Format(items[0]))
|
2018-01-06 22:39:31 +08:00
|
|
|
|
|
|
|
list.AddPath()
|
|
|
|
list.AddModTime()
|
|
|
|
list.SetDirSlash(true)
|
|
|
|
list.SetSeparator("__SEP__")
|
2018-05-13 17:55:18 +08:00
|
|
|
assert.Equal(t, "1__SEP__a__SEP__"+items[0].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[0]))
|
|
|
|
assert.Equal(t, fmt.Sprintf("%d", items[1].Size())+"__SEP__subdir/__SEP__"+items[1].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[1]))
|
2018-01-07 01:53:37 +08:00
|
|
|
|
|
|
|
for _, test := range []struct {
|
2018-01-13 00:30:54 +08:00
|
|
|
ht hash.Type
|
2018-01-07 01:53:37 +08:00
|
|
|
want string
|
|
|
|
}{
|
2018-01-19 04:27:52 +08:00
|
|
|
{hash.MD5, "0cc175b9c0f1b6a831c399e269772661"},
|
|
|
|
{hash.SHA1, "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"},
|
|
|
|
{hash.Dropbox, "bf5d3affb73efd2ec6c36ad3112dd933efed63c4e1cbffcfa88e2759c144f2d8"},
|
2018-01-07 01:53:37 +08:00
|
|
|
} {
|
|
|
|
list.SetOutput(nil)
|
|
|
|
list.AddHash(test.ht)
|
2018-05-13 17:55:18 +08:00
|
|
|
got := list.Format(items[0])
|
2018-01-11 21:52:15 +08:00
|
|
|
if got != "UNSUPPORTED" && got != "" {
|
2018-01-07 01:53:37 +08:00
|
|
|
assert.Equal(t, test.want, got)
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 19:15:05 +08:00
|
|
|
|
|
|
|
list.SetOutput(nil)
|
|
|
|
list.SetSeparator("|")
|
|
|
|
list.SetCSV(true)
|
|
|
|
list.AddSize()
|
|
|
|
list.AddPath()
|
|
|
|
list.AddModTime()
|
|
|
|
list.SetDirSlash(true)
|
|
|
|
assert.Equal(t, "1|a|"+items[0].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[0]))
|
|
|
|
assert.Equal(t, fmt.Sprintf("%d", items[1].Size())+"|subdir/|"+items[1].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[1]))
|
|
|
|
|
2018-01-06 22:39:31 +08:00
|
|
|
}
|