-
Notifications
You must be signed in to change notification settings - Fork 38.7k
/
run_remote_suite.go
242 lines (215 loc) · 7.31 KB
/
run_remote_suite.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
/*
Copyright 2016 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 remote
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"time"
"k8s.io/klog/v2"
)
var mode = flag.String("mode", "gce", "Mode to operate in. One of gce|ssh. Defaults to gce")
var testArgs = flag.String("test_args", "", "Space-separated list of arguments to pass to Ginkgo test runner.")
var instanceNamePrefix = flag.String("instance-name-prefix", "", "prefix for instance names")
var imageConfigFile = flag.String("image-config-file", "", "yaml file describing images to run")
var imageConfigDir = flag.String("image-config-dir", "", "(optional) path to image config files")
var images = flag.String("images", "", "images to test")
var hosts = flag.String("hosts", "", "hosts to test")
var cleanup = flag.Bool("cleanup", true, "If true remove files from remote hosts and delete temporary instances")
var deleteInstances = flag.Bool("delete-instances", true, "If true, delete any instances created")
var buildOnly = flag.Bool("build-only", false, "If true, build e2e_node_test.tar.gz and exit.")
var gubernator = flag.Bool("gubernator", false, "If true, output Gubernator link to view logs")
var ginkgoFlags = flag.String("ginkgo-flags", "", "Passed to ginkgo to specify additional flags such as --skip=.")
var (
arc Archive
)
// Archive contains path info in the archive.
type Archive struct {
sync.Once
path string
err error
}
func getFlag(name string) string {
lookup := flag.Lookup(name)
if lookup == nil {
return ""
}
return lookup.Value.String()
}
func RunRemoteTestSuite(testSuite TestSuite) {
// Listen for SIGINT and ignore the first one. In case SIGINT is sent to this
// process and all its children, we ignore it here, while our children ssh connections
// are stopped. This allows us to gather artifacts and print out test state before
// being killed.
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Printf("Received SIGINT. Will exit on next SIGINT.\n")
<-c
fmt.Printf("Received another SIGINT. Will exit.\n")
os.Exit(1)
}()
rand.Seed(time.Now().UnixNano())
if *buildOnly {
// Build the archive and exit
CreateTestArchive(testSuite,
getFlag("system-spec-name"),
getFlag("kubelet-config-file"))
return
}
// Append some default ginkgo flags. We use similar defaults here as hack/ginkgo-e2e.sh
allGinkgoFlags := fmt.Sprintf("%s --no-color -v", *ginkgoFlags)
fmt.Printf("Will use ginkgo flags as: %s", allGinkgoFlags)
var runner Runner
cfg := Config{
InstanceNamePrefix: *instanceNamePrefix,
ImageConfigFile: *imageConfigFile,
ImageConfigDir: *imageConfigDir,
Images: splitCommaList(*images),
Hosts: parseHostsList(*hosts),
GinkgoFlags: allGinkgoFlags,
DeleteInstances: *deleteInstances,
Cleanup: *cleanup,
TestArgs: *testArgs,
ExtraEnvs: getFlag("extra-envs"),
RuntimeConfig: getFlag("runtime-config"),
SystemSpecName: getFlag("system-spec-name"),
}
var sshRunner Runner
if *mode == "ssh" {
runner = NewSSHRunner(cfg)
} else {
getRunner, err := GetRunner(*mode)
if err != nil {
klog.Fatalf("getting runner mode %q : %v", *mode, err)
}
runner = getRunner(cfg)
sshRunner = NewSSHRunner(cfg)
}
if err := runner.Validate(); err != nil {
klog.Fatalf("validating remote config, %s", err)
}
// Setup coloring
stat, _ := os.Stdout.Stat()
useColor := (stat.Mode() & os.ModeCharDevice) != 0
blue := ""
noColour := ""
if useColor {
blue = "\033[0;34m"
noColour = "\033[0m"
}
results := make(chan *TestResult)
path, err := arc.getArchive(testSuite)
if err != nil {
log.Fatalf("unable to create test archive: %s", err)
}
defer arc.deleteArchive()
running := runner.StartTests(testSuite, path, results)
// You can potentially run SSH based tests while running image based test as well. The GCE provider does this, see
// test-e2e-node.sh.
if sshRunner != nil && len(cfg.Hosts) > 0 {
running += sshRunner.StartTests(testSuite, path, results)
}
// Wait for all tests to complete and emit the results
errCount := 0
exitOk := true
for i := 0; i < running; i++ {
tr := <-results
host := tr.Host
fmt.Println() // Print an empty line
fmt.Printf("%s>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%s\n", blue, noColour)
fmt.Printf("%s> START TEST >%s\n", blue, noColour)
fmt.Printf("%s>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%s\n", blue, noColour)
fmt.Printf("Start Test Suite on Host %s\n", host)
fmt.Printf("%s\n", tr.Output)
if tr.Err != nil {
errCount++
fmt.Printf("Failure Finished Test Suite on Host %s. Refer to artifacts directory for ginkgo log for this host.\n%v\n", host, tr.Err)
} else {
fmt.Printf("Success Finished Test Suite on Host %s. Refer to artifacts directory for ginkgo log for this host.\n", host)
}
exitOk = exitOk && tr.ExitOK
fmt.Printf("%s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<%s\n", blue, noColour)
fmt.Printf("%s< FINISH TEST <%s\n", blue, noColour)
fmt.Printf("%s<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<%s\n", blue, noColour)
fmt.Println() // Print an empty line
}
// Set the exit code if there were failures
if !exitOk {
fmt.Printf("Failure: %d errors encountered.\n", errCount)
callGubernator(*gubernator)
arc.deleteArchive()
os.Exit(1)
}
callGubernator(*gubernator)
}
func splitCommaList(s string) []string {
if len(s) == 0 {
return nil
}
return strings.Split(s, ",")
}
func callGubernator(gubernator bool) {
if gubernator {
fmt.Println("Running gubernator.sh")
output, err := exec.Command("./test/e2e_node/gubernator.sh", "y").Output()
if err != nil {
fmt.Println("gubernator.sh Failed")
fmt.Println(err)
return
}
fmt.Printf("%s", output)
}
return
}
func (a *Archive) getArchive(suite TestSuite) (string, error) {
a.Do(func() {
a.path, a.err = CreateTestArchive(suite,
getFlag("system-spec-name"),
getFlag("kubelet-config-file"))
})
return a.path, a.err
}
func (a *Archive) deleteArchive() {
path, err := a.getArchive(nil)
if err != nil {
return
}
os.Remove(path)
}
// parseHostsList splits a host list of the form a=1.2.3.4,b=5.6.7.8 into the list of hosts [a,b] while registering the
// given addresses
func parseHostsList(hostList string) []string {
if len(hostList) == 0 {
return nil
}
hosts := strings.Split(hostList, ",")
var hostsOnly []string
for _, host := range hosts {
segs := strings.Split(host, "=")
if len(segs) == 2 {
AddHostnameIP(segs[0], segs[1])
} else if len(segs) > 2 {
klog.Fatalf("invalid format of host %q", hostList)
}
hostsOnly = append(hostsOnly, segs[0])
}
return hostsOnly
}