|
| 1 | +// Copyright 2024 TiKV Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package realcluster |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/pingcap/log" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "github.com/stretchr/testify/suite" |
| 29 | + "go.uber.org/zap" |
| 30 | +) |
| 31 | + |
| 32 | +type clusterSuite struct { |
| 33 | + suite.Suite |
| 34 | + |
| 35 | + clusterCnt int |
| 36 | + suiteName string |
| 37 | + ms bool |
| 38 | +} |
| 39 | + |
| 40 | +var ( |
| 41 | + playgroundLogDir = "/tmp/real_cluster/playground" |
| 42 | + tiupBin = os.Getenv("HOME") + "/.tiup/bin/tiup" |
| 43 | +) |
| 44 | + |
| 45 | +// SetupSuite will run before the tests in the suite are run. |
| 46 | +func (s *clusterSuite) SetupSuite() { |
| 47 | + t := s.T() |
| 48 | + |
| 49 | + // Clean the data dir. It is the default data dir of TiUP. |
| 50 | + dataDir := filepath.Join(os.Getenv("HOME"), ".tiup", "data", "pd_real_cluster_test_"+s.suiteName+"_*") |
| 51 | + matches, err := filepath.Glob(dataDir) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + for _, match := range matches { |
| 55 | + require.NoError(t, runCommand("rm", "-rf", match)) |
| 56 | + } |
| 57 | + s.startCluster(t) |
| 58 | + t.Cleanup(func() { |
| 59 | + s.stopCluster(t) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +// TearDownSuite will run after all the tests in the suite have been run. |
| 64 | +func (s *clusterSuite) TearDownSuite() { |
| 65 | + // Even if the cluster deployment fails, we still need to destroy the cluster. |
| 66 | + // If the cluster does not fail to deploy, the cluster will be destroyed in |
| 67 | + // the cleanup function. And these code will not work. |
| 68 | + s.clusterCnt++ |
| 69 | + s.stopCluster(s.T()) |
| 70 | +} |
| 71 | + |
| 72 | +func (s *clusterSuite) startCluster(t *testing.T) { |
| 73 | + log.Info("start to deploy a cluster", zap.Bool("ms", s.ms)) |
| 74 | + |
| 75 | + tag := s.tag() |
| 76 | + deployTiupPlayground(t, tag, s.ms) |
| 77 | + waitTiupReady(t, tag) |
| 78 | + s.clusterCnt++ |
| 79 | +} |
| 80 | + |
| 81 | +func (s *clusterSuite) stopCluster(t *testing.T) { |
| 82 | + s.clusterCnt-- |
| 83 | + |
| 84 | + log.Info("start to destroy a cluster", zap.String("tag", s.tag()), zap.Bool("ms", s.ms)) |
| 85 | + destroy(t, s.tag()) |
| 86 | + time.Sleep(5 * time.Second) |
| 87 | +} |
| 88 | + |
| 89 | +func (s *clusterSuite) tag() string { |
| 90 | + return fmt.Sprintf("pd_real_cluster_test_%s_%d", s.suiteName, s.clusterCnt) |
| 91 | +} |
| 92 | + |
| 93 | +func (s *clusterSuite) restart() { |
| 94 | + tag := s.tag() |
| 95 | + log.Info("start to restart", zap.String("tag", tag)) |
| 96 | + s.stopCluster(s.T()) |
| 97 | + s.startCluster(s.T()) |
| 98 | + log.Info("TiUP restart success") |
| 99 | +} |
| 100 | + |
| 101 | +func destroy(t *testing.T, tag string) { |
| 102 | + cmdStr := fmt.Sprintf("ps -ef | grep %s | awk '{print $2}'", tag) |
| 103 | + cmd := exec.Command("sh", "-c", cmdStr) |
| 104 | + bytes, err := cmd.Output() |
| 105 | + require.NoError(t, err) |
| 106 | + pids := string(bytes) |
| 107 | + pidArr := strings.Split(pids, "\n") |
| 108 | + for _, pid := range pidArr { |
| 109 | + // nolint:errcheck |
| 110 | + runCommand("sh", "-c", "kill -9 "+pid) |
| 111 | + } |
| 112 | + log.Info("destroy success", zap.String("tag", tag)) |
| 113 | +} |
| 114 | + |
| 115 | +func deployTiupPlayground(t *testing.T, tag string, ms bool) { |
| 116 | + curPath, err := os.Getwd() |
| 117 | + require.NoError(t, err) |
| 118 | + require.NoError(t, os.Chdir("../../..")) |
| 119 | + |
| 120 | + if !fileExists("third_bin") || !fileExists("third_bin/tikv-server") || !fileExists("third_bin/tidb-server") || !fileExists("third_bin/tiflash") { |
| 121 | + log.Info("downloading binaries...") |
| 122 | + log.Info("this may take a few minutes, you can also download them manually and put them in the bin directory.") |
| 123 | + require.NoError(t, runCommand("sh", |
| 124 | + "./tests/integrations/realcluster/download_integration_test_binaries.sh")) |
| 125 | + } |
| 126 | + if !fileExists("bin") || !fileExists("bin/pd-server") { |
| 127 | + log.Info("complie pd binaries...") |
| 128 | + require.NoError(t, runCommand("make", "pd-server")) |
| 129 | + } |
| 130 | + |
| 131 | + if !fileExists(playgroundLogDir) { |
| 132 | + require.NoError(t, os.MkdirAll(playgroundLogDir, 0755)) |
| 133 | + } |
| 134 | + |
| 135 | + // nolint:errcheck |
| 136 | + go func() { |
| 137 | + if ms { |
| 138 | + runCommand("sh", "-c", |
| 139 | + tiupBin+` playground nightly --pd.mode ms --kv 3 --tiflash 1 --db 1 --pd 3 --tso 1 --scheduling 1 \ |
| 140 | + --without-monitor --tag `+tag+` \ |
| 141 | + --pd.binpath ./bin/pd-server \ |
| 142 | + --kv.binpath ./third_bin/tikv-server \ |
| 143 | + --db.binpath ./third_bin/tidb-server \ |
| 144 | + --tiflash.binpath ./third_bin/tiflash \ |
| 145 | + --tso.binpath ./bin/pd-server \ |
| 146 | + --scheduling.binpath ./bin/pd-server \ |
| 147 | + --pd.config ./tests/integrations/realcluster/pd.toml \ |
| 148 | + > `+filepath.Join(playgroundLogDir, tag+".log")+` 2>&1 & `) |
| 149 | + } else { |
| 150 | + runCommand("sh", "-c", |
| 151 | + tiupBin+` playground nightly --kv 3 --tiflash 1 --db 1 --pd 3 \ |
| 152 | + --without-monitor --tag `+tag+` \ |
| 153 | + --pd.binpath ./bin/pd-server \ |
| 154 | + --kv.binpath ./third_bin/tikv-server \ |
| 155 | + --db.binpath ./third_bin/tidb-server \ |
| 156 | + --tiflash.binpath ./third_bin/tiflash \ |
| 157 | + --pd.config ./tests/integrations/realcluster/pd.toml \ |
| 158 | + > `+filepath.Join(playgroundLogDir, tag+".log")+` 2>&1 & `) |
| 159 | + } |
| 160 | + }() |
| 161 | + |
| 162 | + // Avoid to change the dir before execute `tiup playground`. |
| 163 | + time.Sleep(10 * time.Second) |
| 164 | + require.NoError(t, os.Chdir(curPath)) |
| 165 | +} |
| 166 | + |
| 167 | +func waitTiupReady(t *testing.T, tag string) { |
| 168 | + const ( |
| 169 | + interval = 5 |
| 170 | + maxTimes = 20 |
| 171 | + ) |
| 172 | + log.Info("start to wait TiUP ready", zap.String("tag", tag)) |
| 173 | + for i := range maxTimes { |
| 174 | + err := runCommand(tiupBin, "playground", "display", "--tag", tag) |
| 175 | + if err == nil { |
| 176 | + log.Info("TiUP is ready", zap.String("tag", tag)) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + log.Info("TiUP is not ready, will retry", zap.Int("retry times", i), |
| 181 | + zap.String("tag", tag), zap.Error(err)) |
| 182 | + time.Sleep(time.Duration(interval) * time.Second) |
| 183 | + } |
| 184 | + require.Failf(t, "TiUP is not ready", "tag: %s", tag) |
| 185 | +} |
0 commit comments