Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: migrate test-infra to testify for ddl/table_test.go #30267

Merged
merged 6 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSchemaVerT copy from getSchemaVer. it will rename getSchemaVer after completing test-infra.

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