2015-06-06 21:24:30 +08:00
|
|
|
// Utilities for testing the Fs
|
2014-07-25 05:50:11 +08:00
|
|
|
package fstest
|
|
|
|
|
|
|
|
// FIXME put name of test FS in Fs structure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2015-02-07 23:52:06 +08:00
|
|
|
"path/filepath"
|
2014-07-25 05:50:11 +08:00
|
|
|
"strings"
|
2014-08-02 00:58:39 +08:00
|
|
|
"testing"
|
2014-07-25 05:50:11 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
2014-07-27 00:18:29 +08:00
|
|
|
// Seed the random number generator
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-25 05:50:11 +08:00
|
|
|
// Represents an item for checking
|
|
|
|
type Item struct {
|
|
|
|
Path string
|
|
|
|
Md5sum string
|
|
|
|
ModTime time.Time
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:18:29 +08:00
|
|
|
// check the mod time to the given precision
|
2014-08-02 00:58:39 +08:00
|
|
|
func (i *Item) CheckModTime(t *testing.T, obj fs.Object, modTime time.Time, precision time.Duration) {
|
2014-07-27 00:18:29 +08:00
|
|
|
dt := modTime.Sub(i.ModTime)
|
|
|
|
if dt >= precision || dt <= -precision {
|
2014-08-02 00:58:39 +08: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-27 00:18:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) {
|
2014-07-25 05:50:11 +08:00
|
|
|
if obj == nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Fatalf("Object is nil")
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
// Check attributes
|
|
|
|
Md5sum, err := obj.Md5sum()
|
|
|
|
if err != nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Fatalf("Failed to read md5sum for %q: %v", obj.Remote(), err)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
if i.Md5sum != Md5sum {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("%s: Md5sum incorrect - expecting %q got %q", obj.Remote(), i.Md5sum, Md5sum)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
if i.Size != obj.Size() {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("%s: Size incorrect - expecting %d got %d", obj.Remote(), i.Size, obj.Size())
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
i.CheckModTime(t, obj, obj.ModTime(), precision)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents all items for checking
|
|
|
|
type Items struct {
|
|
|
|
byName map[string]*Item
|
|
|
|
items []Item
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make an Items
|
|
|
|
func NewItems(items []Item) *Items {
|
|
|
|
is := &Items{
|
|
|
|
byName: make(map[string]*Item),
|
|
|
|
items: items,
|
|
|
|
}
|
|
|
|
// Fill up byName
|
|
|
|
for i := range items {
|
|
|
|
is.byName[items[i].Path] = &items[i]
|
|
|
|
}
|
|
|
|
return is
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check off an item
|
2014-08-02 00:58:39 +08:00
|
|
|
func (is *Items) Find(t *testing.T, obj fs.Object, precision time.Duration) {
|
2014-07-25 05:50:11 +08:00
|
|
|
i, ok := is.byName[obj.Remote()]
|
|
|
|
if !ok {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("Unexpected file %q", obj.Remote())
|
2015-02-07 23:52:06 +08:00
|
|
|
return
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
delete(is.byName, obj.Remote())
|
2014-08-02 00:58:39 +08:00
|
|
|
i.Check(t, obj, precision)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check all done
|
2014-08-02 00:58:39 +08:00
|
|
|
func (is *Items) Done(t *testing.T) {
|
2014-07-25 05:50:11 +08:00
|
|
|
if len(is.byName) != 0 {
|
|
|
|
for name := range is.byName {
|
|
|
|
log.Printf("Not found %q", name)
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Errorf("%d objects not found", len(is.byName))
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks the fs to see if it has the expected contents
|
2014-08-02 00:58:39 +08:00
|
|
|
func CheckListingWithPrecision(t *testing.T, f fs.Fs, items []Item, precision time.Duration) {
|
2014-07-25 05:50:11 +08:00
|
|
|
is := NewItems(items)
|
|
|
|
for obj := range f.List() {
|
2015-01-04 00:28:26 +08:00
|
|
|
if obj == nil {
|
|
|
|
t.Errorf("Unexpected nil in List()")
|
|
|
|
continue
|
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
is.Find(t, obj, precision)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
is.Done(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks the fs to see if it has the expected contents
|
|
|
|
func CheckListing(t *testing.T, f fs.Fs, items []Item) {
|
|
|
|
precision := f.Precision()
|
|
|
|
CheckListingWithPrecision(t, f, items, precision)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a time string or explode
|
|
|
|
func Time(timeString string) time.Time {
|
|
|
|
t, err := time.Parse(time.RFC3339Nano, timeString)
|
|
|
|
if err != nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
log.Fatalf("Failed to parse time %q: %v", timeString, err)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a random string
|
|
|
|
func RandomString(n int) string {
|
|
|
|
source := "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
out := make([]byte, n)
|
|
|
|
for i := range out {
|
|
|
|
out[i] = source[rand.Intn(len(source))]
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2014-08-01 04:24:52 +08:00
|
|
|
// Creates a temporary directory name for local remotes
|
|
|
|
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 23:52:06 +08:00
|
|
|
path = filepath.ToSlash(path)
|
2014-08-01 04:24:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a random bucket or subdirectory name
|
2014-07-25 05:50:11 +08:00
|
|
|
//
|
2014-08-01 04:24:52 +08:00
|
|
|
// Returns a random remote name plus the leaf name
|
|
|
|
func RandomRemoteName(remoteName string) (string, string, error) {
|
2014-07-25 05:50:11 +08:00
|
|
|
var err error
|
2014-08-01 04:24:52 +08:00
|
|
|
var leafName string
|
|
|
|
|
|
|
|
// Make a directory if remote name is null
|
2014-07-25 05:50:11 +08:00
|
|
|
if remoteName == "" {
|
2014-08-01 04:24:52 +08:00
|
|
|
remoteName, err = LocalRemote()
|
2014-07-25 05:50:11 +08:00
|
|
|
if err != nil {
|
2014-08-01 04:24:52 +08:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.HasSuffix(remoteName, ":") {
|
|
|
|
remoteName += "/"
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-01 04:24:52 +08:00
|
|
|
leafName = RandomString(32)
|
|
|
|
remoteName += leafName
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-01 04:24:52 +08:00
|
|
|
return remoteName, leafName, nil
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
|
2014-08-01 04:24:52 +08:00
|
|
|
// Make a random bucket or subdirectory on the remote
|
|
|
|
//
|
|
|
|
// 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-25 05:50:11 +08:00
|
|
|
var parentRemote fs.Fs
|
2014-08-01 04:24:52 +08:00
|
|
|
|
|
|
|
remoteName, _, err = RandomRemoteName(remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-25 05:50:11 +08:00
|
|
|
if subdir {
|
|
|
|
parentRemote, err = fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2014-07-31 15:51:39 +08:00
|
|
|
return nil, nil, err
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
remoteName += "/" + RandomString(8)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote, err := fs.NewFs(remoteName)
|
|
|
|
if err != nil {
|
2014-07-31 15:51:39 +08:00
|
|
|
return nil, nil, err
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
finalise := func() {
|
2014-07-29 04:02:00 +08:00
|
|
|
_ = fs.Purge(remote) // ignore error
|
2014-07-25 05:50:11 +08:00
|
|
|
if parentRemote != nil {
|
2014-07-29 04:02:00 +08:00
|
|
|
err = fs.Purge(parentRemote) // ignore error
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to purge %v: %v", parentRemote, err)
|
|
|
|
}
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 15:51:39 +08:00
|
|
|
return remote, finalise, nil
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestMkdir(t *testing.T, remote fs.Fs) {
|
2014-07-25 05:50:11 +08:00
|
|
|
err := fs.Mkdir(remote)
|
|
|
|
if err != nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Fatalf("Mkdir failed: %v", err)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
CheckListing(t, remote, []Item{})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestPurge(t *testing.T, remote fs.Fs) {
|
2014-07-25 05:50:11 +08:00
|
|
|
err := fs.Purge(remote)
|
|
|
|
if err != nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Fatalf("Purge failed: %v", err)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
2014-08-02 00:58:39 +08:00
|
|
|
CheckListing(t, remote, []Item{})
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 00:58:39 +08:00
|
|
|
func TestRmdir(t *testing.T, remote fs.Fs) {
|
2014-07-25 05:50:11 +08:00
|
|
|
err := fs.Rmdir(remote)
|
|
|
|
if err != nil {
|
2014-08-02 00:58:39 +08:00
|
|
|
t.Fatalf("Rmdir failed: %v", err)
|
2014-07-25 05:50:11 +08:00
|
|
|
}
|
|
|
|
}
|