@@ -315,6 +315,7 @@ func Test_buildTags(t *testing.T) {
315
315
t .Errorf ("buildTags() = %v, want %v" , got , tt .want )
316
316
}
317
317
tagFormat = "json"
318
+ defaultTags = map [string ]string {}
318
319
})
319
320
}
320
321
}
@@ -323,6 +324,7 @@ func Test_annotationPrefix(t *testing.T) {
323
324
324
325
pvc := & corev1.PersistentVolumeClaim {}
325
326
pvc .SetName ("my-pvc" )
327
+ defaultAnnotationPrefix := annotationPrefix
326
328
327
329
tests := []struct {
328
330
name string
@@ -368,6 +370,8 @@ func Test_annotationPrefix(t *testing.T) {
368
370
if got := buildTags (pvc ); ! reflect .DeepEqual (got , tt .want ) {
369
371
t .Errorf ("buildTags() = %v, want %v" , got , tt .want )
370
372
}
373
+ annotationPrefix = defaultAnnotationPrefix
374
+ defaultTags = map [string ]string {}
371
375
})
372
376
}
373
377
}
@@ -484,3 +488,79 @@ func Test_processPersistentVolumeClaim(t *testing.T) {
484
488
}
485
489
486
490
}
491
+
492
+ func Test_templatedTags (t * testing.T ) {
493
+
494
+ pvc := & corev1.PersistentVolumeClaim {}
495
+ pvc .SetName ("my-pvc" )
496
+ pvc .SetNamespace ("my-namespace" )
497
+
498
+ tests := []struct {
499
+ name string
500
+ defaultTags map [string ]string
501
+ annotations map [string ]string
502
+ labels map [string ]string
503
+ want map [string ]string
504
+ }{
505
+ {
506
+ name : "default tag with template" ,
507
+ defaultTags : map [string ]string {"foo" : "{{ .Name }}-{{ .Namespace }}" },
508
+ annotations : map [string ]string {},
509
+ labels : map [string ]string {},
510
+ want : map [string ]string {"foo" : "my-pvc-my-namespace" },
511
+ },
512
+ {
513
+ name : "default tag overwritten with tag template" ,
514
+ defaultTags : map [string ]string {"foo" : "bar" },
515
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Name }}-{{ .Namespace }}\" }" },
516
+ labels : map [string ]string {},
517
+ want : map [string ]string {"foo" : "my-pvc-my-namespace" },
518
+ },
519
+ {
520
+ name : "template using annotation" ,
521
+ defaultTags : map [string ]string {},
522
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Name }}-{{ .Annotations.TeamID }}\" }" , "TeamID" : "1234" },
523
+ labels : map [string ]string {},
524
+ want : map [string ]string {"foo" : "my-pvc-1234" },
525
+ },
526
+ {
527
+ name : "template using label" ,
528
+ defaultTags : map [string ]string {},
529
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Name }}-{{ .Labels.TeamID }}\" }" },
530
+ labels : map [string ]string {"TeamID" : "1234" },
531
+ want : map [string ]string {"foo" : "my-pvc-1234" },
532
+ },
533
+ {
534
+ name : "template using label and annotation" ,
535
+ defaultTags : map [string ]string {},
536
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Name }}-{{ .Labels.TeamID }}\" ,\" bar\" : \" {{ .Name }}-{{ .Annotations.DeptID }}\" }" , "DeptID" : "ABC" },
537
+ labels : map [string ]string {"TeamID" : "1234" },
538
+ want : map [string ]string {"foo" : "my-pvc-1234" , "bar" : "my-pvc-ABC" },
539
+ },
540
+ {
541
+ name : "template using invalid label" ,
542
+ defaultTags : map [string ]string {},
543
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Name }}-{{ .Labels.SomeLabel }}\" }" },
544
+ labels : map [string ]string {"TeamID" : "1234" },
545
+ want : map [string ]string {"foo" : "my-pvc-" },
546
+ },
547
+ {
548
+ name : "template using invalid field" ,
549
+ defaultTags : map [string ]string {},
550
+ annotations : map [string ]string {annotationPrefix + "/tags" : "{\" foo\" : \" {{ .Blah }}-{{ .Labels.TeamID }}\" }" },
551
+ labels : map [string ]string {"TeamID" : "1234" },
552
+ want : map [string ]string {"foo" : "{{ .Blah }}-{{ .Labels.TeamID }}" },
553
+ },
554
+ }
555
+ for _ , tt := range tests {
556
+ t .Run (tt .name , func (t * testing.T ) {
557
+ pvc .SetAnnotations (tt .annotations )
558
+ pvc .SetLabels (tt .labels )
559
+ defaultTags = tt .defaultTags
560
+ if got := buildTags (pvc ); ! reflect .DeepEqual (got , tt .want ) {
561
+ t .Errorf ("buildTags() = %v, want %v" , got , tt .want )
562
+ }
563
+ defaultTags = map [string ]string {}
564
+ })
565
+ }
566
+ }
0 commit comments