Skip to content

Commit

Permalink
Merge pull request kubernetes#125802 from mmorel-35/testifylint/len+e…
Browse files Browse the repository at this point in the history
…mpty

fix: enable empty and len rules from testifylint on pkg and staging package
  • Loading branch information
k8s-ci-robot authored Jul 12, 2024
2 parents 7f23ebe + f014b75 commit 2d4514e
Show file tree
Hide file tree
Showing 54 changed files with 203 additions and 207 deletions.
2 changes: 0 additions & 2 deletions hack/golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,10 @@ linters-settings: # please keep this alphabetized
disable: # TODO: remove each disabled rule and fix it
- blank-import
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- float-compare
- go-require
- len
- nil-compare
- require-error
2 changes: 0 additions & 2 deletions hack/golangci.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,11 @@ linters-settings: # please keep this alphabetized
disable: # TODO: remove each disabled rule and fix it
- blank-import
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- float-compare
- go-require
- len
- nil-compare
- require-error
{{- end}}
10 changes: 5 additions & 5 deletions pkg/controller/endpointslice/endpointslice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestSyncServiceNoSelector(t *testing.T) {
logger, _ := ktesting.NewTestContext(t)
err := esController.syncService(logger, fmt.Sprintf("%s/%s", ns, serviceName))
assert.NoError(t, err)
assert.Len(t, client.Actions(), 0)
assert.Empty(t, client.Actions())
}

func TestServiceExternalNameTypeSync(t *testing.T) {
Expand Down Expand Up @@ -262,11 +262,11 @@ func TestServiceExternalNameTypeSync(t *testing.T) {

err = esController.syncService(logger, fmt.Sprintf("%s/%s", namespace, serviceName))
assert.NoError(t, err)
assert.Len(t, client.Actions(), 0)
assert.Empty(t, client.Actions())

sliceList, err := client.DiscoveryV1().EndpointSlices(namespace).List(context.TODO(), metav1.ListOptions{})
assert.NoError(t, err)
assert.Len(t, sliceList.Items, 0, "Expected 0 endpoint slices")
assert.Empty(t, sliceList.Items, "Expected 0 endpoint slices")
})
}
}
Expand All @@ -288,7 +288,7 @@ func TestSyncServicePendingDeletion(t *testing.T) {
logger, _ := ktesting.NewTestContext(t)
err := esController.syncService(logger, fmt.Sprintf("%s/%s", ns, serviceName))
assert.NoError(t, err)
assert.Len(t, client.Actions(), 0)
assert.Empty(t, client.Actions())
}

// Ensure SyncService for service with selector but no pods results in placeholder EndpointSlice
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestSyncServiceMissing(t *testing.T) {
assert.Nil(t, err, "Expected no error syncing service")

// That should mean no client actions were performed
assert.Len(t, client.Actions(), 0)
assert.Empty(t, client.Actions())

// TriggerTimeTracker should have removed the reference to the missing service
assert.NotContains(t, esController.triggerTimeTracker.ServiceStates, missingServiceKey)
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/garbagecollector/garbagecollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,35 @@ func TestGarbageCollectorConstruction(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 0, len(gc.dependencyGraphBuilder.monitors))
assert.Empty(t, gc.dependencyGraphBuilder.monitors)

// Make sure resource monitor syncing creates and stops resource monitors.
tweakableRM.Add(schema.GroupVersionKind{Group: "tpr.io", Version: "v1", Kind: "unknown"}, nil)
err = gc.resyncMonitors(logger, twoResources)
if err != nil {
t.Errorf("Failed adding a monitor: %v", err)
}
assert.Equal(t, 2, len(gc.dependencyGraphBuilder.monitors))
assert.Len(t, gc.dependencyGraphBuilder.monitors, 2)

err = gc.resyncMonitors(logger, podResource)
if err != nil {
t.Errorf("Failed removing a monitor: %v", err)
}
assert.Equal(t, 1, len(gc.dependencyGraphBuilder.monitors))
assert.Len(t, gc.dependencyGraphBuilder.monitors, 1)

go gc.Run(tCtx, 1)

err = gc.resyncMonitors(logger, twoResources)
if err != nil {
t.Errorf("Failed adding a monitor: %v", err)
}
assert.Equal(t, 2, len(gc.dependencyGraphBuilder.monitors))
assert.Len(t, gc.dependencyGraphBuilder.monitors, 2)

err = gc.resyncMonitors(logger, podResource)
if err != nil {
t.Errorf("Failed removing a monitor: %v", err)
}
assert.Equal(t, 1, len(gc.dependencyGraphBuilder.monitors))
assert.Len(t, gc.dependencyGraphBuilder.monitors, 1)
}

