@@ -509,35 +509,36 @@ func TestFilterLabels(t *testing.T) {
509
509
func TestCreatePatches (t * testing.T ) {
510
510
Convey ("When creating JSON patches" , t , func () {
511
511
existingItems := map [string ]string {"key-1" : "val-1" , "key-2" : "val-2" , "key-3" : "val-3" }
512
+ overwriteKeys := true
512
513
jsonPath := "/root"
513
514
514
- Convey ("When when there are neither itmes to remoe nor to add or update" , func () {
515
- p := createPatches ([]string {"foo" , "bar" }, existingItems , map [string ]string {}, jsonPath )
515
+ Convey ("When there are neither itmes to remoe nor to add or update" , func () {
516
+ p := createPatches ([]string {"foo" , "bar" }, existingItems , map [string ]string {}, jsonPath , overwriteKeys )
516
517
So (len (p ), ShouldEqual , 0 )
517
518
})
518
519
519
- Convey ("When when there are itmes to remoe but none to add or update" , func () {
520
- p := createPatches ([]string {"key-2" , "key-3" , "foo" }, existingItems , map [string ]string {}, jsonPath )
520
+ Convey ("When there are itmes to remoe but none to add or update" , func () {
521
+ p := createPatches ([]string {"key-2" , "key-3" , "foo" }, existingItems , map [string ]string {}, jsonPath , overwriteKeys )
521
522
expected := []utils.JsonPatch {
522
523
utils .NewJsonPatch ("remove" , jsonPath , "key-2" , "" ),
523
524
utils .NewJsonPatch ("remove" , jsonPath , "key-3" , "" ),
524
525
}
525
526
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
526
527
})
527
528
528
- Convey ("When when there are no itmes to remove but new items to add" , func () {
529
+ Convey ("When there are no itmes to remove but new items to add" , func () {
529
530
newItems := map [string ]string {"new-key" : "new-val" , "key-1" : "new-1" }
530
- p := createPatches ([]string {"key-1" }, existingItems , newItems , jsonPath )
531
+ p := createPatches ([]string {"key-1" }, existingItems , newItems , jsonPath , overwriteKeys )
531
532
expected := []utils.JsonPatch {
532
533
utils .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
533
534
utils .NewJsonPatch ("replace" , jsonPath , "key-1" , newItems ["key-1" ]),
534
535
}
535
536
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
536
537
})
537
538
538
- Convey ("When when there are items to remove add and update" , func () {
539
+ Convey ("When there are items to remove add and update" , func () {
539
540
newItems := map [string ]string {"new-key" : "new-val" , "key-2" : "new-2" , "key-4" : "val-4" }
540
- p := createPatches ([]string {"key-1" , "key-2" , "key-3" , "foo" }, existingItems , newItems , jsonPath )
541
+ p := createPatches ([]string {"key-1" , "key-2" , "key-3" , "foo" }, existingItems , newItems , jsonPath , overwriteKeys )
541
542
expected := []utils.JsonPatch {
542
543
utils .NewJsonPatch ("add" , jsonPath , "new-key" , newItems ["new-key" ]),
543
544
utils .NewJsonPatch ("add" , jsonPath , "key-4" , newItems ["key-4" ]),
@@ -547,6 +548,16 @@ func TestCreatePatches(t *testing.T) {
547
548
}
548
549
So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
549
550
})
551
+
552
+ Convey ("When overwrite of keys is denied and there is already an existant key" , func () {
553
+ overwriteKeys = false
554
+ newItems := map [string ]string {"key-1" : "new-2" , "key-4" : "val-4" }
555
+ p := createPatches ([]string {}, existingItems , newItems , jsonPath , overwriteKeys )
556
+ expected := []utils.JsonPatch {
557
+ utils .NewJsonPatch ("add" , jsonPath , "key-4" , newItems ["key-4" ]),
558
+ }
559
+ So (sortJsonPatches (p ), ShouldResemble , sortJsonPatches (expected ))
560
+ })
550
561
})
551
562
}
552
563
@@ -896,3 +907,60 @@ func TestGetDynamicValue(t *testing.T) {
896
907
})
897
908
}
898
909
}
910
+
911
+ func TestFilterTaints (t * testing.T ) {
912
+ testcases := []struct {
913
+ name string
914
+ taints []corev1.Taint
915
+ maxTaints int
916
+ expectedResult []corev1.Taint
917
+ }{
918
+ {
919
+ name : "no restriction on the number of taints" ,
920
+ taints : []corev1.Taint {
921
+ {
922
+ Key : "feature.node.kubernetes.io/key1" ,
923
+ Value : "dummy" ,
924
+ Effect : corev1 .TaintEffectNoSchedule ,
925
+ },
926
+ },
927
+ maxTaints : 0 ,
928
+ expectedResult : []corev1.Taint {
929
+ {
930
+ Key : "feature.node.kubernetes.io/key1" ,
931
+ Value : "dummy" ,
932
+ Effect : corev1 .TaintEffectNoSchedule ,
933
+ },
934
+ },
935
+ },
936
+ {
937
+ name : "max of 1 Taint should be generated" ,
938
+ taints : []corev1.Taint {
939
+ {
940
+ Key : "feature.node.kubernetes.io/key1" ,
941
+ Value : "dummy" ,
942
+ Effect : corev1 .TaintEffectNoSchedule ,
943
+ },
944
+ {
945
+ Key : "feature.node.kubernetes.io/key2" ,
946
+ Value : "dummy" ,
947
+ Effect : corev1 .TaintEffectNoSchedule ,
948
+ },
949
+ },
950
+ maxTaints : 1 ,
951
+ expectedResult : []corev1.Taint {},
952
+ },
953
+ }
954
+
955
+ mockMaster := newFakeMaster (nil )
956
+
957
+ for _ , tc := range testcases {
958
+ t .Run (tc .name , func (t * testing.T ) {
959
+ mockMaster .config .Restrictions .MaxTaintsPerCR = tc .maxTaints
960
+ res := mockMaster .filterTaints (tc .taints )
961
+ Convey ("The expected number of taints should be correct" , t , func () {
962
+ So (len (res ), ShouldEqual , len (tc .expectedResult ))
963
+ })
964
+ })
965
+ }
966
+ }
0 commit comments