2015-09-22 19:47:16 +02:00
|
|
|
// Package fstests provides generic tests for testing the Fs and Object interfaces
|
2015-02-14 19:48:08 +01:00
|
|
|
//
|
|
|
|
// Run go generate to write the tests for the remotes
|
2015-09-22 19:47:16 +02:00
|
|
|
package fstests
|
2015-02-14 19:48:08 +01:00
|
|
|
|
|
|
|
//go:generate go run gen_tests.go
|
2014-07-24 23:50:11 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-15 16:40:18 +02:00
|
|
|
"flag"
|
2016-07-25 20:18:56 +02:00
|
|
|
"fmt"
|
2014-07-24 23:50:11 +02:00
|
|
|
"io"
|
2016-09-10 12:29:57 +02:00
|
|
|
"io/ioutil"
|
2014-07-31 22:24:52 +02:00
|
|
|
"os"
|
2016-04-23 22:46:52 +02:00
|
|
|
"path"
|
2017-07-08 17:26:41 +02:00
|
|
|
"path/filepath"
|
2016-05-07 15:50:35 +02:00
|
|
|
"sort"
|
2014-07-31 22:24:52 +02:00
|
|
|
"strings"
|
2014-07-24 23:50:11 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fstest"
|
2017-02-25 12:09:57 +01:00
|
|
|
"github.com/pkg/errors"
|
2016-04-23 22:46:52 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-07-24 23:50:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-22 19:47:16 +02:00
|
|
|
remote fs.Fs
|
|
|
|
// RemoteName should be set to the name of the remote for testing
|
2014-07-31 22:24:52 +02:00
|
|
|
RemoteName = ""
|
|
|
|
subRemoteName = ""
|
|
|
|
subRemoteLeaf = ""
|
2015-09-22 19:47:16 +02:00
|
|
|
// NilObject should be set to a nil Object from the Fs under test
|
|
|
|
NilObject fs.Object
|
2016-08-20 17:25:14 +02:00
|
|
|
// ExtraConfig is for adding config to a remote
|
|
|
|
ExtraConfig = []ExtraConfigItem{}
|
2017-06-18 23:53:19 +02:00
|
|
|
// SkipBadWindowsCharacters skips unusable characters for windows if set
|
|
|
|
SkipBadWindowsCharacters = map[string]bool{}
|
|
|
|
file1 = fstest.Item{
|
2014-07-24 23:50:11 +02:00
|
|
|
ModTime: fstest.Time("2001-02-03T04:05:06.499999999Z"),
|
|
|
|
Path: "file name.txt",
|
|
|
|
}
|
2016-09-10 12:29:57 +02:00
|
|
|
file1Contents = ""
|
|
|
|
file2 = fstest.Item{
|
2014-07-24 23:50:11 +02:00
|
|
|
ModTime: fstest.Time("2001-02-03T04:05:10.123123123Z"),
|
2016-08-23 15:28:05 +02:00
|
|
|
Path: `hello? sausage/êé/Hello, 世界/ " ' @ < > & ? + ≠/z.txt`,
|
|
|
|
WinPath: `hello_ sausage/êé/Hello, 世界/ _ ' @ _ _ & _ + ≠/z.txt`,
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2017-06-01 23:55:10 +02:00
|
|
|
file2Contents = ""
|
|
|
|
verbose = flag.Bool("verbose", false, "Set to enable logging")
|
|
|
|
dumpHeaders = flag.Bool("dump-headers", false, "Dump HTTP headers - may contain sensitive info")
|
|
|
|
dumpBodies = flag.Bool("dump-bodies", false, "Dump HTTP headers and bodies - may contain sensitive info")
|
|
|
|
overrideRemote = flag.String("remote", "", "Set this to override the default remote name (eg s3:)")
|
2017-07-03 14:39:31 +02:00
|
|
|
isLocalRemote bool
|
2014-07-24 23:50:11 +02:00
|
|
|
)
|
|
|
|
|
2016-08-20 17:25:14 +02:00
|
|
|
// ExtraConfigItem describes a config item added on the fly while testing
|
|
|
|
type ExtraConfigItem struct{ Name, Key, Value string }
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestInit tests basic intitialisation
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestInit(t *testing.T) {
|
2014-07-31 22:24:52 +02:00
|
|
|
var err error
|
2016-02-16 16:25:27 +01:00
|
|
|
|
2017-06-18 23:53:19 +02:00
|
|
|
// Remove bad characters from Windows file name if set
|
|
|
|
if SkipBadWindowsCharacters[RemoteName] {
|
|
|
|
t.Logf("Removing bad windows characters from test file")
|
|
|
|
file2.Path = winPath(file2.Path)
|
|
|
|
}
|
|
|
|
|
2016-02-16 16:25:27 +01: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-07-24 23:50:11 +02:00
|
|
|
fs.LoadConfig()
|
2016-08-20 17:25:14 +02:00
|
|
|
// Set extra config if supplied
|
|
|
|
for _, item := range ExtraConfig {
|
2016-12-20 19:03:09 +01:00
|
|
|
fs.ConfigFileSet(item.Name, item.Key, item.Value)
|
2016-08-20 17:25:14 +02:00
|
|
|
}
|
2017-02-09 22:22:46 +01:00
|
|
|
if *verbose {
|
|
|
|
fs.Config.LogLevel = fs.LogLevelDebug
|
|
|
|
}
|
2015-11-07 10:48:46 +01:00
|
|
|
fs.Config.DumpHeaders = *dumpHeaders
|
|
|
|
fs.Config.DumpBodies = *dumpBodies
|
2017-06-01 23:55:10 +02:00
|
|
|
if *overrideRemote != "" {
|
|
|
|
RemoteName = *overrideRemote
|
|
|
|
}
|
2015-08-15 16:40:18 +02:00
|
|
|
t.Logf("Using remote %q", RemoteName)
|
2014-07-31 22:24:52 +02:00
|
|
|
if RemoteName == "" {
|
|
|
|
RemoteName, err = fstest.LocalRemote()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2017-07-03 14:39:31 +02:00
|
|
|
isLocalRemote = true
|
2014-07-31 22:24:52 +02:00
|
|
|
}
|
|
|
|
subRemoteName, subRemoteLeaf, err = fstest.RandomRemoteName(RemoteName)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-07-31 22:24:52 +02:00
|
|
|
|
|
|
|
remote, err = fs.NewFs(subRemoteName)
|
2015-09-22 19:47:16 +02:00
|
|
|
if err == fs.ErrorNotFoundInConfigFile {
|
2016-06-06 22:23:54 +02:00
|
|
|
t.Logf("Didn't find %q in config file - skipping tests", RemoteName)
|
2014-07-31 09:51:39 +02:00
|
|
|
return
|
|
|
|
}
|
2016-07-25 20:18:56 +02:00
|
|
|
require.NoError(t, err, fmt.Sprintf("unexpected error: %v", err))
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.TestMkdir(t, remote)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func skipIfNotOk(t *testing.T) {
|
|
|
|
if remote == nil {
|
|
|
|
t.Skip("FS not configured")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// Skip if remote is not ListR capable, otherwise set the useListR
|
|
|
|
// flag, returning a function to restore its value
|
|
|
|
func skipIfNotListR(t *testing.T) func() {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
if remote.Features().ListR == nil {
|
|
|
|
t.Skip("FS has no ListR interface")
|
|
|
|
}
|
|
|
|
previous := fs.Config.UseListR
|
|
|
|
fs.Config.UseListR = true
|
|
|
|
return func() {
|
|
|
|
fs.Config.UseListR = previous
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsString tests the String method
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsString(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
str := remote.String()
|
2017-07-03 14:39:31 +02:00
|
|
|
require.NotEqual(t, "", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFsName tests the Name method
|
|
|
|
func TestFsName(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
got := remote.Name()
|
|
|
|
want := RemoteName
|
|
|
|
if isLocalRemote {
|
|
|
|
want = "local:"
|
|
|
|
}
|
|
|
|
require.Equal(t, want, got+":")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFsRoot tests the Root method
|
|
|
|
func TestFsRoot(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
name := remote.Name() + ":"
|
|
|
|
root := remote.Root()
|
|
|
|
if isLocalRemote {
|
2017-07-08 17:26:41 +02:00
|
|
|
// only check last path element on local
|
|
|
|
require.Equal(t, filepath.Base(subRemoteName), filepath.Base(root))
|
|
|
|
} else {
|
|
|
|
require.Equal(t, subRemoteName, name+root)
|
2017-07-03 14:39:31 +02:00
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsRmdirEmpty tests deleting an empty directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsRmdirEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.TestRmdir(t, remote)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsRmdirNotFound tests deleting a non existent directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsRmdirNotFound(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-11-25 22:52:43 +01:00
|
|
|
err := remote.Rmdir("")
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Error(t, err, "Expecting error on Rmdir non existent")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsMkdir tests tests making a directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsMkdir(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.TestMkdir(t, remote)
|
|
|
|
fstest.TestMkdir(t, remote)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 22:52:43 +01:00
|
|
|
// TestFsMkdirRmdirSubdir tests making and removing a sub directory
|
|
|
|
func TestFsMkdirRmdirSubdir(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
dir := "dir/subdir"
|
|
|
|
err := fs.Mkdir(remote, dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListingWithPrecision(t, remote, []fstest.Item{}, []string{"dir", "dir/subdir"}, fs.Config.ModifyWindow)
|
|
|
|
|
|
|
|
err = fs.Rmdir(remote, dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListingWithPrecision(t, remote, []fstest.Item{}, []string{"dir"}, fs.Config.ModifyWindow)
|
|
|
|
|
|
|
|
err = fs.Rmdir(remote, "dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListingWithPrecision(t, remote, []fstest.Item{}, []string{}, fs.Config.ModifyWindow)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsListEmpty tests listing an empty directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsListEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-07 15:50:35 +02:00
|
|
|
// winPath converts a path into a windows safe path
|
|
|
|
func winPath(s string) string {
|
2017-06-18 23:53:19 +02:00
|
|
|
return strings.Map(func(r rune) rune {
|
|
|
|
switch r {
|
|
|
|
case '<', '>', '"', '|', '?', '*', ':':
|
|
|
|
return '_'
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, s)
|
2016-05-07 15:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dirsToNames returns a sorted list of names
|
2017-06-30 14:37:29 +02:00
|
|
|
func dirsToNames(dirs []fs.Directory) []string {
|
2016-05-07 15:50:35 +02:00
|
|
|
names := []string{}
|
|
|
|
for _, dir := range dirs {
|
2017-06-30 14:37:29 +02:00
|
|
|
names = append(names, winPath(dir.Remote()))
|
2016-05-07 15:50:35 +02:00
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// objsToNames returns a sorted list of object names
|
|
|
|
func objsToNames(objs []fs.Object) []string {
|
|
|
|
names := []string{}
|
|
|
|
for _, obj := range objs {
|
|
|
|
names = append(names, winPath(obj.Remote()))
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsListDirEmpty tests listing the directories from an empty directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsListDirEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2017-02-24 23:51:01 +01:00
|
|
|
objs, dirs, err := fs.WalkGetAll(remote, "", true, 1)
|
2016-04-23 22:46:52 +02:00
|
|
|
require.NoError(t, err)
|
2016-05-07 15:50:35 +02:00
|
|
|
assert.Equal(t, []string{}, objsToNames(objs))
|
|
|
|
assert.Equal(t, []string{}, dirsToNames(dirs))
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// TestFsListRDirEmpty tests listing the directories from an empty directory using ListR
|
|
|
|
func TestFsListRDirEmpty(t *testing.T) {
|
|
|
|
defer skipIfNotListR(t)()
|
|
|
|
TestFsListDirEmpty(t)
|
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// TestFsNewObjectNotFound tests not finding a object
|
|
|
|
func TestFsNewObjectNotFound(t *testing.T) {
|
2014-07-24 23:50:11 +02:00
|
|
|
skipIfNotOk(t)
|
2016-06-25 22:23:20 +02:00
|
|
|
// Object in an existing directory
|
|
|
|
o, err := remote.NewObject("potato")
|
|
|
|
assert.Nil(t, o)
|
|
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
|
|
|
// Now try an object in a non existing directory
|
|
|
|
o, err = remote.NewObject("directory/not/found/potato")
|
|
|
|
assert.Nil(t, o)
|
|
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func findObject(t *testing.T, Name string) fs.Object {
|
2015-12-30 11:46:04 +01:00
|
|
|
var obj fs.Object
|
2016-06-25 22:23:20 +02:00
|
|
|
var err error
|
2017-02-22 11:14:40 +01:00
|
|
|
for i := 1; i <= *fstest.ListRetries; i++ {
|
2016-06-25 22:23:20 +02:00
|
|
|
obj, err = remote.NewObject(Name)
|
|
|
|
if err == nil {
|
2015-12-30 11:46:04 +01:00
|
|
|
break
|
|
|
|
}
|
2017-02-22 11:14:40 +01:00
|
|
|
t.Logf("Sleeping for 1 second for findObject eventual consistency: %d/%d (%v)", i, *fstest.ListRetries, err)
|
2015-12-30 11:46:04 +01:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-07-24 23:50:11 +02:00
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
func testPut(t *testing.T, file *fstest.Item) string {
|
2017-06-13 12:22:16 +02:00
|
|
|
tries := 1
|
|
|
|
const maxTries = 10
|
2016-05-07 15:50:35 +02:00
|
|
|
again:
|
2016-09-10 12:29:57 +02:00
|
|
|
contents := fstest.RandomString(100)
|
|
|
|
buf := bytes.NewBufferString(contents)
|
2016-01-17 12:27:31 +01:00
|
|
|
hash := fs.NewMultiHasher()
|
2014-07-24 23:50:11 +02:00
|
|
|
in := io.TeeReader(buf, hash)
|
|
|
|
|
|
|
|
file.Size = int64(buf.Len())
|
2016-02-18 12:35:25 +01:00
|
|
|
obji := fs.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, nil, nil)
|
|
|
|
obj, err := remote.Put(in, obji)
|
2014-07-24 23:50:11 +02:00
|
|
|
if err != nil {
|
2016-05-07 15:50:35 +02:00
|
|
|
// Retry if err returned a retry error
|
2016-07-04 12:57:30 +02:00
|
|
|
if fs.IsRetryError(err) && tries < maxTries {
|
2016-05-07 15:50:35 +02:00
|
|
|
t.Logf("Put error: %v - low level retry %d/%d", err, tries, maxTries)
|
2016-07-12 20:41:34 +02:00
|
|
|
time.Sleep(2 * time.Second)
|
2016-05-07 15:50:35 +02:00
|
|
|
|
|
|
|
tries++
|
|
|
|
goto again
|
|
|
|
}
|
2016-07-25 20:18:56 +02:00
|
|
|
require.NoError(t, err, fmt.Sprintf("Put error: %v", err))
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2016-01-17 12:27:31 +01:00
|
|
|
file.Hashes = hash.Sums()
|
2014-08-01 18:58:39 +02:00
|
|
|
file.Check(t, obj, remote.Precision())
|
2014-07-24 23:50:11 +02:00
|
|
|
// Re-read the object and check again
|
|
|
|
obj = findObject(t, file.Path)
|
2014-08-01 18:58:39 +02:00
|
|
|
file.Check(t, obj, remote.Precision())
|
2016-09-10 12:29:57 +02:00
|
|
|
return contents
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsPutFile1 tests putting a file
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsPutFile1(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-09-10 12:29:57 +02:00
|
|
|
file1Contents = testPut(t, &file1)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 18:06:56 +01:00
|
|
|
type errorReader struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (er errorReader) Read(p []byte) (n int, err error) {
|
|
|
|
return 0, er.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFsPutError tests uploading a file where there is an error
|
|
|
|
//
|
|
|
|
// It makes sure that aborting a file half way through does not create
|
|
|
|
// a file on the remote.
|
|
|
|
func TestFsPutError(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Read 50 bytes then produce an error
|
|
|
|
contents := fstest.RandomString(50)
|
|
|
|
buf := bytes.NewBufferString(contents)
|
|
|
|
er := &errorReader{errors.New("potato")}
|
|
|
|
in := io.MultiReader(buf, er)
|
|
|
|
|
|
|
|
obji := fs.NewStaticObjectInfo(file2.Path, file2.ModTime, 100, true, nil, nil)
|
2017-06-13 12:22:16 +02:00
|
|
|
_, err := remote.Put(in, obji)
|
2016-11-04 18:06:56 +01:00
|
|
|
// assert.Nil(t, obj) - FIXME some remotes return the object even on nil
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
2017-06-13 12:22:16 +02:00
|
|
|
obj, err := remote.NewObject(file2.Path)
|
2016-11-04 18:06:56 +01:00
|
|
|
assert.Nil(t, obj)
|
|
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsPutFile2 tests putting a file into a subdirectory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsPutFile2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-09-10 12:29:57 +02:00
|
|
|
file2Contents = testPut(t, &file2)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 16:06:27 +02:00
|
|
|
// TestFsUpdateFile1 tests updating file1 with new contents
|
|
|
|
func TestFsUpdateFile1(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-09-10 12:29:57 +02:00
|
|
|
file1Contents = testPut(t, &file1)
|
2016-06-12 16:06:27 +02:00
|
|
|
// Note that the next test will check there are no duplicated file names
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:14:40 +01:00
|
|
|
// TestFsListDirFile2 tests the files are correctly uploaded by doing
|
|
|
|
// Depth 1 directory listings
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsListDirFile2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2017-02-22 11:14:40 +01:00
|
|
|
list := func(dir string, expectedDirNames, expectedObjNames []string) {
|
|
|
|
var objNames, dirNames []string
|
|
|
|
for i := 1; i <= *fstest.ListRetries; i++ {
|
2017-06-18 23:53:19 +02:00
|
|
|
objs, dirs, err := fs.WalkGetAll(remote, dir, true, 1)
|
|
|
|
if errors.Cause(err) == fs.ErrorDirNotFound {
|
|
|
|
objs, dirs, err = fs.WalkGetAll(remote, winPath(dir), true, 1)
|
2017-02-22 11:14:40 +01:00
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
objNames = objsToNames(objs)
|
|
|
|
dirNames = dirsToNames(dirs)
|
|
|
|
if len(objNames) >= len(expectedObjNames) && len(dirNames) >= len(expectedDirNames) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Logf("Sleeping for 1 second for TestFsListDirFile2 eventual consistency: %d/%d", i, *fstest.ListRetries)
|
|
|
|
time.Sleep(1 * time.Second)
|
2016-01-20 21:06:05 +01:00
|
|
|
}
|
2017-02-22 11:14:40 +01:00
|
|
|
assert.Equal(t, expectedDirNames, dirNames)
|
|
|
|
assert.Equal(t, expectedObjNames, objNames)
|
|
|
|
}
|
|
|
|
dir := file2.Path
|
|
|
|
deepest := true
|
|
|
|
for dir != "" {
|
|
|
|
expectedObjNames := []string{}
|
|
|
|
expectedDirNames := []string{}
|
|
|
|
child := dir
|
|
|
|
dir = path.Dir(dir)
|
|
|
|
if dir == "." {
|
|
|
|
dir = ""
|
|
|
|
expectedObjNames = append(expectedObjNames, winPath(file1.Path))
|
|
|
|
}
|
|
|
|
if deepest {
|
|
|
|
expectedObjNames = append(expectedObjNames, winPath(file2.Path))
|
|
|
|
deepest = false
|
|
|
|
} else {
|
|
|
|
expectedDirNames = append(expectedDirNames, winPath(child))
|
|
|
|
}
|
|
|
|
list(dir, expectedDirNames, expectedObjNames)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// TestFsListRDirFile2 tests the files are correctly uploaded by doing
|
|
|
|
// Depth 1 directory listings using ListR
|
|
|
|
func TestFsListRDirFile2(t *testing.T) {
|
|
|
|
defer skipIfNotListR(t)()
|
|
|
|
TestFsListDirFile2(t)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsListDirRoot tests that DirList works in the root
|
2014-07-31 22:24:52 +02:00
|
|
|
func TestFsListDirRoot(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
rootRemote, err := fs.NewFs(RemoteName)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2017-02-24 23:51:01 +01:00
|
|
|
_, dirs, err := fs.WalkGetAll(rootRemote, "", true, 1)
|
2016-04-23 22:46:52 +02:00
|
|
|
require.NoError(t, err)
|
2016-05-07 15:50:35 +02:00
|
|
|
assert.Contains(t, dirsToNames(dirs), subRemoteLeaf, "Remote leaf not found")
|
2014-07-31 22:24:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// TestFsListRDirRoot tests that DirList works in the root using ListR
|
|
|
|
func TestFsListRDirRoot(t *testing.T) {
|
|
|
|
defer skipIfNotListR(t)()
|
|
|
|
TestFsListDirRoot(t)
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:46:52 +02:00
|
|
|
// TestFsListSubdir tests List works for a subdirectory
|
|
|
|
func TestFsListSubdir(t *testing.T) {
|
2014-08-01 00:20:39 +02:00
|
|
|
skipIfNotOk(t)
|
2016-05-30 20:44:15 +02:00
|
|
|
fileName := file2.Path
|
2016-08-24 23:21:34 +02:00
|
|
|
var err error
|
|
|
|
var objs []fs.Object
|
2017-06-30 14:37:29 +02:00
|
|
|
var dirs []fs.Directory
|
2016-08-24 23:21:34 +02:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
dir, _ := path.Split(fileName)
|
|
|
|
dir = dir[:len(dir)-1]
|
2017-02-24 23:51:01 +01:00
|
|
|
objs, dirs, err = fs.WalkGetAll(remote, dir, true, -1)
|
2016-08-24 23:21:34 +02:00
|
|
|
if err != fs.ErrorDirNotFound {
|
|
|
|
break
|
|
|
|
}
|
2016-05-30 20:44:15 +02:00
|
|
|
fileName = file2.WinPath
|
2014-08-01 00:20:39 +02:00
|
|
|
}
|
2016-05-30 20:44:15 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, objs, 1)
|
|
|
|
assert.Equal(t, fileName, objs[0].Remote())
|
|
|
|
require.Len(t, dirs, 0)
|
2014-08-01 00:20:39 +02:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// TestFsListRSubdir tests List works for a subdirectory using ListR
|
|
|
|
func TestFsListRSubdir(t *testing.T) {
|
|
|
|
defer skipIfNotListR(t)()
|
|
|
|
TestFsListSubdir(t)
|
|
|
|
}
|
|
|
|
|
2016-05-07 15:50:35 +02:00
|
|
|
// TestFsListLevel2 tests List works for 2 levels
|
|
|
|
func TestFsListLevel2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2017-02-24 23:51:01 +01:00
|
|
|
objs, dirs, err := fs.WalkGetAll(remote, "", true, 2)
|
2016-05-07 15:50:35 +02:00
|
|
|
if err == fs.ErrorLevelNotSupported {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{file1.Path}, objsToNames(objs))
|
|
|
|
assert.Equal(t, []string{`hello_ sausage`, `hello_ sausage/êé`}, dirsToNames(dirs))
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// TestFsListRLevel2 tests List works for 2 levels using ListR
|
|
|
|
func TestFsListRLevel2(t *testing.T) {
|
|
|
|
defer skipIfNotListR(t)()
|
|
|
|
TestFsListLevel2(t)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsListFile1 tests file present
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsListFile1(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// TestFsNewObject tests NewObject
|
|
|
|
func TestFsNewObject(t *testing.T) {
|
2014-07-24 23:50:11 +02:00
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2014-08-01 18:58:39 +02:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsListFile1and2 tests two files present
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsListFile1and2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2017-02-25 12:09:57 +01:00
|
|
|
// TestFsNewObjectDir tests NewObject on a directory which should produce an error
|
|
|
|
func TestFsNewObjectDir(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
dir := path.Dir(file2.Path)
|
|
|
|
obj, err := remote.NewObject(dir)
|
|
|
|
assert.Nil(t, obj)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsCopy tests Copy
|
2015-02-14 19:48:08 +01:00
|
|
|
func TestFsCopy(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have Copy
|
2017-01-13 18:21:47 +01:00
|
|
|
doCopy := remote.Features().Copy
|
|
|
|
if doCopy == nil {
|
2015-02-14 19:48:08 +01:00
|
|
|
t.Skip("FS has no Copier interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
var file1Copy = file1
|
|
|
|
file1Copy.Path += "-copy"
|
|
|
|
|
|
|
|
// do the copy
|
|
|
|
src := findObject(t, file1.Path)
|
2017-01-13 18:21:47 +01:00
|
|
|
dst, err := doCopy(src, file1Copy.Path)
|
2016-07-25 20:18:56 +02:00
|
|
|
if err == fs.ErrorCantCopy {
|
|
|
|
t.Skip("FS can't copy")
|
|
|
|
}
|
|
|
|
require.NoError(t, err, fmt.Sprintf("Error: %#v", err))
|
2015-02-14 19:48:08 +01:00
|
|
|
|
|
|
|
// check file exists in new listing
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2, file1Copy})
|
|
|
|
|
2016-01-17 12:27:31 +01:00
|
|
|
// Check dst lightly - list above has checked ModTime/Hashes
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, file1Copy.Path, dst.Remote())
|
2015-02-14 19:48:08 +01:00
|
|
|
|
|
|
|
// Delete copy
|
|
|
|
err = dst.Remove()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-02-14 19:48:08 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsMove tests Move
|
2015-08-31 22:05:51 +02:00
|
|
|
func TestFsMove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have Move
|
2017-01-13 18:21:47 +01:00
|
|
|
doMove := remote.Features().Move
|
|
|
|
if doMove == nil {
|
2015-08-31 22:05:51 +02:00
|
|
|
t.Skip("FS has no Mover interface")
|
|
|
|
}
|
|
|
|
|
2016-11-01 13:05:06 +01:00
|
|
|
// state of files now:
|
|
|
|
// 1: file name.txt
|
|
|
|
// 2: hello sausage?/../z.txt
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
var file1Move = file1
|
2016-11-01 13:05:06 +01:00
|
|
|
var file2Move = file2
|
2015-08-31 22:05:51 +02:00
|
|
|
|
2016-11-01 13:05:06 +01:00
|
|
|
// check happy path, i.e. no naming conflicts when rename and move are two
|
|
|
|
// separate operations
|
|
|
|
file2Move.Path = "other.txt"
|
2017-01-20 18:12:05 +01:00
|
|
|
file2Move.WinPath = ""
|
2016-11-01 13:05:06 +01:00
|
|
|
src := findObject(t, file2.Path)
|
2017-01-13 18:21:47 +01:00
|
|
|
dst, err := doMove(src, file2Move.Path)
|
2016-07-25 20:18:56 +02:00
|
|
|
if err == fs.ErrorCantMove {
|
|
|
|
t.Skip("FS can't move")
|
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-08-31 22:05:51 +02:00
|
|
|
// check file exists in new listing
|
2016-11-01 13:05:06 +01:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2Move})
|
2016-01-17 12:27:31 +01:00
|
|
|
// Check dst lightly - list above has checked ModTime/Hashes
|
2016-11-01 13:05:06 +01:00
|
|
|
assert.Equal(t, file2Move.Path, dst.Remote())
|
|
|
|
// 1: file name.txt
|
|
|
|
// 2: other.txt
|
|
|
|
|
|
|
|
// Check conflict on "rename, then move"
|
|
|
|
file1Move.Path = "moveTest/other.txt"
|
|
|
|
src = findObject(t, file1.Path)
|
2017-01-13 18:21:47 +01:00
|
|
|
_, err = doMove(src, file1Move.Path)
|
2016-11-01 13:05:06 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1Move, file2Move})
|
|
|
|
// 1: moveTest/other.txt
|
|
|
|
// 2: other.txt
|
2015-08-31 22:05:51 +02:00
|
|
|
|
2016-11-01 13:05:06 +01:00
|
|
|
// Check conflict on "move, then rename"
|
2015-08-31 22:05:51 +02:00
|
|
|
src = findObject(t, file1Move.Path)
|
2017-01-13 18:21:47 +01:00
|
|
|
_, err = doMove(src, file1.Path)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-11-01 13:05:06 +01:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2Move})
|
|
|
|
// 1: file name.txt
|
|
|
|
// 2: other.txt
|
2015-08-31 22:05:51 +02:00
|
|
|
|
2016-11-01 13:05:06 +01:00
|
|
|
src = findObject(t, file2Move.Path)
|
2017-01-13 18:21:47 +01:00
|
|
|
_, err = doMove(src, file2.Path)
|
2016-11-01 13:05:06 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2})
|
|
|
|
// 1: file name.txt
|
|
|
|
// 2: hello sausage?/../z.txt
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move src to this remote using server side move operations.
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
//
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
2015-09-22 19:47:16 +02:00
|
|
|
|
|
|
|
// TestFsDirMove tests DirMove
|
2017-02-05 22:20:56 +01:00
|
|
|
//
|
|
|
|
// go test -v -run '^Test(Setup|Init|FsMkdir|FsPutFile1|FsPutFile2|FsUpdateFile1|FsDirMove)$
|
2015-08-31 22:05:51 +02:00
|
|
|
func TestFsDirMove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have DirMove
|
2017-01-13 18:21:47 +01:00
|
|
|
doDirMove := remote.Features().DirMove
|
|
|
|
if doDirMove == nil {
|
2015-08-31 22:05:51 +02:00
|
|
|
t.Skip("FS has no DirMover interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check it can't move onto itself
|
2017-02-05 22:20:56 +01:00
|
|
|
err := doDirMove(remote, "", "")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Equal(t, fs.ErrorDirExists, err)
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// new remote
|
2016-07-11 12:36:46 +02:00
|
|
|
newRemote, _, removeNewRemote, err := fstest.RandomRemote(RemoteName, false)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-08-31 22:05:51 +02:00
|
|
|
defer removeNewRemote()
|
|
|
|
|
2017-02-05 22:20:56 +01:00
|
|
|
const newName = "new_name/sub_new_name"
|
2015-08-31 22:05:51 +02:00
|
|
|
// try the move
|
2017-02-05 22:20:56 +01:00
|
|
|
err = newRemote.Features().DirMove(remote, "", newName)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// check remotes
|
2015-09-11 11:37:12 +02:00
|
|
|
// FIXME: Prints errors.
|
2015-08-31 22:05:51 +02:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{})
|
2017-02-05 22:20:56 +01:00
|
|
|
file1Copy := file1
|
|
|
|
file1Copy.Path = path.Join(newName, file1.Path)
|
|
|
|
file2Copy := file2
|
|
|
|
file2Copy.Path = path.Join(newName, file2.Path)
|
|
|
|
file2Copy.WinPath = path.Join(newName, file2.WinPath)
|
|
|
|
fstest.CheckListing(t, newRemote, []fstest.Item{file2Copy, file1Copy})
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// move it back
|
2017-02-05 22:20:56 +01:00
|
|
|
err = doDirMove(newRemote, newName, "")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// check remotes
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2, file1})
|
|
|
|
fstest.CheckListing(t, newRemote, []fstest.Item{})
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsRmdirFull tests removing a non empty directory
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsRmdirFull(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-11-25 22:52:43 +01:00
|
|
|
err := remote.Rmdir("")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.Error(t, err, "Expecting error on RMdir on non empty remote")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFsPrecision tests the Precision of the Fs
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFsPrecision(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
precision := remote.Precision()
|
2015-08-17 00:24:34 +02:00
|
|
|
if precision == fs.ModTimeNotSupported {
|
|
|
|
return
|
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
if precision > time.Second || precision < 0 {
|
|
|
|
t.Fatalf("Precision out of range %v", precision)
|
|
|
|
}
|
|
|
|
// FIXME check expected precision
|
|
|
|
}
|
|
|
|
|
2017-05-25 23:05:49 +02:00
|
|
|
// TestFsDirChangeNotify tests that changes to directories are properly
|
|
|
|
// propagated
|
|
|
|
//
|
|
|
|
// go test -v -remote TestDrive: -run '^Test(Setup|Init|FsDirChangeNotify)$' -verbose
|
|
|
|
func TestFsDirChangeNotify(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have DirChangeNotify
|
|
|
|
doDirChangeNotify := remote.Features().DirChangeNotify
|
|
|
|
if doDirChangeNotify == nil {
|
|
|
|
t.Skip("FS has no DirChangeNotify interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fs.Mkdir(remote, "dir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
changes := []string{}
|
|
|
|
quitChannel := doDirChangeNotify(func(x string) {
|
|
|
|
changes = append(changes, x)
|
|
|
|
}, time.Second)
|
|
|
|
defer func() { close(quitChannel) }()
|
|
|
|
|
|
|
|
err = fs.Mkdir(remote, "dir/subdir")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"dir"}, changes)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectString tests the Object String method
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectString(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, file1.Path, obj.String())
|
|
|
|
assert.Equal(t, "<nil>", NilObject.String())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectFs tests the object can be found
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectFs(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-12-20 15:15:11 +01:00
|
|
|
testRemote := remote
|
|
|
|
if obj.Fs() != testRemote {
|
2015-11-08 16:29:58 +01:00
|
|
|
// Check to see if this wraps something else
|
2017-01-13 18:21:47 +01:00
|
|
|
if doUnWrap := testRemote.Features().UnWrap; doUnWrap != nil {
|
|
|
|
testRemote = doUnWrap()
|
2015-11-08 16:29:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 15:15:11 +01:00
|
|
|
assert.Equal(t, obj.Fs(), testRemote)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectRemote tests the Remote is correct
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectRemote(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, file1.Path, obj.Remote())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:27:31 +01:00
|
|
|
// TestObjectHashes checks all the hashes the object supports
|
|
|
|
func TestObjectHashes(t *testing.T) {
|
2014-07-24 23:50:11 +02:00
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-01-17 12:27:31 +01:00
|
|
|
file1.CheckHashes(t, obj)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectModTime tests the ModTime of the object is correct
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectModTime(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2014-08-01 18:58:39 +02:00
|
|
|
file1.CheckModTime(t, obj, obj.ModTime(), remote.Precision())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:13:24 +02:00
|
|
|
// TestObjectMimeType tests the MimeType of the object is correct
|
|
|
|
func TestObjectMimeType(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
do, ok := obj.(fs.MimeTyper)
|
|
|
|
if !ok {
|
|
|
|
t.Skip("MimeType method not supported")
|
|
|
|
}
|
|
|
|
mimeType := do.MimeType()
|
|
|
|
if strings.ContainsRune(mimeType, ';') {
|
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", mimeType)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "text/plain", mimeType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectSetModTime tests that SetModTime works
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectSetModTime(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
newModTime := fstest.Time("2011-12-13T14:15:16.999999999Z")
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-03-22 16:23:37 +01:00
|
|
|
err := obj.SetModTime(newModTime)
|
2017-06-13 14:58:39 +02:00
|
|
|
if err == fs.ErrorCantSetModTime || err == fs.ErrorCantSetModTimeWithoutDelete {
|
2016-03-22 16:23:37 +01:00
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-07-24 23:50:11 +02:00
|
|
|
file1.ModTime = newModTime
|
2015-01-04 17:57:55 +01:00
|
|
|
file1.CheckModTime(t, obj, obj.ModTime(), remote.Precision())
|
2014-07-24 23:50:11 +02:00
|
|
|
// And make a new object and read it from there too
|
|
|
|
TestObjectModTime(t)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectSize tests that Size works
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectSize(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, file1.Size, obj.Size())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// read the contents of an object as a string
|
2017-05-19 13:26:07 +02:00
|
|
|
func readObject(t *testing.T, obj fs.Object, limit int64, options ...fs.OpenOption) string {
|
2016-09-10 12:29:57 +02:00
|
|
|
in, err := obj.Open(options...)
|
|
|
|
require.NoError(t, err)
|
2017-05-19 13:26:07 +02:00
|
|
|
var r io.Reader = in
|
|
|
|
if limit >= 0 {
|
|
|
|
r = &io.LimitedReader{R: r, N: limit}
|
|
|
|
}
|
|
|
|
contents, err := ioutil.ReadAll(r)
|
2016-09-10 12:29:57 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = in.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return string(contents)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectOpen tests that Open works
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectOpen(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2017-05-19 13:26:07 +02:00
|
|
|
assert.Equal(t, file1Contents, readObject(t, obj, -1), "contents of file1 differ")
|
2016-09-10 12:29:57 +02:00
|
|
|
}
|
2016-01-17 12:27:31 +01:00
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// TestObjectOpenSeek tests that Open works with Seek
|
|
|
|
func TestObjectOpenSeek(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2017-05-19 13:26:07 +02:00
|
|
|
assert.Equal(t, file1Contents[50:], readObject(t, obj, -1, &fs.SeekOption{Offset: 50}), "contents of file1 differ after seek")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestObjectPartialRead tests that reading only part of the object does the correct thing
|
|
|
|
func TestObjectPartialRead(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
assert.Equal(t, file1Contents[:50], readObject(t, obj, 50), "contents of file1 differ after limited read")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectUpdate tests that Update works
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectUpdate(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-09-10 12:29:57 +02:00
|
|
|
contents := fstest.RandomString(200)
|
|
|
|
buf := bytes.NewBufferString(contents)
|
2016-01-17 12:27:31 +01:00
|
|
|
hash := fs.NewMultiHasher()
|
2014-07-24 23:50:11 +02:00
|
|
|
in := io.TeeReader(buf, hash)
|
|
|
|
|
|
|
|
file1.Size = int64(buf.Len())
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-09-10 12:29:57 +02:00
|
|
|
obji := fs.NewStaticObjectInfo(file1.Path, file1.ModTime, int64(len(contents)), true, nil, obj.Fs())
|
2016-02-18 12:35:25 +01:00
|
|
|
err := obj.Update(in, obji)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-01-17 12:27:31 +01:00
|
|
|
file1.Hashes = hash.Sums()
|
2016-09-10 12:29:57 +02:00
|
|
|
|
|
|
|
// check the object has been updated
|
2014-08-01 18:58:39 +02:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2016-09-10 12:29:57 +02:00
|
|
|
|
2014-07-24 23:50:11 +02:00
|
|
|
// Re-read the object and check again
|
|
|
|
obj = findObject(t, file1.Path)
|
2014-08-01 18:58:39 +02:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2016-09-10 12:29:57 +02:00
|
|
|
|
|
|
|
// check contents correct
|
2017-05-19 13:26:07 +02:00
|
|
|
assert.Equal(t, contents, readObject(t, obj, -1), "contents of updated file1 differ")
|
2016-09-10 12:29:57 +02:00
|
|
|
file1Contents = contents
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectStorable tests that Storable works
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectStorable(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NotNil(t, !obj.Storable(), "Expecting object to be storable")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:01:53 +02:00
|
|
|
// TestFsIsFile tests that an error is returned along with a valid fs
|
|
|
|
// which points to the parent directory.
|
|
|
|
func TestFsIsFile(t *testing.T) {
|
2014-07-31 22:24:52 +02:00
|
|
|
skipIfNotOk(t)
|
|
|
|
remoteName := subRemoteName + "/" + file2.Path
|
|
|
|
file2Copy := file2
|
|
|
|
file2Copy.Path = "z.txt"
|
2017-01-20 18:12:05 +01:00
|
|
|
file2Copy.WinPath = ""
|
2014-07-31 22:24:52 +02:00
|
|
|
fileRemote, err := fs.NewFs(remoteName)
|
2016-06-21 19:01:53 +02:00
|
|
|
assert.Equal(t, fs.ErrorIsFile, err)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, fileRemote, []fstest.Item{file2Copy})
|
2014-07-31 22:24:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:01:53 +02:00
|
|
|
// TestFsIsFileNotFound tests that an error is not returned if no object is found
|
|
|
|
func TestFsIsFileNotFound(t *testing.T) {
|
2014-07-31 22:24:52 +02:00
|
|
|
skipIfNotOk(t)
|
|
|
|
remoteName := subRemoteName + "/not found.txt"
|
|
|
|
fileRemote, err := fs.NewFs(remoteName)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, fileRemote, []fstest.Item{})
|
2014-07-31 22:24:52 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectRemove tests Remove
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectRemove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
err := obj.Remove()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestObjectPurge tests Purge
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestObjectPurge(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 18:58:39 +02:00
|
|
|
fstest.TestPurge(t, remote)
|
2014-07-28 22:02:00 +02:00
|
|
|
err := fs.Purge(remote)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Error(t, err, "Expecting error after on second purge")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestFinalise tidies up after the previous tests
|
2014-07-24 23:50:11 +02:00
|
|
|
func TestFinalise(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-07-31 22:24:52 +02:00
|
|
|
if strings.HasPrefix(RemoteName, "/") {
|
|
|
|
// Remove temp directory
|
|
|
|
err := os.Remove(RemoteName)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
}
|