// fakeAction records information about requests to aid in testing.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/podautoscaler/horizontal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5293,5 +5293,5 @@ func TestMultipleHPAs(t *testing.T) {
}
}

assert.Equal(t, hpaCount, len(processedHPA), "Expected to process all HPAs")
assert.Len(t, processedHPA, hpaCount, "Expected to process all HPAs")
}
6 changes: 3 additions & 3 deletions pkg/controller/ttl/ttl_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestPatchNode(t *testing.T) {
continue
}
actions := fakeClient.Actions()
assert.Equal(t, 1, len(actions), "unexpected actions: %#v", actions)
assert.Len(t, actions, 1, "unexpected actions: %#v", actions)
patchAction := actions[0].(core.PatchActionImpl)
assert.Equal(t, testCase.patch, string(patchAction.Patch), "%d: unexpected patch: %s", i, string(patchAction.Patch))
}
Expand Down Expand Up @@ -145,9 +145,9 @@ func TestUpdateNodeIfNeeded(t *testing.T) {
}
actions := fakeClient.Actions()
if testCase.patch == "" {
assert.Equal(t, 0, len(actions), "unexpected actions: %#v", actions)
assert.Empty(t, actions, "unexpected actions")
} else {
assert.Equal(t, 1, len(actions), "unexpected actions: %#v", actions)
assert.Len(t, actions, 1, "unexpected actions: %#v", actions)
patchAction := actions[0].(core.PatchActionImpl)
assert.Equal(t, testCase.patch, string(patchAction.Patch), "%d: unexpected patch: %s", i, string(patchAction.Patch))
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/kubelet/cm/devicemanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
as.True(ok)
as.Equal(int64(3), resource1Capacity.Value())
as.Equal(int64(2), resource1Allocatable.Value())
as.Equal(0, len(removedResources))
as.Empty(removedResources)

// Deletes an unhealthy device should NOT change allocatable but change capacity.
devs1 := devs[:len(devs)-1]
Expand All @@ -377,7 +377,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
as.True(ok)
as.Equal(int64(2), resource1Capacity.Value())
as.Equal(int64(2), resource1Allocatable.Value())
as.Equal(0, len(removedResources))
as.Empty(removedResources)

// Updates a healthy device to unhealthy should reduce allocatable by 1.
devs[1].Health = pluginapi.Unhealthy
Expand All @@ -389,7 +389,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
as.True(ok)
as.Equal(int64(3), resource1Capacity.Value())
as.Equal(int64(1), resource1Allocatable.Value())
as.Equal(0, len(removedResources))
as.Empty(removedResources)

// Deletes a healthy device should reduce capacity and allocatable by 1.
devs2 := devs[1:]
Expand All @@ -401,7 +401,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
as.True(ok)
as.Equal(int64(0), resource1Allocatable.Value())
as.Equal(int64(2), resource1Capacity.Value())
as.Equal(0, len(removedResources))
as.Empty(removedResources)

// Tests adding another resource.
resourceName2 := "resource2"
Expand All @@ -410,14 +410,14 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
testManager.endpoints[resourceName2] = endpointInfo{e: e2, opts: nil}
callback(resourceName2, devs)
capacity, allocatable, removedResources = testManager.GetCapacity()
as.Equal(2, len(capacity))
as.Len(capacity, 2)
resource2Capacity, ok := capacity[v1.ResourceName(resourceName2)]
as.True(ok)
resource2Allocatable, ok := allocatable[v1.ResourceName(resourceName2)]
as.True(ok)
as.Equal(int64(3), resource2Capacity.Value())
as.Equal(int64(1), resource2Allocatable.Value())
as.Equal(0, len(removedResources))
as.Empty(removedResources)

// Expires resourceName1 endpoint. Verifies testManager.GetCapacity() reports that resourceName1
// is removed from capacity and it no longer exists in healthyDevices after the call.
Expand All @@ -432,7 +432,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
as.NotContains(testManager.healthyDevices, resourceName1)
as.NotContains(testManager.unhealthyDevices, resourceName1)
as.NotContains(testManager.endpoints, resourceName1)
as.Equal(1, len(testManager.endpoints))
as.Len(testManager.endpoints, 1)

// Stops resourceName2 endpoint. Verifies its stopTime is set, allocate and
// preStartContainer calls return errors.
Expand Down Expand Up @@ -464,7 +464,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
testManager.unhealthyDevices = make(map[string]sets.Set[string])
err = testManager.readCheckpoint()
as.Nil(err)
as.Equal(1, len(testManager.endpoints))
as.Len(testManager.endpoints, 1)
as.Contains(testManager.endpoints, resourceName2)
capacity, allocatable, removed = testManager.GetCapacity()
val, ok = capacity[v1.ResourceName(resourceName2)]
Expand Down Expand Up @@ -506,7 +506,7 @@ func TestGetAllocatableDevicesMultipleResources(t *testing.T) {
testManager.genericDeviceUpdateCallback(resourceName2, resource2Devs)

allocatableDevs := testManager.GetAllocatableDevices()
as.Equal(2, len(allocatableDevs))
as.Len(allocatableDevs, 2)

devInstances1, ok := allocatableDevs[resourceName1]
as.True(ok)
Expand Down Expand Up @@ -543,7 +543,7 @@ func TestGetAllocatableDevicesHealthTransition(t *testing.T) {
testManager.genericDeviceUpdateCallback(resourceName1, resource1Devs)

allocatableDevs := testManager.GetAllocatableDevices()
as.Equal(1, len(allocatableDevs))
as.Len(allocatableDevs, 1)
devInstances, ok := allocatableDevs[resourceName1]
as.True(ok)
checkAllocatableDevicesConsistsOf(as, devInstances, []string{"R1Device1", "R1Device2"})
Expand All @@ -557,7 +557,7 @@ func TestGetAllocatableDevicesHealthTransition(t *testing.T) {
testManager.genericDeviceUpdateCallback(resourceName1, resource1Devs)

allocatableDevs = testManager.GetAllocatableDevices()
as.Equal(1, len(allocatableDevs))
as.Len(allocatableDevs, 1)
devInstances, ok = allocatableDevs[resourceName1]
as.True(ok)
checkAllocatableDevicesConsistsOf(as, devInstances, []string{"R1Device1", "R1Device2", "R1Device3"})
Expand Down Expand Up @@ -1293,9 +1293,9 @@ func TestGetDeviceRunContainerOptions(t *testing.T) {
// when pod is in activePods, GetDeviceRunContainerOptions should return
runContainerOpts, err := testManager.GetDeviceRunContainerOptions(pod1, &pod1.Spec.Containers[0])
as.Nil(err)
as.Equal(len(runContainerOpts.Devices), 3)
as.Equal(len(runContainerOpts.Mounts), 2)
as.Equal(len(runContainerOpts.Envs), 2)
as.Len(runContainerOpts.Devices, 3)
as.Len(runContainerOpts.Mounts, 2)
as.Len(runContainerOpts.Envs, 2)

activePods = []*v1.Pod{pod2}
podsStub.updateActivePods(activePods)
Expand Down Expand Up @@ -1643,7 +1643,7 @@ func TestDevicePreStartContainer(t *testing.T) {

expectedResps, err := allocateStubFunc()([]string{"dev1", "dev2"})
as.Nil(err)
as.Equal(1, len(expectedResps.ContainerResponses))
as.Len(expectedResps.ContainerResponses, 1)
expectedResp := expectedResps.ContainerResponses[0]
as.Equal(len(runContainerOpts.Devices), len(expectedResp.Devices))
as.Equal(len(runContainerOpts.Mounts), len(expectedResp.Mounts))
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/cm/dra/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ func TestGetContainerClaimInfos(t *testing.T) {

fakeClaimInfos, err := manager.GetContainerClaimInfos(test.pod, test.container)
assert.NoError(t, err)
assert.Equal(t, 1, len(fakeClaimInfos))
assert.Len(t, fakeClaimInfos, 1)
assert.Equal(t, test.expectedClaimName, fakeClaimInfos[0].ClaimInfoState.ClaimName)

manager.cache.delete(test.pod.Spec.ResourceClaims[0].Name, "default")
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubelet/images/image_gc_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ func TestGarbageCollectImageTooOld(t *testing.T) {
t.Log(fakeClock.Now())
images, err := manager.imagesInEvictionOrder(ctx, fakeClock.Now())
require.NoError(t, err)
require.Equal(t, len(images), 1)
require.Len(t, images, 1)
// Simulate pod having just used this image, but having been GC'd
images[0].lastUsed = fakeClock.Now()

Expand All @@ -796,7 +796,7 @@ func TestGarbageCollectImageTooOld(t *testing.T) {
fakeClock.Step(policy.MaxAge + 1)
images, err = manager.freeOldImages(ctx, images, fakeClock.Now(), oldStartTime)
require.NoError(t, err)
assert.Len(images, 0)
assert.Empty(images)
assert.Len(fakeRuntime.ImageList, 1)
}

Expand Down Expand Up @@ -837,7 +837,7 @@ func TestGarbageCollectImageMaxAgeDisabled(t *testing.T) {
t.Log(fakeClock.Now())
images, err := manager.imagesInEvictionOrder(ctx, fakeClock.Now())
require.NoError(t, err)
require.Equal(t, len(images), 1)
require.Len(t, images, 1)
assert.Len(fakeRuntime.ImageList, 2)

oldStartTime := fakeClock.Now()
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/images/image_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
assert.Equal(t, c.expected[0].shouldRecordFinishedPullingTime, fakePodPullingTimeRecorder.finishedPullingRecorded)

images, _ := fakeRuntime.ListImages(ctx)
assert.Equal(t, 1, len(images), "ListImages() count")
assert.Len(t, images, 1, "ListImages() count")

image := images[0]
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestPullAndListImageWithRuntimeHandlerInImageCriAPIFeatureGate(t *testing.T
assert.Equal(t, c.expected[0].shouldRecordFinishedPullingTime, fakePodPullingTimeRecorder.finishedPullingRecorded)

images, _ := fakeRuntime.ListImages(ctx)
assert.Equal(t, 1, len(images), "ListImages() count")
assert.Len(t, images, 1, "ListImages() count")

image := images[0]
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/kubelet_node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ func TestFastStatusUpdateOnce(t *testing.T) {

actions := kubeClient.Actions()
if tc.wantPatches == 0 {
require.Len(t, actions, 0)
require.Empty(t, actions)
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/kubelet_pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6111,9 +6111,9 @@ func TestGetNonExistentImagePullSecret(t *testing.T) {
}

pullSecrets := testKubelet.kubelet.getPullSecretsForPod(testPod)
assert.Equal(t, 0, len(pullSecrets))
assert.Empty(t, pullSecrets)

assert.Equal(t, 1, len(fakeRecorder.Events))
assert.Len(t, fakeRecorder.Events, 1)
event := <-fakeRecorder.Events
assert.Equal(t, event, expectedEvent)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ func TestDeleteOrphanedMirrorPods(t *testing.T) {

// Sync with an empty pod list to delete all mirror pods.
kl.HandlePodCleanups(ctx)
assert.Len(t, manager.GetPods(), 0, "Expected 0 mirror pods")
assert.Empty(t, manager.GetPods(), "Expected no mirror pods")
for i, pod := range orphanPods {
name := kubecontainer.GetPodFullName(pod)
creates, deletes := manager.GetCounts(name)
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubelet/kubelet_volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func TestVolumeUnmountAndDetachControllerDisabled(t *testing.T) {
podVolumes = kubelet.volumeManager.GetMountedVolumesForPod(
util.GetUniquePodName(pod))

assert.Len(t, podVolumes, 0,
assert.Empty(t, podVolumes,
"Expected volumes to be unmounted and detached. But some volumes are still mounted: %#v", podVolumes)

assert.NoError(t, volumetest.VerifyTearDownCallCount(
Expand Down Expand Up @@ -667,8 +667,8 @@ func TestVolumeUnmountAndDetachControllerEnabled(t *testing.T) {
util.GetUniquePodName(pod))
assert.Equal(t, podVolumes, allPodVolumes, "GetMountedVolumesForPod and GetPossiblyMountedVolumesForPod should return the same volumes")

assert.Len(t, podVolumes, 0,
"Expected volumes to be unmounted and detached. But some volumes are still mounted: %#v", podVolumes)
assert.Empty(t, podVolumes,
"Expected volumes to be unmounted and detached. But some volumes are still mounted")

assert.NoError(t, volumetest.VerifyTearDownCallCount(
1 /* expectedTearDownCallCount */, testKubelet.volumePlugin))
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/kuberuntime/kuberuntime_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestRemoveContainer(t *testing.T) {

// Create fake sandbox and container
_, fakeContainers := makeAndSetFakePod(t, m, fakeRuntime, pod)
assert.Equal(t, len(fakeContainers), 1)
assert.Len(t, fakeContainers, 1)

containerID := fakeContainers[0].Id
fakeOS := m.osInterface.(*containertest.FakeOS)
Expand Down Expand Up @@ -956,7 +956,7 @@ func TestUpdateContainerResources(t *testing.T) {

// Create fake sandbox and container
_, fakeContainers := makeAndSetFakePod(t, m, fakeRuntime, pod)
assert.Equal(t, len(fakeContainers), 1)
assert.Len(t, fakeContainers, 1)

ctx := context.Background()
cStatus, err := m.getPodContainerStatuses(ctx, pod.UID, pod.Name, pod.Namespace)
Expand Down
Loading

0 comments on commit 2d4514e

Please sign in to comment.