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

operator: migrate test framework to testify #5229

Merged
merged 6 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions server/api/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (suite *labelsStoreTestSuite) TestLabelsGet() {
}

func (suite *labelsStoreTestSuite) TestStoresLabelFilter() {
var table = []struct {
var testCases = []struct {
name, value string
want []*metapb.Store
}{
Expand Down Expand Up @@ -175,12 +175,12 @@ func (suite *labelsStoreTestSuite) TestStoresLabelFilter() {
},
}
re := suite.Require()
for _, t := range table {
url := fmt.Sprintf("%s/labels/stores?name=%s&value=%s", suite.urlPrefix, t.name, t.value)
for _, testCase := range testCases {
url := fmt.Sprintf("%s/labels/stores?name=%s&value=%s", suite.urlPrefix, testCase.name, testCase.value)
info := new(StoresInfo)
err := tu.ReadGetJSON(re, testDialClient, url, info)
suite.NoError(err)
checkStoresInfo(re, info.Stores, t.want)
checkStoresInfo(re, info.Stores, testCase.want)
}
_, err := newStoresLabelFilter("test", ".[test")
suite.Error(err)
Expand Down
26 changes: 13 additions & 13 deletions server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (suite *transferRegionOperatorTestSuite) TestTransferRegionWithPlacementRul
operator := mustReadURL(re, regionURL)
suite.Contains(operator, "operator not found")

tt := []struct {
testCases := []struct {
name string
placementRuleEnable bool
rules []*placement.Rule
Expand Down Expand Up @@ -371,33 +371,33 @@ func (suite *transferRegionOperatorTestSuite) TestTransferRegionWithPlacementRul
}, ", "),
},
}
for _, tc := range tt {
suite.T().Log(tc.name)
suite.svr.GetRaftCluster().GetOpts().SetPlacementRuleEnabled(tc.placementRuleEnable)
if tc.placementRuleEnable {
for _, testCase := range testCases {
suite.T().Log(testCase.name)
suite.svr.GetRaftCluster().GetOpts().SetPlacementRuleEnabled(testCase.placementRuleEnable)
if testCase.placementRuleEnable {
err := suite.svr.GetRaftCluster().GetRuleManager().Initialize(
suite.svr.GetRaftCluster().GetOpts().GetMaxReplicas(),
suite.svr.GetRaftCluster().GetOpts().GetLocationLabels())
suite.NoError(err)
}
if len(tc.rules) > 0 {
if len(testCase.rules) > 0 {
// add customized rule first and then remove default rule
err := suite.svr.GetRaftCluster().GetRuleManager().SetRules(tc.rules)
err := suite.svr.GetRaftCluster().GetRuleManager().SetRules(testCase.rules)
suite.NoError(err)
err = suite.svr.GetRaftCluster().GetRuleManager().DeleteRule("pd", "default")
suite.NoError(err)
}
var err error
if tc.expectedError == nil {
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", suite.urlPrefix), tc.input, tu.StatusOK(re))
if testCase.expectedError == nil {
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", suite.urlPrefix), testCase.input, tu.StatusOK(re))
} else {
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", suite.urlPrefix), tc.input,
tu.StatusNotOK(re), tu.StringContain(re, tc.expectedError.Error()))
err = tu.CheckPostJSON(testDialClient, fmt.Sprintf("%s/operators", suite.urlPrefix), testCase.input,
tu.StatusNotOK(re), tu.StringContain(re, testCase.expectedError.Error()))
}
suite.NoError(err)
if len(tc.expectSteps) > 0 {
if len(testCase.expectSteps) > 0 {
operator = mustReadURL(re, regionURL)
suite.Contains(operator, tc.expectSteps)
suite.Contains(operator, testCase.expectSteps)
}
_, err = apiutil.DoDelete(testDialClient, regionURL)
suite.NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (suite *getRegionTestSuite) TestScanRegionByKeys() {
regions = &RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
suite.NoError(err)
suite.Equal(regions.Count, len(regionIDs))
suite.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
suite.Equal(regions.Regions[i].ID, v)
}
Expand All @@ -514,7 +514,7 @@ func (suite *getRegionTestSuite) TestScanRegionByKeys() {
regions = &RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
suite.NoError(err)
suite.Equal(regions.Count, len(regionIDs))
suite.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
suite.Equal(regions.Regions[i].ID, v)
}
Expand All @@ -523,7 +523,7 @@ func (suite *getRegionTestSuite) TestScanRegionByKeys() {
regions = &RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
suite.NoError(err)
suite.Equal(regions.Count, len(regionIDs))
suite.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
suite.Equal(regions.Regions[i].ID, v)
}
Expand Down
6 changes: 3 additions & 3 deletions server/api/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,10 @@ func (suite *ruleTestSuite) TestBundleBadRequest() {
{"/placement-rule", `[{"group_id":"foo", "rules": [{"group_id":"foo", "id":"baz", "role":"voter", "count":1}]}]`, true},
{"/placement-rule", `[{"group_id":"foo", "rules": [{"group_id":"bar", "id":"baz", "role":"voter", "count":1}]}]`, false},
}
for _, tc := range testCases {
err := tu.CheckPostJSON(testDialClient, suite.urlPrefix+tc.uri, []byte(tc.data),
for _, testCase := range testCases {
err := tu.CheckPostJSON(testDialClient, suite.urlPrefix+testCase.uri, []byte(testCase.data),
func(_ []byte, code int) {
suite.Equal(tc.ok, code == http.StatusOK)
suite.Equal(testCase.ok, code == http.StatusOK)
})
suite.NoError(err)
}
Expand Down
16 changes: 8 additions & 8 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (suite *storeTestSuite) TestStoreDelete() {
for id := 1111; id <= 1115; id++ {
mustPutStore(re, suite.svr, uint64(id), metapb.StoreState_Up, metapb.NodeState_Serving, nil)
}
table := []struct {
testCases := []struct {
id int
status int
}{
Expand All @@ -269,10 +269,10 @@ func (suite *storeTestSuite) TestStoreDelete() {
status: http.StatusGone,
},
}
for _, t := range table {
url := fmt.Sprintf("%s/store/%d", suite.urlPrefix, t.id)
for _, testCase := range testCases {
url := fmt.Sprintf("%s/store/%d", suite.urlPrefix, testCase.id)
status := suite.requestStatusBody(testDialClient, http.MethodDelete, url)
suite.Equal(t.status, status)
suite.Equal(testCase.status, status)
}
// store 6 origin status:offline
url := fmt.Sprintf("%s/store/6", suite.urlPrefix)
Expand Down Expand Up @@ -358,7 +358,7 @@ func (suite *storeTestSuite) TestStoreSetState() {
}

func (suite *storeTestSuite) TestUrlStoreFilter() {
table := []struct {
testCases := []struct {
u string
want []*metapb.Store
}{
Expand All @@ -380,12 +380,12 @@ func (suite *storeTestSuite) TestUrlStoreFilter() {
},
}

for _, t := range table {
uu, err := url.Parse(t.u)
for _, testCase := range testCases {
uu, err := url.Parse(testCase.u)
suite.NoError(err)
f, err := newStoreStateFilter(uu)
suite.NoError(err)
suite.Equal(t.want, f.filter(suite.stores))
suite.Equal(testCase.want, f.filter(suite.stores))
}

u, err := url.Parse("http://localhost:2379/pd/api/v1/stores?state=foo")
Expand Down
16 changes: 8 additions & 8 deletions server/cluster/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func TestShouldRun(t *testing.T) {
re.False(co.shouldRun())
re.Equal(2, tc.core.Regions.GetStoreRegionCount(4))

tbl := []struct {
testCases := []struct {
regionID uint64
shouldRun bool
}{
Expand All @@ -602,11 +602,11 @@ func TestShouldRun(t *testing.T) {
{7, true},
}

for _, t := range tbl {
r := tc.GetRegion(t.regionID)
for _, testCase := range testCases {
r := tc.GetRegion(testCase.regionID)
nr := r.Clone(core.WithLeader(r.GetPeers()[0]))
re.NoError(tc.processRegionHeartbeat(nr))
re.Equal(t.shouldRun, co.shouldRun())
re.Equal(testCase.shouldRun, co.shouldRun())
}
nr := &metapb.Region{Id: 6, Peers: []*metapb.Peer{}}
newRegion := core.NewRegionInfo(nr, nil)
Expand All @@ -630,7 +630,7 @@ func TestShouldRunWithNonLeaderRegions(t *testing.T) {
re.False(co.shouldRun())
re.Equal(10, tc.core.Regions.GetStoreRegionCount(1))

tbl := []struct {
testCases := []struct {
regionID uint64
shouldRun bool
}{
Expand All @@ -645,11 +645,11 @@ func TestShouldRunWithNonLeaderRegions(t *testing.T) {
{9, true},
}

for _, t := range tbl {
r := tc.GetRegion(t.regionID)
for _, testCase := range testCases {
r := tc.GetRegion(testCase.regionID)
nr := r.Clone(core.WithLeader(r.GetPeers()[0]))
re.NoError(tc.processRegionHeartbeat(nr))
re.Equal(t.shouldRun, co.shouldRun())
re.Equal(testCase.shouldRun, co.shouldRun())
}
nr := &metapb.Region{Id: 9, Peers: []*metapb.Peer{}}
newRegion := core.NewRegionInfo(nr, nil)
Expand Down
8 changes: 4 additions & 4 deletions server/schedule/checker/joint_state_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLeaveJointState(t *testing.T) {
Peers []*metapb.Peer // first is leader
OpSteps []operator.OpStep
}
cases := []testCase{
testCases := []testCase{
{
[]*metapb.Peer{
{Id: 101, StoreId: 1, Role: metapb.PeerRole_Voter},
Expand Down Expand Up @@ -111,10 +111,10 @@ func TestLeaveJointState(t *testing.T) {
},
}

for _, tc := range cases {
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: tc.Peers}, tc.Peers[0])
for _, testCase := range testCases {
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: testCase.Peers}, testCase.Peers[0])
op := jsc.Check(region)
checkSteps(re, op, tc.OpSteps)
checkSteps(re, op, testCase.OpSteps)
}
}

Expand Down
8 changes: 4 additions & 4 deletions server/schedule/checker/rule_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,16 +497,16 @@ func (suite *ruleCheckerTestSuite) TestIssue3299() {
},
}

for _, t := range testCases {
for _, testCase := range testCases {
err := suite.ruleManager.SetRule(&placement.Rule{
GroupID: "p",
ID: "0",
Role: placement.Follower,
Count: 1,
LabelConstraints: t.constraints,
LabelConstraints: testCase.constraints,
})
if t.err != "" {
suite.Regexp(t.err, err.Error())
if testCase.err != "" {
suite.Regexp(testCase.err, err.Error())
} else {
suite.NoError(err)
}
Expand Down
42 changes: 21 additions & 21 deletions server/schedule/filter/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func TestDistinctScoreFilter(t *testing.T) {
{[]uint64{1, 3, 4}, 1, 2, true, false},
{[]uint64{1, 4, 6}, 4, 2, false, false},
}
for _, tc := range testCases {
for _, testCase := range testCases {
var stores []*core.StoreInfo
for _, id := range tc.stores {
for _, id := range testCase.stores {
stores = append(stores, allStores[id-1])
}
ls := NewLocationSafeguard("", labels, stores, allStores[tc.source-1])
li := NewLocationImprover("", labels, stores, allStores[tc.source-1])
re.Equal(tc.safeGuardRes, ls.Target(config.NewTestOptions(), allStores[tc.target-1]))
re.Equal(tc.improverRes, li.Target(config.NewTestOptions(), allStores[tc.target-1]))
ls := NewLocationSafeguard("", labels, stores, allStores[testCase.source-1])
li := NewLocationImprover("", labels, stores, allStores[testCase.source-1])
re.Equal(testCase.safeGuardRes, ls.Target(config.NewTestOptions(), allStores[testCase.target-1]))
re.Equal(testCase.improverRes, li.Target(config.NewTestOptions(), allStores[testCase.target-1]))
}
}

Expand Down Expand Up @@ -87,9 +87,9 @@ func TestLabelConstraintsFilter(t *testing.T) {
{"id", "notExists", []string{}, false},
{"_id", "notExists", []string{}, true},
}
for _, tc := range testCases {
filter := NewLabelConstaintFilter("", []placement.LabelConstraint{{Key: tc.key, Op: placement.LabelConstraintOp(tc.op), Values: tc.values}})
re.Equal(tc.res, filter.Source(testCluster.GetOpts(), store))
for _, testCase := range testCases {
filter := NewLabelConstaintFilter("", []placement.LabelConstraint{{Key: testCase.key, Op: placement.LabelConstraintOp(testCase.op), Values: testCase.values}})
re.Equal(testCase.res, filter.Source(testCluster.GetOpts(), store))
}
}

Expand Down Expand Up @@ -124,13 +124,13 @@ func TestRuleFitFilter(t *testing.T) {
{6, 1, map[string]string{"zone": "z4"}, true, true},
}
// Init cluster
for _, tc := range testCases {
testCluster.AddLabelsStore(tc.storeID, tc.regionCount, tc.labels)
for _, testCase := range testCases {
testCluster.AddLabelsStore(testCase.storeID, testCase.regionCount, testCase.labels)
}
for _, tc := range testCases {
for _, testCase := range testCases {
filter := newRuleFitFilter("", testCluster.GetBasicCluster(), testCluster.GetRuleManager(), region, 1)
re.Equal(tc.sourceRes, filter.Source(testCluster.GetOpts(), testCluster.GetStore(tc.storeID)))
re.Equal(tc.targetRes, filter.Target(testCluster.GetOpts(), testCluster.GetStore(tc.storeID)))
re.Equal(testCase.sourceRes, filter.Source(testCluster.GetOpts(), testCluster.GetStore(testCase.storeID)))
re.Equal(testCase.targetRes, filter.Target(testCluster.GetOpts(), testCluster.GetStore(testCase.storeID)))
}
}

Expand All @@ -152,9 +152,9 @@ func TestStoreStateFilter(t *testing.T) {
}

check := func(store *core.StoreInfo, testCases []testCase) {
for _, tc := range testCases {
re.Equal(tc.sourceRes, filters[tc.filterIdx].Source(opt, store))
re.Equal(tc.targetRes, filters[tc.filterIdx].Target(opt, store))
for _, testCase := range testCases {
re.Equal(testCase.sourceRes, filters[testCase.filterIdx].Source(opt, store))
re.Equal(testCase.targetRes, filters[testCase.filterIdx].Target(opt, store))
}
}

Expand Down Expand Up @@ -248,11 +248,11 @@ func TestIsolationFilter(t *testing.T) {
},
}

for _, tc := range testCases {
filter := NewIsolationFilter("", tc.isolationLevel, testCluster.GetLocationLabels(), testCluster.GetRegionStores(tc.region))
for _, testCase := range testCases {
filter := NewIsolationFilter("", testCase.isolationLevel, testCluster.GetLocationLabels(), testCluster.GetRegionStores(testCase.region))
for idx, store := range allStores {
re.Equal(tc.sourceRes[idx], filter.Source(testCluster.GetOpts(), testCluster.GetStore(store.storeID)))
re.Equal(tc.targetRes[idx], filter.Target(testCluster.GetOpts(), testCluster.GetStore(store.storeID)))
re.Equal(testCase.sourceRes[idx], filter.Source(testCluster.GetOpts(), testCluster.GetStore(store.storeID)))
re.Equal(testCase.targetRes[idx], filter.Target(testCluster.GetOpts(), testCluster.GetStore(store.storeID)))
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions server/schedule/labeler/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ func TestIndex(t *testing.T) {
{"abcd", "cd12", map[string]string{"k1": "v0", "k2": "v2"}},
{"cdef", "efef", map[string]string{"k1": "v0", "k2": "v3"}},
}
for _, tc := range testCases {
start, _ := hex.DecodeString(tc.start)
end, _ := hex.DecodeString(tc.end)
for _, testCase := range testCases {
start, _ := hex.DecodeString(testCase.start)
end, _ := hex.DecodeString(testCase.end)
region := core.NewTestRegionInfo(start, end)
labels := labeler.GetRegionLabels(region)
re.Len(labels, len(tc.labels))
re.Len(labels, len(testCase.labels))
for _, l := range labels {
re.Equal(tc.labels[l.Key], l.Value)
re.Equal(testCase.labels[l.Key], l.Value)
}
for _, k := range []string{"k1", "k2"} {
re.Equal(tc.labels[k], labeler.GetRegionLabel(region, k))
re.Equal(testCase.labels[k], labeler.GetRegionLabel(region, k))
}
}
}
Expand Down Expand Up @@ -246,17 +246,17 @@ func TestKeyRange(t *testing.T) {
{"cd12", "dddd", map[string]string{"k3": "v3"}},
{"ffee", "ffff", map[string]string{}},
}
for _, tc := range testCases {
start, _ := hex.DecodeString(tc.start)
end, _ := hex.DecodeString(tc.end)
for _, testCase := range testCases {
start, _ := hex.DecodeString(testCase.start)
end, _ := hex.DecodeString(testCase.end)
region := core.NewTestRegionInfo(start, end)
labels := labeler.GetRegionLabels(region)
re.Len(labels, len(tc.labels))
re.Len(labels, len(testCase.labels))
for _, l := range labels {
re.Equal(l.Value, tc.labels[l.Key])
re.Equal(l.Value, testCase.labels[l.Key])
}
for _, k := range []string{"k1", "k2", "k3"} {
re.Equal(tc.labels[k], labeler.GetRegionLabel(region, k))
re.Equal(testCase.labels[k], labeler.GetRegionLabel(region, k))
}
}
}
Expand Down
Loading