2014-03-28 22:28:16 +00:00
|
|
|
// Test rclone by doing real transactions to a storage provider to and
|
|
|
|
// from the local disk
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2014-07-24 21:50:11 +00:00
|
|
|
"github.com/ncw/rclone/fstest"
|
2014-03-28 22:28:16 +00:00
|
|
|
"github.com/ogier/pflag"
|
|
|
|
|
|
|
|
// Active file systems
|
|
|
|
_ "github.com/ncw/rclone/drive"
|
2014-07-08 20:59:30 +00:00
|
|
|
_ "github.com/ncw/rclone/dropbox"
|
2014-07-13 16:54:03 +00:00
|
|
|
_ "github.com/ncw/rclone/googlecloudstorage"
|
2014-03-28 22:28:16 +00:00
|
|
|
_ "github.com/ncw/rclone/local"
|
|
|
|
_ "github.com/ncw/rclone/s3"
|
|
|
|
_ "github.com/ncw/rclone/swift"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
localName, remoteName string
|
2014-04-24 16:59:05 +00:00
|
|
|
version = pflag.BoolP("version", "V", false, "Print the version number")
|
2014-07-19 11:39:31 +00:00
|
|
|
subDir = pflag.BoolP("subdir", "S", false, "Test with a sub directory")
|
2014-03-28 22:28:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Write a file
|
|
|
|
func WriteFile(filePath, content string, t time.Time) {
|
|
|
|
// FIXME make directories?
|
|
|
|
filePath = path.Join(localName, filePath)
|
2014-07-15 12:28:01 +00:00
|
|
|
dirPath := path.Dir(filePath)
|
|
|
|
err := os.MkdirAll(dirPath, 0770)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to make directories %q: %v", dirPath, err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(filePath, []byte(content), 0600)
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to write file %q: %v", filePath, err)
|
|
|
|
}
|
|
|
|
err = os.Chtimes(filePath, t, t)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to chtimes file %q: %v", filePath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
var t1 = fstest.Time("2001-02-03T04:05:06.499999999Z")
|
|
|
|
var t2 = fstest.Time("2011-12-25T12:59:59.123456789Z")
|
|
|
|
var t3 = fstest.Time("2011-12-30T12:59:59.000000000Z")
|
2014-03-28 22:28:16 +00:00
|
|
|
|
|
|
|
func TestCopy(flocal, fremote fs.Fs) {
|
2014-07-15 12:28:01 +00:00
|
|
|
WriteFile("sub dir/hello world", "hello world", t1)
|
2014-03-28 22:28:16 +00:00
|
|
|
|
2014-06-26 14:33:06 +00:00
|
|
|
// Check dry run is working
|
|
|
|
log.Printf("Copy with --dry-run")
|
|
|
|
fs.Config.DryRun = true
|
2014-04-18 15:34:59 +00:00
|
|
|
err := fs.Sync(fremote, flocal, false)
|
2014-06-26 14:33:06 +00:00
|
|
|
fs.Config.DryRun = false
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Copy failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
items := []fstest.Item{
|
2014-07-15 12:28:01 +00:00
|
|
|
{Path: "sub dir/hello world", Size: 11, ModTime: t1, Md5sum: "5eb63bbbe01eeed093cb22bb8f5acdc3"},
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
2014-06-26 14:33:06 +00:00
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, []fstest.Item{})
|
2014-06-26 14:33:06 +00:00
|
|
|
|
|
|
|
// Now without dry run
|
|
|
|
|
|
|
|
log.Printf("Copy")
|
|
|
|
err = fs.Sync(fremote, flocal, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Copy failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-07-15 12:28:01 +00:00
|
|
|
|
|
|
|
// Now delete the local file and download it
|
|
|
|
|
|
|
|
err = os.Remove(localName + "/sub dir/hello world")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Remove failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, []fstest.Item{})
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-07-15 12:28:01 +00:00
|
|
|
|
|
|
|
log.Printf("Copy - redownload")
|
|
|
|
err = fs.Sync(flocal, fremote, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Copy failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-07-15 12:28:01 +00:00
|
|
|
|
|
|
|
// Clean the directory
|
|
|
|
cleanTempDir()
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSync(flocal, fremote fs.Fs) {
|
2014-07-15 12:28:01 +00:00
|
|
|
WriteFile("empty space", "", t1)
|
|
|
|
|
2014-03-28 22:28:16 +00:00
|
|
|
log.Printf("Sync after changing file modtime only")
|
2014-05-16 15:27:25 +00:00
|
|
|
err := os.Chtimes(localName+"/empty space", t2, t2)
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Chtimes failed: %v", err)
|
|
|
|
}
|
2014-04-18 15:34:59 +00:00
|
|
|
err = fs.Sync(fremote, flocal, true)
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
items := []fstest.Item{
|
2014-05-16 15:27:25 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-03-28 22:28:16 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
log.Printf("Sync after adding a file")
|
2014-07-03 20:32:01 +00:00
|
|
|
WriteFile("potato", "------------------------------------------------------------", t3)
|
2014-04-18 15:34:59 +00:00
|
|
|
err = fs.Sync(fremote, flocal, true)
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
items = []fstest.Item{
|
2014-05-16 15:27:25 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
2014-07-03 20:32:01 +00:00
|
|
|
{Path: "potato", Size: 60, ModTime: t3, Md5sum: "d6548b156ea68a4e003e786df99eee76"},
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-03-28 22:28:16 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
log.Printf("Sync after changing a file's size only")
|
2014-07-03 20:32:01 +00:00
|
|
|
WriteFile("potato", "smaller but same date", t3)
|
2014-04-18 15:34:59 +00:00
|
|
|
err = fs.Sync(fremote, flocal, true)
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
items = []fstest.Item{
|
2014-05-16 15:27:25 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
2014-07-03 20:32:01 +00:00
|
|
|
{Path: "potato", Size: 21, ModTime: t3, Md5sum: "100defcf18c42a1e0dc42a789b107cd2"},
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-03-28 22:28:16 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-07-20 10:23:05 +00:00
|
|
|
log.Printf("Sync after changing a file's contents, modtime but not length")
|
|
|
|
WriteFile("potato", "SMALLER BUT SAME DATE", t2)
|
|
|
|
err = fs.Sync(fremote, flocal, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
items = []fstest.Item{
|
2014-07-20 10:23:05 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
|
|
|
{Path: "potato", Size: 21, ModTime: t2, Md5sum: "e4cb6955d9106df6263c45fcfc10f163"},
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-07-20 10:23:05 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-06-26 14:33:06 +00:00
|
|
|
log.Printf("Sync after removing a file and adding a file --dry-run")
|
|
|
|
WriteFile("potato2", "------------------------------------------------------------", t1)
|
2014-03-28 22:28:16 +00:00
|
|
|
err = os.Remove(localName + "/potato")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Remove failed: %v", err)
|
|
|
|
}
|
2014-06-26 14:33:06 +00:00
|
|
|
fs.Config.DryRun = true
|
2014-04-18 15:34:59 +00:00
|
|
|
err = fs.Sync(fremote, flocal, true)
|
2014-06-26 14:33:06 +00:00
|
|
|
fs.Config.DryRun = false
|
2014-03-28 22:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
before := []fstest.Item{
|
2014-06-26 14:33:06 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
2014-07-20 10:23:05 +00:00
|
|
|
{Path: "potato", Size: 21, ModTime: t2, Md5sum: "e4cb6955d9106df6263c45fcfc10f163"},
|
2014-06-26 14:33:06 +00:00
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
items = []fstest.Item{
|
2014-05-16 15:27:25 +00:00
|
|
|
{Path: "empty space", Size: 0, ModTime: t2, Md5sum: "d41d8cd98f00b204e9800998ecf8427e"},
|
2014-06-26 14:33:06 +00:00
|
|
|
{Path: "potato2", Size: 60, ModTime: t1, Md5sum: "d6548b156ea68a4e003e786df99eee76"},
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, before)
|
2014-03-28 22:28:16 +00:00
|
|
|
|
2014-06-26 14:33:06 +00:00
|
|
|
log.Printf("Sync after removing a file and adding a file")
|
|
|
|
err = fs.Sync(fremote, flocal, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Sync failed: %v", err)
|
|
|
|
}
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.CheckListing(flocal, items)
|
|
|
|
fstest.CheckListing(fremote, items)
|
2014-03-28 22:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLs(flocal, fremote fs.Fs) {
|
|
|
|
// Underlying List has been tested above, so we just make sure it runs
|
|
|
|
err := fs.List(fremote)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("List failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLsd(flocal, fremote fs.Fs) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheck(flocal, fremote fs.Fs) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func syntaxError() {
|
2014-04-24 16:59:05 +00:00
|
|
|
fmt.Fprintf(os.Stderr, `Test rclone with a remote to find bugs in either - %s.
|
2014-03-28 22:28:16 +00:00
|
|
|
|
|
|
|
Syntax: [options] remote:
|
|
|
|
|
|
|
|
Need a remote: as argument. This will create a random container or
|
|
|
|
directory under it and perform tests on it, deleting it at the end.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-07-13 17:58:06 +00:00
|
|
|
`, fs.Version)
|
2014-03-28 22:28:16 +00:00
|
|
|
pflag.PrintDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean the temporary directory
|
|
|
|
func cleanTempDir() {
|
|
|
|
log.Printf("Cleaning temporary directory: %q", localName)
|
|
|
|
err := os.RemoveAll(localName)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to remove %q: %v", localName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pflag.Usage = syntaxError
|
|
|
|
pflag.Parse()
|
2014-04-24 16:59:05 +00:00
|
|
|
if *version {
|
2014-07-13 17:58:06 +00:00
|
|
|
fmt.Printf("rclonetest %s\n", fs.Version)
|
2014-04-24 16:59:05 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2014-03-28 22:28:16 +00:00
|
|
|
fs.LoadConfig()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
args := pflag.Args()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
syntaxError()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fremote, finalise := fstest.RandomRemote(args[0], *subDir)
|
|
|
|
log.Printf("Testing with remote %v", fremote)
|
|
|
|
|
2014-03-28 22:28:16 +00:00
|
|
|
var err error
|
|
|
|
localName, err = ioutil.TempDir("", "rclone")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
log.Printf("Testing with local %q", localName)
|
|
|
|
flocal, err := fs.NewFs(localName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to make %q: %v", remoteName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.CalculateModifyWindow(fremote, flocal)
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
fstest.TestMkdir(fremote)
|
2014-03-28 22:28:16 +00:00
|
|
|
TestCopy(flocal, fremote)
|
|
|
|
TestSync(flocal, fremote)
|
|
|
|
TestLs(flocal, fremote)
|
|
|
|
TestLsd(flocal, fremote)
|
|
|
|
TestCheck(flocal, fremote)
|
|
|
|
//TestRmdir(flocal, fremote)
|
|
|
|
|
2014-07-24 21:50:11 +00:00
|
|
|
finalise()
|
2014-07-19 11:39:31 +00:00
|
|
|
|
2014-03-28 22:28:16 +00:00
|
|
|
cleanTempDir()
|
|
|
|
log.Printf("Tests OK")
|
|
|
|
}
|