forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e.go
627 lines (567 loc) · 17.9 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
Copyright 2014 The Kubernetes Authors.
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.
*/
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
// Add more default --test_args as we migrate them
func argFields(args, dump, ipRange string) []string {
f := strings.Fields(args)
if dump != "" {
f = setFieldDefault(f, "--report-dir", dump)
// Disable logdump within ginkgo as it'll be done in kubetest anyway now.
f = setFieldDefault(f, "--disable-log-dump", "true")
}
if ipRange != "" {
f = setFieldDefault(f, "--cluster-ip-range", ipRange)
}
return f
}
func run(deploy deployer, o options) error {
if o.checkSkew {
os.Setenv("KUBECTL", "./cluster/kubectl.sh --match-server-version")
} else {
os.Setenv("KUBECTL", "./cluster/kubectl.sh")
}
os.Setenv("KUBE_CONFIG_FILE", "config-test.sh")
os.Setenv("KUBE_RUNTIME_CONFIG", o.runtimeConfig)
dump := o.dump
if dump != "" {
if !filepath.IsAbs(dump) { // Directory may change
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to os.Getwd(): %v", err)
}
dump = filepath.Join(wd, dump)
}
}
if o.up {
if o.federation {
if err := xmlWrap("Federation TearDown Previous", fedDown); err != nil {
return fmt.Errorf("error tearing down previous federation control plane: %v", err)
}
}
if err := xmlWrap("TearDown Previous", deploy.Down); err != nil {
return fmt.Errorf("error tearing down previous cluster: %s", err)
}
}
var err error
var errs []error
// Ensures that the cleanup/down action is performed exactly once.
var (
downDone = false
federationDownDone = false
)
var (
beforeResources []byte
upResources []byte
downResources []byte
afterResources []byte
)
if o.checkLeaks {
errs = appendError(errs, xmlWrap("listResources Before", func() error {
beforeResources, err = listResources()
return err
}))
}
if o.up {
// If we tried to bring the cluster up, make a courtesy
// attempt to bring it down so we're not leaving resources around.
if o.down {
defer xmlWrap("Deferred TearDown", func() error {
if !downDone {
return deploy.Down()
}
return nil
})
// Deferred statements are executed in last-in-first-out order, so
// federation down defer must appear after the cluster teardown in
// order to execute that before cluster teardown.
if o.federation {
defer xmlWrap("Deferred Federation TearDown", func() error {
if !federationDownDone {
return fedDown()
}
return nil
})
}
}
// Start the cluster using this version.
if err := xmlWrap("Up", deploy.Up); err != nil {
if dump != "" {
xmlWrap("DumpClusterLogs (--up failed)", func() error {
// This frequently means the cluster does not exist.
// Thus DumpClusterLogs() typically fails.
// Therefore always return null for this scenarios.
// TODO(fejta): report a green E in testgrid if it errors.
deploy.DumpClusterLogs(dump, o.logexporterGCSPath)
return nil
})
}
return fmt.Errorf("starting e2e cluster: %s", err)
}
if o.federation {
if err := xmlWrap("Federation Up", fedUp); err != nil {
xmlWrap("dumpFederationLogs", func() error {
return dumpFederationLogs(dump)
})
return fmt.Errorf("error starting federation: %s", err)
}
}
if !o.nodeTests {
// Check that the api is reachable before proceeding with further steps.
errs = appendError(errs, xmlWrap("Check APIReachability", getKubectlVersion))
if dump != "" {
errs = appendError(errs, xmlWrap("list nodes", func() error {
return listNodes(dump)
}))
}
}
}
if o.checkLeaks {
errs = appendError(errs, xmlWrap("listResources Up", func() error {
upResources, err = listResources()
return err
}))
}
if o.upgradeArgs != "" {
if err := xmlWrap("test setup", deploy.TestSetup); err != nil {
errs = appendError(errs, err)
} else {
errs = appendError(errs, xmlWrap("UpgradeTest", func() error {
return skewTest(argFields(o.upgradeArgs, dump, o.clusterIPRange), "upgrade", o.checkSkew)
}))
}
}
testArgs := argFields(o.testArgs, dump, o.clusterIPRange)
if o.test {
if err := xmlWrap("test setup", deploy.TestSetup); err != nil {
errs = appendError(errs, err)
} else if o.nodeTests {
nodeArgs := strings.Fields(o.nodeArgs)
errs = appendError(errs, xmlWrap("Node Tests", func() error {
return nodeTest(nodeArgs, o.testArgs, o.nodeTestArgs, o.gcpProject, o.gcpZone)
}))
} else {
errs = appendError(errs, xmlWrap("kubectl version", getKubectlVersion))
if o.skew {
errs = appendError(errs, xmlWrap("SkewTest", func() error {
return skewTest(testArgs, "skew", o.checkSkew)
}))
} else {
if err := xmlWrap("IsUp", deploy.IsUp); err != nil {
errs = appendError(errs, err)
} else {
if o.federation {
errs = appendError(errs, xmlWrap("FederationTest", func() error {
return federationTest(testArgs)
}))
} else {
errs = appendError(errs, xmlWrap("Test", func() error {
return test(testArgs)
}))
}
}
}
}
}
if o.testCmd != "" {
errs = appendError(errs, xmlWrap(o.testCmdName, func() error {
cmdLine := os.ExpandEnv(o.testCmd)
return finishRunning(exec.Command(cmdLine))
}))
}
// TODO(bentheelder): consider remapping charts, etc to testCmd
if o.kubemark {
errs = appendError(errs, xmlWrap("Kubemark Overall", func() error {
return kubemarkTest(testArgs, dump, o.kubemarkNodes)
}))
}
if o.charts {
errs = appendError(errs, xmlWrap("Helm Charts", chartsTest))
}
if o.perfTests {
errs = appendError(errs, xmlWrap("Perf Tests", perfTest))
}
if dump != "" {
errs = appendError(errs, xmlWrap("DumpClusterLogs", func() error {
return deploy.DumpClusterLogs(dump, o.logexporterGCSPath)
}))
if o.federation {
errs = appendError(errs, xmlWrap("dumpFederationLogs", func() error {
return dumpFederationLogs(dump)
}))
}
}
if o.checkLeaks {
errs = appendError(errs, xmlWrap("listResources Down", func() error {
downResources, err = listResources()
return err
}))
}
if o.down {
if o.federation {
errs = appendError(errs, xmlWrap("Federation TearDown", func() error {
if !federationDownDone {
err := fedDown()
if err != nil {
return err
}
federationDownDone = true
}
return nil
}))
}
errs = appendError(errs, xmlWrap("TearDown", func() error {
if !downDone {
err := deploy.Down()
if err != nil {
return err
}
downDone = true
}
return nil
}))
}
// Save the state if we upped a new cluster without downing it
// or we are turning up federated clusters without turning up
// the federation control plane.
if o.save != "" && ((!o.down && o.up) || (!o.federation && o.up && o.deployment != "none")) {
errs = appendError(errs, xmlWrap("Save Cluster State", func() error {
return saveState(o.save)
}))
}
if o.checkLeaks {
log.Print("Sleeping for 30 seconds...") // Wait for eventually consistent listing
time.Sleep(30 * time.Second)
if err := xmlWrap("listResources After", func() error {
afterResources, err = listResources()
return err
}); err != nil {
errs = append(errs, err)
} else {
errs = appendError(errs, xmlWrap("diffResources", func() error {
return diffResources(beforeResources, upResources, downResources, afterResources, dump)
}))
}
}
if len(errs) == 0 && o.publish != "" {
errs = appendError(errs, xmlWrap("Publish version", func() error {
// Use plaintext version file packaged with kubernetes.tar.gz
v, err := ioutil.ReadFile("version")
if err != nil {
return err
}
log.Printf("Set %s version to %s", o.publish, string(v))
return finishRunning(exec.Command("gsutil", "cp", "version", o.publish))
}))
}
if len(errs) != 0 {
return fmt.Errorf("encountered %d errors: %v", len(errs), errs)
}
return nil
}
func getKubectlVersion() error {
retries := 5
for {
_, err := output(exec.Command("./cluster/kubectl.sh", "--match-server-version=false", "version"))
if err == nil {
return nil
}
retries--
if retries == 0 {
return err
}
log.Print("Failed to reach api. Sleeping for 10 seconds before retrying...")
time.Sleep(10 * time.Second)
}
}
func listNodes(dump string) error {
b, err := output(exec.Command("./cluster/kubectl.sh", "--match-server-version=false", "get", "nodes", "-oyaml"))
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dump, "nodes.yaml"), b, 0644)
}
func diffResources(before, clusterUp, clusterDown, after []byte, location string) error {
if location == "" {
var err error
location, err = ioutil.TempDir("", "e2e-check-resources")
if err != nil {
return fmt.Errorf("Could not create e2e-check-resources temp dir: %s", err)
}
}
var mode os.FileMode = 0664
bp := filepath.Join(location, "gcp-resources-before.txt")
up := filepath.Join(location, "gcp-resources-cluster-up.txt")
cdp := filepath.Join(location, "gcp-resources-cluster-down.txt")
ap := filepath.Join(location, "gcp-resources-after.txt")
dp := filepath.Join(location, "gcp-resources-diff.txt")
if err := ioutil.WriteFile(bp, before, mode); err != nil {
return err
}
if err := ioutil.WriteFile(up, clusterUp, mode); err != nil {
return err
}
if err := ioutil.WriteFile(cdp, clusterDown, mode); err != nil {
return err
}
if err := ioutil.WriteFile(ap, after, mode); err != nil {
return err
}
stdout, cerr := output(exec.Command("diff", "-sw", "-U0", "-F^\\[.*\\]$", bp, ap))
if err := ioutil.WriteFile(dp, stdout, mode); err != nil {
return err
}
if cerr == nil { // No diffs
return nil
}
lines := strings.Split(string(stdout), "\n")
if len(lines) < 3 { // Ignore the +++ and --- header lines
return nil
}
lines = lines[2:]
var added, report []string
resourceTypeRE := regexp.MustCompile(`^@@.+\s(\[\s\S+\s\])$`)
for _, l := range lines {
if matches := resourceTypeRE.FindStringSubmatch(l); matches != nil {
report = append(report, matches[1])
}
if strings.HasPrefix(l, "+") && len(strings.TrimPrefix(l, "+")) > 0 {
added = append(added, l)
report = append(report, l)
}
}
if len(added) > 0 {
return fmt.Errorf("Error: %d leaked resources\n%v", len(added), strings.Join(report, "\n"))
}
return nil
}
func listResources() ([]byte, error) {
log.Printf("Listing resources...")
stdout, err := output(exec.Command("./cluster/gce/list-resources.sh"))
if err != nil {
return stdout, fmt.Errorf("Failed to list resources (%s):\n%s", err, string(stdout))
}
return stdout, err
}
func clusterSize(deploy deployer) (int, error) {
if err := deploy.TestSetup(); err != nil {
return -1, err
}
o, err := output(exec.Command("kubectl", "get", "nodes", "--no-headers"))
if err != nil {
log.Printf("kubectl get nodes failed: %s\n%s", wrapError(err).Error(), string(o))
return -1, err
}
stdout := strings.TrimSpace(string(o))
log.Printf("Cluster nodes:\n%s", stdout)
return len(strings.Split(stdout, "\n")), nil
}
// commandError will provide stderr output (if available) from structured
// exit errors
type commandError struct {
err error
}
func wrapError(err error) *commandError {
if err == nil {
return nil
}
return &commandError{err: err}
}
func (e *commandError) Error() string {
if e == nil {
return ""
}
exitErr, ok := e.err.(*exec.ExitError)
if !ok {
return e.err.Error()
}
stderr := ""
if exitErr.Stderr != nil {
stderr = string(stderr)
}
return fmt.Sprintf("%q: %q", exitErr.Error(), stderr)
}
func isUp(d deployer) error {
n, err := clusterSize(d)
if err != nil {
return err
}
if n <= 0 {
return fmt.Errorf("cluster found, but %d nodes reported", n)
}
return nil
}
func defaultDumpClusterLogs(localArtifactsDir, logexporterGCSPath string) error {
logDumpPath := "./cluster/log-dump/log-dump.sh"
// cluster/log-dump/log-dump.sh only exists in the Kubernetes tree
// post-1.3. If it doesn't exist, print a debug log but do not report an error.
if _, err := os.Stat(logDumpPath); err != nil {
log.Printf("Could not find %s. This is expected if running tests against a Kubernetes 1.3 or older tree.", logDumpPath)
if cwd, err := os.Getwd(); err == nil {
log.Printf("CWD: %v", cwd)
}
return nil
}
var cmd *exec.Cmd
if logexporterGCSPath != "" {
log.Printf("Dumping logs from nodes to GCS directly at path: %v", logexporterGCSPath)
cmd = exec.Command(logDumpPath, localArtifactsDir, logexporterGCSPath)
} else {
log.Printf("Dumping logs locally to: %v", localArtifactsDir)
cmd = exec.Command(logDumpPath, localArtifactsDir)
}
return finishRunning(cmd)
}
func dumpFederationLogs(location string) error {
// TODO(shashidharatd): Remove below logic of choosing the scripts to run from federation
// repo once the k8s deployment in federation jobs moves to kubernetes-anywhere
var logDumpPath string
if useFederationRepo() {
logDumpPath = "../federation/deploy/cluster/log-dump.sh"
} else {
logDumpPath = "./federation/cluster/log-dump.sh"
}
// federation/cluster/log-dump.sh only exists in the Kubernetes tree
// post-1.6. If it doesn't exist, do nothing and do not report an error.
if _, err := os.Stat(logDumpPath); err == nil {
log.Printf("Dumping Federation logs to: %v", location)
return finishRunning(exec.Command(logDumpPath, location))
}
log.Printf("Could not find %s. This is expected if running tests against a Kubernetes 1.6 or older tree.", logDumpPath)
return nil
}
func perfTest() error {
// Run perf tests
// TODO(fejta): GOPATH may be split by :
cmdline := fmt.Sprintf("%s/src/k8s.io/perf-tests/clusterloader/run-e2e.sh", os.Getenv("GOPATH"))
if err := finishRunning(exec.Command(cmdline)); err != nil {
return err
}
return nil
}
func chartsTest() error {
// Run helm tests.
cmdline := fmt.Sprintf("%s/src/k8s.io/charts/test/helm-test-e2e.sh", os.Getenv("GOPATH"))
return finishRunning(exec.Command(cmdline))
}
func nodeTest(nodeArgs []string, testArgs, nodeTestArgs, project, zone string) error {
// Run node e2e tests.
// TODO(krzyzacy): remove once nodeTest is stable
if wd, err := os.Getwd(); err == nil {
log.Printf("cwd : %s", wd)
}
sshKeyPath := os.Getenv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE")
if _, err := os.Stat(sshKeyPath); err != nil {
return fmt.Errorf("Cannot find ssh key from: %v, err : %v", sshKeyPath, err)
}
// prep node args
runner := []string{
"run",
fmt.Sprintf("%s/src/k8s.io/kubernetes/test/e2e_node/runner/remote/run_remote.go", os.Getenv("GOPATH")),
"--cleanup",
"--logtostderr",
"--vmodule=*=4",
"--ssh-env=gce",
fmt.Sprintf("--results-dir=%s/_artifacts", os.Getenv("WORKSPACE")),
fmt.Sprintf("--project=%s", project),
fmt.Sprintf("--zone=%s", zone),
fmt.Sprintf("--ssh-user=%s", os.Getenv("USER")),
fmt.Sprintf("--ssh-key=%s", sshKeyPath),
fmt.Sprintf("--ginkgo-flags=%s", testArgs),
fmt.Sprintf("--test_args=%s", nodeTestArgs),
fmt.Sprintf("--test-timeout=%s", timeout.String()),
}
runner = append(runner, nodeArgs...)
return finishRunning(exec.Command("go", runner...))
}
func kubemarkTest(testArgs []string, dump, numNodes string) error {
// Stop previously running kubemark cluster (if any).
if err := xmlWrap("Kubemark TearDown Previous", func() error {
return finishRunning(exec.Command("./test/kubemark/stop-kubemark.sh"))
}); err != nil {
return err
}
// If we tried to bring the Kubemark cluster up, make a courtesy
// attempt to bring it down so we're not leaving resources around.
//
// TODO: We should try calling stop-kubemark exactly once. Though to
// stop the leaking resources for now, we want to be on the safe side
// and call it explicitly in defer if the other one is not called.
defer xmlWrap("Kubemark TearDown (Deferred)", func() error {
return finishRunning(exec.Command("./test/kubemark/stop-kubemark.sh"))
})
// Start kubemark cluster.
if err := xmlWrap("Kubemark Up", func() error {
return finishRunning(exec.Command("./test/kubemark/start-kubemark.sh"))
}); err != nil {
if dump != "" {
xmlWrap("Kubemark MasterLogDump (--up failed)", func() error {
return finishRunning(exec.Command("./test/kubemark/master-log-dump.sh", dump))
})
}
return err
}
// Run tests on the kubemark cluster.
if err := xmlWrap("Kubemark Test", func() error {
testArgs = setFieldDefault(testArgs, "--ginkgo.focus", "starting\\s30\\pods")
return finishRunning(exec.Command("./test/kubemark/run-e2e-tests.sh", testArgs...))
}); err != nil {
if dump != "" {
xmlWrap("Kubemark MasterLogDump (--test failed)", func() error {
return finishRunning(exec.Command("./test/kubemark/master-log-dump.sh", dump))
})
}
return err
}
// Dump logs from kubemark master.
xmlWrap("Kubemark MasterLogDump", func() error {
return finishRunning(exec.Command("./test/kubemark/master-log-dump.sh", dump))
})
// Stop the kubemark cluster.
if err := xmlWrap("Kubemark TearDown", func() error {
return finishRunning(exec.Command("./test/kubemark/stop-kubemark.sh"))
}); err != nil {
return err
}
return nil
}
// Runs tests in the kubernetes_skew directory, appending --repor-prefix flag to the run
func skewTest(args []string, prefix string, checkSkew bool) error {
// TODO(fejta): run this inside this kubetest process, do not spawn a new one.
popS, err := pushd("../kubernetes_skew")
if err != nil {
return err
}
defer popS()
args = appendField(args, "--report-prefix", prefix)
return finishRunning(exec.Command(
"kubetest",
"--test",
"--test_args="+strings.Join(args, " "),
fmt.Sprintf("--v=%t", verbose),
fmt.Sprintf("--check-version-skew=%t", checkSkew),
))
}
func test(testArgs []string) error {
return finishRunning(exec.Command("./hack/ginkgo-e2e.sh", testArgs...))
}