Skip to content

Commit

Permalink
realclustertest: apply new style for some test cases (#8732)
Browse files Browse the repository at this point in the history
ref #8683

Signed-off-by: okJiang <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
okJiang and ti-chi-bot[bot] authored Oct 22, 2024
1 parent 0402e15 commit b155a7b
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 260 deletions.
58 changes: 23 additions & 35 deletions tests/integrations/realcluster/real_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

Expand All @@ -37,17 +38,9 @@ type realClusterSuite struct {

var (
playgroundLogDir = filepath.Join("tmp", "real_cluster", "playground")
tiupBin string
tiupBin = os.Getenv("HOME") + "/.tiup/bin/tiup"
)

func init() {
var err error
tiupBin, err = exec.LookPath("tiup")
if err != nil {
panic(err)
}
}

// SetupSuite will run before the tests in the suite are run.
func (s *realClusterSuite) SetupSuite() {
t := s.T()
Expand Down Expand Up @@ -78,7 +71,9 @@ func (s *realClusterSuite) TearDownSuite() {
func (s *realClusterSuite) startRealCluster(t *testing.T) {
log.Info("start to deploy a real cluster")

s.deploy(t)
tag := s.tag()
deployTiupPlayground(t, tag)
waitTiupReady(t, tag)
s.clusterCnt++
}

Expand All @@ -94,33 +89,26 @@ func (s *realClusterSuite) tag() string {
return fmt.Sprintf("pd_real_cluster_test_%s_%d", s.suiteName, s.clusterCnt)
}

// func restartTiUP() {
// log.Info("start to restart TiUP")
// cmd := exec.Command("make", "deploy")
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
// err := cmd.Run()
// if err != nil {
// panic(err)
// }
// log.Info("TiUP restart success")
// }

func (s *realClusterSuite) deploy(t *testing.T) {
func (s *realClusterSuite) restart() {
tag := s.tag()
deployTiupPlayground(t, tag)
waitTiupReady(t, tag)
log.Info("start to restart", zap.String("tag", tag))
s.stopRealCluster(s.T())
s.startRealCluster(s.T())
log.Info("TiUP restart success")
}

func destroy(t *testing.T, tag string) {
cmdStr := fmt.Sprintf("ps -ef | grep 'tiup playground' | grep %s | awk '{print $2}' | head -n 1", tag)
cmdStr := fmt.Sprintf("ps -ef | grep %s | awk '{print $2}'", tag)
cmd := exec.Command("sh", "-c", cmdStr)
bytes, err := cmd.Output()
require.NoError(t, err)
pid := string(bytes)
// nolint:errcheck
runCommand("sh", "-c", "kill -9 "+pid)
log.Info("destroy success", zap.String("pid", pid))
pids := string(bytes)
pidArr := strings.Split(pids, "\n")
for _, pid := range pidArr {
// nolint:errcheck
runCommand("sh", "-c", "kill -9 "+pid)
}
log.Info("destroy success", zap.String("tag", tag))
}

func deployTiupPlayground(t *testing.T, tag string) {
Expand All @@ -146,11 +134,11 @@ func deployTiupPlayground(t *testing.T, tag string) {
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 \
--kv.binpath ./third_bin/tikv-server \
--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 & `)
--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 \
--pd.config ./tests/integrations/realcluster/pd.toml \
> `+filepath.Join(playgroundLogDir, tag+".log")+` 2>&1 & `)
}()

// Avoid to change the dir before execute `tiup playground`.
Expand Down
105 changes: 64 additions & 41 deletions tests/integrations/realcluster/reboot_pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,68 @@

package realcluster

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/client/http"
)

type rebootPDSuite struct {
realClusterSuite
}

func TestRebootPD(t *testing.T) {
suite.Run(t, &rebootPDSuite{
realClusterSuite: realClusterSuite{
suiteName: "reboot_pd",
},
})
}

// https://github.com/tikv/pd/issues/6467
// func TestReloadLabel(t *testing.T) {
// re := require.New(t)
// ctx := context.Background()

// resp, err := pdHTTPCli.GetStores(ctx)
// re.NoError(err)
// re.NotEmpty(resp.Stores)
// firstStore := resp.Stores[0]
// // TiFlash labels will be ["engine": "tiflash"]
// // So we need to merge the labels
// storeLabels := map[string]string{
// "zone": "zone1",
// }
// for _, label := range firstStore.Store.Labels {
// storeLabels[label.Key] = label.Value
// }
// re.NoError(pdHTTPCli.SetStoreLabels(ctx, firstStore.Store.ID, storeLabels))
// defer func() {
// re.NoError(pdHTTPCli.DeleteStoreLabel(ctx, firstStore.Store.ID, "zone"))
// }()

// checkLabelsAreEqual := func() {
// resp, err := pdHTTPCli.GetStore(ctx, uint64(firstStore.Store.ID))
// re.NoError(err)

// labelsMap := make(map[string]string)
// for _, label := range resp.Store.Labels {
// re.NotNil(label)
// labelsMap[label.Key] = label.Value
// }

// for key, value := range storeLabels {
// re.Equal(value, labelsMap[key])
// }
// }
// // Check the label is set
// checkLabelsAreEqual()
// // Restart TiUP to reload the label
// restartTiUP()
// checkLabelsAreEqual()
// }
func (s *rebootPDSuite) TestReloadLabel() {
re := require.New(s.T())
ctx := context.Background()

pdHTTPCli := http.NewClient("pd-real-cluster-test", getPDEndpoints(s.T()))
resp, err := pdHTTPCli.GetStores(ctx)
re.NoError(err)
re.NotEmpty(resp.Stores)
firstStore := resp.Stores[0]
// TiFlash labels will be ["engine": "tiflash"]
// So we need to merge the labels
storeLabels := map[string]string{
"zone": "zone1",
}
for _, label := range firstStore.Store.Labels {
storeLabels[label.Key] = label.Value
}
re.NoError(pdHTTPCli.SetStoreLabels(ctx, firstStore.Store.ID, storeLabels))
defer func() {
re.NoError(pdHTTPCli.DeleteStoreLabel(ctx, firstStore.Store.ID, "zone"))
}()

checkLabelsAreEqual := func() {
resp, err := pdHTTPCli.GetStore(ctx, uint64(firstStore.Store.ID))
re.NoError(err)

labelsMap := make(map[string]string)
for _, label := range resp.Store.Labels {
re.NotNil(label)
labelsMap[label.Key] = label.Value
}

for key, value := range storeLabels {
re.Equal(value, labelsMap[key])
}
}
// Check the label is set
checkLabelsAreEqual()
// Restart to reload the label
s.restart()
pdHTTPCli = http.NewClient("pd-real-cluster-test", getPDEndpoints(s.T()))
checkLabelsAreEqual()
}
Loading

0 comments on commit b155a7b

Please sign in to comment.