Skip to content

Commit

Permalink
executor, tests: move test cases from executor to integrationtest (PA…
Browse files Browse the repository at this point in the history
…RT 3) (#47796)

ref #47076
  • Loading branch information
Defined2014 committed Oct 21, 2023
1 parent 012869f commit 3f9ba2b
Show file tree
Hide file tree
Showing 24 changed files with 3,183 additions and 1,774 deletions.
20 changes: 0 additions & 20 deletions pkg/executor/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,12 @@ package executor_test

import (
"testing"
"time"

"github.com/pingcap/tidb/pkg/executor"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
)

func TestQueryTime(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

costTime := time.Since(tk.Session().GetSessionVars().StartTime)
require.Less(t, costTime, time.Second)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1), (1), (1), (1), (1)")
tk.MustExec("select * from t t1 join t t2 on t1.a = t2.a")

costTime = time.Since(tk.Session().GetSessionVars().StartTime)
require.Less(t, costTime, time.Second)
}

func TestFormatSQL(t *testing.T) {
val := executor.FormatSQL("aaaa")
require.Equal(t, "aaaa", val.String())
Expand Down
115 changes: 0 additions & 115 deletions pkg/executor/cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"net"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -308,120 +307,6 @@ func TestSQLDigestTextRetriever(t *testing.T) {
require.Equal(t, "", r.SQLDigestsMap[updateDigest.String()])
}

func TestFunctionDecodeSQLDigests(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
srv := createRPCServer(t, dom)
defer srv.Stop()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
tk.MustExec("set global tidb_enable_stmt_summary = 1")
tk.MustQuery("select @@global.tidb_enable_stmt_summary").Check(testkit.Rows("1"))
tk.MustExec("drop table if exists test_func_decode_sql_digests")
tk.MustExec("create table test_func_decode_sql_digests(id int primary key, v int)")

q1 := "begin"
norm1, digest1 := parser.NormalizeDigest(q1)
q2 := "select @@tidb_current_ts"
norm2, digest2 := parser.NormalizeDigest(q2)
q3 := "select id, v from test_func_decode_sql_digests where id = 1 for update"
norm3, digest3 := parser.NormalizeDigest(q3)

// TIDB_DECODE_SQL_DIGESTS function doesn't actually do "decoding", instead it queries `statements_summary` and it's
// variations for the corresponding statements.
// Execute the statements so that the queries will be saved into statements_summary table.
tk.MustExec(q1)
// Save the ts to query the transaction from tidb_trx.
ts, err := strconv.ParseUint(tk.MustQuery(q2).Rows()[0][0].(string), 10, 64)
require.NoError(t, err)
require.Greater(t, ts, uint64(0))
tk.MustExec(q3)
tk.MustExec("rollback")

// Test statements truncating.
decoded := fmt.Sprintf(`["%s","%s","%s"]`, norm1, norm2, norm3)
digests := fmt.Sprintf(`["%s","%s","%s"]`, digest1, digest2, digest3)
tk.MustQuery("select tidb_decode_sql_digests(?, 0)", digests).Check(testkit.Rows(decoded))
// The three queries are shorter than truncate length, equal to truncate length and longer than truncate length respectively.
tk.MustQuery("select tidb_decode_sql_digests(?, ?)", digests, len(norm2)).Check(testkit.Rows(
"[\"begin\",\"select @@tidb_current_ts\",\"select `id` , `v` from `...\"]"))

// Empty array.
tk.MustQuery("select tidb_decode_sql_digests('[]')").Check(testkit.Rows("[]"))

// NULL
tk.MustQuery("select tidb_decode_sql_digests(null)").Check(testkit.Rows("<nil>"))

// Array containing wrong types and not-existing digests (maps to null).
tk.MustQuery("select tidb_decode_sql_digests(?)", fmt.Sprintf(`["%s",1,null,"%s",{"a":1},[2],"%s","","abcde"]`, digest1, digest2, digest3)).
Check(testkit.Rows(fmt.Sprintf(`["%s",null,null,"%s",null,null,"%s",null,null]`, norm1, norm2, norm3)))

// Not JSON array (throws warnings)
tk.MustQuery(`select tidb_decode_sql_digests('{"a":1}')`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1210 The argument can't be unmarshalled as JSON array: '{"a":1}'`))
tk.MustQuery(`select tidb_decode_sql_digests('aabbccdd')`).Check(testkit.Rows("<nil>"))
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1210 The argument can't be unmarshalled as JSON array: 'aabbccdd'`))

// Invalid argument count.
tk.MustGetErrCode("select tidb_decode_sql_digests('a', 1, 2)", 1582)
tk.MustGetErrCode("select tidb_decode_sql_digests()", 1582)
}

func TestFunctionDecodeSQLDigestsPrivilege(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
srv := createRPCServer(t, dom)
defer srv.Stop()

dropUserTk := testkit.NewTestKit(t, store)
require.NoError(t, dropUserTk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))

tk := testkit.NewTestKit(t, store)
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
tk.MustExec("create user 'testuser'@'localhost'")
defer dropUserTk.MustExec("drop user 'testuser'@'localhost'")
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "testuser", Hostname: "localhost"}, nil, nil, nil))
tk.MustGetErrMsg("select tidb_decode_sql_digests('[\"aa\"]')", "[expression:1227]Access denied; you need (at least one of) the PROCESS privilege(s) for this operation")

