From 448d6895a87eeb4a3c288cf0ad45c5c96ba4a1cb Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 21 Jun 2022 12:02:37 +0800 Subject: [PATCH] operator: migrate test framework to testify (#5191) ref tikv/pd#4813 Signed-off-by: lhy1024 Co-authored-by: Ti Chi Robot --- server/schedule/operator/builder_test.go | 237 +++++++++--------- server/schedule/operator/status_test.go | 17 +- .../schedule/operator/status_tracker_test.go | 122 ++++----- server/schedule/operator/step_test.go | 130 +++++----- 4 files changed, 258 insertions(+), 248 deletions(-) diff --git a/server/schedule/operator/builder_test.go b/server/schedule/operator/builder_test.go index 22fac017ec00..ed8e6b88ffe5 100644 --- a/server/schedule/operator/builder_test.go +++ b/server/schedule/operator/builder_test.go @@ -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}, @@ -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 @@ -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 { @@ -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) } diff --git a/server/schedule/operator/status_test.go b/server/schedule/operator/status_test.go index 42502e1e0969..6bdf2710657b 100644 --- a/server/schedule/operator/status_test.go +++ b/server/schedule/operator/status_test.go @@ -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)) } } diff --git a/server/schedule/operator/status_tracker_test.go b/server/schedule/operator/status_tracker_test.go index 8ada8b386f2f..d4441b0e7b60 100644 --- a/server/schedule/operator/status_tracker_test.go +++ b/server/schedule/operator/status_tracker_test.go @@ -15,64 +15,64 @@ package operator import ( + "testing" "time" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -var _ = Suite(&testOpStatusTrackerSuite{}) - -type testOpStatusTrackerSuite struct{} - -func (s *testOpStatusTrackerSuite) TestCreate(c *C) { +func TestCreate(t *testing.T) { + re := require.New(t) before := time.Now() trk := NewOpStatusTracker() - c.Assert(trk.Status(), Equals, CREATED) - c.Assert(trk.ReachTime(), DeepEquals, trk.ReachTimeOf(CREATED)) - checkTimeOrder(c, before, trk.ReachTime(), time.Now()) - checkReachTime(c, &trk, CREATED) + re.Equal(CREATED, trk.Status()) + re.Equal(trk.ReachTimeOf(CREATED), trk.ReachTime()) + checkTimeOrder(re, before, trk.ReachTime(), time.Now()) + checkReachTime(re, &trk, CREATED) } -func (s *testOpStatusTrackerSuite) TestNonEndTrans(c *C) { +func TestNonEndTrans(t *testing.T) { + re := require.New(t) { trk := NewOpStatusTracker() - checkInvalidTrans(c, &trk, SUCCESS, REPLACED, TIMEOUT) - checkValidTrans(c, &trk, STARTED) - checkInvalidTrans(c, &trk, EXPIRED) - checkValidTrans(c, &trk, SUCCESS) - checkReachTime(c, &trk, CREATED, STARTED, SUCCESS) + checkInvalidTrans(re, &trk, SUCCESS, REPLACED, TIMEOUT) + checkValidTrans(re, &trk, STARTED) + checkInvalidTrans(re, &trk, EXPIRED) + checkValidTrans(re, &trk, SUCCESS) + checkReachTime(re, &trk, CREATED, STARTED, SUCCESS) } { trk := NewOpStatusTracker() - checkValidTrans(c, &trk, CANCELED) - checkReachTime(c, &trk, CREATED, CANCELED) + checkValidTrans(re, &trk, CANCELED) + checkReachTime(re, &trk, CREATED, CANCELED) } { trk := NewOpStatusTracker() - checkValidTrans(c, &trk, STARTED) - checkValidTrans(c, &trk, CANCELED) - checkReachTime(c, &trk, CREATED, STARTED, CANCELED) + checkValidTrans(re, &trk, STARTED) + checkValidTrans(re, &trk, CANCELED) + checkReachTime(re, &trk, CREATED, STARTED, CANCELED) } { trk := NewOpStatusTracker() - checkValidTrans(c, &trk, STARTED) - checkValidTrans(c, &trk, REPLACED) - checkReachTime(c, &trk, CREATED, STARTED, REPLACED) + checkValidTrans(re, &trk, STARTED) + checkValidTrans(re, &trk, REPLACED) + checkReachTime(re, &trk, CREATED, STARTED, REPLACED) } { trk := NewOpStatusTracker() - checkValidTrans(c, &trk, EXPIRED) - checkReachTime(c, &trk, CREATED, EXPIRED) + checkValidTrans(re, &trk, EXPIRED) + checkReachTime(re, &trk, CREATED, EXPIRED) } { trk := NewOpStatusTracker() - checkValidTrans(c, &trk, STARTED) - checkValidTrans(c, &trk, TIMEOUT) - checkReachTime(c, &trk, CREATED, STARTED, TIMEOUT) + checkValidTrans(re, &trk, STARTED) + checkValidTrans(re, &trk, TIMEOUT) + checkReachTime(re, &trk, CREATED, STARTED, TIMEOUT) } } -func (s *testOpStatusTrackerSuite) TestEndStatusTrans(c *C) { +func TestEndStatusTrans(t *testing.T) { + re := require.New(t) allStatus := make([]OpStatus, 0, statusCount) for st := OpStatus(0); st < statusCount; st++ { allStatus = append(allStatus, st) @@ -80,41 +80,43 @@ func (s *testOpStatusTrackerSuite) TestEndStatusTrans(c *C) { for from := firstEndStatus; from < statusCount; from++ { trk := NewOpStatusTracker() trk.current = from - c.Assert(trk.IsEnd(), IsTrue) - checkInvalidTrans(c, &trk, allStatus...) + re.True(trk.IsEnd()) + checkInvalidTrans(re, &trk, allStatus...) } } -func (s *testOpStatusTrackerSuite) TestCheckExpired(c *C) { +func TestCheckExpired(t *testing.T) { + re := require.New(t) { // Not expired before := time.Now() trk := NewOpStatusTracker() after := time.Now() - c.Assert(trk.CheckExpired(10*time.Second), IsFalse) - c.Assert(trk.Status(), Equals, CREATED) - checkTimeOrder(c, before, trk.ReachTime(), after) + re.False(trk.CheckExpired(10 * time.Second)) + re.Equal(CREATED, trk.Status()) + checkTimeOrder(re, before, trk.ReachTime(), after) } { // Expired but status not changed trk := NewOpStatusTracker() trk.setTime(CREATED, time.Now().Add(-10*time.Second)) - c.Assert(trk.CheckExpired(5*time.Second), IsTrue) - c.Assert(trk.Status(), Equals, EXPIRED) + re.True(trk.CheckExpired(5 * time.Second)) + re.Equal(EXPIRED, trk.Status()) } { // Expired and status changed trk := NewOpStatusTracker() before := time.Now() - c.Assert(trk.To(EXPIRED), IsTrue) + re.True(trk.To(EXPIRED)) after := time.Now() - c.Assert(trk.CheckExpired(0), IsTrue) - c.Assert(trk.Status(), Equals, EXPIRED) - checkTimeOrder(c, before, trk.ReachTime(), after) + re.True(trk.CheckExpired(0)) + re.Equal(EXPIRED, trk.Status()) + checkTimeOrder(re, before, trk.ReachTime(), after) } } -func (s *testOpStatusTrackerSuite) TestCheckStepTimeout(c *C) { +func TestCheckStepTimeout(t *testing.T) { + re := require.New(t) testdata := []struct { step OpStep start time.Time @@ -133,45 +135,45 @@ func (s *testOpStatusTrackerSuite) TestCheckStepTimeout(c *C) { // Timeout and status changed trk := NewOpStatusTracker() trk.To(STARTED) - c.Assert(trk.CheckStepTimeout(v.start, v.step, 0), Equals, v.status == TIMEOUT) - c.Assert(trk.Status(), Equals, v.status) + re.Equal(v.status == TIMEOUT, trk.CheckStepTimeout(v.start, v.step, 0)) + re.Equal(v.status, trk.Status()) } } -func checkTimeOrder(c *C, t1, t2, t3 time.Time) { - c.Assert(t1.Before(t2), IsTrue) - c.Assert(t3.After(t2), IsTrue) +func checkTimeOrder(re *require.Assertions, t1, t2, t3 time.Time) { + re.True(t1.Before(t2)) + re.True(t3.After(t2)) } -func checkValidTrans(c *C, trk *OpStatusTracker, st OpStatus) { +func checkValidTrans(re *require.Assertions, trk *OpStatusTracker, st OpStatus) { before := time.Now() - c.Assert(trk.To(st), IsTrue) - c.Assert(trk.Status(), Equals, st) - c.Assert(trk.ReachTime(), DeepEquals, trk.ReachTimeOf(st)) - checkTimeOrder(c, before, trk.ReachTime(), time.Now()) + re.True(trk.To(st)) + re.Equal(st, trk.Status()) + re.Equal(trk.ReachTimeOf(st), trk.ReachTime()) + checkTimeOrder(re, before, trk.ReachTime(), time.Now()) } -func checkInvalidTrans(c *C, trk *OpStatusTracker, sts ...OpStatus) { +func checkInvalidTrans(re *require.Assertions, trk *OpStatusTracker, sts ...OpStatus) { origin := trk.Status() originTime := trk.ReachTime() sts = append(sts, statusCount, statusCount+1, statusCount+10) for _, st := range sts { - c.Assert(trk.To(st), IsFalse) - c.Assert(trk.Status(), Equals, origin) - c.Assert(trk.ReachTime(), DeepEquals, originTime) + re.False(trk.To(st)) + re.Equal(origin, trk.Status()) + re.Equal(originTime, trk.ReachTime()) } } -func checkReachTime(c *C, trk *OpStatusTracker, reached ...OpStatus) { +func checkReachTime(re *require.Assertions, trk *OpStatusTracker, reached ...OpStatus) { reachedMap := make(map[OpStatus]struct{}, len(reached)) for _, st := range reached { - c.Assert(trk.ReachTimeOf(st).IsZero(), IsFalse) + re.False(trk.ReachTimeOf(st).IsZero()) reachedMap[st] = struct{}{} } for st := OpStatus(0); st <= statusCount+10; st++ { if _, ok := reachedMap[st]; ok { continue } - c.Assert(trk.ReachTimeOf(st).IsZero(), IsTrue) + re.True(trk.ReachTimeOf(st).IsZero()) } } diff --git a/server/schedule/operator/step_test.go b/server/schedule/operator/step_test.go index f4bd9865b25a..aa2f18c72203 100644 --- a/server/schedule/operator/step_test.go +++ b/server/schedule/operator/step_test.go @@ -16,38 +16,43 @@ package operator import ( "context" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/kvproto/pkg/metapb" + "github.com/stretchr/testify/suite" "github.com/tikv/pd/pkg/mock/mockcluster" "github.com/tikv/pd/server/config" "github.com/tikv/pd/server/core" ) -type testStepSuite struct { +type operatorStepTestSuite struct { + suite.Suite + cluster *mockcluster.Cluster } -var _ = Suite(&testStepSuite{}) +func TestOperatorStepTestSuite(t *testing.T) { + suite.Run(t, new(operatorStepTestSuite)) +} type testCase struct { - Peers []*metapb.Peer // first is leader - ConfVerChanged uint64 - IsFinish bool - CheckInProgres Checker + Peers []*metapb.Peer // first is leader + ConfVerChanged uint64 + IsFinish bool + CheckInProgress func(err error, msgAndArgs ...interface{}) bool } -func (s *testStepSuite) SetUpTest(c *C) { - s.cluster = mockcluster.NewCluster(context.Background(), config.NewTestOptions()) +func (suite *operatorStepTestSuite) SetupTest() { + suite.cluster = mockcluster.NewCluster(context.Background(), config.NewTestOptions()) for i := 1; i <= 10; i++ { - s.cluster.PutStoreWithLabels(uint64(i)) + suite.cluster.PutStoreWithLabels(uint64(i)) } - s.cluster.SetStoreDown(8) - s.cluster.SetStoreDown(9) - s.cluster.SetStoreDown(10) + suite.cluster.SetStoreDown(8) + suite.cluster.SetStoreDown(9) + suite.cluster.SetStoreDown(10) } -func (s *testStepSuite) TestTransferLeader(c *C) { +func (suite *operatorStepTestSuite) TestTransferLeader() { step := TransferLeader{FromStore: 1, ToStore: 2} cases := []testCase{ { @@ -58,7 +63,7 @@ func (s *testStepSuite) TestTransferLeader(c *C) { }, 0, false, - IsNil, + suite.NoError, }, { []*metapb.Peer{ @@ -68,7 +73,7 @@ func (s *testStepSuite) TestTransferLeader(c *C) { }, 0, true, - IsNil, + suite.NoError, }, { []*metapb.Peer{ @@ -78,10 +83,10 @@ func (s *testStepSuite) TestTransferLeader(c *C) { }, 0, false, - IsNil, + suite.NoError, }, } - s.check(c, step, "transfer leader from store 1 to store 2", cases) + suite.check(step, "transfer leader from store 1 to store 2", cases) step = TransferLeader{FromStore: 1, ToStore: 9} // 9 is down cases = []testCase{ @@ -93,13 +98,13 @@ func (s *testStepSuite) TestTransferLeader(c *C) { }, 0, false, - NotNil, + suite.Error, }, } - s.check(c, step, "transfer leader from store 1 to store 9", cases) + suite.check(step, "transfer leader from store 1 to store 9", cases) } -func (s *testStepSuite) TestAddPeer(c *C) { +func (suite *operatorStepTestSuite) TestAddPeer() { step := AddPeer{ToStore: 2, PeerID: 2} cases := []testCase{ { @@ -108,7 +113,7 @@ func (s *testStepSuite) TestAddPeer(c *C) { }, 0, false, - IsNil, + suite.NoError, }, { []*metapb.Peer{ @@ -117,10 +122,10 @@ func (s *testStepSuite) TestAddPeer(c *C) { }, 1, true, - IsNil, + suite.NoError, }, } - s.check(c, step, "add peer 2 on store 2", cases) + suite.check(step, "add peer 2 on store 2", cases) step = AddPeer{ToStore: 9, PeerID: 9} cases = []testCase{ @@ -130,13 +135,13 @@ func (s *testStepSuite) TestAddPeer(c *C) { }, 0, false, - NotNil, + suite.Error, }, } - s.check(c, step, "add peer 9 on store 9", cases) + suite.check(step, "add peer 9 on store 9", cases) } -func (s *testStepSuite) TestAddLearner(c *C) { +func (suite *operatorStepTestSuite) TestAddLearner() { step := AddLearner{ToStore: 2, PeerID: 2} cases := []testCase{ { @@ -145,7 +150,7 @@ func (s *testStepSuite) TestAddLearner(c *C) { }, 0, false, - IsNil, + suite.NoError, }, { []*metapb.Peer{ @@ -154,10 +159,10 @@ func (s *testStepSuite) TestAddLearner(c *C) { }, 1, true, - IsNil, + suite.NoError, }, } - s.check(c, step, "add learner peer 2 on store 2", cases) + suite.check(step, "add learner peer 2 on store 2", cases) step = AddLearner{ToStore: 9, PeerID: 9} cases = []testCase{ @@ -167,13 +172,13 @@ func (s *testStepSuite) TestAddLearner(c *C) { }, 0, false, - NotNil, + suite.Error, }, } - s.check(c, step, "add learner peer 9 on store 9", cases) + suite.check(step, "add learner peer 9 on store 9", cases) } -func (s *testStepSuite) TestChangePeerV2Enter(c *C) { +func (suite *operatorStepTestSuite) TestChangePeerV2Enter() { cpe := ChangePeerV2Enter{ PromoteLearners: []PromoteLearner{{PeerID: 3, ToStore: 3}, {PeerID: 4, ToStore: 4}}, DemoteVoters: []DemoteVoter{{PeerID: 1, ToStore: 1}, {PeerID: 2, ToStore: 2}}, @@ -188,7 +193,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - IsNil, + suite.NoError, }, { // after step []*metapb.Peer{ @@ -199,7 +204,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 4, true, - IsNil, + suite.NoError, }, { // miss peer id []*metapb.Peer{ @@ -210,7 +215,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // miss store id []*metapb.Peer{ @@ -221,7 +226,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // miss peer id []*metapb.Peer{ @@ -232,7 +237,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // change is not atomic []*metapb.Peer{ @@ -243,7 +248,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // change is not atomic []*metapb.Peer{ @@ -254,7 +259,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // there are other peers in the joint state []*metapb.Peer{ @@ -266,7 +271,7 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 4, true, - NotNil, + suite.Error, }, { // there are other peers in the joint state []*metapb.Peer{ @@ -279,16 +284,16 @@ func (s *testStepSuite) TestChangePeerV2Enter(c *C) { }, 0, false, - NotNil, + suite.Error, }, } desc := "use joint consensus, " + "promote learner peer 3 on store 3 to voter, promote learner peer 4 on store 4 to voter, " + "demote voter peer 1 on store 1 to learner, demote voter peer 2 on store 2 to learner" - s.check(c, cpe, desc, cases) + suite.check(cpe, desc, cases) } -func (s *testStepSuite) TestChangePeerV2Leave(c *C) { +func (suite *operatorStepTestSuite) TestChangePeerV2Leave() { cpl := ChangePeerV2Leave{ PromoteLearners: []PromoteLearner{{PeerID: 3, ToStore: 3}, {PeerID: 4, ToStore: 4}}, DemoteVoters: []DemoteVoter{{PeerID: 1, ToStore: 1}, {PeerID: 2, ToStore: 2}}, @@ -303,7 +308,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - IsNil, + suite.NoError, }, { // after step []*metapb.Peer{ @@ -314,7 +319,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 4, true, - IsNil, + suite.NoError, }, { // miss peer id []*metapb.Peer{ @@ -325,7 +330,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // miss store id []*metapb.Peer{ @@ -336,7 +341,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // miss peer id []*metapb.Peer{ @@ -347,7 +352,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // change is not atomic []*metapb.Peer{ @@ -358,7 +363,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // change is not atomic []*metapb.Peer{ @@ -369,7 +374,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // there are other peers in the joint state []*metapb.Peer{ @@ -381,7 +386,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, { // there are other peers in the joint state []*metapb.Peer{ @@ -394,7 +399,7 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 4, false, - NotNil, + suite.Error, }, { // demote leader []*metapb.Peer{ @@ -405,21 +410,22 @@ func (s *testStepSuite) TestChangePeerV2Leave(c *C) { }, 0, false, - NotNil, + suite.Error, }, } desc := "leave joint state, " + "promote learner peer 3 on store 3 to voter, promote learner peer 4 on store 4 to voter, " + "demote voter peer 1 on store 1 to learner, demote voter peer 2 on store 2 to learner" - s.check(c, cpl, desc, cases) + suite.check(cpl, desc, cases) } -func (s *testStepSuite) check(c *C, step OpStep, desc string, cases []testCase) { - c.Assert(step.String(), Equals, desc) - for _, tc := range cases { - region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: tc.Peers}, tc.Peers[0]) - c.Assert(step.ConfVerChanged(region), Equals, tc.ConfVerChanged) - c.Assert(step.IsFinish(region), Equals, tc.IsFinish) - c.Assert(step.CheckInProgress(s.cluster, region), tc.CheckInProgres) +func (suite *operatorStepTestSuite) check(step OpStep, desc string, cases []testCase) { + suite.Equal(desc, step.String()) + for _, testCase := range cases { + region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: testCase.Peers}, testCase.Peers[0]) + suite.Equal(testCase.ConfVerChanged, step.ConfVerChanged(region)) + suite.Equal(testCase.IsFinish, step.IsFinish(region)) + err := step.CheckInProgress(suite.cluster, region) + testCase.CheckInProgress(err) } }