Skip to content

Commit

Permalink
assertutil, etcdutil, grpcutil, keyutil, logutil, metricutil: testify…
Browse files Browse the repository at this point in the history
… the tests (#5067)

ref #4813

Testify the pkg/assertutil, pkg/etcdutil, pkg/grpcutil, pkg/keyutil, pkg/logutil, pkg/metricutil tests.

Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato authored May 30, 2022
1 parent 109719f commit 294a016
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 134 deletions.
16 changes: 4 additions & 12 deletions pkg/assertutil/assertutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
96 changes: 44 additions & 52 deletions pkg/etcdutil/etcdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()

Expand All @@ -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)
}
30 changes: 11 additions & 19 deletions pkg/grpcutil/grpcutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,48 @@ 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",
CAPath: "../../tests/client/cert/ca.pem",
}
// 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))
}
15 changes: 3 additions & 12 deletions pkg/keyutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit 294a016

Please sign in to comment.