Skip to content

Commit

Permalink
util/admin: add unit test and move integration test. (#10013)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Apr 4, 2019
1 parent 8800ec1 commit f5fb5aa
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 41 deletions.
41 changes: 0 additions & 41 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,6 @@ func (s *testSuite) TestAdmin(c *C) {
// For "checksum_without_index", we only have one checksum, so the result will be 1.
res.Sort().Check(testkit.Rows("test checksum_with_index 0 2 2", "test checksum_without_index 1 1 1"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, index idx(a, b));")
tk.MustExec("insert into t values(1, 1, 1)")
tk.MustExec("admin check table t")

tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (c2 BOOL, PRIMARY KEY (c2));")
tk.MustExec("INSERT INTO t1 SET c2 = '0';")
Expand All @@ -279,42 +274,6 @@ func (s *testSuite) TestAdmin(c *C) {
tk.MustExec("ALTER TABLE t1 ADD COLUMN c4 bit(10) default 127;")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx3 (c4);")
tk.MustExec("admin check table t1;")

// For add index on virtual column
tk.MustExec("drop table if exists t1;")
tk.MustExec(`create table t1 (
a int as (JSON_EXTRACT(k,'$.a')),
c double as (JSON_EXTRACT(k,'$.c')),
d decimal(20,10) as (JSON_EXTRACT(k,'$.d')),
e char(10) as (JSON_EXTRACT(k,'$.e')),
f date as (JSON_EXTRACT(k,'$.f')),
g time as (JSON_EXTRACT(k,'$.g')),
h datetime as (JSON_EXTRACT(k,'$.h')),
i timestamp as (JSON_EXTRACT(k,'$.i')),
j year as (JSON_EXTRACT(k,'$.j')),
k json);`)

tk.MustExec("insert into t1 set k='{\"a\": 100,\"c\":1.234,\"d\":1.2340000000,\"e\":\"abcdefg\",\"f\":\"2018-09-28\",\"g\":\"12:59:59\",\"h\":\"2018-09-28 12:59:59\",\"i\":\"2018-09-28 16:40:33\",\"j\":\"2018\"}';")
tk.MustExec("alter table t1 add index idx_a(a);")
tk.MustExec("alter table t1 add index idx_c(c);")
tk.MustExec("alter table t1 add index idx_d(d);")
tk.MustExec("alter table t1 add index idx_e(e);")
tk.MustExec("alter table t1 add index idx_f(f);")
tk.MustExec("alter table t1 add index idx_g(g);")
tk.MustExec("alter table t1 add index idx_h(h);")
tk.MustExec("alter table t1 add index idx_j(j);")
tk.MustExec("alter table t1 add index idx_i(i);")
tk.MustExec("alter table t1 add index idx_m(a,c,d,e,f,g,h,i,j);")
tk.MustExec("admin check table t1;")

tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (c1 int);")
tk.MustExec("INSERT INTO t1 SET c1 = 1;")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc1 CHAR(36) NULL DEFAULT '';")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc2 VARCHAR(36) NULL DEFAULT ''")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx1 (cc1);")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx2 (cc2);")
tk.MustExec("admin check table t1;")
}

func (s *testSuite) fillData(tk *testkit.TestKit, table string) {
Expand Down
104 changes: 104 additions & 0 deletions util/admin/admin_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package admin_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/util/testkit"
)

var _ = Suite(&testAdminSuite{})

type testAdminSuite struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
domain *domain.Domain
}

func (s *testAdminSuite) SetUpSuite(c *C) {
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.MustNewMVCCStore()
store, err := mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
d.SetStatsUpdating(true)
s.domain = d
}

func (s *testAdminSuite) TearDownSuite(c *C) {
s.domain.Close()
s.store.Close()
}

func (s *testAdminSuite) TestAdminCheckTable(c *C) {
// test NULL value.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// test index column has pk-handle column
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, index idx(a, b));")
tk.MustExec("insert into t values(1, 1, 1)")
tk.MustExec("admin check table t")

// test for add index on the later added columns.
tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (c1 int);")
tk.MustExec("INSERT INTO t1 SET c1 = 1;")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc1 CHAR(36) NULL DEFAULT '';")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc2 VARCHAR(36) NULL DEFAULT ''")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx1 (cc1);")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx2 (cc2);")
tk.MustExec("admin check table t1;")

// For add index on virtual column
tk.MustExec("drop table if exists t1;")
tk.MustExec(`create table t1 (
a int as (JSON_EXTRACT(k,'$.a')),
c double as (JSON_EXTRACT(k,'$.c')),
d decimal(20,10) as (JSON_EXTRACT(k,'$.d')),
e char(10) as (JSON_EXTRACT(k,'$.e')),
f date as (JSON_EXTRACT(k,'$.f')),
g time as (JSON_EXTRACT(k,'$.g')),
h datetime as (JSON_EXTRACT(k,'$.h')),
i timestamp as (JSON_EXTRACT(k,'$.i')),
j year as (JSON_EXTRACT(k,'$.j')),
k json);`)

tk.MustExec("insert into t1 set k='{\"a\": 100,\"c\":1.234,\"d\":1.2340000000,\"e\":\"abcdefg\",\"f\":\"2018-09-28\",\"g\":\"12:59:59\",\"h\":\"2018-09-28 12:59:59\",\"i\":\"2018-09-28 16:40:33\",\"j\":\"2018\"}';")
tk.MustExec("alter table t1 add index idx_a(a);")
tk.MustExec("alter table t1 add index idx_c(c);")
tk.MustExec("alter table t1 add index idx_d(d);")
tk.MustExec("alter table t1 add index idx_e(e);")
tk.MustExec("alter table t1 add index idx_f(f);")
tk.MustExec("alter table t1 add index idx_g(g);")
tk.MustExec("alter table t1 add index idx_h(h);")
tk.MustExec("alter table t1 add index idx_j(j);")
tk.MustExec("alter table t1 add index idx_i(i);")
tk.MustExec("alter table t1 add index idx_m(a,c,d,e,f,g,h,i,j);")
tk.MustExec("admin check table t1;")
}
54 changes: 54 additions & 0 deletions util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,40 @@ func (s *testSuite) TestCancelJobs(c *C) {
c.Assert(err, IsNil)
}

errs, err = CancelJobs(txn, []int64{})
c.Assert(err, IsNil)
c.Assert(errs, IsNil)
errs, err = CancelJobs(txn, []int64{-1})
c.Assert(err, IsNil)
c.Assert(errs[0], NotNil)
c.Assert(errs[0].Error(), Equals, "[admin:4]DDL Job:-1 not found")

// test cancel finish job.
job := &model.Job{
ID: 100,
SchemaID: 1,
Type: model.ActionCreateTable,
State: model.JobStateDone,
}
err = t.EnQueueDDLJob(job)
c.Assert(err, IsNil)
errs, err = CancelJobs(txn, []int64{100})
c.Assert(err, IsNil)
c.Assert(errs[0], NotNil)
c.Assert(errs[0].Error(), Equals, "[admin:5]This job:100 is finished, so can't be cancelled")

// test can't cancelable job.
job.Type = model.ActionDropIndex
job.SchemaState = model.StateDeleteOnly
job.State = model.JobStateRunning
job.ID = 101
err = t.EnQueueDDLJob(job)
c.Assert(err, IsNil)
errs, err = CancelJobs(txn, []int64{101})
c.Assert(err, IsNil)
c.Assert(errs[0], NotNil)
c.Assert(errs[0].Error(), Equals, "[admin:6]This job:101 is almost finished, can't be cancelled now")

err = txn.Rollback()
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -538,3 +572,23 @@ func setColValue(c *C, txn kv.Transaction, key kv.Key, v types.Datum) {
err = txn.Set(key, value)
c.Assert(err, IsNil)
}

func (s *testSuite) TestIsJobRollbackable(c *C) {
cases := []struct {
tp model.ActionType
state model.SchemaState
result bool
}{
{model.ActionDropIndex, model.StateNone, true},
{model.ActionDropIndex, model.StateDeleteOnly, false},
{model.ActionDropSchema, model.StateDeleteOnly, false},
{model.ActionDropColumn, model.StateDeleteOnly, false},
}
job := &model.Job{}
for _, ca := range cases {
job.Type = ca.tp
job.SchemaState = ca.state
re := IsJobRollbackable(job)
c.Assert(re == ca.result, IsTrue)
}
}

0 comments on commit f5fb5aa

Please sign in to comment.