From a7520ea67224ba0bcd36f0596f6eb3ffa6340bfc Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Wed, 15 Jan 2025 13:55:15 +0800 Subject: [PATCH] clean up the testing environment Signed-off-by: Ryan Leung --- tests/integrations/client/client_test.go | 4 +--- tests/integrations/mcs/tso/api_test.go | 12 +++++----- .../mcs/tso/keyspace_group_manager_test.go | 4 ++-- tests/integrations/mcs/tso/proxy_test.go | 22 +++++++++---------- tests/integrations/tso/client_test.go | 10 ++++----- tests/server/cluster/cluster_test.go | 2 ++ .../region_syncer/region_syncer_test.go | 6 ++--- .../server/storage/hot_region_storage_test.go | 8 ++++--- tools/pd-backup/tests/backup_test.go | 2 +- tools/pd-ctl/tests/keyspace/keyspace_test.go | 7 ++---- tools/pd-ctl/tests/store/store_test.go | 2 +- .../tests/unsafe/unsafe_operation_test.go | 1 - 12 files changed, 38 insertions(+), 42 deletions(-) diff --git a/tests/integrations/client/client_test.go b/tests/integrations/client/client_test.go index 6bd25567f81..0e0bf25d74d 100644 --- a/tests/integrations/client/client_test.go +++ b/tests/integrations/client/client_test.go @@ -229,10 +229,10 @@ func TestGetTSAfterTransferLeader(t *testing.T) { defer cancel() cluster, err := tests.NewTestCluster(ctx, 2) re.NoError(err) + defer cluster.Destroy() endpoints := runServer(re, cluster) leader := cluster.WaitLeader() re.NotEmpty(leader) - defer cluster.Destroy() cli := setupCli(ctx, re, endpoints, opt.WithCustomTimeoutOption(10*time.Second)) defer cli.Close() @@ -517,8 +517,6 @@ func (suite *followerForwardAndHandleTestSuite) SetupSuite() { }) } -func (*followerForwardAndHandleTestSuite) TearDownTest() {} - func (suite *followerForwardAndHandleTestSuite) TearDownSuite() { suite.cluster.Destroy() suite.clean() diff --git a/tests/integrations/mcs/tso/api_test.go b/tests/integrations/mcs/tso/api_test.go index aa153a7eb9f..7f874a52eb2 100644 --- a/tests/integrations/mcs/tso/api_test.go +++ b/tests/integrations/mcs/tso/api_test.go @@ -137,12 +137,12 @@ func TestTSOServerStartFirst(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - apiCluster, err := tests.NewTestClusterWithKeyspaceGroup(ctx, 1, func(conf *config.Config, _ string) { + cluster, err := tests.NewTestClusterWithKeyspaceGroup(ctx, 1, func(conf *config.Config, _ string) { conf.Keyspace.PreAlloc = []string{"k1", "k2"} }) - defer apiCluster.Destroy() + defer cluster.Destroy() re.NoError(err) - addr := apiCluster.GetConfig().GetClientURL() + addr := cluster.GetConfig().GetClientURL() ch := make(chan struct{}) defer close(ch) clusterCh := make(chan *tests.TestTSOCluster) @@ -155,11 +155,11 @@ func TestTSOServerStartFirst(t *testing.T) { clusterCh <- tsoCluster ch <- struct{}{} }() - err = apiCluster.RunInitialServers() + err = cluster.RunInitialServers() re.NoError(err) - leaderName := apiCluster.WaitLeader() + leaderName := cluster.WaitLeader() re.NotEmpty(leaderName) - pdLeaderServer := apiCluster.GetServer(leaderName) + pdLeaderServer := cluster.GetServer(leaderName) re.NoError(pdLeaderServer.BootstrapCluster()) re.NoError(err) tsoCluster := <-clusterCh diff --git a/tests/integrations/mcs/tso/keyspace_group_manager_test.go b/tests/integrations/mcs/tso/keyspace_group_manager_test.go index 51ace0f08c4..9bd5073b5a5 100644 --- a/tests/integrations/mcs/tso/keyspace_group_manager_test.go +++ b/tests/integrations/mcs/tso/keyspace_group_manager_test.go @@ -544,12 +544,12 @@ func TestTwiceSplitKeyspaceGroup(t *testing.T) { } }) re.NoError(err) + defer tc.Destroy() pdAddr := tc.GetConfig().GetClientURL() // Start PD and tso server. err = tc.RunInitialServers() re.NoError(err) - defer tc.Destroy() tc.WaitLeader() leaderServer := tc.GetLeaderServer() re.NoError(leaderServer.BootstrapCluster()) @@ -741,12 +741,12 @@ func TestGetTSOImmediately(t *testing.T) { } }) re.NoError(err) + defer tc.Destroy() pdAddr := tc.GetConfig().GetClientURL() // Start PD and tso server. err = tc.RunInitialServers() re.NoError(err) - defer tc.Destroy() tc.WaitLeader() leaderServer := tc.GetLeaderServer() re.NoError(leaderServer.BootstrapCluster()) diff --git a/tests/integrations/mcs/tso/proxy_test.go b/tests/integrations/mcs/tso/proxy_test.go index 99213742e6c..a0c161c5a70 100644 --- a/tests/integrations/mcs/tso/proxy_test.go +++ b/tests/integrations/mcs/tso/proxy_test.go @@ -43,8 +43,8 @@ type tsoProxyTestSuite struct { suite.Suite ctx context.Context cancel context.CancelFunc - apiCluster *tests.TestCluster - apiLeader *tests.TestServer + cluster *tests.TestCluster + leader *tests.TestServer backendEndpoints string tsoCluster *tests.TestTSOCluster defaultReq *pdpb.TsoRequest @@ -62,15 +62,15 @@ func (s *tsoProxyTestSuite) SetupSuite() { var err error s.ctx, s.cancel = context.WithCancel(context.Background()) // Create an API cluster with 1 server - s.apiCluster, err = tests.NewTestClusterWithKeyspaceGroup(s.ctx, 1) + s.cluster, err = tests.NewTestClusterWithKeyspaceGroup(s.ctx, 1) re.NoError(err) - err = s.apiCluster.RunInitialServers() + err = s.cluster.RunInitialServers() re.NoError(err) - leaderName := s.apiCluster.WaitLeader() + leaderName := s.cluster.WaitLeader() re.NotEmpty(leaderName) - s.apiLeader = s.apiCluster.GetServer(leaderName) - s.backendEndpoints = s.apiLeader.GetAddr() - re.NoError(s.apiLeader.BootstrapCluster()) + s.leader = s.cluster.GetServer(leaderName) + s.backendEndpoints = s.leader.GetAddr() + re.NoError(s.leader.BootstrapCluster()) // Create a TSO cluster with 2 servers s.tsoCluster, err = tests.NewTestTSOCluster(s.ctx, 2, s.backendEndpoints) @@ -78,7 +78,7 @@ func (s *tsoProxyTestSuite) SetupSuite() { s.tsoCluster.WaitForDefaultPrimaryServing(re) s.defaultReq = &pdpb.TsoRequest{ - Header: &pdpb.RequestHeader{ClusterId: s.apiLeader.GetClusterID()}, + Header: &pdpb.RequestHeader{ClusterId: s.leader.GetClusterID()}, Count: 1, } @@ -89,7 +89,7 @@ func (s *tsoProxyTestSuite) SetupSuite() { func (s *tsoProxyTestSuite) TearDownSuite() { cleanupGRPCStreams(s.cleanupFuncs) s.tsoCluster.Destroy() - s.apiCluster.Destroy() + s.cluster.Destroy() s.cancel() } @@ -362,7 +362,7 @@ func (s *tsoProxyTestSuite) generateRequests(requestsPerClient int) []*pdpb.TsoR reqs := make([]*pdpb.TsoRequest, requestsPerClient) for i := range requestsPerClient { reqs[i] = &pdpb.TsoRequest{ - Header: &pdpb.RequestHeader{ClusterId: s.apiLeader.GetClusterID()}, + Header: &pdpb.RequestHeader{ClusterId: s.leader.GetClusterID()}, Count: uint32(i) + 1, // Make sure the count is positive. } } diff --git a/tests/integrations/tso/client_test.go b/tests/integrations/tso/client_test.go index 4822d6439ca..5cd74c705fc 100644 --- a/tests/integrations/tso/client_test.go +++ b/tests/integrations/tso/client_test.go @@ -498,9 +498,9 @@ func TestMixedTSODeployment(t *testing.T) { }() ctx, cancel := context.WithCancel(context.Background()) + defer cancel() cluster, err := tests.NewTestCluster(ctx, 1) re.NoError(err) - defer cancel() defer cluster.Destroy() err = cluster.RunInitialServers() @@ -542,10 +542,12 @@ func TestMixedTSODeployment(t *testing.T) { func TestUpgradingPDAndTSOClusters(t *testing.T) { re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) + defer cancel() // Create an PD cluster which has 3 servers pdCluster, err := tests.NewTestClusterWithKeyspaceGroup(ctx, 3) re.NoError(err) + defer pdCluster.Destroy() err = pdCluster.RunInitialServers() re.NoError(err) leaderName := pdCluster.WaitLeader() @@ -569,7 +571,7 @@ func TestUpgradingPDAndTSOClusters(t *testing.T) { mcs.WaitForTSOServiceAvailable(ctx, re, pdClient) // Restart the API cluster - pdCluster, err = tests.RestartTestPDCluster(ctx, pdCluster) + _, err = tests.RestartTestPDCluster(ctx, pdCluster) re.NoError(err) // The TSO service should be eventually healthy mcs.WaitForTSOServiceAvailable(ctx, re, pdClient) @@ -577,12 +579,10 @@ func TestUpgradingPDAndTSOClusters(t *testing.T) { // Restart the TSO cluster tsoCluster, err = tests.RestartTestTSOCluster(ctx, tsoCluster) re.NoError(err) + defer tsoCluster.Destroy() // The TSO service should be eventually healthy mcs.WaitForTSOServiceAvailable(ctx, re, pdClient) - tsoCluster.Destroy() - pdCluster.Destroy() - cancel() re.NoError(failpoint.Disable("github.com/tikv/pd/client/servicediscovery/usePDServiceMode")) } diff --git a/tests/server/cluster/cluster_test.go b/tests/server/cluster/cluster_test.go index 357a76ace21..4ee4a9c8cf4 100644 --- a/tests/server/cluster/cluster_test.go +++ b/tests/server/cluster/cluster_test.go @@ -640,9 +640,11 @@ func TestRaftClusterStartTSOJob(t *testing.T) { name := "pd1" // case 1: normal start ctx, cancel := context.WithCancel(context.Background()) + defer cancel() tc, err := tests.NewTestCluster(ctx, 1, func(conf *config.Config, _ string) { conf.LeaderLease = 300 }) + defer tc.Destroy() re.NoError(err) re.NoError(tc.RunInitialServers()) re.NotEmpty(tc.WaitLeader()) diff --git a/tests/server/region_syncer/region_syncer_test.go b/tests/server/region_syncer/region_syncer_test.go index 7edcaa5ad9b..3a00d33fbaf 100644 --- a/tests/server/region_syncer/region_syncer_test.go +++ b/tests/server/region_syncer/region_syncer_test.go @@ -38,15 +38,13 @@ func TestMain(m *testing.M) { func TestRegionSyncer(t *testing.T) { re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) + defer cancel() re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/storage/levelDBStorageFastFlush", `return(true)`)) re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/syncer/noFastExitSync", `return(true)`)) re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/syncer/disableClientStreaming", `return(true)`)) cluster, err := tests.NewTestCluster(ctx, 3, func(conf *config.Config, _ string) { conf.PDServerCfg.UseRegionStorage = true }) - defer func() { - cluster.Destroy() - cancel() - }() + defer cluster.Destroy() re.NoError(err) re.NoError(cluster.RunInitialServers()) diff --git a/tests/server/storage/hot_region_storage_test.go b/tests/server/storage/hot_region_storage_test.go index 0643dda08d5..e34d6021f1d 100644 --- a/tests/server/storage/hot_region_storage_test.go +++ b/tests/server/storage/hot_region_storage_test.go @@ -46,6 +46,7 @@ func TestHotRegionStorage(t *testing.T) { }, ) re.NoError(err) + defer cluster.Destroy() err = cluster.RunInitialServers() re.NoError(err) re.NotEmpty(cluster.WaitLeader()) @@ -67,7 +68,7 @@ func TestHotRegionStorage(t *testing.T) { for _, store := range stores { tests.MustPutStore(re, cluster, store) } - defer cluster.Destroy() + startTime := time.Now().Unix() tests.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"), core.SetWrittenBytes(3000000000), core.SetReportInterval(uint64(startTime-utils.RegionHeartBeatReportInterval), uint64(startTime))) @@ -154,6 +155,7 @@ func TestHotRegionStorageReservedDayConfigChange(t *testing.T) { }, ) re.NoError(err) + defer cluster.Destroy() err = cluster.RunInitialServers() re.NoError(err) re.NotEmpty(cluster.WaitLeader()) @@ -175,7 +177,7 @@ func TestHotRegionStorageReservedDayConfigChange(t *testing.T) { for _, store := range stores { tests.MustPutStore(re, cluster, store) } - defer cluster.Destroy() + startTime := time.Now().Unix() tests.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"), core.SetWrittenBytes(3000000000), core.SetReportInterval(uint64(startTime-utils.RegionHeartBeatReportInterval), uint64(startTime))) @@ -246,6 +248,7 @@ func TestHotRegionStorageWriteIntervalConfigChange(t *testing.T) { }, ) re.NoError(err) + defer cluster.Destroy() err = cluster.RunInitialServers() re.NoError(err) re.NotEmpty(cluster.WaitLeader()) @@ -267,7 +270,6 @@ func TestHotRegionStorageWriteIntervalConfigChange(t *testing.T) { for _, store := range stores { tests.MustPutStore(re, cluster, store) } - defer cluster.Destroy() startTime := time.Now().Unix() tests.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"), core.SetWrittenBytes(3000000000), diff --git a/tools/pd-backup/tests/backup_test.go b/tools/pd-backup/tests/backup_test.go index 7c8c03d96e0..2e1297fe034 100644 --- a/tools/pd-backup/tests/backup_test.go +++ b/tools/pd-backup/tests/backup_test.go @@ -35,6 +35,7 @@ func TestBackup(t *testing.T) { defer cancel() cluster, err := tests.NewTestCluster(ctx, 1) re.NoError(err) + defer cluster.Destroy() err = cluster.RunInitialServers() re.NoError(err) re.NotEmpty(cluster.WaitLeader()) @@ -42,7 +43,6 @@ func TestBackup(t *testing.T) { leaderServer.BootstrapCluster() pdAddr := cluster.GetConfig().GetClientURL() urls := strings.Split(pdAddr, ",") - defer cluster.Destroy() client, err := clientv3.New(clientv3.Config{ Endpoints: urls, DialTimeout: 3 * time.Second, diff --git a/tools/pd-ctl/tests/keyspace/keyspace_test.go b/tools/pd-ctl/tests/keyspace/keyspace_test.go index e6c28de599f..c17d496c6c5 100644 --- a/tools/pd-ctl/tests/keyspace/keyspace_test.go +++ b/tools/pd-ctl/tests/keyspace/keyspace_test.go @@ -167,13 +167,10 @@ func (suite *keyspaceTestSuite) SetupTest() { func (suite *keyspaceTestSuite) TearDownTest() { re := suite.Require() - re.NoError(failpoint.Disable("github.com/tikv/pd/server/delayStartServerLoop")) - re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/skipSplitRegion")) -} - -func (suite *keyspaceTestSuite) TearDownSuite() { suite.cancel() suite.cluster.Destroy() + re.NoError(failpoint.Disable("github.com/tikv/pd/server/delayStartServerLoop")) + re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/skipSplitRegion")) } func (suite *keyspaceTestSuite) TestShowKeyspace() { diff --git a/tools/pd-ctl/tests/store/store_test.go b/tools/pd-ctl/tests/store/store_test.go index 8ff36b79abe..414991c97ec 100644 --- a/tools/pd-ctl/tests/store/store_test.go +++ b/tools/pd-ctl/tests/store/store_test.go @@ -46,6 +46,7 @@ func TestStoreLimitV2(t *testing.T) { defer cancel() cluster, err := pdTests.NewTestCluster(ctx, 1) re.NoError(err) + defer cluster.Destroy() err = cluster.RunInitialServers() re.NoError(err) re.NotEmpty(cluster.WaitLeader()) @@ -54,7 +55,6 @@ func TestStoreLimitV2(t *testing.T) { leaderServer := cluster.GetLeaderServer() re.NoError(leaderServer.BootstrapCluster()) - defer cluster.Destroy() // store command args := []string{"-u", pdAddr, "config", "set", "store-limit-version", "v2"} diff --git a/tools/pd-ctl/tests/unsafe/unsafe_operation_test.go b/tools/pd-ctl/tests/unsafe/unsafe_operation_test.go index 652645e1570..86f11f43647 100644 --- a/tools/pd-ctl/tests/unsafe/unsafe_operation_test.go +++ b/tools/pd-ctl/tests/unsafe/unsafe_operation_test.go @@ -39,7 +39,6 @@ func TestRemoveFailedStores(t *testing.T) { re.NoError(err) pdAddr := cluster.GetConfig().GetClientURL() cmd := ctl.GetRootCmd() - defer cluster.Destroy() args := []string{"-u", pdAddr, "unsafe", "remove-failed-stores", "1,2,3"} _, err = tests.ExecuteCommand(cmd, args...)