@@ -209,6 +209,96 @@ func TestSetLastTransitionTime(t *testing.T) {
209
209
}
210
210
}
211
211
212
+ func TestSetWithCustomLastTransitionTime (t * testing.T ) {
213
+ x := metav1 .Date (2012 , time .January , 1 , 12 , 15 , 30 , 5e8 , time .UTC )
214
+ y := metav1 .Date (2012 , time .January , 2 , 12 , 15 , 30 , 5e8 , time .UTC )
215
+
216
+ foo := FalseCondition ("foo" , "reason foo" , clusterv1 .ConditionSeverityInfo , "message foo" )
217
+ fooWithBarMessage := FalseCondition ("foo" , "reason foo" , clusterv1 .ConditionSeverityInfo , "message bar" )
218
+ fooWithLastTransitionTime := FalseCondition ("foo" , "reason foo" , clusterv1 .ConditionSeverityInfo , "message foo" )
219
+ fooWithLastTransitionTime .LastTransitionTime = x
220
+ fooWithLastTransitionTimeWithBarMessage := FalseCondition ("foo" , "reason foo" , clusterv1 .ConditionSeverityInfo , "message bar" )
221
+ fooWithLastTransitionTimeWithBarMessage .LastTransitionTime = y
222
+
223
+ fooWithAnotherState := TrueCondition ("foo" )
224
+ fooWithAnotherStateWithLastTransitionTime := TrueCondition ("foo" )
225
+ fooWithAnotherStateWithLastTransitionTime .LastTransitionTime = y
226
+
227
+ tests := []struct {
228
+ name string
229
+ to Setter
230
+ new * clusterv1.Condition
231
+ LastTransitionTimeCheck func (* WithT , metav1.Time )
232
+ }{
233
+ {
234
+ name : "Set a condition that does not exists should set the last transition time if not defined" ,
235
+ to : setterWithConditions (),
236
+ new : foo ,
237
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
238
+ g .Expect (lastTransitionTime ).ToNot (BeZero ())
239
+ },
240
+ },
241
+ {
242
+ name : "Set a condition that does not exists should preserve the last transition time if defined" ,
243
+ to : setterWithConditions (),
244
+ new : fooWithLastTransitionTime ,
245
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
246
+ g .Expect (lastTransitionTime ).To (Equal (x ))
247
+ },
248
+ },
249
+ {
250
+ name : "Set a condition that already exists with the same state should preserves the last transition time" ,
251
+ to : setterWithConditions (fooWithLastTransitionTime ),
252
+ new : foo ,
253
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
254
+ g .Expect (lastTransitionTime ).To (Equal (x ))
255
+ },
256
+ },
257
+ {
258
+ name : "Set a condition that already exists but with different state should changes the last transition time" ,
259
+ to : setterWithConditions (fooWithLastTransitionTime ),
260
+ new : fooWithAnotherState ,
261
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
262
+ g .Expect (lastTransitionTime ).ToNot (Equal (x ))
263
+ },
264
+ },
265
+ {
266
+ name : "Set a condition that already exists but with different state should preserve the last transition time if defined" ,
267
+ to : setterWithConditions (fooWithLastTransitionTime ),
268
+ new : fooWithAnotherStateWithLastTransitionTime ,
269
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
270
+ g .Expect (lastTransitionTime ).To (Equal (y ))
271
+ },
272
+ },
273
+ {
274
+ name : "Set a condition that already exists but with different Message should preserve the last transition time" ,
275
+ to : setterWithConditions (fooWithLastTransitionTime ),
276
+ new : fooWithBarMessage ,
277
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
278
+ g .Expect (lastTransitionTime ).To (Equal (x ))
279
+ },
280
+ },
281
+ {
282
+ name : "Set a condition that already exists, with different state but same Status should ignore the last transition even if defined" ,
283
+ to : setterWithConditions (fooWithLastTransitionTime ),
284
+ new : fooWithLastTransitionTimeWithBarMessage ,
285
+ LastTransitionTimeCheck : func (g * WithT , lastTransitionTime metav1.Time ) {
286
+ g .Expect (lastTransitionTime ).To (Equal (x ))
287
+ },
288
+ },
289
+ }
290
+
291
+ for _ , tt := range tests {
292
+ t .Run (tt .name , func (t * testing.T ) {
293
+ g := NewWithT (t )
294
+
295
+ SetWithCustomLastTransitionTime (tt .to , tt .new )
296
+
297
+ tt .LastTransitionTimeCheck (g , Get (tt .to , "foo" ).LastTransitionTime )
298
+ })
299
+ }
300
+ }
301
+
212
302
func TestMarkMethods (t * testing.T ) {
213
303
g := NewWithT (t )
214
304
0 commit comments