@@ -106,6 +106,182 @@ var _ = Describe("UpdateStatus", func() {
106106 })
107107 })
108108
109+ Context ("Annotations to Conditions" , func () {
110+ const (
111+ testAnnotationKey = "condition.vmoperator.vmware.com.protected/MyCondition"
112+ testConditionType = "MyConditionType"
113+ testReason = "MyReason"
114+ testMessage = "My message"
115+ )
116+
117+ Context ("When annotation matches the protected condition pattern" , func () {
118+ When ("annotation value has type and status=True" , func () {
119+ BeforeEach (func () {
120+ vmCtx .VM .Annotations = map [string ]string {
121+ testAnnotationKey : testConditionType + ";True" ,
122+ }
123+ })
124+ It ("should set condition to True" , func () {
125+ cond := conditions .Get (vmCtx .VM , testConditionType )
126+ Expect (cond ).ToNot (BeNil ())
127+ Expect (cond .Status ).To (Equal (metav1 .ConditionTrue ))
128+ })
129+ })
130+
131+ When ("annotation value has type and status=False with reason and message" , func () {
132+ BeforeEach (func () {
133+ vmCtx .VM .Annotations = map [string ]string {
134+ testAnnotationKey : testConditionType + ";False;" + testReason + ";" + testMessage ,
135+ }
136+ })
137+ It ("should set condition to False with reason and message" , func () {
138+ cond := conditions .Get (vmCtx .VM , testConditionType )
139+ Expect (cond ).ToNot (BeNil ())
140+ Expect (cond .Status ).To (Equal (metav1 .ConditionFalse ))
141+ Expect (cond .Reason ).To (Equal (testReason ))
142+ Expect (cond .Message ).To (Equal (testMessage ))
143+ })
144+ })
145+
146+ When ("annotation value has type and status=Unknown with reason and message" , func () {
147+ BeforeEach (func () {
148+ vmCtx .VM .Annotations = map [string ]string {
149+ testAnnotationKey : testConditionType + ";Unknown;UnknownReason;Unknown message" ,
150+ }
151+ })
152+ It ("should set condition to Unknown with reason and message" , func () {
153+ cond := conditions .Get (vmCtx .VM , testConditionType )
154+ Expect (cond ).ToNot (BeNil ())
155+ Expect (cond .Status ).To (Equal (metav1 .ConditionUnknown ))
156+ Expect (cond .Reason ).To (Equal ("UnknownReason" ))
157+ Expect (cond .Message ).To (Equal ("Unknown message" ))
158+ })
159+ })
160+
161+ When ("annotation value has type and unrecognized status" , func () {
162+ BeforeEach (func () {
163+ vmCtx .VM .Annotations = map [string ]string {
164+ testAnnotationKey : testConditionType + ";InvalidStatus;" + testReason + ";" + testMessage ,
165+ }
166+ })
167+ It ("should set condition to Unknown" , func () {
168+ cond := conditions .Get (vmCtx .VM , testConditionType )
169+ Expect (cond ).ToNot (BeNil ())
170+ Expect (cond .Status ).To (Equal (metav1 .ConditionUnknown ))
171+ })
172+ })
173+
174+ When ("annotation value has type but empty status" , func () {
175+ BeforeEach (func () {
176+ vmCtx .VM .Annotations = map [string ]string {
177+ testAnnotationKey : testConditionType + ";;" + testReason + ";" + testMessage ,
178+ }
179+ })
180+ It ("should set condition to Unknown" , func () {
181+ cond := conditions .Get (vmCtx .VM , testConditionType )
182+ Expect (cond ).ToNot (BeNil ())
183+ Expect (cond .Status ).To (Equal (metav1 .ConditionUnknown ))
184+ })
185+ })
186+
187+ When ("annotation value has only type" , func () {
188+ BeforeEach (func () {
189+ vmCtx .VM .Annotations = map [string ]string {
190+ testAnnotationKey : testConditionType ,
191+ }
192+ })
193+ It ("should set condition to Unknown" , func () {
194+ cond := conditions .Get (vmCtx .VM , testConditionType )
195+ Expect (cond ).ToNot (BeNil ())
196+ Expect (cond .Status ).To (Equal (metav1 .ConditionUnknown ))
197+ })
198+ })
199+
200+ When ("annotation value is empty" , func () {
201+ BeforeEach (func () {
202+ vmCtx .VM .Annotations = map [string ]string {
203+ testAnnotationKey : "" ,
204+ }
205+ })
206+ It ("should not set any condition" , func () {
207+ cond := conditions .Get (vmCtx .VM , "" )
208+ Expect (cond ).To (BeNil ())
209+ })
210+ })
211+
212+ When ("annotation value has no type (empty type)" , func () {
213+ BeforeEach (func () {
214+ vmCtx .VM .Annotations = map [string ]string {
215+ testAnnotationKey : ";True;" + testReason + ";" + testMessage ,
216+ }
217+ })
218+ It ("should not set a condition for empty type" , func () {
219+ // Since type is empty, no condition with empty type should be set
220+ // The function checks if t != "" before setting conditions
221+ cond := conditions .Get (vmCtx .VM , "" )
222+ Expect (cond ).To (BeNil ())
223+ // But Created condition should still exist
224+ cond = conditions .Get (vmCtx .VM , vmopv1 .VirtualMachineConditionCreated )
225+ Expect (cond ).ToNot (BeNil ())
226+ })
227+ })
228+
229+ When ("multiple protected condition annotations exist" , func () {
230+ BeforeEach (func () {
231+ vmCtx .VM .Annotations = map [string ]string {
232+ "condition.vmoperator.vmware.com.protected/Condition1" : "Type1;True" ,
233+ "condition.vmoperator.vmware.com.protected/Condition2" : "Type2;False;Reason2;Message2" ,
234+ "condition.vmoperator.vmware.com.protected/Condition3" : "Type3;Unknown;Reason3;Message3" ,
235+ }
236+ })
237+ It ("should set all conditions appropriately" , func () {
238+ cond1 := conditions .Get (vmCtx .VM , "Type1" )
239+ Expect (cond1 ).ToNot (BeNil ())
240+ Expect (cond1 .Status ).To (Equal (metav1 .ConditionTrue ))
241+
242+ cond2 := conditions .Get (vmCtx .VM , "Type2" )
243+ Expect (cond2 ).ToNot (BeNil ())
244+ Expect (cond2 .Status ).To (Equal (metav1 .ConditionFalse ))
245+ Expect (cond2 .Reason ).To (Equal ("Reason2" ))
246+ Expect (cond2 .Message ).To (Equal ("Message2" ))
247+
248+ cond3 := conditions .Get (vmCtx .VM , "Type3" )
249+ Expect (cond3 ).ToNot (BeNil ())
250+ Expect (cond3 .Status ).To (Equal (metav1 .ConditionUnknown ))
251+ Expect (cond3 .Reason ).To (Equal ("Reason3" ))
252+ Expect (cond3 .Message ).To (Equal ("Message3" ))
253+ })
254+ })
255+ })
256+
257+ Context ("When annotation does not match the protected condition pattern" , func () {
258+ When ("annotation has different prefix" , func () {
259+ BeforeEach (func () {
260+ vmCtx .VM .Annotations = map [string ]string {
261+ "other.annotation/MyCondition" : testConditionType + ";True" ,
262+ }
263+ })
264+ It ("should not set any condition from this annotation" , func () {
265+ cond := conditions .Get (vmCtx .VM , testConditionType )
266+ Expect (cond ).To (BeNil ())
267+ })
268+ })
269+
270+ When ("no annotations exist" , func () {
271+ BeforeEach (func () {
272+ vmCtx .VM .Annotations = nil
273+ })
274+ It ("should have default conditions set by ReconcileStatus" , func () {
275+ // ReconcileStatus sets multiple conditions including Created and ReconcileReady
276+ // Just verify the Created condition exists and no annotation-based conditions
277+ cond := conditions .Get (vmCtx .VM , vmopv1 .VirtualMachineConditionCreated )
278+ Expect (cond ).ToNot (BeNil ())
279+ Expect (cond .Status ).To (Equal (metav1 .ConditionTrue ))
280+ })
281+ })
282+ })
283+ })
284+
109285 Context ("Network" , func () {
110286
111287 Context ("PrimaryIP" , func () {
0 commit comments