Skip to content

Commit

Permalink
migrate test framework to testify
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Jun 17, 2022
1 parent 9acd56a commit eee23be
Show file tree
Hide file tree
Showing 8 changed files with 846 additions and 873 deletions.
60 changes: 60 additions & 0 deletions pkg/testutil/operator_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package testutil

import (
"github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/server/schedule/operator"
)

Expand Down Expand Up @@ -141,3 +142,62 @@ func CheckTransferPeerWithLeaderTransferFrom(c *check.C, op *operator.Operator,
kind |= operator.OpRegion | operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
}

// CheckAddPeerWithTestify checks if the operator is to add peer on specified store.
func CheckAddPeerWithTestify(re *require.Assertions, op *operator.Operator, kind operator.OpKind, storeID uint64) {
re.NotNil(op)
re.Equal(2, op.Len())
re.Equal(storeID, op.Step(0).(operator.AddLearner).ToStore)
re.IsType(operator.PromoteLearner{}, op.Step(1))
kind |= operator.OpRegion
re.Equal(kind, op.Kind()&kind)
}

// CheckRemovePeerWithTestify checks if the operator is to remove peer on specified store.
func CheckRemovePeerWithTestify(re *require.Assertions, op *operator.Operator, storeID uint64) {
re.NotNil(op)
if op.Len() == 1 {
re.Equal(storeID, op.Step(0).(operator.RemovePeer).FromStore)
} else {
re.Equal(2, op.Len())
re.Equal(storeID, op.Step(0).(operator.TransferLeader).FromStore)
re.Equal(storeID, op.Step(1).(operator.RemovePeer).FromStore)
}
}

// CheckTransferPeerWithTestify checks if the operator is to transfer peer between the specified source and target stores.
func CheckTransferPeerWithTestify(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)

steps, _ := trimTransferLeaders(op)
re.Len(steps, 3)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
kind |= operator.OpRegion
re.Equal(kind, op.Kind()&kind)
}

// CheckSteps checks if the operator matches the given steps.
func CheckSteps(re *require.Assertions, op *operator.Operator, steps []operator.OpStep) {
re.NotEqual(0, op.Kind()&operator.OpMerge)
re.NotNil(steps)
re.Equal(len(steps), op.Len())
for i := range steps {
switch op.Step(i).(type) {
case operator.AddLearner:
re.Equal(steps[i].(operator.AddLearner).ToStore, op.Step(i).(operator.AddLearner).ToStore)
case operator.PromoteLearner:
re.Equal(steps[i].(operator.PromoteLearner).ToStore, op.Step(i).(operator.PromoteLearner).ToStore)
case operator.TransferLeader:
re.Equal(steps[i].(operator.TransferLeader).FromStore, op.Step(i).(operator.TransferLeader).FromStore)
re.Equal(steps[i].(operator.TransferLeader).ToStore, op.Step(i).(operator.TransferLeader).ToStore)
case operator.RemovePeer:
re.Equal(steps[i].(operator.RemovePeer).FromStore, op.Step(i).(operator.RemovePeer).FromStore)
case operator.MergeRegion:
re.Equal(steps[i].(operator.MergeRegion).IsPassive, op.Step(i).(operator.MergeRegion).IsPassive)
default:
re.FailNow("unknown operator step type")
}
}
}
61 changes: 22 additions & 39 deletions server/schedule/checker/joint_state_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,25 @@ package checker

