2017-10-29 12:23:10 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
This provides Run for use in creating test suites
|
|
|
|
|
|
|
|
To use this declare a TestMain
|
|
|
|
|
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fstest.TestMain(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
And then make and destroy a Run in each test
|
|
|
|
|
|
|
|
func TestMkdir(t *testing.T) {
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
defer r.Finalise()
|
|
|
|
// test stuff
|
|
|
|
}
|
|
|
|
|
|
|
|
This will make r.Fremote and r.Flocal for a remote remote and a local
|
|
|
|
remote. The remote is determined by the -remote flag passed in.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
package fstest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2017-10-29 12:23:10 +00:00
|
|
|
"flag"
|
2018-01-25 12:03:39 +00:00
|
|
|
"fmt"
|
2017-10-29 12:23:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-08-22 10:48:17 +00:00
|
|
|
"github.com/rclone/rclone/fs/cache"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2019-09-16 09:59:01 +00:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/object"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2021-06-10 22:46:36 +00:00
|
|
|
"github.com/rclone/rclone/lib/file"
|
2018-01-25 12:03:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-10-29 12:23:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run holds the remotes for a test run
|
|
|
|
type Run struct {
|
|
|
|
LocalName string
|
|
|
|
Flocal fs.Fs
|
|
|
|
Fremote fs.Fs
|
|
|
|
FremoteName string
|
|
|
|
cleanRemote func()
|
|
|
|
mkdir map[string]bool // whether the remote has been made yet for the fs name
|
|
|
|
Logf, Fatalf func(text string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMain drives the tests
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
if !*Individual {
|
|
|
|
oneRun = newRun()
|
|
|
|
}
|
|
|
|
rc := m.Run()
|
|
|
|
if !*Individual {
|
|
|
|
oneRun.Finalise()
|
|
|
|
}
|
|
|
|
os.Exit(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
}
|
|
|
|
|
|
|
|
Initialise()
|
|
|
|
|
|
|
|
var err error
|
2019-08-08 18:58:02 +00:00
|
|
|
r.Fremote, r.FremoteName, r.cleanRemote, err = RandomRemote()
|
2017-10-29 12:23:10 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to open remote %q: %v", *RemoteName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.LocalName, err = ioutil.TempDir("", "rclone")
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
r.LocalName = filepath.ToSlash(r.LocalName)
|
2020-11-05 15:18:51 +00:00
|
|
|
r.Flocal, err = fs.NewFs(context.Background(), r.LocalName)
|
2017-10-29 12:23:10 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to make %q: %v", r.LocalName, err)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-04-09 18:26:27 +00:00
|
|
|
// run f(), retrying it until it returns with no error or the limit
|
|
|
|
// expires and it calls t.Fatalf
|
|
|
|
func retry(t *testing.T, what string, f func() error) {
|
|
|
|
var err error
|
|
|
|
for try := 1; try <= *ListRetries; try++ {
|
|
|
|
err = f()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("%s failed - try %d/%d: %v", what, try, *ListRetries, err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
t.Logf("%s failed: %v", what, err)
|
|
|
|
}
|
2017-10-29 12:23:10 +00:00
|
|
|
|
2018-11-14 17:14:02 +00:00
|
|
|
// newRunIndividual initialise the remote and local for testing and
|
|
|
|
// returns a run object. Pass in true to make individual tests or
|
|
|
|
// false to use the global one.
|
2017-10-29 12:23:10 +00:00
|
|
|
//
|
|
|
|
// r.Flocal is an empty local Fs
|
|
|
|
// r.Fremote is an empty remote Fs
|
|
|
|
//
|
|
|
|
// Finalise() will tidy them away when done.
|
2018-11-14 17:14:02 +00:00
|
|
|
func newRunIndividual(t *testing.T, individual bool) *Run {
|
2019-06-17 08:34:30 +00:00
|
|
|
ctx := context.Background()
|
2017-10-29 12:23:10 +00:00
|
|
|
var r *Run
|
2018-11-14 17:14:02 +00:00
|
|
|
if individual {
|
2017-10-29 12:23:10 +00:00
|
|
|
r = newRun()
|
|
|
|
} else {
|
|
|
|
// If not individual, use the global one with the clean method overridden
|
|
|
|
r = new(Run)
|
|
|
|
*r = *oneRun
|
|
|
|
r.cleanRemote = func() {
|
2018-04-09 18:26:27 +00:00
|
|
|
var toDelete []string
|
2019-06-17 08:34:30 +00:00
|
|
|
err := walk.ListR(ctx, r.Fremote, "", true, -1, walk.ListAll, func(entries fs.DirEntries) error {
|
2017-10-29 12:23:10 +00:00
|
|
|
for _, entry := range entries {
|
|
|
|
switch x := entry.(type) {
|
|
|
|
case fs.Object:
|
2019-06-17 08:34:30 +00:00
|
|
|
retry(t, fmt.Sprintf("removing file %q", x.Remote()), func() error { return x.Remove(ctx) })
|
2017-10-29 12:23:10 +00:00
|
|
|
case fs.Directory:
|
|
|
|
toDelete = append(toDelete, x.Remote())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-04 15:58:29 +00:00
|
|
|
})
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2018-04-09 18:26:27 +00:00
|
|
|
sort.Strings(toDelete)
|
|
|
|
for i := len(toDelete) - 1; i >= 0; i-- {
|
|
|
|
dir := toDelete[i]
|
|
|
|
retry(t, fmt.Sprintf("removing dir %q", dir), func() error {
|
2019-06-17 08:34:30 +00:00
|
|
|
return r.Fremote.Rmdir(ctx, dir)
|
2018-04-09 18:26:27 +00:00
|
|
|
})
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
// Check remote is empty
|
2017-11-01 13:07:33 +00:00
|
|
|
CheckListingWithPrecision(t, r.Fremote, []Item{}, []string{}, r.Fremote.Precision())
|
2019-08-22 10:48:17 +00:00
|
|
|
// Clear the remote cache
|
|
|
|
cache.Clear()
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Logf = t.Logf
|
|
|
|
r.Fatalf = t.Fatalf
|
2020-11-05 16:27:01 +00:00
|
|
|
r.Logf("Remote %q, Local %q, Modify Window %q", r.Fremote, r.Flocal, fs.GetModifyWindow(ctx, r.Fremote))
|
2017-10-29 12:23:10 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-11-14 17:14:02 +00: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 {
|
|
|
|
return newRunIndividual(t, *Individual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRunIndividual as per NewRun but makes an individual remote for this test
|
|
|
|
func NewRunIndividual(t *testing.T) *Run {
|
|
|
|
return newRunIndividual(t, true)
|
|
|
|
}
|
|
|
|
|
2017-10-29 12:23:10 +00:00
|
|
|
// RenameFile renames a file in local
|
|
|
|
func (r *Run) RenameFile(item Item, newpath string) Item {
|
|
|
|
oldFilepath := path.Join(r.LocalName, item.Path)
|
|
|
|
newFilepath := path.Join(r.LocalName, newpath)
|
|
|
|
if err := os.Rename(oldFilepath, newFilepath); err != nil {
|
|
|
|
r.Fatalf("Failed to rename file from %q to %q: %v", item.Path, newpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
item.Path = newpath
|
|
|
|
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile writes a file to local
|
|
|
|
func (r *Run) WriteFile(filePath, content string, t time.Time) Item {
|
|
|
|
item := NewItem(filePath, content, t)
|
|
|
|
// FIXME make directories?
|
|
|
|
filePath = path.Join(r.LocalName, filePath)
|
|
|
|
dirPath := path.Dir(filePath)
|
2021-06-10 22:46:36 +00:00
|
|
|
err := file.MkdirAll(dirPath, 0770)
|
2017-10-29 12:23:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForceMkdir creates the remote
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) ForceMkdir(ctx context.Context, f fs.Fs) {
|
|
|
|
err := f.Mkdir(ctx, "")
|
2017-10-29 12:23:10 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to mkdir %q: %v", f, err)
|
|
|
|
}
|
|
|
|
r.mkdir[f.String()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the remote if it hasn't been created already
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) Mkdir(ctx context.Context, f fs.Fs) {
|
2017-10-29 12:23:10 +00:00
|
|
|
if !r.mkdir[f.String()] {
|
2019-06-17 08:34:30 +00:00
|
|
|
r.ForceMkdir(ctx, f)
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteObjectTo writes an object to the fs, remote passed in
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) WriteObjectTo(ctx context.Context, f fs.Fs, remote, content string, modTime time.Time, useUnchecked bool) Item {
|
2017-10-29 12:23:10 +00:00
|
|
|
put := f.Put
|
|
|
|
if useUnchecked {
|
|
|
|
put = f.Features().PutUnchecked
|
|
|
|
if put == nil {
|
|
|
|
r.Fatalf("Fs doesn't support PutUnchecked")
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 08:34:30 +00:00
|
|
|
r.Mkdir(ctx, f)
|
2019-09-16 09:59:01 +00:00
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 00:17:24 +00:00
|
|
|
// calculate all hashes f supports for content
|
2019-09-16 09:59:01 +00:00
|
|
|
hash, err := hash.NewMultiHasherTypes(f.Hashes())
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to make new multi hasher: %v", err)
|
|
|
|
}
|
|
|
|
_, err = hash.Write([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("Failed to make write to hash: %v", err)
|
|
|
|
}
|
|
|
|
hashSums := hash.Sums()
|
|
|
|
|
2017-10-29 12:23:10 +00:00
|
|
|
const maxTries = 10
|
|
|
|
for tries := 1; ; tries++ {
|
|
|
|
in := bytes.NewBufferString(content)
|
2019-09-16 09:59:01 +00:00
|
|
|
objinfo := object.NewStaticObjectInfo(remote, modTime, int64(len(content)), true, hashSums, nil)
|
2019-06-17 08:34:30 +00:00
|
|
|
_, err := put(ctx, in, objinfo)
|
2017-10-29 12:23:10 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Retry if err returned a retry error
|
2018-01-12 16:30:54 +00:00
|
|
|
if fserrors.IsRetryError(err) && tries < maxTries {
|
2017-10-29 12:23:10 +00:00
|
|
|
r.Logf("Retry Put of %q to %v: %d/%d (%v)", remote, f, tries, maxTries, err)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.Fatalf("Failed to put %q to %q: %v", remote, f, err)
|
|
|
|
}
|
|
|
|
return NewItem(remote, content, modTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteObject writes an object to the remote
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) WriteObject(ctx context.Context, remote, content string, modTime time.Time) Item {
|
|
|
|
return r.WriteObjectTo(ctx, r.Fremote, remote, content, modTime, false)
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteUncheckedObject writes an object to the remote not checking for duplicates
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) WriteUncheckedObject(ctx context.Context, remote, content string, modTime time.Time) Item {
|
|
|
|
return r.WriteObjectTo(ctx, r.Fremote, remote, content, modTime, true)
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBoth calls WriteObject and WriteFile with the same arguments
|
2019-06-17 08:34:30 +00:00
|
|
|
func (r *Run) WriteBoth(ctx context.Context, remote, content string, modTime time.Time) Item {
|
2017-10-29 12:23:10 +00:00
|
|
|
r.WriteFile(remote, content, modTime)
|
2019-06-17 08:34:30 +00:00
|
|
|
return r.WriteObject(ctx, remote, content, modTime)
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckWithDuplicates does a test but allows duplicates
|
|
|
|
func (r *Run) CheckWithDuplicates(t *testing.T, items ...Item) {
|
2018-01-25 12:03:39 +00:00
|
|
|
var want, got []string
|
|
|
|
|
|
|
|
// construct a []string of desired items
|
|
|
|
for _, item := range items {
|
|
|
|
want = append(want, fmt.Sprintf("%q %d", item.Path, item.Size))
|
|
|
|
}
|
|
|
|
sort.Strings(want)
|
|
|
|
|
|
|
|
// do the listing
|
2019-06-17 08:34:30 +00:00
|
|
|
objs, _, err := walk.GetAll(context.Background(), r.Fremote, "", true, -1)
|
2018-01-25 12:03:39 +00:00
|
|
|
if err != nil && err != fs.ErrorDirNotFound {
|
|
|
|
t.Fatalf("Error listing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a []string of actual items
|
|
|
|
for _, o := range objs {
|
|
|
|
got = append(got, fmt.Sprintf("%q %d", o.Remote(), o.Size()))
|
|
|
|
}
|
|
|
|
sort.Strings(got)
|
|
|
|
|
|
|
|
assert.Equal(t, want, got)
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2019-08-22 10:48:17 +00:00
|
|
|
// Clear the remote cache
|
|
|
|
cache.Clear()
|
2017-10-29 12:23:10 +00:00
|
|
|
}
|