From c0a6b116f6000f0d4d42136bb4ff3ce995dd28ee Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Wed, 22 Jun 2022 14:14:37 +0800 Subject: [PATCH] *: migrate test framework to testify (#5193) ref tikv/pd#4813 Signed-off-by: Ryan Leung Co-authored-by: Ti Chi Robot --- pkg/assertutil/assertutil.go | 12 +- pkg/assertutil/assertutil_test.go | 5 +- server/api/version_test.go | 5 +- server/join/join_test.go | 26 +-- server/schedule/labeler/labeler_test.go | 212 +++++++++--------- server/schedule/labeler/rule_test.go | 36 ++- server/server_test.go | 5 +- tests/client/client_test.go | 5 +- tests/pdctl/helper.go | 5 +- .../global_config/global_config_test.go | 4 +- tests/server/member/member_test.go | 5 +- 11 files changed, 163 insertions(+), 157 deletions(-) diff --git a/pkg/assertutil/assertutil.go b/pkg/assertutil/assertutil.go index d750b63a7aa7..5da161556746 100644 --- a/pkg/assertutil/assertutil.go +++ b/pkg/assertutil/assertutil.go @@ -22,20 +22,14 @@ type Checker struct { } // NewChecker creates Checker with FailNow function. -func NewChecker(failNow func()) *Checker { - return &Checker{ - FailNow: failNow, - } -} - -func (c *Checker) failNow() { - c.FailNow() +func NewChecker() *Checker { + return &Checker{} } // AssertNil calls the injected IsNil assertion. func (c *Checker) AssertNil(obtained interface{}) { if c.IsNil == nil { - c.failNow() + c.FailNow() return } c.IsNil(obtained) diff --git a/pkg/assertutil/assertutil_test.go b/pkg/assertutil/assertutil_test.go index 8da2ad2b1643..dcdbbd4252db 100644 --- a/pkg/assertutil/assertutil_test.go +++ b/pkg/assertutil/assertutil_test.go @@ -25,9 +25,10 @@ func TestNilFail(t *testing.T) { t.Parallel() re := require.New(t) var failErr error - checker := NewChecker(func() { + checker := NewChecker() + checker.FailNow = func() { failErr = errors.New("called assert func not exist") - }) + } re.Nil(checker.IsNil) checker.AssertNil(nil) re.NotNil(failErr) diff --git a/server/api/version_test.go b/server/api/version_test.go index e28dbf08df9e..7cbbab688cf0 100644 --- a/server/api/version_test.go +++ b/server/api/version_test.go @@ -33,7 +33,10 @@ import ( var _ = Suite(&testVersionSuite{}) func checkerWithNilAssert(c *C) *assertutil.Checker { - checker := assertutil.NewChecker(c.FailNow) + checker := assertutil.NewChecker() + checker.FailNow = func() { + c.FailNow() + } checker.IsNil = func(obtained interface{}) { c.Assert(obtained, IsNil) } diff --git a/server/join/join_test.go b/server/join/join_test.go index 4ef4eb65c17c..b8f001b5398e 100644 --- a/server/join/join_test.go +++ b/server/join/join_test.go @@ -17,32 +17,28 @@ package join import ( "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/assertutil" "github.com/tikv/pd/pkg/testutil" "github.com/tikv/pd/server" ) -func TestJoin(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testJoinServerSuite{}) - -type testJoinServerSuite struct{} - -func checkerWithNilAssert(c *C) *assertutil.Checker { - checker := assertutil.NewChecker(c.FailNow) +func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker { + checker := assertutil.NewChecker() + checker.FailNow = func() { + re.FailNow("") + } checker.IsNil = func(obtained interface{}) { - c.Assert(obtained, IsNil) + re.Nil(obtained) } return checker } // A PD joins itself. -func (s *testJoinServerSuite) TestPDJoinsItself(c *C) { - cfg := server.NewTestSingleConfig(checkerWithNilAssert(c)) +func TestPDJoinsItself(t *testing.T) { + re := require.New(t) + cfg := server.NewTestSingleConfig(checkerWithNilAssert(re)) defer testutil.CleanServer(cfg.DataDir) cfg.Join = cfg.AdvertiseClientUrls - c.Assert(PrepareJoinCluster(cfg), NotNil) + re.Error(PrepareJoinCluster(cfg)) } diff --git a/server/schedule/labeler/labeler_test.go b/server/schedule/labeler/labeler_test.go index 375e87851ec7..e848ace1b127 100644 --- a/server/schedule/labeler/labeler_test.go +++ b/server/schedule/labeler/labeler_test.go @@ -23,32 +23,14 @@ import ( "testing" "time" - . "github.com/pingcap/check" "github.com/pingcap/failpoint" + "github.com/stretchr/testify/require" "github.com/tikv/pd/server/core" "github.com/tikv/pd/server/storage" - "github.com/tikv/pd/server/storage/endpoint" ) -func TestT(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testLabelerSuite{}) - -type testLabelerSuite struct { - store endpoint.RuleStorage - labeler *RegionLabeler -} - -func (s *testLabelerSuite) SetUpTest(c *C) { - s.store = storage.NewStorageWithMemoryBackend() - var err error - s.labeler, err = NewRegionLabeler(context.Background(), s.store, time.Millisecond*10) - c.Assert(err, IsNil) -} - -func (s *testLabelerSuite) TestAdjustRule(c *C) { +func TestAdjustRule(t *testing.T) { + re := require.New(t) rule := LabelRule{ ID: "foo", Labels: []RegionLabel{ @@ -58,21 +40,22 @@ func (s *testLabelerSuite) TestAdjustRule(c *C) { Data: makeKeyRanges("12abcd", "34cdef", "56abcd", "78cdef"), } err := rule.checkAndAdjust() - c.Assert(err, IsNil) - c.Assert(rule.Data.([]*KeyRangeRule), HasLen, 2) - c.Assert(rule.Data.([]*KeyRangeRule)[0].StartKey, BytesEquals, []byte{0x12, 0xab, 0xcd}) - c.Assert(rule.Data.([]*KeyRangeRule)[0].EndKey, BytesEquals, []byte{0x34, 0xcd, 0xef}) - c.Assert(rule.Data.([]*KeyRangeRule)[1].StartKey, BytesEquals, []byte{0x56, 0xab, 0xcd}) - c.Assert(rule.Data.([]*KeyRangeRule)[1].EndKey, BytesEquals, []byte{0x78, 0xcd, 0xef}) + re.NoError(err) + re.Len(rule.Data.([]*KeyRangeRule), 2) + re.Equal([]byte{0x12, 0xab, 0xcd}, rule.Data.([]*KeyRangeRule)[0].StartKey) + re.Equal([]byte{0x34, 0xcd, 0xef}, rule.Data.([]*KeyRangeRule)[0].EndKey) + re.Equal([]byte{0x56, 0xab, 0xcd}, rule.Data.([]*KeyRangeRule)[1].StartKey) + re.Equal([]byte{0x78, 0xcd, 0xef}, rule.Data.([]*KeyRangeRule)[1].EndKey) } -func (s *testLabelerSuite) TestAdjustRule2(c *C) { +func TestAdjustRule2(t *testing.T) { + re := require.New(t) ruleData := `{"id":"id", "labels": [{"key": "k1", "value": "v1"}], "rule_type":"key-range", "data": [{"start_key":"", "end_key":""}]}` var rule LabelRule err := json.Unmarshal([]byte(ruleData), &rule) - c.Assert(err, IsNil) + re.NoError(err) err = rule.checkAndAdjust() - c.Assert(err, IsNil) + re.NoError(err) badRuleData := []string{ // no id @@ -95,40 +78,44 @@ func (s *testLabelerSuite) TestAdjustRule2(c *C) { `{"id":"id", "labels": [{"key": "k1", "value": "v1"}], "rule_type":"key-range", "data": [{"start_key":"abcd", "end_key":"123"}]}`, `{"id":"id", "labels": [{"key": "k1", "value": "v1"}], "rule_type":"key-range", "data": [{"start_key":"abcd", "end_key":"1234"}]}`, } - for i, str := range badRuleData { + for _, str := range badRuleData { var rule LabelRule err := json.Unmarshal([]byte(str), &rule) - c.Assert(err, IsNil, Commentf("#%d", i)) + re.NoError(err) err = rule.checkAndAdjust() - c.Assert(err, NotNil, Commentf("#%d", i)) + re.Error(err) } } -func (s *testLabelerSuite) TestGetSetRule(c *C) { +func TestGetSetRule(t *testing.T) { + re := require.New(t) + store := storage.NewStorageWithMemoryBackend() + labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10) + re.NoError(err) rules := []*LabelRule{ {ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: makeKeyRanges("1234", "5678")}, {ID: "rule2", Labels: []RegionLabel{{Key: "k2", Value: "v2"}}, RuleType: "key-range", Data: makeKeyRanges("ab12", "cd12")}, {ID: "rule3", Labels: []RegionLabel{{Key: "k3", Value: "v3"}}, RuleType: "key-range", Data: makeKeyRanges("abcd", "efef")}, } for _, r := range rules { - err := s.labeler.SetLabelRule(r) - c.Assert(err, IsNil) + err := labeler.SetLabelRule(r) + re.NoError(err) } - allRules := s.labeler.GetAllLabelRules() + allRules := labeler.GetAllLabelRules() sort.Slice(allRules, func(i, j int) bool { return allRules[i].ID < allRules[j].ID }) - c.Assert(allRules, DeepEquals, rules) + re.Equal(rules, allRules) - byIDs, err := s.labeler.GetLabelRules([]string{"rule3", "rule1"}) - c.Assert(err, IsNil) - c.Assert(byIDs, DeepEquals, []*LabelRule{rules[2], rules[0]}) + byIDs, err := labeler.GetLabelRules([]string{"rule3", "rule1"}) + re.NoError(err) + re.Equal([]*LabelRule{rules[2], rules[0]}, byIDs) - err = s.labeler.DeleteLabelRule("rule2") - c.Assert(err, IsNil) - c.Assert(s.labeler.GetLabelRule("rule2"), IsNil) - byIDs, err = s.labeler.GetLabelRules([]string{"rule1", "rule2"}) - c.Assert(err, IsNil) - c.Assert(byIDs, DeepEquals, []*LabelRule{rules[0]}) + err = labeler.DeleteLabelRule("rule2") + re.NoError(err) + re.Nil(labeler.GetLabelRule("rule2")) + byIDs, err = labeler.GetLabelRules([]string{"rule1", "rule2"}) + re.NoError(err) + re.Equal([]*LabelRule{rules[0]}, byIDs) // patch patch := LabelRulePatch{ @@ -137,16 +124,20 @@ func (s *testLabelerSuite) TestGetSetRule(c *C) { }, DeleteRules: []string{"rule1"}, } - err = s.labeler.Patch(patch) - c.Assert(err, IsNil) - allRules = s.labeler.GetAllLabelRules() + err = labeler.Patch(patch) + re.NoError(err) + allRules = labeler.GetAllLabelRules() sort.Slice(allRules, func(i, j int) bool { return allRules[i].ID < allRules[j].ID }) for id, rule := range allRules { - expectSameRules(c, rule, rules[id+1]) + expectSameRules(re, rule, rules[id+1]) } } -func (s *testLabelerSuite) TestIndex(c *C) { +func TestIndex(t *testing.T) { + re := require.New(t) + store := storage.NewStorageWithMemoryBackend() + labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10) + re.NoError(err) rules := []*LabelRule{ {ID: "rule0", Labels: []RegionLabel{{Key: "k1", Value: "v0"}}, RuleType: "key-range", Data: makeKeyRanges("", "")}, {ID: "rule1", Index: 1, Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: makeKeyRanges("1234", "5678")}, @@ -154,8 +145,8 @@ func (s *testLabelerSuite) TestIndex(c *C) { {ID: "rule3", Index: 1, Labels: []RegionLabel{{Key: "k2", Value: "v3"}}, RuleType: "key-range", Data: makeKeyRanges("abcd", "efef")}, } for _, r := range rules { - err := s.labeler.SetLabelRule(r) - c.Assert(err, IsNil) + err := labeler.SetLabelRule(r) + re.NoError(err) } type testCase struct { @@ -173,67 +164,75 @@ func (s *testLabelerSuite) TestIndex(c *C) { start, _ := hex.DecodeString(tc.start) end, _ := hex.DecodeString(tc.end) region := core.NewTestRegionInfo(start, end) - labels := s.labeler.GetRegionLabels(region) - c.Assert(labels, HasLen, len(tc.labels)) + labels := labeler.GetRegionLabels(region) + re.Len(labels, len(tc.labels)) for _, l := range labels { - c.Assert(l.Value, Equals, tc.labels[l.Key]) + re.Equal(tc.labels[l.Key], l.Value) } for _, k := range []string{"k1", "k2"} { - c.Assert(s.labeler.GetRegionLabel(region, k), Equals, tc.labels[k]) + re.Equal(tc.labels[k], labeler.GetRegionLabel(region, k)) } } } -func (s *testLabelerSuite) TestSaveLoadRule(c *C) { +func TestSaveLoadRule(t *testing.T) { + re := require.New(t) + store := storage.NewStorageWithMemoryBackend() + labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10) + re.NoError(err) rules := []*LabelRule{ {ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: makeKeyRanges("1234", "5678")}, {ID: "rule2", Labels: []RegionLabel{{Key: "k2", Value: "v2"}}, RuleType: "key-range", Data: makeKeyRanges("ab12", "cd12")}, {ID: "rule3", Labels: []RegionLabel{{Key: "k3", Value: "v3"}}, RuleType: "key-range", Data: makeKeyRanges("abcd", "efef")}, } for _, r := range rules { - err := s.labeler.SetLabelRule(r) - c.Assert(err, IsNil) + err := labeler.SetLabelRule(r) + re.NoError(err) } - labeler, err := NewRegionLabeler(context.Background(), s.store, time.Millisecond*100) - c.Assert(err, IsNil) + labeler, err = NewRegionLabeler(context.Background(), store, time.Millisecond*100) + re.NoError(err) for _, r := range rules { r2 := labeler.GetLabelRule(r.ID) - expectSameRules(c, r2, r) + expectSameRules(re, r2, r) } } -func expectSameRegionLabels(c *C, r1, r2 *RegionLabel) { +func expectSameRegionLabels(re *require.Assertions, r1, r2 *RegionLabel) { r1.checkAndAdjustExpire() r2.checkAndAdjustExpire() if len(r1.TTL) == 0 { - c.Assert(r2, DeepEquals, r1) + re.Equal(r1, r2) } r2.StartAt = r1.StartAt r2.checkAndAdjustExpire() - c.Assert(r2, DeepEquals, r1) + re.Equal(r1, r2) } -func expectSameRules(c *C, r1, r2 *LabelRule) { - c.Assert(r1.Labels, HasLen, len(r2.Labels)) +func expectSameRules(re *require.Assertions, r1, r2 *LabelRule) { + re.Len(r1.Labels, len(r2.Labels)) for id := 0; id < len(r1.Labels); id++ { - expectSameRegionLabels(c, &r1.Labels[id], &r2.Labels[id]) + expectSameRegionLabels(re, &r1.Labels[id], &r2.Labels[id]) } - c.Assert(r2, DeepEquals, r1) + re.Equal(r1, r2) } -func (s *testLabelerSuite) TestKeyRange(c *C) { +func TestKeyRange(t *testing.T) { + re := require.New(t) + store := storage.NewStorageWithMemoryBackend() + labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10) + re.NoError(err) rules := []*LabelRule{ {ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: makeKeyRanges("1234", "5678")}, {ID: "rule2", Labels: []RegionLabel{{Key: "k2", Value: "v2"}}, RuleType: "key-range", Data: makeKeyRanges("ab12", "cd12")}, {ID: "rule3", Labels: []RegionLabel{{Key: "k3", Value: "v3"}}, RuleType: "key-range", Data: makeKeyRanges("abcd", "efef")}, } for _, r := range rules { - err := s.labeler.SetLabelRule(r) - c.Assert(err, IsNil) + err := labeler.SetLabelRule(r) + re.NoError(err) } type testCase struct { @@ -251,18 +250,22 @@ func (s *testLabelerSuite) TestKeyRange(c *C) { start, _ := hex.DecodeString(tc.start) end, _ := hex.DecodeString(tc.end) region := core.NewTestRegionInfo(start, end) - labels := s.labeler.GetRegionLabels(region) - c.Assert(labels, HasLen, len(tc.labels)) + labels := labeler.GetRegionLabels(region) + re.Len(labels, len(tc.labels)) for _, l := range labels { - c.Assert(tc.labels[l.Key], Equals, l.Value) + re.Equal(l.Value, tc.labels[l.Key]) } for _, k := range []string{"k1", "k2", "k3"} { - c.Assert(s.labeler.GetRegionLabel(region, k), Equals, tc.labels[k]) + re.Equal(tc.labels[k], labeler.GetRegionLabel(region, k)) } } } -func (s *testLabelerSuite) TestLabelerRuleTTL(c *C) { +func TestLabelerRuleTTL(t *testing.T) { + re := require.New(t) + store := storage.NewStorageWithMemoryBackend() + labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10) + re.NoError(err) rules := []*LabelRule{ { ID: "rule1", @@ -291,56 +294,57 @@ func (s *testLabelerSuite) TestLabelerRuleTTL(c *C) { end, _ := hex.DecodeString("5678") region := core.NewTestRegionInfo(start, end) // the region has no lable rule at the beginning. - c.Assert(s.labeler.GetRegionLabels(region), HasLen, 0) + re.Empty(labeler.GetRegionLabels(region)) // set rules for the region. for _, r := range rules { - err := s.labeler.SetLabelRule(r) - c.Assert(err, IsNil) + err := labeler.SetLabelRule(r) + re.NoError(err) } // get rule with "rule2". - c.Assert(s.labeler.GetLabelRule("rule2"), NotNil) - c.Assert(failpoint.Enable("github.com/tikv/pd/server/schedule/labeler/regionLabelExpireSub1Minute", "return(true)"), IsNil) + re.NotNil(labeler.GetLabelRule("rule2")) + re.NoError(failpoint.Enable("github.com/tikv/pd/server/schedule/labeler/regionLabelExpireSub1Minute", "return(true)")) // rule2 should expire and only 2 labels left. - labels := s.labeler.GetRegionLabels(region) - c.Assert(labels, HasLen, 2) + labels := labeler.GetRegionLabels(region) + re.Len(labels, 2) - c.Assert(failpoint.Disable("github.com/tikv/pd/server/schedule/labeler/regionLabelExpireSub1Minute"), IsNil) + re.NoError(failpoint.Disable("github.com/tikv/pd/server/schedule/labeler/regionLabelExpireSub1Minute")) // rule2 should be exist since `GetRegionLabels` won't clear it physically. - s.checkRuleInMemoryAndStoage(c, "rule2", true) - c.Assert(s.labeler.GetLabelRule("rule2"), IsNil) + checkRuleInMemoryAndStoage(re, labeler, "rule2", true) + re.Nil(labeler.GetLabelRule("rule2")) // rule2 should be physically clear. - s.checkRuleInMemoryAndStoage(c, "rule2", false) + checkRuleInMemoryAndStoage(re, labeler, "rule2", false) - c.Assert(s.labeler.GetRegionLabel(region, "k2"), Equals, "") + re.Equal("", labeler.GetRegionLabel(region, "k2")) - c.Assert(s.labeler.GetLabelRule("rule3"), NotNil) - c.Assert(s.labeler.GetLabelRule("rule1"), NotNil) + re.NotNil(labeler.GetLabelRule("rule3")) + re.NotNil(labeler.GetLabelRule("rule1")) } -func (s *testLabelerSuite) checkRuleInMemoryAndStoage(c *C, ruleID string, exist bool) { - c.Assert(s.labeler.labelRules[ruleID] != nil, Equals, exist) +func checkRuleInMemoryAndStoage(re *require.Assertions, labeler *RegionLabeler, ruleID string, exist bool) { + re.Equal(exist, labeler.labelRules[ruleID] != nil) existInStorage := false - s.labeler.storage.LoadRegionRules(func(k, v string) { + labeler.storage.LoadRegionRules(func(k, v string) { if k == ruleID { existInStorage = true } }) - c.Assert(existInStorage, Equals, exist) + re.Equal(exist, existInStorage) } -func (s *testLabelerSuite) TestGC(c *C) { +func TestGC(t *testing.T) { + re := require.New(t) // set gcInterval to 1 hour. store := storage.NewStorageWithMemoryBackend() labeler, err := NewRegionLabeler(context.Background(), store, time.Hour) - c.Assert(err, IsNil) + re.NoError(err) ttls := []string{"1ms", "1ms", "1ms", "5ms", "5ms", "10ms", "1h", "24h"} start, _ := hex.DecodeString("1234") end, _ := hex.DecodeString("5678") region := core.NewTestRegionInfo(start, end) // the region has no lable rule at the beginning. - c.Assert(labeler.GetRegionLabels(region), HasLen, 0) + re.Empty(labeler.GetRegionLabels(region)) labels := []RegionLabel{} for id, ttl := range ttls { @@ -351,10 +355,10 @@ func (s *testLabelerSuite) TestGC(c *C) { RuleType: "key-range", Data: makeKeyRanges("1234", "5678")} err := labeler.SetLabelRule(rule) - c.Assert(err, IsNil) + re.NoError(err) } - c.Assert(labeler.labelRules, HasLen, len(ttls)) + re.Len(labeler.labelRules, len(ttls)) // check all rules unitl some rule expired. for { @@ -366,14 +370,14 @@ func (s *testLabelerSuite) TestGC(c *C) { } // no rule was cleared because the gc interval is big. - c.Assert(labeler.labelRules, HasLen, len(ttls)) + re.Len(labeler.labelRules, len(ttls)) labeler.checkAndClearExpiredLabels() labeler.RLock() currentRuleLen := len(labeler.labelRules) labeler.RUnlock() - c.Assert(currentRuleLen <= 5, IsTrue) + re.LessOrEqual(currentRuleLen, 5) } func makeKeyRanges(keys ...string) []interface{} { diff --git a/server/schedule/labeler/rule_test.go b/server/schedule/labeler/rule_test.go index 5b7b3b1f76ec..0b3417540072 100644 --- a/server/schedule/labeler/rule_test.go +++ b/server/schedule/labeler/rule_test.go @@ -17,46 +17,44 @@ package labeler import ( "encoding/json" "math" + "testing" "time" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -var _ = Suite(&testRuleSuite{}) - -type testRuleSuite struct{} - -func (s *testLabelerSuite) TestRegionLabelTTL(c *C) { +func TestRegionLabelTTL(t *testing.T) { + re := require.New(t) label := RegionLabel{Key: "k1", Value: "v1"} // test label with no ttl. err := label.checkAndAdjustExpire() - c.Assert(err, IsNil) - c.Assert(label.StartAt, HasLen, 0) - c.Assert(label.expire, IsNil) + re.NoError(err) + re.Empty(label.StartAt) + re.Empty(label.expire) // test rule with illegal ttl. label.TTL = "ttl" err = label.checkAndAdjustExpire() - c.Assert(err, NotNil) + re.Error(err) // test legal rule with ttl label.TTL = "10h10m10s10ms" err = label.checkAndAdjustExpire() - c.Assert(err, IsNil) - c.Assert(len(label.StartAt) > 0, IsTrue) - c.Assert(label.expireBefore(time.Now().Add(time.Hour)), IsFalse) - c.Assert(label.expireBefore(time.Now().Add(24*time.Hour)), IsTrue) + re.NoError(err) + re.Greater(len(label.StartAt), 0) + re.False(label.expireBefore(time.Now().Add(time.Hour))) + re.True(label.expireBefore(time.Now().Add(24 * time.Hour))) // test legal rule with ttl, rule unmarshal from json. data, err := json.Marshal(label) - c.Assert(err, IsNil) + re.NoError(err) var label2 RegionLabel err = json.Unmarshal(data, &label2) - c.Assert(err, IsNil) - c.Assert(label2.StartAt, Equals, label.StartAt) - c.Assert(label2.TTL, Equals, label.TTL) + re.NoError(err) + re.Equal(label.StartAt, label2.StartAt) + re.Equal(label.TTL, label2.TTL) label2.checkAndAdjustExpire() // The `expire` should be the same with minor inaccuracies. - c.Assert(math.Abs(label2.expire.Sub(*label.expire).Seconds()) < 1, IsTrue) + re.True(math.Abs(label2.expire.Sub(*label.expire).Seconds()) < 1) } diff --git a/server/server_test.go b/server/server_test.go index c6c14fe011f6..2a31cfb3b1c5 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -55,7 +55,10 @@ func mustWaitLeader(c *C, svrs []*Server) *Server { } func checkerWithNilAssert(c *C) *assertutil.Checker { - checker := assertutil.NewChecker(c.FailNow) + checker := assertutil.NewChecker() + checker.FailNow = func() { + c.FailNow() + } checker.IsNil = func(obtained interface{}) { c.Assert(obtained, IsNil) } diff --git a/tests/client/client_test.go b/tests/client/client_test.go index 86824261d12b..a9022d04a20f 100644 --- a/tests/client/client_test.go +++ b/tests/client/client_test.go @@ -729,9 +729,10 @@ func (suite *clientTestSuite) TearDownSuite() { } func (suite *clientTestSuite) checkerWithNilAssert() *assertutil.Checker { - checker := assertutil.NewChecker(func() { + checker := assertutil.NewChecker() + checker.FailNow = func() { suite.FailNow("should be nil") - }) + } checker.IsNil = func(obtained interface{}) { suite.Nil(obtained) } diff --git a/tests/pdctl/helper.go b/tests/pdctl/helper.go index 775f0b40f15e..bbfa351cc32c 100644 --- a/tests/pdctl/helper.go +++ b/tests/pdctl/helper.go @@ -124,9 +124,10 @@ func MustPutRegion(re *require.Assertions, cluster *tests.TestCluster, regionID, } func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker { - checker := assertutil.NewChecker(func() { + checker := assertutil.NewChecker() + checker.FailNow = func() { re.FailNow("should be nil") - }) + } checker.IsNil = func(obtained interface{}) { re.Nil(obtained) } diff --git a/tests/server/global_config/global_config_test.go b/tests/server/global_config/global_config_test.go index 87dc62e35a23..c2b12353eea1 100644 --- a/tests/server/global_config/global_config_test.go +++ b/tests/server/global_config/global_config_test.go @@ -65,7 +65,9 @@ func (s TestReceiver) Send(m *pdpb.WatchGlobalConfigResponse) error { func (s *GlobalConfigTestSuite) SetUpSuite(c *C) { var err error var gsi *server.Server - gsi, s.cleanup, err = server.NewTestServer(assertutil.NewChecker(func() {})) + checker := assertutil.NewChecker() + checker.FailNow = func() {} + gsi, s.cleanup, err = server.NewTestServer(checker) s.server = &server.GrpcServer{Server: gsi} c.Assert(err, IsNil) addr := s.server.GetAddr() diff --git a/tests/server/member/member_test.go b/tests/server/member/member_test.go index 0215d95e5fff..9c856b739a13 100644 --- a/tests/server/member/member_test.go +++ b/tests/server/member/member_test.go @@ -47,7 +47,10 @@ func TestMain(m *testing.M) { } func checkerWithNilAssert(c *C) *assertutil.Checker { - checker := assertutil.NewChecker(c.FailNow) + checker := assertutil.NewChecker() + checker.FailNow = func() { + c.FailNow() + } checker.IsNil = func(obtained interface{}) { c.Assert(obtained, IsNil) }