-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
integration-test.go
executable file
·256 lines (231 loc) · 6.44 KB
/
integration-test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"fmt"
"bytes"
"flag"
"io"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"
)
var (
// Flags
debug = flag.Bool("d", false, "Print commands instead of running them.")
verbose = flag.Bool("v", false, "Print commands as they are being executed and command output.")
gopath = flag.String("gopath", os.ExpandEnv("${HOME}/go"), "GOPATH to use.")
prepareOnly = flag.Bool("prepare-only", false, "Do everything except run tests")
pr = flag.String("pr", "", "PR number to test")
branch = flag.String("branch", "", "branch to test (defaults to master)")
backends = flag.String("backends", "", "to pass to test_all -backends")
remotes = flag.String("remotes", "", "to pass to test_all -remotes")
tests = flag.String("tests", "", "to pass to test_all -tests")
runRegexp = flag.String("run", "", "to pass to test_all -run")
outputDir = flag.String("output", "/home/rclone/integration-test/rclone-integration-tests", "write test output here")
outputDirMax = flag.Int("output-max", 60, "maximum number of directories in outputDir")
maxTries = flag.Int("maxtries", -1, "if set, overrides the -maxtries for test_all")
// Globals
gobin string // place for go binaries - filled in by main()
rcloneVersion string // version of rclone
rcloneCommit string // commit of rclone
)
// cmdEnv - create a shell command with env
func cmdEnv(args, env []string) *exec.Cmd {
if *debug {
args = append([]string{"echo"}, args...)
}
cmd := exec.Command(args[0], args[1:]...)
if env != nil {
cmd.Env = append(os.Environ(), env...)
}
if *debug || *verbose {
log.Printf("args = %v, env = %v\n", args, cmd.Env)
}
log.Println(strings.Join(args, " "))
return cmd
}
// runEnv - run a shell command with env
//
// The outputs the command output to stderr and returns it
func runEnv(args, env []string) (string, error) {
cmd := cmdEnv(args, env)
var b bytes.Buffer
cmd.Stderr = io.MultiWriter(&b, os.Stderr)
cmd.Stdout = cmd.Stderr
err := cmd.Run()
out := b.Bytes()
outStr := string(out)
if *verbose {
log.Print(string(out))
}
if err != nil {
log.Print("----------------------------")
log.Printf("Failed to run %v: %v", args, err)
log.Print("----------------------------")
}
return outStr, err
}
// run a shell command
func run(args ...string) string {
out, err := runEnv(args, nil)
if err != nil {
log.Fatalf("Exiting after error: %v", err)
}
return out
}
// xrun runs a shell command ignoring errors
func xrun(args ...string) string {
out, err := runEnv(args, nil)
if err != nil {
log.Printf("Ignoring error: %v", err)
}
return out
}
// chdir or die
func chdir(dir string) {
log.Printf("cd %s", dir)
err := os.Chdir(dir)
if err != nil {
log.Fatalf("Couldn't cd into %q: %v", dir, err)
}
}
// set env or die
func setenv(key, value string) {
log.Printf("export %s=%s", key, value)
err := os.Setenv(key, value)
if err != nil {
log.Fatalf("Couldn't Setenv(%q, %q): %v", key, value, err)
}
}
// make all the directories or die
func mkdirall(path string) {
log.Printf("mkdir -p %s", path)
err := os.MkdirAll(path, 0777)
if err != nil {
log.Fatalf("Couldn't MkdirAll(%q): %v", path, err)
}
}
// check the path or directory exists
func exists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else if os.IsNotExist(err) {
return false
} else {
log.Fatalf("exists(%q): %v", path, err)
}
return false
}
// install a github repo into the GOPATH
func installGitHubRepo(repo string) {
gitPath := path.Join(*gopath, "src/github.com/"+repo)
// make path and cd
mkdirall(gitPath)
chdir(gitPath)
// checkout the code
if exists(".git") {
run("git", "stash", "--include-untracked") // stash any local changes just in case
run("git", "checkout", "master")
run("git", "pull")
} else {
run("git", "clone", "https://github.com/"+repo+".git", ".")
}
}
// install rclone and checkout correct branch
func installRclone() {
installGitHubRepo("rclone/rclone")
// tidy up from previous runs
run("rm", "-f", "fs/operations/operations.test", "fs/sync/sync.test", "fs/test_all.log", "summary", "test.log")
branchName := "master"
pullName := ""
if *pr != "" {
branchName = "pr-" + *pr
pullName = "pull/" + *pr + "/head"
} else if *branch != "" {
branchName = *branch
pullName = branchName
}
if pullName != "" {
xrun("git", "branch", "-D", branchName)
run("git", "fetch", "origin", pullName+":"+branchName)
run("git", "checkout", branchName)
}
// build rclone
run("make")
}
func installRestic() {
// make sure restic is up to date for the cmd/serve/restic integration tests
run("go", "get", "-u", "github.com/restic/restic/...")
}
// run the rclone integration tests against all the remotes
func runTests(rclonePath string) {
chdir(rclonePath)
run("make", "test_all")
if !*prepareOnly {
args := []string{
path.Join(gobin, "test_all"),
"-verbose",
"-upload", "pub.rclone.org:integration-tests",
"-email", "nick@craig-wood.com",
"-output", *outputDir,
}
if *backends != "" {
args = append(args, "-backends", *backends)
}
if *remotes != "" {
args = append(args, "-remotes", *remotes)
}
if *tests != "" {
args = append(args, "-tests", *tests)
}
if *runRegexp != "" {
args = append(args, "-run", *runRegexp)
}
if *maxTries > 0 {
args = append(args, "-maxtries", fmt.Sprint(*maxTries))
}
xrun(args...)
}
}
// make sure there aren't too many items in the output dir
func tidyOutputDir() {
fis, err := os.ReadDir(*outputDir)
if err != nil {
log.Fatalf("Failed to read output directory %q: %v", outputDir, err)
}
var names []string
for _, fi := range fis {
if fi.IsDir() {
names = append(names, fi.Name())
}
}
sort.Strings(names)
if trim := len(names) - *outputDirMax; trim > 0 {
log.Printf("Need to trim %d directories", trim)
for _, dir := range names[:trim] {
dir = path.Join(*outputDir, dir)
log.Printf("Trimming %s", dir)
err := os.RemoveAll(dir)
if err != nil {
log.Printf("Failed to remove %q: %v", dir, err)
}
}
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 0 {
log.Fatalf("Syntax: %s [opts]", os.Args[0])
}
tidyOutputDir()
gobin = path.Join(*gopath, "bin")
setenv("GOPATH", *gopath)
setenv("GOTAGS", "cmount") // make sure we build the optional extras
installGitHubRepo("restic/restic") // install restic source so we can use its tests
installRclone()
rclonePath := path.Join(*gopath, "src/github.com/rclone/rclone")
runTests(rclonePath)
}