From a47e80964e4cfb8e2416641b5b8c590261322452 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Wed, 30 Oct 2024 16:13:48 +0800 Subject: [PATCH] add ms cluster case Signed-off-by: Ryan Leung --- .../{real_cluster.go => cluster.go} | 98 ++++++++++++++++--- .../realcluster/cluster_id_test.go | 4 +- .../realcluster/reboot_pd_test.go | 4 +- .../realcluster/scheduler_test.go | 4 +- tests/integrations/realcluster/ts_test.go | 40 +++++++- 5 files changed, 129 insertions(+), 21 deletions(-) rename tests/integrations/realcluster/{real_cluster.go => cluster.go} (62%) diff --git a/tests/integrations/realcluster/real_cluster.go b/tests/integrations/realcluster/cluster.go similarity index 62% rename from tests/integrations/realcluster/real_cluster.go rename to tests/integrations/realcluster/cluster.go index 441a13f4a73..40fe6bef94f 100644 --- a/tests/integrations/realcluster/real_cluster.go +++ b/tests/integrations/realcluster/cluster.go @@ -29,7 +29,7 @@ import ( "go.uber.org/zap" ) -type realClusterSuite struct { +type clusterSuite struct { suite.Suite clusterCnt int @@ -42,7 +42,7 @@ var ( ) // SetupSuite will run before the tests in the suite are run. -func (s *realClusterSuite) SetupSuite() { +func (s *clusterSuite) SetupSuite() { t := s.T() // Clean the data dir. It is the default data dir of TiUP. @@ -60,7 +60,7 @@ func (s *realClusterSuite) SetupSuite() { } // TearDownSuite will run after all the tests in the suite have been run. -func (s *realClusterSuite) TearDownSuite() { +func (s *clusterSuite) TearDownSuite() { // Even if the cluster deployment fails, we still need to destroy the cluster. // If the cluster does not fail to deploy, the cluster will be destroyed in // the cleanup function. And these code will not work. @@ -68,16 +68,16 @@ func (s *realClusterSuite) TearDownSuite() { s.stopRealCluster(s.T()) } -func (s *realClusterSuite) startRealCluster(t *testing.T) { +func (s *clusterSuite) startRealCluster(t *testing.T) { log.Info("start to deploy a real cluster") tag := s.tag() - deployTiupPlayground(t, tag) + deployTiupPlayground(t, tag, false) waitTiupReady(t, tag) s.clusterCnt++ } -func (s *realClusterSuite) stopRealCluster(t *testing.T) { +func (s *clusterSuite) stopRealCluster(t *testing.T) { s.clusterCnt-- log.Info("start to destroy a real cluster", zap.String("tag", s.tag())) @@ -85,11 +85,11 @@ func (s *realClusterSuite) stopRealCluster(t *testing.T) { time.Sleep(5 * time.Second) } -func (s *realClusterSuite) tag() string { +func (s *clusterSuite) tag() string { return fmt.Sprintf("pd_real_cluster_test_%s_%d", s.suiteName, s.clusterCnt) } -func (s *realClusterSuite) restart() { +func (s *clusterSuite) restart() { tag := s.tag() log.Info("start to restart", zap.String("tag", tag)) s.stopRealCluster(s.T()) @@ -111,7 +111,7 @@ func destroy(t *testing.T, tag string) { log.Info("destroy success", zap.String("tag", tag)) } -func deployTiupPlayground(t *testing.T, tag string) { +func deployTiupPlayground(t *testing.T, tag string, ms bool) { curPath, err := os.Getwd() require.NoError(t, err) require.NoError(t, os.Chdir("../../..")) @@ -130,15 +130,32 @@ func deployTiupPlayground(t *testing.T, tag string) { if !fileExists(playgroundLogDir) { require.NoError(t, os.MkdirAll(playgroundLogDir, 0755)) } + // nolint:errcheck go func() { - runCommand("sh", "-c", - tiupBin+` playground nightly --kv 3 --tiflash 1 --db 1 --pd 3 \ - --without-monitor --tag `+tag+` --pd.binpath ./bin/pd-server \ + if ms { + runCommand("sh", "-c", + tiupBin+` playground nightly --pd.mode ms --kv 3 --tiflash 1 --db 1 --pd 3 --tso 1 --scheduling 1 \ + --without-monitor --tag `+tag+` \ + --pd.binpath ./bin/pd-server \ + --kv.binpath ./third_bin/tikv-server \ + --db.binpath ./third_bin/tidb-server \ + --tiflash.binpath ./third_bin/tiflash \ + --tso.binpath ./bin/pd-server \ + --scheduling.binpath ./bin/pd-server \ + --pd.config ./tests/integrations/realcluster/pd.toml \ + > `+filepath.Join(playgroundLogDir, tag+".log")+` 2>&1 & `) + } else { + runCommand("sh", "-c", + tiupBin+` playground nightly --kv 3 --tiflash 1 --db 1 --pd 3 \ + --without-monitor --tag `+tag+` \ + --pd.binpath ./bin/pd-server \ --kv.binpath ./third_bin/tikv-server \ - --db.binpath ./third_bin/tidb-server --tiflash.binpath ./third_bin/tiflash \ + --db.binpath ./third_bin/tidb-server \ + --tiflash.binpath ./third_bin/tiflash \ --pd.config ./tests/integrations/realcluster/pd.toml \ > `+filepath.Join(playgroundLogDir, tag+".log")+` 2>&1 & `) + } }() // Avoid to change the dir before execute `tiup playground`. @@ -165,3 +182,58 @@ func waitTiupReady(t *testing.T, tag string) { } require.Failf(t, "TiUP is not ready", "tag: %s", tag) } + +type msClusterSuite struct { + suite.Suite + + clusterCnt int + suiteName string +} + +// SetupSuite will run before the tests in the suite are run. +func (s *msClusterSuite) SetupSuite() { + t := s.T() + + // Clean the data dir. It is the default data dir of TiUP. + dataDir := filepath.Join(os.Getenv("HOME"), ".tiup", "data", "pd_ms_cluster_test_"+s.suiteName+"_*") + matches, err := filepath.Glob(dataDir) + require.NoError(t, err) + + for _, match := range matches { + require.NoError(t, runCommand("rm", "-rf", match)) + } + s.startCluster(t) + t.Cleanup(func() { + s.stopCluster(t) + }) +} + +// TearDownSuite will run after all the tests in the suite have been run. +func (s *msClusterSuite) TearDownSuite() { + // Even if the cluster deployment fails, we still need to destroy the cluster. + // If the cluster does not fail to deploy, the cluster will be destroyed in + // the cleanup function. And these code will not work. + s.clusterCnt++ + s.stopCluster(s.T()) +} + +func (s *msClusterSuite) startCluster(t *testing.T) { + log.Info("start to deploy a ms cluster") + + tag := s.tag() + deployTiupPlayground(t, tag, true) + waitTiupReady(t, tag) + s.clusterCnt++ +} + +func (s *msClusterSuite) stopCluster(t *testing.T) { + s.clusterCnt-- + + log.Info("start to destroy a ms cluster", zap.String("tag", s.tag())) + destroy(t, s.tag()) + time.Sleep(5 * time.Second) +} + +func (s *msClusterSuite) tag() string { + return fmt.Sprintf("pd_ms_cluster_test_%s_%d", s.suiteName, s.clusterCnt) +} diff --git a/tests/integrations/realcluster/cluster_id_test.go b/tests/integrations/realcluster/cluster_id_test.go index 27bede7fa1d..eb7db24ef80 100644 --- a/tests/integrations/realcluster/cluster_id_test.go +++ b/tests/integrations/realcluster/cluster_id_test.go @@ -26,12 +26,12 @@ import ( ) type clusterIDSuite struct { - realClusterSuite + clusterSuite } func TestClusterID(t *testing.T) { suite.Run(t, &clusterIDSuite{ - realClusterSuite: realClusterSuite{ + clusterSuite: clusterSuite{ suiteName: "cluster_id", }, }) diff --git a/tests/integrations/realcluster/reboot_pd_test.go b/tests/integrations/realcluster/reboot_pd_test.go index e3f37ac0605..d35a921cb74 100644 --- a/tests/integrations/realcluster/reboot_pd_test.go +++ b/tests/integrations/realcluster/reboot_pd_test.go @@ -24,12 +24,12 @@ import ( ) type rebootPDSuite struct { - realClusterSuite + clusterSuite } func TestRebootPD(t *testing.T) { suite.Run(t, &rebootPDSuite{ - realClusterSuite: realClusterSuite{ + clusterSuite: clusterSuite{ suiteName: "reboot_pd", }, }) diff --git a/tests/integrations/realcluster/scheduler_test.go b/tests/integrations/realcluster/scheduler_test.go index c0aff2669e9..c5bb2acee0e 100644 --- a/tests/integrations/realcluster/scheduler_test.go +++ b/tests/integrations/realcluster/scheduler_test.go @@ -30,12 +30,12 @@ import ( ) type schedulerSuite struct { - realClusterSuite + clusterSuite } func TestScheduler(t *testing.T) { suite.Run(t, &schedulerSuite{ - realClusterSuite: realClusterSuite{ + clusterSuite: clusterSuite{ suiteName: "scheduler", }, }) diff --git a/tests/integrations/realcluster/ts_test.go b/tests/integrations/realcluster/ts_test.go index f19124d04a4..79ad970cd0b 100644 --- a/tests/integrations/realcluster/ts_test.go +++ b/tests/integrations/realcluster/ts_test.go @@ -22,12 +22,12 @@ import ( ) type tsSuite struct { - realClusterSuite + clusterSuite } func TestTS(t *testing.T) { suite.Run(t, &tsSuite{ - realClusterSuite: realClusterSuite{ + clusterSuite: clusterSuite{ suiteName: "ts", }, }) @@ -56,3 +56,39 @@ func (s *tsSuite) TestTS() { db.MustClose() } + +type msTSSuite struct { + msClusterSuite +} + +func TestMSTS(t *testing.T) { + suite.Run(t, &msTSSuite{ + msClusterSuite: msClusterSuite{ + suiteName: "ts", + }, + }) +} + +func (s *msTSSuite) TestTS() { + re := require.New(s.T()) + + db := OpenTestDB(s.T()) + db.MustExec("use test") + db.MustExec("drop table if exists t") + db.MustExec("create table t(a int, index i(a))") + db.MustExec("insert t values (1), (2), (3)") + var rows int + err := db.inner.Raw("select count(*) from t").Row().Scan(&rows) + re.NoError(err) + re.Equal(3, rows) + + re.NoError(err) + re.Equal(3, rows) + + var ts uint64 + err = db.inner.Begin().Raw("select @@tidb_current_ts").Scan(&ts).Rollback().Error + re.NoError(err) + re.NotEqual(0, GetTimeFromTS(ts)) + + db.MustClose() +}