2015-09-23 01:47:16 +08:00
|
|
|
// Package fstests provides generic tests for testing the Fs and Object interfaces
|
2015-02-15 02:48:08 +08:00
|
|
|
//
|
|
|
|
// Run go generate to write the tests for the remotes
|
2015-09-23 01:47:16 +08:00
|
|
|
package fstests
|
2015-02-15 02:48:08 +08:00
|
|
|
|
|
|
|
//go:generate go run gen_tests.go
|
2014-07-25 05:50:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-15 22:40:18 +08:00
|
|
|
"flag"
|
2014-07-25 05:50:11 +08:00
|
|
|
"io"
|
2014-07-31 15:51:39 +08:00
|
|
|
"log"
|
2014-08-01 04:24:52 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
2014-07-25 05:50:11 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fstest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-23 01:47:16 +08:00
|
|
|
remote fs.Fs
|
|
|
|
// RemoteName should be set to the name of the remote for testing
|
2014-08-01 04:24:52 +08:00
|
|
|
RemoteName = ""
|
|
|
|
subRemoteName = ""
|
|
|
|
subRemoteLeaf = ""
|
2015-09-23 01:47:16 +08:00
|
|
|
// NilObject should be set to a nil Object from the Fs under test
|
|
|
|
NilObject fs.Object
|
|
|
|
file1 = fstest.Item{
|
2014-07-25 05:50:11 +08:00
|
|
|
ModTime: fstest.Time("2001-02-03T04:05:06.499999999Z"),
|
|
|
|
Path: "file name.txt",
|
|
|
|
}
|
|
|
|
file2 = fstest.Item{
|
|
|
|
ModTime: fstest.Time("2001-02-03T04:05:10.123123123Z"),
|
|
|
|
Path: `hello? sausage/êé/Hello, 世界/ " ' @ < > & ?/z.txt`,
|
2015-10-16 17:49:00 +08:00
|
|
|
WinPath: `hello_ sausage/êé/Hello, 世界/ _ ' @ _ _ & _/z.txt`,
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2016-01-21 04:06:05 +08:00
|
|
|
verbose = flag.Bool("verbose", false, "Set to enable logging")
|
2015-11-07 17:48:46 +08:00
|
|
|
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")
|
2014-07-25 05:50:11 +08:00
|
|
|
)
|
|
|
|
|
2016-01-21 04:06:05 +08:00
|
|
|
const eventualConsistencyRetries = 10
|
|
|
|
|
2015-08-15 22:40:18 +08:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&RemoteName, "remote", "", "Set this to override the default remote name (eg s3:)")
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestInit tests basic intitialisation
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestInit(t *testing.T) {
|
2014-08-01 04:24:52 +08:00
|
|
|
var err error
|
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-07-25 05:50:11 +08:00
|
|
|
fs.LoadConfig()
|
2016-01-21 04:06:05 +08:00
|
|
|
fs.Config.Verbose = *verbose
|
|
|
|
fs.Config.Quiet = !*verbose
|
2015-11-07 17:48:46 +08:00
|
|
|
fs.Config.DumpHeaders = *dumpHeaders
|
|
|
|
fs.Config.DumpBodies = *dumpBodies
|
2015-08-15 22:40:18 +08:00
|
|
|
t.Logf("Using remote %q", RemoteName)
|
2014-08-01 04:24:52 +08:00
|
|
|
if RemoteName == "" {
|
|
|
|
RemoteName, err = fstest.LocalRemote()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create tmp dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subRemoteName, subRemoteLeaf, err = fstest.RandomRemoteName(RemoteName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't make remote name: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote, err = fs.NewFs(subRemoteName)
|
2015-09-23 01:47:16 +08:00
|
|
|
if err == fs.ErrorNotFoundInConfigFile {
|
2014-07-31 15:51:39 +08:00
|
|
|
log.Printf("Didn't find %q in config file - skipping tests", RemoteName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't start FS: %v", err)
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.TestMkdir(t, remote)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func skipIfNotOk(t *testing.T) {
|
|
|
|
if remote == nil {
|
|
|
|
t.Skip("FS not configured")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsString tests the String method
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsString(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
str := remote.String()
|
|
|
|
if str == "" {
|
|
|
|
t.Fatal("Bad fs.String()")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsRmdirEmpty tests deleting an empty directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsRmdirEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.TestRmdir(t, remote)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsRmdirNotFound tests deleting a non existent directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsRmdirNotFound(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
err := remote.Rmdir()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expecting error on Rmdir non existent")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsMkdir tests tests making a directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsMkdir(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.TestMkdir(t, remote)
|
|
|
|
fstest.TestMkdir(t, remote)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListEmpty tests listing an empty directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsListEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListDirEmpty tests listing the directories from an empty directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsListDirEmpty(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2016-04-22 03:06:21 +08:00
|
|
|
dirs, err := fs.NewLister().SetLevel(1).Start(remote).GetDirs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
t.Errorf("Found unexpected item %q", dir.Name)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsNewFsObjectNotFound tests not finding a object
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsNewFsObjectNotFound(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
if remote.NewFsObject("potato") != nil {
|
|
|
|
t.Fatal("Didn't expect to find object")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func findObject(t *testing.T, Name string) fs.Object {
|
2015-12-30 18:46:04 +08:00
|
|
|
var obj fs.Object
|
2016-01-21 04:06:05 +08:00
|
|
|
for i := 1; i <= eventualConsistencyRetries; i++ {
|
2015-12-30 18:46:04 +08:00
|
|
|
obj = remote.NewFsObject(Name)
|
|
|
|
if obj != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-01-21 04:06:05 +08:00
|
|
|
t.Logf("Sleeping for 1 second for findObject eventual consistency: %d/%d", i, eventualConsistencyRetries)
|
2015-12-30 18:46:04 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
if obj == nil {
|
2014-07-27 00:18:29 +08:00
|
|
|
t.Fatalf("Object not found: %q", Name)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPut(t *testing.T, file *fstest.Item) {
|
|
|
|
buf := bytes.NewBufferString(fstest.RandomString(100))
|
2016-01-17 19:27:31 +08:00
|
|
|
hash := fs.NewMultiHasher()
|
2014-07-25 05:50:11 +08:00
|
|
|
in := io.TeeReader(buf, hash)
|
|
|
|
|
|
|
|
file.Size = int64(buf.Len())
|
2016-02-18 19:35:25 +08:00
|
|
|
obji := fs.NewStaticObjectInfo(file.Path, file.ModTime, file.Size, true, nil, nil)
|
|
|
|
obj, err := remote.Put(in, obji)
|
2014-07-25 05:50:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Put error", err)
|
|
|
|
}
|
2016-01-17 19:27:31 +08:00
|
|
|
file.Hashes = hash.Sums()
|
2014-08-02 00:58:39 +08:00
|
|
|
file.Check(t, obj, remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
// Re-read the object and check again
|
|
|
|
obj = findObject(t, file.Path)
|
2014-08-02 00:58:39 +08:00
|
|
|
file.Check(t, obj, remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsPutFile1 tests putting a file
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsPutFile1(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
testPut(t, &file1)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsPutFile2 tests putting a file into a subdirectory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsPutFile2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
testPut(t, &file2)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListDirFile2 tests the files are correctly uploaded
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsListDirFile2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
found := false
|
2016-01-21 04:06:05 +08:00
|
|
|
for i := 1; i <= eventualConsistencyRetries; i++ {
|
2016-04-22 03:06:21 +08:00
|
|
|
dirs, err := fs.NewLister().SetLevel(1).Start(remote).GetDirs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if dir.Name != `hello? sausage` && dir.Name != `hello_ sausage` {
|
|
|
|
t.Errorf("Found unexpected item %q", dir.Name)
|
2016-01-21 04:06:05 +08:00
|
|
|
} else {
|
|
|
|
found = true
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2016-01-21 04:06:05 +08:00
|
|
|
if found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Logf("Sleeping for 1 second for TestFsListDirFile2 eventual consistency: %d/%d", i, eventualConsistencyRetries)
|
|
|
|
time.Sleep(1 * time.Second)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Didn't find %q", `hello? sausage`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListDirRoot tests that DirList works in the root
|
2014-08-01 04:24:52 +08:00
|
|
|
func TestFsListDirRoot(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
rootRemote, err := fs.NewFs(RemoteName)
|
|
|
|
if err != nil {
|
2015-02-28 23:35:54 +08:00
|
|
|
t.Fatalf("Failed to make remote %q: %v", RemoteName, err)
|
2014-08-01 04:24:52 +08:00
|
|
|
}
|
|
|
|
found := false
|
2016-04-22 03:06:21 +08:00
|
|
|
dirs, err := fs.NewLister().SetLevel(1).Start(rootRemote).GetDirs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if dir.Name == subRemoteLeaf {
|
2014-08-01 04:24:52 +08:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Didn't find %q", subRemoteLeaf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListRoot tests List works in the root
|
2014-08-01 06:20:39 +08:00
|
|
|
func TestFsListRoot(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
rootRemote, err := fs.NewFs(RemoteName)
|
|
|
|
if err != nil {
|
2015-02-28 23:35:54 +08:00
|
|
|
t.Fatalf("Failed to make remote %q: %v", RemoteName, err)
|
2014-08-01 06:20:39 +08:00
|
|
|
}
|
|
|
|
// Should either find file1 and file2 or nothing
|
|
|
|
found1 := false
|
2015-09-11 17:37:12 +08:00
|
|
|
f1 := subRemoteLeaf + "/" + file1.Path
|
2014-08-01 06:20:39 +08:00
|
|
|
found2 := false
|
2015-09-11 17:37:12 +08:00
|
|
|
f2 := subRemoteLeaf + "/" + file2.Path
|
|
|
|
f2Alt := subRemoteLeaf + "/" + file2.WinPath
|
2014-08-01 06:20:39 +08:00
|
|
|
count := 0
|
2016-04-22 03:06:21 +08:00
|
|
|
objs, err := fs.NewLister().Start(rootRemote).GetObjects()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, obj := range objs {
|
2014-08-01 06:20:39 +08:00
|
|
|
count++
|
2015-09-11 17:37:12 +08:00
|
|
|
if obj.Remote() == f1 {
|
2014-08-01 06:20:39 +08:00
|
|
|
found1 = true
|
|
|
|
}
|
2015-09-11 17:37:12 +08:00
|
|
|
if obj.Remote() == f2 || obj.Remote() == f2Alt {
|
2014-08-01 06:20:39 +08:00
|
|
|
found2 = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count == 0 {
|
2016-04-22 03:06:21 +08:00
|
|
|
// Nothing found is OK
|
2014-08-01 06:20:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if found1 && found2 {
|
2016-04-22 03:06:21 +08:00
|
|
|
// Both found is OK
|
2014-08-01 06:20:39 +08:00
|
|
|
return
|
|
|
|
}
|
2015-09-11 17:37:12 +08:00
|
|
|
t.Errorf("Didn't find %q (%v) and %q (%v) or no files (count %d)", f1, found1, f2, found2, count)
|
2014-08-01 06:20:39 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListFile1 tests file present
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsListFile1(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsNewFsObject tests NewFsObject
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsNewFsObject(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2014-08-02 00:58:39 +08:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsListFile1and2 tests two files present
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsListFile1and2(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsCopy tests Copy
|
2015-02-15 02:48:08 +08:00
|
|
|
func TestFsCopy(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have Copy
|
|
|
|
_, ok := remote.(fs.Copier)
|
|
|
|
if !ok {
|
|
|
|
t.Skip("FS has no Copier interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
var file1Copy = file1
|
|
|
|
file1Copy.Path += "-copy"
|
|
|
|
|
|
|
|
// do the copy
|
|
|
|
src := findObject(t, file1.Path)
|
|
|
|
dst, err := remote.(fs.Copier).Copy(src, file1Copy.Path)
|
|
|
|
if err != nil {
|
2015-10-30 16:40:06 +08:00
|
|
|
t.Fatalf("Copy failed: %v (%#v)", err, err)
|
2015-02-15 02:48:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check file exists in new listing
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file1, file2, file1Copy})
|
|
|
|
|
2016-01-17 19:27:31 +08:00
|
|
|
// Check dst lightly - list above has checked ModTime/Hashes
|
2015-02-15 02:48:08 +08:00
|
|
|
if dst.Remote() != file1Copy.Path {
|
|
|
|
t.Errorf("object path: want %q got %q", file1Copy.Path, dst.Remote())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete copy
|
|
|
|
err = dst.Remove()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Remove copy error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsMove tests Move
|
2015-09-01 04:05:51 +08:00
|
|
|
func TestFsMove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have Move
|
|
|
|
_, ok := remote.(fs.Mover)
|
|
|
|
if !ok {
|
|
|
|
t.Skip("FS has no Mover interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
var file1Move = file1
|
|
|
|
file1Move.Path += "-move"
|
|
|
|
|
|
|
|
// do the move
|
|
|
|
src := findObject(t, file1.Path)
|
|
|
|
dst, err := remote.(fs.Mover).Move(src, file1Move.Path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Move failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check file exists in new listing
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2, file1Move})
|
|
|
|
|
2016-01-17 19:27:31 +08:00
|
|
|
// Check dst lightly - list above has checked ModTime/Hashes
|
2015-09-01 04:05:51 +08:00
|
|
|
if dst.Remote() != file1Move.Path {
|
|
|
|
t.Errorf("object path: want %q got %q", file1Move.Path, dst.Remote())
|
|
|
|
}
|
|
|
|
|
|
|
|
// move it back
|
|
|
|
src = findObject(t, file1Move.Path)
|
|
|
|
_, err = remote.(fs.Mover).Move(src, file1.Path)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Move failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check file exists in new listing
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2, file1})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-23 01:47:16 +08:00
|
|
|
|
|
|
|
// TestFsDirMove tests DirMove
|
2015-09-01 04:05:51 +08:00
|
|
|
func TestFsDirMove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
|
|
|
|
// Check have DirMove
|
|
|
|
_, ok := remote.(fs.DirMover)
|
|
|
|
if !ok {
|
|
|
|
t.Skip("FS has no DirMover interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check it can't move onto itself
|
|
|
|
err := remote.(fs.DirMover).DirMove(remote)
|
|
|
|
if err != fs.ErrorDirExists {
|
|
|
|
t.Errorf("Expecting fs.ErrorDirExists got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// new remote
|
|
|
|
newRemote, removeNewRemote, err := fstest.RandomRemote(RemoteName, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create remote: %v", err)
|
|
|
|
}
|
|
|
|
defer removeNewRemote()
|
|
|
|
|
|
|
|
// try the move
|
|
|
|
err = newRemote.(fs.DirMover).DirMove(remote)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to DirMove: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check remotes
|
2015-09-11 17:37:12 +08:00
|
|
|
// FIXME: Prints errors.
|
2015-09-01 04:05:51 +08:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{})
|
|
|
|
fstest.CheckListing(t, newRemote, []fstest.Item{file2, file1})
|
|
|
|
|
|
|
|
// move it back
|
|
|
|
err = remote.(fs.DirMover).DirMove(newRemote)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to DirMove: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check remotes
|
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2, file1})
|
|
|
|
fstest.CheckListing(t, newRemote, []fstest.Item{})
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsRmdirFull tests removing a non empty directory
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsRmdirFull(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
err := remote.Rmdir()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expecting error on RMdir on non empty remote")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFsPrecision tests the Precision of the Fs
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFsPrecision(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
precision := remote.Precision()
|
2015-08-17 06:24:34 +08:00
|
|
|
if precision == fs.ModTimeNotSupported {
|
|
|
|
return
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
if precision > time.Second || precision < 0 {
|
|
|
|
t.Fatalf("Precision out of range %v", precision)
|
|
|
|
}
|
|
|
|
// FIXME check expected precision
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectString tests the Object String method
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectString(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
s := obj.String()
|
|
|
|
if s != file1.Path {
|
|
|
|
t.Errorf("String() wrong %v != %v", s, file1.Path)
|
|
|
|
}
|
|
|
|
obj = NilObject
|
|
|
|
s = obj.String()
|
|
|
|
if s != "<nil>" {
|
|
|
|
t.Errorf("String() wrong %v != %v", s, "<nil>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectFs tests the object can be found
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectFs(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2015-11-08 23:29:58 +08:00
|
|
|
equal := obj.Fs() == remote
|
|
|
|
if !equal {
|
|
|
|
// Check to see if this wraps something else
|
|
|
|
if unwrap, ok := remote.(fs.UnWrapper); ok {
|
|
|
|
equal = obj.Fs() == unwrap.UnWrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !equal {
|
2014-07-25 05:50:11 +08:00
|
|
|
t.Errorf("Fs is wrong %v != %v", obj.Fs(), remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectRemote tests the Remote is correct
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectRemote(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
if obj.Remote() != file1.Path {
|
|
|
|
t.Errorf("Remote is wrong %v != %v", obj.Remote(), file1.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 19:27:31 +08:00
|
|
|
// TestObjectHashes checks all the hashes the object supports
|
|
|
|
func TestObjectHashes(t *testing.T) {
|
2014-07-25 05:50:11 +08:00
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-01-17 19:27:31 +08:00
|
|
|
file1.CheckHashes(t, obj)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectModTime tests the ModTime of the object is correct
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectModTime(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
2014-08-02 00:58:39 +08:00
|
|
|
file1.CheckModTime(t, obj, obj.ModTime(), remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectSetModTime tests that SetModTime works
|
2014-07-25 05:50:11 +08: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 23:23:37 +08:00
|
|
|
err := obj.SetModTime(newModTime)
|
|
|
|
if err == fs.ErrorCantSetModTime {
|
|
|
|
t.Log(err)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
file1.ModTime = newModTime
|
2015-01-05 00:57:55 +08:00
|
|
|
file1.CheckModTime(t, obj, obj.ModTime(), remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
// And make a new object and read it from there too
|
|
|
|
TestObjectModTime(t)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectSize tests that Size works
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectSize(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
if obj.Size() != file1.Size {
|
|
|
|
t.Errorf("Size is wrong %v != %v", obj.Size(), file1.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectOpen tests that Open works
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectOpen(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
in, err := obj.Open()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Open() return error: %v", err)
|
|
|
|
}
|
2016-01-17 19:27:31 +08:00
|
|
|
hasher := fs.NewMultiHasher()
|
|
|
|
n, err := io.Copy(hasher, in)
|
2014-07-25 05:50:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("io.Copy() return error: %v", err)
|
|
|
|
}
|
|
|
|
if n != file1.Size {
|
|
|
|
t.Fatalf("Read wrong number of bytes %d != %d", n, file1.Size)
|
|
|
|
}
|
|
|
|
err = in.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("in.Close() return error: %v", err)
|
|
|
|
}
|
2016-01-17 19:27:31 +08:00
|
|
|
// Check content of file by comparing the calculated hashes
|
|
|
|
for hashType, got := range hasher.Sums() {
|
|
|
|
want := file1.Hashes[hashType]
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("%v is wrong %v != %v", hashType, want, got)
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2016-01-17 19:27:31 +08:00
|
|
|
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectUpdate tests that Update works
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectUpdate(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
buf := bytes.NewBufferString(fstest.RandomString(200))
|
2016-01-17 19:27:31 +08:00
|
|
|
hash := fs.NewMultiHasher()
|
2014-07-25 05:50:11 +08:00
|
|
|
in := io.TeeReader(buf, hash)
|
|
|
|
|
|
|
|
file1.Size = int64(buf.Len())
|
|
|
|
obj := findObject(t, file1.Path)
|
2016-02-18 19:35:25 +08:00
|
|
|
obji := fs.NewStaticObjectInfo("", file1.ModTime, file1.Size, true, nil, obj.Fs())
|
|
|
|
err := obj.Update(in, obji)
|
2014-07-25 05:50:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Update error", err)
|
|
|
|
}
|
2016-01-17 19:27:31 +08:00
|
|
|
file1.Hashes = hash.Sums()
|
2014-08-02 00:58:39 +08:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
// Re-read the object and check again
|
|
|
|
obj = findObject(t, file1.Path)
|
2014-08-02 00:58:39 +08:00
|
|
|
file1.Check(t, obj, remote.Precision())
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectStorable tests that Storable works
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectStorable(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
if !obj.Storable() {
|
|
|
|
t.Fatalf("Expecting %v to be storable", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestLimitedFs tests that a LimitedFs is created
|
2014-08-01 04:24:52 +08:00
|
|
|
func TestLimitedFs(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
remoteName := subRemoteName + "/" + file2.Path
|
|
|
|
file2Copy := file2
|
|
|
|
file2Copy.Path = "z.txt"
|
|
|
|
fileRemote, err := fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2015-02-28 23:35:54 +08:00
|
|
|
t.Fatalf("Failed to make remote %q: %v", remoteName, err)
|
2014-08-01 04:24:52 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, fileRemote, []fstest.Item{file2Copy})
|
2014-08-01 04:24:52 +08:00
|
|
|
_, ok := fileRemote.(*fs.Limited)
|
|
|
|
if !ok {
|
2015-11-08 23:29:58 +08:00
|
|
|
// Check to see if this wraps a Limited FS
|
|
|
|
if unwrap, hasUnWrap := fileRemote.(fs.UnWrapper); hasUnWrap {
|
|
|
|
_, ok = unwrap.UnWrap().(*fs.Limited)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%v is not a fs.Limited", fileRemote)
|
|
|
|
}
|
2014-08-01 04:24:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestLimitedFsNotFound tests that a LimitedFs is not created if no object
|
2014-08-01 04:24:52 +08:00
|
|
|
func TestLimitedFsNotFound(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
remoteName := subRemoteName + "/not found.txt"
|
|
|
|
fileRemote, err := fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2015-02-28 23:35:54 +08:00
|
|
|
t.Fatalf("Failed to make remote %q: %v", remoteName, err)
|
2014-08-01 04:24:52 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, fileRemote, []fstest.Item{})
|
2014-08-01 04:24:52 +08:00
|
|
|
_, ok := fileRemote.(*fs.Limited)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("%v is is a fs.Limited", fileRemote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectRemove tests Remove
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectRemove(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
|
|
|
obj := findObject(t, file1.Path)
|
|
|
|
err := obj.Remove()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Remove error", err)
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.CheckListing(t, remote, []fstest.Item{file2})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestObjectPurge tests Purge
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestObjectPurge(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-02 00:58:39 +08:00
|
|
|
fstest.TestPurge(t, remote)
|
2014-07-29 04:02:00 +08:00
|
|
|
err := fs.Purge(remote)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expecting error after on second purge")
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// TestFinalise tidies up after the previous tests
|
2014-07-25 05:50:11 +08:00
|
|
|
func TestFinalise(t *testing.T) {
|
|
|
|
skipIfNotOk(t)
|
2014-08-01 04:24:52 +08:00
|
|
|
if strings.HasPrefix(RemoteName, "/") {
|
|
|
|
// Remove temp directory
|
|
|
|
err := os.Remove(RemoteName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to remove %q: %v\n", RemoteName, err)
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
}
|