From cd4895690ab178021107aa51ba497e7b3a043b8e Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 24 Jul 2017 22:46:43 +0100 Subject: [PATCH] fstest: Factor test initialisation into Initialise() --- cmd/mountlib/mounttest/fs.go | 30 +++++------------------------- fs/operations_test.go | 36 +++++++----------------------------- fs/sync_test.go | 6 +++--- fstest/fstest.go | 29 +++++++++++++++++++++++++++-- fstest/fstests/fstests.go | 25 ++++++------------------- 5 files changed, 48 insertions(+), 78 deletions(-) diff --git a/cmd/mountlib/mounttest/fs.go b/cmd/mountlib/mounttest/fs.go index 4739badb2..2d037fc03 100644 --- a/cmd/mountlib/mounttest/fs.go +++ b/cmd/mountlib/mounttest/fs.go @@ -23,17 +23,6 @@ import ( "github.com/stretchr/testify/require" ) -// Globals -var ( - RemoteName = flag.String("remote", "", "Remote to test with, defaults to local filesystem") - SubDir = flag.Bool("subdir", false, "Set to test with a sub directory") - Verbose = flag.Bool("verbose", false, "Set to enable logging") - DumpHeaders = flag.Bool("dump-headers", false, "Set to dump headers (needs -verbose)") - DumpBodies = flag.Bool("dump-bodies", false, "Set to dump bodies (needs -verbose)") - Individual = flag.Bool("individual", false, "Make individual bucket/container/directory for each test - much slower") - LowLevelRetries = flag.Int("low-level-retries", 10, "Number of low level retries") -) - type ( // UnmountFn is called to unmount the file system UnmountFn func() error @@ -81,26 +70,17 @@ func newRun() *Run { umountResult: make(chan error, 1), } - // 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 - fs.LoadConfig() - if *Verbose { - fs.Config.LogLevel = fs.LogLevelDebug - } - fs.Config.DumpHeaders = *DumpHeaders - fs.Config.DumpBodies = *DumpBodies - fs.Config.LowLevelRetries = *LowLevelRetries + fstest.Initialise() + var err error - r.fremote, r.fremoteName, r.cleanRemote, err = fstest.RandomRemote(*RemoteName, *SubDir) + r.fremote, r.fremoteName, r.cleanRemote, err = fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) if err != nil { - log.Fatalf("Failed to open remote %q: %v", *RemoteName, err) + log.Fatalf("Failed to open remote %q: %v", *fstest.RemoteName, err) } err = r.fremote.Mkdir("") if err != nil { - log.Fatalf("Failed to open mkdir %q: %v", *RemoteName, err) + log.Fatalf("Failed to open mkdir %q: %v", *fstest.RemoteName, err) } if runtime.GOOS != "windows" { diff --git a/fs/operations_test.go b/fs/operations_test.go index ca9774306..7702cef75 100644 --- a/fs/operations_test.go +++ b/fs/operations_test.go @@ -43,18 +43,6 @@ import ( "github.com/stretchr/testify/require" ) -// Globals -var ( - RemoteName = flag.String("remote", "", "Remote to test with, defaults to local filesystem") - SubDir = flag.Bool("subdir", false, "Set to test with a sub directory") - Verbose = flag.Bool("verbose", false, "Set to enable logging") - DumpHeaders = flag.Bool("dump-headers", false, "Set to dump headers (needs -verbose)") - DumpBodies = flag.Bool("dump-bodies", false, "Set to dump bodies (needs -verbose)") - Individual = flag.Bool("individual", false, "Make individual bucket/container/directory for each test - much slower") - LowLevelRetries = flag.Int("low-level-retries", 10, "Number of low level retries") - UseListR = flag.Bool("fast-list", false, "Use recursive list if available. Uses more memory but fewer transactions.") -) - // Some times used in the tests var ( t1 = fstest.Time("2001-02-03T04:05:06.499999999Z") @@ -65,11 +53,11 @@ var ( // TestMain drives the tests func TestMain(m *testing.M) { flag.Parse() - if !*Individual { + if !*fstest.Individual { oneRun = newRun() } rc := m.Run() - if !*Individual { + if !*fstest.Individual { oneRun.Finalise() } os.Exit(rc) @@ -103,22 +91,12 @@ func newRun() *Run { mkdir: make(map[string]bool), } - // 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 - fs.LoadConfig() - if *Verbose { - fs.Config.LogLevel = fs.LogLevelDebug - } - fs.Config.DumpHeaders = *DumpHeaders - fs.Config.DumpBodies = *DumpBodies - fs.Config.LowLevelRetries = *LowLevelRetries - fs.Config.UseListR = *UseListR + fstest.Initialise() + var err error - r.fremote, r.fremoteName, r.cleanRemote, err = fstest.RandomRemote(*RemoteName, *SubDir) + r.fremote, r.fremoteName, r.cleanRemote, err = fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) if err != nil { - r.Fatalf("Failed to open remote %q: %v", *RemoteName, err) + r.Fatalf("Failed to open remote %q: %v", *fstest.RemoteName, err) } r.localName, err = ioutil.TempDir("", "rclone") @@ -150,7 +128,7 @@ func (d dirsToRemove) Less(i, j int) bool { return len(d[i]) > len(d[j]) } // Finalise() will tidy them away when done. func NewRun(t *testing.T) *Run { var r *Run - if *Individual { + if *fstest.Individual { r = newRun() } else { // If not individual, use the global one with the clean method overridden diff --git a/fs/sync_test.go b/fs/sync_test.go index bc0b527bc..6e980670a 100644 --- a/fs/sync_test.go +++ b/fs/sync_test.go @@ -102,7 +102,7 @@ func TestServerSideCopy(t *testing.T) { file1 := r.WriteObject("sub dir/hello world", "hello world", t1) fstest.CheckItems(t, r.fremote, file1) - fremoteCopy, _, finaliseCopy, err := fstest.RandomRemote(*RemoteName, *SubDir) + fremoteCopy, _, finaliseCopy, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) require.NoError(t, err) defer finaliseCopy() t.Logf("Server side copy (if possible) %v -> %v", r.fremote, fremoteCopy) @@ -735,7 +735,7 @@ func testServerSideMove(t *testing.T, r *Run, fremoteMove fs.Fs, withFilter bool func TestServerSideMove(t *testing.T) { r := NewRun(t) defer r.Finalise() - fremoteMove, _, finaliseMove, err := fstest.RandomRemote(*RemoteName, *SubDir) + fremoteMove, _, finaliseMove, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) require.NoError(t, err) defer finaliseMove() testServerSideMove(t, r, fremoteMove, false) @@ -751,7 +751,7 @@ func TestServerSideMoveWithFilter(t *testing.T) { fs.Config.Filter.MinSize = -1 }() - fremoteMove, _, finaliseMove, err := fstest.RandomRemote(*RemoteName, *SubDir) + fremoteMove, _, finaliseMove, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) require.NoError(t, err) defer finaliseMove() testServerSideMove(t, r, fremoteMove, true) diff --git a/fstest/fstest.go b/fstest/fstest.go index 5cbc164df..8a03554e8 100644 --- a/fstest/fstest.go +++ b/fstest/fstest.go @@ -24,11 +24,20 @@ import ( "github.com/stretchr/testify/require" ) +// Globals var ( - // MatchTestRemote matches the remote names used for testing - MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{24}$`) + RemoteName = flag.String("remote", "", "Remote to test with, defaults to local filesystem") + SubDir = flag.Bool("subdir", false, "Set to test with a sub directory") + Verbose = flag.Bool("verbose", false, "Set to enable logging") + DumpHeaders = flag.Bool("dump-headers", false, "Set to dump headers (needs -verbose)") + DumpBodies = flag.Bool("dump-bodies", false, "Set to dump bodies (needs -verbose)") + Individual = flag.Bool("individual", false, "Make individual bucket/container/directory for each test - much slower") + LowLevelRetries = flag.Int("low-level-retries", 10, "Number of low level retries") + UseListR = flag.Bool("fast-list", false, "Use recursive list if available. Uses more memory but fewer transactions.") // ListRetries is the number of times to retry a listing to overcome eventual consistency ListRetries = flag.Int("list-retries", 6, "Number or times to retry listing") + // MatchTestRemote matches the remote names used for testing + MatchTestRemote = regexp.MustCompile(`^rclone-test-[abcdefghijklmnopqrstuvwxyz0123456789]{24}$`) ) // Seed the random number generator @@ -37,6 +46,22 @@ func init() { } +// Initialise rclone for testing +func Initialise() { + // 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 + fs.LoadConfig() + if *Verbose { + fs.Config.LogLevel = fs.LogLevelDebug + } + fs.Config.DumpHeaders = *DumpHeaders + fs.Config.DumpBodies = *DumpBodies + fs.Config.LowLevelRetries = *LowLevelRetries + fs.Config.UseListR = *UseListR +} + // Item represents an item for checking type Item struct { Path string diff --git a/fstest/fstests/fstests.go b/fstest/fstests/fstests.go index e2d66df2e..af48766fd 100644 --- a/fstest/fstests/fstests.go +++ b/fstest/fstests/fstests.go @@ -7,7 +7,6 @@ package fstests import ( "bytes" - "flag" "fmt" "io" "io/ioutil" @@ -48,12 +47,8 @@ var ( Path: `hello? sausage/êé/Hello, 世界/ " ' @ < > & ? + ≠/z.txt`, WinPath: `hello_ sausage/êé/Hello, 世界/ _ ' @ _ _ & _ + ≠/z.txt`, } - 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:)") - isLocalRemote bool + file2Contents = "" + isLocalRemote bool ) // ExtraConfigItem describes a config item added on the fly while testing @@ -69,22 +64,14 @@ func TestInit(t *testing.T) { file2.Path = winPath(file2.Path) } - // 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 - fs.LoadConfig() + fstest.Initialise() + // Set extra config if supplied for _, item := range ExtraConfig { fs.ConfigFileSet(item.Name, item.Key, item.Value) } - if *verbose { - fs.Config.LogLevel = fs.LogLevelDebug - } - fs.Config.DumpHeaders = *dumpHeaders - fs.Config.DumpBodies = *dumpBodies - if *overrideRemote != "" { - RemoteName = *overrideRemote + if *fstest.RemoteName != "" { + RemoteName = *fstest.RemoteName } t.Logf("Using remote %q", RemoteName) if RemoteName == "" {