diff --git a/pkg/audit/audit_test.go b/pkg/audit/audit_test.go index 3bac22fcd80..be9416fde13 100644 --- a/pkg/audit/audit_test.go +++ b/pkg/audit/audit_test.go @@ -94,7 +94,7 @@ func TestLocalLogBackendUsingFile(t *testing.T) { re := require.New(t) backend := NewLocalLogBackend(true) fname := initLog() - defer os.Remove(fname) + defer os.RemoveAll(fname) req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:2379/test?test=test", strings.NewReader("testBody")) re.False(backend.ProcessHTTPRequest(req)) info := requestutil.GetRequestInfo(req) @@ -126,7 +126,7 @@ func BenchmarkLocalLogAuditUsingFile(b *testing.B) { b.StopTimer() backend := NewLocalLogBackend(true) fname := initLog() - defer os.Remove(fname) + defer os.RemoveAll(fname) req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:2379/test?test=test", strings.NewReader("testBody")) b.StartTimer() for i := 0; i < b.N; i++ { diff --git a/pkg/election/leadership_test.go b/pkg/election/leadership_test.go index 73a941c3c1c..eec1b0c0b01 100644 --- a/pkg/election/leadership_test.go +++ b/pkg/election/leadership_test.go @@ -193,7 +193,7 @@ func TestExitWatch(t *testing.T) { // Case7: loss the quorum when the watch loop is running checkExitWatch(t, leaderKey, func(server *embed.Etcd, client *clientv3.Client) func() { tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests") - defer os.Remove(tempStdoutFile.Name()) + defer os.RemoveAll(tempStdoutFile.Name()) logCfg := &log.Config{} logCfg.File.Filename = tempStdoutFile.Name() logCfg.Level = "info" @@ -284,7 +284,7 @@ func checkExitWatch(t *testing.T, leaderKey string, injectFunc func(server *embe func TestRequestProgress(t *testing.T) { checkWatcherRequestProgress := func(injectWatchChanBlock bool) { tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests") - defer os.Remove(tempStdoutFile.Name()) + defer os.RemoveAll(tempStdoutFile.Name()) logCfg := &log.Config{} logCfg.File.Filename = tempStdoutFile.Name() logCfg.Level = "debug" diff --git a/pkg/utils/etcdutil/etcdutil_test.go b/pkg/utils/etcdutil/etcdutil_test.go index e9731de2329..bb006f50da8 100644 --- a/pkg/utils/etcdutil/etcdutil_test.go +++ b/pkg/utils/etcdutil/etcdutil_test.go @@ -772,7 +772,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() { func (suite *loopWatcherTestSuite) TestWatcherRequestProgress() { checkWatcherRequestProgress := func(injectWatchChanBlock bool) { tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests") - defer os.Remove(tempStdoutFile.Name()) + defer os.RemoveAll(tempStdoutFile.Name()) cfg := &log.Config{} cfg.File.Filename = tempStdoutFile.Name() cfg.Level = "debug" diff --git a/server/api/version_test.go b/server/api/version_test.go index 804704b084c..46bc80537a6 100644 --- a/server/api/version_test.go +++ b/server/api/version_test.go @@ -74,7 +74,7 @@ func TestGetVersion(t *testing.T) { func() { temp.Close() os.Stdout = old - os.Remove(fname) + os.RemoveAll(fname) svr.Close() cancel() testutil.CleanServer(cfg.DataDir) diff --git a/tests/integrations/mcs/tso/server_test.go b/tests/integrations/mcs/tso/server_test.go index 72430c507c7..d87f1542179 100644 --- a/tests/integrations/mcs/tso/server_test.go +++ b/tests/integrations/mcs/tso/server_test.go @@ -136,7 +136,7 @@ func (suite *tsoServerTestSuite) TestParticipantStartWithAdvertiseListenAddr() { re.NoError(err) // Setup the logger. - err = tests.InitTSOLogger(cfg) + err = tests.InitLogger(cfg.Log, cfg.Logger, cfg.LogProps, cfg.Security.RedactInfoLog) re.NoError(err) s, cleanup, err := tests.NewTSOTestServer(suite.ctx, cfg) diff --git a/tests/pdctl/config/config_test.go b/tests/pdctl/config/config_test.go index 5d74267a7b2..f43a964b50c 100644 --- a/tests/pdctl/config/config_test.go +++ b/tests/pdctl/config/config_test.go @@ -321,6 +321,7 @@ func TestPlacementRules(t *testing.T) { f, _ := os.CreateTemp("/tmp", "pd_tests") fname := f.Name() f.Close() + defer os.RemoveAll(fname) // test load _, err = pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "placement-rules", "load", "--out="+fname) @@ -493,9 +494,7 @@ func TestPlacementRuleBundle(t *testing.T) { re.NoError(err) fname := f.Name() f.Close() - defer func() { - os.RemoveAll(fname) - }() + defer os.RemoveAll(fname) // test load var bundles []placement.GroupBundle diff --git a/tests/scheduling_cluster.go b/tests/scheduling_cluster.go index 209371d197c..c74d73b0d4b 100644 --- a/tests/scheduling_cluster.go +++ b/tests/scheduling_cluster.go @@ -61,7 +61,7 @@ func (tc *TestSchedulingCluster) AddServer(addr string) error { if err != nil { return err } - err = InitSchedulingLogger(generatedCfg) + err = InitLogger(generatedCfg.Log, generatedCfg.Logger, generatedCfg.LogProps, generatedCfg.Security.RedactInfoLog) if err != nil { return err } diff --git a/tests/server/api/api_test.go b/tests/server/api/api_test.go index 243bdad399c..54603192abe 100644 --- a/tests/server/api/api_test.go +++ b/tests/server/api/api_test.go @@ -454,6 +454,7 @@ func (suite *middlewareTestSuite) TestAuditPrometheusBackend() { func (suite *middlewareTestSuite) TestAuditLocalLogBackend() { tempStdoutFile, _ := os.CreateTemp("/tmp", "pd_tests") + defer os.RemoveAll(tempStdoutFile.Name()) cfg := &log.Config{} cfg.File.Filename = tempStdoutFile.Name() cfg.Level = "info" diff --git a/tests/testutil.go b/tests/testutil.go index 9d7b06322c2..53efcff7658 100644 --- a/tests/testutil.go +++ b/tests/testutil.go @@ -29,34 +29,20 @@ import ( tso "github.com/tikv/pd/pkg/mcs/tso/server" "github.com/tikv/pd/pkg/utils/logutil" "github.com/tikv/pd/pkg/utils/testutil" + "go.uber.org/zap" ) var once sync.Once -// InitSchedulingLogger initializes the logger for test. -func InitSchedulingLogger(cfg *sc.Config) (err error) { +// InitLogger initializes the logger for test. +func InitLogger(logConfig log.Config, logger *zap.Logger, logProps *log.ZapProperties, isRedactInfoLogEnabled bool) (err error) { once.Do(func() { // Setup the logger. - err = logutil.SetupLogger(cfg.Log, &cfg.Logger, &cfg.LogProps, cfg.Security.RedactInfoLog) + err = logutil.SetupLogger(logConfig, &logger, &logProps, isRedactInfoLogEnabled) if err != nil { return } - log.ReplaceGlobals(cfg.Logger, cfg.LogProps) - // Flushing any buffered log entries. - log.Sync() - }) - return err -} - -// InitTSOLogger initializes the logger for test. -func InitTSOLogger(cfg *tso.Config) (err error) { - once.Do(func() { - // Setup the logger. - err = logutil.SetupLogger(cfg.Log, &cfg.Logger, &cfg.LogProps, cfg.Security.RedactInfoLog) - if err != nil { - return - } - log.ReplaceGlobals(cfg.Logger, cfg.LogProps) + log.ReplaceGlobals(logger, logProps) // Flushing any buffered log entries. log.Sync() }) @@ -88,7 +74,7 @@ func StartSingleTSOTestServerWithoutCheck(ctx context.Context, re *require.Asser cfg, err := tso.GenerateConfig(cfg) re.NoError(err) // Setup the logger. - err = InitTSOLogger(cfg) + err = InitLogger(cfg.Log, cfg.Logger, cfg.LogProps, cfg.Security.RedactInfoLog) re.NoError(err) return NewTSOTestServer(ctx, cfg) } diff --git a/tests/tso_cluster.go b/tests/tso_cluster.go index 1264cc8aed8..2f80f7ff970 100644 --- a/tests/tso_cluster.go +++ b/tests/tso_cluster.go @@ -114,7 +114,7 @@ func (tc *TestTSOCluster) AddServer(addr string) error { if err != nil { return err } - err = InitTSOLogger(generatedCfg) + err = InitLogger(generatedCfg.Log, generatedCfg.Logger, generatedCfg.LogProps, generatedCfg.Security.RedactInfoLog) if err != nil { return err } diff --git a/tools/pd-backup/pdbackup/backup_test.go b/tools/pd-backup/pdbackup/backup_test.go index d93fd77a336..7f77ebc104f 100644 --- a/tools/pd-backup/pdbackup/backup_test.go +++ b/tools/pd-backup/pdbackup/backup_test.go @@ -166,7 +166,7 @@ func (s *backupTestSuite) TestGetBackupInfo() { tmpFile, err := os.CreateTemp(os.TempDir(), "pd_backup_info_test.json") s.NoError(err) - defer os.Remove(tmpFile.Name()) + defer os.RemoveAll(tmpFile.Name()) s.NoError(OutputToFile(actual, tmpFile)) _, err = tmpFile.Seek(0, 0)