Skip to content

Commit

Permalink
operator: migrate test framework to testify (tikv#5191)
Browse files Browse the repository at this point in the history
ref tikv#4813

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

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
2 people authored and CabinfeverB committed Jul 14, 2022
1 parent 134baae commit 448d689
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 248 deletions.
237 changes: 120 additions & 117 deletions server/schedule/operator/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,132 +16,137 @@ package operator

import (
"context"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
)

var _ = Suite(&testBuilderSuite{})
type operatorBuilderTestSuite struct {
suite.Suite

type testBuilderSuite struct {
cluster *mockcluster.Cluster
ctx context.Context
cancel context.CancelFunc
}

func (s *testBuilderSuite) SetUpTest(c *C) {
func TestOperatorBuilderTestSuite(t *testing.T) {
suite.Run(t, new(operatorBuilderTestSuite))
}

func (suite *operatorBuilderTestSuite) SetupTest() {
opts := config.NewTestOptions()
s.ctx, s.cancel = context.WithCancel(context.Background())
s.cluster = mockcluster.NewCluster(s.ctx, opts)
s.cluster.SetLabelPropertyConfig(config.LabelPropertyConfig{
suite.ctx, suite.cancel = context.WithCancel(context.Background())
suite.cluster = mockcluster.NewCluster(suite.ctx, opts)
suite.cluster.SetLabelPropertyConfig(config.LabelPropertyConfig{
config.RejectLeader: {{Key: "noleader", Value: "true"}},
})
s.cluster.SetLocationLabels([]string{"zone", "host"})
s.cluster.AddLabelsStore(1, 0, map[string]string{"zone": "z1", "host": "h1"})
s.cluster.AddLabelsStore(2, 0, map[string]string{"zone": "z1", "host": "h1"})
s.cluster.AddLabelsStore(3, 0, map[string]string{"zone": "z1", "host": "h1"})
s.cluster.AddLabelsStore(4, 0, map[string]string{"zone": "z1", "host": "h1"})
s.cluster.AddLabelsStore(5, 0, map[string]string{"zone": "z1", "host": "h1"})
s.cluster.AddLabelsStore(6, 0, map[string]string{"zone": "z1", "host": "h2"})
s.cluster.AddLabelsStore(7, 0, map[string]string{"zone": "z1", "host": "h2"})
s.cluster.AddLabelsStore(8, 0, map[string]string{"zone": "z2", "host": "h1"})
s.cluster.AddLabelsStore(9, 0, map[string]string{"zone": "z2", "host": "h2"})
s.cluster.AddLabelsStore(10, 0, map[string]string{"zone": "z3", "host": "h1", "noleader": "true"})
suite.cluster.SetLocationLabels([]string{"zone", "host"})
suite.cluster.AddLabelsStore(1, 0, map[string]string{"zone": "z1", "host": "h1"})
suite.cluster.AddLabelsStore(2, 0, map[string]string{"zone": "z1", "host": "h1"})
suite.cluster.AddLabelsStore(3, 0, map[string]string{"zone": "z1", "host": "h1"})
suite.cluster.AddLabelsStore(4, 0, map[string]string{"zone": "z1", "host": "h1"})
suite.cluster.AddLabelsStore(5, 0, map[string]string{"zone": "z1", "host": "h1"})
suite.cluster.AddLabelsStore(6, 0, map[string]string{"zone": "z1", "host": "h2"})
suite.cluster.AddLabelsStore(7, 0, map[string]string{"zone": "z1", "host": "h2"})
suite.cluster.AddLabelsStore(8, 0, map[string]string{"zone": "z2", "host": "h1"})
suite.cluster.AddLabelsStore(9, 0, map[string]string{"zone": "z2", "host": "h2"})
suite.cluster.AddLabelsStore(10, 0, map[string]string{"zone": "z3", "host": "h1", "noleader": "true"})
}

func (s *testBuilderSuite) TearDownTest(c *C) {
s.cancel()
func (suite *operatorBuilderTestSuite) TearDownTest() {
suite.cancel()
}

func (s *testBuilderSuite) TestNewBuilder(c *C) {
func (suite *operatorBuilderTestSuite) TestNewBuilder() {
peers := []*metapb.Peer{{Id: 11, StoreId: 1}, {Id: 12, StoreId: 2, Role: metapb.PeerRole_Learner}}
region := core.NewRegionInfo(&metapb.Region{Id: 42, Peers: peers}, peers[0])
builder := NewBuilder("test", s.cluster, region)
c.Assert(builder.err, IsNil)
c.Assert(builder.originPeers, HasLen, 2)
c.Assert(builder.originPeers[1], DeepEquals, peers[0])
c.Assert(builder.originPeers[2], DeepEquals, peers[1])
c.Assert(builder.originLeaderStoreID, Equals, uint64(1))
c.Assert(builder.targetPeers, HasLen, 2)
c.Assert(builder.targetPeers[1], DeepEquals, peers[0])
c.Assert(builder.targetPeers[2], DeepEquals, peers[1])
builder := NewBuilder("test", suite.cluster, region)
suite.NoError(builder.err)
suite.Len(builder.originPeers, 2)
suite.Equal(peers[0], builder.originPeers[1])
suite.Equal(peers[1], builder.originPeers[2])
suite.Equal(uint64(1), builder.originLeaderStoreID)
suite.Len(builder.targetPeers, 2)
suite.Equal(peers[0], builder.targetPeers[1])
suite.Equal(peers[1], builder.targetPeers[2])

region = region.Clone(core.WithLeader(nil))
builder = NewBuilder("test", s.cluster, region)
c.Assert(builder.err, NotNil)
builder = NewBuilder("test", suite.cluster, region)
suite.Error(builder.err)
}

func (s *testBuilderSuite) newBuilder() *Builder {
func (suite *operatorBuilderTestSuite) newBuilder() *Builder {
peers := []*metapb.Peer{
{Id: 11, StoreId: 1},
{Id: 12, StoreId: 2},
{Id: 13, StoreId: 3, Role: metapb.PeerRole_Learner},
}
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: peers}, peers[0])
return NewBuilder("test", s.cluster, region)
return NewBuilder("test", suite.cluster, region)
}

func (s *testBuilderSuite) TestRecord(c *C) {
c.Assert(s.newBuilder().AddPeer(&metapb.Peer{StoreId: 1}).err, NotNil)
c.Assert(s.newBuilder().AddPeer(&metapb.Peer{StoreId: 4}).err, IsNil)
c.Assert(s.newBuilder().PromoteLearner(1).err, NotNil)
c.Assert(s.newBuilder().PromoteLearner(3).err, IsNil)
c.Assert(s.newBuilder().SetLeader(1).SetLeader(2).err, IsNil)
c.Assert(s.newBuilder().SetLeader(3).err, NotNil)
c.Assert(s.newBuilder().RemovePeer(4).err, NotNil)
c.Assert(s.newBuilder().AddPeer(&metapb.Peer{StoreId: 4, Role: metapb.PeerRole_Learner}).RemovePeer(4).err, IsNil)
c.Assert(s.newBuilder().SetLeader(2).RemovePeer(2).err, NotNil)
c.Assert(s.newBuilder().PromoteLearner(4).err, NotNil)
c.Assert(s.newBuilder().SetLeader(4).err, NotNil)
c.Assert(s.newBuilder().SetPeers(map[uint64]*metapb.Peer{2: {Id: 2}}).err, NotNil)
func (suite *operatorBuilderTestSuite) TestRecord() {
suite.Error(suite.newBuilder().AddPeer(&metapb.Peer{StoreId: 1}).err)
suite.NoError(suite.newBuilder().AddPeer(&metapb.Peer{StoreId: 4}).err)
suite.Error(suite.newBuilder().PromoteLearner(1).err)
suite.NoError(suite.newBuilder().PromoteLearner(3).err)
suite.NoError(suite.newBuilder().SetLeader(1).SetLeader(2).err)
suite.Error(suite.newBuilder().SetLeader(3).err)
suite.Error(suite.newBuilder().RemovePeer(4).err)
suite.NoError(suite.newBuilder().AddPeer(&metapb.Peer{StoreId: 4, Role: metapb.PeerRole_Learner}).RemovePeer(4).err)
suite.Error(suite.newBuilder().SetLeader(2).RemovePeer(2).err)
suite.Error(suite.newBuilder().PromoteLearner(4).err)
suite.Error(suite.newBuilder().SetLeader(4).err)
suite.Error(suite.newBuilder().SetPeers(map[uint64]*metapb.Peer{2: {Id: 2}}).err)

m := map[uint64]*metapb.Peer{
2: {StoreId: 2},
3: {StoreId: 3, Role: metapb.PeerRole_Learner},
4: {StoreId: 4},
}
builder := s.newBuilder().SetPeers(m).EnableLightWeight()
c.Assert(builder.targetPeers, HasLen, 3)
c.Assert(builder.targetPeers[2], DeepEquals, m[2])
c.Assert(builder.targetPeers[3], DeepEquals, m[3])
c.Assert(builder.targetPeers[4], DeepEquals, m[4])
c.Assert(builder.targetLeaderStoreID, Equals, uint64(0))
c.Assert(builder.lightWeight, IsTrue)
builder := suite.newBuilder().SetPeers(m).EnableLightWeight()
suite.Len(builder.targetPeers, 3)
suite.Equal(m[2], builder.targetPeers[2])
suite.Equal(m[3], builder.targetPeers[3])
suite.Equal(m[4], builder.targetPeers[4])
suite.Equal(uint64(0), builder.targetLeaderStoreID)
suite.True(builder.lightWeight)
}

func (s *testBuilderSuite) TestPrepareBuild(c *C) {
func (suite *operatorBuilderTestSuite) TestPrepareBuild() {
// no voter.
_, err := s.newBuilder().SetPeers(map[uint64]*metapb.Peer{4: {StoreId: 4, Role: metapb.PeerRole_Learner}}).prepareBuild()
c.Assert(err, NotNil)
_, err := suite.newBuilder().SetPeers(map[uint64]*metapb.Peer{4: {StoreId: 4, Role: metapb.PeerRole_Learner}}).prepareBuild()
suite.Error(err)

// use joint consensus
builder := s.newBuilder().SetPeers(map[uint64]*metapb.Peer{
builder := suite.newBuilder().SetPeers(map[uint64]*metapb.Peer{
1: {StoreId: 1, Role: metapb.PeerRole_Learner},
3: {StoreId: 3},
4: {StoreId: 4, Id: 14},
5: {StoreId: 5, Role: metapb.PeerRole_Learner},
})
_, err = builder.prepareBuild()
c.Assert(err, IsNil)
c.Assert(builder.toAdd, HasLen, 2)
c.Assert(builder.toAdd[4].GetRole(), Not(Equals), metapb.PeerRole_Learner)
c.Assert(builder.toAdd[4].GetId(), Equals, uint64(14))
c.Assert(builder.toAdd[5].GetRole(), Equals, metapb.PeerRole_Learner)
c.Assert(builder.toAdd[5].GetId(), Not(Equals), uint64(0))
c.Assert(builder.toRemove, HasLen, 1)
c.Assert(builder.toRemove[2], NotNil)
c.Assert(builder.toPromote, HasLen, 1)
c.Assert(builder.toPromote[3], NotNil)
c.Assert(builder.toDemote, HasLen, 1)
c.Assert(builder.toDemote[1], NotNil)
c.Assert(builder.currentLeaderStoreID, Equals, uint64(1))
suite.NoError(err)
suite.Len(builder.toAdd, 2)
suite.NotEqual(metapb.PeerRole_Learner, builder.toAdd[4].GetRole())
suite.Equal(uint64(14), builder.toAdd[4].GetId())
suite.Equal(metapb.PeerRole_Learner, builder.toAdd[5].GetRole())
suite.NotEqual(uint64(0), builder.toAdd[5].GetId())
suite.Len(builder.toRemove, 1)
suite.NotNil(builder.toRemove[2])
suite.Len(builder.toPromote, 1)
suite.NotNil(builder.toPromote[3])
suite.Len(builder.toDemote, 1)
suite.NotNil(builder.toDemote[1])
suite.Equal(uint64(1), builder.currentLeaderStoreID)

// do not use joint consensus
builder = s.newBuilder().SetPeers(map[uint64]*metapb.Peer{
builder = suite.newBuilder().SetPeers(map[uint64]*metapb.Peer{
1: {StoreId: 1, Role: metapb.PeerRole_Learner},
2: {StoreId: 2},
3: {StoreId: 3},
Expand All @@ -150,22 +155,22 @@ func (s *testBuilderSuite) TestPrepareBuild(c *C) {
})
builder.useJointConsensus = false
_, err = builder.prepareBuild()
c.Assert(err, IsNil)
c.Assert(builder.toAdd, HasLen, 3)
c.Assert(builder.toAdd[1].GetRole(), Equals, metapb.PeerRole_Learner)
c.Assert(builder.toAdd[1].GetId(), Not(Equals), uint64(0))
c.Assert(builder.toAdd[4].GetRole(), Not(Equals), metapb.PeerRole_Learner)
c.Assert(builder.toAdd[4].GetId(), Equals, uint64(14))
c.Assert(builder.toAdd[5].GetRole(), Equals, metapb.PeerRole_Learner)
c.Assert(builder.toAdd[5].GetId(), Not(Equals), uint64(0))
c.Assert(builder.toRemove, HasLen, 1)
c.Assert(builder.toRemove[1], NotNil)
c.Assert(builder.toPromote, HasLen, 1)
c.Assert(builder.toPromote[3], NotNil)
c.Assert(builder.currentLeaderStoreID, Equals, uint64(1))
suite.NoError(err)
suite.Len(builder.toAdd, 3)
suite.Equal(metapb.PeerRole_Learner, builder.toAdd[1].GetRole())
suite.NotEqual(uint64(0), builder.toAdd[1].GetId())
suite.NotEqual(metapb.PeerRole_Learner, builder.toAdd[4].GetRole())
suite.Equal(uint64(14), builder.toAdd[4].GetId())
suite.Equal(metapb.PeerRole_Learner, builder.toAdd[5].GetRole())
suite.NotEqual(uint64(0), builder.toAdd[5].GetId())
suite.Len(builder.toRemove, 1)
suite.NotNil(builder.toRemove[1])
suite.Len(builder.toPromote, 1)
suite.NotNil(builder.toPromote[3])
suite.Equal(uint64(1), builder.currentLeaderStoreID)
}

func (s *testBuilderSuite) TestBuild(c *C) {
func (suite *operatorBuilderTestSuite) TestBuild() {
type testCase struct {
name string
useJointConsensus bool
Expand Down Expand Up @@ -530,9 +535,9 @@ func (s *testBuilderSuite) TestBuild(c *C) {
}

for _, tc := range cases {
c.Log(tc.name)
suite.T().Log(tc.name)
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: tc.originPeers}, tc.originPeers[0])
builder := NewBuilder("test", s.cluster, region)
builder := NewBuilder("test", suite.cluster, region)
builder.useJointConsensus = tc.useJointConsensus
m := make(map[uint64]*metapb.Peer)
for _, p := range tc.targetPeers {
Expand All @@ -541,71 +546,69 @@ func (s *testBuilderSuite) TestBuild(c *C) {
builder.SetPeers(m).SetLeader(tc.targetPeers[0].GetStoreId())
op, err := builder.Build(0)
if len(tc.steps) == 0 {
c.Assert(err, NotNil)
suite.Error(err)
continue
}
c.Assert(err, IsNil)
c.Assert(op.Kind(), Equals, tc.kind)
c.Assert(op.Len(), Equals, len(tc.steps))
suite.NoError(err)
suite.Equal(tc.kind, op.Kind())
suite.Len(tc.steps, op.Len())
for i := 0; i < op.Len(); i++ {
switch step := op.Step(i).(type) {
case TransferLeader:
c.Assert(step.FromStore, Equals, tc.steps[i].(TransferLeader).FromStore)
c.Assert(step.ToStore, Equals, tc.steps[i].(TransferLeader).ToStore)
suite.Equal(tc.steps[i].(TransferLeader).FromStore, step.FromStore)
suite.Equal(tc.steps[i].(TransferLeader).ToStore, step.ToStore)
case AddPeer:
c.Assert(step.ToStore, Equals, tc.steps[i].(AddPeer).ToStore)
suite.Equal(tc.steps[i].(AddPeer).ToStore, step.ToStore)
case RemovePeer:
c.Assert(step.FromStore, Equals, tc.steps[i].(RemovePeer).FromStore)
suite.Equal(tc.steps[i].(RemovePeer).FromStore, step.FromStore)
case AddLearner:
c.Assert(step.ToStore, Equals, tc.steps[i].(AddLearner).ToStore)
suite.Equal(tc.steps[i].(AddLearner).ToStore, step.ToStore)
case PromoteLearner:
c.Assert(step.ToStore, Equals, tc.steps[i].(PromoteLearner).ToStore)
suite.Equal(tc.steps[i].(PromoteLearner).ToStore, step.ToStore)
case ChangePeerV2Enter:
c.Assert(len(step.PromoteLearners), Equals, len(tc.steps[i].(ChangePeerV2Enter).PromoteLearners))
c.Assert(len(step.DemoteVoters), Equals, len(tc.steps[i].(ChangePeerV2Enter).DemoteVoters))
suite.Len(tc.steps[i].(ChangePeerV2Enter).PromoteLearners, len(step.PromoteLearners))
suite.Len(tc.steps[i].(ChangePeerV2Enter).DemoteVoters, len(step.DemoteVoters))
for j, p := range tc.steps[i].(ChangePeerV2Enter).PromoteLearners {
c.Assert(step.PromoteLearners[j].ToStore, Equals, p.ToStore)
suite.Equal(p.ToStore, step.PromoteLearners[j].ToStore)
}
for j, d := range tc.steps[i].(ChangePeerV2Enter).DemoteVoters {
c.Assert(step.DemoteVoters[j].ToStore, Equals, d.ToStore)
suite.Equal(d.ToStore, step.DemoteVoters[j].ToStore)
}
case ChangePeerV2Leave:
c.Assert(len(step.PromoteLearners), Equals, len(tc.steps[i].(ChangePeerV2Leave).PromoteLearners))
c.Assert(len(step.DemoteVoters), Equals, len(tc.steps[i].(ChangePeerV2Leave).DemoteVoters))
suite.Len(tc.steps[i].(ChangePeerV2Leave).PromoteLearners, len(step.PromoteLearners))
suite.Len(tc.steps[i].(ChangePeerV2Leave).DemoteVoters, len(step.DemoteVoters))
for j, p := range tc.steps[i].(ChangePeerV2Leave).PromoteLearners {
c.Assert(step.PromoteLearners[j].ToStore, Equals, p.ToStore)
suite.Equal(p.ToStore, step.PromoteLearners[j].ToStore)
}
for j, d := range tc.steps[i].(ChangePeerV2Leave).DemoteVoters {
c.Assert(step.DemoteVoters[j].ToStore, Equals, d.ToStore)
suite.Equal(d.ToStore, step.DemoteVoters[j].ToStore)
}
}
}
}
}

// Test for not set unhealthy peer as target for promote learner and transfer leader
func (s *testBuilderSuite) TestTargetUnhealthyPeer(c *C) {
func (suite *operatorBuilderTestSuite) TestTargetUnhealthyPeer() {
p := &metapb.Peer{Id: 2, StoreId: 2, Role: metapb.PeerRole_Learner}
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: []*metapb.Peer{{Id: 1, StoreId: 1},
p}}, &metapb.Peer{Id: 1, StoreId: 1}, core.WithPendingPeers([]*metapb.Peer{p}))
builder := NewBuilder("test", s.cluster, region)
builder := NewBuilder("test", suite.cluster, region)
builder.PromoteLearner(2)
c.Assert(builder.err, NotNil)
suite.Error(builder.err)
region = core.NewRegionInfo(&metapb.Region{Id: 1, Peers: []*metapb.Peer{{Id: 1, StoreId: 1},
p}}, &metapb.Peer{Id: 1, StoreId: 1}, core.WithDownPeers([]*pdpb.PeerStats{{Peer: p}}))
builder = NewBuilder("test", s.cluster, region)
builder = NewBuilder("test", suite.cluster, region)
builder.PromoteLearner(2)
c.Assert(builder.err, NotNil)

suite.Error(builder.err)
p = &metapb.Peer{Id: 2, StoreId: 2, Role: metapb.PeerRole_Voter}
region = core.NewRegionInfo(&metapb.Region{Id: 1, Peers: []*metapb.Peer{{Id: 1, StoreId: 1},
p}}, &metapb.Peer{Id: 1, StoreId: 1}, core.WithPendingPeers([]*metapb.Peer{p}))
builder = NewBuilder("test", s.cluster, region)
builder = NewBuilder("test", suite.cluster, region)
builder.SetLeader(2)
c.Assert(builder.err, NotNil)
suite.Error(builder.err)
region = core.NewRegionInfo(&metapb.Region{Id: 1, Peers: []*metapb.Peer{{Id: 1, StoreId: 1},
p}}, &metapb.Peer{Id: 1, StoreId: 1}, core.WithDownPeers([]*pdpb.PeerStats{{Peer: p}}))
builder = NewBuilder("test", s.cluster, region)
builder = NewBuilder("test", suite.cluster, region)
builder.SetLeader(2)
c.Assert(builder.err, NotNil)
suite.Error(builder.err)
}
17 changes: 8 additions & 9 deletions server/schedule/operator/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
package operator

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

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

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

func (s *testOpStatusSuite) TestIsEndStatus(c *C) {
func TestIsEndStatus(t *testing.T) {
re := require.New(t)
for st := OpStatus(0); st < firstEndStatus; st++ {
c.Assert(IsEndStatus(st), IsFalse)
re.False(IsEndStatus(st))
}
for st := firstEndStatus; st < statusCount; st++ {
c.Assert(IsEndStatus(st), IsTrue)
re.True(IsEndStatus(st))
}
for st := statusCount; st < statusCount+100; st++ {
c.Assert(IsEndStatus(st), IsFalse)
re.False(IsEndStatus(st))
}
}
Loading

0 comments on commit 448d689

Please sign in to comment.