@@ -17,6 +17,8 @@ limitations under the License.
17
17
package handler_test
18
18
19
19
import (
20
+ "fmt"
21
+
20
22
. "github.com/onsi/ginkgo"
21
23
. "github.com/onsi/gomega"
22
24
appsv1 "k8s.io/api/apps/v1"
@@ -39,12 +41,22 @@ var _ = Describe("Eventhandler", func() {
39
41
var instance handler.EnqueueRequestForObject
40
42
var pod * corev1.Pod
41
43
var mapper meta.RESTMapper
44
+ podTypeAnnotations := fmt .Sprintf ("%v.%v" , "Pods" , "core" )
42
45
t := true
43
46
BeforeEach (func () {
44
47
q = controllertest.Queue {Interface : workqueue .New ()}
45
48
pod = & corev1.Pod {
46
- ObjectMeta : metav1.ObjectMeta {Namespace : "biz" , Name : "baz" },
49
+ ObjectMeta : metav1.ObjectMeta {
50
+ Namespace : "biz" ,
51
+ Name : "baz" ,
52
+ Annotations : map [string ]string {
53
+ handler .NamespaceAnnotation : "biz" ,
54
+ handler .NameAnnotation : "baz" ,
55
+ handler .TypeAnnotation : podTypeAnnotations ,
56
+ },
57
+ },
47
58
}
59
+
48
60
Expect (cfg ).NotTo (BeNil ())
49
61
50
62
var err error
@@ -950,4 +962,192 @@ var _ = Describe("Eventhandler", func() {
950
962
close (done )
951
963
})
952
964
})
965
+
966
+ Describe ("EnqueueRequestForAnnotation" , func () {
967
+ It ("should enqueue a Request with the Annotation of the object in the CreateEvent." , func () {
968
+ instance := handler.EnqueueRequestForAnnotation {Type : podTypeAnnotations }
969
+
970
+ evt := event.CreateEvent {
971
+ Object : pod ,
972
+ Meta : pod .GetObjectMeta (),
973
+ }
974
+ instance .Create (evt , q )
975
+ Expect (q .Len ()).To (Equal (1 ))
976
+
977
+ i , _ := q .Get ()
978
+ Expect (i ).To (Equal (reconcile.Request {
979
+ NamespacedName : types.NamespacedName {Namespace : "biz" , Name : "baz" }}))
980
+ })
981
+
982
+ It ("should enqueue a Request with the annotation of the object in the DeleteEvent." , func () {
983
+ instance := handler.EnqueueRequestForAnnotation {Type : podTypeAnnotations }
984
+
985
+ evt := event.DeleteEvent {
986
+ Object : pod ,
987
+ Meta : pod .GetObjectMeta (),
988
+ }
989
+ instance .Delete (evt , q )
990
+ Expect (q .Len ()).To (Equal (1 ))
991
+
992
+ i , _ := q .Get ()
993
+ Expect (i ).To (Equal (reconcile.Request {
994
+ NamespacedName : types.NamespacedName {Namespace : "biz" , Name : "baz" }}))
995
+ })
996
+
997
+ It ("should enqueue a Request with the Annotations of the object in the UpdateEvent." , func () {
998
+ newPod := pod .DeepCopy ()
999
+ newPod .Name = pod .Name + "2"
1000
+ newPod .Namespace = pod .Namespace + "2"
1001
+
1002
+ instance := handler.EnqueueRequestForAnnotation {Type : podTypeAnnotations }
1003
+
1004
+ evt := event.UpdateEvent {
1005
+ ObjectOld : pod ,
1006
+ MetaOld : pod .GetObjectMeta (),
1007
+ ObjectNew : newPod ,
1008
+ MetaNew : newPod .GetObjectMeta (),
1009
+ }
1010
+ instance .Update (evt , q )
1011
+ Expect (q .Len ()).To (Equal (1 ))
1012
+
1013
+ i , _ := q .Get ()
1014
+ Expect (i ).To (Equal (reconcile.Request {
1015
+ NamespacedName : types.NamespacedName {Namespace : "biz" , Name : "baz" }}))
1016
+ })
1017
+
1018
+ It ("should enqueue a Request with the Annotation of the object in the GenericEvent." , func () {
1019
+ instance := handler.EnqueueRequestForAnnotation {Type : podTypeAnnotations }
1020
+
1021
+ evt := event.GenericEvent {
1022
+ Object : pod ,
1023
+ Meta : pod .GetObjectMeta (),
1024
+ }
1025
+ instance .Generic (evt , q )
1026
+ Expect (q .Len ()).To (Equal (1 ))
1027
+
1028
+ i , _ := q .Get ()
1029
+ Expect (i ).To (Equal (reconcile.Request {
1030
+ NamespacedName : types.NamespacedName {Namespace : "biz" , Name : "baz" }}))
1031
+ })
1032
+
1033
+ It ("should not enqueue a Request if there are no annotations matching with the object." , func () {
1034
+ var repl * appsv1.ReplicaSet
1035
+
1036
+ repl = & appsv1.ReplicaSet {
1037
+ ObjectMeta : metav1.ObjectMeta {Namespace : "foo" , Name : "faz" },
1038
+ }
1039
+
1040
+ instance := handler.EnqueueRequestForAnnotation {Type : "ReplicaSet.apps" }
1041
+
1042
+ evt := event.CreateEvent {
1043
+ Object : repl ,
1044
+ Meta : repl .GetObjectMeta (),
1045
+ }
1046
+
1047
+ instance .Create (evt , q )
1048
+ Expect (q .Len ()).To (Equal (0 ))
1049
+
1050
+ })
1051
+
1052
+ It ("should not enqueue a Request if there are no NamespacedNameAnnotation matching Namespace and Name." , func () {
1053
+ var repl * appsv1.ReplicaSet
1054
+
1055
+ repl = & appsv1.ReplicaSet {
1056
+ ObjectMeta : metav1.ObjectMeta {
1057
+ Namespace : "foo" ,
1058
+ Name : "faz" ,
1059
+ Annotations : map [string ]string {
1060
+ handler .TypeAnnotation : "ReplicaSet.apps" ,
1061
+ },
1062
+ },
1063
+ }
1064
+
1065
+ instance := handler.EnqueueRequestForAnnotation {Type : "ReplicaSet.apps" }
1066
+ evt := event.CreateEvent {
1067
+ Object : repl ,
1068
+ Meta : repl .GetObjectMeta (),
1069
+ }
1070
+
1071
+ instance .Create (evt , q )
1072
+ Expect (q .Len ()).To (Equal (0 ))
1073
+
1074
+ })
1075
+
1076
+ It ("should not enqueue a Request if there are no TypeAnnotation matching Group and Kind." , func () {
1077
+ var repl * appsv1.ReplicaSet
1078
+
1079
+ repl = & appsv1.ReplicaSet {
1080
+ ObjectMeta : metav1.ObjectMeta {
1081
+ Namespace : "foo" ,
1082
+ Name : "faz" ,
1083
+
1084
+ Annotations : map [string ]string {
1085
+ handler .NamespaceAnnotation : "foo" ,
1086
+ handler .NameAnnotation : "faz" ,
1087
+ },
1088
+ },
1089
+ }
1090
+
1091
+ instance := handler.EnqueueRequestForAnnotation {Type : "ReplicaSet.apps" }
1092
+ evt := event.CreateEvent {
1093
+ Object : repl ,
1094
+ Meta : repl .GetObjectMeta (),
1095
+ }
1096
+
1097
+ instance .Create (evt , q )
1098
+ Expect (q .Len ()).To (Equal (0 ))
1099
+
1100
+ })
1101
+
1102
+ It ("should enqueue a Request for a object that is cluster scoped which has the annotations" , func () {
1103
+
1104
+ var nd * corev1.Node
1105
+
1106
+ nd = & corev1.Node {
1107
+ ObjectMeta : metav1.ObjectMeta {
1108
+ Name : "node-1" ,
1109
+ Annotations : map [string ]string {
1110
+ handler .NameAnnotation : "node-1" ,
1111
+ handler .TypeAnnotation : "Node.core" ,
1112
+ },
1113
+ },
1114
+ }
1115
+
1116
+ instance := handler.EnqueueRequestForAnnotation {Type : "Node.core" }
1117
+ evt := event.CreateEvent {
1118
+ Object : nd ,
1119
+ Meta : nd .GetObjectMeta (),
1120
+ }
1121
+ instance .Create (evt , q )
1122
+ Expect (q .Len ()).To (Equal (1 ))
1123
+
1124
+ i , _ := q .Get ()
1125
+ Expect (i ).To (Equal (reconcile.Request {
1126
+ NamespacedName : types.NamespacedName {Namespace : "" , Name : "node-1" }}))
1127
+
1128
+ })
1129
+
1130
+ It ("should not enqueue a Request for a object that is cluster scoped which has not the annotations" , func () {
1131
+
1132
+ var nd * corev1.Node
1133
+
1134
+ nd = & corev1.Node {
1135
+ ObjectMeta : metav1.ObjectMeta {Name : "node-1" },
1136
+ }
1137
+
1138
+ instance := handler.EnqueueRequestForAnnotation {Type : "Node.core" }
1139
+ evt := event.CreateEvent {
1140
+ Object : nd ,
1141
+ Meta : nd .GetObjectMeta (),
1142
+ }
1143
+ instance .Create (evt , q )
1144
+ Expect (q .Len ()).To (Equal (0 ))
1145
+
1146
+ })
1147
+
1148
+ // TODO:Do we need to test the cases where:
1149
+ // the old pod has the annotations and the new pod does not
1150
+ // the new pod has the annotations and the old pod does not
1151
+ })
1152
+
953
1153
})
0 commit comments