@@ -103,9 +103,10 @@ func TestIsPending(t *testing.T) {
103103
104104func TestMarkAsPending (t * testing.T ) {
105105 tests := []struct {
106- name string
107- obj client.Object
108- hook runtimecatalog.Hook
106+ name string
107+ obj client.Object
108+ hook runtimecatalog.Hook
109+ expectedAnnotation string
109110 }{
110111 {
111112 name : "should add the marker if not already marked as pending" ,
@@ -115,7 +116,8 @@ func TestMarkAsPending(t *testing.T) {
115116 Namespace : "test-ns" ,
116117 },
117118 },
118- hook : runtimehooksv1 .AfterClusterUpgrade ,
119+ hook : runtimehooksv1 .AfterClusterUpgrade ,
120+ expectedAnnotation : "AfterClusterUpgrade" ,
119121 },
120122 {
121123 name : "should add the marker if not already marked as pending - other hooks are present" ,
@@ -128,7 +130,8 @@ func TestMarkAsPending(t *testing.T) {
128130 },
129131 },
130132 },
131- hook : runtimehooksv1 .AfterClusterUpgrade ,
133+ hook : runtimehooksv1 .AfterClusterUpgrade ,
134+ expectedAnnotation : "AfterClusterUpgrade,AfterControlPlaneUpgrade" ,
132135 },
133136 {
134137 name : "should pass if the marker is already marked as pending" ,
@@ -141,7 +144,22 @@ func TestMarkAsPending(t *testing.T) {
141144 },
142145 },
143146 },
144- hook : runtimehooksv1 .AfterClusterUpgrade ,
147+ hook : runtimehooksv1 .AfterClusterUpgrade ,
148+ expectedAnnotation : "AfterClusterUpgrade" ,
149+ },
150+ {
151+ name : "should pass if the marker is already marked as pending and remove empty string entry" ,
152+ obj : & corev1.ConfigMap {
153+ ObjectMeta : metav1.ObjectMeta {
154+ Name : "test-cluster" ,
155+ Namespace : "test-ns" ,
156+ Annotations : map [string ]string {
157+ runtimev1 .PendingHooksAnnotation : ",AfterClusterUpgrade" ,
158+ },
159+ },
160+ },
161+ hook : runtimehooksv1 .AfterClusterUpgrade ,
162+ expectedAnnotation : "AfterClusterUpgrade" ,
145163 },
146164 }
147165
@@ -153,15 +171,17 @@ func TestMarkAsPending(t *testing.T) {
153171 g .Expect (MarkAsPending (ctx , fakeClient , tt .obj , tt .hook )).To (Succeed ())
154172 annotations := tt .obj .GetAnnotations ()
155173 g .Expect (annotations [runtimev1 .PendingHooksAnnotation ]).To (ContainSubstring (runtimecatalog .HookName (tt .hook )))
174+ g .Expect (annotations [runtimev1 .PendingHooksAnnotation ]).To (Equal (tt .expectedAnnotation ))
156175 })
157176 }
158177}
159178
160179func TestMarkAsDone (t * testing.T ) {
161180 tests := []struct {
162- name string
163- obj client.Object
164- hook runtimecatalog.Hook
181+ name string
182+ obj client.Object
183+ hook runtimecatalog.Hook
184+ expectedAnnotation string
165185 }{
166186 {
167187 name : "should pass if the marker is not already present" ,
@@ -171,7 +191,8 @@ func TestMarkAsDone(t *testing.T) {
171191 Namespace : "test-ns" ,
172192 },
173193 },
174- hook : runtimehooksv1 .AfterClusterUpgrade ,
194+ hook : runtimehooksv1 .AfterClusterUpgrade ,
195+ expectedAnnotation : "" ,
175196 },
176197 {
177198 name : "should remove if the marker is already present" ,
@@ -184,7 +205,8 @@ func TestMarkAsDone(t *testing.T) {
184205 },
185206 },
186207 },
187- hook : runtimehooksv1 .AfterClusterUpgrade ,
208+ hook : runtimehooksv1 .AfterClusterUpgrade ,
209+ expectedAnnotation : "" ,
188210 },
189211 {
190212 name : "should remove if the marker is already present among multiple hooks" ,
@@ -197,7 +219,22 @@ func TestMarkAsDone(t *testing.T) {
197219 },
198220 },
199221 },
200- hook : runtimehooksv1 .AfterClusterUpgrade ,
222+ hook : runtimehooksv1 .AfterClusterUpgrade ,
223+ expectedAnnotation : "AfterControlPlaneUpgrade" ,
224+ },
225+ {
226+ name : "should remove empty string entry" ,
227+ obj : & corev1.ConfigMap {
228+ ObjectMeta : metav1.ObjectMeta {
229+ Name : "test-cluster" ,
230+ Namespace : "test-ns" ,
231+ Annotations : map [string ]string {
232+ runtimev1 .PendingHooksAnnotation : ",AfterClusterUpgrade,AfterControlPlaneUpgrade" ,
233+ },
234+ },
235+ },
236+ hook : runtimehooksv1 .AfterClusterUpgrade ,
237+ expectedAnnotation : "AfterControlPlaneUpgrade" ,
201238 },
202239 }
203240
@@ -209,6 +246,7 @@ func TestMarkAsDone(t *testing.T) {
209246 g .Expect (MarkAsDone (ctx , fakeClient , tt .obj , tt .hook )).To (Succeed ())
210247 annotations := tt .obj .GetAnnotations ()
211248 g .Expect (annotations [runtimev1 .PendingHooksAnnotation ]).NotTo (ContainSubstring (runtimecatalog .HookName (tt .hook )))
249+ g .Expect (annotations [runtimev1 .PendingHooksAnnotation ]).To (Equal (tt .expectedAnnotation ))
212250 })
213251 }
214252}
0 commit comments