Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

realclustertest: apply new style for some test cases #8732

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading