@@ -105,7 +105,7 @@ func TestCompareSpecs(t *testing.T) {
105
105
"name" : "nginx" ,
106
106
},
107
107
}
108
- assert .Equal (t , reflect .DeepEqual (merged , mergedExpected ), true )
108
+ assert .True (t , reflect .DeepEqual (merged , mergedExpected ))
109
109
110
110
spec1 = map [string ]interface {}{
111
111
"containers" : map [string ]interface {}{
@@ -196,11 +196,13 @@ func TestConvertPolicyStatusToStringLongMsg(t *testing.T) {
196
196
func TestMergeArraysMustHave (t * testing.T ) {
197
197
t .Parallel ()
198
198
199
- testcases := map [ string ] struct {
199
+ type TestCaseStruct struct {
200
200
desiredList []interface {}
201
201
currentList []interface {}
202
202
expectedList []interface {}
203
- }{
203
+ }
204
+
205
+ testcases := map [string ]TestCaseStruct {
204
206
"merge array with existing element into array preserves array" : {
205
207
[]interface {}{
206
208
map [string ]interface {}{"a" : "apple" , "b" : "boy" },
@@ -287,28 +289,34 @@ func TestMergeArraysMustHave(t *testing.T) {
287
289
},
288
290
}
289
291
292
+ runTest := func (t * testing.T , test TestCaseStruct ) {
293
+ t .Helper ()
294
+
295
+ actualMergedList := mergeArrays (test .desiredList , test .currentList , "musthave" , true )
296
+ assert .Equal (t , fmt .Sprintf ("%+v" , test .expectedList ), fmt .Sprintf ("%+v" , actualMergedList ))
297
+ assert .True (t , checkListsMatch (test .expectedList , actualMergedList ))
298
+ }
299
+
290
300
for testName , test := range testcases {
291
- testName := testName
292
- test := test
301
+ local := test
293
302
294
303
t .Run (testName , func (t * testing.T ) {
295
304
t .Parallel ()
296
-
297
- actualMergedList := mergeArrays (test .desiredList , test .currentList , "musthave" , true )
298
- assert .Equal (t , fmt .Sprintf ("%+v" , test .expectedList ), fmt .Sprintf ("%+v" , actualMergedList ))
299
- assert .True (t , checkListsMatch (test .expectedList , actualMergedList ))
305
+ runTest (t , local )
300
306
})
301
307
}
302
308
}
303
309
304
310
func TestMergeArraysMustOnlyHave (t * testing.T ) {
305
311
t .Parallel ()
306
312
307
- testcases := map [ string ] struct {
313
+ type TestCaseStruct struct {
308
314
desiredList []interface {}
309
315
currentList []interface {}
310
316
expectedList []interface {}
311
- }{
317
+ }
318
+
319
+ testcases := map [string ]TestCaseStruct {
312
320
"merge array with one element into array with two becomes new array" : {
313
321
[]interface {}{
314
322
map [string ]interface {}{"a" : "apple" , "b" : "boy" },
@@ -374,16 +382,20 @@ func TestMergeArraysMustOnlyHave(t *testing.T) {
374
382
},
375
383
}
376
384
385
+ runTest := func (t * testing.T , test TestCaseStruct ) {
386
+ t .Helper ()
387
+
388
+ actualMergedList := mergeArrays (test .desiredList , test .currentList , "mustonlyhave" , true )
389
+ assert .Equal (t , fmt .Sprintf ("%+v" , test .expectedList ), fmt .Sprintf ("%+v" , actualMergedList ))
390
+ assert .True (t , checkListsMatch (test .expectedList , actualMergedList ))
391
+ }
392
+
377
393
for testName , test := range testcases {
378
- testName := testName
379
- test := test
394
+ local := test
380
395
381
396
t .Run (testName , func (t * testing.T ) {
382
397
t .Parallel ()
383
-
384
- actualMergedList := mergeArrays (test .desiredList , test .currentList , "mustonlyhave" , true )
385
- assert .Equal (t , fmt .Sprintf ("%+v" , test .expectedList ), fmt .Sprintf ("%+v" , actualMergedList ))
386
- assert .True (t , checkListsMatch (test .expectedList , actualMergedList ))
398
+ runTest (t , local )
387
399
})
388
400
}
389
401
}
@@ -602,22 +614,22 @@ func TestAddRelatedObject(t *testing.T) {
602
614
related := relatedList [0 ]
603
615
604
616
// get the related object and validate what we added is in the status
605
- assert .True (t , related . Compliant == string (policyv1 .Compliant ))
606
- assert .True (t , related . Reason == "reason" )
607
- assert .True (t , related . Object . APIVersion == scopedGVR .GroupVersion ().String ())
608
- assert .True (t , related .Object .Kind == "ConfigurationPolicy" )
609
- assert .True (t , related .Object .Metadata .Name == name )
610
- assert .True (t , related .Object .Metadata .Namespace == namespace )
617
+ assert .Equal (t , string (policyv1 .Compliant ), related . Compliant )
618
+ assert .Equal (t , "reason" , related . Reason )
619
+ assert .Equal (t , scopedGVR .GroupVersion ().String (), related . Object . APIVersion )
620
+ assert .Equal (t , "ConfigurationPolicy" , related .Object .Kind )
621
+ assert .Equal (t , name , related .Object .Metadata .Name )
622
+ assert .Equal (t , namespace , related .Object .Metadata .Namespace )
611
623
612
624
// add the same object and make sure the existing one is overwritten
613
625
reason = "new"
614
626
compliant = false
615
627
relatedList = addRelatedObjects (compliant , scopedGVR , "ConfigurationPolicy" , namespace , []string {name }, reason , nil )
616
628
related = relatedList [0 ]
617
629
618
- assert .True (t , len ( relatedList ) == 1 )
619
- assert .True (t , related . Compliant == string (policyv1 .NonCompliant ))
620
- assert .True (t , related . Reason == "new" )
630
+ assert .Len (t , relatedList , 1 )
631
+ assert .Equal (t , string (policyv1 .NonCompliant ), related . Compliant )
632
+ assert .Equal (t , "new" , related . Reason )
621
633
622
634
// add a new related object and make sure the entry is appended
623
635
name = "bar"
@@ -626,11 +638,11 @@ func TestAddRelatedObject(t *testing.T) {
626
638
addRelatedObjects (compliant , scopedGVR , "ConfigurationPolicy" , namespace , []string {name }, reason , nil )... ,
627
639
)
628
640
629
- assert .True (t , len ( relatedList ) == 2 )
641
+ assert .Len (t , relatedList , 2 )
630
642
631
643
related = relatedList [1 ]
632
644
633
- assert .True (t , related .Object .Metadata .Name == name )
645
+ assert .Equal (t , name , related .Object .Metadata .Name )
634
646
}
635
647
636
648
func TestUpdatedRelatedObjects (t * testing.T ) {
@@ -670,15 +682,15 @@ func TestUpdatedRelatedObjects(t *testing.T) {
670
682
)
671
683
672
684
r .updatedRelatedObjects (policy , relatedList )
673
- assert .True (t , relatedList [0 ].Object .Metadata .Name == "bar" )
685
+ assert .Equal (t , "bar" , relatedList [0 ].Object .Metadata .Name )
674
686
675
687
// append another object named bar but also with namespace bar
676
688
relatedList = append (relatedList , addRelatedObjects (
677
689
true , scopedGVR , "ConfigurationPolicy" , "bar" , []string {name }, "reason" , nil )... ,
678
690
)
679
691
680
692
r .updatedRelatedObjects (policy , relatedList )
681
- assert .True (t , relatedList [0 ].Object .Metadata .Namespace == "bar" )
693
+ assert .Equal (t , "bar" , relatedList [0 ].Object .Metadata .Namespace )
682
694
683
695
// clear related objects and test sorting with no namespace
684
696
scopedGVR .Namespaced = false
@@ -690,7 +702,7 @@ func TestUpdatedRelatedObjects(t *testing.T) {
690
702
)
691
703
692
704
r .updatedRelatedObjects (policy , relatedList )
693
- assert .True (t , relatedList [0 ].Object .Metadata .Name == "bar" )
705
+ assert .Equal (t , "bar" , relatedList [0 ].Object .Metadata .Name )
694
706
}
695
707
696
708
func TestCreateStatus (t * testing.T ) {
@@ -1023,14 +1035,14 @@ func TestCreateStatus(t *testing.T) {
1023
1035
}
1024
1036
1025
1037
for _ , test := range testcases {
1026
- test := test
1038
+ local := test
1027
1039
1028
- t .Run (test .testName , func (t * testing.T ) {
1029
- compliant , reason , msg := createStatus (test .resourceName , test .namespaceToEvent )
1040
+ t .Run (local .testName , func (t * testing.T ) {
1041
+ compliant , reason , msg := createStatus (local .resourceName , local .namespaceToEvent )
1030
1042
1031
- assert .Equal (t , test .expectedCompliant , compliant )
1032
- assert .Equal (t , test .expectedReason , reason )
1033
- assert .Equal (t , test .expectedMsg , msg )
1043
+ assert .Equal (t , local .expectedCompliant , compliant )
1044
+ assert .Equal (t , local .expectedReason , reason )
1045
+ assert .Equal (t , local .expectedMsg , msg )
1034
1046
})
1035
1047
}
1036
1048
}
@@ -1122,7 +1134,7 @@ func TestShouldEvaluatePolicy(t *testing.T) {
1122
1134
lastEvaluatedTwelveHoursAgo := & sync.Map {}
1123
1135
lastEvaluatedTwelveHoursAgo .Store (policy .UID , twelveHoursAgo )
1124
1136
1125
- tests := [] struct {
1137
+ type TestCaseStruct struct {
1126
1138
testDescription string
1127
1139
lastEvaluated string
1128
1140
lastEvaluatedGeneration int64
@@ -1134,7 +1146,9 @@ func TestShouldEvaluatePolicy(t *testing.T) {
1134
1146
cleanupImmediately bool
1135
1147
finalizers []string
1136
1148
lastEvaluatedCache * sync.Map
1137
- }{
1149
+ }
1150
+
1151
+ tests := []TestCaseStruct {
1138
1152
{
1139
1153
"Just evaluated and the generation is unchanged" ,
1140
1154
inFuture ,
@@ -1410,40 +1424,46 @@ func TestShouldEvaluatePolicy(t *testing.T) {
1410
1424
},
1411
1425
}
1412
1426
1413
- for _ , test := range tests {
1414
- test := test
1427
+ runTest := func ( t * testing. T , test TestCaseStruct ) {
1428
+ t . Helper ()
1415
1429
1416
- t .Run (
1417
- test .testDescription ,
1418
- func (t * testing.T ) {
1419
- t .Parallel ()
1420
- policy := policy .DeepCopy ()
1430
+ policyCopy := policy .DeepCopy ()
1421
1431
1422
- policy .Status .LastEvaluated = test .lastEvaluated
1423
- policy .Status .LastEvaluatedGeneration = test .lastEvaluatedGeneration
1424
- policy .Spec .EvaluationInterval = test .evaluationInterval
1425
- policy .Status .ComplianceState = test .complianceState
1426
- policy .ObjectMeta .DeletionTimestamp = test .deletionTimestamp
1427
- policy .ObjectMeta .Finalizers = test .finalizers
1432
+ policyCopy .Status .LastEvaluated = test .lastEvaluated
1433
+ policyCopy .Status .LastEvaluatedGeneration = test .lastEvaluatedGeneration
1434
+ policyCopy .Spec .EvaluationInterval = test .evaluationInterval
1435
+ policyCopy .Status .ComplianceState = test .complianceState
1436
+ policyCopy .ObjectMeta .DeletionTimestamp = test .deletionTimestamp
1437
+ policyCopy .ObjectMeta .Finalizers = test .finalizers
1428
1438
1429
- r := & ConfigurationPolicyReconciler {
1430
- SelectorReconciler : & fakeSR {},
1431
- lastEvaluatedCache : * test .lastEvaluatedCache , //nolint: govet
1432
- }
1439
+ r := & ConfigurationPolicyReconciler {
1440
+ SelectorReconciler : & fakeSR {},
1441
+ lastEvaluatedCache : * test .lastEvaluatedCache , //nolint: govet
1442
+ }
1433
1443
1434
- actual , actualDuration := r .shouldEvaluatePolicy (policy , test .cleanupImmediately )
1444
+ actual , actualDuration := r .shouldEvaluatePolicy (policyCopy , test .cleanupImmediately )
1435
1445
1436
- if actual != test .expected {
1437
- t .Fatalf ("expected %v but got %v" , test .expected , actual )
1438
- }
1446
+ if actual != test .expected {
1447
+ t .Fatalf ("expected %v but got %v" , test .expected , actual )
1448
+ }
1439
1449
1440
- if test .expectedPositiveDuration && actualDuration <= 0 {
1441
- t .Fatalf ("expected a positive duration but got %v" , actualDuration .String ())
1442
- }
1450
+ if test .expectedPositiveDuration && actualDuration <= 0 {
1451
+ t .Fatalf ("expected a positive duration but got %v" , actualDuration .String ())
1452
+ }
1443
1453
1444
- if ! test .expectedPositiveDuration && actualDuration > 0 {
1445
- t .Fatalf ("expected a zero duration but got %v" , actualDuration .String ())
1446
- }
1454
+ if ! test .expectedPositiveDuration && actualDuration > 0 {
1455
+ t .Fatalf ("expected a zero duration but got %v" , actualDuration .String ())
1456
+ }
1457
+ }
1458
+
1459
+ for _ , test := range tests {
1460
+ local := test
1461
+
1462
+ t .Run (
1463
+ test .testDescription ,
1464
+ func (t * testing.T ) {
1465
+ t .Parallel ()
1466
+ runTest (t , local )
1447
1467
},
1448
1468
)
1449
1469
}
0 commit comments