tk = testkit.NewTestKit(t, store)
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
tk.MustExec("create user 'testuser2'@'localhost'")
defer dropUserTk.MustExec("drop user 'testuser2'@'localhost'")
tk.MustExec("grant process on *.* to 'testuser2'@'localhost'")
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "testuser2", Hostname: "localhost"}, nil, nil, nil))
tk.MustExec("select tidb_decode_sql_digests('[\"aa\"]')")
}

func TestFunctionEncodeSQLDigest(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
srv := createRPCServer(t, dom)
defer srv.Stop()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
require.NoError(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil, nil))
tk.MustExec("drop table if exists test_func_encode_sql_digest")
tk.MustExec("create table test_func_encode_sql_digest(id int primary key, v int)")

q1 := "begin"
digest1 := parser.DigestHash(q1)
q2 := "select @@tidb_current_ts"
digest2 := parser.DigestHash(q2)
q3 := "select id, v from test_func_decode_sql_digests where id = 1 for update"
digest3 := parser.DigestHash(q3)

tk.MustQuery(fmt.Sprintf("select tidb_encode_sql_digest(\"%s\")", q1)).Check(testkit.Rows(digest1.String()))
tk.MustQuery(fmt.Sprintf("select tidb_encode_sql_digest(\"%s\")", q2)).Check(testkit.Rows(digest2.String()))
tk.MustQuery(fmt.Sprintf("select tidb_encode_sql_digest(\"%s\")", q3)).Check(testkit.Rows(digest3.String()))

tk.MustQuery("select tidb_encode_sql_digest(null)").Check(testkit.Rows("<nil>"))
tk.MustGetErrCode("select tidb_encode_sql_digest()", 1582)

tk.MustQuery("select (select tidb_encode_sql_digest('select 1')) = tidb_encode_sql_digest('select 1;')").Check(testkit.Rows("1"))
tk.MustQuery("select (select tidb_encode_sql_digest('select 1')) = tidb_encode_sql_digest('select 1 ;')").Check(testkit.Rows("1"))
tk.MustQuery("select (select tidb_encode_sql_digest('select 1')) = tidb_encode_sql_digest('select 2 ;')").Check(testkit.Rows("1"))
}

func prepareLogs(t *testing.T, logData []string, fileNames []string) {
for i, log := range logData {
f, err := os.OpenFile(fileNames[i], os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
Expand Down
74 changes: 0 additions & 74 deletions pkg/executor/compact_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,80 +55,6 @@ func withMockTiFlash(nodes int) mockstore.MockTiKVStoreOption {
)
}

func TestCompactUnknownTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

err := tk.ExecToErr(`alter table test compact tiflash replica;`)
require.Equal(t, "[planner:1046]No database selected", err.Error())

err = tk.ExecToErr(`alter table test.foo compact tiflash replica;`)
require.Equal(t, "[schema:1146]Table 'test.foo' doesn't exist", err.Error())

tk.MustExec("use test")
err = tk.ExecToErr(`alter table bar compact;`)
require.Equal(t, "[schema:1146]Table 'test.bar' doesn't exist", err.Error())
}

func TestCompactTableNoTiFlashReplica(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("create table t(a int)")
tk.MustExec(`alter table t compact tiflash replica;`)
tk.MustQuery(`show warnings;`).Check(testkit.Rows(
`Warning 1105 compact skipped: no tiflash replica in the table`,
))

tk.MustExec(`alter table test.t compact;`)
tk.MustQuery(`show warnings;`).Check(testkit.Rows(
`Warning 1105 compact skipped: no tiflash replica in the table`,
))

tk = testkit.NewTestKit(t, store)
tk.MustExec(`alter table test.t compact;`)
tk.MustQuery(`show warnings;`).Check(testkit.Rows(
`Warning 1105 compact skipped: no tiflash replica in the table`,
))
}

func TestCompactTableNoPartition(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("create table t(a int)")
_, err := tk.Exec("alter table t compact partition p1,p2 tiflash replica;")
require.NotNil(t, err)
require.Equal(t, "table:t is not a partition table, but user specify partition name list:[p1 p2]", err.Error())
}

func TestCompactTablePartitionInvalid(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec(`
CREATE TABLE t (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(25) NOT NULL,
lname VARCHAR(25) NOT NULL,
store_id INT NOT NULL,
department_id INT NOT NULL
)
PARTITION BY RANGE(id) (
PARTITION p0 VALUES LESS THAN (5),
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (15),
PARTITION p3 VALUES LESS THAN MAXVALUE
);
`)
_, err := tk.Exec("alter table t compact partition p1,p2,p4 tiflash replica;")
require.NotNil(t, err)
require.Equal(t, "[table:1735]Unknown partition 'p4' in table 't'", err.Error())
}

func TestCompactTableTooBusy(t *testing.T) {
mocker := newCompactRequestMocker(t)
mocker.MockFrom(`tiflash0/#1`, func(req *kvrpcpb.CompactRequest) (*kvrpcpb.CompactResponse, error) {
Expand Down
Loading

0 comments on commit 3f9ba2b

Please sign in to comment.