Skip to content

Commit 2d4514e

Browse files
authored
Merge pull request kubernetes#125802 from mmorel-35/testifylint/len+empty
fix: enable empty and len rules from testifylint on pkg and staging package
2 parents 7f23ebe + f014b75 commit 2d4514e

File tree

54 files changed

+203
-207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+203
-207
lines changed

hack/golangci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ linters-settings: # please keep this alphabetized
237237
disable: # TODO: remove each disabled rule and fix it
238238
- blank-import
239239
- compares
240-
- empty
241240
- error-is-as
242241
- error-nil
243242
- expected-actual
244243
- float-compare
245244
- go-require
246-
- len
247245
- nil-compare
248246
- require-error

hack/golangci.yaml.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,11 @@ linters-settings: # please keep this alphabetized
213213
disable: # TODO: remove each disabled rule and fix it
214214
- blank-import
215215
- compares
216-
- empty
217216
- error-is-as
218217
- error-nil
219218
- expected-actual
220219
- float-compare
221220
- go-require
222-
- len
223221
- nil-compare
224222
- require-error
225223
{{- end}}

pkg/controller/endpointslice/endpointslice_controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestSyncServiceNoSelector(t *testing.T) {
195195
logger, _ := ktesting.NewTestContext(t)
196196
err := esController.syncService(logger, fmt.Sprintf("%s/%s", ns, serviceName))
197197
assert.NoError(t, err)
198-
assert.Len(t, client.Actions(), 0)
198+
assert.Empty(t, client.Actions())
199199
}
200200

201201
func TestServiceExternalNameTypeSync(t *testing.T) {
@@ -262,11 +262,11 @@ func TestServiceExternalNameTypeSync(t *testing.T) {
262262

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

267267
sliceList, err := client.DiscoveryV1().EndpointSlices(namespace).List(context.TODO(), metav1.ListOptions{})
268268
assert.NoError(t, err)
269-
assert.Len(t, sliceList.Items, 0, "Expected 0 endpoint slices")
269+
assert.Empty(t, sliceList.Items, "Expected 0 endpoint slices")
270270
})
271271
}
272272
}
@@ -288,7 +288,7 @@ func TestSyncServicePendingDeletion(t *testing.T) {
288288
logger, _ := ktesting.NewTestContext(t)
289289
err := esController.syncService(logger, fmt.Sprintf("%s/%s", ns, serviceName))
290290
assert.NoError(t, err)
291-
assert.Len(t, client.Actions(), 0)
291+
assert.Empty(t, client.Actions())
292292
}
293293

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

343343
// That should mean no client actions were performed
344-
assert.Len(t, client.Actions(), 0)
344+
assert.Empty(t, client.Actions())
345345

346346
// TriggerTimeTracker should have removed the reference to the missing service
347347
assert.NotContains(t, esController.triggerTimeTracker.ServiceStates, missingServiceKey)

pkg/controller/garbagecollector/garbagecollector_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,35 @@ func TestGarbageCollectorConstruction(t *testing.T) {
104104
if err != nil {
105105
t.Fatal(err)
106106
}
107-
assert.Equal(t, 0, len(gc.dependencyGraphBuilder.monitors))
107+
assert.Empty(t, gc.dependencyGraphBuilder.monitors)
108108

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

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

123123
go gc.Run(tCtx, 1)
124124

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

131131
err = gc.resyncMonitors(logger, podResource)
132132
if err != nil {
133133
t.Errorf("Failed removing a monitor: %v", err)
134134
}
135-
assert.Equal(t, 1, len(gc.dependencyGraphBuilder.monitors))
135+
assert.Len(t, gc.dependencyGraphBuilder.monitors, 1)
136136
}
137137

138138
// fakeAction records information about requests to aid in testing.

pkg/controller/podautoscaler/horizontal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5293,5 +5293,5 @@ func TestMultipleHPAs(t *testing.T) {
52935293
}
52945294
}
52955295

5296-
assert.Equal(t, hpaCount, len(processedHPA), "Expected to process all HPAs")
5296+
assert.Len(t, processedHPA, hpaCount, "Expected to process all HPAs")
52975297
}

