2015-12-30 00:18:31 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Run tests for all the remotes
|
|
|
|
//
|
|
|
|
// Run with go run test_all.go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-04-26 07:11:37 +00:00
|
|
|
"regexp"
|
2015-12-30 00:18:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-01-03 21:49:26 +00:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
_ "github.com/ncw/rclone/fs/all" // import all fs
|
2016-01-24 12:37:46 +00:00
|
|
|
"github.com/ncw/rclone/fstest"
|
2015-12-30 00:18:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
remotes = []string{
|
2016-08-23 16:44:07 +00:00
|
|
|
"TestAmazonCloudDrive:",
|
|
|
|
"TestB2:",
|
|
|
|
"TestCryptDrive:",
|
|
|
|
"TestCryptSwift:",
|
2015-12-30 00:18:31 +00:00
|
|
|
"TestDrive:",
|
|
|
|
"TestDropbox:",
|
2016-08-23 16:44:07 +00:00
|
|
|
"TestGoogleCloudStorage:",
|
2015-12-30 00:18:31 +00:00
|
|
|
"TestHubic:",
|
2016-08-23 16:44:07 +00:00
|
|
|
"TestOneDrive:",
|
|
|
|
"TestS3:",
|
2017-01-31 20:34:11 +00:00
|
|
|
"TestSftp:",
|
2016-08-23 16:44:07 +00:00
|
|
|
"TestSwift:",
|
2015-12-07 04:01:03 +00:00
|
|
|
"TestYandex:",
|
2016-12-27 20:52:30 +00:00
|
|
|
"TestFTP:",
|
2015-12-30 00:18:31 +00:00
|
|
|
}
|
|
|
|
binary = "fs.test"
|
|
|
|
// Flags
|
2016-04-26 07:11:37 +00:00
|
|
|
maxTries = flag.Int("maxtries", 5, "Number of times to try each test")
|
|
|
|
runTests = flag.String("remotes", "", "Comma separated list of remotes to test, eg 'TestSwift:,TestS3'")
|
2015-12-30 11:34:22 +00:00
|
|
|
verbose = flag.Bool("verbose", false, "Run the tests with -v")
|
2016-01-03 21:49:26 +00:00
|
|
|
clean = flag.Bool("clean", false, "Instead of testing, clean all left over test directories")
|
2016-02-25 22:05:57 +00:00
|
|
|
runOnly = flag.String("run-only", "", "Run only those tests matching the regexp supplied")
|
2015-12-30 00:18:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test holds info about a running test
|
|
|
|
type test struct {
|
2016-04-26 07:11:37 +00:00
|
|
|
remote string
|
|
|
|
subdir bool
|
|
|
|
cmdLine []string
|
|
|
|
cmdString string
|
|
|
|
try int
|
|
|
|
err error
|
|
|
|
output []byte
|
|
|
|
failedTests []string
|
|
|
|
runFlag string
|
2015-12-30 00:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTest creates a new test
|
|
|
|
func newTest(remote string, subdir bool) *test {
|
|
|
|
t := &test{
|
|
|
|
remote: remote,
|
|
|
|
subdir: subdir,
|
2015-12-30 11:34:22 +00:00
|
|
|
cmdLine: []string{"./" + binary, "-remote", remote},
|
2015-12-30 00:18:31 +00:00
|
|
|
try: 1,
|
|
|
|
}
|
2015-12-30 11:34:22 +00:00
|
|
|
if *verbose {
|
|
|
|
t.cmdLine = append(t.cmdLine, "-test.v")
|
|
|
|
}
|
2016-02-25 22:05:57 +00:00
|
|
|
if *runOnly != "" {
|
|
|
|
t.cmdLine = append(t.cmdLine, "-test.run", *runOnly)
|
|
|
|
}
|
2015-12-30 00:18:31 +00:00
|
|
|
if subdir {
|
|
|
|
t.cmdLine = append(t.cmdLine, "-subdir")
|
|
|
|
}
|
|
|
|
t.cmdString = strings.Join(t.cmdLine, " ")
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-05-07 20:19:26 +00:00
|
|
|
// dumpOutput prints the error output
|
|
|
|
func (t *test) dumpOutput() {
|
|
|
|
log.Println("------------------------------------------------------------")
|
|
|
|
log.Printf("---- %q ----", t.cmdString)
|
|
|
|
log.Println(string(t.output))
|
|
|
|
log.Println("------------------------------------------------------------")
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:11:37 +00:00
|
|
|
var failRe = regexp.MustCompile(`(?m)^--- FAIL: (Test\w*) \(`)
|
|
|
|
|
|
|
|
// findFailures looks for all the tests which failed
|
|
|
|
func (t *test) findFailures() {
|
2016-05-07 20:19:26 +00:00
|
|
|
oldFailedTests := t.failedTests
|
2016-04-26 07:11:37 +00:00
|
|
|
t.failedTests = nil
|
|
|
|
for _, matches := range failRe.FindAllSubmatch(t.output, -1) {
|
|
|
|
t.failedTests = append(t.failedTests, string(matches[1]))
|
|
|
|
}
|
|
|
|
if len(t.failedTests) != 0 {
|
|
|
|
t.runFlag = "^(" + strings.Join(t.failedTests, "|") + ")$"
|
|
|
|
} else {
|
|
|
|
t.runFlag = ""
|
|
|
|
}
|
|
|
|
if t.passed() && len(t.failedTests) != 0 {
|
|
|
|
log.Printf("%q - Expecting no errors but got: %v", t.cmdString, t.failedTests)
|
2016-05-07 20:19:26 +00:00
|
|
|
t.dumpOutput()
|
2016-04-26 07:11:37 +00:00
|
|
|
} else if !t.passed() && len(t.failedTests) == 0 {
|
|
|
|
log.Printf("%q - Expecting errors but got none: %v", t.cmdString, t.failedTests)
|
2016-05-07 20:19:26 +00:00
|
|
|
t.dumpOutput()
|
|
|
|
t.failedTests = oldFailedTests
|
2016-04-26 07:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 00:18:31 +00:00
|
|
|
// trial runs a single test
|
|
|
|
func (t *test) trial() {
|
2016-04-26 07:11:37 +00:00
|
|
|
cmdLine := t.cmdLine[:]
|
|
|
|
if t.runFlag != "" {
|
|
|
|
cmdLine = append(cmdLine, "-test.run", t.runFlag)
|
|
|
|
}
|
|
|
|
cmdString := strings.Join(cmdLine, " ")
|
|
|
|
log.Printf("%q - Starting (try %d/%d)", cmdString, t.try, *maxTries)
|
|
|
|
cmd := exec.Command(cmdLine[0], cmdLine[1:]...)
|
2015-12-30 00:18:31 +00:00
|
|
|
start := time.Now()
|
|
|
|
t.output, t.err = cmd.CombinedOutput()
|
|
|
|
duration := time.Since(start)
|
2016-04-26 07:11:37 +00:00
|
|
|
t.findFailures()
|
2015-12-30 00:18:31 +00:00
|
|
|
if t.passed() {
|
2016-04-26 07:11:37 +00:00
|
|
|
log.Printf("%q - Finished OK in %v (try %d/%d)", cmdString, duration, t.try, *maxTries)
|
2015-12-30 00:18:31 +00:00
|
|
|
} else {
|
2016-04-26 07:11:37 +00:00
|
|
|
log.Printf("%q - Finished ERROR in %v (try %d/%d): %v: Failed %v", cmdString, duration, t.try, *maxTries, t.err, t.failedTests)
|
2015-12-30 00:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 21:49:26 +00:00
|
|
|
// cleanFs runs a single clean fs for left over directories
|
|
|
|
func (t *test) cleanFs() error {
|
|
|
|
f, err := fs.NewFs(t.remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-26 07:11:37 +00:00
|
|
|
dirs, err := fs.NewLister().SetLevel(1).Start(f, "").GetDirs()
|
|
|
|
for _, dir := range dirs {
|
2016-01-24 12:37:46 +00:00
|
|
|
if fstest.MatchTestRemote.MatchString(dir.Name) {
|
2016-01-03 21:49:26 +00:00
|
|
|
log.Printf("Purging %s%s", t.remote, dir.Name)
|
|
|
|
dir, err := fs.NewFs(t.remote + dir.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fs.Purge(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean runs a single clean on a fs for left over directories
|
|
|
|
func (t *test) clean() {
|
|
|
|
log.Printf("%q - Starting clean (try %d/%d)", t.remote, t.try, *maxTries)
|
|
|
|
start := time.Now()
|
|
|
|
t.err = t.cleanFs()
|
|
|
|
if t.err != nil {
|
|
|
|
log.Printf("%q - Failed to purge %v", t.remote, t.err)
|
|
|
|
}
|
|
|
|
duration := time.Since(start)
|
|
|
|
if t.passed() {
|
|
|
|
log.Printf("%q - Finished OK in %v (try %d/%d)", t.cmdString, duration, t.try, *maxTries)
|
|
|
|
} else {
|
|
|
|
log.Printf("%q - Finished ERROR in %v (try %d/%d): %v", t.cmdString, duration, t.try, *maxTries, t.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 00:18:31 +00:00
|
|
|
// passed returns true if the test passed
|
|
|
|
func (t *test) passed() bool {
|
|
|
|
return t.err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run runs all the trials for this test
|
|
|
|
func (t *test) run(result chan<- *test) {
|
2015-12-30 11:34:22 +00:00
|
|
|
for t.try = 1; t.try <= *maxTries; t.try++ {
|
2016-01-03 21:49:26 +00:00
|
|
|
if *clean {
|
|
|
|
if !t.subdir {
|
|
|
|
t.clean()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.trial()
|
|
|
|
}
|
2015-12-30 00:18:31 +00:00
|
|
|
if t.passed() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !t.passed() {
|
2016-05-07 20:19:26 +00:00
|
|
|
t.dumpOutput()
|
2015-12-30 00:18:31 +00:00
|
|
|
}
|
|
|
|
result <- t
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeTestBinary makes the binary we will run
|
|
|
|
func makeTestBinary() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
binary += ".exe"
|
|
|
|
}
|
|
|
|
log.Printf("Making test binary %q", binary)
|
|
|
|
err := exec.Command("go", "test", "-c", "-o", binary).Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to make test binary: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(binary); err != nil {
|
|
|
|
log.Fatalf("Couldn't find test binary %q", binary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeTestBinary removes the binary made in makeTestBinary
|
|
|
|
func removeTestBinary() {
|
|
|
|
err := os.Remove(binary) // Delete the binary when finished
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error removing test binary %q: %v", binary, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if *runTests != "" {
|
|
|
|
remotes = strings.Split(*runTests, ",")
|
|
|
|
}
|
|
|
|
log.Printf("Testing remotes: %s", strings.Join(remotes, ", "))
|
|
|
|
|
|
|
|
start := time.Now()
|
2016-01-03 21:49:26 +00:00
|
|
|
if *clean {
|
|
|
|
fs.LoadConfig()
|
|
|
|
} else {
|
|
|
|
makeTestBinary()
|
|
|
|
defer removeTestBinary()
|
|
|
|
}
|
2015-12-30 00:18:31 +00:00
|
|
|
|
|
|
|
// start the tests
|
|
|
|
results := make(chan *test, 8)
|
|
|
|
awaiting := 0
|
|
|
|
for _, remote := range remotes {
|
|
|
|
awaiting += 2
|
|
|
|
go newTest(remote, false).run(results)
|
|
|
|
go newTest(remote, true).run(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the tests to finish
|
|
|
|
var failed []*test
|
|
|
|
for ; awaiting > 0; awaiting-- {
|
|
|
|
t := <-results
|
|
|
|
if !t.passed() {
|
|
|
|
failed = append(failed, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
duration := time.Since(start)
|
|
|
|
|
|
|
|
// Summarise results
|
|
|
|
if len(failed) == 0 {
|
|
|
|
log.Printf("PASS: All tests finished OK in %v", duration)
|
|
|
|
} else {
|
|
|
|
log.Printf("FAIL: %d tests failed in %v", len(failed), duration)
|
|
|
|
for _, t := range failed {
|
|
|
|
log.Printf(" * %s", t.cmdString)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|