...
1
2
3
4 package manifestreader
5
6 import (
7 "os"
8 "path/filepath"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
13 "sigs.k8s.io/kustomize/kyaml/kio/kioutil"
14 )
15
16 func TestPathManifestReader_Read(t *testing.T) {
17 testCases := map[string]struct {
18 manifests map[string]string
19 namespace string
20 enforceNamespace bool
21 validate bool
22
23 infosCount int
24 namespaces []string
25 }{
26 "namespace should be set if not already present": {
27 manifests: map[string]string{
28 "dep.yaml": depManifest,
29 },
30 namespace: "foo",
31 enforceNamespace: true,
32
33 infosCount: 1,
34 namespaces: []string{"foo"},
35 },
36 "multiple manifests": {
37 manifests: map[string]string{
38 "dep.yaml": depManifest,
39 "cm.yaml": cmManifest,
40 },
41 namespace: "default",
42 enforceNamespace: true,
43
44 infosCount: 2,
45 namespaces: []string{"default", "default"},
46 },
47 }
48
49 for tn, tc := range testCases {
50 t.Run(tn, func(t *testing.T) {
51 tf := cmdtesting.NewTestFactory().WithNamespace("test-ns")
52 defer tf.Cleanup()
53
54 mapper, err := tf.ToRESTMapper()
55 if !assert.NoError(t, err) {
56 t.FailNow()
57 }
58
59 dir, err := os.MkdirTemp("", "path-reader-test")
60 assert.NoError(t, err)
61 for filename, content := range tc.manifests {
62 p := filepath.Join(dir, filename)
63 err := os.WriteFile(p, []byte(content), 0600)
64 assert.NoError(t, err)
65 }
66
67 objs, err := (&PathManifestReader{
68 Path: dir,
69 ReaderOptions: ReaderOptions{
70 Mapper: mapper,
71 Namespace: tc.namespace,
72 EnforceNamespace: tc.enforceNamespace,
73 Validate: tc.validate,
74 },
75 }).Read()
76
77 assert.NoError(t, err)
78 assert.Equal(t, len(objs), tc.infosCount)
79
80 for i, obj := range objs {
81 assert.Equal(t, tc.namespaces[i], obj.GetNamespace())
82 _, ok := obj.GetAnnotations()[kioutil.PathAnnotation]
83 assert.True(t, ok)
84 }
85 })
86 }
87 }
88
View as plain text