Skip to content

Commit

Permalink
placement: migrate test framework to testify
Browse files Browse the repository at this point in the history
close tikv#5097

Signed-off-by: disksing <i@disksing.com>
  • Loading branch information
disksing committed Jun 2, 2022
1 parent d114cd6 commit 4d413aa
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 238 deletions.
16 changes: 7 additions & 9 deletions server/schedule/placement/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package placement

import (
. "github.com/pingcap/check"
)

var _ = Suite(&testConfigSuite{})
"testing"

type testConfigSuite struct {
}
"github.com/stretchr/testify/require"
)

func (s *testConfigSuite) TestTrim(c *C) {
func TestTrim(t *testing.T) {
re := require.New(t)
rc := newRuleConfig()
rc.setRule(&Rule{GroupID: "g1", ID: "id1"})
rc.setRule(&Rule{GroupID: "g1", ID: "id2"})
Expand Down Expand Up @@ -76,7 +74,7 @@ func (s *testConfigSuite) TestTrim(c *C) {
p := rc.beginPatch()
tc.ops(p)
p.trim()
c.Assert(p.mut.rules, DeepEquals, tc.mutRules)
c.Assert(p.mut.groups, DeepEquals, tc.mutGroups)
re.Equal(tc.mutRules, p.mut.rules)
re.Equal(tc.mutGroups, p.mut.groups)
}
}
53 changes: 26 additions & 27 deletions server/schedule/placement/fit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ import (
"fmt"
"strconv"
"strings"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/server/core"
)

var _ = Suite(&testFitSuite{})

type testFitSuite struct{}

func (s *testFitSuite) makeStores() StoreSet {
func makeStores() StoreSet {
stores := core.NewStoresInfo()
for zone := 1; zone <= 5; zone++ {
for rack := 1; rack <= 5; rack++ {
Expand All @@ -50,7 +48,7 @@ func (s *testFitSuite) makeStores() StoreSet {
}

// example: "1111_leader,1234,2111_learner"
func (s *testFitSuite) makeRegion(def string) *core.RegionInfo {
func makeRegion(def string) *core.RegionInfo {
var regionMeta metapb.Region
var leader *metapb.Peer
for _, peerDef := range strings.Split(def, ",") {
Expand All @@ -71,7 +69,7 @@ func (s *testFitSuite) makeRegion(def string) *core.RegionInfo {

// example: "3/voter/zone=zone1+zone2,rack=rack2/zone,rack,host"
// count role constraints location_labels
func (s *testFitSuite) makeRule(def string) *Rule {
func makeRule(def string) *Rule {
var rule Rule
splits := strings.Split(def, "/")
rule.Count, _ = strconv.Atoi(splits[0])
Expand All @@ -92,7 +90,7 @@ func (s *testFitSuite) makeRule(def string) *Rule {
return &rule
}

func (s *testFitSuite) checkPeerMatch(peers []*metapb.Peer, expect string) bool {
func checkPeerMatch(peers []*metapb.Peer, expect string) bool {
if len(peers) == 0 && expect == "" {
return true
}
Expand All @@ -111,8 +109,9 @@ func (s *testFitSuite) checkPeerMatch(peers []*metapb.Peer, expect string) bool
return len(m) == 0
}

func (s *testFitSuite) TestFitRegion(c *C) {
stores := s.makeStores()
func TestFitRegion(t *testing.T) {
re := require.New(t)
stores := makeStores()

cases := []struct {
region string
Expand Down Expand Up @@ -140,34 +139,34 @@ func (s *testFitSuite) TestFitRegion(c *C) {
}

for _, cc := range cases {
region := s.makeRegion(cc.region)
region := makeRegion(cc.region)
var rules []*Rule
for _, r := range cc.rules {
rules = append(rules, s.makeRule(r))
rules = append(rules, makeRule(r))
}
rf := fitRegion(stores.GetStores(), region, rules)
expects := strings.Split(cc.fitPeers, "/")
for i, f := range rf.RuleFits {
c.Assert(s.checkPeerMatch(f.Peers, expects[i]), IsTrue)
re.True(checkPeerMatch(f.Peers, expects[i]))
}
if len(rf.RuleFits) < len(expects) {
c.Assert(s.checkPeerMatch(rf.OrphanPeers, expects[len(rf.RuleFits)]), IsTrue)
re.True(checkPeerMatch(rf.OrphanPeers, expects[len(rf.RuleFits)]))
}
}
}

func (s *testFitSuite) TestIsolationScore(c *C) {
stores := s.makeStores()
func TestIsolationScore(t *testing.T) {
as := assert.New(t)
stores := makeStores()
testCases := []struct {
peers1 []uint64
Checker
peers2 []uint64
checker func(interface{}, interface{}, ...interface{}) bool
peers1 []uint64
peers2 []uint64
}{
{[]uint64{1111, 1112}, Less, []uint64{1111, 1121}},
{[]uint64{1111, 1211}, Less, []uint64{1111, 2111}},
{[]uint64{1111, 1211, 1311, 2111, 3111}, Less, []uint64{1111, 1211, 2111, 2211, 3111}},
{[]uint64{1111, 1211, 2111, 2211, 3111}, Equals, []uint64{1111, 2111, 2211, 3111, 3211}},
{[]uint64{1111, 1211, 2111, 2211, 3111}, Greater, []uint64{1111, 1121, 2111, 2211, 3111}},
{as.Less, []uint64{1111, 1112}, []uint64{1111, 1121}},
{as.Less, []uint64{1111, 1211}, []uint64{1111, 2111}},
{as.Less, []uint64{1111, 1211, 1311, 2111, 3111}, []uint64{1111, 1211, 2111, 2211, 3111}},
{as.Equal, []uint64{1111, 1211, 2111, 2211, 3111}, []uint64{1111, 2111, 2211, 3111, 3211}},
{as.Greater, []uint64{1111, 1211, 2111, 2211, 3111}, []uint64{1111, 1121, 2111, 2211, 3111}},
}

makePeers := func(ids []uint64) []*fitPeer {
Expand All @@ -185,6 +184,6 @@ func (s *testFitSuite) TestIsolationScore(c *C) {
peers1, peers2 := makePeers(tc.peers1), makePeers(tc.peers2)
score1 := isolationScore(peers1, []string{"zone", "rack", "host"})
score2 := isolationScore(peers2, []string{"zone", "rack", "host"})
c.Assert(score1, tc.Checker, score2)
tc.checker(score1, score2)
}
}
21 changes: 7 additions & 14 deletions server/schedule/placement/label_constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@ package placement
import (
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/server/core"
)

func TestPlacement(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testLabelConstraintsSuite{})

type testLabelConstraintsSuite struct{}

func (s *testLabelConstraintsSuite) TestLabelConstraint(c *C) {
func TestLabelConstraint(t *testing.T) {
re := require.New(t)
stores := []map[string]string{
{"zone": "zone1", "rack": "rack1"}, // 1
{"zone": "zone1", "rack": "rack2"}, // 2
Expand Down Expand Up @@ -61,11 +54,11 @@ func (s *testLabelConstraintsSuite) TestLabelConstraint(c *C) {
matched = append(matched, j+1)
}
}
c.Assert(matched, DeepEquals, expect[i])
re.Equal(expect[i], matched)
}
}

func (s *testLabelConstraintsSuite) TestLabelConstraints(c *C) {
func TestLabelConstraints(t *testing.T) {
re := require.New(t)
stores := []map[string]string{
{}, // 1
{"k1": "v1"}, // 2
Expand Down Expand Up @@ -100,6 +93,6 @@ func (s *testLabelConstraintsSuite) TestLabelConstraints(c *C) {
matched = append(matched, j+1)
}
}
c.Assert(matched, DeepEquals, expect[i])
re.Equal(expect[i], matched)
}
}
20 changes: 11 additions & 9 deletions server/schedule/placement/region_rule_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
package placement

import (
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/server/core"
)

func (s *testRuleSuite) TestRegionRuleFitCache(c *C) {
func TestRegionRuleFitCache(t *testing.T) {
re := require.New(t)
originRegion := mockRegion(3, 0)
originRules := addExtraRules(0)
originStores := mockStores(3)
Expand Down Expand Up @@ -174,20 +176,20 @@ func (s *testRuleSuite) TestRegionRuleFitCache(c *C) {
},
}
for _, testcase := range testcases {
c.Log(testcase.name)
c.Assert(cache.IsUnchanged(testcase.region, testcase.rules, mockStores(3)), Equals, testcase.unchanged)
t.Log(testcase.name)
re.Equal(testcase.unchanged, cache.IsUnchanged(testcase.region, testcase.rules, mockStores(3)))
}
for _, testcase := range testcases {
c.Log(testcase.name)
c.Assert(cache.IsUnchanged(testcase.region, testcase.rules, mockStoresNoHeartbeat(3)), Equals, false)
t.Log(testcase.name)
re.Equal(false, cache.IsUnchanged(testcase.region, testcase.rules, mockStoresNoHeartbeat(3)))
}
// Invalid Input4
c.Assert(cache.IsUnchanged(mockRegion(3, 0), addExtraRules(0), nil), IsFalse)
re.False(cache.IsUnchanged(mockRegion(3, 0), addExtraRules(0), nil))
// Invalid Input5
c.Assert(cache.IsUnchanged(mockRegion(3, 0), addExtraRules(0), []*core.StoreInfo{}), IsFalse)
re.False(cache.IsUnchanged(mockRegion(3, 0), addExtraRules(0), []*core.StoreInfo{}))
// origin rules changed, assert whether cache is changed
originRules[0].Version++
c.Assert(cache.IsUnchanged(originRegion, originRules, originStores), IsFalse)
re.False(cache.IsUnchanged(originRegion, originRules, originStores))
}

func mockRegionRuleFitCache(region *core.RegionInfo, rules []*Rule, regionStores []*core.StoreInfo) *RegionRuleFitCache {
Expand Down
Loading

0 comments on commit 4d413aa

Please sign in to comment.