Skip to content

Commit

Permalink
br/pkg/task: migrate test-infra to testify (#30605)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Dec 17, 2021
1 parent a6fc5c6 commit d0fc70e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
49 changes: 23 additions & 26 deletions br/pkg/task/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,53 @@ import (
"testing"
"time"

. "github.com/pingcap/check"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
backup "github.com/pingcap/kvproto/pkg/brpb"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testBackupSuite{})
func TestParseTSString(t *testing.T) {
t.Parallel()

func TestT(t *testing.T) {
TestingT(t)
}

type testBackupSuite struct{}

func (s *testBackupSuite) TestParseTSString(c *C) {
var (
ts uint64
err error
)

ts, err = parseTSString("")
c.Assert(err, IsNil)
c.Assert(int(ts), Equals, 0)
require.NoError(t, err)
require.Zero(t, ts)

ts, err = parseTSString("400036290571534337")
c.Assert(err, IsNil)
c.Assert(int(ts), Equals, 400036290571534337)
require.NoError(t, err)
require.Equal(t, uint64(400036290571534337), ts)

_, offset := time.Now().Local().Zone()
ts, err = parseTSString("2018-05-11 01:42:23")
c.Assert(err, IsNil)
c.Assert(int(ts), Equals, 400032515489792000-(offset*1000)<<18)
require.NoError(t, err)
require.Equal(t, uint64(400032515489792000-(offset*1000)<<18), ts)
}

func (s *testBackupSuite) TestParseCompressionType(c *C) {
func TestParseCompressionType(t *testing.T) {
t.Parallel()

var (
ct backuppb.CompressionType
ct backup.CompressionType
err error
)
ct, err = parseCompressionType("lz4")
c.Assert(err, IsNil)
c.Assert(int(ct), Equals, 1)
require.NoError(t, err)
require.Equal(t, 1, int(ct))

ct, err = parseCompressionType("snappy")
c.Assert(err, IsNil)
c.Assert(int(ct), Equals, 2)
require.NoError(t, err)
require.Equal(t, 2, int(ct))

ct, err = parseCompressionType("zstd")
c.Assert(err, IsNil)
c.Assert(int(ct), Equals, 3)
require.NoError(t, err)
require.Equal(t, 3, int(ct))

ct, err = parseCompressionType("Other Compression (strings)")
c.Assert(err, ErrorMatches, "invalid compression.*")
c.Assert(int(ct), Equals, 0)
require.Error(t, err)
require.Regexp(t, "invalid compression.*", err.Error())
require.Zero(t, ct)
}
77 changes: 39 additions & 38 deletions br/pkg/task/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ package task
import (
"encoding/hex"
"fmt"
"testing"

. "github.com/pingcap/check"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
backup "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/kvproto/pkg/encryptionpb"
"github.com/pingcap/tidb/config"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testCommonSuite{})

type testCommonSuite struct{}

type fakeValue string

func (f fakeValue) String() string {
Expand All @@ -31,43 +28,48 @@ func (f fakeValue) Type() string {
panic("implement me")
}

func (*testCommonSuite) TestUrlNoQuery(c *C) {
func TestUrlNoQuery(t *testing.T) {
t.Parallel()
flag := &pflag.Flag{
Name: flagStorage,
Value: fakeValue("s3://some/what?secret=a123456789&key=987654321"),
}

field := flagToZapField(flag)
c.Assert(field.Key, Equals, flagStorage)
c.Assert(field.Interface.(fmt.Stringer).String(), Equals, "s3://some/what")
require.Equal(t, flagStorage, field.Key)
require.Equal(t, "s3://some/what", field.Interface.(fmt.Stringer).String())
}

func (s *testCommonSuite) TestTiDBConfigUnchanged(c *C) {
func TestTiDBConfigUnchanged(t *testing.T) {
t.Parallel()
cfg := config.GetGlobalConfig()
restoreConfig := enableTiDBConfig()
c.Assert(cfg, Not(DeepEquals), config.GetGlobalConfig())
require.NotEqual(t, config.GetGlobalConfig(), cfg)
restoreConfig()
c.Assert(cfg, DeepEquals, config.GetGlobalConfig())
require.Equal(t, config.GetGlobalConfig(), cfg)
}

func (s *testCommonSuite) TestStripingPDURL(c *C) {
func TestStripingPDURL(t *testing.T) {
t.Parallel()
nor1, err := normalizePDURL("https://pd:5432", true)
c.Assert(err, IsNil)
c.Assert(nor1, Equals, "pd:5432")
require.NoError(t, err)
require.Equal(t, "pd:5432", nor1)
_, err = normalizePDURL("https://pd.pingcap.com", false)
c.Assert(err, ErrorMatches, ".*pd url starts with https while TLS disabled.*")
require.Error(t, err)
require.Regexp(t, ".*pd url starts with https while TLS disabled.*", err.Error())
_, err = normalizePDURL("http://127.0.0.1:2379", true)
c.Assert(err, ErrorMatches, ".*pd url starts with http while TLS enabled.*")
require.Error(t, err)
require.Regexp(t, ".*pd url starts with http while TLS enabled.*", err.Error())
nor, err := normalizePDURL("http://127.0.0.1", false)
c.Assert(nor, Equals, "127.0.0.1")
c.Assert(err, IsNil)
require.NoError(t, err)
require.Equal(t, "127.0.0.1", nor)
noChange, err := normalizePDURL("127.0.0.1:2379", false)
c.Assert(err, IsNil)
c.Assert(noChange, Equals, "127.0.0.1:2379")
require.NoError(t, err)
require.Equal(t, "127.0.0.1:2379", noChange)
}

func (s *testCommonSuite) TestCheckCipherKeyMatch(c *C) {
testCases := []struct {
func TestCheckCipherKeyMatch(t *testing.T) {
t.Parallel()
cases := []struct {
CipherType encryptionpb.EncryptionMethod
CipherKey string
ok bool
Expand Down Expand Up @@ -112,19 +114,18 @@ func (s *testCommonSuite) TestCheckCipherKeyMatch(c *C) {
},
}

for _, t := range testCases {
cipherKey, err := hex.DecodeString(t.CipherKey)
c.Assert(err, IsNil)

r := checkCipherKeyMatch(&backuppb.CipherInfo{
CipherType: t.CipherType,
for _, c := range cases {
cipherKey, err := hex.DecodeString(c.CipherKey)
require.NoError(t, err)
require.Equal(t, c.ok, checkCipherKeyMatch(&backup.CipherInfo{
CipherType: c.CipherType,
CipherKey: cipherKey,
})
c.Assert(r, Equals, t.ok)
}))
}
}

func (s *testCommonSuite) TestCheckCipherKey(c *C) {
func TestCheckCipherKey(t *testing.T) {
t.Parallel()
cases := []struct {
cipherKey string
keyFile string
Expand Down Expand Up @@ -152,12 +153,12 @@ func (s *testCommonSuite) TestCheckCipherKey(c *C) {
},
}

for _, t := range cases {
err := checkCipherKey(t.cipherKey, t.keyFile)
if t.ok {
c.Assert(err, IsNil)
for _, c := range cases {
err := checkCipherKey(c.cipherKey, c.keyFile)
if c.ok {
require.NoError(t, err)
} else {
c.Assert(err, NotNil)
require.Error(t, err)
}
}
}
18 changes: 9 additions & 9 deletions br/pkg/task/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
package task

import (
. "github.com/pingcap/check"
"testing"

"github.com/pingcap/tidb/br/pkg/restore"
"github.com/stretchr/testify/require"
)

type testRestoreSuite struct{}

var _ = Suite(&testRestoreSuite{})
func TestRestoreConfigAdjust(t *testing.T) {
t.Parallel()

func (s *testRestoreSuite) TestRestoreConfigAdjust(c *C) {
cfg := &RestoreConfig{}
cfg.adjustRestoreConfig()

c.Assert(cfg.Config.Concurrency, Equals, uint32(defaultRestoreConcurrency))
c.Assert(cfg.Config.SwitchModeInterval, Equals, defaultSwitchInterval)
c.Assert(cfg.MergeSmallRegionKeyCount, Equals, restore.DefaultMergeRegionKeyCount)
c.Assert(cfg.MergeSmallRegionSizeBytes, Equals, restore.DefaultMergeRegionSizeBytes)
require.Equal(t, uint32(defaultRestoreConcurrency), cfg.Config.Concurrency)
require.Equal(t, defaultSwitchInterval, cfg.Config.SwitchModeInterval)
require.Equal(t, restore.DefaultMergeRegionKeyCount, cfg.MergeSmallRegionKeyCount)
require.Equal(t, restore.DefaultMergeRegionSizeBytes, cfg.MergeSmallRegionSizeBytes)
}

0 comments on commit d0fc70e

Please sign in to comment.