Debian Codegraph @

cmd/config/internal/commands/e2e/e2e_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package e2e
5
6 import (
7 "bytes"
8 "fmt"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "runtime"
13 "strings"
14 "sync"
15 "testing"
16
17 "github.com/stretchr/testify/assert"
18 "sigs.k8s.io/kustomize/kyaml/testutil"
19 )
20
21 func TestRunE2e(t *testing.T) {
22 binDir := build()
23
24 tests := []struct {
25 name string
26 args func(string) []string
27 files func(string) map[string]string
28 expectedFiles func(string) map[string]string
29 expectedErr string
30 skipIfFalseEnv string
31 }{
32
33 {
34 name: "exec_function_no_args",
35 args: func(d string) []string {
36 return []string{
37 "--enable-exec", "--exec-path", filepath.Join(d, e2eConfigDir),
38 }
39 },
40 files: func(d string) map[string]string {
41 return map[string]string{
42 "deployment.yaml": `
43 apiVersion: apps/v1
44 kind: Deployment
45 metadata:
46 name: foo
47 `,
48 }
49 },
50 expectedFiles: func(d string) map[string]string {
51 return map[string]string{
52 "deployment.yaml": `
53 apiVersion: apps/v1
54 kind: Deployment
55 metadata:
56 name: foo
57 annotations:
58 a-string-value: ''
59 a-int-value: '0'
60 a-bool-value: 'false'
61 `,
62 }
63 },
64 },
65
66 {
67 name: "exec_function_no_args_json",
68 args: func(d string) []string {
69 return []string{
70 "--enable-exec", "--exec-path", filepath.Join(d, "e2econtainerconfig"),
71 }
72 },
73 files: func(d string) map[string]string {
74 return map[string]string{
75 "deployment.json": `
76 {
77 "apiVersion": "apps/v1",
78 "kind": "Deployment",
79 "metadata": {
80 "name": "foo"
81 }
82 }
83 `,
84 }
85 },
86 expectedFiles: func(d string) map[string]string {
87 return map[string]string{
88 "deployment.json": `
89 {
90 "apiVersion": "apps/v1",
91 "kind": "Deployment",
92 "metadata": {
93 "annotations": {
94 "a-bool-value": "false",
95 "a-int-value": "0",
96 "a-string-value": ""
97 },
98 "name": "foo"
99 }
100 }
101 `,
102 }
103 },
104 },
105
106 {
107 name: "exec_function_args",
108 args: func(d string) []string {
109 return []string{
110 "--enable-exec", "--exec-path", filepath.Join(d, "e2econtainerconfig"),
111 "--", "stringValue=a", "intValue=1", "boolValue=true",
112 }
113 },
114 files: func(d string) map[string]string {
115 return map[string]string{
116 "deployment.yaml": `
117 apiVersion: apps/v1
118 kind: Deployment
119 metadata:
120 name: foo
121 `,
122 }
123 },
124 expectedFiles: func(d string) map[string]string {
125 return map[string]string{
126 "deployment.yaml": `
127 apiVersion: apps/v1
128 kind: Deployment
129 metadata:
130 name: foo
131 annotations:
132 a-string-value: 'a'
133 a-int-value: '1'
134 a-bool-value: 'true'
135 `,
136 }
137 },
138 },
139
140 {
141 name: "exec_function_config",
142 args: func(d string) []string {
143 return []string{"--enable-exec"}
144 },
145 files: func(d string) map[string]string {
146 return map[string]string{
147 "deployment.yaml": fmt.Sprintf(`
148 apiVersion: apps/v1
149 kind: Deployment
150 metadata:
151 name: foo
152 annotations:
153 config.kubernetes.io/function: |
154 exec:
155 path: %s
156 `, filepath.Join(d, "e2econtainerconfig")),
157 }
158 },
159 expectedFiles: func(d string) map[string]string {
160 return map[string]string{
161 "deployment.yaml": fmt.Sprintf(`
162 apiVersion: apps/v1
163 kind: Deployment
164 metadata:
165 name: foo
166 annotations:
167 config.kubernetes.io/function: |
168 exec:
169 path: %s
170 a-string-value: ''
171 a-int-value: '0'
172 a-bool-value: 'false'
173 `, filepath.Join(d, "e2econtainerconfig"))}
174 },
175 },
176
177 //
178 // Starklark function tests
179 //
180 {
181 name: "exec_function_config_data",
182 args: func(d string) []string {
183 return []string{"--enable-exec"}
184 },
185 files: func(d string) map[string]string {
186 return map[string]string{
187 "config.yaml": fmt.Sprintf(`
188 apiVersion: example.com/v1alpha1
189 kind: Input
190 metadata:
191 name: foo
192 annotations:
193 config.kubernetes.io/function: |
194 exec:
195 path: %s
196 data:
197 stringValue: a
198 intValue: 2
199 boolValue: true
200 `, filepath.Join(d, "e2econtainerconfig")),
201 "deployment.yaml": `
202 apiVersion: apps/v1
203 kind: Deployment
204 metadata:
205 name: foo
206 `,
207 }
208 },
209 expectedFiles: func(d string) map[string]string {
210 return map[string]string{
211 "config.yaml": fmt.Sprintf(`
212 apiVersion: example.com/v1alpha1
213 kind: Input
214 metadata:
215 name: foo
216 annotations:
217 config.kubernetes.io/function: |
218 exec:
219 path: %s
220 a-string-value: 'a'
221 a-int-value: '2'
222 a-bool-value: 'true'
223 data:
224 stringValue: a
225 intValue: 2
226 boolValue: true
227 `, filepath.Join(d, "e2econtainerconfig")),
228 "deployment.yaml": `
229 apiVersion: apps/v1
230 kind: Deployment
231 metadata:
232 name: foo
233 annotations:
234 a-string-value: 'a'
235 a-int-value: '2'
236 a-bool-value: 'true'
237 `,
238 }
239 },
240 },
241
242 {
243 //
244 // NOTE: Do not change the expected value of this test. It is to ensure that
245 // exec functions are off by default when run from the CLI.
246 // exec functions execute arbitrary code outside of a sandbox environment.
247 //
248 name: "exec_function_config_disabled",
249 args: func(d string) []string { return []string{} },
250 files: func(d string) map[string]string {
251 return map[string]string{
252 "config.yaml": fmt.Sprintf(`
253 apiVersion: example.com/v1alpha1
254 kind: Input
255 metadata:
256 name: foo
257 annotations:
258 config.kubernetes.io/function: |
259 exec:
260 path: %s
261 data:
262 stringValue: a
263 intValue: 2
264 boolValue: true
265 `, filepath.Join(d, "e2econtainerconfig")),
266 "deployment.yaml": `
267 apiVersion: apps/v1
268 kind: Deployment
269 metadata:
270 name: foo
271 `,
272 }
273 },
274 expectedFiles: func(d string) map[string]string {
275 return map[string]string{
276 "config.yaml": fmt.Sprintf(`
277 apiVersion: example.com/v1alpha1
278 kind: Input
279 metadata:
280 name: foo
281 annotations:
282 config.kubernetes.io/function: |
283 exec:
284 path: %s
285 data:
286 stringValue: a
287 intValue: 2
288 boolValue: true
289 `, filepath.Join(d, "e2econtainerconfig")),
290 "deployment.yaml": `
291 apiVersion: apps/v1
292 kind: Deployment
293 metadata:
294 name: foo
295 annotations:
296 internal.config.kubernetes.io/annotations-migration-resource-id: '1'
297 `,
298 }
299 },
300 },
301
302 {
303 name: "exec_function_no_enable",
304 expectedErr: "must specify --enable-exec with --exec-path",
305 args: func(d string) []string {
306 return []string{"--exec-path", filepath.Join(d, "e2econtainerconfig")}
307 },
308 files: func(d string) map[string]string {
309 return map[string]string{
310 "deployment.yaml": `
311 apiVersion: apps/v1
312 kind: Deployment
313 metadata:
314 name: foo
315 `,
316 }
317 },
318 expectedFiles: func(d string) map[string]string {
319 return map[string]string{
320 "deployment.yaml": `
321 apiVersion: apps/v1
322 kind: Deployment
323 metadata:
324 name: foo
325 annotations:
326 a-string-value: ''
327 a-int-value: '0'
328 a-bool-value: 'false'
329 `,
330 }
331 },
332 },
333
334 //
335 // Container
336 //
337 {
338 name: "container_function_no_args",
339 skipIfFalseEnv: "KUSTOMIZE_DOCKER_E2E",
340 args: func(d string) []string {
341 return []string{"--image", "gcr.io/kustomize-functions/e2econtainerconfig"}
342 },
343 files: func(d string) map[string]string {
344 return map[string]string{
345 "deployment.yaml": `
346 apiVersion: apps/v1
347 kind: Deployment
348 metadata:
349 name: foo
350 `,
351 }
352 },
353 expectedFiles: func(d string) map[string]string {
354 return map[string]string{
355 "deployment.yaml": `
356 apiVersion: apps/v1
357 kind: Deployment
358 metadata:
359 name: foo
360 annotations:
361 a-string-value: ''
362 a-int-value: '0'
363 a-bool-value: 'false'
364 `,
365 }
366 },
367 },
368
369 {
370 name: "container_function_args",
371 skipIfFalseEnv: "KUSTOMIZE_DOCKER_E2E",
372 args: func(d string) []string {
373 return []string{
374 "--image", "gcr.io/kustomize-functions/e2econtainerconfig",
375 "--", "stringValue=a", "intValue=1", "boolValue=true",
376 }
377 },
378 files: func(d string) map[string]string {
379 return map[string]string{
380 "deployment.yaml": `
381 apiVersion: apps/v1
382 kind: Deployment
383 metadata:
384 name: foo
385 `,
386 }
387 },
388 expectedFiles: func(d string) map[string]string {
389 return map[string]string{
390 "deployment.yaml": `
391 apiVersion: apps/v1
392 kind: Deployment
393 metadata:
394 name: foo
395 annotations:
396 a-string-value: 'a'
397 a-int-value: '1'
398 a-bool-value: 'true'
399 `,
400 }
401 },
402 },
403
404 {
405 name: "container_function_config",
406 skipIfFalseEnv: "KUSTOMIZE_DOCKER_E2E",
407 args: func(d string) []string { return []string{} },
408 files: func(d string) map[string]string {
409 return map[string]string{
410 "deployment.yaml": `
411 apiVersion: apps/v1
412 kind: Deployment
413 metadata:
414 name: foo
415 annotations:
416 config.kubernetes.io/function: |
417 container:
418 image: "gcr.io/kustomize-functions/e2econtainerconfig"
419 `,
420 }
421 },
422 expectedFiles: func(d string) map[string]string {
423 return map[string]string{
424 "deployment.yaml": `
425 apiVersion: apps/v1
426 kind: Deployment
427 metadata:
428 name: foo
429 annotations:
430 config.kubernetes.io/function: |
431 container:
432 image: "gcr.io/kustomize-functions/e2econtainerconfig"
433 a-string-value: ''
434 a-int-value: '0'
435 a-bool-value: 'false'
436 `}
437 },
438 },
439
440 {
441 name: "container_function_config",
442 skipIfFalseEnv: "KUSTOMIZE_DOCKER_E2E",
443 args: func(d string) []string { return []string{} },
444 files: func(d string) map[string]string {
445 return map[string]string{
446 "config.yaml": `
447 apiVersion: example.com/v1alpha1
448 kind: Input
449 metadata:
450 name: foo
451 annotations:
452 config.kubernetes.io/function: |
453 container:
454 image: "gcr.io/kustomize-functions/e2econtainerconfig"
455 data:
456 stringValue: a
457 intValue: 2
458 boolValue: true
459 `,
460 "deployment.yaml": `
461 apiVersion: apps/v1
462 kind: Deployment
463 metadata:
464 name: foo
465 `,
466 }
467 },
468 expectedFiles: func(d string) map[string]string {
469 return map[string]string{
470 "config.yaml": `
471 apiVersion: example.com/v1alpha1
472 kind: Input
473 metadata:
474 name: foo
475 annotations:
476 config.kubernetes.io/function: |
477 container:
478 image: "gcr.io/kustomize-functions/e2econtainerconfig"
479 a-string-value: 'a'
480 a-int-value: '2'
481 a-bool-value: 'true'
482 data:
483 stringValue: a
484 intValue: 2
485 boolValue: true
486 `,
487 "deployment.yaml": `
488 apiVersion: apps/v1
489 kind: Deployment
490 metadata:
491 name: foo
492 annotations:
493 a-string-value: 'a'
494 a-int-value: '2'
495 a-bool-value: 'true'
496 `,
497 }
498 },
499 },
500 }
501
502 // TODO: dedup this with the shared version
503 for i := range tests {
504 tt := tests[i]
505 t.Run(tt.name, func(t *testing.T) {
506 if tt.skipIfFalseEnv != "" && os.Getenv(tt.skipIfFalseEnv) == "false" {
507 t.Skip()
508 }
509
510 dir, err := os.MkdirTemp("", "kustomize-test-data-")
511 if !assert.NoError(t, err) {
512 t.FailNow()
513 }
514 defer os.RemoveAll(dir)
515 os.Chdir(dir)
516
517 // write the input
518 for path, data := range tt.files(binDir) {
519 err := os.WriteFile(path, []byte(data), 0600)
520 testutil.AssertNoError(t, err)
521 }
522
523 args := append([]string{"fn", "run", "."}, tt.args(binDir)...)
524 cmd := exec.Command(filepath.Join(binDir, kyamlBin), args...) //nolint: gosec
525 cmd.Dir = dir
526 var stdErr, stdOut bytes.Buffer
527 cmd.Stdout = &stdOut
528 cmd.Stderr = &stdErr
529 cmd.Env = os.Environ()
530
531 err = cmd.Run()
532 if tt.expectedErr != "" {
533 if !assert.Contains(t, stdErr.String(), tt.expectedErr, stdErr.String()) {
534 t.FailNow()
535 }
536 return
537 }
538 testutil.AssertNoError(t, err, stdErr.String())
539
540 for path, data := range tt.expectedFiles(binDir) {
541 b, err := os.ReadFile(path)
542 testutil.AssertNoError(t, err, stdErr.String())
543
544 if !assert.Equal(t, strings.TrimSpace(data), strings.TrimSpace(string(b)), stdErr.String()) {
545 t.FailNow()
546 }
547 }
548 })
549 }
550 }
551
552 var buildOnce sync.Once
553 var binDir string
554
555 const e2eConfigDir = "e2econtainerconfig"
556
557 func build() string {
558 // only build the binaries once
559 buildOnce.Do(func() {
560 var err error
561 binDir, err = os.MkdirTemp("", "kustomize-test-")
562 if err != nil {
563 panic(err)
564 }
565
566 build := exec.Command("go", "build", "-o", //nolint: gosec
567 filepath.Join(binDir, e2econtainerconfigBin))
568 build.Dir = "e2econtainerconfig"
569 build.Stdout = os.Stdout
570 build.Stderr = os.Stderr
571 build.Env = os.Environ()
572
573 err = build.Run()
574 if err != nil {
575 panic(err)
576 }
577
578 build = exec.Command("go", "build", "-o", filepath.Join(binDir, kyamlBin)) //nolint: gosec
579 build.Dir = filepath.Join("..", "..", "..", "kubectl-krm")
580 build.Stdout = os.Stdout
581 build.Stderr = os.Stderr
582 err = build.Run()
583 if err != nil {
584 panic(err)
585 }
586
587 if os.Getenv("KUSTOMIZE_DOCKER_E2E") == "false" {
588 return
589 }
590 build = exec.Command("docker", "build", ".",
591 "-f", "./cmd/config/internal/commands/e2e/e2econtainerconfig/Dockerfile",
592 "-t", "gcr.io/kustomize-functions/e2econtainerconfig",
593 )
594 build.Dir = "../../../../../" // Repo root
595 build.Stdout = os.Stdout
596 build.Stderr = os.Stderr
597 err = build.Run()
598 if err != nil {
599 panic(err)
600 }
601 })
602
603 return binDir
604 }
605
606 var (
607 e2econtainerconfigBin string
608 kyamlBin string
609 )
610
611 func init() { //nolint: gochecknoinits
612 kyamlBin = "kubectl-krm"
613 e2econtainerconfigBin = e2eConfigDir
614
615 if runtime.GOOS == "windows" {
616 kyamlBin = "kubectl-krm.exe"
617 e2econtainerconfigBin = "e2econtainerconfig.exe"
618 }
619 }