Debian Codegraph @

cmd/config/internal/commands/tree_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package commands_test
5
6 import (
7 "bytes"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "sigs.k8s.io/kustomize/cmd/config/internal/commands"
15 )
16
17 func TestTreeCommandDefaultCurDir_files(t *testing.T) {
18 d := t.TempDir()
19 cwd, err := os.Getwd()
20 if !assert.NoError(t, err) {
21 t.FailNow()
22 }
23
24 if !assert.NoError(t, os.Chdir(d)) {
25 return
26 }
27
28 t.Cleanup(func() {
29 if !assert.NoError(t, os.Chdir(cwd)) {
30 t.FailNow()
31 }
32 })
33
34 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
35 apiVersion: v1
36 kind: Abstraction
37 metadata:
38 name: foo
39 configFn:
40 container:
41 image: gcr.io/example/reconciler:v1
42 annotations:
43 config.kubernetes.io/local-config: "true"
44 spec:
45 replicas: 1
46 ---
47 kind: Deployment
48 metadata:
49 labels:
50 app: nginx2
51 name: foo
52 annotations:
53 app: nginx2
54 spec:
55 replicas: 1
56 ---
57 kind: Service
58 metadata:
59 name: foo
60 annotations:
61 app: nginx
62 spec:
63 selector:
64 app: nginx
65 `), 0600)
66 if !assert.NoError(t, err) {
67 return
68 }
69 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment
70 metadata:
71 labels:
72 app: nginx
73 name: bar
74 annotations:
75 app: nginx
76 spec:
77 replicas: 3
78 `), 0600)
79 if !assert.NoError(t, err) {
80 return
81 }
82
83 // fmt the files
84 b := &bytes.Buffer{}
85 r := commands.GetTreeRunner("")
86 r.Command.SetArgs([]string{})
87 r.Command.SetOut(b)
88 if !assert.NoError(t, r.Command.Execute()) {
89 return
90 }
91
92 if !assert.Equal(t, `.
93 ├── [f1.yaml] Deployment foo
94 ├── [f1.yaml] Service foo
95 └── [f2.yaml] Deployment bar
96 `, b.String()) {
97 return
98 }
99 }
100
101 // TestCmd_files verifies fmt reads the files and filters them
102 func TestTreeCommand_files(t *testing.T) {
103 d := t.TempDir()
104
105 err := os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
106 apiVersion: v1
107 kind: Abstraction
108 metadata:
109 name: foo
110 configFn:
111 container:
112 image: gcr.io/example/reconciler:v1
113 annotations:
114 config.kubernetes.io/local-config: "true"
115 spec:
116 replicas: 1
117 ---
118 kind: Deployment
119 metadata:
120 labels:
121 app: nginx2
122 name: foo
123 annotations:
124 app: nginx2
125 spec:
126 replicas: 1
127 ---
128 kind: Service
129 metadata:
130 name: foo
131 annotations:
132 app: nginx
133 spec:
134 selector:
135 app: nginx
136 `), 0600)
137 if !assert.NoError(t, err) {
138 return
139 }
140 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment
141 metadata:
142 labels:
143 app: nginx
144 name: bar
145 annotations:
146 app: nginx
147 spec:
148 replicas: 3
149 `), 0600)
150 if !assert.NoError(t, err) {
151 return
152 }
153
154 // fmt the files
155 b := &bytes.Buffer{}
156 r := commands.GetTreeRunner("")
157 r.Command.SetArgs([]string{d})
158 r.Command.SetOut(b)
159 if !assert.NoError(t, r.Command.Execute()) {
160 return
161 }
162
163 if !assert.Equal(t, fmt.Sprintf(`%s
164 ├── [f1.yaml] Deployment foo
165 ├── [f1.yaml] Service foo
166 └── [f2.yaml] Deployment bar
167 `, d), b.String()) {
168 return
169 }
170 }
171
172 func TestTreeCommand_Kustomization(t *testing.T) {
173 d := t.TempDir()
174
175 err := os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment
176 metadata:
177 labels:
178 app: nginx
179 name: bar
180 annotations:
181 app: nginx
182 spec:
183 replicas: 3
184 `), 0600)
185 if !assert.NoError(t, err) {
186 return
187 }
188
189 err = os.WriteFile(filepath.Join(d, "Kustomization"), []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
190 kind: Kustomization
191 resources:
192 - f2.yaml
193 `), 0600)
194 if !assert.NoError(t, err) {
195 return
196 }
197
198 // fmt the files
199 b := &bytes.Buffer{}
200 r := commands.GetTreeRunner("")
201 r.Command.SetArgs([]string{d})
202 r.Command.SetOut(b)
203 if !assert.NoError(t, r.Command.Execute()) {
204 return
205 }
206
207 if !assert.Equal(t, fmt.Sprintf(`%s
208 └── [f2.yaml] Deployment bar
209 `, d), b.String()) {
210 return
211 }
212 }
213
214 func TestTreeCommand_subpkgs(t *testing.T) {
215 d := t.TempDir()
216
217 err := os.MkdirAll(filepath.Join(d, "subpkg"), 0700)
218 if !assert.NoError(t, err) {
219 t.FailNow()
220 }
221
222 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
223 apiVersion: v1
224 kind: Abstraction
225 metadata:
226 name: foo
227 configFn:
228 container:
229 image: gcr.io/example/reconciler:v1
230 annotations:
231 config.kubernetes.io/local-config: "true"
232 spec:
233 replicas: 1
234 ---
235 kind: Deployment
236 metadata:
237 labels:
238 app: nginx2
239 name: foo
240 annotations:
241 app: nginx2
242 spec:
243 replicas: 1
244 ---
245 kind: Service
246 metadata:
247 name: foo
248 annotations:
249 app: nginx
250 spec:
251 selector:
252 app: nginx
253 `), 0600)
254 if !assert.NoError(t, err) {
255 return
256 }
257 err = os.WriteFile(filepath.Join(d, "subpkg", "f2.yaml"), []byte(`kind: Deployment
258 metadata:
259 labels:
260 app: nginx
261 name: bar
262 annotations:
263 app: nginx
264 spec:
265 replicas: 3
266 `), 0600)
267 if !assert.NoError(t, err) {
268 return
269 }
270
271 err = os.WriteFile(filepath.Join(d, "Krmfile"), []byte(`apiVersion: kpt.dev/v1alpha1
272 kind: Krmfile
273 metadata:
274 name: mainpkg
275 openAPI:
276 definitions:
277 `), 0600)
278 if !assert.NoError(t, err) {
279 return
280 }
281 err = os.WriteFile(filepath.Join(d, "subpkg", "Krmfile"), []byte(`apiVersion: kpt.dev/v1alpha1
282 kind: Krmfile
283 metadata:
284 name: subpkg
285 openAPI:
286 definitions:
287 `), 0600)
288 if !assert.NoError(t, err) {
289 return
290 }
291
292 // fmt the files
293 b := &bytes.Buffer{}
294 r := commands.GetTreeRunner("")
295 r.Command.SetArgs([]string{d})
296 r.Command.SetOut(b)
297 if !assert.NoError(t, r.Command.Execute()) {
298 return
299 }
300
301 if !assert.Equal(t, fmt.Sprintf(`%s
302 ├── [f1.yaml] Deployment foo
303 ├── [f1.yaml] Service foo
304 └── Pkg: subpkg
305 └── [f2.yaml] Deployment bar
306 `, d), b.String()) {
307 return
308 }
309 }
310
311 func TestTreeCommand_stdin(t *testing.T) {
312 // fmt the files
313 b := &bytes.Buffer{}
314 r := commands.GetTreeRunner("")
315 r.Command.SetArgs([]string{"-"})
316 r.Command.SetIn(bytes.NewBufferString(`apiVersion: extensions/v1
317 kind: Deployment
318 metadata:
319 labels:
320 app: nginx2
321 name: foo3
322 namespace: default
323 annotations:
324 app: nginx2
325 config.kubernetes.io/path: f1.yaml
326 spec:
327 replicas: 1
328 ---
329 apiVersion: extensions/v1
330 kind: Deployment
331 metadata:
332 labels:
333 app: nginx2
334 name: foo3
335 namespace: default
336 annotations:
337 app: nginx2
338 config.kubernetes.io/path: f1.yaml
339 spec:
340 replicas: 1
341 ---
342 apiVersion: apps/v1
343 kind: Deployment
344 metadata:
345 labels:
346 app: nginx2
347 name: foo3
348 namespace: default
349 annotations:
350 app: nginx2
351 config.kubernetes.io/path: f1.yaml
352 spec:
353 replicas: 1
354 ---
355 apiVersion: apps/v1
356 kind: Deployment
357 metadata:
358 labels:
359 app: nginx2
360 name: foo2
361 namespace: default2
362 annotations:
363 app: nginx2
364 config.kubernetes.io/path: f1.yaml
365 spec:
366 replicas: 1
367 ---
368 apiVersion: apps/v1
369 kind: Deployment
370 metadata:
371 labels:
372 app: nginx3
373 name: foo
374 namespace: default
375 annotations:
376 app: nginx3
377 config.kubernetes.io/path: f1.yaml
378 spec:
379 replicas: 1
380 ---
381 kind: Deployment
382 metadata:
383 labels:
384 app: nginx
385 annotations:
386 app: nginx
387 config.kubernetes.io/path: bar-package/f2.yaml
388 name: bar
389 spec:
390 replicas: 3
391 ---
392 kind: Service
393 metadata:
394 name: foo
395 namespace: default
396 annotations:
397 app: nginx
398 config.kubernetes.io/path: f1.yaml
399 spec:
400 selector:
401 app: nginx
402 `))
403 r.Command.SetOut(b)
404 if !assert.NoError(t, r.Command.Execute()) {
405 return
406 }
407
408 if !assert.Equal(t, `.
409 ├── [f1.yaml] Deployment default/foo
410 ├── [f1.yaml] Service default/foo
411 ├── [f1.yaml] Deployment default/foo3
412 ├── [f1.yaml] Deployment default/foo3
413 ├── [f1.yaml] Deployment default/foo3
414 ├── [f1.yaml] Deployment default2/foo2
415 └── bar-package
416 └── [f2.yaml] Deployment bar
417 `, b.String()) {
418 return
419 }
420 }
421
422 func TestTreeCommand_includeReconcilers(t *testing.T) {
423 d := t.TempDir()
424
425 err := os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
426 kind: Deployment
427 metadata:
428 labels:
429 app: nginx2
430 name: foo
431 annotations:
432 app: nginx2
433 spec:
434 replicas: 1
435 ---
436 kind: Service
437 metadata:
438 name: foo
439 annotations:
440 app: nginx
441 spec:
442 selector:
443 app: nginx
444 `), 0600)
445 if !assert.NoError(t, err) {
446 return
447 }
448 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`
449 apiVersion: gcr.io/example/reconciler:v1
450 kind: Abstraction
451 metadata:
452 name: foo
453 spec:
454 replicas: 1
455 ---
456 apiVersion: apps/v1
457 kind: Deployment
458 metadata:
459 labels:
460 app: nginx
461 name: bar
462 annotations:
463 app: nginx
464 spec:
465 replicas: 3
466 `), 0600)
467 if !assert.NoError(t, err) {
468 return
469 }
470
471 // fmt the files
472 b := &bytes.Buffer{}
473 r := commands.GetTreeRunner("")
474 r.Command.SetArgs([]string{d, "--include-local"})
475 r.Command.SetOut(b)
476 if !assert.NoError(t, r.Command.Execute()) {
477 return
478 }
479
480 if !assert.Equal(t, fmt.Sprintf(`%s
481 ├── [f1.yaml] Deployment foo
482 ├── [f1.yaml] Service foo
483 ├── [f2.yaml] Deployment bar
484 └── [f2.yaml] Abstraction foo
485 `, d), b.String()) {
486 return
487 }
488 }
489
490 func TestTreeCommand_excludeNonReconcilers(t *testing.T) {
491 d := t.TempDir()
492
493 err := os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
494 kind: Deployment
495 metadata:
496 labels:
497 app: nginx2
498 name: foo
499 annotations:
500 app: nginx2
501 spec:
502 replicas: 1
503 ---
504 kind: Service
505 metadata:
506 name: foo
507 annotations:
508 app: nginx
509 spec:
510 selector:
511 app: nginx
512 `), 0600)
513 if !assert.NoError(t, err) {
514 return
515 }
516 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`
517 apiVersion: v1
518 kind: Abstraction
519 metadata:
520 name: foo
521 configFn:
522 container:
523 image: gcr.io/example/reconciler:v1
524 annotations:
525 config.kubernetes.io/local-config: "true"
526 spec:
527 replicas: 1
528 ---
529 apiVersion: apps/v1
530 kind: Deployment
531 metadata:
532 labels:
533 app: nginx
534 name: bar
535 annotations:
536 app: nginx
537 spec:
538 replicas: 3
539 `), 0600)
540 if !assert.NoError(t, err) {
541 return
542 }
543
544 // fmt the files
545 b := &bytes.Buffer{}
546 r := commands.GetTreeRunner("")
547 r.Command.SetArgs([]string{d, "--include-local", "--exclude-non-local"})
548 r.Command.SetOut(b)
549 if !assert.NoError(t, r.Command.Execute()) {
550 return
551 }
552
553 if !assert.Equal(t, fmt.Sprintf(`%s
554 └── [f2.yaml] Abstraction foo
555 `, d), b.String()) {
556 return
557 }
558 }
559
560 // TestTreeCommand_images tests that the image flag returns the image specified
561 // by various workloads.
562 func TestTreeCommand_images(t *testing.T) {
563 d := t.TempDir()
564 cwd, err := os.Getwd()
565 if !assert.NoError(t, err) {
566 t.FailNow()
567 }
568
569 if !assert.NoError(t, os.Chdir(d)) {
570 return
571 }
572
573 t.Cleanup(func() {
574 if !assert.NoError(t, os.Chdir(cwd)) {
575 t.FailNow()
576 }
577 })
578
579 err = os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
580 kind: Deployment
581 metadata:
582 name: deployment
583 spec:
584 template:
585 spec:
586 containers:
587 - name: test-deployment
588 image: docker.io/bash:alpine3.18
589 ---
590 kind: StatefulSet
591 metadata:
592 name: statefulset
593 spec:
594 template:
595 spec:
596 containers:
597 - name: test-statefulset
598 image: gcr.io/distroless/static-debian12:nonroot
599 ---
600 kind: Job
601 metadata:
602 name: job
603 spec:
604 template:
605 spec:
606 containers:
607 - name: job
608 image: tagless
609 ---
610 kind: CronJob
611 metadata:
612 name: cronjob
613 spec:
614 jobTemplate:
615 spec:
616 template:
617 spec:
618 containers:
619 - name: test-cronjob
620 image: local-image:v1.0.0
621 ---
622 kind: Service
623 metadata:
624 name: service
625 spec:
626 selector:
627 app: nginx
628 `), 0600)
629 if !assert.NoError(t, err) {
630 return
631 }
632
633 // fmt the files
634 b := &bytes.Buffer{}
635 r := commands.GetTreeRunner("")
636 r.Command.SetArgs([]string{"--image"})
637 r.Command.SetOut(b)
638 if !assert.NoError(t, r.Command.Execute()) {
639 return
640 }
641
642 if !assert.Equal(t, `.
643 ├── [f1.yaml] CronJob cronjob
644 │ └── spec.jobTemplate.spec.template.spec.containers
645 │ └── 0
646 │ └── image: local-image:v1.0.0
647 ├── [f1.yaml] Deployment deployment
648 │ └── spec.template.spec.containers
649 │ └── 0
650 │ └── image: docker.io/bash:alpine3.18
651 ├── [f1.yaml] Job job
652 │ └── spec.template.spec.containers
653 │ └── 0
654 │ └── image: tagless
655 ├── [f1.yaml] Service service
656 └── [f1.yaml] StatefulSet statefulset
657 └── spec.template.spec.containers
658 └── 0
659 └── image: gcr.io/distroless/static-debian12:nonroot
660 `, b.String()) {
661 return
662 }
663 }