forked from nytimes/drone-gke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
542 lines (465 loc) · 18.7 KB
/
main_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
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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/urfave/cli"
)
type MockedRunner struct {
mock.Mock
Runner
}
func (m *MockedRunner) Run(name string, arg ...string) error {
// https://godoc.org/github.com/stretchr/testify/mock
// Arguments given in .On()
args := m.Called(append([]string{name}, arg...))
// Returns error given in .Return()
return args.Error(0)
}
func TestCheckParams(t *testing.T) {
// Testing with cli.Context:
// https://github.com/urfave/cli/blob/master/context_test.go#L10
// No args set
set := flag.NewFlagSet("empty-set", 0)
c := cli.NewContext(nil, set, nil)
err := checkParams(c)
assert.Error(t, err)
// Required args set
set = flag.NewFlagSet("missing-zone-region", 0)
c = cli.NewContext(nil, set, nil)
set.String("token", "{}", "")
set.String("cluster", "cluster-0", "")
err = checkParams(c)
assert.Error(t, err)
// Mutually-exclusive args set
set = flag.NewFlagSet("both-zone-region", 0)
c = cli.NewContext(nil, set, nil)
set.String("token", "{}", "")
set.String("zone", "us-east1-b", "")
set.String("region", "us-east1", "")
set.String("cluster", "cluster-0", "")
err = checkParams(c)
assert.Error(t, err)
// Zonal args set
set = flag.NewFlagSet("zonal-set", 0)
c = cli.NewContext(nil, set, nil)
set.String("token", "{}", "")
set.String("zone", "us-east1-b", "")
set.String("cluster", "cluster-0", "")
err = checkParams(c)
assert.NoError(t, err)
// Regional args set
set = flag.NewFlagSet("regional-set", 0)
c = cli.NewContext(nil, set, nil)
set.String("token", "{}", "")
set.String("region", "us-west1", "")
set.String("cluster", "cluster-0", "")
err = checkParams(c)
assert.NoError(t, err)
}
func TestValidateKubectlVersion(t *testing.T) {
// kubectl-version is NOT set (using default kubectl version)
set := flag.NewFlagSet("default-kubectl-version", 0)
c := cli.NewContext(nil, set, nil)
availableVersions := []string{}
err := validateKubectlVersion(c, availableVersions)
assert.NoError(t, err, "expected validateKubectlVersion to return nil when no kubectl-version param was set")
// kubectl-version is set and extra kubectl versions are NOT available
set = flag.NewFlagSet("kubectl-version-set-no-extra-versions-available", 0)
c = cli.NewContext(nil, set, nil)
set.String("kubectl-version", "1.14", "")
availableVersions = []string{}
err = validateKubectlVersion(c, availableVersions)
assert.Error(t, err, "expected validateKubectlVersion to return an error when no extra kubectl versions are available")
// kubectl-version is set, extra kubectl versions are available, kubectl-version is included
set = flag.NewFlagSet("valid-kubectl-version", 0)
c = cli.NewContext(nil, set, nil)
set.String("kubectl-version", "1.13", "")
availableVersions = []string{"1.11", "1.12", "1.13", "1.14"}
err = validateKubectlVersion(c, availableVersions)
assert.NoError(t, err, "expected validateKubectlVersion to return an error when kubectl-version is set, extra kubectl versions are available, kubectl-version is included")
// kubectl-version is set, extra kubectl versions are available, kubectl-version is NOT included
set = flag.NewFlagSet("invalid-kubectl-version", 0)
c = cli.NewContext(nil, set, nil)
set.String("kubectl-version", "9.99", "")
availableVersions = []string{"1.11", "1.12", "1.13", "1.14"}
err = validateKubectlVersion(c, availableVersions)
assert.Error(t, err, "expected validateKubectlVersion to return nil when kubectl-version is set, extra kubectl versions are available, kubectl-version is NOT included")
}
func TestGetProjectFromToken(t *testing.T) {
token := "{\"project_id\":\"test-project\"}"
assert.Equal(t, "test-project", getProjectFromToken(token))
}
func TestParseVars(t *testing.T) {
// No vars set
set := flag.NewFlagSet("test-set", 0)
c := cli.NewContext(nil, set, nil)
vars, err := parseVars(c)
assert.Equal(t, map[string]interface{}{}, vars)
assert.NoError(t, err)
// Invalid JSON
set.String("vars", "{", "")
vars, err = parseVars(c)
assert.Equal(t, map[string]interface{}(nil), vars)
assert.Error(t, err)
// No error
set = flag.NewFlagSet("test-set", 0)
set.String("vars", "{\"var0\": \"val0\", \"var1\": \"val1\"}", "")
c = cli.NewContext(nil, set, nil)
vars, err = parseVars(c)
assert.Equal(t, map[string]interface{}{"var0": "val0", "var1": "val1"}, vars)
assert.NoError(t, err)
}
func TestParseSecrets(t *testing.T) {
// Unset all secrets first
os.Clearenv()
// No secrets
secrets, err := parseSecrets()
assert.Equal(t, map[string]string{}, secrets)
assert.NoError(t, err)
// No error
os.Setenv("SECRET_TEST0", "test0")
os.Setenv("SECRET_TEST1", "test1")
os.Setenv("SECRET_BASE64_TEST0", "dGVzdDA=")
secrets, err = parseSecrets()
assert.Equal(
t,
map[string]string{
"SECRET_TEST0": "dGVzdDA=",
"SECRET_TEST1": "dGVzdDE=",
"SECRET_BASE64_TEST0": "dGVzdDA=",
},
secrets)
assert.NoError(t, err)
// Empty string is not allowed
os.Clearenv()
os.Setenv("SECRET_TEST", "")
secrets, err = parseSecrets()
assert.Equal(t, map[string]string(nil), secrets)
assert.Error(t, err)
// Not able to use os.Setenv() to set env vars without "=", or duplicate keys
}
func TestFetchCredentials(t *testing.T) {
// Set cli.Context
zonal := flag.NewFlagSet("zonal-set", 0)
zonal.String("token", "{\"key\", \"val\"}", "")
zonal.String("cluster", "cluster-0", "")
zonal.String("zone", "us-east1-b", "")
zonalContext := cli.NewContext(nil, zonal, nil)
regional := flag.NewFlagSet("regional-set", 0)
regional.String("token", "{\"key\", \"val\"}", "")
regional.String("cluster", "cluster-0", "")
regional.String("region", "us-west1", "")
regionalContext := cli.NewContext(nil, regional, nil)
// No error
testRunner := new(MockedRunner)
testRunner.On("Run", []string{"gcloud", "auth", "activate-service-account", "--key-file", "/tmp/gcloud.json"}).Return(nil)
testRunner.On("Run", []string{"gcloud", "container", "clusters", "get-credentials", "cluster-0", "--project", "test-project", "--zone", "us-east1-b"}).Return(nil)
testRunner.On("Run", []string{"gcloud", "container", "clusters", "get-credentials", "cluster-0", "--project", "test-project", "--region", "us-west1"}).Return(nil)
zonalErr := fetchCredentials(zonalContext, "test-project", testRunner)
regionalErr := fetchCredentials(regionalContext, "test-project", testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, zonalErr)
assert.NoError(t, regionalErr)
// Verify token file
buf, err := ioutil.ReadFile("/tmp/gcloud.json")
assert.Equal(t, "{\"key\", \"val\"}", string(buf))
// Run() error
testRunner = new(MockedRunner)
testRunner.On("Run", []string{"gcloud", "auth", "activate-service-account", "--key-file", "/tmp/gcloud.json"}).Return(fmt.Errorf("e"))
err = fetchCredentials(zonalContext, "test-project", testRunner)
testRunner.AssertExpectations(t)
assert.Error(t, err)
}
func TestTemplateData(t *testing.T) {
// Set cli.Context with data
set := flag.NewFlagSet("test-set", 0)
set.String("drone-branch", "master", "")
set.String("drone-build-number", "2", "")
set.String("drone-commit", "e0f21b90a", "")
set.String("drone-tag", "v0.0.0", "")
set.String("cluster", "cluster-0", "")
set.String("zone", "us-east1-b", "")
c := cli.NewContext(nil, set, nil)
// No error
// Create data maps
vars := map[string]interface{}{"key0": "val0", "key1": "hello $USER"}
secrets := map[string]string{"SECRET_TEST": "test_val"}
// Call
tmplData, secretsData, secretsDataRedacted, err := templateData(c, "test-project", vars, secrets)
// Verify
assert.Equal(t, map[string]interface{}{
"BRANCH": "master",
"BUILD_NUMBER": "2",
"COMMIT": "e0f21b90a",
"TAG": "v0.0.0",
"project": "test-project",
"zone": "us-east1-b",
"cluster": "cluster-0",
"namespace": "",
"key0": "val0",
"key1": "hello $USER",
}, tmplData)
assert.Equal(t, map[string]interface{}{
"key0": "val0",
"key1": "hello $USER",
"SECRET_TEST": "test_val",
}, secretsData)
assert.Equal(t, map[string]string{"SECRET_TEST": "VALUE REDACTED"}, secretsDataRedacted)
assert.NoError(t, err)
// Variable overrides existing ones
vars = map[string]interface{}{"zone": "us-east4-b"}
tmplData, secretsData, secretsDataRedacted, err = templateData(c, "us-east1-b", vars, secrets)
assert.Error(t, err)
// Secret overrides variable
vars = map[string]interface{}{"SECRET_TEST": "val0"}
secrets = map[string]string{"SECRET_TEST": "test_val"}
tmplData, secretsData, secretsDataRedacted, err = templateData(c, "us-east1-b", vars, secrets)
assert.Error(t, err)
}
func TestTemplateDataExpandingVars(t *testing.T) {
os.Clearenv()
os.Setenv("USER", "drone-user")
// Set cli.Context with data
set := flag.NewFlagSet("test-set", 0)
set.String("drone-branch", "master", "")
set.String("drone-build-number", "2", "")
set.String("drone-commit", "e0f21b90a", "")
set.String("drone-tag", "v0.0.0", "")
set.String("cluster", "cluster-0", "")
set.String("zone", "us-east1-b", "")
set.Bool("expand-env-vars", true, "")
c := cli.NewContext(nil, set, nil)
// No error
// Create data maps
vars := map[string]interface{}{"key0": "val0", "key1": "hello $USER"}
secrets := map[string]string{"SECRET_TEST": "test_val"}
// Call
tmplData, secretsData, secretsDataRedacted, err := templateData(c, "test-project", vars, secrets)
// Verify
assert.Equal(t, map[string]interface{}{
"BRANCH": "master",
"BUILD_NUMBER": "2",
"COMMIT": "e0f21b90a",
"TAG": "v0.0.0",
"project": "test-project",
"zone": "us-east1-b",
"cluster": "cluster-0",
"namespace": "",
"key0": "val0",
"key1": "hello drone-user",
}, tmplData)
assert.Equal(t, map[string]interface{}{
"key0": "val0",
"key1": "hello drone-user",
"SECRET_TEST": "test_val",
}, secretsData)
assert.Equal(t, map[string]string{"SECRET_TEST": "VALUE REDACTED"}, secretsDataRedacted)
assert.NoError(t, err)
// Variable overrides existing ones
vars = map[string]interface{}{"zone": "us-east4-b"}
tmplData, secretsData, secretsDataRedacted, err = templateData(c, "us-east1-b", vars, secrets)
assert.Error(t, err)
// Secret overrides variable
vars = map[string]interface{}{"SECRET_TEST": "val0"}
secrets = map[string]string{"SECRET_TEST": "test_val"}
tmplData, secretsData, secretsDataRedacted, err = templateData(c, "us-east1-b", vars, secrets)
assert.Error(t, err)
}
func TestRenderTemplates(t *testing.T) {
// Mkdir for testing template files
os.MkdirAll("/tmp/drone-gke-tests/", os.ModePerm)
kubeTemplatePath := "/tmp/drone-gke-tests/.kube.yml"
secretTemplatePath := "/tmp/drone-gke-tests/.kube.sec.yml"
// Set cli.Context with data
set := flag.NewFlagSet("test-set", 0)
set.String("kube-template", kubeTemplatePath, "")
set.String("secret-template", secretTemplatePath, "")
c := cli.NewContext(nil, set, nil)
tmplData := map[string]interface{}{
"COMMIT": "e0f21b90a",
"key0": "val0",
}
secretsData := map[string]interface{}{"SECRET_TEST": "test_sec_val"}
// No template file, should error
os.Remove(kubeTemplatePath)
os.Remove(secretTemplatePath)
_, err := renderTemplates(c, tmplData, secretsData)
assert.Error(t, err)
// Normal
// Create test template files
tmplBuf := []byte("{{.COMMIT}}-{{.key0}}")
err = ioutil.WriteFile(kubeTemplatePath, tmplBuf, 0600)
assert.NoError(t, err)
tmplBuf = []byte("{{.SECRET_TEST}}")
err = ioutil.WriteFile(secretTemplatePath, tmplBuf, 0600)
assert.NoError(t, err)
// Render
manifestPaths, err := renderTemplates(c, tmplData, secretsData)
assert.NoError(t, err)
// Verify token files
buf, err := ioutil.ReadFile(manifestPaths[kubeTemplatePath])
assert.Equal(t, "e0f21b90a-val0", string(buf))
buf, err = ioutil.ReadFile(manifestPaths[secretTemplatePath])
assert.Equal(t, "test_sec_val", string(buf))
// Secret variables shouldn't be available in kube template
tmplBuf = []byte("{{.SECRET_TEST}}")
err = ioutil.WriteFile(kubeTemplatePath, tmplBuf, 0600)
_, err = renderTemplates(c, tmplData, secretsData)
assert.Error(t, err)
}
func TestPrintKubectlVersion(t *testing.T) {
testRunner := new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "version"}).Return(nil)
err := printKubectlVersion(testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
}
func TestSetNamespace(t *testing.T) {
// Zonal args set
set := flag.NewFlagSet("zonal-set", 0)
set.String("zone", "us-east1-b", "")
set.String("cluster", "cluster-0", "")
set.String("namespace", "test-ns", "")
set.Bool("dry-run", false, "")
c := cli.NewContext(nil, set, nil)
testRunner := new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "config", "set-context", "gke_test-project_us-east1-b_cluster-0", "--namespace", "test-ns"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--filename", "/tmp/namespace.json"}).Return(nil)
err := setNamespace(c, "test-project", testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
// Region args set
set = flag.NewFlagSet("regional-set", 0)
set.String("region", "us-west1", "")
set.String("cluster", "regional-cluster", "")
set.String("namespace", "test-ns", "")
set.Bool("dry-run", false, "")
c = cli.NewContext(nil, set, nil)
testRunner = new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "config", "set-context", "gke_test-project_us-west1_regional-cluster", "--namespace", "test-ns"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--filename", "/tmp/namespace.json"}).Return(nil)
err = setNamespace(c, "test-project", testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
// Verify written file
buf, err := ioutil.ReadFile("/tmp/namespace.json")
assert.Equal(t, "\n---\napiVersion: v1\nkind: Namespace\nmetadata:\n name: test-ns\n", string(buf))
// Dry-run
set = flag.NewFlagSet("test-set", 0)
set.String("zone", "us-east1-b", "")
set.String("cluster", "cluster-0", "")
set.String("namespace", "Feature/1892-TEST-NS", "")
set.Bool("dry-run", true, "")
c = cli.NewContext(nil, set, nil)
testRunner = new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "config", "set-context", "gke_test-project_us-east1-b_cluster-0", "--namespace", "feature-1892-test-ns"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/tmp/namespace.json"}).Return(nil)
err = setNamespace(c, "test-project", testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
}
func TestApplyManifests(t *testing.T) {
// Normal
set := flag.NewFlagSet("test-set", 0)
set.String("kube-template", ".kube.yml", "")
set.String("secret-template", ".kube.sec.yml", "")
set.Bool("dry-run", false, "")
c := cli.NewContext(nil, set, nil)
manifestPaths := map[string]string{
".kube.yml": "/path/to/kube-tamplate",
".kube.sec.yml": "/path/to/secret-tamplate",
}
testRunner := new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/path/to/kube-tamplate"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/path/to/secret-tamplate"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--filename", "/path/to/kube-tamplate"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--filename", "/path/to/secret-tamplate"}).Return(nil)
err := applyManifests(c, manifestPaths, testRunner, testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
// No secrets manifest
manifestPaths = map[string]string{
".kube.yml": "/path/to/kube-tamplate",
}
testRunner = new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/path/to/kube-tamplate"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--filename", "/path/to/kube-tamplate"}).Return(nil)
err = applyManifests(c, manifestPaths, testRunner, testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
// Dry-run
set = flag.NewFlagSet("test-set", 0)
set.String("kube-template", ".kube.yml", "")
set.String("secret-template", ".kube.sec.yml", "")
set.Bool("dry-run", true, "")
c = cli.NewContext(nil, set, nil)
manifestPaths = map[string]string{
".kube.yml": "/path/to/kube-tamplate",
".kube.sec.yml": "/path/to/secret-tamplate",
}
testRunner = new(MockedRunner)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/path/to/kube-tamplate"}).Return(nil)
testRunner.On("Run", []string{"kubectl", "apply", "--record", "--dry-run", "--filename", "/path/to/secret-tamplate"}).Return(nil)
err = applyManifests(c, manifestPaths, testRunner, testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
}
// RunWaitForRollout is a helper function for testing WaitForRollout. For each flag-value
// in flagValues it will expect a corresponding call of the form:
// "kubectl rollout status <expected-value> ..."
func RunWaitForRollout(t *testing.T, specs []string, expectedValues []string) {
set := flag.NewFlagSet("test-set", 0)
set.Int("wait-seconds", 256, "")
set.String("namespace", "test-ns", "")
strSlice := cli.StringSlice{}
for _, spec := range specs {
strSlice.Set(spec)
}
strSliceFlag := cli.StringSliceFlag{Name: "wait-deployments", Value: &strSlice}
strSliceFlag.Apply(set)
c := cli.NewContext(nil, set, nil)
testRunner := new(MockedRunner)
for _, s := range expectedValues {
testRunner.On("Run", []string{"timeout", "256", "kubectl", "rollout", "status", s, "--namespace", "test-ns"}).Return(nil)
}
err := waitForRollout(c, testRunner)
testRunner.AssertExpectations(t)
assert.NoError(t, err)
}
func TestWaitForRollout(t *testing.T) {
RunWaitForRollout(t, []string{"deployment/d1"}, []string{"deployment/d1"})
RunWaitForRollout(t,
[]string{"deployment/d1", "statefulset/s1"},
[]string{"deployment/d1", "statefulset/s1"})
RunWaitForRollout(t, []string{"d1"}, []string{"deployment/d1"})
RunWaitForRollout(t,
[]string{"d1", "d2"},
[]string{"deployment/d1", "deployment/d2"})
}
func TestApplyArgs(t *testing.T) {
args := applyArgs(false, "/path/to/file/1")
assert.Equal(t, []string{"apply", "--record", "--filename", "/path/to/file/1"}, args)
args = applyArgs(true, "/path/to/file/2")
assert.Equal(t, []string{"apply", "--record", "--dry-run", "--filename", "/path/to/file/2"}, args)
}
func TestPrintTrimmedError(t *testing.T) {
output := &bytes.Buffer{}
// Empty
printTrimmedError(strings.NewReader(""), output)
assert.Equal(t, "\n", output.String())
// One line
output.Reset()
printTrimmedError(strings.NewReader("one line"), output)
assert.Equal(t, "one line\n", output.String())
// Mutiple lines
output.Reset()
printTrimmedError(strings.NewReader("line 1\nline 2\nline 3"), output)
assert.Equal(t, "line 3\n", output.String())
}