Skip to content

Commit

Permalink
reduce code
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Oct 26, 2023
1 parent 084c739 commit 1af43f9
Showing 1 changed file with 22 additions and 41 deletions.
63 changes: 22 additions & 41 deletions tests/server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,7 @@ func (suite *operatorTestSuite) TestOperator() {

func (suite *operatorTestSuite) checkAddRemovePeer(cluster *tests.TestCluster) {
re := suite.Require()

// pause rule checker to avoid unexpected operator
checkerName := "rule"
addr := cluster.GetLeaderServer().GetAddr()
resp := make(map[string]interface{})
url := fmt.Sprintf("%s/pd/api/v1/checker/%s", addr, checkerName)
err := tu.CheckPostJSON(testDialClient, url, []byte(`{"delay":1000}`), tu.StatusOK(re))
re.NoError(err)
err = tu.ReadGetJSON(re, testDialClient, url, &resp)
re.NoError(err)
re.True(resp["paused"].(bool))

suite.pauseRuleChecker(cluster)
stores := []*metapb.Store{
{
Id: 1,
Expand Down Expand Up @@ -127,7 +116,7 @@ func (suite *operatorTestSuite) checkAddRemovePeer(cluster *tests.TestCluster) {

urlPrefix := fmt.Sprintf("%s/pd/api/v1", cluster.GetLeaderServer().GetAddr())
regionURL := fmt.Sprintf("%s/operators/%d", urlPrefix, region.GetId())
err = tu.CheckGetJSON(testDialClient, regionURL, nil,
err := tu.CheckGetJSON(testDialClient, regionURL, nil,
tu.StatusNotOK(re), tu.StringContain(re, "operator not found"))
suite.NoError(err)
recordURL := fmt.Sprintf("%s/operators/records?from=%s", urlPrefix, strconv.FormatInt(time.Now().Unix(), 10))
Expand Down Expand Up @@ -183,26 +172,15 @@ func (suite *operatorTestSuite) checkAddRemovePeer(cluster *tests.TestCluster) {

// Fail to get operator if from is latest.
time.Sleep(time.Second)
url = fmt.Sprintf("%s/operators/records?from=%s", urlPrefix, strconv.FormatInt(time.Now().Unix(), 10))
url := fmt.Sprintf("%s/operators/records?from=%s", urlPrefix, strconv.FormatInt(time.Now().Unix(), 10))
err = tu.CheckGetJSON(testDialClient, url, nil,
tu.StatusNotOK(re), tu.StringContain(re, "operator not found"))
suite.NoError(err)
}

func (suite *operatorTestSuite) checkMergeRegionOperator(cluster *tests.TestCluster) {
re := suite.Require()

// pause rule checker to avoid unexpected operator
checkerName := "rule"
addr := cluster.GetLeaderServer().GetAddr()
resp := make(map[string]interface{})
url := fmt.Sprintf("%s/pd/api/v1/checker/%s", addr, checkerName)
err := tu.CheckPostJSON(testDialClient, url, []byte(`{"delay":1000}`), tu.StatusOK(re))
re.NoError(err)
err = tu.ReadGetJSON(re, testDialClient, url, &resp)
re.NoError(err)
re.True(resp["paused"].(bool))

suite.pauseRuleChecker(cluster)
r1 := core.NewTestRegionInfo(10, 1, []byte(""), []byte("b"), core.SetWrittenBytes(1000), core.SetReadBytes(1000), core.SetRegionConfVer(1), core.SetRegionVersion(1))
tests.MustPutRegionInfo(re, cluster, r1)
r2 := core.NewTestRegionInfo(20, 1, []byte("b"), []byte("c"), core.SetWrittenBytes(2000), core.SetReadBytes(0), core.SetRegionConfVer(2), core.SetRegionVersion(3))
Expand All @@ -211,7 +189,7 @@ func (suite *operatorTestSuite) checkMergeRegionOperator(cluster *tests.TestClus
tests.MustPutRegionInfo(re, cluster, r3)

urlPrefix := fmt.Sprintf("%s/pd/api/v1", cluster.GetLeaderServer().GetAddr())
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", urlPrefix), []byte(`{"name":"merge-region", "source_region_id": 10, "target_region_id": 20}`), tu.StatusOK(re))
err := tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", urlPrefix), []byte(`{"name":"merge-region", "source_region_id": 10, "target_region_id": 20}`), tu.StatusOK(re))
suite.NoError(err)

tu.CheckDelete(testDialClient, fmt.Sprintf("%s/operators/%d", urlPrefix, 10), tu.StatusOK(re))
Expand All @@ -228,18 +206,7 @@ func (suite *operatorTestSuite) checkMergeRegionOperator(cluster *tests.TestClus

func (suite *operatorTestSuite) checkTransferRegionWithPlacementRule(cluster *tests.TestCluster) {
re := suite.Require()

// pause rule checker to avoid unexpected operator
checkerName := "rule"
addr := cluster.GetLeaderServer().GetAddr()
resp := make(map[string]interface{})
url := fmt.Sprintf("%s/pd/api/v1/checker/%s", addr, checkerName)
err := tu.CheckPostJSON(testDialClient, url, []byte(`{"delay":1000}`), tu.StatusOK(re))
re.NoError(err)
err = tu.ReadGetJSON(re, testDialClient, url, &resp)
re.NoError(err)
re.True(resp["paused"].(bool))

suite.pauseRuleChecker(cluster)
stores := []*metapb.Store{
{
Id: 1,
Expand Down Expand Up @@ -285,7 +252,7 @@ func (suite *operatorTestSuite) checkTransferRegionWithPlacementRule(cluster *te

urlPrefix := fmt.Sprintf("%s/pd/api/v1", cluster.GetLeaderServer().GetAddr())
regionURL := fmt.Sprintf("%s/operators/%d", urlPrefix, region.GetId())
err = tu.CheckGetJSON(testDialClient, regionURL, nil,
err := tu.CheckGetJSON(testDialClient, regionURL, nil,
tu.StatusNotOK(re), tu.StringContain(re, "operator not found"))
re.NoError(err)
convertStepsToStr := func(steps []string) string {
Expand Down Expand Up @@ -449,7 +416,7 @@ func (suite *operatorTestSuite) checkTransferRegionWithPlacementRule(cluster *te
},
}
svr := cluster.GetLeaderServer()
url = fmt.Sprintf("%s/pd/api/v1/config", svr.GetAddr())
url := fmt.Sprintf("%s/pd/api/v1/config", svr.GetAddr())
for _, testCase := range testCases {
suite.T().Log(testCase.name)
data := make(map[string]interface{})
Expand Down Expand Up @@ -508,3 +475,17 @@ func (suite *operatorTestSuite) checkTransferRegionWithPlacementRule(cluster *te
suite.NoError(err)
}
}

// pauseRuleChecker will pause rule checker to avoid unexpected operator.
func (suite *operatorTestSuite) pauseRuleChecker(cluster *tests.TestCluster) {
re := suite.Require()
checkerName := "rule"
addr := cluster.GetLeaderServer().GetAddr()
resp := make(map[string]interface{})
url := fmt.Sprintf("%s/pd/api/v1/checker/%s", addr, checkerName)
err := tu.CheckPostJSON(testDialClient, url, []byte(`{"delay":1000}`), tu.StatusOK(re))
re.NoError(err)
err = tu.ReadGetJSON(re, testDialClient, url, &resp)
re.NoError(err)
re.True(resp["paused"].(bool))
}

0 comments on commit 1af43f9

Please sign in to comment.