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.
|
|
|
|
//
|
|
|
|
// Call fs.Stats.ResetCounters() before every fs.Sync() as it uses the
|
|
|
|
// error count internally.
|
2014-08-02 00:58:39 +08:00
|
|
|
|
|
|
|
package fs_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-02-07 23:52:06 +08:00
|
|
|
"path/filepath"
|
2014-08-02 00:58:39 +08:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2016-01-03 22:12:01 +08:00
|
|
|
_ "github.com/ncw/rclone/fs/all" // import all fs
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
2016-01-13 01:38:28 +08:00
|
|
|
RemoteName = flag.String("remote", "", "Remote to test with, defaults to local filesystem")
|
|
|
|
SubDir = flag.Bool("subdir", false, "Set to test with a sub directory")
|
|
|
|
Verbose = flag.Bool("verbose", false, "Set to enable logging")
|
|
|
|
DumpHeaders = flag.Bool("dump-headers", false, "Set to dump headers (needs -verbose)")
|
|
|
|
DumpBodies = flag.Bool("dump-bodies", false, "Set to dump bodies (needs -verbose)")
|
|
|
|
Individual = flag.Bool("individual", false, "Make individual bucket/container/directory for each test - much slower")
|
|
|
|
LowLevelRetries = flag.Int("low-level-retries", 10, "Number of low level retries")
|
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) {
|
|
|
|
flag.Parse()
|
|
|
|
if !*Individual {
|
|
|
|
oneRun = newRun()
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
rc := m.Run()
|
|
|
|
if !*Individual {
|
|
|
|
oneRun.Finalise()
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
os.Exit(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run holds the remotes for a test run
|
|
|
|
type Run struct {
|
|
|
|
localName string
|
|
|
|
flocal fs.Fs
|
|
|
|
fremote fs.Fs
|
|
|
|
cleanRemote func()
|
|
|
|
mkdir map[string]bool // whether the remote has been made yet for the fs name
|
|
|
|
Logf, Fatalf func(text string, args ...interface{})
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// oneRun holds the master run data if individual is not set
|
|
|
|
var oneRun *Run
|
|
|
|
|
|
|
|
// newRun initialise the remote and local for testing and returns a
|
|
|
|
// run object.
|
|
|
|
//
|
|
|
|
// r.flocal is an empty local Fs
|
|
|
|
// r.fremote is an empty remote Fs
|
|
|
|
//
|
|
|
|
// Finalise() will tidy them away when done.
|
|
|
|
func newRun() *Run {
|
|
|
|
r := &Run{
|
|
|
|
Logf: log.Printf,
|
|
|
|
Fatalf: log.Fatalf,
|
|
|
|
mkdir: make(map[string]bool),
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-02-16 23:25:27 +08:00
|
|
|
// Never ask for passwords, fail instead.
|
|
|
|
// If your local config is encrypted set environment variable
|
|
|
|
// "RCLONE_CONFIG_PASS=hunter2" (or your password)
|
|
|
|
*fs.AskPassword = false
|
2014-08-02 00:58:39 +08:00
|
|
|
fs.LoadConfig()
|
2015-03-05 01:59:31 +08:00
|
|
|
fs.Config.Verbose = *Verbose
|
|
|
|
fs.Config.Quiet = !*Verbose
|
2015-12-29 08:17:39 +08:00
|
|
|
fs.Config.DumpHeaders = *DumpHeaders
|
|
|
|
fs.Config.DumpBodies = *DumpBodies
|
2016-01-13 01:38:28 +08:00
|
|
|
fs.Config.LowLevelRetries = *LowLevelRetries
|
2014-08-02 00:58:39 +08:00
|
|
|
var err error
|
2016-01-17 18:08:28 +08:00
|
|
|
r.fremote, r.cleanRemote, err = fstest.RandomRemote(*RemoteName, *SubDir)
|
2014-08-02 00:58:39 +08:00
|
|
|
if err != nil {
|
2016-01-17 18:08:28 +08:00
|
|
|
r.Fatalf("Failed to open remote %q: %v", *RemoteName, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
r.localName, err = ioutil.TempDir("", "rclone")
|
2014-08-02 00:58:39 +08:00
|
|
|
if err != nil {
|
2016-01-17 18:08:28 +08:00
|
|
|
r.Fatalf("Failed to create temp dir: %v", err)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
r.localName = filepath.ToSlash(r.localName)
|
|
|
|
r.flocal, err = fs.NewFs(r.localName)
|
2014-08-02 00:58:39 +08:00
|
|
|
if err != nil {
|
2016-01-17 18:08:28 +08:00
|
|
|
r.Fatalf("Failed to make %q: %v", r.localName, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
fs.CalculateModifyWindow(r.fremote, r.flocal)
|
|
|
|
return r
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// NewRun initialise the remote and local for testing and returns a
|
|
|
|
// run object. Call this from the tests.
|
|
|
|
//
|
|
|
|
// r.flocal is an empty local Fs
|
|
|
|
// r.fremote is an empty remote Fs
|
|
|
|
//
|
|
|
|
// Finalise() will tidy them away when done.
|
|
|
|
func NewRun(t *testing.T) *Run {
|
|
|
|
var r *Run
|
|
|
|
if *Individual {
|
|
|
|
r = newRun()
|
|
|
|
} else {
|
|
|
|
// If not individual, use the global one with the clean method overridden
|
|
|
|
r = new(Run)
|
|
|
|
*r = *oneRun
|
|
|
|
r.cleanRemote = func() {
|
2016-04-24 04:46:52 +08:00
|
|
|
list := fs.NewLister().Start(r.fremote, "")
|
2016-04-22 03:06:21 +08:00
|
|
|
for {
|
|
|
|
o, err := list.GetObject()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error listing: %v", err)
|
|
|
|
}
|
|
|
|
// Check if we are finished
|
|
|
|
if o == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = o.Remove()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error removing file: %v", err)
|
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
}
|
2016-01-24 20:37:19 +08:00
|
|
|
// Check remote is empty
|
|
|
|
fstest.CheckItems(t, r.fremote)
|
2016-01-17 18:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Logf = t.Logf
|
|
|
|
r.Fatalf = t.Fatalf
|
|
|
|
r.Logf("Remote %q, Local %q, Modify Window %q", r.fremote, r.flocal, fs.Config.ModifyWindow)
|
|
|
|
return r
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
// Write a file to local
|
|
|
|
func (r *Run) WriteFile(filePath, content string, t time.Time) fstest.Item {
|
|
|
|
item := fstest.NewItem(filePath, content, t)
|
|
|
|
// FIXME make directories?
|
|
|
|
filePath = path.Join(r.localName, filePath)
|
|
|
|
dirPath := path.Dir(filePath)
|
|
|
|
err := os.MkdirAll(dirPath, 0770)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to make directories %q: %v", dirPath, err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(filePath, []byte(content), 0600)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to write file %q: %v", filePath, err)
|
|
|
|
}
|
|
|
|
err = os.Chtimes(filePath, t, t)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to chtimes file %q: %v", filePath, err)
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteObjectTo writes an object to the fs, remote passed in
|
2016-06-12 22:06:27 +08:00
|
|
|
func (r *Run) WriteObjectTo(f fs.Fs, remote, content string, modTime time.Time, useUnchecked bool) fstest.Item {
|
|
|
|
put := f.Put
|
|
|
|
if useUnchecked {
|
|
|
|
if fPutUnchecked, ok := f.(fs.PutUncheckeder); ok {
|
|
|
|
put = fPutUnchecked.PutUnchecked
|
|
|
|
} else {
|
|
|
|
r.Fatalf("Fs doesn't support PutUnchecked")
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
const maxTries = 5
|
|
|
|
if !r.mkdir[f.String()] {
|
|
|
|
err := f.Mkdir()
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to mkdir %q: %v", f, err)
|
|
|
|
}
|
|
|
|
r.mkdir[f.String()] = true
|
|
|
|
}
|
|
|
|
for tries := 1; ; tries++ {
|
|
|
|
in := bytes.NewBufferString(content)
|
2016-02-18 19:35:25 +08:00
|
|
|
objinfo := fs.NewStaticObjectInfo(remote, modTime, int64(len(content)), true, nil, nil)
|
2016-06-12 22:06:27 +08:00
|
|
|
_, err := put(in, objinfo)
|
2016-01-17 18:08:28 +08:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Retry if err returned a retry error
|
|
|
|
if retry, ok := err.(fs.Retry); ok && retry.Retry() && tries < maxTries {
|
|
|
|
r.Logf("Retry Put of %q to %v: %d/%d (%v)", remote, f, tries, maxTries, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.Fatalf("Failed to put %q to %q: %v", remote, f, err)
|
|
|
|
}
|
|
|
|
return fstest.NewItem(remote, content, modTime)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// WriteObject writes an object to the remote
|
|
|
|
func (r *Run) WriteObject(remote, content string, modTime time.Time) fstest.Item {
|
2016-06-12 22:06:27 +08:00
|
|
|
return r.WriteObjectTo(r.fremote, remote, content, modTime, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteUncheckedObject writes an object to the remote not checking for duplicates
|
|
|
|
func (r *Run) WriteUncheckedObject(remote, content string, modTime time.Time) fstest.Item {
|
|
|
|
return r.WriteObjectTo(r.fremote, remote, content, modTime, true)
|
2016-01-17 18:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBoth calls WriteObject and WriteFile with the same arguments
|
|
|
|
func (r *Run) WriteBoth(remote, content string, modTime time.Time) fstest.Item {
|
|
|
|
r.WriteFile(remote, content, modTime)
|
|
|
|
return r.WriteObject(remote, content, modTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean the temporary directory
|
|
|
|
func (r *Run) cleanTempDir() {
|
|
|
|
err := os.RemoveAll(r.localName)
|
|
|
|
if err != nil {
|
|
|
|
r.Logf("Failed to clean temporary directory %q: %v", r.localName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finalise cleans the remote and local
|
|
|
|
func (r *Run) Finalise() {
|
|
|
|
// r.Logf("Cleaning remote %q", r.fremote)
|
|
|
|
r.cleanRemote()
|
|
|
|
// r.Logf("Cleaning local %q", r.localName)
|
|
|
|
r.cleanTempDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestMkdir(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
fstest.TestMkdir(t, r.fremote)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check dry run is working
|
|
|
|
func TestCopyWithDryRun(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
|
|
|
fs.Config.DryRun = true
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.CopyDir(r.fremote, r.flocal)
|
2014-08-02 00:58:39 +08:00
|
|
|
fs.Config.DryRun = false
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now without dry run
|
|
|
|
func TestCopy(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
|
|
|
|
|
|
|
err := fs.CopyDir(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-06-25 21:28:26 +08:00
|
|
|
// Now with --no-traverse
|
|
|
|
func TestCopyNoTraverse(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
fs.Config.NoTraverse = true
|
|
|
|
defer func() { fs.Config.NoTraverse = false }()
|
|
|
|
|
|
|
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
|
|
|
|
|
|
|
err := fs.CopyDir(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-06-25 21:28:26 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now with --no-traverse
|
|
|
|
func TestSyncNoTraverse(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
fs.Config.NoTraverse = true
|
|
|
|
defer func() { fs.Config.NoTraverse = false }()
|
|
|
|
|
|
|
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
|
|
|
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-06-25 21:28:26 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
}
|
|
|
|
|
2016-06-03 04:02:44 +08:00
|
|
|
// Test copy with depth
|
|
|
|
func TestCopyWithDepth(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("sub dir/hello world", "hello world", t1)
|
|
|
|
file2 := r.WriteFile("hello world2", "hello world2", t2)
|
|
|
|
|
|
|
|
// Check the MaxDepth too
|
|
|
|
fs.Config.MaxDepth = 1
|
|
|
|
defer func() { fs.Config.MaxDepth = -1 }()
|
|
|
|
|
|
|
|
err := fs.CopyDir(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-06-03 04:02:44 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file2)
|
|
|
|
}
|
|
|
|
|
2015-02-15 02:48:08 +08:00
|
|
|
// Test a server side copy if possible, or the backup path if not
|
|
|
|
func TestServerSideCopy(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("sub dir/hello world", "hello world", t1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
|
2015-02-15 02:48:08 +08:00
|
|
|
fremoteCopy, finaliseCopy, err := fstest.RandomRemote(*RemoteName, *SubDir)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-02-15 02:48:08 +08:00
|
|
|
defer finaliseCopy()
|
2016-01-17 18:08:28 +08:00
|
|
|
t.Logf("Server side copy (if possible) %v -> %v", r.fremote, fremoteCopy)
|
2015-02-15 02:48:08 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
err = fs.CopyDir(fremoteCopy, r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-02-15 02:48:08 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, fremoteCopy, file1)
|
2015-02-15 02:48:08 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestLsd(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("sub dir/hello world", "hello world", t1)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.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
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Check that if the local file doesn't exist when we copy it up,
|
|
|
|
// nothing happens to the remote file
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestCopyAfterDelete(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("sub dir/hello world", "hello world", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
err := fs.Mkdir(r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-22 03:06:21 +08:00
|
|
|
|
|
|
|
err = fs.CopyDir(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Check the copy downloading a file
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestCopyRedownload(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("sub dir/hello world", "hello world", t1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
|
|
|
|
err := fs.CopyDir(r.flocal, r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:08:27 +08:00
|
|
|
// Create a file and sync it. Change the last modified date and resync.
|
|
|
|
// If we're only doing sync by size and checksum, we expect nothing to
|
|
|
|
// to be transferred on the second sync.
|
|
|
|
func TestSyncBasedOnCheckSum(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
2015-06-03 22:08:27 +08:00
|
|
|
fs.Config.CheckSum = true
|
2015-06-06 15:38:45 +08:00
|
|
|
defer func() { fs.Config.CheckSum = false }()
|
2015-06-03 22:08:27 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
file1 := r.WriteFile("check sum", "", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
2015-06-03 22:08:27 +08:00
|
|
|
|
2015-06-06 15:38:45 +08:00
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-06-03 22:08:27 +08:00
|
|
|
|
2015-06-06 15:38:45 +08:00
|
|
|
// We should have transferred exactly one file.
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Change last modified date only
|
|
|
|
file2 := r.WriteFile("check sum", "", t2)
|
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
2015-06-03 22:08:27 +08:00
|
|
|
|
2015-06-06 15:38:45 +08:00
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err = fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-06-03 22:08:27 +08:00
|
|
|
|
2015-06-06 15:38:45 +08:00
|
|
|
// We should have transferred no files
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2015-06-06 15:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and sync it. Change the last modified date and the
|
|
|
|
// file contents but not the size. If we're only doing sync by size
|
|
|
|
// only, we expect nothing to to be transferred on the second sync.
|
|
|
|
func TestSyncSizeOnly(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
2015-06-06 15:38:45 +08:00
|
|
|
fs.Config.SizeOnly = true
|
|
|
|
defer func() { fs.Config.SizeOnly = false }()
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
file1 := r.WriteFile("sizeonly", "potato", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
|
|
|
// We should have transferred exactly one file.
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
|
|
|
// Update mtime, md5sum but not length of file
|
2016-01-17 18:08:28 +08:00
|
|
|
file2 := r.WriteFile("sizeonly", "POTATO", t2)
|
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
2016-06-18 00:20:08 +08:00
|
|
|
err = fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-06-18 00:20:08 +08:00
|
|
|
|
|
|
|
// We should have transferred no files
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
|
2016-06-18 00:20:08 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and sync it. Keep the last modified date but change
|
|
|
|
// the size. With --ignore-size we expect nothing to to be
|
|
|
|
// transferred on the second sync.
|
|
|
|
func TestSyncIgnoreSize(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
fs.Config.IgnoreSize = true
|
|
|
|
defer func() { fs.Config.IgnoreSize = false }()
|
|
|
|
|
|
|
|
file1 := r.WriteFile("ignore-size", "contents", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-06-18 00:20:08 +08:00
|
|
|
|
|
|
|
// We should have transferred exactly one file.
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
|
2016-06-18 00:20:08 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
|
|
|
|
// Update size but not date of file
|
|
|
|
file2 := r.WriteFile("ignore-size", "longer contents but same date", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err = fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-06-06 15:38:45 +08:00
|
|
|
|
|
|
|
// We should have transferred no files
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2016-03-23 01:02:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncIgnoreTimes(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("existing", "potato", t1)
|
2016-04-19 00:48:52 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2016-03-23 01:02:27 +08:00
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-23 01:02:27 +08:00
|
|
|
|
|
|
|
// We should have transferred exactly 0 files because the
|
|
|
|
// files were identical.
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(0), fs.Stats.GetTransfers())
|
2016-03-23 01:02:27 +08:00
|
|
|
|
|
|
|
fs.Config.IgnoreTimes = true
|
|
|
|
defer func() { fs.Config.IgnoreTimes = false }()
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err = fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-23 01:02:27 +08:00
|
|
|
|
|
|
|
// We should have transferred exactly one file even though the
|
|
|
|
// files were identical.
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, int64(1), fs.Stats.GetTransfers())
|
2016-03-23 01:02:27 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2015-06-03 22:08:27 +08:00
|
|
|
}
|
|
|
|
|
2016-01-05 18:35:36 +08:00
|
|
|
func TestSyncIgnoreExisting(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("existing", "potato", t1)
|
|
|
|
|
2016-01-05 18:35:36 +08:00
|
|
|
fs.Config.IgnoreExisting = true
|
|
|
|
defer func() { fs.Config.IgnoreExisting = false }()
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2016-01-05 18:35:36 +08:00
|
|
|
|
|
|
|
// Change everything
|
2016-01-17 18:08:28 +08:00
|
|
|
r.WriteFile("existing", "newpotatoes", t2)
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err = fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-05 18:35:36 +08:00
|
|
|
// Items should not change
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2016-01-05 18:35:36 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestSyncAfterChangingModtimeOnly(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("empty space", "", t2)
|
|
|
|
r.WriteObject("empty space", "", t1)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncAfterAddingAFile(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("empty space", "", t2)
|
|
|
|
file2 := r.WriteFile("potato", "------------------------------------------------------------", t3)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncAfterChangingFilesSizeOnly(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteObject("potato", "------------------------------------------------------------", t3)
|
|
|
|
file2 := r.WriteFile("potato", "smaller but same date", t3)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file2)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Sync after changing a file's contents, changing modtime but length
|
|
|
|
// remaining the same
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestSyncAfterChangingContentsOnly(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
var file1 fstest.Item
|
|
|
|
if r.fremote.Precision() == fs.ModTimeNotSupported {
|
2015-09-10 06:21:50 +08:00
|
|
|
t.Logf("ModTimeNotSupported so forcing file to be a different size")
|
2016-01-17 18:08:28 +08:00
|
|
|
file1 = r.WriteObject("potato", "different size to make sure it syncs", t3)
|
|
|
|
} else {
|
|
|
|
file1 = r.WriteObject("potato", "smaller but same date", t3)
|
2015-09-10 06:21:50 +08:00
|
|
|
}
|
2016-01-17 18:08:28 +08:00
|
|
|
file2 := r.WriteFile("potato", "SMALLER BUT SAME DATE", t2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file2)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync after removing a file and adding a file --dry-run
|
|
|
|
func TestSyncAfterRemovingAFileAndAddingAFileDryRun(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteObject("potato", "SMALLER BUT SAME DATE", t2)
|
|
|
|
file3 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
fs.Config.DryRun = true
|
2016-01-17 18:08:28 +08:00
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2014-08-02 00:58:39 +08:00
|
|
|
fs.Config.DryRun = false
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file3, file1)
|
|
|
|
fstest.CheckItems(t, r.fremote, file3, file2)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync after removing a file and adding a file
|
|
|
|
func TestSyncAfterRemovingAFileAndAddingAFile(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteObject("potato", "SMALLER BUT SAME DATE", t2)
|
|
|
|
file3 := r.WriteBoth("empty space", "", t2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file2, file3)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file3)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1, file3)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file3)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-24 02:27:00 +08:00
|
|
|
// Sync after removing a file and adding a file with IO Errors
|
|
|
|
func TestSyncAfterRemovingAFileAndAddingAFileWithErrors(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteObject("potato", "SMALLER BUT SAME DATE", t2)
|
|
|
|
file3 := r.WriteBoth("empty space", "", t2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file2, file3)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file3)
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
fs.Stats.Error()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, fs.ErrorNotDeleting, err)
|
2016-01-24 02:27:00 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file1, file3)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2, file3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync test delete during
|
|
|
|
func TestSyncDeleteDuring(t *testing.T) {
|
|
|
|
// This is the default so we've checked this already
|
|
|
|
// check it is the default
|
|
|
|
if !(!fs.Config.DeleteBefore && fs.Config.DeleteDuring && !fs.Config.DeleteAfter) {
|
|
|
|
t.Fatalf("Didn't default to --delete-during")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync test delete before
|
|
|
|
func TestSyncDeleteBefore(t *testing.T) {
|
|
|
|
fs.Config.DeleteBefore = true
|
|
|
|
fs.Config.DeleteDuring = false
|
|
|
|
fs.Config.DeleteAfter = false
|
|
|
|
defer func() {
|
|
|
|
fs.Config.DeleteBefore = false
|
|
|
|
fs.Config.DeleteDuring = true
|
|
|
|
fs.Config.DeleteAfter = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
TestSyncAfterRemovingAFileAndAddingAFile(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync test delete after
|
|
|
|
func TestSyncDeleteAfter(t *testing.T) {
|
|
|
|
fs.Config.DeleteBefore = false
|
|
|
|
fs.Config.DeleteDuring = false
|
|
|
|
fs.Config.DeleteAfter = true
|
|
|
|
defer func() {
|
|
|
|
fs.Config.DeleteBefore = false
|
|
|
|
fs.Config.DeleteDuring = true
|
|
|
|
fs.Config.DeleteAfter = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
TestSyncAfterRemovingAFileAndAddingAFile(t)
|
|
|
|
}
|
|
|
|
|
2015-09-27 23:13:20 +08:00
|
|
|
// Test with exclude
|
|
|
|
func TestSyncWithExclude(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
file3 := r.WriteFile("enormous", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 100 bytes
|
|
|
|
|
2015-11-12 19:46:04 +08:00
|
|
|
fs.Config.Filter.MaxSize = 40
|
2015-09-27 23:13:20 +08:00
|
|
|
defer func() {
|
2016-06-04 04:54:27 +08:00
|
|
|
fs.Config.Filter.MaxSize = -1
|
2015-09-27 23:13:20 +08:00
|
|
|
}()
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file2, file1)
|
2015-11-12 19:46:04 +08:00
|
|
|
|
|
|
|
// Now sync the other way round and check enormous doesn't get
|
|
|
|
// deleted as it is excluded from the sync
|
2016-01-17 18:08:28 +08:00
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err = fs.Sync(r.flocal, r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2, file1, file3)
|
2015-09-27 23:13:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with exclude and delete excluded
|
2016-01-17 18:08:28 +08:00
|
|
|
func TestSyncWithExcludeAndDeleteExcluded(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1) // 60 bytes
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
file3 := r.WriteBoth("enormous", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 100 bytes
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2, file3)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file2, file3)
|
|
|
|
|
2015-09-27 23:13:20 +08:00
|
|
|
fs.Config.Filter.MaxSize = 40
|
|
|
|
fs.Config.Filter.DeleteExcluded = true
|
2016-01-17 18:08:28 +08:00
|
|
|
defer func() {
|
2016-06-04 04:54:27 +08:00
|
|
|
fs.Config.Filter.MaxSize = -1
|
2015-09-27 23:13:20 +08:00
|
|
|
fs.Config.Filter.DeleteExcluded = false
|
2016-01-17 18:08:28 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file2)
|
2015-09-27 23:13:20 +08:00
|
|
|
|
2015-11-12 19:46:04 +08:00
|
|
|
// Check sync the other way round to make sure enormous gets
|
|
|
|
// deleted even though it is excluded
|
2016-01-17 18:08:28 +08:00
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err = fs.Sync(r.flocal, r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.flocal, file2)
|
2015-09-27 23:13:20 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 01:46:40 +08:00
|
|
|
// Test with UpdateOlder set
|
|
|
|
func TestSyncWithUpdateOlder(t *testing.T) {
|
2016-03-01 15:26:33 +08:00
|
|
|
if fs.Config.ModifyWindow == fs.ModTimeNotSupported {
|
|
|
|
t.Skip("Can't run this test on fs which doesn't support mod time")
|
|
|
|
}
|
2016-03-01 01:46:40 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
t2plus := t2.Add(time.Second / 2)
|
|
|
|
t2minus := t2.Add(time.Second / 2)
|
|
|
|
oneF := r.WriteFile("one", "one", t1)
|
|
|
|
twoF := r.WriteFile("two", "two", t3)
|
|
|
|
threeF := r.WriteFile("three", "three", t2)
|
|
|
|
fourF := r.WriteFile("four", "four", t2)
|
|
|
|
fiveF := r.WriteFile("five", "five", t2)
|
|
|
|
fstest.CheckItems(t, r.flocal, oneF, twoF, threeF, fourF, fiveF)
|
|
|
|
oneO := r.WriteObject("one", "ONE", t2)
|
|
|
|
twoO := r.WriteObject("two", "TWO", t2)
|
|
|
|
threeO := r.WriteObject("three", "THREE", t2plus)
|
|
|
|
fourO := r.WriteObject("four", "FOURFOUR", t2minus)
|
|
|
|
fstest.CheckItems(t, r.fremote, oneO, twoO, threeO, fourO)
|
|
|
|
|
|
|
|
fs.Config.UpdateOlder = true
|
|
|
|
oldModifyWindow := fs.Config.ModifyWindow
|
|
|
|
fs.Config.ModifyWindow = fs.ModTimeNotSupported
|
|
|
|
defer func() {
|
|
|
|
fs.Config.UpdateOlder = false
|
|
|
|
fs.Config.ModifyWindow = oldModifyWindow
|
|
|
|
}()
|
|
|
|
|
|
|
|
fs.Stats.ResetCounters()
|
|
|
|
err := fs.Sync(r.fremote, r.flocal)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-01 01:46:40 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, oneO, twoF, threeO, fourF, fiveF)
|
|
|
|
}
|
|
|
|
|
2015-08-25 04:42:23 +08:00
|
|
|
// Test a server side move if possible, or the backup path if not
|
|
|
|
func TestServerSideMove(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file2, file1)
|
|
|
|
|
2015-08-25 04:42:23 +08:00
|
|
|
fremoteMove, finaliseMove, err := fstest.RandomRemote(*RemoteName, *SubDir)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-08-25 04:42:23 +08:00
|
|
|
defer finaliseMove()
|
2016-01-17 18:08:28 +08:00
|
|
|
t.Logf("Server side move (if possible) %v -> %v", r.fremote, fremoteMove)
|
2015-08-25 04:42:23 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
// Write just one file in the new remote
|
2016-06-12 22:06:27 +08:00
|
|
|
r.WriteObjectTo(fremoteMove, "empty space", "", t2, false)
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, fremoteMove, file2)
|
2015-08-25 04:42:23 +08:00
|
|
|
|
|
|
|
// Do server side move
|
2016-02-26 04:05:34 +08:00
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err = fs.MoveDir(fremoteMove, r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-08-25 04:42:23 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote)
|
|
|
|
fstest.CheckItems(t, fremoteMove, file2, file1)
|
2015-08-25 04:42:23 +08:00
|
|
|
|
|
|
|
// Move it back again, dst does not exist this time
|
2016-02-26 04:05:34 +08:00
|
|
|
fs.Stats.ResetCounters()
|
2016-01-17 18:08:28 +08:00
|
|
|
err = fs.MoveDir(r.fremote, fremoteMove)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-08-25 04:42:23 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file2, file1)
|
|
|
|
fstest.CheckItems(t, fremoteMove)
|
2015-08-25 04:42:23 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestLs(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2)
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.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
|
|
|
}
|
|
|
|
|
|
|
|
func TestLsLong(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2)
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.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"
|
2016-01-17 18:08:28 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMd5sum(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2)
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
var buf bytes.Buffer
|
2016-01-17 18:08:28 +08:00
|
|
|
err := fs.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)
|
|
|
|
}
|
2015-08-17 06:24:34 +08:00
|
|
|
if !strings.Contains(res, "6548b156ea68a4e003e786df99eee76 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
func TestSha1sum(t *testing.T) {
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteBoth("potato2", "------------------------------------------------------------", t1)
|
|
|
|
file2 := r.WriteBoth("empty space", "", t2)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := fs.Sha1sum(r.fremote, &buf)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-01-17 18:08:28 +08:00
|
|
|
res := buf.String()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 02:48:48 +08:00
|
|
|
func TestCount(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
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
|
|
|
|
2016-06-03 04:02:44 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2, file3)
|
|
|
|
|
|
|
|
// Check the MaxDepth too
|
|
|
|
fs.Config.MaxDepth = 1
|
|
|
|
defer func() { fs.Config.MaxDepth = -1 }()
|
2016-01-17 18:08:28 +08:00
|
|
|
|
|
|
|
objects, size, err := fs.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) {
|
|
|
|
r := NewRun(t)
|
|
|
|
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
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2, file3)
|
|
|
|
|
|
|
|
fs.Config.Filter.MaxSize = 60
|
|
|
|
defer func() {
|
2016-06-04 04:54:27 +08:00
|
|
|
fs.Config.Filter.MaxSize = -1
|
2015-12-03 06:25:32 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
err := fs.Delete(r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2015-12-03 06:25:32 +08:00
|
|
|
fstest.CheckItems(t, r.fremote, file3)
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestCheck(t *testing.T) {
|
2016-01-17 18:08:28 +08:00
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
check := func(i int, wantErrors int64) {
|
|
|
|
fs.Debug(r.fremote, "%d: Starting check test", i)
|
|
|
|
oldErrors := fs.Stats.GetErrors()
|
|
|
|
err := fs.Check(r.flocal, r.fremote)
|
|
|
|
gotErrors := fs.Stats.GetErrors() - oldErrors
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
fs.Debug(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)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1)
|
|
|
|
check(1, 0)
|
|
|
|
|
|
|
|
file2 := r.WriteFile("potato2", "------------------------------------------------------------", t1)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file2)
|
|
|
|
check(2, 1)
|
|
|
|
|
|
|
|
file3 := r.WriteObject("empty space", "", t2)
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file3)
|
|
|
|
check(3, 2)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
fstest.CheckItems(t, r.fremote, file1, file2r, file3)
|
2016-01-17 18:08:28 +08:00
|
|
|
check(4, 1)
|
2014-08-02 00:58:39 +08:00
|
|
|
|
2016-01-17 18:08:28 +08:00
|
|
|
r.WriteFile("empty space", "", t2)
|
|
|
|
fstest.CheckItems(t, r.flocal, file1, file2, file3)
|
|
|
|
check(5, 0)
|
2014-08-02 00:58:39 +08:00
|
|
|
}
|
2016-03-06 00:10:51 +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-03-06 00:10:51 +08:00
|
|
|
func (r *Run) checkWithDuplicates(t *testing.T, items ...fstest.Item) {
|
|
|
|
objects, size, err := fs.Count(r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(len(items)), objects)
|
2016-03-06 00:10:51 +08:00
|
|
|
wantSize := int64(0)
|
|
|
|
for _, item := range items {
|
|
|
|
wantSize += item.Size
|
|
|
|
}
|
2016-06-30 00:59:31 +08:00
|
|
|
assert.Equal(t, wantSize, size)
|
2016-03-06 00:10:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateInteractive(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file3 := r.WriteUncheckedObject("one", "This is one", t1)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateInteractive)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateSkip(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file3 := r.WriteUncheckedObject("one", "This is another one", t1)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateSkip)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
|
|
|
r.checkWithDuplicates(t, file1, file3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateFirst(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one", "This is one A", t1)
|
|
|
|
file3 := r.WriteUncheckedObject("one", "This is one BB", t1)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateFirst)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
|
|
|
objects, size, err := fs.Count(r.fremote)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, objects)
|
2016-03-06 00:10:51 +08:00
|
|
|
if size != file1.Size && size != file2.Size && size != file3.Size {
|
|
|
|
t.Errorf("Size not one of the object sizes %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateNewest(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one", "This is one too", t2)
|
|
|
|
file3 := r.WriteUncheckedObject("one", "This is another one", t3)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateNewest)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateOldest(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one", "This is one too", t2)
|
|
|
|
file3 := r.WriteUncheckedObject("one", "This is another one", t3)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateOldest)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeduplicateRename(t *testing.T) {
|
|
|
|
if *RemoteName != "TestDrive:" {
|
|
|
|
t.Skip("Can only test deduplicate on google drive")
|
|
|
|
}
|
|
|
|
r := NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
file1 := r.WriteUncheckedObject("one.txt", "This is one", t1)
|
|
|
|
file2 := r.WriteUncheckedObject("one.txt", "This is one too", t2)
|
|
|
|
file3 := r.WriteUncheckedObject("one.txt", "This is another one", t3)
|
2016-03-06 00:10:51 +08:00
|
|
|
r.checkWithDuplicates(t, file1, file2, file3)
|
|
|
|
|
|
|
|
err := fs.Deduplicate(r.fremote, fs.DeduplicateRename)
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-06 00:10:51 +08:00
|
|
|
|
2016-04-24 04:46:52 +08:00
|
|
|
list := fs.NewLister().Start(r.fremote, "")
|
2016-04-22 03:06:21 +08:00
|
|
|
for {
|
|
|
|
o, err := list.GetObject()
|
2016-06-30 00:59:31 +08:00
|
|
|
require.NoError(t, err)
|
2016-04-22 03:06:21 +08:00
|
|
|
// Check if we are finished
|
|
|
|
if o == nil {
|
|
|
|
break
|
|
|
|
}
|
2016-03-06 00:10:51 +08:00
|
|
|
remote := o.Remote()
|
|
|
|
if remote != "one-1.txt" &&
|
|
|
|
remote != "one-2.txt" &&
|
|
|
|
remote != "one-3.txt" {
|
|
|
|
t.Errorf("Bad file name after rename %q", remote)
|
|
|
|
}
|
|
|
|
size := o.Size()
|
|
|
|
if size != file1.Size && size != file2.Size && size != file3.Size {
|
|
|
|
t.Errorf("Size not one of the object sizes %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|