Skip to content

Commit e34f7f4

Browse files
authored
Merge pull request kubernetes#127671 from mmorel-35/testify/error-contains
fix: use `ErrorContains(t, err` instead of `Contains(t, err.Error()`
2 parents 0c713c7 + f777add commit e34f7f4

File tree

13 files changed

+21
-20
lines changed

13 files changed

+21
-20
lines changed

cmd/gotemplate/gotemplate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestGenerate(t *testing.T) {
7070
require.NoError(t, err, "expand template")
7171
require.Equal(t, tt.expected, out.String())
7272
} else {
73-
require.Contains(t, err.Error(), tt.expectedErr)
73+
require.ErrorContains(t, err, tt.expectedErr)
7474
}
7575
})
7676
}

cmd/kube-proxy/app/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func TestLoadConfigFailures(t *testing.T) {
315315
_, err := options.loadConfig([]byte(config))
316316

317317
require.Error(t, err, tc.name)
318-
require.Contains(t, err.Error(), tc.expErr)
318+
require.ErrorContains(t, err, tc.expErr)
319319

320320
if tc.checkFn != nil {
321321
require.True(t, tc.checkFn(err), tc.name)

cmd/kube-scheduler/app/options/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ profiles:
933933
if err != nil {
934934
if tc.expectedError != "" || tc.checkErrFn != nil {
935935
if tc.expectedError != "" {
936-
assert.Contains(t, err.Error(), tc.expectedError)
936+
assert.ErrorContains(t, err, tc.expectedError)
937937
}
938938
if tc.checkErrFn != nil {
939939
assert.True(t, tc.checkErrFn(err), "got error: %v", err)

cmd/kubelet/app/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ port: 123
315315
require.NoError(t, err, "failed to merge kubelet drop-in configs")
316316
} else {
317317
require.Error(t, err)
318-
require.Contains(t, err.Error(), test.expectMergeError)
318+
require.ErrorContains(t, err, test.expectMergeError)
319319
}
320320
}
321321

pkg/controller/podautoscaler/replica_calculator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (tc *replicaCalcTestCase) runTest(t *testing.T) {
362362

363363
if tc.expectedError != nil {
364364
require.Error(t, err, "there should be an error calculating the replica count")
365-
assert.Contains(t, err.Error(), tc.expectedError.Error(), "the error message should have contained the expected error message")
365+
assert.ErrorContains(t, err, tc.expectedError.Error(), "the error message should have contained the expected error message")
366366
return
367367
}
368368
require.NoError(t, err, "there should not have been an error calculating the replica count")
@@ -412,7 +412,7 @@ func (tc *replicaCalcTestCase) runTest(t *testing.T) {
412412

413413
if tc.expectedError != nil {
414414
require.Error(t, err, "there should be an error calculating the replica count")
415-
assert.Contains(t, err.Error(), tc.expectedError.Error(), "the error message should have contained the expected error message")
415+
assert.ErrorContains(t, err, tc.expectedError.Error(), "the error message should have contained the expected error message")
416416
return
417417
}
418418
require.NoError(t, err, "there should not have been an error calculating the replica count")

pkg/kubelet/cm/cpumanager/state/state_checkpoint_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func TestCheckpointStateRestore(t *testing.T) {
224224
require.NoError(t, err)
225225
} else {
226226
require.Error(t, err)
227-
require.Contains(t, err.Error(), "could not restore state from checkpoint")
228-
require.Contains(t, err.Error(), tc.expectedError)
227+
require.ErrorContains(t, err, "could not restore state from checkpoint")
228+
require.ErrorContains(t, err, tc.expectedError)
229229
return
230230
}
231231

pkg/kubelet/cm/dra/manager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func TestPrepareResources(t *testing.T) {
597597
if test.expectedErrMsg != "" {
598598
assert.Error(t, err)
599599
if err != nil {
600-
assert.Contains(t, err.Error(), test.expectedErrMsg)
600+
assert.ErrorContains(t, err, test.expectedErrMsg)
601601
}
602602
return // PrepareResources returned an error so stopping the test case here
603603
}
@@ -739,7 +739,7 @@ func TestUnprepareResources(t *testing.T) {
739739
if test.expectedErrMsg != "" {
740740
assert.Error(t, err)
741741
if err != nil {
742-
assert.Contains(t, err.Error(), test.expectedErrMsg)
742+
assert.ErrorContains(t, err, test.expectedErrMsg)
743743
}
744744
return // PrepareResources returned an error so stopping the test case here
745745
}
@@ -863,7 +863,7 @@ func TestGetContainerClaimInfos(t *testing.T) {
863863
if test.expectedErrMsg != "" {
864864
assert.Error(t, err)
865865
if err != nil {
866-
assert.Contains(t, err.Error(), test.expectedErrMsg)
866+
assert.ErrorContains(t, err, test.expectedErrMsg)
867867
}
868868
return
869869
}

pkg/kubelet/cm/memorymanager/state/state_checkpoint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestCheckpointStateRestore(t *testing.T) {
134134
restoredState, err := NewCheckpointState(testingDir, testingCheckpoint, "static")
135135
if strings.TrimSpace(tc.expectedError) != "" {
136136
assert.Error(t, err)
137-
assert.Contains(t, err.Error(), "could not restore state from checkpoint: "+tc.expectedError)
137+
assert.ErrorContains(t, err, "could not restore state from checkpoint: "+tc.expectedError)
138138
} else {
139139
assert.NoError(t, err, "unexpected error while creating checkpointState")
140140
// compare state after restoration with the one expected

pkg/kubelet/kuberuntime/kuberuntime_container_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ func TestGenerateLinuxConfigSupplementalGroupsPolicy(t *testing.T) {
780780
} else {
781781
assert.NotEmpty(t, err, "Unexpected success")
782782
assert.Empty(t, actual, "Unexpected non empty value")
783-
assert.Contains(t, err.Error(), tc.expectedErrMsg, "Error for %s", tc.name)
783+
assert.ErrorContainsf(t, err, tc.expectedErrMsg, "Error for %s", tc.name)
784784
}
785785
})
786786
}

pkg/kubelet/volumemanager/reconciler/reconciler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ func Test_GenerateMapVolumeFunc_Plugin_Not_Found(t *testing.T) {
11421142
err := oex.MountVolume(waitForAttachTimeout, volumeToMount, asw, false)
11431143
// Assert
11441144
if assert.Error(t, err) {
1145-
assert.Contains(t, err.Error(), tc.expectedErrMsg)
1145+
assert.ErrorContains(t, err, tc.expectedErrMsg)
11461146
}
11471147
})
11481148
}
@@ -1184,7 +1184,7 @@ func Test_GenerateUnmapVolumeFunc_Plugin_Not_Found(t *testing.T) {
11841184
err := oex.UnmountVolume(volumeToUnmount, asw, "" /* podsDir */)
11851185
// Assert
11861186
if assert.Error(t, err) {
1187-
assert.Contains(t, err.Error(), tc.expectedErrMsg)
1187+
assert.ErrorContains(t, err, tc.expectedErrMsg)
11881188
}
11891189
})
11901190
}
@@ -1225,7 +1225,7 @@ func Test_GenerateUnmapDeviceFunc_Plugin_Not_Found(t *testing.T) {
12251225
err := oex.UnmountDevice(deviceToDetach, asw, hostutil)
12261226
// Assert
12271227
if assert.Error(t, err) {
1228-
assert.Contains(t, err.Error(), tc.expectedErrMsg)
1228+
assert.ErrorContains(t, err, tc.expectedErrMsg)
12291229
}
12301230
})
12311231
}

0 commit comments

Comments
 (0)