Skip to content

Commit d3b0f1d

Browse files
Update golangci-lint
update these to latest github.com/daixiang0/[email protected] above sigs.k8s.io/kustomize/kustomize/[email protected] above github.com/securego/gosec/v2/cmd/[email protected] above Fix unit test lint issues Signed-off-by: yiraeChristineKim <[email protected]>
1 parent 0d84aee commit d3b0f1d

11 files changed

+138
-109
lines changed

Diff for: build/common/Makefile.common.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CONTROLLER_GEN_VERSION := v0.16.3
77
# https://github.com/kubernetes-sigs/kustomize/releases/latest
88
KUSTOMIZE_VERSION := v5.4.3
99
# https://github.com/golangci/golangci-lint/releases/latest
10-
GOLANGCI_VERSION := v1.52.2
10+
GOLANGCI_VERSION := v1.55.1
1111
# https://github.com/mvdan/gofumpt/releases/latest
1212
GOFUMPT_VERSION := v0.7.0
1313
# https://github.com/daixiang0/gci/releases/latest

Diff for: build/common/config/.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ issues:
193193
- return statements should not be cuddled if block has more than two lines
194194
- declarations should never be cuddled
195195
- don't use leading k in Go names
196+
- "require-error: for error assertions use require"
196197

197198
exclude-rules:
198199
# Allow dot imports in the tests.

