diff --git a/executor/grant_test.go b/executor/grant_test.go index 200624d56778d..d7b2e24d3d159 100644 --- a/executor/grant_test.go +++ b/executor/grant_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 PingCAP, Inc. +// Copyright 2021 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,18 +17,24 @@ package executor_test import ( "fmt" "strings" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/parser/terror" - "github.com/pingcap/tidb/util/testkit" + "github.com/pingcap/tidb/testkit" + "github.com/stretchr/testify/require" ) -func (s *testSuiteP1) TestGrantGlobal(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantGlobal(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Create a new user. createUserSQL := `CREATE USER 'testGlobal'@'localhost' IDENTIFIED BY '123';` tk.MustExec(createUserSQL) @@ -64,8 +70,13 @@ func (s *testSuiteP1) TestGrantGlobal(c *C) { } } -func (s *testSuite3) TestGrantDBScope(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantDBScope(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Create a new user. createUserSQL := `CREATE USER 'testDB'@'localhost' IDENTIFIED BY '123';` tk.MustExec(createUserSQL) @@ -94,14 +105,19 @@ func (s *testSuite3) TestGrantDBScope(c *C) { // Grant in wrong scope. _, err := tk.Exec(` grant create user on test.* to 'testDB1'@'localhost';`) - c.Assert(terror.ErrorEqual(err, executor.ErrWrongUsage.GenWithStackByArgs("DB GRANT", "GLOBAL PRIVILEGES")), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrWrongUsage.GenWithStackByArgs("DB GRANT", "GLOBAL PRIVILEGES"))) _, err = tk.Exec("GRANT SUPER ON test.* TO 'testDB1'@'localhost';") - c.Assert(terror.ErrorEqual(err, executor.ErrWrongUsage.GenWithStackByArgs("DB GRANT", "NON-DB PRIVILEGES")), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrWrongUsage.GenWithStackByArgs("DB GRANT", "NON-DB PRIVILEGES"))) } -func (s *testSuite3) TestWithGrantOption(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestWithGrantOption(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Create a new user. createUserSQL := `CREATE USER 'testWithGrant'@'localhost' IDENTIFIED BY '123';` tk.MustExec(createUserSQL) @@ -121,8 +137,13 @@ func (s *testSuite3) TestWithGrantOption(c *C) { tk.MustQuery("SELECT grant_priv FROM mysql.user WHERE User=\"testWithGrant1\"").Check(testkit.Rows("Y")) } -func (s *testSuiteP1) TestGrantTableScope(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantTableScope(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Create a new user. createUserSQL := `CREATE USER 'testTbl'@'localhost' IDENTIFIED BY '123';` tk.MustExec(createUserSQL) @@ -135,11 +156,11 @@ func (s *testSuiteP1) TestGrantTableScope(c *C) { sql := fmt.Sprintf("GRANT %s ON test.test1 TO 'testTbl'@'localhost';", mysql.Priv2Str[v]) tk.MustExec(sql) rows := tk.MustQuery(`SELECT Table_priv FROM mysql.Tables_priv WHERE User="testTbl" and host="localhost" and db="test" and Table_name="test1";`).Rows() - c.Assert(rows, HasLen, 1) + require.Len(t, rows, 1) row := rows[0] - c.Assert(row, HasLen, 1) + require.Len(t, rows, 1) p := fmt.Sprintf("%v", row[0]) - c.Assert(strings.Index(p, mysql.Priv2SetStr[v]), Greater, -1) + require.Greater(t, strings.Index(p, mysql.Priv2SetStr[v]), -1) } // Create a new user. createUserSQL = `CREATE USER 'testTbl1'@'localhost' IDENTIFIED BY '123';` @@ -151,19 +172,24 @@ func (s *testSuiteP1) TestGrantTableScope(c *C) { // Make sure all the table privs for granted user are in the Table_priv set. for _, v := range mysql.AllTablePrivs { rows := tk.MustQuery(`SELECT Table_priv FROM mysql.Tables_priv WHERE User="testTbl1" and host="localhost" and db="test" and Table_name="test2";`).Rows() - c.Assert(rows, HasLen, 1) + require.Len(t, rows, 1) row := rows[0] - c.Assert(row, HasLen, 1) + require.Len(t, rows, 1) p := fmt.Sprintf("%v", row[0]) - c.Assert(strings.Index(p, mysql.Priv2SetStr[v]), Greater, -1) + require.Greater(t, strings.Index(p, mysql.Priv2SetStr[v]), -1) } _, err := tk.Exec("GRANT SUPER ON test2 TO 'testTbl1'@'localhost';") - c.Assert(err, ErrorMatches, "\\[executor:1144\\]Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be used") + require.EqualError(t, err, "[executor:1144]Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be used") } -func (s *testSuite3) TestGrantColumnScope(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantColumnScope(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Create a new user. createUserSQL := `CREATE USER 'testCol'@'localhost' IDENTIFIED BY '123';` tk.MustExec(createUserSQL) @@ -178,11 +204,11 @@ func (s *testSuite3) TestGrantColumnScope(c *C) { sql := fmt.Sprintf("GRANT %s(c1) ON test.test3 TO 'testCol'@'localhost';", mysql.Priv2Str[v]) tk.MustExec(sql) rows := tk.MustQuery(`SELECT Column_priv FROM mysql.Columns_priv WHERE User="testCol" and host="localhost" and db="test" and Table_name="test3" and Column_name="c1";`).Rows() - c.Assert(rows, HasLen, 1) + require.Len(t, rows, 1) row := rows[0] - c.Assert(row, HasLen, 1) + require.Len(t, rows, 1) p := fmt.Sprintf("%v", row[0]) - c.Assert(strings.Index(p, mysql.Priv2SetStr[v]), Greater, -1) + require.Greater(t, strings.Index(p, mysql.Priv2SetStr[v]), -1) } // Create a new user. @@ -194,19 +220,24 @@ func (s *testSuite3) TestGrantColumnScope(c *C) { // Make sure all the column privs for granted user are in the Column_priv set. for _, v := range mysql.AllColumnPrivs { rows := tk.MustQuery(`SELECT Column_priv FROM mysql.Columns_priv WHERE User="testCol1" and host="localhost" and db="test" and Table_name="test3" and Column_name="c2";`).Rows() - c.Assert(rows, HasLen, 1) + require.Len(t, rows, 1) row := rows[0] - c.Assert(row, HasLen, 1) + require.Len(t, rows, 1) p := fmt.Sprintf("%v", row[0]) - c.Assert(strings.Index(p, mysql.Priv2SetStr[v]), Greater, -1) + require.Greater(t, strings.Index(p, mysql.Priv2SetStr[v]), -1) } _, err := tk.Exec("GRANT SUPER(c2) ON test3 TO 'testCol1'@'localhost';") - c.Assert(err, ErrorMatches, "\\[executor:1221\\]Incorrect usage of COLUMN GRANT and NON-COLUMN PRIVILEGES") + require.EqualError(t, err, "[executor:1221]Incorrect usage of COLUMN GRANT and NON-COLUMN PRIVILEGES") } -func (s *testSuite3) TestIssue2456(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestIssue2456(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("CREATE USER 'dduser'@'%' IDENTIFIED by '123456';") tk.MustExec("CREATE DATABASE `dddb_%`;") tk.MustExec("CREATE table `dddb_%`.`te%` (id int);") @@ -214,17 +245,27 @@ func (s *testSuite3) TestIssue2456(c *C) { tk.MustExec("GRANT ALL PRIVILEGES ON `dddb_%`.`te%` to 'dduser'@'%';") } -func (s *testSuite3) TestNoAutoCreateUser(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestNoAutoCreateUser(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`) tk.MustExec(`SET sql_mode='NO_AUTO_CREATE_USER'`) _, err := tk.Exec(`GRANT ALL PRIVILEGES ON *.* to 'test'@'%' IDENTIFIED BY 'xxx'`) - c.Check(err, NotNil) - c.Assert(terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant), IsTrue) + require.Error(t, err) + require.True(t, terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant)) } -func (s *testSuite3) TestCreateUserWhenGrant(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestCreateUserWhenGrant(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`) // This only applies to sql_mode:NO_AUTO_CREATE_USER off tk.MustExec(`SET SQL_MODE=''`) @@ -236,14 +277,19 @@ func (s *testSuite3) TestCreateUserWhenGrant(c *C) { tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`) } -func (s *testSuite3) TestGrantPrivilegeAtomic(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantPrivilegeAtomic(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec(`drop role if exists r1, r2, r3, r4;`) tk.MustExec(`create role r1, r2, r3;`) tk.MustExec(`create table test.testatomic(x int);`) _, err := tk.Exec(`grant update, select, insert, delete on *.* to r1, r2, r4;`) - c.Assert(terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant)) tk.MustQuery(`select Update_priv, Select_priv, Insert_priv, Delete_priv from mysql.user where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows( "N N N N", "N N N N", @@ -251,7 +297,7 @@ func (s *testSuite3) TestGrantPrivilegeAtomic(c *C) { )) tk.MustExec(`grant update, select, insert, delete on *.* to r1, r2, r3;`) _, err = tk.Exec(`revoke all on *.* from r1, r2, r4, r3;`) - c.Check(err, NotNil) + require.Error(t, err) tk.MustQuery(`select Update_priv, Select_priv, Insert_priv, Delete_priv from mysql.user where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows( "Y Y Y Y", "Y Y Y Y", @@ -259,11 +305,11 @@ func (s *testSuite3) TestGrantPrivilegeAtomic(c *C) { )) _, err = tk.Exec(`grant update, select, insert, delete on test.* to r1, r2, r4;`) - c.Assert(terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant)) tk.MustQuery(`select Update_priv, Select_priv, Insert_priv, Delete_priv from mysql.db where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows()) tk.MustExec(`grant update, select, insert, delete on test.* to r1, r2, r3;`) _, err = tk.Exec(`revoke all on *.* from r1, r2, r4, r3;`) - c.Check(err, NotNil) + require.Error(t, err) tk.MustQuery(`select Update_priv, Select_priv, Insert_priv, Delete_priv from mysql.db where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows( "Y Y Y Y", "Y Y Y Y", @@ -271,11 +317,11 @@ func (s *testSuite3) TestGrantPrivilegeAtomic(c *C) { )) _, err = tk.Exec(`grant update, select, insert, delete on test.testatomic to r1, r2, r4;`) - c.Assert(terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrCantCreateUserWithGrant)) tk.MustQuery(`select Table_priv from mysql.tables_priv where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows()) tk.MustExec(`grant update, select, insert, delete on test.testatomic to r1, r2, r3;`) _, err = tk.Exec(`revoke all on *.* from r1, r2, r4, r3;`) - c.Check(err, NotNil) + require.Error(t, err) tk.MustQuery(`select Table_priv from mysql.tables_priv where user in ('r1', 'r2', 'r3', 'r4') and host = "%";`).Check(testkit.Rows( "Select,Insert,Update,Delete", "Select,Insert,Update,Delete", @@ -287,8 +333,13 @@ func (s *testSuite3) TestGrantPrivilegeAtomic(c *C) { } -func (s *testSuite3) TestIssue2654(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestIssue2654(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec(`DROP USER IF EXISTS 'test'@'%'`) tk.MustExec(`CREATE USER 'test'@'%' IDENTIFIED BY 'test'`) tk.MustExec("GRANT SELECT ON test.* to 'test'") @@ -296,8 +347,13 @@ func (s *testSuite3) TestIssue2654(c *C) { rows.Check(testkit.Rows(`test %`)) } -func (s *testSuite3) TestGrantUnderANSIQuotes(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantUnderANSIQuotes(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // Fix a bug that the GrantExec fails in ANSI_QUOTES sql mode // The bug is caused by the improper usage of double quotes like: // INSERT INTO mysql.user ... VALUES ("..", "..", "..") @@ -307,8 +363,13 @@ func (s *testSuite3) TestGrantUnderANSIQuotes(c *C) { tk.MustExec(`DROP USER IF EXISTS 'web'@'%'`) } -func (s *testSuite3) TestMaintainRequire(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestMaintainRequire(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) // test create with require tk.MustExec(`CREATE USER 'ssl_auser'@'%' require issuer '/CN=TiDB admin/OU=TiDB/O=PingCAP/L=San Francisco/ST=California/C=US' subject '/CN=tester1/OU=TiDB/O=PingCAP.Inc/L=Haidian/ST=Beijing/C=ZH' cipher 'AES128-GCM-SHA256'`) @@ -356,57 +417,72 @@ func (s *testSuite3) TestMaintainRequire(c *C) { // check issuer/subject/cipher value _, err := tk.Exec(`CREATE USER 'u4'@'%' require issuer 'CN=TiDB,OU=PingCAP'`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u5'@'%' require subject '/CN=TiDB\OU=PingCAP'`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u6'@'%' require subject '/CN=TiDB\NC=PingCAP'`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u7'@'%' require cipher 'AES128-GCM-SHA1'`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u8'@'%' require subject '/CN'`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u9'@'%' require cipher 'TLS_AES_256_GCM_SHA384' cipher 'RC4-SHA'`) - c.Assert(err.Error(), Equals, "Duplicate require CIPHER clause") + require.EqualError(t, err, "Duplicate require CIPHER clause") _, err = tk.Exec(`CREATE USER 'u9'@'%' require issuer 'CN=TiDB,OU=PingCAP' issuer 'CN=TiDB,OU=PingCAP2'`) - c.Assert(err.Error(), Equals, "Duplicate require ISSUER clause") + require.EqualError(t, err, "Duplicate require ISSUER clause") _, err = tk.Exec(`CREATE USER 'u9'@'%' require subject '/CN=TiDB\OU=PingCAP' subject '/CN=TiDB\OU=PingCAP2'`) - c.Assert(err.Error(), Equals, "Duplicate require SUBJECT clause") + require.EqualError(t, err, "Duplicate require SUBJECT clause") _, err = tk.Exec(`CREATE USER 'u9'@'%' require ssl ssl`) - c.Assert(err, NotNil) + require.Error(t, err) _, err = tk.Exec(`CREATE USER 'u9'@'%' require x509 x509`) - c.Assert(err, NotNil) + require.Error(t, err) } -func (s *testSuite3) TestMaintainAuthString(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestMaintainAuthString(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec(`CREATE USER 'maint_auth_str1'@'%' IDENTIFIED BY 'foo'`) tk.MustQuery("SELECT authentication_string FROM mysql.user WHERE `Host` = '%' and `User` = 'maint_auth_str1'").Check(testkit.Rows("*F3A2A51A9B0F2BE2468926B4132313728C250DBF")) tk.MustExec(`ALTER USER 'maint_auth_str1'@'%' REQUIRE SSL`) tk.MustQuery("SELECT authentication_string FROM mysql.user WHERE `Host` = '%' and `User` = 'maint_auth_str1'").Check(testkit.Rows("*F3A2A51A9B0F2BE2468926B4132313728C250DBF")) } -func (s *testSuite3) TestGrantOnNonExistTable(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantOnNonExistTable(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("create user genius") tk.MustExec("use test") _, err := tk.Exec("select * from nonexist") - c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotExists), IsTrue) + require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists)) // GRANT ON non-existent table success, see issue #28533 tk.MustExec("grant Select,Insert on nonexist to 'genius'") tk.MustExec("create table if not exists xx (id int)") // Case sensitive _, err = tk.Exec("grant Select,Insert on XX to 'genius'") - c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotExists), IsTrue) + require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists)) _, err = tk.Exec("grant Select,Insert on xx to 'genius'") - c.Assert(err, IsNil) + require.NoError(t, err) _, err = tk.Exec("grant Select,Update on test.xx to 'genius'") - c.Assert(err, IsNil) + require.NoError(t, err) } -func (s *testSuite3) TestIssue22721(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestIssue22721(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create table if not exists xx (id int)") tk.MustExec("CREATE USER 'sync_ci_data'@'%' IDENTIFIED BY 'sNGNQo12fEHe0n3vU';") @@ -416,21 +492,26 @@ func (s *testSuite3) TestIssue22721(c *C) { tk.MustExec("GRANT USAGE ON test.xx TO 'sync_ci_data'@'%';") } -func (s *testSuite3) TestPerformanceSchemaPrivGrant(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestPerformanceSchemaPrivGrant(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("create user issue27867;") defer func() { tk.MustExec("drop user issue27867;") }() - c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost"}, nil, nil), IsTrue) + require.True(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost"}, nil, nil)) err := tk.ExecToErr("grant all on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") // Check case insensitivity err = tk.ExecToErr("grant all on PERFormanCE_scHemA.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormanCE_scHemA'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormanCE_scHemA'") // Check other database privileges tk.MustExec("grant select on performance_schema.* to issue27867;") tk.MustExec("grant insert on performance_schema.* to issue27867;") @@ -439,36 +520,41 @@ func (s *testSuite3) TestPerformanceSchemaPrivGrant(c *C) { tk.MustExec("grant drop on performance_schema.* to issue27867;") tk.MustExec("grant lock tables on performance_schema.* to issue27867;") err = tk.ExecToErr("grant create on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") err = tk.ExecToErr("grant references on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") err = tk.ExecToErr("grant alter on PERFormAnCE_scHemA.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormAnCE_scHemA'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormAnCE_scHemA'") err = tk.ExecToErr("grant execute on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") err = tk.ExecToErr("grant index on PERFormanCE_scHemA.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormanCE_scHemA'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'PERFormanCE_scHemA'") err = tk.ExecToErr("grant create view on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") err = tk.ExecToErr("grant show view on performance_schema.* to issue27867;") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") + require.Error(t, err) + require.EqualError(t, err, "[executor:1044]Access denied for user 'root'@'%' to database 'performance_schema'") } -func (s *testSuite3) TestGrantDynamicPrivs(c *C) { - tk := testkit.NewTestKit(c, s.store) +func TestGrantDynamicPrivs(t *testing.T) { + t.Parallel() + + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) tk.MustExec("create user dyn") _, err := tk.Exec("GRANT BACKUP_ADMIN ON test.* TO dyn") - c.Assert(terror.ErrorEqual(err, executor.ErrIllegalPrivilegeLevel), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrIllegalPrivilegeLevel)) _, err = tk.Exec("GRANT BOGUS_GRANT ON *.* TO dyn") - c.Assert(terror.ErrorEqual(err, executor.ErrDynamicPrivilegeNotRegistered), IsTrue) + require.True(t, terror.ErrorEqual(err, executor.ErrDynamicPrivilegeNotRegistered)) tk.MustExec("GRANT BACKUP_Admin ON *.* TO dyn") // grant one priv tk.MustQuery("SELECT * FROM mysql.global_grants WHERE `Host` = '%' AND `User` = 'dyn' ORDER BY user,host,priv,with_grant_option").Check(testkit.Rows("dyn % BACKUP_ADMIN N")) diff --git a/executor/hash_table_test.go b/executor/hash_table_serial_test.go similarity index 69% rename from executor/hash_table_test.go rename to executor/hash_table_serial_test.go index 9b9dfa4dbd329..3def2a0871adc 100644 --- a/executor/hash_table_test.go +++ b/executor/hash_table_serial_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 PingCAP, Inc. +// Copyright 2021 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,14 +18,15 @@ import ( "fmt" "hash" "hash/fnv" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/memory" "github.com/pingcap/tidb/util/mock" + "github.com/stretchr/testify/require" ) func initBuildChunk(numRows int) (*chunk.Chunk, []*types.FieldType) { @@ -82,34 +83,34 @@ func (h hashCollision) Sum(b []byte) []byte { panic("not implement func (h hashCollision) Size() int { panic("not implemented") } func (h hashCollision) BlockSize() int { panic("not implemented") } -func (s *pkgTestSerialSuite) TestHashRowContainer(c *C) { +func TestHashRowContainer(t *testing.T) { hashFunc := fnv.New64 - rowContainer, copiedRC := s.testHashRowContainer(c, hashFunc, false) - c.Assert(rowContainer.stat.probeCollision, Equals, int64(0)) + rowContainer, copiedRC := testHashRowContainer(t, hashFunc, false) + require.Equal(t, int64(0), rowContainer.stat.probeCollision) // On windows time.Now() is imprecise, the elapse time may equal 0 - c.Assert(rowContainer.stat.buildTableElapse >= 0, IsTrue) - c.Assert(copiedRC.stat.probeCollision, Equals, rowContainer.stat.probeCollision) - c.Assert(copiedRC.stat.buildTableElapse, Equals, rowContainer.stat.buildTableElapse) + require.True(t, rowContainer.stat.buildTableElapse >= 0) + require.Equal(t, rowContainer.stat.probeCollision, copiedRC.stat.probeCollision) + require.Equal(t, rowContainer.stat.buildTableElapse, copiedRC.stat.buildTableElapse) - rowContainer, copiedRC = s.testHashRowContainer(c, hashFunc, true) - c.Assert(rowContainer.stat.probeCollision, Equals, int64(0)) - c.Assert(rowContainer.stat.buildTableElapse >= 0, IsTrue) - c.Assert(copiedRC.stat.probeCollision, Equals, rowContainer.stat.probeCollision) - c.Assert(copiedRC.stat.buildTableElapse, Equals, rowContainer.stat.buildTableElapse) + rowContainer, copiedRC = testHashRowContainer(t, hashFunc, true) + require.Equal(t, int64(0), rowContainer.stat.probeCollision) + require.True(t, rowContainer.stat.buildTableElapse >= 0) + require.Equal(t, rowContainer.stat.probeCollision, copiedRC.stat.probeCollision) + require.Equal(t, rowContainer.stat.buildTableElapse, copiedRC.stat.buildTableElapse) h := &hashCollision{count: 0} hashFuncCollision := func() hash.Hash64 { return h } - rowContainer, copiedRC = s.testHashRowContainer(c, hashFuncCollision, false) - c.Assert(h.count > 0, IsTrue) - c.Assert(rowContainer.stat.probeCollision > int64(0), IsTrue) - c.Assert(rowContainer.stat.buildTableElapse >= 0, IsTrue) - c.Assert(copiedRC.stat.probeCollision, Equals, rowContainer.stat.probeCollision) - c.Assert(copiedRC.stat.buildTableElapse, Equals, rowContainer.stat.buildTableElapse) + rowContainer, copiedRC = testHashRowContainer(t, hashFuncCollision, false) + require.True(t, h.count > 0) + require.True(t, rowContainer.stat.probeCollision > int64(0)) + require.True(t, rowContainer.stat.buildTableElapse >= 0) + require.Equal(t, rowContainer.stat.probeCollision, copiedRC.stat.probeCollision) + require.Equal(t, rowContainer.stat.buildTableElapse, copiedRC.stat.buildTableElapse) } -func (s *pkgTestSerialSuite) testHashRowContainer(c *C, hashFunc func() hash.Hash64, spill bool) (originRC, copiedRC *hashRowContainer) { +func testHashRowContainer(t *testing.T, hashFunc func() hash.Hash64, spill bool) (originRC, copiedRC *hashRowContainer) { sctx := mock.NewContext() var err error numRows := 10 @@ -134,16 +135,16 @@ func (s *pkgTestSerialSuite) testHashRowContainer(c *C, hashFunc func() hash.Has rowContainer.rowContainer.ActionSpillForTest().Action(tracker) } err = rowContainer.PutChunk(chk0, nil) - c.Assert(err, IsNil) + require.NoError(t, err) err = rowContainer.PutChunk(chk1, nil) - c.Assert(err, IsNil) + require.NoError(t, err) rowContainer.ActionSpill().(*chunk.SpillDiskAction).WaitForTest() - c.Assert(rowContainer.alreadySpilledSafeForTest(), Equals, spill) - c.Assert(rowContainer.GetMemTracker().BytesConsumed() == 0, Equals, spill) - c.Assert(rowContainer.GetMemTracker().BytesConsumed() > 0, Equals, !spill) + require.Equal(t, spill, rowContainer.alreadySpilledSafeForTest()) + require.Equal(t, spill, rowContainer.GetMemTracker().BytesConsumed() == 0) + require.Equal(t, !spill, rowContainer.GetMemTracker().BytesConsumed() > 0) if rowContainer.alreadySpilledSafeForTest() { - c.Assert(rowContainer.GetDiskTracker(), NotNil) - c.Assert(rowContainer.GetDiskTracker().BytesConsumed() > 0, Equals, true) + require.NotNil(t, rowContainer.GetDiskTracker()) + require.True(t, rowContainer.GetDiskTracker().BytesConsumed() > 0) } probeChk, probeColType := initProbeChunk(2) @@ -155,9 +156,9 @@ func (s *pkgTestSerialSuite) testHashRowContainer(c *C, hashFunc func() hash.Has probeCtx.hasNull = make([]bool, 1) probeCtx.hashVals = append(hCtx.hashVals, hashFunc()) matched, _, err := rowContainer.GetMatchedRowsAndPtrs(hCtx.hashVals[1].Sum64(), probeRow, probeCtx) - c.Assert(err, IsNil) - c.Assert(len(matched), Equals, 2) - c.Assert(matched[0].GetDatumRow(colTypes), DeepEquals, chk0.GetRow(1).GetDatumRow(colTypes)) - c.Assert(matched[1].GetDatumRow(colTypes), DeepEquals, chk1.GetRow(1).GetDatumRow(colTypes)) + require.NoError(t, err) + require.Equal(t, 2, len(matched)) + require.Equal(t, chk0.GetRow(1).GetDatumRow(colTypes), matched[0].GetDatumRow(colTypes)) + require.Equal(t, chk1.GetRow(1).GetDatumRow(colTypes), matched[1].GetDatumRow(colTypes)) return rowContainer, copiedRC }