Skip to content

Commit

Permalink
ddl: migrate test-infra to testify for ddl/table_test.go (#30267)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Dec 6, 2021
1 parent d3ba0c2 commit 0b3ec09
Show file tree
Hide file tree
Showing 6 changed files with 542 additions and 311 deletions.
45 changes: 45 additions & 0 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/tikv"
)

Expand Down Expand Up @@ -129,6 +130,17 @@ func getSchemaVer(c *C, ctx sessionctx.Context) int64 {
return ver
}

func getSchemaVerT(t *testing.T, ctx sessionctx.Context) int64 {
err := ctx.NewTxn(context.Background())
require.NoError(t, err)
txn, err := ctx.Txn(true)
require.NoError(t, err)
m := meta.NewMeta(txn)
ver, err := m.GetSchemaVersion()
require.NoError(t, err)
return ver
}

type historyJobArgs struct {
ver int64
db *model.DBInfo
Expand All @@ -146,6 +158,16 @@ func checkEqualTable(c *C, t1, t2 *model.TableInfo) {
c.Assert(t1.AutoIncID, DeepEquals, t2.AutoIncID)
}

func checkEqualTableT(t *testing.T, t1, t2 *model.TableInfo) {
require.Equal(t, t1.ID, t2.ID)
require.Equal(t, t1.Name, t2.Name)
require.Equal(t, t1.Charset, t2.Charset)
require.Equal(t, t1.Collate, t2.Collate)
require.EqualValues(t, t1.PKIsHandle, t2.PKIsHandle)
require.EqualValues(t, t1.Comment, t2.Comment)
require.EqualValues(t, t1.AutoIncID, t2.AutoIncID)
}

func checkHistoryJob(c *C, job *model.Job) {
c.Assert(job.State, Equals, model.JobStateSynced)
}
Expand Down Expand Up @@ -173,6 +195,29 @@ func checkHistoryJobArgs(c *C, ctx sessionctx.Context, id int64, args *historyJo
}
}

func checkHistoryJobArgsT(t *testing.T, ctx sessionctx.Context, id int64, args *historyJobArgs) {
txn, err := ctx.Txn(true)
require.NoError(t, err)
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(id)
require.NoError(t, err)
require.Greater(t, historyJob.BinlogInfo.FinishedTS, uint64(0))

if args.tbl != nil {
require.Equal(t, args.ver, historyJob.BinlogInfo.SchemaVersion)
checkEqualTableT(t, historyJob.BinlogInfo.TableInfo, args.tbl)
return
}

// for handling schema job
require.Equal(t, args.ver, historyJob.BinlogInfo.SchemaVersion)
require.EqualValues(t, args.db, historyJob.BinlogInfo.DBInfo)
// only for creating schema job
if args.db != nil && len(args.tblIDs) == 0 {
return
}
}

func buildCreateIdxJob(dbInfo *model.DBInfo, tblInfo *model.TableInfo, unique bool, indexName string, colName string) *model.Job {
return &model.Job{
SchemaID: dbInfo.ID,
Expand Down
55 changes: 55 additions & 0 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ddl
import (
"context"
"sync"
"testing"
"time"

. "github.com/pingcap/check"
Expand All @@ -36,6 +37,7 @@ import (
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/testutil"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testDDLSuite{})
Expand Down Expand Up @@ -513,6 +515,37 @@ func testCheckJobDone(c *C, d *ddl, job *model.Job, isAdd bool) {
c.Assert(err, IsNil)
}

func testCheckJobDoneT(t *testing.T, d *ddl, job *model.Job, isAdd bool) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
require.Equal(t, model.JobStateSynced, historyJob.State)
if isAdd {
require.Equal(t, model.StatePublic, historyJob.SchemaState)
} else {
require.Equal(t, model.StateNone, historyJob.SchemaState)
}

return nil
})
require.NoError(t, err)
}

func testCheckJobCancelledT(t *testing.T, d *ddl, job *model.Job, state *model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
tt := meta.NewMeta(txn)
historyJob, err := tt.GetHistoryDDLJob(job.ID)
require.NoError(t, err)
require.True(t, historyJob.IsCancelled() || historyJob.IsRollbackDone(), "history job %s", historyJob)
if state != nil {
require.Equal(t, *state, historyJob.SchemaState)
}
return nil
})
require.NoError(t, err)
}

