diff --git a/pkg/assertutil/assertutil_test.go b/pkg/assertutil/assertutil_test.go index 754af4509e2..6cdfd591937 100644 --- a/pkg/assertutil/assertutil_test.go +++ b/pkg/assertutil/assertutil_test.go @@ -18,23 +18,15 @@ import ( "errors" "testing" - "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -func Test(t *testing.T) { - check.TestingT(t) -} - -var _ = check.Suite(&testAssertUtilSuite{}) - -type testAssertUtilSuite struct{} - -func (s *testAssertUtilSuite) TestNilFail(c *check.C) { +func TestNilFail(t *testing.T) { var failErr error checker := NewChecker(func() { failErr = errors.New("called assert func not exist") }) - c.Assert(checker.IsNil, check.IsNil) + require.Nil(t, checker.IsNil) checker.AssertNil(nil) - c.Assert(failErr, check.NotNil) + require.NotNil(t, failErr) } diff --git a/pkg/etcdutil/etcdutil_test.go b/pkg/etcdutil/etcdutil_test.go index c3dc327949d..7bc73f12cbe 100644 --- a/pkg/etcdutil/etcdutil_test.go +++ b/pkg/etcdutil/etcdutil_test.go @@ -21,43 +21,35 @@ import ( "testing" "time" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" "go.etcd.io/etcd/clientv3" "go.etcd.io/etcd/embed" "go.etcd.io/etcd/pkg/types" ) -func Test(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testEtcdutilSuite{}) - -type testEtcdutilSuite struct{} - -func (s *testEtcdutilSuite) TestMemberHelpers(c *C) { +func TestMemberHelpers(t *testing.T) { cfg1 := NewTestSingleConfig() etcd1, err := embed.StartEtcd(cfg1) defer func() { etcd1.Close() CleanConfig(cfg1) }() - c.Assert(err, IsNil) + require.NoError(t, err) ep1 := cfg1.LCUrls[0].String() client1, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep1}, }) - c.Assert(err, IsNil) + require.NoError(t, err) <-etcd1.Server.ReadyNotify() // Test ListEtcdMembers listResp1, err := ListEtcdMembers(client1) - c.Assert(err, IsNil) - c.Assert(listResp1.Members, HasLen, 1) + require.NoError(t, err) + require.Len(t, listResp1.Members, 1) // types.ID is an alias of uint64. - c.Assert(listResp1.Members[0].ID, Equals, uint64(etcd1.Server.ID())) + require.Equal(t, uint64(etcd1.Server.ID()), listResp1.Members[0].ID) // Test AddEtcdMember // Make a new etcd config. @@ -69,67 +61,67 @@ func (s *testEtcdutilSuite) TestMemberHelpers(c *C) { // Add it to the cluster above. peerURL := cfg2.LPUrls[0].String() addResp, err := AddEtcdMember(client1, []string{peerURL}) - c.Assert(err, IsNil) + require.NoError(t, err) etcd2, err := embed.StartEtcd(cfg2) defer func() { etcd2.Close() CleanConfig(cfg2) }() - c.Assert(err, IsNil) - c.Assert(addResp.Member.ID, Equals, uint64(etcd2.Server.ID())) + require.NoError(t, err) + require.Equal(t, uint64(etcd2.Server.ID()), addResp.Member.ID) ep2 := cfg2.LCUrls[0].String() client2, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep2}, }) - c.Assert(err, IsNil) + require.NoError(t, err) <-etcd2.Server.ReadyNotify() - c.Assert(err, IsNil) + require.NoError(t, err) listResp2, err := ListEtcdMembers(client2) - c.Assert(err, IsNil) - c.Assert(listResp2.Members, HasLen, 2) + require.NoError(t, err) + require.Len(t, listResp2.Members, 2) for _, m := range listResp2.Members { switch m.ID { case uint64(etcd1.Server.ID()): case uint64(etcd2.Server.ID()): default: - c.Fatalf("unknown member: %v", m) + t.Fatalf("unknown member: %v", m) } } // Test CheckClusterID urlsMap, err := types.NewURLsMap(cfg2.InitialCluster) - c.Assert(err, IsNil) + require.NoError(t, err) err = CheckClusterID(etcd1.Server.Cluster().ID(), urlsMap, &tls.Config{MinVersion: tls.VersionTLS12}) - c.Assert(err, IsNil) + require.NoError(t, err) // Test RemoveEtcdMember _, err = RemoveEtcdMember(client1, uint64(etcd2.Server.ID())) - c.Assert(err, IsNil) + require.NoError(t, err) listResp3, err := ListEtcdMembers(client1) - c.Assert(err, IsNil) - c.Assert(listResp3.Members, HasLen, 1) - c.Assert(listResp3.Members[0].ID, Equals, uint64(etcd1.Server.ID())) + require.NoError(t, err) + require.Len(t, listResp3.Members, 1) + require.Equal(t, uint64(etcd1.Server.ID()), listResp3.Members[0].ID) } -func (s *testEtcdutilSuite) TestEtcdKVGet(c *C) { +func TestEtcdKVGet(t *testing.T) { cfg := NewTestSingleConfig() etcd, err := embed.StartEtcd(cfg) defer func() { etcd.Close() CleanConfig(cfg) }() - c.Assert(err, IsNil) + require.NoError(t, err) ep := cfg.LCUrls[0].String() client, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep}, }) - c.Assert(err, IsNil) + require.NoError(t, err) <-etcd.Server.ReadyNotify() @@ -139,69 +131,69 @@ func (s *testEtcdutilSuite) TestEtcdKVGet(c *C) { kv := clientv3.NewKV(client) for i := range keys { _, err = kv.Put(context.TODO(), keys[i], vals[i]) - c.Assert(err, IsNil) + require.NoError(t, err) } // Test simple point get resp, err := EtcdKVGet(client, "test/key1") - c.Assert(err, IsNil) - c.Assert(string(resp.Kvs[0].Value), Equals, "val1") + require.NoError(t, err) + require.Equal(t, "val1", string(resp.Kvs[0].Value)) // Test range get withRange := clientv3.WithRange("test/zzzz") withLimit := clientv3.WithLimit(3) resp, err = EtcdKVGet(client, "test/", withRange, withLimit, clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)) - c.Assert(err, IsNil) - c.Assert(resp.Kvs, HasLen, 3) + require.NoError(t, err) + require.Len(t, resp.Kvs, 3) for i := range resp.Kvs { - c.Assert(string(resp.Kvs[i].Key), Equals, keys[i]) - c.Assert(string(resp.Kvs[i].Value), Equals, vals[i]) + require.Equal(t, keys[i], string(resp.Kvs[i].Key)) + require.Equal(t, vals[i], string(resp.Kvs[i].Value)) } lastKey := string(resp.Kvs[len(resp.Kvs)-1].Key) next := clientv3.GetPrefixRangeEnd(lastKey) resp, err = EtcdKVGet(client, next, withRange, withLimit, clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)) - c.Assert(err, IsNil) - c.Assert(resp.Kvs, HasLen, 2) + require.NoError(t, err) + require.Len(t, resp.Kvs, 2) } -func (s *testEtcdutilSuite) TestEtcdKVPutWithTTL(c *C) { +func TestEtcdKVPutWithTTL(t *testing.T) { cfg := NewTestSingleConfig() etcd, err := embed.StartEtcd(cfg) defer func() { etcd.Close() CleanConfig(cfg) }() - c.Assert(err, IsNil) + require.NoError(t, err) ep := cfg.LCUrls[0].String() client, err := clientv3.New(clientv3.Config{ Endpoints: []string{ep}, }) - c.Assert(err, IsNil) + require.NoError(t, err) <-etcd.Server.ReadyNotify() _, err = EtcdKVPutWithTTL(context.TODO(), client, "test/ttl1", "val1", 2) - c.Assert(err, IsNil) + require.NoError(t, err) _, err = EtcdKVPutWithTTL(context.TODO(), client, "test/ttl2", "val2", 4) - c.Assert(err, IsNil) + require.NoError(t, err) time.Sleep(3 * time.Second) // test/ttl1 is outdated resp, err := EtcdKVGet(client, "test/ttl1") - c.Assert(err, IsNil) - c.Assert(resp.Count, Equals, int64(0)) + require.NoError(t, err) + require.Equal(t, int64(0), resp.Count) // but test/ttl2 is not resp, err = EtcdKVGet(client, "test/ttl2") - c.Assert(err, IsNil) - c.Assert(string(resp.Kvs[0].Value), Equals, "val2") + require.NoError(t, err) + require.Equal(t, "val2", string(resp.Kvs[0].Value)) time.Sleep(2 * time.Second) // test/ttl2 is also outdated resp, err = EtcdKVGet(client, "test/ttl2") - c.Assert(err, IsNil) - c.Assert(resp.Count, Equals, int64(0)) + require.NoError(t, err) + require.Equal(t, int64(0), resp.Count) } diff --git a/pkg/grpcutil/grpcutil_test.go b/pkg/grpcutil/grpcutil_test.go index 4d30bf6ed1d..d1b9d3a8830 100644 --- a/pkg/grpcutil/grpcutil_test.go +++ b/pkg/grpcutil/grpcutil_test.go @@ -4,31 +4,23 @@ import ( "os" "testing" - . "github.com/pingcap/check" "github.com/pingcap/errors" + "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/errs" ) -func Test(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&gRPCUtilSuite{}) - -type gRPCUtilSuite struct{} - -func loadTLSContent(c *C, caPath, certPath, keyPath string) (caData, certData, keyData []byte) { +func loadTLSContent(t *testing.T, caPath, certPath, keyPath string) (caData, certData, keyData []byte) { var err error caData, err = os.ReadFile(caPath) - c.Assert(err, IsNil) + require.NoError(t, err) certData, err = os.ReadFile(certPath) - c.Assert(err, IsNil) + require.NoError(t, err) keyData, err = os.ReadFile(keyPath) - c.Assert(err, IsNil) + require.NoError(t, err) return } -func (s *gRPCUtilSuite) TestToTLSConfig(c *C) { +func TestToTLSConfig(t *testing.T) { tlsConfig := TLSConfig{ KeyPath: "../../tests/client/cert/pd-server-key.pem", CertPath: "../../tests/client/cert/pd-server.pem", @@ -36,24 +28,24 @@ func (s *gRPCUtilSuite) TestToTLSConfig(c *C) { } // test without bytes _, err := tlsConfig.ToTLSConfig() - c.Assert(err, IsNil) + require.NoError(t, err) // test with bytes - caData, certData, keyData := loadTLSContent(c, tlsConfig.CAPath, tlsConfig.CertPath, tlsConfig.KeyPath) + caData, certData, keyData := loadTLSContent(t, tlsConfig.CAPath, tlsConfig.CertPath, tlsConfig.KeyPath) tlsConfig.SSLCABytes = caData tlsConfig.SSLCertBytes = certData tlsConfig.SSLKEYBytes = keyData _, err = tlsConfig.ToTLSConfig() - c.Assert(err, IsNil) + require.NoError(t, err) // test wrong cert bytes tlsConfig.SSLCertBytes = []byte("invalid cert") _, err = tlsConfig.ToTLSConfig() - c.Assert(errors.ErrorEqual(err, errs.ErrCryptoX509KeyPair), IsTrue) + require.True(t, errors.ErrorEqual(err, errs.ErrCryptoX509KeyPair)) // test wrong ca bytes tlsConfig.SSLCertBytes = certData tlsConfig.SSLCABytes = []byte("invalid ca") _, err = tlsConfig.ToTLSConfig() - c.Assert(errors.ErrorEqual(err, errs.ErrCryptoAppendCertsFromPEM), IsTrue) + require.True(t, errors.ErrorEqual(err, errs.ErrCryptoAppendCertsFromPEM)) } diff --git a/pkg/keyutil/util_test.go b/pkg/keyutil/util_test.go index 59708177f5b..6603c61b131 100644 --- a/pkg/keyutil/util_test.go +++ b/pkg/keyutil/util_test.go @@ -17,21 +17,12 @@ package keyutil import ( "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" ) -func Test(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testKeyUtilSuite{}) - -type testKeyUtilSuite struct { -} - -func (s *testKeyUtilSuite) TestKeyUtil(c *C) { +func TestKeyUtil(t *testing.T) { startKey := []byte("a") endKey := []byte("b") key := BuildKeyRangeKey(startKey, endKey) - c.Assert(key, Equals, "61-62") + require.Equal(t, "61-62", key) } diff --git a/pkg/logutil/log_test.go b/pkg/logutil/log_test.go index 33b0320b6f8..42a9126ea33 100644 --- a/pkg/logutil/log_test.go +++ b/pkg/logutil/log_test.go @@ -16,32 +16,25 @@ package logutil import ( "fmt" + "reflect" "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" "go.uber.org/zap/zapcore" ) -func Test(t *testing.T) { - TestingT(t) +func TestStringToZapLogLevel(t *testing.T) { + require.Equal(t, zapcore.FatalLevel, StringToZapLogLevel("fatal")) + require.Equal(t, zapcore.ErrorLevel, StringToZapLogLevel("ERROR")) + require.Equal(t, zapcore.WarnLevel, StringToZapLogLevel("warn")) + require.Equal(t, zapcore.WarnLevel, StringToZapLogLevel("warning")) + require.Equal(t, zapcore.DebugLevel, StringToZapLogLevel("debug")) + require.Equal(t, zapcore.InfoLevel, StringToZapLogLevel("info")) + require.Equal(t, zapcore.InfoLevel, StringToZapLogLevel("whatever")) } -var _ = Suite(&testLogSuite{}) - -type testLogSuite struct{} - -func (s *testLogSuite) TestStringToZapLogLevel(c *C) { - c.Assert(StringToZapLogLevel("fatal"), Equals, zapcore.FatalLevel) - c.Assert(StringToZapLogLevel("ERROR"), Equals, zapcore.ErrorLevel) - c.Assert(StringToZapLogLevel("warn"), Equals, zapcore.WarnLevel) - c.Assert(StringToZapLogLevel("warning"), Equals, zapcore.WarnLevel) - c.Assert(StringToZapLogLevel("debug"), Equals, zapcore.DebugLevel) - c.Assert(StringToZapLogLevel("info"), Equals, zapcore.InfoLevel) - c.Assert(StringToZapLogLevel("whatever"), Equals, zapcore.InfoLevel) -} - -func (s *testLogSuite) TestRedactLog(c *C) { - testcases := []struct { +func TestRedactLog(t *testing.T) { + testCases := []struct { name string arg interface{} enableRedactLog bool @@ -73,16 +66,16 @@ func (s *testLogSuite) TestRedactLog(c *C) { }, } - for _, testcase := range testcases { - c.Log(testcase.name) - SetRedactLog(testcase.enableRedactLog) - switch r := testcase.arg.(type) { + for _, testCase := range testCases { + t.Log(testCase.name) + SetRedactLog(testCase.enableRedactLog) + switch r := testCase.arg.(type) { case []byte: - c.Assert(RedactBytes(r), DeepEquals, testcase.expect) + require.True(t, reflect.DeepEqual(testCase.expect, RedactBytes(r))) case string: - c.Assert(RedactString(r), DeepEquals, testcase.expect) + require.True(t, reflect.DeepEqual(testCase.expect, RedactString(r))) case fmt.Stringer: - c.Assert(RedactStringer(r), DeepEquals, testcase.expect) + require.True(t, reflect.DeepEqual(testCase.expect, RedactStringer(r))) default: panic("unmatched case") } diff --git a/pkg/metricutil/metricutil_test.go b/pkg/metricutil/metricutil_test.go index 1fbfacf58cf..512732c7f7e 100644 --- a/pkg/metricutil/metricutil_test.go +++ b/pkg/metricutil/metricutil_test.go @@ -18,20 +18,11 @@ import ( "testing" "time" - . "github.com/pingcap/check" + "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/typeutil" ) -func Test(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testMetricsSuite{}) - -type testMetricsSuite struct { -} - -func (s *testMetricsSuite) TestCamelCaseToSnakeCase(c *C) { +func TestCamelCaseToSnakeCase(t *testing.T) { inputs := []struct { name string newName string @@ -59,11 +50,11 @@ func (s *testMetricsSuite) TestCamelCaseToSnakeCase(c *C) { } for _, input := range inputs { - c.Assert(camelCaseToSnakeCase(input.name), Equals, input.newName) + require.Equal(t, input.newName, camelCaseToSnakeCase(input.name)) } } -func (s *testMetricsSuite) TestCoverage(c *C) { +func TestCoverage(t *testing.T) { cfgs := []*MetricConfig{ { PushJob: "j1",