From c05646e3527787b54f0407e47a71242b8f7db460 Mon Sep 17 00:00:00 2001 From: D3Hunter Date: Tue, 21 Jan 2025 18:05:38 +0800 Subject: [PATCH] test: fix error checking (#59092) ref pingcap/tidb#57275 --- br/pkg/stream/stream_metas_test.go | 4 ++-- br/pkg/streamhelper/integration_test.go | 4 ++-- br/pkg/task/stream_test.go | 4 ++-- br/pkg/utils/backoff_test.go | 2 +- pkg/ddl/tests/tiflash/ddl_tiflash_test.go | 2 +- pkg/executor/sortexec/topn_chunk_heap.go | 2 +- pkg/executor/test/admintest/admin_test.go | 2 +- pkg/expression/helper_test.go | 2 +- pkg/expression/integration_test/integration_test.go | 12 ++++++------ pkg/owner/manager_test.go | 4 ++-- pkg/planner/core/plan_cache_lru_test.go | 2 +- pkg/table/column_test.go | 4 ++-- pkg/table/temptable/interceptor_test.go | 2 +- 13 files changed, 23 insertions(+), 23 deletions(-) diff --git a/br/pkg/stream/stream_metas_test.go b/br/pkg/stream/stream_metas_test.go index d45ac44646eb1..4d6be57ebd3a3 100644 --- a/br/pkg/stream/stream_metas_test.go +++ b/br/pkg/stream/stream_metas_test.go @@ -2672,7 +2672,7 @@ func TestRetry(t *testing.T) { est := MigrationExtension(s) mg := est.MergeAndMigrateTo(ctx, 2, MMOptSkipLockingInTest()) require.Len(t, mg.Warnings, 1) - require.Error(t, mg.Warnings[0], "this disk remembers nothing") + require.ErrorContains(t, mg.Warnings[0], "this disk remembers nothing") requireMigrationsEqual(t, mg.NewBase, mig(mDel(mN(1), lN(1), lN(2)))) mg = est.MergeAndMigrateTo(ctx, 2, MMOptSkipLockingInTest()) @@ -2703,7 +2703,7 @@ func TestRetryRemoveCompaction(t *testing.T) { est := MigrationExtension(s) mg := est.migrateTo(ctx, mig1) require.Len(t, mg.Warnings, 1) - require.Error(t, mg.Warnings[0], "this disk will never forget") + require.ErrorContains(t, mg.Warnings[0], "this disk will never forget") requireMigrationsEqual(t, mg.NewBase, mig( mCompaction(placeholder(cDir(2)), placeholder(aDir(2)), 28, 32), mTruncatedTo(27), diff --git a/br/pkg/streamhelper/integration_test.go b/br/pkg/streamhelper/integration_test.go index ff7c23b138fbe..dd762a55efbc4 100644 --- a/br/pkg/streamhelper/integration_test.go +++ b/br/pkg/streamhelper/integration_test.go @@ -317,7 +317,7 @@ func testStreamListening(t *testing.T, metaCli streamhelper.AdvancerExt) { fifth, ok := <-ch require.True(t, ok) require.Equal(t, fifth.Type, streamhelper.EventErr) - require.Error(t, fifth.Err, context.Canceled) + require.ErrorIs(t, fifth.Err, context.Canceled) item, ok := <-ch require.False(t, ok, "%v", item) } @@ -349,7 +349,7 @@ func testStreamClose(t *testing.T, metaCli streamhelper.AdvancerExt) { third := <-ch require.Equal(t, third.Type, streamhelper.EventErr) - require.Error(t, third.Err, io.EOF) + require.ErrorIs(t, third.Err, io.EOF) item, ok := <-ch require.False(t, ok, "%#v", item) } diff --git a/br/pkg/task/stream_test.go b/br/pkg/task/stream_test.go index 847699bc152cd..f3393a22a03d1 100644 --- a/br/pkg/task/stream_test.go +++ b/br/pkg/task/stream_test.go @@ -193,8 +193,8 @@ func TestGetLogRangeWithFullBackupDir(t *testing.T) { Storage: testDir, } _, err = getLogRange(context.TODO(), &cfg) - require.Error(t, err, errors.Annotate(berrors.ErrStorageUnknown, - "the storage has been used for full backup")) + require.ErrorIs(t, err, berrors.ErrStorageUnknown) + require.ErrorContains(t, err, "the storage has been used for full backup") } func TestGetLogRangeWithLogBackupDir(t *testing.T) { diff --git a/br/pkg/utils/backoff_test.go b/br/pkg/utils/backoff_test.go index e63c2776f1472..140e91e725d9d 100644 --- a/br/pkg/utils/backoff_test.go +++ b/br/pkg/utils/backoff_test.go @@ -101,7 +101,7 @@ func TestWithRetryReturnLastErr(t *testing.T) { return nil }, backoffStrategy) require.Equal(t, 4, counter) - require.ErrorIs(t, berrors.ErrKVRangeIsEmpty, err) + require.ErrorIs(t, err, berrors.ErrKVRangeIsEmpty) } func TestBackoffWithFatalRawGRPCError(t *testing.T) { diff --git a/pkg/ddl/tests/tiflash/ddl_tiflash_test.go b/pkg/ddl/tests/tiflash/ddl_tiflash_test.go index 1486624f87fd6..82000ba6eebdd 100644 --- a/pkg/ddl/tests/tiflash/ddl_tiflash_test.go +++ b/pkg/ddl/tests/tiflash/ddl_tiflash_test.go @@ -946,7 +946,7 @@ func TestTiFlashBatchKill(t *testing.T) { require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/ddl/FastFailCheckTiFlashPendingTables")) }() timeOut, err := execWithTimeout(t, tk, time.Second*2000, "alter database tiflash_ddl_limit set tiflash replica 1") - require.Error(t, err, "[executor:1317]Query execution was interrupted") + require.ErrorContains(t, err, "[executor:1317]Query execution was interrupted") require.False(t, timeOut) wg.Wait() } diff --git a/pkg/executor/sortexec/topn_chunk_heap.go b/pkg/executor/sortexec/topn_chunk_heap.go index 94508b2268b84..567ef241814b0 100644 --- a/pkg/executor/sortexec/topn_chunk_heap.go +++ b/pkg/executor/sortexec/topn_chunk_heap.go @@ -185,5 +185,5 @@ func TestKillSignalInTopN(t *testing.T, topnExec *TopNExec) { topnExec.Ctx().GetSessionVars().SQLKiller.SendKillSignal(sqlkiller.QueryInterrupted) err = topnExec.spillHelper.spillHeap(chkHeap) - require.Error(t, err, exeerrors.ErrQueryInterrupted.GenWithStackByArgs()) + require.ErrorIs(t, err, exeerrors.ErrQueryInterrupted) } diff --git a/pkg/executor/test/admintest/admin_test.go b/pkg/executor/test/admintest/admin_test.go index dda1139c486f7..f79e28500d8cf 100644 --- a/pkg/executor/test/admintest/admin_test.go +++ b/pkg/executor/test/admintest/admin_test.go @@ -1461,7 +1461,7 @@ func TestAdminCheckTableFailed(t *testing.T) { require.NoError(t, err) err = tk.ExecToErr("admin check table admin_test") require.Error(t, err) - require.Error(t, err, "[admin:8223]data inconsistency in table: admin_test, index: c2, handle: 2, index-values:\"\" != record-values:\"handle: 2, values: [KindInt64 12]\"") + require.ErrorContains(t, err, "[admin:8223]data inconsistency in table: admin_test, index: c2") tk.MustExec("set @@tidb_redact_log=1;") err = tk.ExecToErr("admin check table admin_test") require.Error(t, err) diff --git a/pkg/expression/helper_test.go b/pkg/expression/helper_test.go index e55d2d36885b3..4b07d6ffb73d8 100644 --- a/pkg/expression/helper_test.go +++ b/pkg/expression/helper_test.go @@ -58,7 +58,7 @@ func TestGetTimeValue(t *testing.T) { require.Equal(t, "2012-12-12 00:00:00", timeValue.String()) err = sessionVars.SetSystemVar("timestamp", "") - require.Error(t, err, "Incorrect argument type to variable 'timestamp'") + require.ErrorContains(t, err, "Incorrect argument type to variable 'timestamp'") v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, types.MinFsp, nil) require.NoError(t, err) diff --git a/pkg/expression/integration_test/integration_test.go b/pkg/expression/integration_test/integration_test.go index 18240631e4769..7d120515e56c0 100644 --- a/pkg/expression/integration_test/integration_test.go +++ b/pkg/expression/integration_test/integration_test.go @@ -3058,7 +3058,7 @@ func TestTimeBuiltin(t *testing.T) { "period_add(0, 20)", "period_add(0, 0)", "period_add(-1, 1)", "period_add(200013, 1)", "period_add(-200012, 1)", "period_add('', '')", } { err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod)) - require.Error(t, err, "[expression:1210]Incorrect arguments to period_add") + require.ErrorContains(t, err, "[expression:1210]Incorrect arguments to period_add") } // for period_diff @@ -3070,7 +3070,7 @@ func TestTimeBuiltin(t *testing.T) { "period_diff(-00013,1)", "period_diff(00013,1)", "period_diff(0, 0)", "period_diff(200013, 1)", "period_diff(5612, 4513)", "period_diff('', '')", } { err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod)) - require.Error(t, err, "[expression:1210]Incorrect arguments to period_diff") + require.ErrorContains(t, err, "[expression:1210]Incorrect arguments to period_diff") } // TODO: fix `CAST(xx as duration)` and release the test below: @@ -3844,12 +3844,12 @@ func TestSetVariables(t *testing.T) { require.NoError(t, err) _, err = tk.Exec("INSERT INTO tab0 select cast('999:44:33' as time);") require.Error(t, err) - require.Error(t, err, "[types:1292]Truncated incorrect time value: '999:44:33'") + require.ErrorContains(t, err, "[types:1292]Truncated incorrect time value: '999:44:33'") _, err = tk.Exec("set sql_mode=' ,';") require.Error(t, err) _, err = tk.Exec("INSERT INTO tab0 select cast('999:44:33' as time);") require.Error(t, err) - require.Error(t, err, "[types:1292]Truncated incorrect time value: '999:44:33'") + require.ErrorContains(t, err, "[types:1292]Truncated incorrect time value: '999:44:33'") // issue #5478 _, err = tk.Exec("set session transaction read write;") @@ -3893,10 +3893,10 @@ func TestSetVariables(t *testing.T) { _, err = tk.Exec("set @@global.max_user_connections='';") require.Error(t, err) - require.Error(t, err, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_user_connections").Error()) + require.ErrorContains(t, err, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_user_connections").Error()) _, err = tk.Exec("set @@global.max_prepared_stmt_count='';") require.Error(t, err) - require.Error(t, err, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_prepared_stmt_count").Error()) + require.ErrorContains(t, err, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_prepared_stmt_count").Error()) // Previously global values were cached. This is incorrect. // See: https://github.com/pingcap/tidb/issues/24368 diff --git a/pkg/owner/manager_test.go b/pkg/owner/manager_test.go index b1dd667b37c56..628f4beb84f46 100644 --- a/pkg/owner/manager_test.go +++ b/pkg/owner/manager_test.go @@ -225,7 +225,7 @@ func TestSetAndGetOwnerOpValue(t *testing.T) { // test del owner key when SetOwnerOpValue require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/owner/MockDelOwnerKey", `return("delOwnerKeyAndNotOwner")`)) err = ownerMgr.SetOwnerOpValue(context.Background(), owner.OpNone) - require.Error(t, err, "put owner key failed, cmp is false") + require.ErrorContains(t, err, "put owner key failed, cmp is false") op, err = owner.GetOwnerOpValue(context.Background(), tInfo.client, "/owner/key") require.NotNil(t, err) require.Equal(t, concurrency.ErrElectionNoLeader.Error(), err.Error()) @@ -241,7 +241,7 @@ func TestSetAndGetOwnerOpValue(t *testing.T) { // And then the manager campaigns the owner again, and become the owner. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/owner/MockDelOwnerKey", `return("onlyDelOwnerKey")`)) err = ownerMgr.SetOwnerOpValue(context.Background(), owner.OpSyncUpgradingState) - require.Error(t, err, "put owner key failed, cmp is false") + require.ErrorContains(t, err, "put owner key failed, cmp is false") isOwner = checkOwner(ownerMgr, true) require.True(t, isOwner) op, err = owner.GetOwnerOpValue(context.Background(), tInfo.client, "/owner/key") diff --git a/pkg/planner/core/plan_cache_lru_test.go b/pkg/planner/core/plan_cache_lru_test.go index 178fa21df599e..6b4810dabcbdd 100644 --- a/pkg/planner/core/plan_cache_lru_test.go +++ b/pkg/planner/core/plan_cache_lru_test.go @@ -316,7 +316,7 @@ func TestLRUPCSetCapacity(t *testing.T) { require.Nil(t, root) err = lru.SetCapacity(0) - require.Error(t, err, "capacity of LRU cache should be at least 1") + require.ErrorContains(t, err, "capacity of LRU cache should be at least 1") } func TestIssue37914(t *testing.T) { diff --git a/pkg/table/column_test.go b/pkg/table/column_test.go index 37505f2e4e18d..6e7133129ad9b 100644 --- a/pkg/table/column_test.go +++ b/pkg/table/column_test.go @@ -325,10 +325,10 @@ func TestCastValue(t *testing.T) { require.NoError(t, err) require.Equal(t, "utf8mb4_general_ci", val.Collation()) val, err = CastValue(ctx, types.NewBinaryLiteralDatum([]byte{0xE5, 0xA5, 0xBD, 0x81}), &colInfoS, false, false) - require.Error(t, err, "[table:1366]Incorrect string value '\\x81' for column ''") + require.ErrorContains(t, err, "[table:1366]Incorrect string value '\\x81' for column ''") require.Equal(t, "utf8mb4_general_ci", val.Collation()) val, err = CastValue(ctx, types.NewDatum([]byte{0xE5, 0xA5, 0xBD, 0x81}), &colInfoS, false, false) - require.Error(t, err, "[table:1366]Incorrect string value '\\x81' for column ''") + require.ErrorContains(t, err, "[table:1366]Incorrect string value '\\x81' for column ''") require.Equal(t, "utf8mb4_general_ci", val.Collation()) } diff --git a/pkg/table/temptable/interceptor_test.go b/pkg/table/temptable/interceptor_test.go index 4988beff6ba33..70bae043d9322 100644 --- a/pkg/table/temptable/interceptor_test.go +++ b/pkg/table/temptable/interceptor_test.go @@ -314,7 +314,7 @@ func TestGetSessionTemporaryTableKey(t *testing.T) { // test normal table should not be allowed val, err = getSessionKey(ctx, normalTb.Meta(), retriever, encodeTableKey(1)) - require.Error(t, err, "Cannot get normal table key from session") + require.ErrorContains(t, err, "Cannot get normal table key from session") require.Nil(t, val) require.Equal(t, 0, len(retriever.GetInvokes()))