forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e.go
844 lines (751 loc) · 25.2 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
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"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"k8s.io/test-infra/kubetest/e2e"
"k8s.io/test-infra/kubetest/process"
"k8s.io/test-infra/kubetest/util"
)
// Add more default --test_args as we migrate them
func argFields(args, dump, ipRange string) []string {
f := strings.Fields(args)
if dump != "" {
f = util.SetFieldDefault(f, "--report-dir", dump)
// Disable logdump within ginkgo as it'll be done in kubetest anyway now.
f = util.SetFieldDefault(f, "--disable-log-dump", "true")
}
if ipRange != "" {
f = util.SetFieldDefault(f, "--cluster-ip-range", ipRange)
}
return f
}
func run(deploy deployer, o options) error {
cmd, err := deploy.KubectlCommand()
if err != nil {
return err
}
if cmd == nil {
cmd = exec.Command("./cluster/kubectl.sh")
}
if o.checkSkew {
cmd.Args = append(cmd.Args, "--match-server-version")
}
os.Setenv("KUBECTL", strings.Join(cmd.Args, " "))
os.Setenv("KUBE_CONFIG_FILE", "config-test.sh")
os.Setenv("KUBE_RUNTIME_CONFIG", o.runtimeConfig)
var errs []error
dump, err := util.OptionalAbsPath(o.dump)
if err != nil {
return fmt.Errorf("failed handling --dump path: %w", err)
}
dumpPreTestLogs, err := util.OptionalAbsPath(o.dumpPreTestLogs)
if err != nil {
return fmt.Errorf("failed handling --dump-pre-test-logs path: %w", err)
}
if o.up {
if err := control.XMLWrap(&suite, "TearDown Previous", deploy.Down); err != nil {
return fmt.Errorf("error tearing down previous cluster: %s", err)
}
}
// Ensures that the cleanup/down action is performed exactly once.
var (
downDone = false
)
var (
beforeResources []byte
upResources []byte
downResources []byte
afterResources []byte
)
if o.checkLeaks {
errs = util.AppendError(errs, control.XMLWrap(&suite, "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 control.XMLWrap(&suite, "Deferred TearDown", func() error {
if !downDone {
return deploy.Down()
}
return nil
})
}
// Start the cluster using this version.
if err := control.XMLWrap(&suite, "Up", deploy.Up); err != nil {
if dump != "" {
control.XMLWrap(&suite, "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 node testing is enabled, check that the api is reachable before
// proceeding with further steps. This is accomplished by listing the nodes.
if !o.nodeTests && !strings.EqualFold(string(o.build), "none") {
errs = util.AppendError(errs, control.XMLWrap(&suite, "Check APIReachability", func() error { return getKubectlVersion(deploy) }))
if dump != "" {
errs = util.AppendError(errs, control.XMLWrap(&suite, "list nodes", func() error {
return listNodes(deploy, dump)
}))
}
}
}
if o.checkLeaks {
errs = util.AppendError(errs, control.XMLWrap(&suite, "listResources Up", func() error {
upResources, err = listResources()
return err
}))
}
if o.upgradeArgs != "" {
if err := control.XMLWrap(&suite, "test setup", deploy.TestSetup); err != nil {
errs = util.AppendError(errs, err)
} else {
errs = util.AppendError(errs, control.XMLWrap(&suite, "UpgradeTest", func() error {
// upgrade tests really only run one spec
var env []string
for _, v := range os.Environ() {
if !strings.HasPrefix(v, "GINKGO_PARALLEL") {
env = append(env, v)
}
}
return skewTestEnv(env, argFields(o.upgradeArgs, dump, o.clusterIPRange), "upgrade", o.checkSkew)
}))
}
}
if dumpPreTestLogs != "" {
errs = append(errs, dumpRemoteLogs(deploy, o, dumpPreTestLogs, "pre-test")...)
}
testArgs := argFields(o.testArgs, dump, o.clusterIPRange)
if o.test {
if err := control.XMLWrap(&suite, "test setup", deploy.TestSetup); err != nil {
errs = util.AppendError(errs, err)
} else {
if o.preTestCmd != "" {
errs = util.AppendError(errs, control.XMLWrap(&suite, "pre-test command", func() error {
cmdLineTokenized := strings.Fields(os.ExpandEnv(o.preTestCmd))
return control.FinishRunning(exec.Command(cmdLineTokenized[0], cmdLineTokenized[1:]...))
}))
}
if o.nodeTests {
nodeArgs := strings.Fields(o.nodeArgs)
errs = util.AppendError(errs, control.XMLWrap(&suite, "Node Tests", func() error {
return nodeTest(nodeArgs, o.testArgs, o.nodeTestArgs, o.gcpProject, o.gcpZone, o.runtimeConfig)
}))
} else if err := control.XMLWrap(&suite, "IsUp", deploy.IsUp); err != nil {
errs = util.AppendError(errs, err)
} else {
if o.deployment != "conformance" {
errs = util.AppendError(errs, control.XMLWrap(&suite, "kubectl version", func() error { return getKubectlVersion(deploy) }))
}
if o.skew {
errs = util.AppendError(errs, control.XMLWrap(&suite, "SkewTest", func() error {
return skewTest(testArgs, "skew", o.checkSkew)
}))
} else {
var tester e2e.Tester
tester = &GinkgoScriptTester{}
if testBuilder, ok := deploy.(e2e.TestBuilder); ok {
tester, err = testBuilder.BuildTester(toBuildTesterOptions(&o))
errs = util.AppendError(errs, err)
}
if tester != nil {
errs = util.AppendError(errs, control.XMLWrap(&suite, "Test", func() error {
return tester.Run(control, testArgs)
}))
}
}
}
}
}
var kubemarkUpErr error
if o.kubemark {
errs = util.AppendError(errs, control.XMLWrap(&suite, "Kubemark Overall", func() error {
if kubemarkUpErr = kubemarkUp(dump, o, deploy); kubemarkUpErr != nil {
return kubemarkUpErr
}
// running test in clusterloader, or other custom commands, skip the ginkgo call
if o.testCmd != "" {
return nil
}
return kubemarkGinkgoTest(testArgs, dump)
}))
}
if kubemarkUpErr == nil && o.testCmd != "" {
if err := control.XMLWrap(&suite, "test setup", deploy.TestSetup); err != nil {
errs = util.AppendError(errs, err)
} else {
if o.preTestCmd != "" {
errs = util.AppendError(errs, control.XMLWrap(&suite, "pre-test command", func() error {
cmdLineTokenized := strings.Fields(os.ExpandEnv(o.preTestCmd))
return control.FinishRunning(exec.Command(cmdLineTokenized[0], cmdLineTokenized[1:]...))
}))
}
errs = util.AppendError(errs, control.XMLWrap(&suite, o.testCmdName, func() error {
cmdLine := os.ExpandEnv(o.testCmd)
return control.FinishRunning(exec.Command(cmdLine, o.testCmdArgs...))
}))
}
}
// TODO: consider remapping charts, etc to testCmd
var kubemarkWg sync.WaitGroup
var kubemarkDownErr error
if o.down && o.kubemark {
kubemarkWg.Add(1)
go kubemarkDown(&kubemarkDownErr, &kubemarkWg, o.provider, dump, o.logexporterGCSPath)
}
if o.charts {
errs = util.AppendError(errs, control.XMLWrap(&suite, "Helm Charts", chartsTest))
}
if dump != "" {
errs = append(errs, dumpRemoteLogs(deploy, o, dump, "")...)
}
if o.checkLeaks {
errs = util.AppendError(errs, control.XMLWrap(&suite, "listResources Down", func() error {
downResources, err = listResources()
return err
}))
}
if o.down {
errs = util.AppendError(errs, control.XMLWrap(&suite, "TearDown", func() error {
if !downDone {
err := deploy.Down()
if err != nil {
return err
}
downDone = true
}
return nil
}))
}
// Wait for kubemarkDown step to finish before going further.
kubemarkWg.Wait()
errs = util.AppendError(errs, kubemarkDownErr)
// Save the state if we upped a new cluster without downing it
if o.save != "" && ((!o.down && o.up) || (o.up && o.deployment != "none")) {
errs = util.AppendError(errs, control.XMLWrap(&suite, "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 := control.XMLWrap(&suite, "listResources After", func() error {
afterResources, err = listResources()
return err
}); err != nil {
errs = append(errs, err)
} else {
errs = util.AppendError(errs, control.XMLWrap(&suite, "diffResources", func() error {
return diffResources(beforeResources, upResources, downResources, afterResources, dump)
}))
}
}
if len(errs) == 0 {
if pub, ok := deploy.(publisher); ok {
errs = util.AppendError(errs, pub.Publish())
}
}
if len(errs) == 0 && o.publish != "" {
errs = util.AppendError(errs, control.XMLWrap(&suite, "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 gcsWrite(o.publish, v)
}))
}
if o.postTestCmd != "" && (o.test || (kubemarkUpErr == nil && o.testCmd != "")) {
errs = util.AppendError(errs, control.XMLWrap(&suite, "post-test command", func() error {
cmdLineTokenized := strings.Fields(os.ExpandEnv(o.postTestCmd))
return control.FinishRunning(exec.Command(cmdLineTokenized[0], cmdLineTokenized[1:]...))
}))
}
if len(errs) != 0 {
return fmt.Errorf("encountered %d errors: %v", len(errs), errs)
}
return nil
}
func getKubectlVersion(dp deployer) error {
cmd, err := dp.KubectlCommand()
if err != nil {
return err
}
if cmd == nil {
cmd = exec.Command("./cluster/kubectl.sh")
}
cmd.Args = append(cmd.Args, "--match-server-version=false", "version")
copied := *cmd
retries := 5
for {
_, err := control.Output(&copied)
if err == nil {
return nil
}
retries--
if retries == 0 {
return err
}
log.Printf("Failed to reach api. Sleeping for 10 seconds before retrying... (%v)", copied.Args)
time.Sleep(10 * time.Second)
}
}
func dumpRemoteLogs(deploy deployer, o options, path, reason string) []error {
if o.kubemark {
// For dumping kubemark logs with logexporter, we should use
// root cluster kubeconfig.
kubeconfigKubemark := os.Getenv("KUBECONFIG")
kubeconfigRoot := os.Getenv("KUBEMARK_ROOT_KUBECONFIG")
if err := os.Setenv("KUBECONFIG", kubeconfigRoot); err != nil {
return []error{err}
}
defer os.Setenv("KUBECONFIG", kubeconfigKubemark)
}
if reason != "" {
reason += " "
}
var errs []error
errs = util.AppendError(errs, control.XMLWrap(&suite, reason+"DumpClusterLogs", func() error {
return deploy.DumpClusterLogs(path, o.logexporterGCSPath)
}))
return errs
}
func listNodes(dp deployer, dump string) error {
cmd, err := dp.KubectlCommand()
if err != nil {
return err
}
if cmd == nil {
cmd = exec.Command("./cluster/kubectl.sh")
}
cmd.Args = append(cmd.Args, "--match-server-version=false", "get", "nodes", "-oyaml")
b, err := control.Output(cmd)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dump, "nodes.yaml"), b, 0644)
}
func listKubemarkNodes(dp deployer, dump string) error {
cmd, err := dp.KubectlCommand()
if err != nil {
return err
}
if cmd == nil {
cmd = exec.Command("./cluster/kubectl.sh")
}
cmd.Args = append(cmd.Args, "--match-server-version=false", "--kubeconfig=./test/kubemark/resources/kubeconfig.kubemark", "get", "nodes", "-oyaml")
b, err := control.Output(cmd)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dump, "kubemark_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 := control.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 := control.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 := control.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 logDumpPath(provider string) string {
// Use the log dumping script outside of kubernetes/kubernetes repo.
// Guarding against K8s provider as the script is tested only for gce
// and gke cases at the moment.
if os.Getenv("USE_TEST_INFRA_LOG_DUMPING") == "true" && (provider == "gce" || provider == "gke") {
if logDumpPath := os.Getenv("LOG_DUMP_SCRIPT_PATH"); logDumpPath != "" {
return logDumpPath
}
}
return "./cluster/log-dump/log-dump.sh"
}
func kubemarkPath() string {
if kubemarkPath := os.Getenv("KUBEMARK_PATH"); kubemarkPath != "" {
return os.ExpandEnv(kubemarkPath)
}
return "./test/kubemark/"
}
func defaultDumpClusterLogs(localArtifactsDir, logexporterGCSPath, provider string) error {
logDumpPath := logDumpPath(provider)
if _, err := os.Stat(logDumpPath); err != nil {
log.Printf("Could not find %s.", 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 control.FinishRunning(cmd)
}
func chartsTest() error {
// Run helm tests.
cmdline := util.K8s("charts", "test", "helm-test-e2e.sh")
return control.FinishRunning(exec.Command(cmdline))
}
func nodeTest(nodeArgs []string, testArgs, nodeTestArgs, project, zone, runtimeConfig 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 : %w", sshKeyPath, err)
}
artifactsDir, ok := os.LookupEnv("ARTIFACTS")
if !ok {
// TODO(krzyzacy): old behavior, consider deprecate
artifactsDir = filepath.Join(os.Getenv("WORKSPACE"), "_artifacts")
}
var sshUser string
// Use the KUBE_SSH_USER environment variable if it is set. This is particularly
// required for Fedora CoreOS hosts that only have the user 'core`. Tests
// using Fedora CoreOS as a host for node tests must set KUBE_SSH_USER
// environment variable so that test infrastructure can communicate with the host
// successfully using ssh.
if os.Getenv("KUBE_SSH_USER") != "" {
sshUser = os.Getenv("KUBE_SSH_USER")
} else {
sshUser = os.Getenv("USER")
}
// prep node args
runner := []string{
"run",
util.K8s("kubernetes", "test", "e2e_node", "runner", "remote", "run_remote.go"),
"--cleanup",
"--logtostderr",
"--vmodule=*=4",
"--ssh-env=gce",
fmt.Sprintf("--results-dir=%s", artifactsDir),
fmt.Sprintf("--project=%s", project),
fmt.Sprintf("--zone=%s", zone),
fmt.Sprintf("--ssh-user=%s", sshUser),
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()),
}
if runtimeConfig != "" {
runner = append(runner, fmt.Sprintf("--runtime-config=%s", runtimeConfig))
}
runner = append(runner, nodeArgs...)
return control.FinishRunning(exec.Command("go", runner...))
}
func kubemarkUp(dump string, o options, deploy deployer) error {
// Stop previously running kubemark cluster (if any).
if err := control.XMLWrap(&suite, "Kubemark TearDown Previous", func() error {
if err := control.FinishRunning(exec.Command(path.Join(kubemarkPath(), "stop-kubemark.sh"))); err != nil {
return fmt.Errorf("failed to stop kubemark cluster, err: %w", err)
}
return nil
}); err != nil {
return err
}
log.Printf("finished tearing down kubemark")
if err := control.XMLWrap(&suite, "IsUp", deploy.IsUp); err != nil {
return err
}
// Start kubemark cluster.
if err := control.XMLWrap(&suite, "Kubemark Up", func() error {
return control.FinishRunning(exec.Command(path.Join(kubemarkPath(), "start-kubemark.sh")))
}); err != nil {
return err
}
// Check kubemark apiserver reachability by listing all nodes.
if dump != "" {
control.XMLWrap(&suite, "list kubemark nodes", func() error {
return listKubemarkNodes(deploy, dump)
})
}
// detect master IP
if err := os.Setenv("MASTER_NAME", os.Getenv("INSTANCE_PREFIX")+"-kubemark-master"); err != nil {
return err
}
var masterIP, masterInternalIP []byte
if o.deployment == "bash" && o.provider == "gce" {
var err error
masterIP, err = control.Output(exec.Command(
"gcloud", "compute", "addresses", "describe",
os.Getenv("MASTER_NAME")+"-ip",
"--project="+o.gcpProject,
"--region="+o.gcpZone[:len(o.gcpZone)-2],
"--format=value(address)"))
if err != nil {
return fmt.Errorf("failed to get masterIP: %w", err)
}
masterInternalIP, err = control.Output(exec.Command(
"gcloud", "compute", "instances", "describe",
os.Getenv("MASTER_NAME"),
"--project="+o.gcpProject,
"--zone="+o.gcpZone,
"--format=value(networkInterfaces[0].networkIP)"))
if err != nil {
return fmt.Errorf("failed to get masterInternalIP: %w", err)
}
} else if o.deployment == "aks" {
var err error
masterIP, err = control.Output(exec.Command(
"az", "aks", "show",
"-g", *aksResourceGroupName,
"-n", *aksResourceName,
"--query", "fqdn", "-o", "tsv"))
if err != nil {
return fmt.Errorf("failed to get masterIP: %w", err)
}
masterInternalIP = masterIP
}
if err := os.Setenv("KUBE_MASTER_IP", strings.TrimSpace(string(masterIP))); err != nil {
return err
}
// MASTER_IP variable is required by the clusterloader. It requires to have master ip provided,
// due to master being unregistered.
if err := os.Setenv("MASTER_IP", strings.TrimSpace(string(masterIP))); err != nil {
return err
}
// MASTER_INTERNAL_IP variable is needed by the clusterloader2 when running on kubemark clusters.
if err := os.Setenv("MASTER_INTERNAL_IP", strings.TrimSpace(string(masterInternalIP))); err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
// Remember root cluster kubeconfig, this nescessary for dumping logs with logexporter.
if err := os.Setenv("KUBEMARK_ROOT_KUBECONFIG", os.Getenv("KUBECONFIG")); err != nil {
return err
}
if err := os.Setenv("KUBECONFIG", fmt.Sprintf("%s/test/kubemark/resources/kubeconfig.kubemark", cwd)); err != nil {
return err
}
// 'Stop kubemark cluster' step has now been moved outside this function
// to make it asynchronous with other steps (to speed test execution).
return nil
}
func kubemarkGinkgoTest(testArgs []string, dump string) error {
if os.Getenv("ENABLE_KUBEMARK_CLUSTER_AUTOSCALER") == "true" {
testArgs = append(testArgs, "--kubemark-external-kubeconfig="+os.Getenv("DEFAULT_KUBECONFIG"))
}
// Run tests on the kubemark cluster.
return control.XMLWrap(&suite, "Kubemark Test", func() error {
testArgs = util.SetFieldDefault(testArgs, "--ginkgo.focus", "starting\\s30\\spods")
// TODO(krzyzacy): unsure if the envs in kubemark/util.sh makes a difference to e2e tests
// will verify and remove (or uncomment) next
//util := os.Getenv("WORKSPACE") + "/kubernetes/cluster/kubemark/util.sh"
//testArgs = append([]string{"-c", "source", util, " ; ./hack/ginkgo-e2e.sh"}, testArgs...)
cmd := exec.Command("./hack/ginkgo-e2e.sh", testArgs...)
cmd.Env = append(
os.Environ(),
"KUBERNETES_PROVIDER=kubemark",
"KUBE_CONFIG_FILE=config-default.sh",
"KUBE_MASTER_URL=https://"+os.Getenv("KUBE_MASTER_IP"),
)
return control.FinishRunning(cmd)
})
}
// Brings down the kubemark cluster.
func kubemarkDown(err *error, wg *sync.WaitGroup, provider, dump, logexporterGCSPath string) {
defer wg.Done()
control.XMLWrap(&suite, "Kubemark MasterLogDump", func() error {
logDumpPath := logDumpPath(provider)
masterName := os.Getenv("MASTER_NAME")
var cmd *exec.Cmd
if provider == "gke" {
log.Printf("Skipping dumping logs for gke provider")
return nil
}
if logexporterGCSPath != "" {
log.Printf("Dumping logs for kubemark master to GCS directly at path: %v", logexporterGCSPath)
cmd = exec.Command(logDumpPath, dump, logexporterGCSPath)
} else {
log.Printf("Dumping logs for kubemark master locally to: %v", dump)
cmd = exec.Command(logDumpPath, dump)
}
cmd.Env = append(
os.Environ(),
"KUBEMARK_MASTER_NAME="+masterName,
"DUMP_ONLY_MASTER_LOGS=true",
)
return control.FinishRunning(cmd)
})
*err = control.XMLWrap(&suite, "Kubemark TearDown", func() error {
return control.FinishRunning(exec.Command(path.Join(kubemarkPath(), "stop-kubemark.sh")))
})
}
// Runs tests in the kubernetes_skew directory, appending --report-prefix flag to the run
func skewTest(args []string, prefix string, checkSkew bool) error {
return skewTestEnv(nil, args, prefix, checkSkew)
}
// Runs tests in the kubernetes_skew directory, appending --report-prefix flag to the run
func skewTestEnv(env, args []string, prefix string, checkSkew bool) error {
// TODO(fejta): run this inside this kubetest process, do not spawn a new one.
popS, err := util.Pushd("../kubernetes_skew")
if err != nil {
return err
}
defer popS()
args = util.AppendField(args, "--report-prefix", prefix)
cmd := exec.Command(
"kubetest",
"--test",
"--test_args="+strings.Join(args, " "),
fmt.Sprintf("--check-version-skew=%t", checkSkew),
)
cmd.Env = env
return control.FinishRunning(cmd)
}
// GinkgoScriptTester implements Tester by calling the hack/ginkgo-e2e.sh script
type GinkgoScriptTester struct {
}
// Run executes ./hack/ginkgo-e2e.sh
func (t *GinkgoScriptTester) Run(control *process.Control, testArgs []string) error {
return control.FinishRunning(exec.Command("./hack/ginkgo-e2e.sh", testArgs...))
}
// toBuildTesterOptions builds the BuildTesterOptions data structure for passing to BuildTester
func toBuildTesterOptions(o *options) *e2e.BuildTesterOptions {
return &e2e.BuildTesterOptions{
FocusRegex: o.focusRegex,
SkipRegex: o.skipRegex,
Parallelism: o.ginkgoParallel.Get(),
StorageTestDriverPath: o.storageTestDriverPath,
}
}