1
2
3
4 package generators_test
5
6 import (
7 "path/filepath"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 . "sigs.k8s.io/kustomize/api/internal/generators"
12 "sigs.k8s.io/kustomize/api/kv"
13 "sigs.k8s.io/kustomize/api/pkg/loader"
14 valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
15 "sigs.k8s.io/kustomize/api/types"
16 "sigs.k8s.io/kustomize/kyaml/filesys"
17 )
18
19 var binaryHello = []byte{
20 0xff,
21 0x68,
22 0x65,
23 0x6c,
24 0x6c,
25 0x6f,
26 }
27
28 func manyHellos(count int) (result []byte) {
29 for i := 0; i < count; i++ {
30 result = append(result, binaryHello...)
31 }
32 return
33 }
34
35 func TestMakeConfigMap(t *testing.T) {
36 type expected struct {
37 out string
38 errMsg string
39 }
40
41 testCases := map[string]struct {
42 args types.ConfigMapArgs
43 exp expected
44 }{
45 "construct config map from env": {
46 args: types.ConfigMapArgs{
47 GeneratorArgs: types.GeneratorArgs{
48 Name: "envConfigMap",
49 KvPairSources: types.KvPairSources{
50 EnvSources: []string{
51 filepath.Join("configmap", "app.env"),
52 },
53 },
54 },
55 },
56 exp: expected{
57 out: `apiVersion: v1
58 kind: ConfigMap
59 metadata:
60 name: envConfigMap
61 data:
62 DB_PASSWORD: qwerty
63 DB_USERNAME: admin
64 `,
65 },
66 },
67 "construct config map from text file": {
68 args: types.ConfigMapArgs{
69 GeneratorArgs: types.GeneratorArgs{
70 Name: "fileConfigMap1",
71 KvPairSources: types.KvPairSources{
72 FileSources: []string{
73 filepath.Join("configmap", "app-init.ini"),
74 },
75 },
76 },
77 },
78 exp: expected{
79 out: `apiVersion: v1
80 kind: ConfigMap
81 metadata:
82 name: fileConfigMap1
83 data:
84 app-init.ini: |
85 FOO=bar
86 BAR=baz
87 `,
88 },
89 },
90 "construct config map from text and binary file": {
91 args: types.ConfigMapArgs{
92 GeneratorArgs: types.GeneratorArgs{
93 Name: "fileConfigMap2",
94 KvPairSources: types.KvPairSources{
95 FileSources: []string{
96 filepath.Join("configmap", "app-init.ini"),
97 filepath.Join("configmap", "app.bin"),
98 },
99 },
100 },
101 },
102 exp: expected{
103 out: `apiVersion: v1
104 kind: ConfigMap
105 metadata:
106 name: fileConfigMap2
107 data:
108 app-init.ini: |
109 FOO=bar
110 BAR=baz
111 binaryData:
112 app.bin: |
113 /2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
114 xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
115 bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
116 hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
117 `,
118 },
119 },
120 "construct config map from literal": {
121 args: types.ConfigMapArgs{
122 GeneratorArgs: types.GeneratorArgs{
123 Name: "literalConfigMap1",
124 KvPairSources: types.KvPairSources{
125 LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
126 },
127 Options: &types.GeneratorOptions{
128 Labels: map[string]string{
129 "foo": "bar",
130 },
131 },
132 },
133 },
134 exp: expected{
135 out: `apiVersion: v1
136 kind: ConfigMap
137 metadata:
138 name: literalConfigMap1
139 labels:
140 foo: 'bar'
141 data:
142 a: x
143 b: "y"
144 c: Hello World
145 d: "true"
146 `,
147 },
148 },
149 "construct config map from literal with GeneratorOptions in ConfigMapArgs": {
150 args: types.ConfigMapArgs{
151 GeneratorArgs: types.GeneratorArgs{
152 Name: "literalConfigMap2",
153 KvPairSources: types.KvPairSources{
154 LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
155 },
156 Options: &types.GeneratorOptions{
157 Labels: map[string]string{
158 "veggie": "celery",
159 "dog": "beagle",
160 "cat": "annoying",
161 },
162 Annotations: map[string]string{
163 "river": "Missouri",
164 "city": "Iowa City",
165 },
166 Immutable: true,
167 },
168 },
169 },
170 exp: expected{
171 out: `apiVersion: v1
172 kind: ConfigMap
173 metadata:
174 name: literalConfigMap2
175 labels:
176 cat: 'annoying'
177 dog: 'beagle'
178 veggie: 'celery'
179 annotations:
180 city: 'Iowa City'
181 river: 'Missouri'
182 data:
183 a: x
184 b: "y"
185 c: Hello World
186 d: "true"
187 immutable: true
188 `,
189 },
190 },
191 }
192 fSys := filesys.MakeFsInMemory()
193 fSys.WriteFile(
194 filesys.RootedPath("configmap", "app.env"),
195 []byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
196 fSys.WriteFile(
197 filesys.RootedPath("configmap", "app-init.ini"),
198 []byte("FOO=bar\nBAR=baz\n"))
199 fSys.WriteFile(
200 filesys.RootedPath("configmap", "app.bin"),
201 manyHellos(30))
202 kvLdr := kv.NewLoader(
203 loader.NewFileLoaderAtRoot(fSys),
204 valtest_test.MakeFakeValidator())
205
206 for n := range testCases {
207 tc := testCases[n]
208 t.Run(n, func(t *testing.T) {
209 rn, err := MakeConfigMap(kvLdr, &tc.args)
210 if err != nil {
211 if !assert.EqualError(t, err, tc.exp.errMsg) {
212 t.FailNow()
213 }
214 return
215 }
216 if tc.exp.errMsg != "" {
217 t.Fatalf("%s: should return error '%s'", n, tc.exp.errMsg)
218 }
219 output := rn.MustString()
220 if !assert.Equal(t, tc.exp.out, output) {
221 t.FailNow()
222 }
223 })
224 }
225 }
226
View as plain text