Skip to content

Commit bab4e87

Browse files
committed
test: field overrides for configMapGenerator + secretGenerator
1 parent 7581569 commit bab4e87

File tree

2 files changed

+241
-12
lines changed

2 files changed

+241
-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: 139 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 TestSecretGeneratorStringDataFromProperties(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
@@ -161,6 +161,137 @@ type: Opaque
161161
`)
162162
}
163163

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

0 commit comments

Comments
 (0)