2015-09-22 19:47:16 +02:00
|
|
|
// Package fstest provides utilities for testing the Fs
|
2014-07-24 23:50:11 +02:00
|
|
|
package fstest
|
|
|
|
|
|
|
|
// FIXME put name of test FS in Fs structure
|
|
|
|
|
|
|
|
import (
|
2016-01-17 11:08:28 +01:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2014-07-24 23:50:11 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2015-02-07 16:52:06 +01:00
|
|
|
"path/filepath"
|
2016-01-24 13:37:46 +01:00
|
|
|
"regexp"
|
2014-07-24 23:50:11 +02:00
|
|
|
"strings"
|
2014-08-01 18:58:39 +02:00
|
|
|
"testing"
|
2014-07-24 23:50:11 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
2016-01-24 13:37:46 +01:00
|
|
|
var (
|
|
|
|
// MatchTestRemote matches the remote names used for testing
|
|
|
|
MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{24}$`)
|
|
|
|
)
|
|
|
|
|
2014-07-26 18:18:29 +02:00
|
|
|
// Seed the random number generator
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Item represents an item for checking
|
2014-07-24 23:50:11 +02:00
|
|
|
type Item struct {
|
|
|
|
Path string
|
2016-01-17 12:27:31 +01:00
|
|
|
Hashes map[fs.HashType]string
|
2014-07-24 23:50:11 +02:00
|
|
|
ModTime time.Time
|
|
|
|
Size int64
|
2015-09-11 11:37:12 +02:00
|
|
|
WinPath string
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-17 11:08:28 +01:00
|
|
|
// NewItem creates an item from a string content
|
|
|
|
func NewItem(Path, Content string, modTime time.Time) Item {
|
|
|
|
i := Item{
|
|
|
|
Path: Path,
|
|
|
|
ModTime: modTime,
|
|
|
|
Size: int64(len(Content)),
|
|
|
|
}
|
|
|
|
hash := fs.NewMultiHasher()
|
|
|
|
buf := bytes.NewBufferString(Content)
|
|
|
|
_, err := io.Copy(hash, buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create item: %v", err)
|
|
|
|
}
|
|
|
|
i.Hashes = hash.Sums()
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CheckTimeEqualWithPrecision checks the times are equal within the
|
|
|
|
// precision, returns the delta and a flag
|
2015-08-17 00:24:34 +02:00
|
|
|
func CheckTimeEqualWithPrecision(t0, t1 time.Time, precision time.Duration) (time.Duration, bool) {
|
|
|
|
dt := t0.Sub(t1)
|
|
|
|
if dt >= precision || dt <= -precision {
|
|
|
|
return dt, false
|
|
|
|
}
|
|
|
|
return dt, true
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CheckModTime checks the mod time to the given precision
|
2014-08-01 18:58:39 +02:00
|
|
|
func (i *Item) CheckModTime(t *testing.T, obj fs.Object, modTime time.Time, precision time.Duration) {
|
2015-08-17 00:24:34 +02:00
|
|
|
dt, ok := CheckTimeEqualWithPrecision(modTime, i.ModTime, precision)
|
|
|
|
if !ok {
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Errorf("%s: Modification time difference too big |%s| > %s (%s vs %s) (precision %s)", obj.Remote(), dt, precision, modTime, i.ModTime, precision)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 12:27:31 +01:00
|
|
|
// CheckHashes checks all the hashes the object supports are correct
|
|
|
|
func (i *Item) CheckHashes(t *testing.T, obj fs.Object) {
|
2014-07-24 23:50:11 +02:00
|
|
|
if obj == nil {
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Fatalf("Object is nil")
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2016-01-11 13:39:33 +01:00
|
|
|
types := obj.Fs().Hashes().Array()
|
|
|
|
for _, hash := range types {
|
|
|
|
// Check attributes
|
|
|
|
sum, err := obj.Hash(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: Failed to read hash %v for %q: %v", obj.Fs().String(), hash, obj.Remote(), err)
|
|
|
|
}
|
2016-01-17 12:27:31 +01:00
|
|
|
if !fs.HashEquals(i.Hashes[hash], sum) {
|
|
|
|
t.Errorf("%s/%s: %v hash incorrect - expecting %q got %q", obj.Fs().String(), obj.Remote(), hash, i.Hashes[hash], sum)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2016-01-17 12:27:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check checks all the attributes of the object are correct
|
|
|
|
func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) {
|
|
|
|
i.CheckHashes(t, obj)
|
2014-07-24 23:50:11 +02:00
|
|
|
if i.Size != obj.Size() {
|
2016-01-11 13:39:33 +01:00
|
|
|
t.Errorf("%s/%s: Size incorrect - expecting %d got %d", obj.Fs().String(), obj.Remote(), i.Size, obj.Size())
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
i.CheckModTime(t, obj, obj.ModTime(), precision)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Items represents all items for checking
|
2014-07-24 23:50:11 +02:00
|
|
|
type Items struct {
|
2015-09-11 11:37:12 +02:00
|
|
|
byName map[string]*Item
|
|
|
|
byNameAlt map[string]*Item
|
|
|
|
items []Item
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// NewItems makes an Items
|
2014-07-24 23:50:11 +02:00
|
|
|
func NewItems(items []Item) *Items {
|
|
|
|
is := &Items{
|
2015-09-11 11:37:12 +02:00
|
|
|
byName: make(map[string]*Item),
|
|
|
|
byNameAlt: make(map[string]*Item),
|
|
|
|
items: items,
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
// Fill up byName
|
|
|
|
for i := range items {
|
|
|
|
is.byName[items[i].Path] = &items[i]
|
2015-09-11 11:37:12 +02:00
|
|
|
is.byNameAlt[items[i].WinPath] = &items[i]
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
return is
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Find checks off an item
|
2014-08-01 18:58:39 +02:00
|
|
|
func (is *Items) Find(t *testing.T, obj fs.Object, precision time.Duration) {
|
2014-07-24 23:50:11 +02:00
|
|
|
i, ok := is.byName[obj.Remote()]
|
|
|
|
if !ok {
|
2015-09-11 11:37:12 +02:00
|
|
|
i, ok = is.byNameAlt[obj.Remote()]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected file %q", obj.Remote())
|
|
|
|
return
|
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
delete(is.byName, i.Path)
|
|
|
|
delete(is.byName, i.WinPath)
|
2014-08-01 18:58:39 +02:00
|
|
|
i.Check(t, obj, precision)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Done checks all finished
|
2014-08-01 18:58:39 +02:00
|
|
|
func (is *Items) Done(t *testing.T) {
|
2014-07-24 23:50:11 +02:00
|
|
|
if len(is.byName) != 0 {
|
|
|
|
for name := range is.byName {
|
2015-12-29 01:16:53 +01:00
|
|
|
t.Logf("Not found %q", name)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Errorf("%d objects not found", len(is.byName))
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CheckListingWithPrecision checks the fs to see if it has the
|
|
|
|
// expected contents with the given precision.
|
2014-08-01 18:58:39 +02:00
|
|
|
func CheckListingWithPrecision(t *testing.T, f fs.Fs, items []Item, precision time.Duration) {
|
2014-07-24 23:50:11 +02:00
|
|
|
is := NewItems(items)
|
2015-08-24 22:42:23 +02:00
|
|
|
oldErrors := fs.Stats.GetErrors()
|
2015-09-14 22:01:25 +02:00
|
|
|
var objs []fs.Object
|
2016-01-17 11:08:28 +01:00
|
|
|
const retries = 6
|
|
|
|
sleep := time.Second / 2
|
2015-11-14 13:57:17 +01:00
|
|
|
for i := 1; i <= retries; i++ {
|
2015-09-14 22:01:25 +02:00
|
|
|
objs = nil
|
|
|
|
for obj := range f.List() {
|
|
|
|
objs = append(objs, obj)
|
|
|
|
}
|
|
|
|
if len(objs) == len(items) {
|
2016-01-17 11:08:28 +01:00
|
|
|
// Put an extra sleep in if we did any retries just to make sure it really
|
|
|
|
// is consistent (here is looking at you Amazon Cloud Drive!)
|
|
|
|
if i != 1 {
|
|
|
|
extraSleep := 5*time.Second + sleep
|
|
|
|
t.Logf("Sleeping for %v just to make sure", extraSleep)
|
|
|
|
time.Sleep(extraSleep)
|
|
|
|
}
|
2015-09-14 22:01:25 +02:00
|
|
|
break
|
|
|
|
}
|
2016-01-17 11:08:28 +01:00
|
|
|
sleep *= 2
|
|
|
|
t.Logf("Sleeping for %v for list eventual consistency: %d/%d", sleep, i, retries)
|
|
|
|
time.Sleep(sleep)
|
2015-09-14 22:01:25 +02:00
|
|
|
}
|
|
|
|
for _, obj := range objs {
|
2015-01-03 17:28:26 +01:00
|
|
|
if obj == nil {
|
|
|
|
t.Errorf("Unexpected nil in List()")
|
|
|
|
continue
|
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
is.Find(t, obj, precision)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
is.Done(t)
|
2015-08-24 22:42:23 +02:00
|
|
|
// Don't notice an error when listing an empty directory
|
|
|
|
if len(items) == 0 && oldErrors == 0 && fs.Stats.GetErrors() == 1 {
|
|
|
|
fs.Stats.ResetErrors()
|
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CheckListing checks the fs to see if it has the expected contents
|
2014-08-01 18:58:39 +02:00
|
|
|
func CheckListing(t *testing.T, f fs.Fs, items []Item) {
|
|
|
|
precision := f.Precision()
|
|
|
|
CheckListingWithPrecision(t, f, items, precision)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-17 11:08:28 +01:00
|
|
|
// CheckItems checks the fs to see if it has only the items passed in
|
|
|
|
// using a precision of fs.Config.ModifyWindow
|
|
|
|
func CheckItems(t *testing.T, f fs.Fs, items ...Item) {
|
|
|
|
CheckListingWithPrecision(t, f, items, fs.Config.ModifyWindow)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Time parses a time string or logs a fatal error
|
2014-07-24 23:50:11 +02:00
|
|
|
func Time(timeString string) time.Time {
|
|
|
|
t, err := time.Parse(time.RFC3339Nano, timeString)
|
|
|
|
if err != nil {
|
2014-08-01 18:58:39 +02:00
|
|
|
log.Fatalf("Failed to parse time %q: %v", timeString, err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// RandomString create a random string for test purposes
|
2014-07-24 23:50:11 +02:00
|
|
|
func RandomString(n int) string {
|
2016-01-24 13:37:46 +01:00
|
|
|
const (
|
|
|
|
vowel = "aeiou"
|
|
|
|
consonant = "bcdfghjklmnpqrstvwxyz"
|
|
|
|
digit = "0123456789"
|
|
|
|
)
|
|
|
|
pattern := []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit}
|
2014-07-24 23:50:11 +02:00
|
|
|
out := make([]byte, n)
|
2016-01-24 13:37:46 +01:00
|
|
|
p := 0
|
2014-07-24 23:50:11 +02:00
|
|
|
for i := range out {
|
2016-01-24 13:37:46 +01:00
|
|
|
source := pattern[p]
|
|
|
|
p = (p + 1) % len(pattern)
|
2014-07-24 23:50:11 +02:00
|
|
|
out[i] = source[rand.Intn(len(source))]
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// LocalRemote creates a temporary directory name for local remotes
|
2014-07-31 22:24:52 +02:00
|
|
|
func LocalRemote() (path string, err error) {
|
|
|
|
path, err = ioutil.TempDir("", "rclone")
|
|
|
|
if err == nil {
|
|
|
|
// Now remove the directory
|
|
|
|
err = os.Remove(path)
|
|
|
|
}
|
2015-02-07 16:52:06 +01:00
|
|
|
path = filepath.ToSlash(path)
|
2014-07-31 22:24:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// RandomRemoteName makes a random bucket or subdirectory name
|
2014-07-24 23:50:11 +02:00
|
|
|
//
|
2014-07-31 22:24:52 +02:00
|
|
|
// Returns a random remote name plus the leaf name
|
|
|
|
func RandomRemoteName(remoteName string) (string, string, error) {
|
2014-07-24 23:50:11 +02:00
|
|
|
var err error
|
2014-07-31 22:24:52 +02:00
|
|
|
var leafName string
|
|
|
|
|
|
|
|
// Make a directory if remote name is null
|
2014-07-24 23:50:11 +02:00
|
|
|
if remoteName == "" {
|
2014-07-31 22:24:52 +02:00
|
|
|
remoteName, err = LocalRemote()
|
2014-07-24 23:50:11 +02:00
|
|
|
if err != nil {
|
2014-07-31 22:24:52 +02:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.HasSuffix(remoteName, ":") {
|
|
|
|
remoteName += "/"
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2016-01-24 13:37:46 +01:00
|
|
|
leafName = "rclone-test-" + RandomString(24)
|
|
|
|
if !MatchTestRemote.MatchString(leafName) {
|
|
|
|
log.Fatalf("%q didn't match the test remote name regexp", leafName)
|
|
|
|
}
|
2014-07-31 22:24:52 +02:00
|
|
|
remoteName += leafName
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-07-31 22:24:52 +02:00
|
|
|
return remoteName, leafName, nil
|
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// RandomRemote makes a random bucket or subdirectory on the remote
|
2014-07-31 22:24:52 +02:00
|
|
|
//
|
|
|
|
// Call the finalise function returned to Purge the fs at the end (and
|
|
|
|
// the parent if necessary)
|
|
|
|
func RandomRemote(remoteName string, subdir bool) (fs.Fs, func(), error) {
|
|
|
|
var err error
|
2014-07-24 23:50:11 +02:00
|
|
|
var parentRemote fs.Fs
|
2014-07-31 22:24:52 +02:00
|
|
|
|
|
|
|
remoteName, _, err = RandomRemoteName(remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-24 23:50:11 +02:00
|
|
|
if subdir {
|
|
|
|
parentRemote, err = fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2014-07-31 09:51:39 +02:00
|
|
|
return nil, nil, err
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2016-01-24 13:37:46 +01:00
|
|
|
remoteName += "/rclone-test-subdir-" + RandomString(8)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
remote, err := fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2014-07-31 09:51:39 +02:00
|
|
|
return nil, nil, err
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
finalise := func() {
|
2014-07-28 22:02:00 +02:00
|
|
|
_ = fs.Purge(remote) // ignore error
|
2014-07-24 23:50:11 +02:00
|
|
|
if parentRemote != nil {
|
2014-07-28 22:02:00 +02:00
|
|
|
err = fs.Purge(parentRemote) // ignore error
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to purge %v: %v", parentRemote, err)
|
|
|
|
}
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:51:39 +02:00
|
|
|
return remote, finalise, nil
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestMkdir tests Mkdir works
|
2014-08-01 18:58:39 +02:00
|
|
|
func TestMkdir(t *testing.T, remote fs.Fs) {
|
2014-07-24 23:50:11 +02:00
|
|
|
err := fs.Mkdir(remote)
|
|
|
|
if err != nil {
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Fatalf("Mkdir failed: %v", err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
CheckListing(t, remote, []Item{})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestPurge tests Purge works
|
2014-08-01 18:58:39 +02:00
|
|
|
func TestPurge(t *testing.T, remote fs.Fs) {
|
2014-07-24 23:50:11 +02:00
|
|
|
err := fs.Purge(remote)
|
|
|
|
if err != nil {
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Fatalf("Purge failed: %v", err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
2014-08-01 18:58:39 +02:00
|
|
|
CheckListing(t, remote, []Item{})
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// TestRmdir tests Rmdir works
|
2014-08-01 18:58:39 +02:00
|
|
|
func TestRmdir(t *testing.T, remote fs.Fs) {
|
2014-07-24 23:50:11 +02:00
|
|
|
err := fs.Rmdir(remote)
|
|
|
|
if err != nil {
|
2014-08-01 18:58:39 +02:00
|
|
|
t.Fatalf("Rmdir failed: %v", err)
|
2014-07-24 23:50:11 +02:00
|
|
|
}
|
|
|
|
}
|