pkg/controller/ttl/ttl_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestPatchNode(t *testing.T) {
8686
continue
8787
}
8888
actions := fakeClient.Actions()
89-
assert.Equal(t, 1, len(actions), "unexpected actions: %#v", actions)
89+
assert.Len(t, actions, 1, "unexpected actions: %#v", actions)
9090
patchAction := actions[0].(core.PatchActionImpl)
9191
assert.Equal(t, testCase.patch, string(patchAction.Patch), "%d: unexpected patch: %s", i, string(patchAction.Patch))
9292
}
@@ -145,9 +145,9 @@ func TestUpdateNodeIfNeeded(t *testing.T) {
145145
}
146146
actions := fakeClient.Actions()
147147
if testCase.patch == "" {
148-
assert.Equal(t, 0, len(actions), "unexpected actions: %#v", actions)
148+
assert.Empty(t, actions, "unexpected actions")
149149
} else {
150-
assert.Equal(t, 1, len(actions), "unexpected actions: %#v", actions)
150+
assert.Len(t, actions, 1, "unexpected actions: %#v", actions)
151151
patchAction := actions[0].(core.PatchActionImpl)
152152
assert.Equal(t, testCase.patch, string(patchAction.Patch), "%d: unexpected patch: %s", i, string(patchAction.Patch))
153153
}

pkg/kubelet/cm/devicemanager/manager_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
365365
as.True(ok)
366366
as.Equal(int64(3), resource1Capacity.Value())
367367
as.Equal(int64(2), resource1Allocatable.Value())
368-
as.Equal(0, len(removedResources))
368+
as.Empty(removedResources)
369369

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

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

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

406406
// Tests adding another resource.
407407
resourceName2 := "resource2"
@@ -410,14 +410,14 @@ func TestUpdateCapacityAllocatable(t *testing.T) {
410410
testManager.endpoints[resourceName2] = endpointInfo{e: e2, opts: nil}
411411
callback(resourceName2, devs)
412412
capacity, allocatable, removedResources = testManager.GetCapacity()
413-
as.Equal(2, len(capacity))
413+
as.Len(capacity, 2)
414414
resource2Capacity, ok := capacity[v1.ResourceName(resourceName2)]
415415
as.True(ok)
416416
resource2Allocatable, ok := allocatable[v1.ResourceName(resourceName2)]
417417
as.True(ok)
418418
as.Equal(int64(3), resource2Capacity.Value())
419419
as.Equal(int64(1), resource2Allocatable.Value())
420-
as.Equal(0, len(removedResources))
420+
as.Empty(removedResources)
421421

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

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

508508
allocatableDevs := testManager.GetAllocatableDevices()
509-
as.Equal(2, len(allocatableDevs))
509+
as.Len(allocatableDevs, 2)
510510

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

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

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

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

16441644
expectedResps, err := allocateStubFunc()([]string{"dev1", "dev2"})
16451645
as.Nil(err)
1646-
as.Equal(1, len(expectedResps.ContainerResponses))
1646+
as.Len(expectedResps.ContainerResponses, 1)
16471647
expectedResp := expectedResps.ContainerResponses[0]
16481648
as.Equal(len(runContainerOpts.Devices), len(expectedResp.Devices))
16491649
as.Equal(len(runContainerOpts.Mounts), len(expectedResp.Mounts))

pkg/kubelet/cm/dra/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ func TestGetContainerClaimInfos(t *testing.T) {
14281428

14291429
fakeClaimInfos, err := manager.GetContainerClaimInfos(test.pod, test.container)
14301430
assert.NoError(t, err)
1431-
assert.Equal(t, 1, len(fakeClaimInfos))
1431+
assert.Len(t, fakeClaimInfos, 1)
14321432
assert.Equal(t, test.expectedClaimName, fakeClaimInfos[0].ClaimInfoState.ClaimName)
14331433

14341434
manager.cache.delete(test.pod.Spec.ResourceClaims[0].Name, "default")

pkg/kubelet/images/image_gc_manager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ func TestGarbageCollectImageTooOld(t *testing.T) {
780780
t.Log(fakeClock.Now())
781781
images, err := manager.imagesInEvictionOrder(ctx, fakeClock.Now())
782782
require.NoError(t, err)
783-
require.Equal(t, len(images), 1)
783+
require.Len(t, images, 1)
784784
// Simulate pod having just used this image, but having been GC'd
785785
images[0].lastUsed = fakeClock.Now()
786786

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

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

843843
oldStartTime := fakeClock.Now()

pkg/kubelet/images/image_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func TestPullAndListImageWithPodAnnotations(t *testing.T) {
374374
assert.Equal(t, c.expected[0].shouldRecordFinishedPullingTime, fakePodPullingTimeRecorder.finishedPullingRecorded)
375375

376376
images, _ := fakeRuntime.ListImages(ctx)
377-
assert.Equal(t, 1, len(images), "ListImages() count")
377+
assert.Len(t, images, 1, "ListImages() count")
378378

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

433433
images, _ := fakeRuntime.ListImages(ctx)
434-
assert.Equal(t, 1, len(images), "ListImages() count")
434+
assert.Len(t, images, 1, "ListImages() count")
435435

436436
image := images[0]
437437
assert.Equal(t, "missing_image:latest", image.ID, "Image ID")

0 commit comments

Comments
 (0)