Skip to content

Commit c0b3af1

Browse files
committed
test: field overrides for configMapGenerator + secretGenerator
1 parent 072ae78 commit c0b3af1

File tree

2 files changed

+242
-12
lines changed

2 files changed

+242
-12
lines changed

api/krusty/configmaps_test.go

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func TestGeneratorFromProperties(t *testing.T) {
8787
th := kusttest_test.MakeHarness(t)
8888
th.WriteK("base", `
8989
configMapGenerator:
90-
- name: test-configmap
91-
behavior: create
92-
envs:
93-
- properties
90+
- name: test-configmap
91+
behavior: create
92+
envs:
93+
- properties
9494
`)
9595
th.WriteF("base/properties", `
9696
VAR1=100
@@ -118,6 +118,104 @@ metadata:
118118
`)
119119
}
120120

121+
func TestGeneratorOverrideDataWithBinaryDataInvalidAtKubeAPI(t *testing.T) {
122+
// the resulting ConfigMap will fail Kubernetes API validation:
123+
// The ConfigMap "test-configmap-b6h9d5bfmt" is invalid: data[CHANGING]: Invalid value: "CHANGING": duplicate of key present in binaryData
124+
th := kusttest_test.MakeHarness(t)
125+
th.WriteK("base", `
126+
configMapGenerator:
127+
- name: test-configmap
128+
behavior: create
129+
envs:
130+
- properties
131+
`)
132+
th.WriteF("base/properties", `
133+
CHANGING=data-before
134+
BASE=red
135+
`)
136+
th.WriteK("overlay", `
137+
resources:
138+
- ../base
139+
configMapGenerator:
140+
- name: test-configmap
141+
behavior: "merge"
142+
envs:
143+
- properties
144+
files:
145+
- CHANGING
146+
`)
147+
th.WriteF("overlay/CHANGING", string(manyHellos(30)))
148+
th.WriteF("overlay/properties", `
149+
OVERLAY=blue
150+
`)
151+
m := th.Run("overlay", th.MakeDefaultOptions())
152+
th.AssertActualEqualsExpected(m, `
153+
apiVersion: v1
154+
binaryData:
155+
CHANGING: |
156+
/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
157+
xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
158+
bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
159+
hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
160+
data:
161+
BASE: red
162+
CHANGING: data-before
163+
OVERLAY: blue
164+
kind: ConfigMap
165+
metadata:
166+
name: test-configmap-b6h9d5bfmt
167+
`)
168+
}
169+
170+
func TestGeneratorOverrideBinaryDataWithDataInvalidAtKubeAPI(t *testing.T) {
171+
// the resulting ConfigMap will fail Kubernetes API validation:
172+
// The ConfigMap "test-configmap-kt6d6mk694" is invalid: data[CHANGING]: Invalid value: "CHANGING": duplicate of key present in binaryData
173+
th := kusttest_test.MakeHarness(t)
174+
th.WriteK("base", `
175+
configMapGenerator:
176+
- name: test-configmap
177+
behavior: create
178+
envs:
179+
- properties
180+
files:
181+
- CHANGING
182+
`)
183+
th.WriteF("base/CHANGING", string(manyHellos(30)))
184+
th.WriteF("base/properties", `
185+
BASE=red
186+
`)
187+
th.WriteK("overlay", `
188+
resources:
189+
- ../base
190+
configMapGenerator:
191+
- name: test-configmap
192+
behavior: "merge"
193+
envs:
194+
- properties
195+
`)
196+
th.WriteF("overlay/properties", `
197+
CHANGING=data-after
198+
OVERLAY=blue
199+
`)
200+
m := th.Run("overlay", th.MakeDefaultOptions())
201+
th.AssertActualEqualsExpected(m, `
202+
apiVersion: v1
203+
binaryData:
204+
CHANGING: |
205+
/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbG
206+
xv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hl
207+
bGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv/2
208+
hlbGxv/2hlbGxv/2hlbGxv/2hlbGxv
209+
data:
210+
BASE: red
211+
CHANGING: data-after
212+
OVERLAY: blue
213+
kind: ConfigMap
214+
metadata:
215+
name: test-configmap-kt6d6mk694
216+
`)
217+
}
218+
121219
// Generate a Secret and a ConfigMap from the same data
122220
// to compare the result.
123221
func TestGeneratorBasics(t *testing.T) {

api/krusty/secrets_test.go

Lines changed: 140 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ func TestSecretGeneratorFromProperties(t *testing.T) {
8989
th := kusttest_test.MakeHarness(t)
9090
th.WriteK("base", `
9191
secretGenerator:
92-
- name: test-secret
93-
behavior: create
94-
envs:
95-
- properties
92+
- name: test-secret
93+
behavior: create
94+
envs:
95+
- properties
9696
`)
9797
th.WriteF("base/properties", `
9898
VAR1=100
@@ -126,10 +126,10 @@ func TestSecretGeneratorEmitStringDataFromProperties(t *testing.T) {
126126
th := kusttest_test.MakeHarness(t)
127127
th.WriteK("base", `
128128
secretGenerator:
129-
- name: test-secret
130-
behavior: create
131-
envs:
132-
- properties
129+
- name: test-secret
130+
behavior: create
131+
envs:
132+
- properties
133133
`)
134134
th.WriteF("base/properties", `
135135
VAR1=100
@@ -162,6 +162,138 @@ type: Opaque
162162
}
163163

164164
// Generate Secrets similar to TestGeneratorBasics with emitStringData enabled and
165+
func TestSecretGeneratorDoesNotTouchStringData(t *testing.T) {
166+
th := kusttest_test.MakeHarness(t)
167+
th.WriteK("base", `
168+
secretGenerator:
169+
- name: test-secret
170+
emitStringData: true
171+
behavior: create
172+
envs:
173+
- properties
174+
`)
175+
th.WriteF("base/properties", `
176+
VAR1=100
177+
`)
178+
th.WriteK("overlay", `
179+
resources:
180+
- ../base
181+
secretGenerator:
182+
- name: test-secret
183+
emitStringData: false
184+
behavior: "merge"
185+
envs:
186+
- properties
187+
`)
188+
th.WriteF("overlay/properties", `
189+
VAR2=200
190+
`)
191+
m := th.Run("overlay", th.MakeDefaultOptions())
192+
th.AssertActualEqualsExpected(m, `
193+
apiVersion: v1
194+
data:
195+
VAR2: MjAw
196+
kind: Secret
197+
metadata:
198+
name: test-secret-962dt6476k
199+
stringData:
200+
VAR1: "100"
201+
type: Opaque
202+
`)
203+
}
204+
205+
func TestSecretGeneratorOverrideDataWithStringDataWorksAtKubeAPI(t *testing.T) {
206+
// The resulting Secret will have a duplicate key in both data and stringData
207+
// The stringData override will work, because the kube API considers stringData authoritative and write-only
208+
th := kusttest_test.MakeHarness(t)
209+
th.WriteK("base", `
210+
secretGenerator:
211+
- name: test-secret
212+
behavior: create
213+
envs:
214+
- properties
215+
`)
216+
th.WriteF("base/properties", `
217+
CHANGING=data-before
218+
BASE=red
219+
`)
220+
th.WriteK("overlay", `
221+
resources:
222+
- ../base
223+
secretGenerator:
224+
- name: test-secret
225+
emitStringData: true
226+
behavior: "merge"
227+
envs:
228+
- properties
229+
`)
230+
th.WriteF("overlay/properties", `
231+
CHANGING=stringData-after
232+
OVERLAY=blue
233+
`)
234+
m := th.Run("overlay", th.MakeDefaultOptions())
235+
th.AssertActualEqualsExpected(m, `
236+
apiVersion: v1
237+
data:
238+
BASE: cmVk
239+
CHANGING: ZGF0YS1iZWZvcmU=
240+
kind: Secret
241+
metadata:
242+
name: test-secret-c4g4kdc558
243+
stringData:
244+
CHANGING: stringData-after
245+
OVERLAY: blue
246+
type: Opaque
247+
`)
248+
}
249+
250+
func TestSecretGeneratorOverrideStringDataWithDataSilentlyFailsAtKubeAPI(t *testing.T) {
251+
// The resulting Secret will have a duplicate key in both data and stringData
252+
// The data override will fail, because the kube API considers the older value in stringData authoritative and write-only
253+
th := kusttest_test.MakeHarness(t)
254+
th.WriteK("base", `
255+
secretGenerator:
256+
- name: test-secret
257+
emitStringData: true
258+
behavior: create
259+
envs:
260+
- properties
261+
`)
262+
th.WriteF("base/properties", `
263+
CHANGING=stringData-before
264+
BASE=red
265+
`)
266+
th.WriteK("overlay", `
267+
resources:
268+
- ../base
269+
secretGenerator:
270+
- name: test-secret
271+
emitStringData: false
272+
behavior: "merge"
273+
envs:
274+
- properties
275+
`)
276+
th.WriteF("overlay/properties", `
277+
CHANGING=data-after
278+
OVERLAY=blue
279+
`)
280+
m := th.Run("overlay", th.MakeDefaultOptions())
281+
th.AssertActualEqualsExpected(m, `
282+
apiVersion: v1
283+
data:
284+
CHANGING: ZGF0YS1hZnRlcg==
285+
OVERLAY: Ymx1ZQ==
286+
kind: Secret
287+
metadata:
288+
name: test-secret-b65ktgckfh
289+
stringData:
290+
BASE: red
291+
CHANGING: stringData-before
292+
type: Opaque
293+
`)
294+
}
295+
296+
// Generate Secrets similar to TestGeneratorBasics with stringData enabled and
165297
// disabled.
166298
func TestSecretGeneratorEmitStringData(t *testing.T) {
167299
th := kusttest_test.MakeHarness(t)

0 commit comments

Comments
 (0)