Debian Codegraph @

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