forked from onosproject/helmit
/
test.go
279 lines (245 loc) · 8.21 KB
/
test.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
// Copyright 2020-present Open Networking Foundation.
//
// 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 cli
import (
"errors"
"go/build"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/wangxn2015/helmit/pkg/job"
"github.com/wangxn2015/helmit/pkg/util/logging"
"github.com/spf13/cobra"
"github.com/wangxn2015/helmit/pkg/test"
"github.com/wangxn2015/helmit/pkg/util/random"
corev1 "k8s.io/api/core/v1"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const testExamples = `
# Run tests packaged in a Docker image.
helmit test --image atomix/kubernetes-tests:latest
# Run tests by referencing a command package and providing a context.
# The specified context will be loaded into the test pod as the current working directory.
helmit test ./cmd/tests --context ./charts
# Run tests in a specific namespace.
helmit test ./cmd/tests -n integration-tests
# Run a test suite by name.
helmit test ./cmd/tests -c ./charts --suite atomix
# Run a single test by name.
helmit test ./cmd/tests -c ./charts --suite atomix --test TestMap
# Override Helm chart values with flags.
# Value overrids must be namespaced with the name of the release to which to apply the value.
helmit test ./cmd/tests -c ./charts --set atomix-controller.image=atomix/atomix-controller:latest --set atomix-raft.replicas=3 --suite atomix
# Override Helm chart values with values files.
# Values files must be key/value pairs where the key is the Helm release name and the value the path to the file.
helmit test ./cmd/tests -c ./charts -f atomix-controller=./atomix-controller.yaml --suite atomix
`
func getTestCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Aliases: []string{"tests"},
Short: "Run tests on Kubernetes",
Example: testExamples,
Args: cobra.MaximumNArgs(1),
RunE: runTestCommand,
}
cmd.Flags().StringP("namespace", "n", "default", "the namespace in which to run the tests")
cmd.Flags().String("service-account", "", "the name of the service account to use to run test pods")
cmd.Flags().StringP("context", "c", "", "the test context")
cmd.Flags().StringP("image", "i", "", "the test image to run")
cmd.Flags().String("image-pull-policy", string(corev1.PullIfNotPresent), "the Docker image pull policy")
cmd.Flags().StringArrayP("values", "f", []string{}, "release values paths")
cmd.Flags().StringArray("set", []string{}, "chart value overrides")
cmd.Flags().StringSliceP("suite", "s", []string{}, "the name of test suite to run")
cmd.Flags().StringSliceP("test", "t", []string{}, "the name of the test method to run")
cmd.Flags().Duration("timeout", 10*time.Minute, "test timeout")
cmd.Flags().Int("iterations", 1, "number of iterations")
cmd.Flags().Bool("until-failure", false, "run until an error is detected")
cmd.Flags().Bool("no-teardown", false, "do not tear down clusters following tests")
cmd.Flags().StringSlice("secret", []string{}, "secrets to pass to the kubernetes pod")
cmd.Flags().StringToStringP("args", "a", map[string]string{}, "a mapping of named test arguments")
return cmd
}
func runTestCommand(cmd *cobra.Command, args []string) error {
setupCommand(cmd)
pkgPath := ""
if len(args) > 0 {
pkgPath = args[0]
}
namespace, _ := cmd.Flags().GetString("namespace")
serviceAccount, _ := cmd.Flags().GetString("service-account")
context, _ := cmd.Flags().GetString("context")
image, _ := cmd.Flags().GetString("image")
files, _ := cmd.Flags().GetStringArray("values")
sets, _ := cmd.Flags().GetStringArray("set")
suites, _ := cmd.Flags().GetStringSlice("suite")
testNames, _ := cmd.Flags().GetStringSlice("test")
timeout, _ := cmd.Flags().GetDuration("timeout")
pullPolicy, _ := cmd.Flags().GetString("image-pull-policy")
iterations, _ := cmd.Flags().GetInt("iterations")
untilFailure, _ := cmd.Flags().GetBool("until-failure")
noTeardown, _ := cmd.Flags().GetBool("no-teardown")
secretsArray, _ := cmd.Flags().GetStringSlice("secret")
testArgs, _ := cmd.Flags().GetStringToString("args")
// Either a command package or image must be specified
if pkgPath == "" && image == "" {
return errors.New("must specify either a test package or --image to run")
}
// Generate a unique test ID
testID := random.NewPetName(2)
// If a command package was provided, build a binary and update the image tag
var executable string
if pkgPath != "" {
executable = filepath.Join(os.TempDir(), "helmit", testID)
err := buildBinary(pkgPath, executable)
if err != nil {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
return err
}
if image == "" {
image = "onosproject/helmit-runner:latest"
}
}
// If a context was provided, convert the context to its absolute path
if context != "" {
path, err := filepath.Abs(context)
if err != nil {
return err
}
context = path
}
if untilFailure {
iterations = -1
}
valueFiles, err := parseFiles(files)
if err != nil {
return err
}
values, err := parseOverrides(sets)
if err != nil {
return err
}
secrets, err := parseSecrets(secretsArray)
if err != nil {
return err
}
config := &test.Config{
Config: &job.Config{
ID: testID,
ServiceAccount: serviceAccount,
Namespace: namespace,
Image: image,
ImagePullPolicy: corev1.PullPolicy(pullPolicy),
Executable: executable,
Context: context,
ValueFiles: valueFiles,
Values: values,
Timeout: timeout,
NoTeardown: noTeardown,
Secrets: secrets,
},
Suites: suites,
Tests: testNames,
Iterations: iterations,
Verbose: logging.GetVerbose(),
NoTeardown: noTeardown,
Args: testArgs,
}
return test.Run(config)
}
func buildBinary(pkgPath, binPath string) error {
workDir, err := os.Getwd()
if err != nil {
return err
}
pkg, err := build.Import(pkgPath, workDir, build.ImportComment)
if err != nil {
return err
}
if !pkg.IsCommand() {
return errors.New("test package must be a command")
}
// Build the command
build := exec.Command("go", "build", "-o", binPath, pkgPath)
build.Stderr = os.Stderr
build.Stdout = os.Stdout
env := os.Environ()
env = append(env, "GOOS=linux", "CGO_ENABLED=0")
build.Env = env
return build.Run()
}
func parseFiles(files []string) (map[string][]string, error) {
if len(files) == 0 {
return map[string][]string{}, nil
}
values := make(map[string][]string)
for _, path := range files {
index := strings.Index(path, "=")
if index == -1 {
return nil, errors.New("values file must be in the format {release}={file}")
}
release, path := path[:index], path[index+1:]
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
_, err = os.Stat(path)
if err != nil {
return nil, err
}
releaseValues, ok := values[release]
if !ok {
releaseValues = make([]string, 0)
}
values[release] = append(releaseValues, path)
}
return values, nil
}
func parseOverrides(values []string) (map[string][]string, error) {
overrides := make(map[string][]string)
for _, set := range values {
index := strings.Index(set, ".")
if index == -1 {
return nil, errors.New("values must be in the format {release}.{path}={value}")
}
release, value := set[:index], set[index+1:]
override, ok := overrides[release]
if !ok {
override = make([]string, 0)
}
overrides[release] = append(override, value)
}
return overrides, nil
}
func parseSecrets(secrets []string) (map[string]string, error) {
if len(secrets) == 0 {
return map[string]string{}, nil
}
values := make(map[string]string)
for _, secret := range secrets {
index := strings.Index(secret, "=")
if index == -1 {
return nil, errors.New("secrets must be in the format {key}={value}")
}
key, value := secret[:index], secret[index+1:]
values[key] = value
}
return values, nil
}