forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
e2e.go
577 lines (511 loc) · 16.6 KB
/
e2e.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// e2e.go runs the e2e test suite. No non-standard package dependencies; call with "go run".
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
var (
isup = flag.Bool("isup", false, "Check to see if the e2e cluster is up, then exit.")
build = flag.Bool("build", false, "If true, build a new release. Otherwise, use whatever is there.")
version = flag.String("version", "", "The version to be tested (including the leading 'v'). An empty string defaults to the local build, but it can be set to any release (e.g. v0.4.4, v0.6.0).")
up = flag.Bool("up", false, "If true, start the the e2e cluster. If cluster is already up, recreate it.")
push = flag.Bool("push", false, "If true, push to e2e cluster. Has no effect if -up is true.")
pushup = flag.Bool("pushup", false, "If true, push to e2e cluster if it's up, otherwise start the e2e cluster.")
down = flag.Bool("down", false, "If true, tear down the cluster before exiting.")
orderseed = flag.Int64("orderseed", 0, "If non-zero, seed of random test shuffle order. (Otherwise random.)")
test = flag.Bool("test", false, "Run all tests in hack/e2e-suite.")
tests = flag.String("tests", "", "Run only tests in hack/e2e-suite matching this glob. Ignored if -test is set.")
times = flag.Int("times", 1, "Number of times each test is eligible to be run. Individual order is determined by shuffling --times instances of each test using --orderseed (like a multi-deck shoe of cards).")
root = flag.String("root", absOrDie(filepath.Clean(filepath.Join(path.Base(os.Args[0]), ".."))), "Root directory of kubernetes repository.")
tap = flag.Bool("tap", false, "Enable Test Anything Protocol (TAP) output (disables --verbose, only failure output recorded)")
verbose = flag.Bool("v", false, "If true, print all command output.")
trace_bash = flag.Bool("trace-bash", false, "If true, pass -x to bash to trace all bash commands")
checkVersionSkew = flag.Bool("check_version_skew", true, ""+
"By default, verify that client and server have exact version match. "+
"You can explicitly set to false if you're, e.g., testing client changes "+
"for which the server version doesn't make a difference.")
cfgCmd = flag.String("cfg", "", "If nonempty, pass this as an argument, and call kubecfg. Implies -v.")
ctlCmd = flag.String("ctl", "", "If nonempty, pass this as an argument, and call kubectl. Implies -v. (-test, -cfg, -ctl are mutually exclusive)")
)
const (
serverTarName = "kubernetes-server-linux-amd64.tar.gz"
saltTarName = "kubernetes-salt.tar.gz"
downloadDirName = "_output/downloads"
tarDirName = "server"
tempDirName = "upgrade-e2e-temp-dir"
minMinionCount = 2
)
var (
signals = make(chan os.Signal, 100)
// Root directory of the specified cluster version, rather than of where
// this script is being run from.
versionRoot = *root
)
func absOrDie(path string) string {
out, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return out
}
type TestResult struct {
Pass int
Fail int
}
type ResultsByTest map[string]TestResult
func main() {
flag.Parse()
signal.Notify(signals, os.Interrupt)
if *tap {
fmt.Printf("TAP version 13\n")
log.SetPrefix("# ")
// TODO: this limitation is fixable by moving runBash to
// outputing to temp files, which still lets people check on
// stuck things interactively. The current stdout/stderr
// approach isn't really going to work with TAP, though.
*verbose = false
}
if *test {
*tests = "*"
}
if *isup {
status := 1
if IsUp() {
status = 0
log.Printf("Cluster is UP")
} else {
log.Printf("Cluster is DOWN")
}
os.Exit(status)
}
if *build {
if !runBash("build-release", `test-build-release`) {
log.Fatal("Error building. Aborting.")
}
}
if *version != "" {
// If the desired version isn't available already, do whatever's needed
// to make it available. Once done, update the root directory for client
// tools to be the root of the release directory so that the given
// release's tools will be used. We can't use this new root for
// everything because it likely doesn't have the hack/ directory in it.
if newVersionRoot, err := PrepareVersion(*version); err != nil {
log.Fatalf("Error preparing a binary of version %s: %s. Aborting.", *version, err)
} else {
versionRoot = newVersionRoot
}
}
if *pushup {
if IsUp() {
log.Printf("e2e cluster is up, pushing.")
*up = false
*push = true
} else {
log.Printf("e2e cluster is down, creating.")
*up = true
*push = false
}
}
if *up {
if !Up() {
log.Fatal("Error starting e2e cluster. Aborting.")
}
} else if *push {
if !runBash("push", path.Join(versionRoot, "/cluster/kube-push.sh")) {
log.Fatal("Error pushing e2e cluster. Aborting.")
}
}
failure := false
switch {
case *cfgCmd != "":
failure = !runBash("'kubecfg "+*cfgCmd+"'", "$KUBECFG "+*cfgCmd)
case *ctlCmd != "":
failure = !runBash("'kubectl "+*ctlCmd+"'", "$KUBECTL "+*ctlCmd)
case *tests != "":
failure = PrintResults(Test())
}
if *down {
TearDown()
}
if failure {
os.Exit(1)
}
}
func TearDown() bool {
return runBash("teardown", "test-teardown")
}
// Up brings an e2e cluster up, recreating it if one is already running.
func Up() bool {
if IsUp() {
log.Printf("e2e cluster already running; will teardown")
if res := TearDown(); !res {
return false
}
}
return runBash("up", path.Join(versionRoot, "/cluster/kube-up.sh; test-setup;"))
}
// Ensure that the cluster is large engough to run the e2e tests.
func ValidateClusterSize() {
// Check that there are at least 3 minions running
res, stdout, _ := runBashWithOutputs(
"validate cluster size",
"cluster/kubectl.sh get minions --no-headers | wc -l")
if !res {
log.Fatal("Could not get nodes to validate cluster size")
}
numNodes, err := strconv.Atoi(strings.TrimSpace(stdout))
if err != nil {
log.Fatalf("Could not count number of nodes to validate cluster size (%s)", err)
}
if numNodes < minMinionCount {
log.Fatalf("Cluster size (%d) is too small to run e2e tests. %d Minions are required.", numNodes, minMinionCount)
}
}
// Is the e2e cluster up?
func IsUp() bool {
return runBash("get status", `$KUBECTL version`)
}
// PrepareVersion makes sure that the specified release version is locally
// available and ready to be used by kube-up or kube-push. Returns the director
// path of the release.
func PrepareVersion(version string) (string, error) {
if version == "" {
// Assume that the build flag already handled building a local binary.
return *root, nil
}
// If the version isn't a local build, try fetching the release from Google
// Cloud Storage.
downloadDir := filepath.Join(*root, downloadDirName)
if err := os.MkdirAll(downloadDir, 0755); err != nil {
return "", err
}
localReleaseDir := filepath.Join(downloadDir, version)
if err := os.MkdirAll(localReleaseDir, 0755); err != nil {
return "", err
}
remoteReleaseTar := fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/kubernetes.tar.gz", version)
localReleaseTar := filepath.Join(downloadDir, fmt.Sprintf("kubernetes-%s.tar.gz", version))
if _, err := os.Stat(localReleaseTar); os.IsNotExist(err) {
out, err := os.Create(localReleaseTar)
if err != nil {
return "", err
}
resp, err := http.Get(remoteReleaseTar)
if err != nil {
out.Close()
return "", err
}
defer resp.Body.Close()
io.Copy(out, resp.Body)
if err != nil {
out.Close()
return "", err
}
out.Close()
}
if !runRawBash("untarRelease", fmt.Sprintf("tar -C %s -zxf %s --strip-components=1", localReleaseDir, localReleaseTar)) {
log.Fatal("Failed to untar release. Aborting.")
}
// Now that we have the binaries saved locally, use the path to the untarred
// directory as the "root" path for future operations.
return localReleaseDir, nil
}
// Fisher-Yates shuffle using the given RNG r
func shuffleStrings(strings []string, r *rand.Rand) {
for i := len(strings) - 1; i > 0; i-- {
j := r.Intn(i + 1)
strings[i], strings[j] = strings[j], strings[i]
}
}
func Test() (results ResultsByTest) {
defer runBashUntil("watchEvents", "while true; do $KUBECTL --watch-only get events; done")()
if !IsUp() {
log.Fatal("Testing requested, but e2e cluster not up!")
}
ValidateClusterSize()
// run tests!
dir, err := os.Open(filepath.Join(*root, "hack", "e2e-suite"))
if err != nil {
log.Fatal("Couldn't open e2e-suite dir")
}
defer dir.Close()
names, err := dir.Readdirnames(0)
if err != nil {
log.Fatal("Couldn't read names in e2e-suite dir")
}
toRun := make([]string, 0, len(names))
for i := range names {
name := names[i]
if name == "." || name == ".." {
continue
}
if match, err := path.Match(*tests, name); !match && err == nil {
continue
}
if err != nil {
log.Fatalf("Bad test pattern: %v", *tests)
}
toRun = append(toRun, name)
}
if *orderseed == 0 {
// Use low order bits of NanoTime as the default seed. (Using
// all the bits makes for a long, very similar looking seed
// between runs.)
*orderseed = time.Now().UnixNano() & (1<<32 - 1)
}
sort.Strings(toRun)
if *times != 1 {
if *times <= 0 {
log.Fatal("Invalid --times (negative or no testing requested)!")
}
newToRun := make([]string, 0, *times*len(toRun))
for i := 0; i < *times; i++ {
newToRun = append(newToRun, toRun...)
}
toRun = newToRun
}
shuffleStrings(toRun, rand.New(rand.NewSource(*orderseed)))
log.Printf("Running tests matching %v shuffled with seed %#x: %v", *tests, *orderseed, toRun)
results = ResultsByTest{}
if *tap {
fmt.Printf("1..%v\n", len(toRun))
}
for i, name := range toRun {
absName := filepath.Join(*root, "hack", "e2e-suite", name)
log.Printf("Starting test [%v/%v]: %v", i+1, len(toRun), name)
start := time.Now()
testResult := results[name]
res, stdout, stderr := runBashWithOutputs(name, absName)
// The duration_ms output is an undocumented Jenkins TAP
// plugin feature for test duration. One might think _ms means
// milliseconds, but Jenkins interprets this field in seconds.
duration_secs := time.Now().Sub(start).Seconds()
if res {
fmt.Printf("ok %v - %v\n", i+1, name)
if *tap {
fmt.Printf(" ---\n duration_ms: %.3f\n ...\n", duration_secs)
}
testResult.Pass++
} else {
fmt.Printf("not ok %v - %v\n", i+1, name)
if *tap {
fmt.Printf(" ---\n duration_ms: %.3f\n", duration_secs)
}
printBashOutputs(" ", " ", stdout, stderr, *tap)
if *tap {
fmt.Printf(" ...\n")
}
testResult.Fail++
}
results[name] = testResult
}
return
}
func PrintResults(results ResultsByTest) bool {
failures := 0
passed := []string{}
flaky := []string{}
failed := []string{}
for test, result := range results {
if result.Pass > 0 && result.Fail == 0 {
passed = append(passed, test)
} else if result.Pass > 0 && result.Fail > 0 {
flaky = append(flaky, test)
failures += result.Fail
} else {
failed = append(failed, test)
failures += result.Fail
}
}
sort.Strings(passed)
sort.Strings(flaky)
sort.Strings(failed)
printSubreport("Passed", passed, results)
printSubreport("Flaky", flaky, results)
printSubreport("Failed", failed, results)
if failures > 0 {
log.Printf("%v test(s) failed.", failures)
} else {
log.Printf("Success!")
}
return failures > 0
}
func printSubreport(title string, tests []string, results ResultsByTest) {
report := title + " tests:"
for _, test := range tests {
result := results[test]
report += fmt.Sprintf(" %v[%v/%v]", test, result.Pass, result.Pass+result.Fail)
}
log.Printf(report)
}
// All nonsense below is temporary until we have go versions of these things.
// Runs the provided bash without wrapping it in any kubernetes-specific gunk.
func runRawBashWithOutputs(stepName, bash string) (bool, string, string) {
cmd := exec.Command("bash", "-s")
if *trace_bash {
cmd.Args = append(cmd.Args, "-x")
}
cmd.Stdin = strings.NewReader(bash)
return finishRunning(stepName, cmd)
}
func runRawBash(stepName, bashFragment string) bool {
result, _, _ := runRawBashWithOutputs(stepName, bashFragment)
return result
}
func runBashWithOutputs(stepName, bashFragment string) (bool, string, string) {
return runRawBashWithOutputs(stepName, bashWrap(bashFragment))
}
func runBash(stepName, bashFragment string) bool {
return runRawBash(stepName, bashWrap(bashFragment))
}
// call the returned anonymous function to stop.
func runBashUntil(stepName, bashFragment string) func() {
cmd := exec.Command("bash", "-s")
cmd.Stdin = strings.NewReader(bashWrap(bashFragment))
log.Printf("Running in background: %v", stepName)
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
cmd.Stdout, cmd.Stderr = stdout, stderr
if err := cmd.Start(); err != nil {
log.Printf("Unable to start '%v': '%v'", stepName, err)
return func() {}
}
return func() {
cmd.Process.Signal(os.Interrupt)
headerprefix := stepName + " "
lineprefix := " "
if *tap {
headerprefix = "# " + headerprefix
lineprefix = "# " + lineprefix
}
printBashOutputs(headerprefix, lineprefix, string(stdout.Bytes()), string(stderr.Bytes()), false)
}
}
func finishRunning(stepName string, cmd *exec.Cmd) (bool, string, string) {
log.Printf("Running: %v", stepName)
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
if *verbose {
cmd.Stdout = io.MultiWriter(os.Stdout, stdout)
cmd.Stderr = io.MultiWriter(os.Stderr, stderr)
} else {
cmd.Stdout = stdout
cmd.Stderr = stderr
}
done := make(chan struct{})
defer close(done)
go func() {
for {
select {
case <-done:
return
case s := <-signals:
cmd.Process.Signal(s)
}
}
}()
if err := cmd.Run(); err != nil {
log.Printf("Error running %v: %v", stepName, err)
return false, string(stdout.Bytes()), string(stderr.Bytes())
}
return true, string(stdout.Bytes()), string(stderr.Bytes())
}
func printBashOutputs(headerprefix, lineprefix, stdout, stderr string, escape bool) {
// The |'s (plus appropriate prefixing) are to make this look
// "YAMLish" to the Jenkins TAP plugin:
// https://wiki.jenkins-ci.org/display/JENKINS/TAP+Plugin
if stdout != "" {
fmt.Printf("%vstdout: |\n", headerprefix)
if escape {
stdout = escapeOutput(stdout)
}
printPrefixedLines(lineprefix, stdout)
}
if stderr != "" {
fmt.Printf("%vstderr: |\n", headerprefix)
if escape {
stderr = escapeOutput(stderr)
}
printPrefixedLines(lineprefix, stderr)
}
}
// Escape stdout/stderr so the Jenkins YAMLish parser doesn't barf on
// it. This escaping is crude (it masks all colons as something humans
// will hopefully see as a colon, for instance), but it should get the
// job done without pulling in a whole YAML package.
func escapeOutput(s string) (out string) {
for _, r := range s {
switch {
case r == '\n':
out += string(r)
case !strconv.IsPrint(r):
out += " "
case r == ':':
out += "\ua789" // "꞉", modifier letter colon
default:
out += string(r)
}
}
return
}
func printPrefixedLines(prefix, s string) {
for _, line := range strings.Split(s, "\n") {
fmt.Printf("%v%v\n", prefix, line)
}
}
// returns either "", or a list of args intended for appending with the
// kubecfg or kubectl commands (begining with a space).
func kubecfgArgs() string {
if *checkVersionSkew {
return " -expect_version_match"
}
return ""
}
// returns either "", or a list of args intended for appending with the
// kubectl command (begining with a space).
func kubectlArgs() string {
if *checkVersionSkew {
return " --match-server-version"
}
return ""
}
func bashWrap(cmd string) string {
return `
set -o errexit
set -o nounset
set -o pipefail
export KUBE_CONFIG_FILE="config-test.sh"
# TODO(jbeda): This will break on usage if there is a space in
# ${KUBE_ROOT}. Convert to an array? Or an exported function?
export KUBECFG="` + versionRoot + `/cluster/kubecfg.sh` + kubecfgArgs() + `"
export KUBECTL="` + versionRoot + `/cluster/kubectl.sh` + kubectlArgs() + `"
source "` + *root + `/cluster/kube-env.sh"
source "` + versionRoot + `/cluster/${KUBERNETES_PROVIDER}/util.sh"
prepare-e2e
` + cmd + `
`
}