@@ -193,3 +193,189 @@ func TestCustomizeAnnotation(t *testing.T) {
193
193
})
194
194
}
195
195
}
196
+
197
+ func TestCustomizeLabel (t * testing.T ) {
198
+ testCases := []struct {
199
+ Name string
200
+ Customization []config.Customization
201
+ Component string
202
+ TypeMeta metav1.TypeMeta
203
+ ExistingLabels []func () map [string ]string
204
+ Expect map [string ]string
205
+ }{
206
+ {
207
+ Name : "no customization" ,
208
+ Customization : nil ,
209
+ Component : "component" ,
210
+ TypeMeta : common .TypeMetaDeployment ,
211
+ Expect : map [string ]string {},
212
+ },
213
+ {
214
+ Customization : []config.Customization {},
215
+ Name : "empty customization" ,
216
+ Component : "component" ,
217
+ TypeMeta : common .TypeMetaDeployment ,
218
+ Expect : map [string ]string {},
219
+ },
220
+ {
221
+ Customization : []config.Customization {
222
+ {
223
+ TypeMeta : common .TypeMetaBatchJob ,
224
+ Metadata : metav1.ObjectMeta {
225
+ Name : "component" ,
226
+ Labels : map [string ]string {
227
+ "key1" : "value1" ,
228
+ },
229
+ },
230
+ },
231
+ },
232
+ Name : "ignore different typeMeta labels" ,
233
+ Component : "component" ,
234
+ TypeMeta : common .TypeMetaDeployment ,
235
+ Expect : map [string ]string {},
236
+ },
237
+ {
238
+ Customization : []config.Customization {
239
+ {
240
+ TypeMeta : common .TypeMetaDeployment ,
241
+ Metadata : metav1.ObjectMeta {
242
+ Name : "component2" ,
243
+ Labels : map [string ]string {
244
+ "key1" : "value1" ,
245
+ },
246
+ },
247
+ },
248
+ },
249
+ Name : "ignore same typeMeta, different name labels" ,
250
+ Component : "component" ,
251
+ TypeMeta : common .TypeMetaDeployment ,
252
+ Expect : map [string ]string {},
253
+ },
254
+ {
255
+ Customization : []config.Customization {
256
+ {
257
+ TypeMeta : common .TypeMetaDeployment ,
258
+ Metadata : metav1.ObjectMeta {
259
+ Name : "component" ,
260
+ Labels : map [string ]string {
261
+ "key1" : "value1" ,
262
+ },
263
+ },
264
+ },
265
+ },
266
+ Name : "single component labels" ,
267
+ Component : "component" ,
268
+ TypeMeta : common .TypeMetaDeployment ,
269
+ Expect : map [string ]string {
270
+ "key1" : "value1" ,
271
+ },
272
+ },
273
+ {
274
+ Customization : []config.Customization {
275
+ {
276
+ TypeMeta : common .TypeMetaDeployment ,
277
+ Metadata : metav1.ObjectMeta {
278
+ Name : "component" ,
279
+ Labels : map [string ]string {
280
+ "key1" : "value1" ,
281
+ "key2" : "value2" ,
282
+ },
283
+ },
284
+ },
285
+ {
286
+ TypeMeta : common .TypeMetaDeployment ,
287
+ Metadata : metav1.ObjectMeta {
288
+ Name : "component" ,
289
+ Labels : map [string ]string {
290
+ "key3" : "value3" ,
291
+ },
292
+ },
293
+ },
294
+ },
295
+ Name : "multiple component labels" ,
296
+ Component : "component" ,
297
+ TypeMeta : common .TypeMetaDeployment ,
298
+ Expect : map [string ]string {
299
+ "key1" : "value1" ,
300
+ "key2" : "value2" ,
301
+ "key3" : "value3" ,
302
+ },
303
+ },
304
+ {
305
+ Customization : []config.Customization {
306
+ {
307
+ TypeMeta : metav1.TypeMeta {
308
+ APIVersion : "*" ,
309
+ Kind : "*" ,
310
+ },
311
+ Metadata : metav1.ObjectMeta {
312
+ Name : "*" ,
313
+ Labels : map [string ]string {
314
+ "key1" : "value1" ,
315
+ },
316
+ },
317
+ },
318
+ },
319
+ Name : "wildcard labels" ,
320
+ Component : "component" ,
321
+ TypeMeta : common .TypeMetaDeployment ,
322
+ Expect : map [string ]string {
323
+ "key1" : "value1" ,
324
+ },
325
+ },
326
+ {
327
+ Customization : []config.Customization {
328
+ {
329
+ TypeMeta : metav1.TypeMeta {
330
+ APIVersion : "*" ,
331
+ Kind : "*" ,
332
+ },
333
+ Metadata : metav1.ObjectMeta {
334
+ Name : "*" ,
335
+ Labels : map [string ]string {
336
+ "key1" : "value1" ,
337
+ "key2" : "override" ,
338
+ "key3" : "" ,
339
+ },
340
+ },
341
+ },
342
+ },
343
+ Name : "override input, do not override existing" ,
344
+ Component : "component" ,
345
+ TypeMeta : common .TypeMetaDeployment ,
346
+ ExistingLabels : []func () map [string ]string {
347
+ func () map [string ]string {
348
+ return map [string ]string {
349
+ "key2" : "original" ,
350
+ }
351
+ },
352
+ },
353
+ Expect : map [string ]string {
354
+ "key1" : "value1" ,
355
+ "key2" : "original" ,
356
+ "key3" : "" ,
357
+ },
358
+ },
359
+ }
360
+
361
+ for _ , testCase := range testCases {
362
+ t .Run (testCase .Name , func (t * testing.T ) {
363
+ ctx , err := common .NewRenderContext (config.Config {
364
+ Customization : & testCase .Customization ,
365
+ }, versions.Manifest {}, "test_namespace" )
366
+ require .NoError (t , err )
367
+
368
+ result := common .CustomizeLabel (ctx , testCase .Component , testCase .TypeMeta , testCase .ExistingLabels ... )
369
+
370
+ // These all have the default labels - add these in
371
+ expectation := common .DefaultLabels (testCase .Component )
372
+ for k , v := range testCase .Expect {
373
+ expectation [k ] = v
374
+ }
375
+
376
+ if ! reflect .DeepEqual (expectation , result ) {
377
+ t .Errorf ("expected %v but got %v" , expectation , result )
378
+ }
379
+ })
380
+ }
381
+ }
0 commit comments