func testCheckJobCancelled(c *C, d *ddl, job *model.Job, state *model.SchemaState) {
err := kv.RunInNewTxn(context.Background(), d.store, false, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
Expand Down Expand Up @@ -544,6 +577,23 @@ func doDDLJobErrWithSchemaState(ctx sessionctx.Context, d *ddl, c *C, schemaID,
return job
}

func doDDLJobErrWithSchemaStateT(ctx sessionctx.Context, d *ddl, t *testing.T, schemaID, tableID int64, tp model.ActionType,
args []interface{}, state *model.SchemaState) *model.Job {
job := &model.Job{
SchemaID: schemaID,
TableID: tableID,
Type: tp,
Args: args,
BinlogInfo: &model.HistoryInfo{},
}
err := d.doDDLJob(ctx, job)
// TODO: Add the detail error check.
require.Error(t, err, "err:%v", err)
testCheckJobCancelledT(t, d, job, state)

return job
}

func doDDLJobSuccess(ctx sessionctx.Context, d *ddl, c *C, schemaID, tableID int64, tp model.ActionType,
args []interface{}) {
job := &model.Job{
Expand All @@ -562,6 +612,11 @@ func doDDLJobErr(c *C, schemaID, tableID int64, tp model.ActionType, args []inte
return doDDLJobErrWithSchemaState(ctx, d, c, schemaID, tableID, tp, args, nil)
}

func doDDLJobErrT(t *testing.T, schemaID, tableID int64, tp model.ActionType, args []interface{},
ctx sessionctx.Context, d *ddl) *model.Job {
return doDDLJobErrWithSchemaStateT(ctx, d, t, schemaID, tableID, tp, args, nil)
}

func checkCancelState(txn kv.Transaction, job *model.Job, test *testCancelJob) error {
var checkErr error
addIndexFirstReorg := (test.act == model.ActionAddIndex || test.act == model.ActionAddPrimaryKey) &&
Expand Down
33 changes: 33 additions & 0 deletions ddl/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/util/mock"
Expand Down Expand Up @@ -201,6 +202,38 @@ LOOP:
}
}

var _ = Suite(&testTableSuite{})

type testTableSuite struct {
store kv.Storage
dbInfo *model.DBInfo

d *ddl
}

func (s *testTableSuite) SetUpSuite(c *C) {
s.store = testCreateStore(c, "test_table")
ddl, err := testNewDDLAndStart(
context.Background(),
WithStore(s.store),
WithLease(testLease),
)
c.Assert(err, IsNil)
s.d = ddl

s.dbInfo, err = testSchemaInfo(s.d, "test_table")
c.Assert(err, IsNil)
testCreateSchema(c, testNewContext(s.d), s.d, s.dbInfo)
}

func (s *testTableSuite) TearDownSuite(c *C) {
testDropSchema(c, testNewContext(s.d), s.d, s.dbInfo)
err := s.d.Stop()
c.Assert(err, IsNil)
err = s.store.Close()
c.Assert(err, IsNil)
}

func (s *testTableSuite) TestTableResume(c *C) {
d := s.d

Expand Down
27 changes: 27 additions & 0 deletions ddl/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ddl

import (
"context"
"testing"
"time"

. "github.com/pingcap/check"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testSchemaSuite{})
Expand Down Expand Up @@ -68,6 +70,23 @@ func testCreateSchema(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo
return job
}

func testCreateSchemaT(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo) *model.Job {
job := &model.Job{
SchemaID: dbInfo.ID,
Type: model.ActionCreateSchema,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo},
}
err := d.doDDLJob(ctx, job)
require.NoError(t, err)

v := getSchemaVerT(t, ctx)
dbInfo.State = model.StatePublic
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, db: dbInfo})
dbInfo.State = model.StateNone
return job
}

func buildDropSchemaJob(dbInfo *model.DBInfo) *model.Job {
return &model.Job{
SchemaID: dbInfo.ID,
Expand All @@ -84,6 +103,14 @@ func testDropSchema(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo)
return job, ver
}

func testDropSchemaT(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo) (*model.Job, int64) {
job := buildDropSchemaJob(dbInfo)
err := d.doDDLJob(ctx, job)
require.NoError(t, err)
ver := getSchemaVerT(t, ctx)
return job, ver
}

func isDDLJobDone(c *C, t *meta.Meta) bool {
job, err := t.GetDDLJobByIdx(0)
c.Assert(err, IsNil)
Expand Down
Loading

0 comments on commit 0b3ec09

Please sign in to comment.