Debian Codegraph @

api/internal/localizer/localizer_test.go

raw
1 // Copyright 2022 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package localizer_test
5
6 import (
7 "fmt"
8 "io/fs"
9 "path/filepath"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 . "sigs.k8s.io/kustomize/api/internal/localizer"
15 "sigs.k8s.io/kustomize/kyaml/filesys"
16 )
17
18 const (
19 podConfiguration = `apiVersion: v1
20 kind: Pod
21 metadata:
22 name: pod
23 spec:
24 containers:
25 - name: nginx
26 image: nginx:1.14.2
27 ports:
28 - containerPort: 80`
29
30 replacementTransformerWithPath = `apiVersion: builtin
31 kind: ReplacementTransformer
32 metadata:
33 name: replacement
34 replacements:
35 - path: replacement.yaml
36 - source:
37 fieldPath: metadata.[name=my-pod]
38 group: apps
39 namespace: test
40 version: v1
41 targets:
42 - fieldPaths:
43 - spec.containers.0.name
44 select:
45 name: another-pod
46 `
47
48 replacements = `
49 - source:
50 name: src
51 fieldPath: path
52 options:
53 delimiter: '='
54 index: 1
55 targets:
56 - select:
57 kind: Pod
58 reject:
59 version: v1
60 fieldPaths:
61 - metadata.annotations.config\.kubernetes\.io/local-config
62 - sequence.*
63 - source:
64 kind: Deployment
65 fieldPath: sequence.-
66 targets:
67 - select:
68 namespace: my
69 fieldPaths:
70 - path
71 options:
72 delimiter: '='
73 index: 0
74 `
75
76 valuesFile = `minecraftServer:
77 difficulty: peaceful
78 `
79 )
80
81 func makeMemoryFs(t *testing.T) filesys.FileSystem {
82 t.Helper()
83 req := require.New(t)
84
85 fSys := filesys.MakeFsInMemory()
86 req.NoError(fSys.MkdirAll("/a/b"))
87 req.NoError(fSys.WriteFile("/a/pod.yaml", []byte(podConfiguration)))
88
89 dirChain := "/alpha/beta/gamma/delta"
90 req.NoError(fSys.MkdirAll(dirChain))
91 req.NoError(fSys.WriteFile(filepath.Join(dirChain, "deployment.yaml"), []byte("deployment configuration")))
92 req.NoError(fSys.Mkdir("/alpha/beta/say"))
93 return fSys
94 }
95
96 func addFiles(t *testing.T, fSys filesys.FileSystem, parentDir string, files map[string]string) {
97 t.Helper()
98
99 // in-memory file system makes all necessary dirs when writing files
100 for file, content := range files {
101 require.NoError(t, fSys.WriteFile(filepath.Join(parentDir, file), []byte(content)))
102 }
103 }
104
105 func checkRun(t *testing.T, fSys filesys.FileSystem, target, scope, dst string) {
106 t.Helper()
107
108 actualDst, err := Run(target, scope, dst, fSys)
109 require.NoError(t, err)
110 require.Equal(t, dst, actualDst)
111 }
112
113 func makeFileSystems(t *testing.T, target string, files map[string]string) (expected filesys.FileSystem, actual filesys.FileSystem) {
114 t.Helper()
115
116 copies := make([]filesys.FileSystem, 2)
117 for i := range copies {
118 copies[i] = makeMemoryFs(t)
119 addFiles(t, copies[i], target, files)
120 }
121 return copies[0], copies[1]
122 }
123
124 func checkFSys(t *testing.T, fSysExpected filesys.FileSystem, fSysActual filesys.FileSystem) {
125 t.Helper()
126
127 assert.Equal(t, fSysExpected, fSysActual)
128 if t.Failed() {
129 reportFSysDiff(t, fSysExpected, fSysActual)
130 }
131 }
132
133 func reportFSysDiff(t *testing.T, fSysExpected filesys.FileSystem, fSysActual filesys.FileSystem) {
134 t.Helper()
135
136 visited := make(map[string]struct{})
137 err := fSysActual.Walk("/", func(path string, info fs.FileInfo, err error) error {
138 require.NoError(t, err)
139 visited[path] = struct{}{}
140
141 if info.IsDir() {
142 assert.Truef(t, fSysExpected.IsDir(path), "unexpected directory %q", path)
143 } else {
144 actualContent, readErr := fSysActual.ReadFile(path)
145 require.NoError(t, readErr)
146 expectedContent, findErr := fSysExpected.ReadFile(path)
147 require.NoErrorf(t, findErr, "unexpected file %q", path)
148 if findErr == nil {
149 assert.Equal(t, string(expectedContent), string(actualContent))
150 }
151 }
152 return nil
153 })
154 require.NoError(t, err)
155
156 err = fSysExpected.Walk("/", func(path string, info fs.FileInfo, err error) error {
157 require.NoError(t, err)
158
159 _, exists := visited[path]
160 assert.Truef(t, exists, "expected path %q not found", path)
161 return nil
162 })
163 require.NoError(t, err)
164 }
165
166 func checkLocalizeInTargetSuccess(t *testing.T, files map[string]string) {
167 t.Helper()
168
169 fSys := makeMemoryFs(t)
170 addFiles(t, fSys, "/a", files)
171
172 checkRun(t, fSys, "/a", "/", "/dst")
173 fSysExpected := makeMemoryFs(t)
174 addFiles(t, fSysExpected, "/a", files)
175 addFiles(t, fSysExpected, "/dst/a", files)
176 checkFSys(t, fSysExpected, fSys)
177 }
178
179 func TestTargetIsScope(t *testing.T) {
180 kustomization := map[string]string{
181 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
182 kind: Kustomization
183 namePrefix: my-
184 `,
185 }
186 fSysExpected, fSysActual := makeFileSystems(t, "/a", kustomization)
187
188 checkRun(t, fSysActual, "/a", "/a", "/a/b/dst")
189 addFiles(t, fSysExpected, "/a/b/dst", kustomization)
190 checkFSys(t, fSysExpected, fSysActual)
191 }
192
193 func TestTargetNestedInScope(t *testing.T) {
194 kustomization := map[string]string{
195 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
196 kind: Kustomization
197 patches:
198 - patch: |-
199 - op: replace
200 path: /some/existing/path
201 value: new value
202 target:
203 kind: Deployment
204 labelSelector: env=dev
205 `,
206 }
207 fSysExpected, fSysActual := makeFileSystems(t, "/a/b", kustomization)
208
209 checkRun(t, fSysActual, "/a/b", "/", "/a/b/dst")
210 addFiles(t, fSysExpected, "/a/b/dst/a/b", kustomization)
211 checkFSys(t, fSysExpected, fSysActual)
212 }
213
214 func TestLoadKustomizationName(t *testing.T) {
215 kustomization := map[string]string{
216 "Kustomization": `apiVersion: kustomize.config.k8s.io/v1beta1
217 kind: Kustomization
218 labels:
219 - pairs:
220 label-one: value-one
221 label-two: value-two
222 `,
223 }
224 checkLocalizeInTargetSuccess(t, kustomization)
225 }
226
227 func TestLoadGVKNN(t *testing.T) {
228 for name, kustomization := range map[string]string{
229 "missing": `namePrefix: my-
230 `,
231 "wrong": `kind: NotChecked
232 `,
233 } {
234 t.Run(name, func(t *testing.T) {
235 files := map[string]string{
236 "kustomization.yaml": kustomization,
237 }
238 checkLocalizeInTargetSuccess(t, files)
239 })
240 }
241 }
242
243 func TestLoadLegacyFields(t *testing.T) {
244 kustomization := map[string]string{
245 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
246 commonLabels:
247 app: bingo
248 imageTags:
249 - name: postgres
250 newName: my-registry/my-postgres
251 newTag: v1
252 kind: Kustomization
253 `,
254 }
255 checkLocalizeInTargetSuccess(t, kustomization)
256 }
257
258 func TestLoadUnknownKustFields(t *testing.T) {
259 fSysExpected, fSysTest := makeFileSystems(t, "/a", map[string]string{
260 "kustomization.yaml": `namePrefix: valid
261 suffix: invalid`,
262 })
263
264 _, err := Run("/a", "", "", fSysTest)
265 require.EqualError(t, err,
266 `unable to localize target "/a": invalid Kustomization: json: unknown field "suffix"`)
267
268 checkFSys(t, fSysExpected, fSysTest)
269 }
270
271 func TestLocalizeFileName(t *testing.T) {
272 for name, path := range map[string]string{
273 "nested_directories": "a/b/c/d/patch.yaml",
274 "localize_dir_name_when_no_remote": LocalizeDir,
275 "in_localize_dir_name_when_no_remote": fmt.Sprintf("%s/patch.yaml", LocalizeDir),
276 "no_file_extension": "patch",
277 "kustomization_name": "a/kustomization.yaml",
278 } {
279 t.Run(name, func(t *testing.T) {
280 kustAndPatch := map[string]string{
281 "kustomization.yaml": fmt.Sprintf(`apiVersion: kustomize.config.k8s.io/v1beta1
282 kind: Kustomization
283 patches:
284 - path: %s
285 `, path),
286 path: podConfiguration,
287 }
288 checkLocalizeInTargetSuccess(t, kustAndPatch)
289 })
290 }
291 }
292
293 func TestLocalizeFileCleaned(t *testing.T) {
294 kustAndPatch := map[string]string{
295 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
296 kind: Kustomization
297 patches:
298 - path: ../gamma/../../../alpha/beta/./gamma/patch.yaml
299 - path: /alpha/beta/../beta/./gamma/patch2.yaml
300 `,
301 "patch.yaml": podConfiguration,
302 "patch2.yaml": podConfiguration,
303 }
304 expected, actual := makeFileSystems(t, "/alpha/beta/gamma", kustAndPatch)
305
306 checkRun(t, actual, "/alpha/beta/gamma", "/", "/localized-gamma")
307 addFiles(t, expected, "/localized-gamma/alpha/beta/gamma", map[string]string{
308 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
309 kind: Kustomization
310 patches:
311 - path: patch.yaml
312 - path: patch2.yaml
313 `,
314 "patch.yaml": podConfiguration,
315 "patch2.yaml": podConfiguration,
316 })
317 checkFSys(t, expected, actual)
318 }
319
320 func TestLocalizeUnreferencedIgnored(t *testing.T) {
321 targetAndUnreferenced := map[string]string{
322 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
323 configMapGenerator:
324 - envs:
325 - env
326 name: referenced-file
327 kind: Kustomization
328 `,
329 "env": "APPLE=orange",
330 "env.properties": "USERNAME=password",
331 "dir/resource.yaml": podConfiguration,
332 }
333 expected, actual := makeFileSystems(t, "/alpha/beta", targetAndUnreferenced)
334
335 checkRun(t, actual, "/alpha/beta", "/alpha", "/beta")
336 addFiles(t, expected, "/beta/beta", map[string]string{
337 "kustomization.yaml": targetAndUnreferenced["kustomization.yaml"],
338 "env": targetAndUnreferenced["env"],
339 })
340 checkFSys(t, expected, actual)
341 }
342
343 func TestLocalizePatches(t *testing.T) {
344 kustAndPatch := map[string]string{
345 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
346 kind: Kustomization
347 patches:
348 - patch: |-
349 apiVersion: v1
350 kind: Deployment
351 metadata:
352 labels:
353 app.kubernetes.io/version: 1.21.0
354 name: dummy-app
355 target:
356 labelSelector: app.kubernetes.io/name=nginx
357 - options:
358 allowNameChange: true
359 path: patch.yaml
360 `,
361 "patch.yaml": podConfiguration,
362 }
363 checkLocalizeInTargetSuccess(t, kustAndPatch)
364 }
365
366 func TestLocalizeOpenAPI(t *testing.T) {
367 type testCase struct {
368 name string
369 files map[string]string
370 }
371 for _, test := range []testCase{
372 {
373 name: "no_path",
374 files: map[string]string{
375 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
376 kind: Kustomization
377 openapi:
378 version: v1.20.4
379 `,
380 },
381 },
382 {
383 name: "path",
384 files: map[string]string{
385 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
386 kind: Kustomization
387 openapi:
388 path: openapi.json
389 `,
390 "openapi.json": `{
391 "definitions": {
392 "io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta": {
393 "properties": {
394 "name": {
395 "type": "string"
396 }
397 },
398 "type": "object"
399 }
400 }
401 }`,
402 },
403 },
404 } {
405 t.Run(test.name, func(t *testing.T) {
406 checkLocalizeInTargetSuccess(t, test.files)
407 })
408 }
409 }
410
411 func TestLocalizeConfigurations(t *testing.T) {
412 kustAndConfigs := map[string]string{
413 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
414 configurations:
415 - commonLabels.yaml
416 - namePrefix.yaml
417 kind: Kustomization
418 `,
419 "commonLabels.yaml": `commonLabels:
420 - path: new/path
421 create: true`,
422 "namePrefix.yaml": `namePrefix:
423 - version: v1
424 path: metadata/name
425 - group: custom
426 path: metadata/name`,
427 }
428 checkLocalizeInTargetSuccess(t, kustAndConfigs)
429 }
430
431 func TestLocalizeCrds(t *testing.T) {
432 kustAndCrds := map[string]string{
433 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
434 crds:
435 - crd1.yaml
436 - crd2.yaml
437 kind: Kustomization
438 `,
439 "crd1.yaml": `apiVersion: apiextensions.k8s.io/v1
440 kind: CustomResourceDefinition
441 metadata:
442 name: controller.stable.example.com`,
443 "crd2.yaml": `apiVersion: apiextensions.k8s.io/v1
444 kind: CustomResourceDefinition
445 metadata:
446 name: crontabs.stable.example.com
447 scope: Cluster`,
448 }
449 checkLocalizeInTargetSuccess(t, kustAndCrds)
450 }
451
452 func TestLocalizePatchesJson(t *testing.T) {
453 kustAndPatches := map[string]string{
454 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
455 kind: Kustomization
456 patchesJson6902:
457 - path: patch.yaml
458 target:
459 annotationSelector: zone=west
460 name: pod
461 version: v1
462 - patch: '[{"op": "add", "path": "/new/path", "value": "value"}]'
463 target:
464 group: apps
465 kind: Pod
466 - path: patch.json
467 target:
468 namespace: my
469 `,
470 "patch.yaml": `- op: add
471 path: /some/new/path
472 value: value
473 - op: replace
474 path: /some/existing/path
475 value: new value`,
476 "patch.json": ` [
477 {"op": "copy", "from": "/here", "path": "/there"},
478 {"op": "remove", "path": "/some/existing/path"},
479 ]`,
480 }
481 checkLocalizeInTargetSuccess(t, kustAndPatches)
482 }
483
484 func TestLocalizePatchesSM(t *testing.T) {
485 kustAndPatches := map[string]string{
486 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
487 kind: Kustomization
488 patchesStrategicMerge:
489 - |-
490 apiVersion: v1
491 kind: ConfigMap
492 metadata:
493 name: map
494 data:
495 - APPLE: orange
496 - patch.yaml
497 `,
498 "patch.yaml": podConfiguration,
499 }
500 checkLocalizeInTargetSuccess(t, kustAndPatches)
501 }
502
503 func TestLocalizeReplacements(t *testing.T) {
504 kustAndReplacement := map[string]string{
505 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
506 kind: Kustomization
507 replacements:
508 - path: replacement.yaml
509 - source:
510 fieldPath: path.0
511 name: map
512 targets:
513 - fieldPaths:
514 - path
515 select:
516 name: my-map
517 `,
518 "replacement.yaml": replacements,
519 }
520 checkLocalizeInTargetSuccess(t, kustAndReplacement)
521 }
522
523 func TestLocalizeConfigMapGenerator(t *testing.T) {
524 kustAndData := map[string]string{
525 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
526 configMapGenerator:
527 - env: single.env
528 envs:
529 - standard.env
530 namespace: my
531 options:
532 immutable: true
533 - behavior: merge
534 files:
535 - key.properties
536 literals:
537 - PEAR=pineapple
538 kind: Kustomization
539 metadata:
540 name: test
541 `,
542 "single.env": `MAY=contain
543 MORE=than
544 ONE=pair`,
545 "standard.env": `SIZE=0.1
546 IS_GLOBAL=true`,
547 "key.properties": "value",
548 }
549 checkLocalizeInTargetSuccess(t, kustAndData)
550 }
551
552 func TestLocalizeSecretGenerator(t *testing.T) {
553 kustAndData := map[string]string{
554 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
555 kind: Kustomization
556 secretGenerator:
557 - behavior: create
558 files:
559 - key=b/value.properties
560 - b/value
561 name: secret
562 - envs:
563 - crt
564 - key
565 type: kubernetes.io/tls
566 - literals:
567 - APPLE=orange
568 - PLUM=pluot
569 name: no-files
570 - env: more-fruit
571 `,
572 "crt": "tls.crt=LS0tLS1CRUd...0tLQo=",
573 "key": "tls.key=LS0tLS1CRUd...0tLQo=",
574 "more-fruit": "GRAPE=lime",
575 "b/value.properties": "value",
576 "b/value": "value",
577 }
578 checkLocalizeInTargetSuccess(t, kustAndData)
579 }
580
581 func TestLocalizeFileNoFile(t *testing.T) {
582 kustAndPatch := map[string]string{
583 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
584 kind: Kustomization
585 patches:
586 - path: name-DNE.yaml
587 `,
588 }
589 expected, actual := makeFileSystems(t, "/a/b", kustAndPatch)
590
591 _, err := Run("/a/b", "", "/dst", actual)
592 require.EqualError(t, err, `unable to localize target "/a/b": unable to localize patches: invalid file reference: '/a/b/name-DNE.yaml' doesn't exist`)
593
594 checkFSys(t, expected, actual)
595 }
596
597 func TestLocalizePluginsInlineAndFile(t *testing.T) {
598 for _, test := range []struct {
599 name string
600 files map[string]string
601 }{
602 {
603 name: "generators",
604 files: map[string]string{
605 "kustomization.yaml": `generators:
606 - generator.yaml
607 - |
608 apiVersion: builtin
609 env: second.properties
610 kind: ConfigMapGenerator
611 metadata:
612 name: inline
613 `,
614 "generator.yaml": `apiVersion: builtin
615 env: first.properties
616 kind: ConfigMapGenerator
617 metadata:
618 name: file
619 `,
620 "first.properties": "APPLE=orange",
621 "second.properties": "BANANA=pear",
622 },
623 },
624 {
625 name: "transformers",
626 files: map[string]string{
627 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
628 kind: Kustomization
629 transformers:
630 - |
631 apiVersion: builtin
632 kind: PatchTransformer
633 metadata:
634 name: inline
635 path: patchSM-one.yaml
636 - patch.yaml
637 `,
638 "patch.yaml": `apiVersion: builtin
639 kind: PatchTransformer
640 metadata:
641 name: file
642 path: patchSM-two.yaml
643 `,
644 "patchSM-one.yaml": podConfiguration,
645 "patchSM-two.yaml": podConfiguration,
646 },
647 },
648 {
649 name: "validators",
650 files: map[string]string{
651 "kustomization.yaml": `validators:
652 - |
653 apiVersion: builtin
654 kind: ReplacementTransformer
655 metadata:
656 name: inline
657 replacements:
658 - path: first.yaml
659 - second.yaml
660 `,
661 "first.yaml": replacementTransformerWithPath,
662 "second.yaml": replacementTransformerWithPath,
663 "replacement.yaml": replacements,
664 },
665 },
666 } {
667 t.Run(test.name, func(t *testing.T) {
668 checkLocalizeInTargetSuccess(t, test.files)
669 })
670 }
671 }
672
673 func TestLocalizeMultiplePluginsInEntry(t *testing.T) {
674 kustAndPlugins := map[string]string{
675 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
676 kind: Kustomization
677 transformers:
678 - |
679 apiVersion: builtin
680 kind: PatchTransformer
681 metadata:
682 name: one
683 path: patchSM-one.yaml
684 ---
685 apiVersion: builtin
686 kind: PatchTransformer
687 metadata:
688 name: two
689 path: patchSM-two.yaml
690 `,
691 "patchSM-one.yaml": podConfiguration,
692 "patchSM-two.yaml": podConfiguration,
693 }
694 checkLocalizeInTargetSuccess(t, kustAndPlugins)
695 }
696
697 func TestLocalizeCleanedPathInPath(t *testing.T) {
698 const patchf = `apiVersion: builtin
699 kind: PatchTransformer
700 metadata:
701 name: cleaned-path
702 path: %s
703 `
704 kustAndPlugins := map[string]string{
705 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
706 kind: Kustomization
707 transformers:
708 - patch.yaml
709 `,
710 "patch.yaml": fmt.Sprintf(patchf, "../a/patchSM.yaml"),
711 "patchSM.yaml": podConfiguration,
712 }
713 expected, actual := makeFileSystems(t, "/a", kustAndPlugins)
714
715 checkRun(t, actual, "/a", "/a", "/dst")
716 addFiles(t, expected, "/dst", map[string]string{
717 "kustomization.yaml": kustAndPlugins["kustomization.yaml"],
718 "patch.yaml": fmt.Sprintf(patchf, "patchSM.yaml"),
719 "patchSM.yaml": kustAndPlugins["patchSM.yaml"],
720 })
721 checkFSys(t, expected, actual)
722 }
723
724 func TestLocalizeGeneratorsConfigMap(t *testing.T) {
725 files := map[string]string{
726 "kustomization.yaml": `generators:
727 - configMapGenerator
728 `,
729 "configMapGenerator": `apiVersion: builtin
730 behavior: create
731 env: one.env
732 envs:
733 - two.env
734 - three.env
735 files:
736 - four.properties
737 - key=five.properties
738 kind: ConfigMapGenerator
739 metadata:
740 name: custom-generator
741 options:
742 disableNameSuffix: true
743 `,
744 "one.env": "key1=value1",
745 "two.env": "key2=value2",
746 "three.env": "key3=value3",
747 "four.properties": "key4=value4",
748 "five.properties": "key5=value5",
749 }
750 checkLocalizeInTargetSuccess(t, files)
751 }
752
753 func TestLocalizeGeneratorsSecret(t *testing.T) {
754 files := map[string]string{
755 "kustomization.yaml": `generators:
756 - secretGenerator
757 `,
758 "secretGenerator": `apiVersion: builtin
759 env: one.env
760 envs:
761 - two.env
762 - three.env
763 files:
764 - four.properties
765 - key=five.properties
766 kind: SecretGenerator
767 literals:
768 - key6=value6
769 metadata:
770 name: custom-generator
771 `,
772 "one.env": "key1=value1",
773 "two.env": "key2=value2",
774 "three.env": "key3=value3",
775 "four.properties": "key4=value4",
776 "five.properties": "key5=value5",
777 }
778 checkLocalizeInTargetSuccess(t, files)
779 }
780
781 func TestLocalizeTransformersPatch(t *testing.T) {
782 kustAndPatches := map[string]string{
783 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
784 kind: Kustomization
785 transformers:
786 - |
787 apiVersion: builtin
788 kind: PatchTransformer
789 metadata:
790 name: no-path
791 patch: '[{"op": "add", "path": "/path", "value": "value"}]'
792 target:
793 name: pod
794 - patch.yaml
795 `,
796 "patch.yaml": `apiVersion: builtin
797 kind: PatchTransformer
798 metadata:
799 name: path
800 path: patchSM.yaml
801 `,
802 "patchSM.yaml": podConfiguration,
803 }
804 checkLocalizeInTargetSuccess(t, kustAndPatches)
805 }
806
807 func TestLocalizeTransformersPatchJson(t *testing.T) {
808 kustAndPatches := map[string]string{
809 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
810 kind: Kustomization
811 transformers:
812 - patch.yaml
813 `,
814 "patch.yaml": `apiVersion: builtin
815 kind: PatchJson6902Transformer
816 metadata:
817 name: path
818 path: nested-patch.yaml
819 target:
820 name: pod
821 namespace: test
822 ---
823 apiVersion: builtin
824 jsonOp: |-
825 op: replace
826 path: /path
827 value: new value
828 kind: PatchJson6902Transformer
829 metadata:
830 name: patch6902
831 target:
832 name: deployment
833 `,
834 "nested-patch.yaml": ` [
835 {"op": "copy", "from": "/existing/path", "path": "/another/path"},
836 ]
837 `,
838 }
839 checkLocalizeInTargetSuccess(t, kustAndPatches)
840 }
841
842 func TestLocalizeTransformersPatchSM(t *testing.T) {
843 kustAndPatches := map[string]string{
844 "kustomization.yaml": `transformers:
845 - patch.yaml
846 `,
847 "patch.yaml": `apiVersion: builtin
848 kind: PatchStrategicMergeTransformer
849 metadata:
850 name: path
851 paths:
852 - nested-patch.yaml
853 - |-
854 apiVersion: v1
855 kind: Pod
856 metadata:
857 name: my-pod
858 `,
859 "nested-patch.yaml": podConfiguration,
860 }
861 checkLocalizeInTargetSuccess(t, kustAndPatches)
862 }
863
864 func TestLocalizeTransformersReplacement(t *testing.T) {
865 kustAndReplacements := map[string]string{
866 "kustomization.yaml": `transformers:
867 - replacement-transformer.yaml
868 `,
869 "replacement-transformer.yaml": replacementTransformerWithPath,
870 "replacement.yaml": replacements,
871 }
872 checkLocalizeInTargetSuccess(t, kustAndReplacements)
873 }
874
875 func TestLocalizePluginsNoPaths(t *testing.T) {
876 kustAndPlugins := map[string]string{
877 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
878 kind: Kustomization
879 transformers:
880 - |
881 apiVersion: different
882 kind: MyTransformer
883 metadata:
884 name: still-copied
885 path: /nothing/special
886 - prefix.yaml
887 `,
888 "prefix.yaml": `apiVersion: builtin
889 kind: PrefixTransformer
890 metadata:
891 name: other-built-ins-still-copied
892 prefix: copy
893 `,
894 }
895 checkLocalizeInTargetSuccess(t, kustAndPlugins)
896 }
897
898 func TestLocalizeValidators(t *testing.T) {
899 kustAndPlugin := map[string]string{
900 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
901 kind: Kustomization
902 validators:
903 - replacement-no-change.yaml
904 `,
905 "replacement-no-change.yaml": replacementTransformerWithPath,
906 "replacement.yaml": replacements,
907 }
908 checkLocalizeInTargetSuccess(t, kustAndPlugin)
909 }
910
911 func TestLocalizeBuiltinPlugins_SequenceScalarEquivalence(t *testing.T) {
912 kustomization := map[string]string{
913 "kustomization.yaml": `transformers:
914 - |
915 apiVersion: builtin
916 kind: PatchTransformer
917 metadata:
918 name: path-should-be-scalar-but-accept-sequence
919 path:
920 - patchSM.yaml
921 `,
922 "patchSM.yaml": podConfiguration,
923 }
924 checkLocalizeInTargetSuccess(t, kustomization)
925 }
926
927 func TestLocalizeBuiltinPlugins_NotResource(t *testing.T) {
928 type testCase struct {
929 name string
930 files map[string]string
931 errPrefix string
932 inlineErrMsg string
933 fileErrMsg string
934 }
935 for _, test := range []testCase{
936 {
937 name: "bad_inline_resource",
938 files: map[string]string{
939 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
940 generators:
941 - |
942 apiVersion: builtin
943 kind: ConfigMapGenerator
944 kind: Kustomization
945 `,
946 },
947 errPrefix: `unable to load generators entry: unable to load resource entry "apiVersion: builtin\nkind: ConfigMapGenerator\n"`,
948 inlineErrMsg: `missing metadata.name in object {{builtin ConfigMapGenerator} {{ } map[] map[]}}`,
949 fileErrMsg: `invalid file reference: '/apiVersion: builtin
950 kind: ConfigMapGenerator
951 ' doesn't exist`,
952 },
953 {
954 name: "bad_file_resource",
955 files: map[string]string{
956 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
957 kind: Kustomization
958 transformers:
959 - plugin.yaml
960 `,
961 "plugin.yaml": `apiVersion: builtin
962 metadata:
963 name: PatchTransformer
964 `,
965 },
966 errPrefix: `unable to load transformers entry: unable to load resource entry "plugin.yaml"`,
967 inlineErrMsg: `missing Resource metadata`,
968 fileErrMsg: `missing kind in object {{builtin } {{PatchTransformer } map[] map[]}}`,
969 },
970 } {
971 t.Run(test.name, func(t *testing.T) {
972 expected, actual := makeFileSystems(t, "/", test.files)
973
974 _, err := Run("/", "", "/dst", actual)
975
976 var actualErr ResourceLoadError
977 require.ErrorAs(t, err, &actualErr)
978 require.EqualError(t, actualErr.InlineError, test.inlineErrMsg)
979 require.EqualError(t, actualErr.FileError, test.fileErrMsg)
980
981 require.EqualError(t, err, fmt.Sprintf(`unable to localize target "/": %s: when parsing as inline received error: %s
982 when parsing as filepath received error: %s`, test.errPrefix, test.inlineErrMsg, test.fileErrMsg))
983
984 checkFSys(t, expected, actual)
985 })
986 }
987 }
988
989 func TestLocalizeBuiltinPlugins_Errors(t *testing.T) {
990 for name, test := range map[string]struct {
991 files map[string]string
992 fieldSpecErr string
993 locErr string
994 }{
995 "file_dne": {
996 files: map[string]string{
997 "kustomization.yaml": `transformers:
998 - |
999 apiVersion: builtin
1000 kind: PatchTransformer
1001 metadata:
1002 name: file-does-not-exist
1003 path: patchSM.yaml
1004 `,
1005 },
1006 fieldSpecErr: "considering field 'path' of object PatchTransformer.builtin.[noGrp]/file-does-not-exist.[noNs]",
1007 locErr: "invalid file reference: '/a/patchSM.yaml' doesn't exist",
1008 },
1009 "not_sequence_or_scalar": {
1010 files: map[string]string{
1011 "kustomization.yaml": `transformers:
1012 - |
1013 apiVersion: builtin
1014 kind: PatchTransformer
1015 metadata:
1016 name: path-node-has-wrong-kind
1017 path:
1018 mappingNode: patchSM.yaml
1019 `,
1020 "patchSM.yaml": podConfiguration,
1021 },
1022 fieldSpecErr: "considering field 'path' of object PatchTransformer.builtin.[noGrp]/path-node-has-wrong-kind.[noNs]",
1023 locErr: "expected sequence or scalar node",
1024 },
1025 } {
1026 t.Run(name, func(t *testing.T) {
1027 expected, actual := makeFileSystems(t, "/a", test.files)
1028 _, err := Run("/a", "", "/dst", actual)
1029 const errPrefix = `unable to localize target "/a"`
1030 require.EqualError(t, err, fmt.Sprintf(
1031 "%s: %s: %s", errPrefix, test.fieldSpecErr, test.locErr))
1032 checkFSys(t, expected, actual)
1033 })
1034 }
1035 }
1036
1037 func TestLocalizeDirInTarget(t *testing.T) {
1038 type testCase struct {
1039 name string
1040 files map[string]string
1041 }
1042 for _, tc := range []testCase{
1043 {
1044 name: "multi_nested_child",
1045 files: map[string]string{
1046 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1047 components:
1048 - delta/epsilon
1049 kind: Kustomization
1050 `,
1051 "delta/epsilon/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1052 kind: Component
1053 namespace: kustomize-namespace
1054 `,
1055 },
1056 },
1057 {
1058 name: "recursive",
1059 files: map[string]string{
1060 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1061 components:
1062 - delta
1063 kind: Kustomization
1064 `,
1065 "delta/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1066 components:
1067 - epsilon
1068 kind: Component
1069 `,
1070 "delta/epsilon/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1071 kind: Component
1072 namespace: kustomize-namespace
1073 `,
1074 },
1075 },
1076 {
1077 name: "file_in_dir",
1078 files: map[string]string{
1079 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1080 components:
1081 - delta
1082 kind: Kustomization
1083 `,
1084 "delta/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1085 kind: Component
1086 patches:
1087 - path: patch.yaml
1088 `,
1089 "delta/patch.yaml": podConfiguration,
1090 },
1091 },
1092 {
1093 name: "multiple_calls",
1094 files: map[string]string{
1095 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1096 components:
1097 - delta
1098 - delta/epsilon
1099 kind: Kustomization
1100 `,
1101 "delta/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1102 kind: Component
1103 namespace: kustomize-namespace
1104 `,
1105 "delta/epsilon/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1106 buildMetadata:
1107 - managedByLabel
1108 kind: Component
1109 `,
1110 },
1111 },
1112 {
1113 name: "localize_directory_name_when_no_remote",
1114 files: map[string]string{
1115 "kustomization.yaml": fmt.Sprintf(`apiVersion: kustomize.config.k8s.io/v1beta1
1116 components:
1117 - %s
1118 kind: Kustomization
1119 `, LocalizeDir),
1120 fmt.Sprintf("%s/kustomization.yaml", LocalizeDir): `apiVersion: kustomize.config.k8s.io/v1alpha1
1121 kind: Component
1122 namespace: kustomize-namespace
1123 `,
1124 },
1125 },
1126 } {
1127 t.Run(tc.name, func(t *testing.T) {
1128 checkLocalizeInTargetSuccess(t, tc.files)
1129 })
1130 }
1131 }
1132
1133 func TestLocalizeDirCleanedSibling(t *testing.T) {
1134 kustAndComponents := map[string]string{
1135 // This test checks that winding paths that might traverse through directories
1136 // outside of scope, which will not be present at destination, are cleaned.
1137 "beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1138 components:
1139 - delta/../../../../a/b/../../alpha/beta/sibling
1140 kind: Kustomization`,
1141 "beta/sibling/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1142 kind: Component
1143 namespace: kustomize-namespace
1144 `,
1145 }
1146 expected, actual := makeFileSystems(t, "/alpha", kustAndComponents)
1147
1148 checkRun(t, actual, "/alpha/beta/gamma", "/alpha", "/alpha/beta/dst")
1149 cleanedFiles := map[string]string{
1150 "beta/gamma/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1151 components:
1152 - ../sibling
1153 kind: Kustomization
1154 `,
1155 "beta/sibling/kustomization.yaml": kustAndComponents["beta/sibling/kustomization.yaml"],
1156 }
1157 addFiles(t, expected, "/alpha/beta/dst", cleanedFiles)
1158 checkFSys(t, expected, actual)
1159 }
1160
1161 func TestLocalizeBases(t *testing.T) {
1162 kustAndBases := map[string]string{
1163 "kustomization.yaml": `bases:
1164 - b
1165 - c/d
1166 `,
1167 "b/kustomization.yaml": `kind: Kustomization
1168 `,
1169 "c/d/kustomization.yaml": `kind: Kustomization
1170 `,
1171 }
1172 checkLocalizeInTargetSuccess(t, kustAndBases)
1173 }
1174
1175 func TestLocalizeComponents(t *testing.T) {
1176 kustAndComponents := map[string]string{
1177 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1178 components:
1179 - a
1180 - alpha
1181 kind: Kustomization
1182 `,
1183 "a/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1184 kind: Component
1185 namePrefix: my-
1186 `,
1187 "alpha/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1alpha1
1188 kind: Component
1189 nameSuffix: -test
1190 `,
1191 }
1192 checkLocalizeInTargetSuccess(t, kustAndComponents)
1193 }
1194
1195 func TestLocalizeResources(t *testing.T) {
1196 kustAndResources := map[string]string{
1197 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1198 kind: Kustomization
1199 resources:
1200 - pod.yaml
1201 - /a/b/pod2.yaml
1202 - ../../alpha
1203 `,
1204 "pod.yaml": podConfiguration,
1205 "pod2.yaml": podConfiguration,
1206 "../../alpha/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1207 kind: Kustomization
1208 namePrefix: my-
1209 `,
1210 }
1211
1212 // Absolute path of `/a/b/pod2.yaml` is expected to be converted to a path
1213 // relative to the kustomization root.
1214 expectedKustAndResources := map[string]string{
1215 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1216 kind: Kustomization
1217 resources:
1218 - pod.yaml
1219 - pod2.yaml
1220 - ../../alpha
1221 `,
1222 "pod.yaml": podConfiguration,
1223 "pod2.yaml": podConfiguration,
1224 "../../alpha/kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1225 kind: Kustomization
1226 namePrefix: my-
1227 `,
1228 }
1229
1230 expectedFs, actualFs := makeFileSystems(t, "/a/b", kustAndResources)
1231
1232 checkRun(t, actualFs, "/a/b", "/", "/localized-b")
1233 addFiles(t, expectedFs, "/localized-b/a/b", expectedKustAndResources)
1234 checkFSys(t, expectedFs, actualFs)
1235 }
1236
1237 func TestLocalizePathError(t *testing.T) {
1238 kustAndResources := map[string]string{
1239 "kustomization.yaml": `apiVersion: kustomize.config.k8s.io/v1beta1
1240 kind: Kustomization
1241 resources:
1242 - b
1243 `,
1244 }
1245 expected, actual := makeFileSystems(t, "/a", kustAndResources)
1246
1247 _, err := Run("/a", "/", "", actual)
1248
1249 const expectedFileErr = `invalid file reference: '/a/b' must resolve to a file`
1250 const expectedRootErr = `unable to localize root "b": unable to find one of 'kustomization.yaml', 'kustomization.yml' or 'Kustomization' in directory '/a/b'`
1251 var actualErr PathLocalizeError
1252 require.ErrorAs(t, err, &actualErr)
1253 require.Equal(t, "b", actualErr.Path)
1254 require.EqualError(t, actualErr.FileError, expectedFileErr)
1255 require.EqualError(t, actualErr.RootError, expectedRootErr)
1256
1257 const expectedErrPrefix = `unable to localize target "/a": unable to localize resources entry`
1258 require.EqualError(t, err, fmt.Sprintf(`%s: could not localize path "b" as file: %s; could not localize path "b" as directory: %s`,
1259 expectedErrPrefix, expectedFileErr, expectedRootErr))
1260
1261 checkFSys(t, expected, actual)
1262 }
1263
1264 func TestLocalizeHelmChartInflationGenerator(t *testing.T) {
1265 helmKust := map[string]string{
1266 "kustomization.yaml": `helmChartInflationGenerator:
1267 - chartName: nothing-to-localize
1268 chartRepoUrl: https://itzg.github.io/warcraft-server-charts
1269 releaseName: moria
1270 - chartName: localize-values
1271 values: minecraftValues.yaml
1272 valuesLocal:
1273 minecraftServer:
1274 eula: true
1275 valuesMerge: replace
1276 - chartHome: home
1277 chartName: copy-chartHome
1278 `,
1279 "minecraftValues.yaml": valuesFile,
1280 "charts/localize-values/values.yaml": valuesFile,
1281 "home/copy-chartHome/values.yaml": valuesFile,
1282 }
1283 checkLocalizeInTargetSuccess(t, helmKust)
1284 }
1285
1286 func TestLocalizeHelmCharts(t *testing.T) {
1287 for _, test := range []struct {
1288 name string
1289 files map[string]string
1290 }{
1291 {
1292 name: "charts_only",
1293 files: map[string]string{
1294 "kustomization.yaml": `helmCharts:
1295 - name: nothing-to-localize
1296 repo: https://helm.releases.hashicorp.com
1297 version: 1.0.0
1298 - includeCRDs: true
1299 name: localize-valuesFile
1300 valuesFile: file
1301 - additionalValuesFiles:
1302 - another
1303 - third
1304 `,
1305 "file": valuesFile,
1306 "another": valuesFile,
1307 "third": valuesFile,
1308 "charts/nothing-to-localize/values.yaml": valuesFile,
1309 "charts/localize-valuesFile/values.yaml": valuesFile,
1310 },
1311 },
1312 {
1313 name: "charts_globals_no_home",
1314 files: map[string]string{
1315 "kustomization.yaml": `helmCharts:
1316 - name: default
1317 helmGlobals:
1318 configHome: .
1319 `,
1320 "charts/default/values.yaml": valuesFile,
1321 },
1322 },
1323 {
1324 name: "home_only",
1325 files: map[string]string{
1326 "kustomization.yaml": `helmGlobals:
1327 chartHome: home
1328 `,
1329 "home/name/values.yaml": valuesFile,
1330 },
1331 },
1332 } {
1333 t.Run(test.name, func(t *testing.T) {
1334 checkLocalizeInTargetSuccess(t, test.files)
1335 })
1336 }
1337 }
1338
1339 func TestLocalizeHelmChartsNoDefault(t *testing.T) {
1340 files := map[string]string{
1341 "kustomization.yaml": `helmGlobals:
1342 chartHome: home
1343 `,
1344 "home/name/values.yaml": valuesFile,
1345 "charts/name/values.yaml": valuesFile,
1346 }
1347 expected, actual := makeFileSystems(t, "/a", files)
1348
1349 checkRun(t, actual, "/a", "/a", "/dst")
1350 addFiles(t, expected, "/dst", map[string]string{
1351 "kustomization.yaml": files["kustomization.yaml"],
1352 "home/name/values.yaml": valuesFile,
1353 })
1354 checkFSys(t, expected, actual)
1355 }
1356
1357 func TestCopyChartHomeSimple(t *testing.T) {
1358 for _, test := range []struct {
1359 name string
1360 files map[string]string
1361 }{
1362 {
1363 name: "does_not_exist",
1364 files: map[string]string{
1365 "kustomization.yaml": `helmGlobals:
1366 chartHome: untar-dir
1367 `,
1368 },
1369 },
1370 {
1371 name: "chart_home_structure",
1372 files: map[string]string{
1373 "kustomization.yaml": `helmGlobals:
1374 chartHome: home
1375 `,
1376 "home/minecraft-3.1.3.tgz": "blah",
1377 "home/terraform-1.0.0.tgz": "la",
1378 "home/minecraft/Chart.yaml": `annotations:
1379 artifacthub.io/links: |
1380 - name: source
1381 url: https://minecraft.net/
1382 `,
1383 "home/terraform/Chart.yaml": `description: Minecraft server
1384 `,
1385 },
1386 },
1387 } {
1388 t.Run(test.name, func(t *testing.T) {
1389 checkLocalizeInTargetSuccess(t, test.files)
1390 })
1391 }
1392 }
1393
1394 func TestCopyChartHomeChanges(t *testing.T) {
1395 for name, test := range map[string]struct {
1396 files map[string]string
1397 copiedFiles map[string]string
1398 }{
1399 "clean_does_not_exist": {
1400 files: map[string]string{
1401 "kustomization.yaml": `helmGlobals:
1402 chartHome: ../b/home
1403 `,
1404 },
1405 copiedFiles: map[string]string{
1406 "kustomization.yaml": `helmGlobals:
1407 chartHome: home
1408 `,
1409 },
1410 },
1411 "clean_default": {
1412 files: map[string]string{
1413 "kustomization.yaml": `helmGlobals:
1414 chartHome: ../b/charts
1415 `,
1416 "charts/name/values.yaml": valuesFile,
1417 },
1418 copiedFiles: map[string]string{
1419 "kustomization.yaml": `helmGlobals:
1420 chartHome: charts
1421 `,
1422 "charts/name/values.yaml": valuesFile,
1423 },
1424 },
1425 "not_copied": {
1426 files: map[string]string{
1427 "kustomization.yaml": `helmCharts:
1428 - name: name
1429 valuesFile: home/name/values.yaml
1430 helmGlobals:
1431 chartHome: home
1432 `,
1433 "home/name/values.yaml": valuesFile,
1434 "home/name/many-other-files": "other contents",
1435 },
1436 copiedFiles: map[string]string{
1437 "kustomization.yaml": `helmCharts:
1438 - name: name
1439 valuesFile: home/name/values.yaml
1440 helmGlobals:
1441 chartHome: home
1442 `,
1443 "home/name/values.yaml": valuesFile,
1444 },
1445 },
1446 "does_not_exist_exits_scope": {
1447 files: map[string]string{
1448 "kustomization.yaml": `helmGlobals:
1449 chartHome: ../home
1450 `,
1451 "../../home/will-exist-at-dst/values.yaml": valuesFile,
1452 },
1453 copiedFiles: map[string]string{
1454 "kustomization.yaml": `helmGlobals:
1455 chartHome: ../home
1456 `,
1457 },
1458 },
1459 } {
1460 t.Run(name, func(t *testing.T) {
1461 expected, actual := makeFileSystems(t, "/a/b", test.files)
1462
1463 checkRun(t, actual, "/a/b", "/a/b", "/dst")
1464 addFiles(t, expected, "/dst", test.copiedFiles)
1465 checkFSys(t, expected, actual)
1466 })
1467 }
1468 }
1469
1470 func TestCopyChartHomeEmpty(t *testing.T) {
1471 kustomization := map[string]string{
1472 "kustomization.yaml": `helmGlobals:
1473 chartHome: home
1474 `,
1475 }
1476 expected, actual := makeFileSystems(t, "/a", kustomization)
1477 require.NoError(t, actual.Mkdir("/a/home"))
1478 require.NoError(t, expected.Mkdir("/a/home"))
1479
1480 checkRun(t, actual, "/a", "/a", "/dst")
1481 addFiles(t, expected, "/dst", kustomization)
1482 require.NoError(t, expected.Mkdir("/dst/home"))
1483 checkFSys(t, expected, actual)
1484 }
1485
1486 func TestCopyChartHomeError(t *testing.T) {
1487 for name, test := range map[string]struct {
1488 err string
1489 files map[string]string
1490 }{
1491 "absolute": {
1492 err: `unable to copy helmGlobals: absolute path "/a/b/home" not handled in alpha`,
1493 files: map[string]string{
1494 "a/b/kustomization.yaml": `helmGlobals:
1495 chartHome: /a/b/home
1496 `,
1497 "a/b/home/name/values.yaml": valuesFile,
1498 },
1499 },
1500 "file": {
1501 err: `unable to copy helmGlobals: unable to copy home "home": invalid chart home: invalid root reference: must build at directory: '/a/b/home': file is not directory`,
1502 files: map[string]string{
1503 "a/b/kustomization.yaml": `helmGlobals:
1504 chartHome: home
1505 `,
1506 "a/b/home": valuesFile,
1507 },
1508 },
1509 "scope": {
1510 err: `unable to copy helmGlobals: unable to copy home "../../alpha/home": invalid chart home: root "/alpha/home" outside localize scope "/a"`,
1511 files: map[string]string{
1512 "a/b/kustomization.yaml": `helmGlobals:
1513 chartHome: ../../alpha/home
1514 `,
1515 "alpha/home/values.yaml": valuesFile,
1516 },
1517 },
1518 } {
1519 t.Run(name, func(t *testing.T) {
1520 expected, actual := makeFileSystems(t, "/", test.files)
1521
1522 _, err := Run("/a/b", "/a", "/dst", actual)
1523 const prefix = `unable to localize target "/a/b"`
1524 require.EqualError(t, err, fmt.Sprintf("%s: %s", prefix, test.err))
1525
1526 checkFSys(t, expected, actual)
1527 })
1528 }
1529 }
1530
1531 func TestLocalizeGeneratorsHelm(t *testing.T) {
1532 files := map[string]string{
1533 "kustomization.yaml": `generators:
1534 - default.yaml
1535 - explicit.yaml
1536 `,
1537 "default.yaml": `apiVersion: builtin
1538 kind: HelmChartInflationGenerator
1539 metadata:
1540 name: no-explicit-references
1541 name: minecraft
1542 releaseName: moria
1543 repo: https://itzg.github.io/minecraft-server-charts
1544 version: 3.1.3
1545 `,
1546 "explicit.yaml": `additionalValuesFiles:
1547 - time.yaml
1548 - life.yaml
1549 - light.yaml
1550 apiVersion: builtin
1551 chartHome: home
1552 kind: HelmChartInflationGenerator
1553 metadata:
1554 name: explicit-references
1555 name: mapleStory
1556 valuesFile: mapleValues.yaml
1557 `,
1558 "time.yaml": valuesFile,
1559 "life.yaml": valuesFile,
1560 "light.yaml": valuesFile,
1561 "mapleValues.yaml": valuesFile,
1562 "home/mapleStory/values.yaml": valuesFile,
1563 "charts/minecraft/values.yaml": valuesFile,
1564 }
1565 checkLocalizeInTargetSuccess(t, files)
1566 }
1567
1568 func TestLocalizeGeneratorsNoHelm(t *testing.T) {
1569 files := map[string]string{
1570 "kustomization.yaml": `generators:
1571 - configMap.yaml
1572 `,
1573 "configMap.yaml": `apiVersion: builtin
1574 kind: ConfigMapGenerator
1575 literals:
1576 - APPLE=orange
1577 metadata:
1578 name: not-helm-shouldn't-copy-default-helm-chart-home
1579 `,
1580 "charts/minecraft/values.yaml": valuesFile,
1581 }
1582 expected, actual := makeFileSystems(t, "/a", files)
1583
1584 checkRun(t, actual, "/a", "/a", "/dst")
1585 addFiles(t, expected, "/dst", map[string]string{
1586 "kustomization.yaml": files["kustomization.yaml"],
1587 "configMap.yaml": files["configMap.yaml"],
1588 })
1589 checkFSys(t, expected, actual)
1590 }
1591
1592 func TestLocalizeEmpty(t *testing.T) {
1593 for name, kustomization := range map[string]string{
1594 "file": `configurations:
1595 - ""
1596 `,
1597 "root": `bases:
1598 - ""
1599 `,
1600 "resource": `resources:
1601 - ""
1602 `,
1603 "generator_file_src": `configMapGenerator:
1604 - files:
1605 - ""
1606 `,
1607 "patchesStrategicMerge": `patchesStrategicMerge:
1608 - ""
1609 `,
1610 "custom_transformers": `transformers:
1611 - ""
1612 `,
1613 "custom_transformer_field": `transformers:
1614 - |
1615 apiVersion: builtin
1616 kind: PatchStrategicMergeTransformer
1617 metadata:
1618 name: empty
1619 paths:
1620 - ""
1621 `,
1622 } {
1623 t.Run(name, func(t *testing.T) {
1624 checkLocalizeInTargetSuccess(t, map[string]string{
1625 "kustomization.yaml": kustomization,
1626 })
1627 })
1628 }
1629 }