From e9714b6af1bbc41724ae1c0433b9221adb9bf286 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Thu, 12 Sep 2024 14:43:18 +0800 Subject: [PATCH] *: use the same path for tests (#8603) ref tikv/pd#7969 Signed-off-by: Ryan Leung Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- Makefile | 4 +--- pkg/mcs/scheduling/server/rule/watcher_test.go | 3 ++- pkg/utils/etcdutil/testutil.go | 2 +- pkg/utils/testutil/testutil.go | 2 +- server/server_test.go | 5 +++-- server/testutil.go | 2 +- tests/config.go | 2 +- tests/integrations/client/global_config_test.go | 2 +- tools/pd-backup/pdbackup/backup_test.go | 2 +- tools/pd-ctl/tests/config/config_test.go | 6 +++--- tools/pd-simulator/simulator/config/config.go | 2 +- tools/pd-ut/ut.go | 4 ++-- 12 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 34c3be775be..0cba7051a29 100644 --- a/Makefile +++ b/Makefile @@ -315,9 +315,7 @@ CLEAN_UT_BINARY := find . -name '*.test.bin'| xargs rm -f clean-test: # Cleaning test tmp... - rm -rf /tmp/test_pd* - rm -rf /tmp/pd-tests* - rm -rf /tmp/test_etcd* + rm -rf /tmp/pd_tests* rm -f $(REAL_CLUSTER_TEST_PATH)/playground.log go clean -testcache @$(CLEAN_UT_BINARY) diff --git a/pkg/mcs/scheduling/server/rule/watcher_test.go b/pkg/mcs/scheduling/server/rule/watcher_test.go index bf9ea0afc54..d3d20888c2d 100644 --- a/pkg/mcs/scheduling/server/rule/watcher_test.go +++ b/pkg/mcs/scheduling/server/rule/watcher_test.go @@ -18,6 +18,7 @@ import ( "context" "encoding/json" "os" + "path/filepath" "strconv" "testing" "time" @@ -83,7 +84,7 @@ func prepare(t require.TestingT) (context.Context, *clientv3.Client, func()) { re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) cfg := etcdutil.NewTestSingleConfig() - cfg.Dir = os.TempDir() + "/test_etcd" + cfg.Dir = filepath.Join(os.TempDir(), "/pd_tests") os.RemoveAll(cfg.Dir) etcd, err := embed.StartEtcd(cfg) re.NoError(err) diff --git a/pkg/utils/etcdutil/testutil.go b/pkg/utils/etcdutil/testutil.go index e67ae415736..7cb718664d9 100644 --- a/pkg/utils/etcdutil/testutil.go +++ b/pkg/utils/etcdutil/testutil.go @@ -51,7 +51,7 @@ func NewTestSingleConfig() *embed.Config { } func genRandName() string { - return "test_etcd_" + strconv.FormatInt(time.Now().UnixNano(), 10) + return "pd" + strconv.FormatInt(time.Now().UnixNano(), 10) } // NewTestEtcdCluster is used to create a etcd cluster for the unit test purpose. diff --git a/pkg/utils/testutil/testutil.go b/pkg/utils/testutil/testutil.go index 76f7058738b..55e44039591 100644 --- a/pkg/utils/testutil/testutil.go +++ b/pkg/utils/testutil/testutil.go @@ -94,7 +94,7 @@ func CleanServer(dataDir string) { // InitTempFileLogger initializes the logger and redirects the log output to a temporary file. func InitTempFileLogger(level string) (fname string) { cfg := &log.Config{} - f, _ := os.CreateTemp(os.TempDir(), "pd_tests") + f, _ := os.CreateTemp("", "pd_tests") fname = f.Name() f.Close() cfg.File.Filename = fname diff --git a/server/server_test.go b/server/server_test.go index 50c116083e0..af76146df36 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "net/http" + "os" "path/filepath" "testing" @@ -290,8 +291,8 @@ func TestCheckClusterID(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() cfgs := NewTestMultiConfig(assertutil.CheckerWithNilAssert(re), 2) - for i, cfg := range cfgs { - cfg.DataDir = fmt.Sprintf("/tmp/test_pd_check_clusterID_%d", i) + for _, cfg := range cfgs { + cfg.DataDir, _ = os.MkdirTemp("", "pd_tests") // Clean up before testing. testutil.CleanServer(cfg.DataDir) } diff --git a/server/testutil.go b/server/testutil.go index 7b159d7d94b..2c844bf3d6b 100644 --- a/server/testutil.go +++ b/server/testutil.go @@ -78,7 +78,7 @@ func NewTestSingleConfig(c *assertutil.Checker) *config.Config { cfg.AdvertiseClientUrls = cfg.ClientUrls cfg.AdvertisePeerUrls = cfg.PeerUrls - cfg.DataDir, _ = os.MkdirTemp(os.TempDir(), "test_pd") + cfg.DataDir, _ = os.MkdirTemp("", "pd_tests") cfg.InitialCluster = fmt.Sprintf("pd=%s", cfg.PeerUrls) cfg.DisableStrictReconfigCheck = true cfg.TickInterval = typeutil.NewDuration(100 * time.Millisecond) diff --git a/tests/config.go b/tests/config.go index 62dd5f08c1f..ee184f0f90e 100644 --- a/tests/config.go +++ b/tests/config.go @@ -36,7 +36,7 @@ type serverConfig struct { } func newServerConfig(name string, cc *clusterConfig, join bool) *serverConfig { - tempDir, _ := os.MkdirTemp(os.TempDir(), "pd-tests") + tempDir, _ := os.MkdirTemp("", "pd_tests") return &serverConfig{ Name: name, DataDir: tempDir, diff --git a/tests/integrations/client/global_config_test.go b/tests/integrations/client/global_config_test.go index d813ec99676..5eed743d8ba 100644 --- a/tests/integrations/client/global_config_test.go +++ b/tests/integrations/client/global_config_test.go @@ -378,7 +378,7 @@ func (suite *globalConfigTestSuite) TestEtcdNotStart() { re.Error(err) _, err = suite.server.LoadGlobalConfig(suite.server.Context(), &pdpb.LoadGlobalConfigRequest{ - Names: []string{"test_etcd"}, + Names: []string{"pd_tests"}, }) re.Error(err) } diff --git a/tools/pd-backup/pdbackup/backup_test.go b/tools/pd-backup/pdbackup/backup_test.go index 8124763551b..d23f1878b85 100644 --- a/tools/pd-backup/pdbackup/backup_test.go +++ b/tools/pd-backup/pdbackup/backup_test.go @@ -142,7 +142,7 @@ func (s *backupTestSuite) TestGetBackupInfo() { } re.Equal(expected, actual) - tmpFile, err := os.CreateTemp(os.TempDir(), "pd_backup_info_test.json") + tmpFile, err := os.CreateTemp("", "pd_tests") re.NoError(err) defer os.RemoveAll(tmpFile.Name()) diff --git a/tools/pd-ctl/tests/config/config_test.go b/tools/pd-ctl/tests/config/config_test.go index 0404796a81e..d4b1e90ec1b 100644 --- a/tools/pd-ctl/tests/config/config_test.go +++ b/tools/pd-ctl/tests/config/config_test.go @@ -358,7 +358,7 @@ func (suite *configTestSuite) checkConfigForwardControl(cluster *pdTests.TestClu leaderServer := cluster.GetLeaderServer() pdAddr := leaderServer.GetAddr() - f, _ := os.CreateTemp(os.TempDir(), "pd_tests") + f, _ := os.CreateTemp("", "pd_tests") fname := f.Name() f.Close() defer os.RemoveAll(fname) @@ -571,7 +571,7 @@ func (suite *configTestSuite) checkPlacementRules(cluster *pdTests.TestCluster) // test show checkShowRuleKey(re, pdAddr, [][2]string{{placement.DefaultGroupID, placement.DefaultRuleID}}) - f, _ := os.CreateTemp(os.TempDir(), "pd_tests") + f, _ := os.CreateTemp("", "pd_tests") fname := f.Name() f.Close() defer os.RemoveAll(fname) @@ -718,7 +718,7 @@ func (suite *configTestSuite) checkPlacementRuleBundle(cluster *pdTests.TestClus re.NoError(json.Unmarshal(output, &bundle)) re.Equal(placement.GroupBundle{ID: placement.DefaultGroupID, Index: 0, Override: false, Rules: []*placement.Rule{{GroupID: placement.DefaultGroupID, ID: placement.DefaultRuleID, Role: placement.Voter, Count: 3}}}, bundle) - f, err := os.CreateTemp(os.TempDir(), "pd_tests") + f, err := os.CreateTemp("", "pd_tests") re.NoError(err) fname := f.Name() f.Close() diff --git a/tools/pd-simulator/simulator/config/config.go b/tools/pd-simulator/simulator/config/config.go index 32c483ceba7..ece3b6fd91f 100644 --- a/tools/pd-simulator/simulator/config/config.go +++ b/tools/pd-simulator/simulator/config/config.go @@ -98,7 +98,7 @@ func NewSimConfig(serverLogLevel string) *SimConfig { cfg.AdvertiseClientUrls = cfg.ClientUrls cfg.AdvertisePeerUrls = cfg.PeerUrls - cfg.DataDir, _ = os.MkdirTemp(os.TempDir(), "test_pd") + cfg.DataDir, _ = os.MkdirTemp("", "pd_tests") cfg.InitialCluster = fmt.Sprintf("pd=%s", cfg.PeerUrls) cfg.Log.Level = serverLogLevel return &SimConfig{ServerConfig: cfg} diff --git a/tools/pd-ut/ut.go b/tools/pd-ut/ut.go index 6b366a137a2..d7786feac1d 100644 --- a/tools/pd-ut/ut.go +++ b/tools/pd-ut/ut.go @@ -119,7 +119,7 @@ func main() { if coverProfile != "" { var err error - coverFileTempDir, err = os.MkdirTemp(os.TempDir(), "cov") + coverFileTempDir, err = os.MkdirTemp("", "cov") if err != nil { fmt.Println("create temp dir fail", coverFileTempDir) os.Exit(1) @@ -771,7 +771,7 @@ func buildTestBinaryMulti(pkgs []string) ([]byte, error) { cmd.Args = append(cmd.Args, "-race") } cmd.Dir = workDir - outputFile, err := os.CreateTemp("", "test_pd_ut*.out") + outputFile, err := os.CreateTemp("", "pd_tests") if err != nil { return nil, err }