import (
"context"
"testing"

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

var _ = Suite(&testJointStateCheckerSuite{})

type testJointStateCheckerSuite struct {
cluster *mockcluster.Cluster
jsc *JointStateChecker
ctx context.Context
cancel context.CancelFunc
}

func (s *testJointStateCheckerSuite) SetUpSuite(c *C) {
s.ctx, s.cancel = context.WithCancel(context.Background())
}

func (s *testJointStateCheckerSuite) TearDownTest(c *C) {
s.cancel()
}

func (s *testJointStateCheckerSuite) SetUpTest(c *C) {
s.cluster = mockcluster.NewCluster(s.ctx, config.NewTestOptions())
s.jsc = NewJointStateChecker(s.cluster)
func TestLeaveJointState(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster := mockcluster.NewCluster(ctx, config.NewTestOptions())
jsc := NewJointStateChecker(cluster)
for id := uint64(1); id <= 10; id++ {
s.cluster.PutStoreWithLabels(id)
cluster.PutStoreWithLabels(id)
}
}

func (s *testJointStateCheckerSuite) TestLeaveJointState(c *C) {
jsc := s.jsc
type testCase struct {
Peers []*metapb.Peer // first is leader
OpSteps []operator.OpStep
Expand Down Expand Up @@ -131,38 +114,38 @@ func (s *testJointStateCheckerSuite) TestLeaveJointState(c *C) {
for _, tc := range cases {
region := core.NewRegionInfo(&metapb.Region{Id: 1, Peers: tc.Peers}, tc.Peers[0])
op := jsc.Check(region)
s.checkSteps(c, op, tc.OpSteps)
checkSteps(re, op, tc.OpSteps)
}
}

func (s *testJointStateCheckerSuite) checkSteps(c *C, op *operator.Operator, steps []operator.OpStep) {
func checkSteps(re *require.Assertions, op *operator.Operator, steps []operator.OpStep) {
if len(steps) == 0 {
c.Assert(op, IsNil)
re.Nil(op)
return
}

c.Assert(op, NotNil)
c.Assert(op.Desc(), Equals, "leave-joint-state")
re.NotNil(op)
re.Equal("leave-joint-state", op.Desc())

c.Assert(op.Len(), Equals, len(steps))
re.Equal(len(steps), op.Len())
for i := range steps {
switch obtain := op.Step(i).(type) {
case operator.ChangePeerV2Leave:
expect := steps[i].(operator.ChangePeerV2Leave)
c.Assert(len(obtain.PromoteLearners), Equals, len(expect.PromoteLearners))
c.Assert(len(obtain.DemoteVoters), Equals, len(expect.DemoteVoters))
re.Equal(len(expect.PromoteLearners), len(obtain.PromoteLearners))
re.Equal(len(expect.DemoteVoters), len(obtain.DemoteVoters))
for j, p := range expect.PromoteLearners {
c.Assert(expect.PromoteLearners[j].ToStore, Equals, p.ToStore)
re.Equal(p.ToStore, expect.PromoteLearners[j].ToStore)
}
for j, d := range expect.DemoteVoters {
c.Assert(obtain.DemoteVoters[j].ToStore, Equals, d.ToStore)
re.Equal(d.ToStore, obtain.DemoteVoters[j].ToStore)
}
case operator.TransferLeader:
expect := steps[i].(operator.TransferLeader)
c.Assert(obtain.FromStore, Equals, expect.FromStore)
c.Assert(obtain.ToStore, Equals, expect.ToStore)
re.Equal(expect.FromStore, obtain.FromStore)
re.Equal(expect.ToStore, obtain.ToStore)
default:
c.Fatal("unknown operator step type")
re.FailNow("unknown operator step type")
}
}
}
44 changes: 15 additions & 29 deletions server/schedule/checker/learner_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,27 @@ package checker

import (
"context"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/schedule/operator"
"github.com/tikv/pd/server/versioninfo"
)

var _ = Suite(&testLearnerCheckerSuite{})

type testLearnerCheckerSuite struct {
cluster *mockcluster.Cluster
lc *LearnerChecker
ctx context.Context
cancel context.CancelFunc
}

func (s *testLearnerCheckerSuite) SetUpTest(c *C) {
s.ctx, s.cancel = context.WithCancel(context.Background())
s.cluster = mockcluster.NewCluster(s.ctx, config.NewTestOptions())
s.cluster.SetClusterVersion(versioninfo.MinSupportedVersion(versioninfo.Version4_0))
s.lc = NewLearnerChecker(s.cluster)
func TestPromoteLearner(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster := mockcluster.NewCluster(ctx, config.NewTestOptions())
cluster.SetClusterVersion(versioninfo.MinSupportedVersion(versioninfo.Version4_0))
lc := NewLearnerChecker(cluster)
for id := uint64(1); id <= 10; id++ {
s.cluster.PutStoreWithLabels(id)
cluster.PutStoreWithLabels(id)
}
}

func (s *testLearnerCheckerSuite) TearDownTest(c *C) {
s.cancel()
}

func (s *testLearnerCheckerSuite) TestPromoteLearner(c *C) {
lc := s.lc

region := core.NewRegionInfo(
&metapb.Region{
Expand All @@ -62,12 +48,12 @@ func (s *testLearnerCheckerSuite) TestPromoteLearner(c *C) {
},
}, &metapb.Peer{Id: 101, StoreId: 1})
op := lc.Check(region)
c.Assert(op, NotNil)
c.Assert(op.Desc(), Equals, "promote-learner")
c.Assert(op.Step(0), FitsTypeOf, operator.PromoteLearner{})
c.Assert(op.Step(0).(operator.PromoteLearner).ToStore, Equals, uint64(3))
re.NotNil(op)
re.Equal("promote-learner", op.Desc())
re.IsType(operator.PromoteLearner{}, op.Step(0))
re.Equal(uint64(3), op.Step(0).(operator.PromoteLearner).ToStore)

region = region.Clone(core.WithPendingPeers([]*metapb.Peer{region.GetPeer(103)}))
op = lc.Check(region)
c.Assert(op, IsNil)
re.Nil(op)
}
Loading

0 comments on commit eee23be

Please sign in to comment.