@@ -4,9 +4,11 @@ import (
4
4
"fmt"
5
5
"testing"
6
6
7
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
+
9
+ "github.com/stretchr/testify/assert"
7
10
"github.com/stretchr/testify/require"
8
11
corev1 "k8s.io/api/core/v1"
9
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
12
)
11
13
12
14
func TestSetRequirementStatus (t * testing.T ) {
@@ -282,3 +284,93 @@ func TestSupports(t *testing.T) {
282
284
})
283
285
}
284
286
}
287
+
288
+ func TestSetPhaseWithConditions (t * testing.T ) {
289
+ tests := []struct {
290
+ description string
291
+ limit int
292
+ currentLength int
293
+ startIndex int
294
+ }{
295
+ {
296
+ // The original list is already at limit (length == limit).
297
+ // We expect the oldest element ( item at 0 index) to be removed.
298
+ description : "TestSetPhaseWithConditionsLengthAtLimit" ,
299
+ limit : ConditionsLengthLimit ,
300
+ currentLength : ConditionsLengthLimit ,
301
+
302
+ // The first element from the original list should be dropped from
303
+ // the new list.
304
+ startIndex : 1 ,
305
+ },
306
+ {
307
+ // The original list is 1 length away from limit.
308
+ // We don't expect the list to be trimmed.
309
+ description : "TestSetPhaseWithConditionsLengthBelowLimit" ,
310
+ limit : ConditionsLengthLimit ,
311
+ currentLength : ConditionsLengthLimit - 1 ,
312
+
313
+ // Everything in the original list should be preserved.
314
+ startIndex : 0 ,
315
+ },
316
+ {
317
+ // The original list has N more element(s) than allowed limit.
318
+ // We expect (N + 1) oldest elements to be deleted to keep the list
319
+ // at limit.
320
+ description : "TestSetPhaseWithConditionsLimitExceeded" ,
321
+ limit : ConditionsLengthLimit ,
322
+ currentLength : ConditionsLengthLimit + 10 ,
323
+
324
+ // The first 11 (N=10 plus 1 to make room for the newly added
325
+ // condition) elements from the original list should be dropped.
326
+ startIndex : 11 ,
327
+ },
328
+ }
329
+
330
+ for _ , tt := range tests {
331
+ t .Run (tt .description , func (t * testing.T ) {
332
+ csv := ClusterServiceVersion {}
333
+ csv .Status .Conditions = helperNewConditions (tt .currentLength )
334
+
335
+ now := metav1 .Now ()
336
+
337
+ oldConditionsWant := csv .Status .Conditions [tt .startIndex :]
338
+ lastAddedConditionWant := ClusterServiceVersionCondition {
339
+ Phase : ClusterServiceVersionPhase ("Pending" ),
340
+ LastTransitionTime : now ,
341
+ LastUpdateTime : now ,
342
+ Message : "message" ,
343
+ Reason : ConditionReason ("reason" ),
344
+ }
345
+
346
+ csv .SetPhase ("Pending" , "reason" , "message" , now )
347
+
348
+ conditionsGot := csv .Status .Conditions
349
+ assert .Equal (t , tt .limit , len (conditionsGot ))
350
+
351
+ oldConditionsGot := conditionsGot [0 : len (conditionsGot )- 1 ]
352
+ assert .EqualValues (t , oldConditionsWant , oldConditionsGot )
353
+
354
+ lastAddedConditionGot := conditionsGot [len (conditionsGot )- 1 ]
355
+ assert .Equal (t , lastAddedConditionWant , lastAddedConditionGot )
356
+ })
357
+ }
358
+ }
359
+
360
+ func helperNewConditions (count int ) []ClusterServiceVersionCondition {
361
+ conditions := make ([]ClusterServiceVersionCondition , 0 )
362
+
363
+ for i := 1 ; i <= count ; i ++ {
364
+ now := metav1 .Now ()
365
+ condition := ClusterServiceVersionCondition {
366
+ Phase : ClusterServiceVersionPhase (fmt .Sprintf ("phase-%d" , i )),
367
+ LastTransitionTime : now ,
368
+ LastUpdateTime : now ,
369
+ Message : fmt .Sprintf ("message-%d" , i ),
370
+ Reason : ConditionReason (fmt .Sprintf ("reason-%d" , i )),
371
+ }
372
+ conditions = append (conditions , condition )
373
+ }
374
+
375
+ return conditions
376
+ }
0 commit comments