forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
e2e.go
232 lines (200 loc) · 6.71 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
/*
Copyright 2014 The Kubernetes Authors 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 (
"flag"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"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.")
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.")
test = flag.Bool("test", false, "Run Ginkgo tests.")
testArgs = flag.String("test_args", "", "Space-separated list of arguments to pass to Ginkgo test runner.")
root = flag.String("root", absOrDie(filepath.Clean(filepath.Join(path.Base(os.Args[0]), ".."))), "Root directory of kubernetes repository.")
verbose = flag.Bool("v", false, "If true, print all command output.")
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.")
checkNodeCount = flag.Bool("check_node_count", true, ""+
"By default, verify that the cluster has at least two nodes."+
"You can explicitly set to false if you're, e.g., testing single-node clusters "+
"for which the node count is supposed to be one.")
ctlCmd = flag.String("ctl", "", "If nonempty, pass this as an argument, and call kubectl. Implies -v. (-test, -cfg, -ctl are mutually exclusive)")
)
const (
minNodeCount = 2
)
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() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
if *isup {
status := 1
if IsUp() {
status = 0
log.Printf("Cluster is UP")
} else {
log.Printf("Cluster is DOWN")
}
os.Exit(status)
}
if *build {
// The build-release script needs stdin to ask the user whether
// it's OK to download the docker image.
cmd := exec.Command(path.Join(*root, "hack/e2e-internal/build-release.sh"))
cmd.Stdin = os.Stdin
if !finishRunning("build-release", cmd) {
log.Fatal("Error building. Aborting.")
}
}
os.Setenv("KUBECTL", *root+`/cluster/kubectl.sh`+kubectlArgs())
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 !finishRunning("push", exec.Command(path.Join(*root, "hack/e2e-internal/e2e-push.sh"))) {
log.Fatal("Error pushing e2e cluster. Aborting.")
}
}
success := true
switch {
case *ctlCmd != "":
ctlArgs := strings.Fields(*ctlCmd)
os.Setenv("KUBE_CONFIG_FILE", "config-test.sh")
success = finishRunning("'kubectl "+*ctlCmd+"'", exec.Command(path.Join(*root, "cluster/kubectl.sh"), ctlArgs...))
case *test:
success = Test()
}
if *down {
TearDown()
}
if !success {
os.Exit(1)
}
}
func TearDown() bool {
return finishRunning("teardown", exec.Command(path.Join(*root, "hack/e2e-internal/e2e-down.sh")))
}
// 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 finishRunning("up", exec.Command(path.Join(*root, "hack/e2e-internal/e2e-up.sh")))
}
// Ensure that the cluster is large engough to run the e2e tests.
func ValidateClusterSize() {
if os.Getenv("FEDERATION") == "true" {
//TODO(colhom): federated equivalent of ValidateClusterSize
return
}
// Check that there are at least minNodeCount nodes running
cmd := exec.Command(path.Join(*root, "hack/e2e-internal/e2e-cluster-size.sh"))
if *verbose {
cmd.Stderr = os.Stderr
}
stdout, err := cmd.Output()
if err != nil {
log.Fatalf("Could not get nodes to validate cluster size (%s)", err)
}
numNodes, err := strconv.Atoi(strings.TrimSpace(string(stdout)))
if err != nil {
log.Fatalf("Could not count number of nodes to validate cluster size (%s)", err)
}
if numNodes < minNodeCount {
log.Fatalf("Cluster size (%d) is too small to run e2e tests. %d Nodes are required.", numNodes, minNodeCount)
}
}
// Is the e2e cluster up?
func IsUp() bool {
return finishRunning("get status", exec.Command(path.Join(*root, "hack/e2e-internal/e2e-status.sh")))
}
func Test() bool {
if !IsUp() {
log.Fatal("Testing requested, but e2e cluster not up!")
}
if *checkNodeCount {
ValidateClusterSize()
}
if os.Getenv("FEDERATION") == "" {
return finishRunning("Ginkgo tests", exec.Command(filepath.Join(*root, "hack/ginkgo-e2e.sh"), strings.Fields(*testArgs)...))
} else {
if *testArgs == "" {
*testArgs = "--ginkgo.focus=\\[Feature:Federation\\]"
}
return finishRunning("Federated Ginkgo tests", exec.Command(filepath.Join(*root, "hack/federated-ginkgo-e2e.sh"), strings.Fields(*testArgs)...))
}
}
func finishRunning(stepName string, cmd *exec.Cmd) bool {
if *verbose {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
log.Printf("Running: %v", stepName)
defer func(start time.Time) {
log.Printf("Step '%s' finished in %s", stepName, time.Since(start))
}(time.Now())
if err := cmd.Run(); err != nil {
log.Printf("Error running %v: %v", stepName, err)
return false
}
return true
}
// returns either "", or a list of args intended for appending with the
// kubectl command (beginning with a space).
func kubectlArgs() string {
args := []string{""}
if *checkVersionSkew {
args = append(args, "--match-server-version")
}
return strings.Join(args, " ")
}