Diff for: controllers/configurationpolicy_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ func (r *ConfigurationPolicyReconciler) determineDesiredObjects(
14291429
objSelector, err := metav1.LabelSelectorAsSelector(objectSelector)
14301430
if err != nil {
14311431
log.Error(err, "Failed to select the resources",
1432-
"objectSelector", fmt.Sprint(objectT.ObjectSelector.String()))
1432+
"objectSelector", objectT.ObjectSelector.String())
14331433

14341434
msg := fmt.Sprintf(
14351435
"Error parsing provided objectSelector in the object-template at index [%d]: %v",
@@ -1471,7 +1471,7 @@ func (r *ConfigurationPolicyReconciler) determineDesiredObjects(
14711471

14721472
if err != nil {
14731473
log.Error(err, "Failed to fetch the resources",
1474-
"objectSelector", fmt.Sprint(objectT.ObjectSelector.String()))
1474+
"objectSelector", objectT.ObjectSelector.String())
14751475

14761476
msg := fmt.Sprintf(
14771477
"Error listing resources with provided objectSelector in the object-template at index [%d]: %v",

Diff for: controllers/configurationpolicy_controller_test.go

+86-66
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestCompareSpecs(t *testing.T) {
105105
"name": "nginx",
106106
},
107107
}
108-
assert.Equal(t, reflect.DeepEqual(merged, mergedExpected), true)
108+
assert.True(t, reflect.DeepEqual(merged, mergedExpected))
109109

110110
spec1 = map[string]interface{}{
111111
"containers": map[string]interface{}{
@@ -196,11 +196,13 @@ func TestConvertPolicyStatusToStringLongMsg(t *testing.T) {
196196
func TestMergeArraysMustHave(t *testing.T) {
197197
t.Parallel()
198198

199-
testcases := map[string]struct {
199+
type TestCaseStruct struct {
200200
desiredList []interface{}
201201
currentList []interface{}
202202
expectedList []interface{}
203-
}{
203+
}
204+
205+
testcases := map[string]TestCaseStruct{
204206
"merge array with existing element into array preserves array": {
205207
[]interface{}{
206208
map[string]interface{}{"a": "apple", "b": "boy"},
@@ -287,28 +289,34 @@ func TestMergeArraysMustHave(t *testing.T) {
287289
},
288290
}
289291

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+
290300
for testName, test := range testcases {
291-
testName := testName
292-
test := test
301+
local := test
293302

294303
t.Run(testName, func(t *testing.T) {
295304
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)
300306
})
301307
}
302308
}
303309

304310
func TestMergeArraysMustOnlyHave(t *testing.T) {
305311
t.Parallel()
306312

307-
testcases := map[string]struct {
313+
type TestCaseStruct struct {
308314
desiredList []interface{}
309315
currentList []interface{}
310316
expectedList []interface{}
311-
}{
317+
}
318+
319+
testcases := map[string]TestCaseStruct{
312320
"merge array with one element into array with two becomes new array": {
313321
[]interface{}{
314322
map[string]interface{}{"a": "apple", "b": "boy"},
@@ -374,16 +382,20 @@ func TestMergeArraysMustOnlyHave(t *testing.T) {
374382
},
375383
}
376384

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+
377393
for testName, test := range testcases {
378-
testName := testName
379-
test := test
394+
local := test
380395

381396
t.Run(testName, func(t *testing.T) {
382397
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)
387399
})
388400
}
389401
}
@@ -602,22 +614,22 @@ func TestAddRelatedObject(t *testing.T) {
602614
related := relatedList[0]
603615

604616
// 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)
611623

612624
// add the same object and make sure the existing one is overwritten
613625
reason = "new"
614626
compliant = false
615627
relatedList = addRelatedObjects(compliant, scopedGVR, "ConfigurationPolicy", namespace, []string{name}, reason, nil)
616628
related = relatedList[0]
617629

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)
621633

622634
// add a new related object and make sure the entry is appended
623635
name = "bar"
@@ -626,11 +638,11 @@ func TestAddRelatedObject(t *testing.T) {
626638
addRelatedObjects(compliant, scopedGVR, "ConfigurationPolicy", namespace, []string{name}, reason, nil)...,
627639
)
628640

629-
assert.True(t, len(relatedList) == 2)
641+
assert.Len(t, relatedList, 2)
630642

631643
related = relatedList[1]
632644

633-
assert.True(t, related.Object.Metadata.Name == name)
645+
assert.Equal(t, name, related.Object.Metadata.Name)
634646
}
635647

636648
func TestUpdatedRelatedObjects(t *testing.T) {
@@ -670,15 +682,15 @@ func TestUpdatedRelatedObjects(t *testing.T) {
670682
)
671683

672684
r.updatedRelatedObjects(policy, relatedList)
673-
assert.True(t, relatedList[0].Object.Metadata.Name == "bar")
685+
assert.Equal(t, "bar", relatedList[0].Object.Metadata.Name)
674686

675687
// append another object named bar but also with namespace bar
676688
relatedList = append(relatedList, addRelatedObjects(
677689
true, scopedGVR, "ConfigurationPolicy", "bar", []string{name}, "reason", nil)...,
678690
)
679691

680692
r.updatedRelatedObjects(policy, relatedList)
681-
assert.True(t, relatedList[0].Object.Metadata.Namespace == "bar")
693+
assert.Equal(t, "bar", relatedList[0].Object.Metadata.Namespace)
682694

683695
// clear related objects and test sorting with no namespace
684696
scopedGVR.Namespaced = false
@@ -690,7 +702,7 @@ func TestUpdatedRelatedObjects(t *testing.T) {
690702
)
691703

692704
r.updatedRelatedObjects(policy, relatedList)
693-
assert.True(t, relatedList[0].Object.Metadata.Name == "bar")
705+
assert.Equal(t, "bar", relatedList[0].Object.Metadata.Name)
694706
}
695707

696708
func TestCreateStatus(t *testing.T) {
@@ -1023,14 +1035,14 @@ func TestCreateStatus(t *testing.T) {
10231035
}
10241036

10251037
for _, test := range testcases {
1026-
test := test
1038+
local := test
10271039

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)
10301042

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)
10341046
})
10351047
}
10361048
}
@@ -1122,7 +1134,7 @@ func TestShouldEvaluatePolicy(t *testing.T) {
11221134
lastEvaluatedTwelveHoursAgo := &sync.Map{}
11231135
lastEvaluatedTwelveHoursAgo.Store(policy.UID, twelveHoursAgo)
11241136

1125-
tests := []struct {
1137+
type TestCaseStruct struct {
11261138
testDescription string
11271139
lastEvaluated string
11281140
lastEvaluatedGeneration int64
@@ -1134,7 +1146,9 @@ func TestShouldEvaluatePolicy(t *testing.T) {
11341146
cleanupImmediately bool
11351147
finalizers []string
11361148
lastEvaluatedCache *sync.Map
1137-
}{
1149+
}
1150+
1151+
tests := []TestCaseStruct{
11381152
{
11391153
"Just evaluated and the generation is unchanged",
11401154
inFuture,
@@ -1410,40 +1424,46 @@ func TestShouldEvaluatePolicy(t *testing.T) {
14101424
},
14111425
}
14121426

1413-
for _, test := range tests {
1414-
test := test
1427+
runTest := func(t *testing.T, test TestCaseStruct) {
1428+
t.Helper()
14151429

1416-
t.Run(
1417-
test.testDescription,
1418-
func(t *testing.T) {
1419-
t.Parallel()
1420-
policy := policy.DeepCopy()
1430+
policyCopy := policy.DeepCopy()
14211431

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
14281438

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+
}
14331443

1434-
actual, actualDuration := r.shouldEvaluatePolicy(policy, test.cleanupImmediately)
1444+
actual, actualDuration := r.shouldEvaluatePolicy(policyCopy, test.cleanupImmediately)
14351445

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+
}
14391449

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+
}
14431453

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)
14471467
},
14481468
)
14491469
}

Diff for: controllers/configurationpolicy_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func checkFieldsWithSort(
239239
}
240240

241241
// An error indicates the value is a regular string, so check equality normally
242-
if fmt.Sprint(oVal) != fmt.Sprint(mVal) {
242+
if fmt.Sprint(oVal) != mVal {
243243
return false
244244
}
245245
} else {

Diff for: controllers/configurationpolicy_utils_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestFormatTemplateStringAnnotation(t *testing.T) {
5656
}
5757

5858
policyTemplateFormatted := formatMetadata(policyTemplate)
59-
assert.Equal(t, policyTemplateFormatted["annotations"], "not-an-annotation")
59+
assert.Equal(t, "not-an-annotation", policyTemplateFormatted["annotations"])
6060
}
6161

6262
func TestAddConditionToStatusNeverEvalInterval(t *testing.T) {
@@ -86,18 +86,18 @@ func TestAddConditionToStatusNeverEvalInterval(t *testing.T) {
8686
addConditionToStatus(policy, 0, test.compliancy == policyv1.Compliant, "Some reason", "Some message")
8787

8888
details := policy.Status.CompliancyDetails
89-
assert.Equal(t, len(details), 1)
89+
assert.Len(t, details, 1)
9090

9191
detail := details[0]
9292
conditions := detail.Conditions
93-
assert.Equal(t, len(conditions), 1)
93+
assert.Len(t, conditions, 1)
9494

9595
condition := conditions[0]
9696
lowercaseCompliance := strings.ToLower(string(test.compliancy))
9797
expectedMsg := `Some message. This policy will not be evaluated again due to ` +
9898
fmt.Sprintf(`spec.evaluationInterval.%s being set to "never".`, lowercaseCompliance)
9999

100-
assert.Equal(t, condition.Message, expectedMsg)
100+
assert.Equal(t, expectedMsg, condition.Message)
101101
},
102102
)
103103
}

0 commit comments

Comments
 (0)