Skip to content

Commit

Permalink
scheduler: migrate test framework to testify (tikv#5298)
Browse files Browse the repository at this point in the history
close tikv#5121

Signed-off-by: HunDunDM <[email protected]>
  • Loading branch information
HunDunDM authored and CabinfeverB committed Jul 14, 2022
1 parent 4c4c70d commit d56f52d
Show file tree
Hide file tree
Showing 12 changed files with 1,046 additions and 1,088 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/montanaflynn/stats v0.5.0
github.com/petermattis/goid v0.0.0-20211229010228-4d14c490ee36 // indirect
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0
github.com/pingcap/errcode v0.3.0
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20200702092429-9f69995143ce
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d h1:U+PMnTlV2tu7RuMK5e
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d/go.mod h1:lXfE4PvvTW5xOjO6Mba8zDPyw8M93B6AQ7frTGnMlA8=
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ=
github.com/pingcap/check v0.0.0-20191107115940-caf2b9e6ccf4/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12 h1:rfD9v3+ppLPzoQBgZev0qYCpegrwyFx/BUpkApEiKdY=
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0 h1:HVl5539r48eA+uDuX/ziBmQCxzT1pGrzWbKuXT46Bq0=
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0/go.mod h1:PYMCGwN0JHjoqGr3HrZoD+b8Tgx8bKnArhSq8YVzUMc=
github.com/pingcap/errcode v0.3.0 h1:IF6LC/4+b1KNwrMlr2rBTUrojFPMexXBcDWZSpNwxjg=
github.com/pingcap/errcode v0.3.0/go.mod h1:4b2X8xSqxIroj/IZ9MX/VGZhAwc11wB9wRIzHvz6SeM=
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
Expand Down
97 changes: 46 additions & 51 deletions pkg/testutil/operator_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,39 @@
package testutil

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

// CheckTransferLeader checks if the operator is to transfer leader between the specified source and target stores.
func CheckTransferLeader(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
c.Assert(op.Step(0), check.DeepEquals, operator.TransferLeader{FromStore: sourceID, ToStore: targetID})
func CheckTransferLeader(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
re.Equal(op.Len(), 1)
re.Equal(operator.TransferLeader{FromStore: sourceID, ToStore: targetID}, op.Step(0))
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferLeaderFrom checks if the operator is to transfer leader out of the specified store.
func CheckTransferLeaderFrom(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
c.Assert(op.Step(0).(operator.TransferLeader).FromStore, check.Equals, sourceID)
func CheckTransferLeaderFrom(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
re.NotNil(op)
re.Equal(op.Len(), 1)
re.Equal(sourceID, op.Step(0).(operator.TransferLeader).FromStore)
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckMultiTargetTransferLeader checks if the operator is to transfer leader from the specified source to one of the target stores.
func CheckMultiTargetTransferLeader(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64, targetIDs []uint64) {
c.Assert(op, check.NotNil)
c.Assert(op.Len(), check.Equals, 1)
func CheckMultiTargetTransferLeader(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64, targetIDs []uint64) {
re.NotNil(op)
re.Equal(op.Len(), 1)
expectedOps := make([]interface{}, 0, len(targetIDs))
for _, targetID := range targetIDs {
expectedOps = append(expectedOps, operator.TransferLeader{FromStore: sourceID, ToStore: targetID, ToStores: targetIDs})
}
c.Assert(op.Step(0), check.DeepEqualsIn, expectedOps)
re.Contains(expectedOps, op.Step(0))
kind |= operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

func trimTransferLeaders(op *operator.Operator) (steps []operator.OpStep, lastLeader uint64) {
Expand All @@ -64,61 +63,57 @@ func trimTransferLeaders(op *operator.Operator) (steps []operator.OpStep, lastLe
}

// CheckTransferPeer checks if the operator is to transfer peer between the specified source and target stores.
func CheckTransferPeer(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeer(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, _ := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 3)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
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
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferLearner checks if the operator is to transfer learner between the specified source and target stores.
func CheckTransferLearner(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferLearner(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, _ := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 2)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1].(operator.RemovePeer).FromStore, check.Equals, sourceID)
re.Len(steps, 2)
re.Equal(targetID, steps[0].(operator.AddLearner).ToStore)
re.Equal(sourceID, steps[1].(operator.RemovePeer).FromStore)
kind |= operator.OpRegion
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferPeerWithLeaderTransfer checks if the operator is to transfer
// peer between the specified source and target stores and it meanwhile
// transfers the leader out of source store.
func CheckTransferPeerWithLeaderTransfer(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeerWithLeaderTransfer(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID, targetID uint64) {
re.NotNil(op)
steps, lastLeader := trimTransferLeaders(op)
c.Assert(steps, check.HasLen, 3)
c.Assert(steps[0].(operator.AddLearner).ToStore, check.Equals, targetID)
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
c.Assert(lastLeader, check.Not(check.Equals), uint64(0))
c.Assert(lastLeader, check.Not(check.Equals), sourceID)
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)
re.NotZero(lastLeader)
re.NotEqual(sourceID, lastLeader)
kind |= operator.OpRegion
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckTransferPeerWithLeaderTransferFrom checks if the operator is to transfer
// peer out of the specified store and it meanwhile transfers the leader out of
// the store.
func CheckTransferPeerWithLeaderTransferFrom(c *check.C, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
c.Assert(op, check.NotNil)

func CheckTransferPeerWithLeaderTransferFrom(re *require.Assertions, op *operator.Operator, kind operator.OpKind, sourceID uint64) {
re.NotNil(op)
steps, lastLeader := trimTransferLeaders(op)
c.Assert(steps[0], check.FitsTypeOf, operator.AddLearner{})
c.Assert(steps[1], check.FitsTypeOf, operator.PromoteLearner{})
c.Assert(steps[2].(operator.RemovePeer).FromStore, check.Equals, sourceID)
c.Assert(lastLeader, check.Not(check.Equals), uint64(0))
c.Assert(lastLeader, check.Not(check.Equals), sourceID)
re.IsType(operator.AddLearner{}, steps[0])
re.IsType(operator.PromoteLearner{}, steps[1])
re.Equal(sourceID, steps[2].(operator.RemovePeer).FromStore)
re.NotZero(lastLeader)
re.NotEqual(sourceID, lastLeader)
kind |= operator.OpRegion | operator.OpLeader
c.Assert(op.Kind()&kind, check.Equals, kind)
re.Equal(kind, op.Kind()&kind)
}

// CheckAddPeer checks if the operator is to add peer on specified store.
Expand Down Expand Up @@ -167,7 +162,7 @@ func CheckTransferPeerWithTestify(re *require.Assertions, op *operator.Operator,

// 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.NotZero(op.Kind() & operator.OpMerge)
re.NotNil(steps)
re.Len(steps, op.Len())
for i := range steps {
Expand Down
Loading

0 comments on commit d56f52d

Please sign in to comment.