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