-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
243 lines (206 loc) · 6.36 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/kelda/blimp/pkg/errors"
)
// Must be run from the root of the Blimp repo.
var random = rand.New(
rand.NewSource(time.Now().UnixNano()))
func main() {
concurrency := flag.Int("concurrency", 1, "")
runRegex := flag.String("run", "", "")
flag.Parse()
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
if err := run(*concurrency, *runRegex); err != nil {
fmt.Printf("Test failed: %s\n", err)
os.Exit(1)
}
}
type testResult struct {
id int
output []byte
err error
duration time.Duration
}
func run(concurrency int, runRegex string) error {
managerHost := os.Getenv("MANAGER_HOST")
managerHost = strings.Replace(managerHost, "localhost", "host.docker.internal", 1)
log.Info("Building test runner image")
err := buildRunnerImage(managerHost)
if err != nil {
return errors.WithContext("build base image", err)
}
log.Info("Starting tests")
testResults := make(chan testResult, concurrency)
for id := 1; id <= concurrency; id++ {
id := id
go func() {
// Clean up the old test runner if it exists.
runCommand("docker", "rm", "-f", containerName(id))
// Auth0's Authentication API has a global limit of 300 requests
// per minute for free tenants.
time.Sleep(time.Duration(random.Intn(30)) * time.Second)
// Run the test.
start := time.Now()
output, err := runTest(id,
fmt.Sprintf("_internal-scale-testing+%d@kelda.io", id),
"VBiJH2tL7Mg?bvZvbTzNAiZ{",
runRegex,
)
testDuration := time.Now().Sub(start)
testResults <- testResult{
id: id,
output: output,
err: err,
duration: testDuration,
}
// Remove the test runner immediately if the test passed.
if err == nil {
runCommand("docker", "exec", containerName(id), "blimp", "down")
runCommand("docker", "rm", "-f", containerName(id))
}
}()
}
// Collect the test results.
var failures int
for i := 0; i < concurrency; i++ {
fmt.Println()
result := <-testResults
if result.err == nil && result.duration > 40*time.Minute {
result.err = errors.New("test passed, but took too long (%s)", result.duration)
}
msg := "RUNNER PASSED"
fields := log.Fields{
"id": result.id,
"duration": result.duration,
}
if result.err != nil {
fields["error"] = result.err
msg = "RUNNER FAILED"
failures++
}
log.WithFields(fields).Info(msg)
fmt.Println(string(result.output))
}
fmt.Println()
if failures == 0 {
fmt.Println("ALL RUNNERS PASSED")
os.Exit(0)
} else {
fmt.Printf("%d / %d RUNNERS FAILED\n", failures, concurrency)
os.Exit(1)
}
return nil
}
func buildRunnerImage(managerHost string) error {
buildCtx, err := ioutil.TempDir("", "blimp-test-runner")
if err != nil {
return errors.WithContext("make build context dir", err)
}
defer os.RemoveAll(buildCtx)
dockerfileLines := []string{
"FROM golang:1.13",
"RUN apt-get update && apt-get install ca-certificates",
"GO-COPY github.com/kelda/node-todo",
// Install dependencies.
"WORKDIR /go/src/github.com/kelda/blimp",
"GO-COPY github.com/kelda/blimp/Makefile",
"RUN make go-get",
"WORKDIR /go/src/github.com/kelda/blimp",
"GO-COPY github.com/kelda/blimp/go.mod",
"GO-COPY github.com/kelda/blimp/go.sum",
"RUN go mod download",
// Build the CLI.
"WORKDIR /go/src/github.com/kelda/blimp",
"GO-COPY github.com/kelda/blimp",
"RUN make build-cli-linux && mv ./blimp-linux /usr/local/bin/blimp",
// Build the tests.
"WORKDIR /go/src/github.com/kelda/blimp",
"GO-COPY github.com/kelda/blimp",
"RUN go test -v -c -o /usr/local/bin/run-tests --tags ci --timeout 0 ./ci",
fmt.Sprintf("ENV MANAGER_HOST=%s", managerHost),
}
// Resolve the COPY statements to their full source and destination.
for i, line := range dockerfileLines {
if !strings.HasPrefix(line, "GO-COPY") {
continue
}
parts := strings.Split(line, " ")
if len(parts) != 2 {
return errors.New("malformed GO-COPY: %s", line)
}
localPath := parts[1]
pathInBuildCtx := filepath.Join("go/src", localPath)
buildCtxPath := filepath.Join(buildCtx, pathInBuildCtx)
if err := os.MkdirAll(filepath.Dir(buildCtxPath), 0755); err != nil {
return errors.WithContext("make directory for repo", err)
}
err := runCommand("cp",
// Copy all the contents of the directory.
"-R",
// Follow symlinks (this is necessary to copy github.com/kelda/blimp/certs).
"-L",
filepath.Join(build.Default.GOPATH, "src", localPath),
filepath.Dir(buildCtxPath))
if err != nil {
return errors.WithContext("copy repo", err)
}
dockerfileLines[i] = fmt.Sprintf("COPY %s /%s", pathInBuildCtx, pathInBuildCtx)
}
dockerfile := strings.Join(dockerfileLines, "\n")
err = ioutil.WriteFile(filepath.Join(buildCtx, "Dockerfile"), []byte(dockerfile), 0644)
if err != nil {
return errors.WithContext("write dockerfile", err)
}
// Ignore .git so that it doesn't invalidate the build cache.
err = ioutil.WriteFile(filepath.Join(buildCtx, ".dockerignore"), []byte("**/.git"), 0644)
if err != nil {
return errors.WithContext("write .dockerignore", err)
}
buildCmd := exec.Command("docker", "build", "-t", "blimp-test-runner", buildCtx)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
return errors.WithContext("build image", err)
}
return nil
}
func runCommand(command string, args ...string) error {
output, err := exec.Command(command, args...).CombinedOutput()
if err != nil {
return errors.New("command failed (%s): %s", err, string(output))
}
return nil
}
func runTest(id int, username, password, runRegex string) ([]byte, error) {
out, err := exec.Command("docker", "run",
"-v", "/var/run/docker.sock:/var/run/docker.sock",
"--name", containerName(id),
"--detach",
"blimp-test-runner",
"tail", "-f", "/dev/null").CombinedOutput()
if err != nil {
return out, errors.WithContext("start test container", err)
}
testCmd := "run-tests -test.v"
if runRegex != "" {
testCmd += " -test.run " + runRegex
}
return exec.Command("docker", "exec", containerName(id), "bash", "-c",
fmt.Sprintf("set -o pipefail; "+
"blimp loginpw --username %s --password %s && %s |& tee /proc/1/fd/1",
username, password, testCmd)).CombinedOutput()
}
func containerName(id int) string {
return fmt.Sprintf("blimp-test-runner-%d", id)
}