From f6faa86301d84e0e03859402c2ce427ae3be30d2 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 30 Dec 2024 07:43:23 +0100 Subject: [PATCH] chore: enable testifylint linter Signed-off-by: Matthieu MOREL --- .golangci.yaml | 9 +++++++++ pkg/cache/cluster_test.go | 18 +++++++++--------- pkg/cache/predicates_test.go | 6 +++--- pkg/diff/diff_test.go | 6 +++--- pkg/sync/hook/helm/weight_test.go | 4 ++-- pkg/sync/sync_context_test.go | 16 ++++++++-------- pkg/utils/kube/kube_test.go | 4 ++-- 7 files changed, 36 insertions(+), 27 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index c516a7c99..07cdfc908 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,3 +1,12 @@ issues: exclude-files: - "pkg/diff/internal/fieldmanager/borrowed_.*\\.go$" +linters: + enable: + - testifylint +linters-settings: + testifylint: + enable-all: true + disable: + - go-require + - require-error diff --git a/pkg/cache/cluster_test.go b/pkg/cache/cluster_test.go index 8f3263da6..e0a67e3e0 100644 --- a/pkg/cache/cluster_test.go +++ b/pkg/cache/cluster_test.go @@ -371,9 +371,9 @@ metadata: return len(r.OwnerRefs) == 0 }) require.NoError(t, err) - assert.Equal(t, managedObjs, map[kube.ResourceKey]*unstructured.Unstructured{ + assert.Equal(t, map[kube.ResourceKey]*unstructured.Unstructured{ kube.NewResourceKey("apps", "Deployment", "default", "helm-guestbook"): mustToUnstructured(testDeploy()), - }) + }, managedObjs) } func TestGetManagedLiveObjsNamespacedModeClusterLevelResource(t *testing.T) { @@ -424,7 +424,7 @@ metadata: _, err = cluster.GetManagedLiveObjs([]*unstructured.Unstructured{clusterLevelRes}, func(r *Resource) bool { return len(r.OwnerRefs) == 0 }) - assert.Nil(t, err) + assert.NoError(t, err) otherNamespaceRes := strToUnstructured(` apiVersion: apps/v1 @@ -465,9 +465,9 @@ metadata: return len(r.OwnerRefs) == 0 }) require.NoError(t, err) - assert.Equal(t, managedObjs, map[kube.ResourceKey]*unstructured.Unstructured{ + assert.Equal(t, map[kube.ResourceKey]*unstructured.Unstructured{ kube.NewResourceKey("apps", "Deployment", "default", "helm-guestbook"): mustToUnstructured(testDeploy()), - }) + }, managedObjs) } func TestGetManagedLiveObjsValidNamespace(t *testing.T) { @@ -493,9 +493,9 @@ metadata: return len(r.OwnerRefs) == 0 }) require.NoError(t, err) - assert.Equal(t, managedObjs, map[kube.ResourceKey]*unstructured.Unstructured{ + assert.Equal(t, map[kube.ResourceKey]*unstructured.Unstructured{ kube.NewResourceKey("apps", "Deployment", "default", "helm-guestbook"): mustToUnstructured(testDeploy()), - }) + }, managedObjs) } func TestGetManagedLiveObjsInvalidNamespace(t *testing.T) { @@ -598,9 +598,9 @@ metadata: assert.NoError(t, err) assert.Equal(t, testCaseCopy.expectConvertToVersionCalled, convertToVersionWasCalled) assert.Equal(t, testCaseCopy.expectGetResourceCalled, getResourceWasCalled) - assert.Equal(t, managedObjs, map[kube.ResourceKey]*unstructured.Unstructured{ + assert.Equal(t, map[kube.ResourceKey]*unstructured.Unstructured{ kube.NewResourceKey(cronTabGroup, "CronTab", "default", "test-crontab"): mustToUnstructured(testCronTab()), - }) + }, managedObjs) }) } } diff --git a/pkg/cache/predicates_test.go b/pkg/cache/predicates_test.go index fa8cb1ad2..8887b47c8 100644 --- a/pkg/cache/predicates_test.go +++ b/pkg/cache/predicates_test.go @@ -82,12 +82,12 @@ func TestGetNamespaceResources(t *testing.T) { resources := cluster.FindResources("default", TopLevelResource) assert.Len(t, resources, 2) - assert.Equal(t, resources[getResourceKey(t, defaultNamespaceTopLevel1)].Ref.Name, "helm-guestbook1") - assert.Equal(t, resources[getResourceKey(t, defaultNamespaceTopLevel2)].Ref.Name, "helm-guestbook2") + assert.Equal(t, "helm-guestbook1", resources[getResourceKey(t, defaultNamespaceTopLevel1)].Ref.Name) + assert.Equal(t, "helm-guestbook2", resources[getResourceKey(t, defaultNamespaceTopLevel2)].Ref.Name) resources = cluster.FindResources("kube-system", TopLevelResource) assert.Len(t, resources, 1) - assert.Equal(t, resources[getResourceKey(t, kubesystemNamespaceTopLevel2)].Ref.Name, "helm-guestbook3") + assert.Equal(t, "helm-guestbook3", resources[getResourceKey(t, kubesystemNamespaceTopLevel2)].Ref.Name) } func ExampleNewClusterCache_inspectNamespaceResources() { diff --git a/pkg/diff/diff_test.go b/pkg/diff/diff_test.go index d3b5f32b2..c769a8ba3 100644 --- a/pkg/diff/diff_test.go +++ b/pkg/diff/diff_test.go @@ -530,9 +530,9 @@ func TestDiffResourceWithInvalidField(t *testing.T) { diffRes := diff(t, &leftUn, rightUn, diffOptionsForTest()...) assert.True(t, diffRes.Modified) ascii, err := printDiff(diffRes) - assert.Nil(t, err) + assert.NoError(t, err) - assert.True(t, strings.Contains(ascii, "invalidKey")) + assert.Contains(t, ascii, "invalidKey") if ascii != "" { t.Log(ascii) } @@ -1255,7 +1255,7 @@ func TestHideSecretDataHandleEmptySecret(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, target) assert.NotNil(t, live) - assert.Equal(t, nil, target.Object["data"]) + assert.Nil(t, target.Object["data"]) assert.Equal(t, map[string]interface{}{"namespace": "++++++++", "token": "++++++++"}, secretData(live)) } diff --git a/pkg/sync/hook/helm/weight_test.go b/pkg/sync/hook/helm/weight_test.go index cfeec535d..7f1247902 100644 --- a/pkg/sync/hook/helm/weight_test.go +++ b/pkg/sync/hook/helm/weight_test.go @@ -9,6 +9,6 @@ import ( ) func TestWeight(t *testing.T) { - assert.Equal(t, Weight(NewPod()), 0) - assert.Equal(t, Weight(Annotate(NewPod(), "helm.sh/hook-weight", "1")), 1) + assert.Equal(t, 0, Weight(NewPod())) + assert.Equal(t, 1, Weight(Annotate(NewPod(), "helm.sh/hook-weight", "1"))) } diff --git a/pkg/sync/sync_context_test.go b/pkg/sync/sync_context_test.go index 7e416d20b..cbacca0c3 100644 --- a/pkg/sync/sync_context_test.go +++ b/pkg/sync/sync_context_test.go @@ -688,7 +688,7 @@ func TestDoNotSyncOrPruneHooks(t *testing.T) { syncCtx.hooks = []*unstructured.Unstructured{targetPod, liveSvc} syncCtx.Sync() phase, _, resources := syncCtx.GetState() - assert.Len(t, resources, 0) + assert.Empty(t, resources) assert.Equal(t, synccommon.OperationSucceeded, phase) } @@ -1715,7 +1715,7 @@ func TestSetOperationFailed(t *testing.T) { sc.setOperationFailed(nil, tasks, "one or more objects failed to apply") - assert.Equal(t, sc.message, "one or more objects failed to apply, reason: namespace not found") + assert.Equal(t, "one or more objects failed to apply, reason: namespace not found", sc.message) } @@ -1729,7 +1729,7 @@ func TestSetOperationFailedDuplicatedMessages(t *testing.T) { sc.setOperationFailed(nil, tasks, "one or more objects failed to apply") - assert.Equal(t, sc.message, "one or more objects failed to apply, reason: namespace not found") + assert.Equal(t, "one or more objects failed to apply, reason: namespace not found", sc.message) } @@ -1739,7 +1739,7 @@ func TestSetOperationFailedNoTasks(t *testing.T) { sc.setOperationFailed(nil, nil, "one or more objects failed to apply") - assert.Equal(t, sc.message, "one or more objects failed to apply") + assert.Equal(t, "one or more objects failed to apply", sc.message) } @@ -1990,7 +1990,7 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) { syncCtx.Sync() phase, _, result = syncCtx.GetState() assert.Equal(t, synccommon.OperationRunning, phase) - assert.Equal(t, 1, len(result)) + assert.Len(t, result, 1) assert.Equal(t, "pod-3", result[0].ResourceKey.Name) assert.Equal(t, synccommon.ResultCodePruned, result[0].Status) @@ -2004,7 +2004,7 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) { syncCtx.Sync() phase, _, result = syncCtx.GetState() assert.Equal(t, synccommon.OperationRunning, phase) - assert.Equal(t, 2, len(result)) + assert.Len(t, result, 2) assert.Equal(t, "pod-2", result[1].ResourceKey.Name) assert.Equal(t, synccommon.ResultCodePruned, result[1].Status) @@ -2017,7 +2017,7 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) { phase, msg, result = syncCtx.GetState() assert.Equal(t, synccommon.OperationRunning, phase) assert.Equal(t, "waiting for deletion of /Pod/pod-2", msg) - assert.Equal(t, 2, len(result)) + assert.Len(t, result, 2) // simulate successful delete of pod2 syncCtx.resources = groupResources(ReconciliationResult{ @@ -2030,7 +2030,7 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) { syncCtx.Sync() phase, _, result = syncCtx.GetState() assert.Equal(t, synccommon.OperationSucceeded, phase) - assert.Equal(t, 3, len(result)) + assert.Len(t, result, 3) assert.Equal(t, "pod-3", result[0].ResourceKey.Name) assert.Equal(t, "pod-2", result[1].ResourceKey.Name) assert.Equal(t, "pod-1", result[2].ResourceKey.Name) diff --git a/pkg/utils/kube/kube_test.go b/pkg/utils/kube/kube_test.go index e2ecca3a3..2b7f1fe0c 100644 --- a/pkg/utils/kube/kube_test.go +++ b/pkg/utils/kube/kube_test.go @@ -62,7 +62,7 @@ func TestUnsetLabels(t *testing.T) { func TestCleanKubectlOutput(t *testing.T) { { s := `error: error validating "STDIN": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1beta2.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false` - assert.Equal(t, cleanKubectlOutput(s), `error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1beta2.DeploymentSpec`) + assert.Equal(t, `error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1beta2.DeploymentSpec`, cleanKubectlOutput(s)) } { s := `error when applying patch: @@ -72,7 +72,7 @@ Resource: "/v1, Resource=services", GroupVersionKind: "/v1, Kind=Service" Name: "my-service", Namespace: "argocd-e2e--test-immutable-change-ysfud" Object: &{map["apiVersion":"v1" "kind":"Service" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"labels\":{\"app.kubernetes.io/instance\":\"test-immutable-change\"},\"name\":\"my-service\",\"namespace\":\"argocd-e2e--test-immutable-change-ysfud\"},\"spec\":{\"clusterIP\":\"10.96.0.43\",\"ports\":[{\"port\":80,\"protocol\":\"TCP\",\"targetPort\":9376}],\"selector\":{\"app\":\"MyApp\"}}}\n"] "creationTimestamp":"2019-12-11T15:29:56Z" "labels":map["app.kubernetes.io/instance":"test-immutable-change"] "name":"my-service" "namespace":"argocd-e2e--test-immutable-change-ysfud" "resourceVersion":"157426" "selfLink":"/api/v1/namespaces/argocd-e2e--test-immutable-change-ysfud/services/my-service" "uid":"339cf96f-47eb-4759-ac95-30a169dce004"] "spec":map["clusterIP":"10.96.0.43" "ports":[map["port":'P' "protocol":"TCP" "targetPort":'\u24a0']] "selector":map["app":"MyApp"] "sessionAffinity":"None" "type":"ClusterIP"] "status":map["loadBalancer":map[]]]} for: "/var/folders/_m/991sn1ds7g39lnbhp6wvqp9d_j5476/T/224503547": Service "my-service" is invalid: spec.clusterIP: Invalid value: "10.96.0.44": field is immutable` - assert.Equal(t, cleanKubectlOutput(s), `Service "my-service" is invalid: spec.clusterIP: Invalid value: "10.96.0.44": field is immutable`) + assert.Equal(t, `Service "my-service" is invalid: spec.clusterIP: Invalid value: "10.96.0.44": field is immutable`, cleanKubectlOutput(s)) } }