forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_placement_test.go
473 lines (392 loc) · 19.6 KB
/
show_placement_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// 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.
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"fmt"
"testing"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
func TestShowPlacement(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2, t3, t4, db2.t2")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop database if exists db3")
tk.MustExec("drop placement policy if exists pa1")
tk.MustExec("drop placement policy if exists pa2")
tk.MustExec("drop placement policy if exists pb1")
// prepare policies
tk.MustExec("create placement policy pa2 " +
"LEADER_CONSTRAINTS=\"[+region=us-east-1]\" " +
"FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" " +
"FOLLOWERS=3")
defer tk.MustExec("drop placement policy pa2")
tk.MustExec("create placement policy pa1 " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1,cn-east-2\"" +
"SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy pa1")
tk.MustExec("create placement policy pb1 " +
"VOTER_CONSTRAINTS=\"[+region=bj]\" " +
"LEARNER_CONSTRAINTS=\"[+region=sh]\" " +
"CONSTRAINTS=\"[+disk=ssd]\"" +
"VOTERS=5 " +
"LEARNERS=3")
defer tk.MustExec("drop placement policy pb1")
// prepare database
tk.MustExec("create database db2 PLACEMENT POLICY pa2")
defer tk.MustExec("drop database if exists db2")
// prepare tables
tk.MustExec("create table t1 (id int) placement policy pa1")
defer tk.MustExec("drop table if exists t1")
tk.MustExec("create table t2 (id int)")
defer tk.MustExec("drop table if exists t2")
tk.MustExec("CREATE TABLE t3 (id INT) placement policy pa1 PARTITION BY RANGE (id) (" +
"PARTITION p0 VALUES LESS THAN (100) placement policy pa2," +
"PARTITION p1 VALUES LESS THAN (1000)," +
"PARTITION p2 VALUES LESS THAN (10000)" +
")")
defer tk.MustExec("drop table if exists t3")
tk.MustExec("create table db2.t2 (id int) PLACEMENT POLICY pa2")
defer tk.MustExec("drop table if exists db2.t2")
tk.MustQuery("show placement").Check(testkit.Rows(
"POLICY pa1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY pa2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" NULL",
"POLICY pb1 CONSTRAINTS=\"[+disk=ssd]\" VOTERS=5 VOTER_CONSTRAINTS=\"[+region=bj]\" LEARNERS=3 LEARNER_CONSTRAINTS=\"[+region=sh]\" NULL",
"DATABASE db2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" PENDING",
"TABLE db2.t2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" PENDING",
"TABLE test.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t3 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t3 PARTITION p0 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" PENDING",
"TABLE test.t3 PARTITION p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t3 PARTITION p2 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
))
tk.MustQuery("show placement like 'POLICY%'").Check(testkit.Rows(
"POLICY pa1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY pa2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" NULL",
"POLICY pb1 CONSTRAINTS=\"[+disk=ssd]\" VOTERS=5 VOTER_CONSTRAINTS=\"[+region=bj]\" LEARNERS=3 LEARNER_CONSTRAINTS=\"[+region=sh]\" NULL",
))
tk.MustQuery("show placement like 'POLICY pa%'").Check(testkit.Rows(
"POLICY pa1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY pa2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=3 FOLLOWER_CONSTRAINTS=\"[+region=us-east-2]\" NULL",
))
tk.MustQuery("show placement where Target='POLICY pb1'").Check(testkit.Rows(
"POLICY pb1 CONSTRAINTS=\"[+disk=ssd]\" VOTERS=5 VOTER_CONSTRAINTS=\"[+region=bj]\" LEARNERS=3 LEARNER_CONSTRAINTS=\"[+region=sh]\" NULL",
))
}
func TestShowPlacementPrivilege(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1,t2,t3, db2.t1, db2.t3")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop database if exists db3")
tk.MustExec("drop user if exists user1")
tk.MustExec("drop placement policy if exists p1")
// prepare user
tk.MustExec("create user user1")
defer tk.MustExec("drop user user1")
// prepare policy
tk.MustExec("create placement policy p1 " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1,cn-east-2\"" +
"SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy p1")
// prepare database
tk.MustExec("create database db3 PLACEMENT POLICY p1")
defer tk.MustExec("drop database if exists db3")
tk.MustExec("create database db2 PLACEMENT POLICY p1")
defer tk.MustExec("drop database if exists db2")
// prepare tables
tk.MustExec("create table t1 (id int) placement policy p1")
defer tk.MustExec("drop table if exists t1")
tk.MustExec("create table t2 (id int) PLACEMENT POLICY p1")
defer tk.MustExec("drop table if exists t2")
tk.MustExec("CREATE TABLE t3 (id INT) PARTITION BY RANGE (id) (" +
"PARTITION p1 VALUES LESS THAN (100) PLACEMENT POLICY p1" +
")")
defer tk.MustExec("drop table if exists t3")
tk.MustExec("create table db2.t1 (id int) PLACEMENT POLICY p1")
defer tk.MustExec("drop table if exists db2.t1")
tk.MustExec("create table db2.t3 (id int) PLACEMENT POLICY p1")
defer tk.MustExec("drop table if exists db2.t3")
tk1 := testkit.NewTestKit(t, store)
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "user1", Hostname: "%"}, nil, nil))
// before grant
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
))
// do some grant
tk.MustExec(`grant select on test.t1 to 'user1'@'%'`)
tk.MustExec(`grant select on test.t3 to 'user1'@'%'`)
tk.MustExec(`grant select on db2.t1 to 'user1'@'%'`)
// after grant
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"DATABASE db2 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE db2.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t3 PARTITION p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
))
}
func TestShowPlacementForDB(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("create placement policy p1 " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1,cn-east-2\"" +
"SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy p1")
tk.MustExec("create database db2 placement policy p1")
defer tk.MustExec("drop database if exists db2")
err := tk.QueryToErr("show placement for database dbnoexist")
require.EqualError(t, err, "[schema:1049]Unknown database 'dbnoexist'")
tk.MustQuery("show placement for database test").Check(testkit.Rows())
tk.MustQuery("show placement for database db2").Check(testkit.Rows(
"DATABASE db2 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" SCHEDULED",
))
}
func TestShowPlacementForTableAndPartition(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
tk.MustExec("drop table if exists t1,t2,t3,t4,db2.t1")
tk.MustExec("drop database if exists db2")
tk.MustExec("create placement policy p1 " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1,cn-east-2\"" +
"SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy p1")
tk.MustExec("create placement policy p2 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=2")
defer tk.MustExec("drop placement policy p2")
// table ref a policy
tk.MustExec("create table t1 (id int) placement policy p1")
defer tk.MustExec("drop table if exists t1")
tk.MustQuery("show placement for table t1").Check(testkit.Rows(
"TABLE test.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
))
// table no placement
tk.MustExec("create table t3 (id int)")
defer tk.MustExec("drop table if exists t3")
tk.MustQuery("show placement for table t3").Check(testkit.Rows())
// table do not display partition placement
tk.MustExec("create table t4 (id int) PLACEMENT POLICY p2 PARTITION BY RANGE (id) (" +
"PARTITION p0 VALUES LESS THAN (100)," +
"PARTITION p1 VALUES LESS THAN (1000)," +
"PARTITION p2 VALUES LESS THAN (10000) PLACEMENT POLICY p1" +
")")
defer tk.MustExec("drop table if exists t4")
tk.MustQuery("show placement for table t4").Check(testkit.Rows(
"TABLE test.t4 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=2 PENDING",
))
// partition inherent table
tk.MustQuery("show placement for table t4 partition p0").Check(testkit.Rows(
"TABLE test.t4 PARTITION p0 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=2 PENDING",
))
// partition custom placement
tk.MustQuery("show placement for table t4 partition p2").Check(testkit.Rows(
"TABLE test.t4 PARTITION p2 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
))
// partition without placement
tk.MustExec("create table t5 (id int) PARTITION BY RANGE (id) (" +
"PARTITION p0 VALUES LESS THAN (100)" +
")")
tk.MustQuery("show placement for table t5 partition p0").Check(testkit.Rows())
// table name with format db.table
tk.MustExec("create database db2")
defer tk.MustExec("drop database if exists db2")
tk.MustExec("create table db2.t1 (id int) PLACEMENT POLICY p2")
defer tk.MustExec("drop table if exists db2.t1")
tk.MustQuery("show placement for table db2.t1").Check(testkit.Rows(
"TABLE db2.t1 LEADER_CONSTRAINTS=\"[+region=us-east-1]\" FOLLOWERS=2 PENDING",
))
// not exists
err := tk.ExecToErr("show placement for table tn")
require.EqualError(t, err, "[schema:1146]Table 'test.tn' doesn't exist")
err = tk.ExecToErr("show placement for table dbn.t1")
require.EqualError(t, err, "[schema:1146]Table 'dbn.t1' doesn't exist")
err = tk.ExecToErr("show placement for table tn partition pn")
require.EqualError(t, err, "[schema:1146]Table 'test.tn' doesn't exist")
err = tk.QueryToErr("show placement for table t1 partition pn")
require.EqualError(t, err, "[table:1735]Unknown partition 'pn' in table 't1'")
err = tk.QueryToErr("show placement for table t4 partition pn")
require.EqualError(t, err, "[table:1735]Unknown partition 'pn' in table 't4'")
}
func TestShowPlacementForDBPrivilege(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists db2.t1")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop user if exists user1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
// prepare policy
tk.MustExec("create placement policy p1 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy if exists p1")
tk.MustExec("create placement policy p2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r3\" SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy if exists p2")
// prepare user
tk.MustExec("create user user1")
defer tk.MustExec("drop user user1")
// prepare database
tk.MustExec("create database db2 PLACEMENT POLICY p1")
defer tk.MustExec("drop database db2")
// prepare table
tk.MustExec("create table db2.t1 (id int) PLACEMENT POLICY p2")
defer tk.MustExec("drop table db2.t1")
tk1 := testkit.NewTestKit(t, store)
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "user1", Hostname: "%"}, nil, nil))
privs := []string{
"all privileges on db2.*",
"create on db2.*",
"alter on db2.*",
"drop on db2.*",
"select on db2.t1",
"insert on db2.t1",
"create on db2.t1",
"delete on db2.t1",
}
// before grant
err := tk1.QueryToErr("show placement for database db2")
require.EqualError(t, err, executor.ErrDBaccessDenied.GenWithStackByArgs("user1", "%", "db2").Error())
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r3\" SCHEDULE=\"EVEN\" NULL",
))
for _, priv := range privs {
// do grant
tk.MustExec(fmt.Sprintf("grant %s to 'user1'@'%%'", priv))
tk1.MustQuery("show placement for database db2").Check(testkit.Rows(
"DATABASE db2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\" PENDING",
))
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r3\" SCHEDULE=\"EVEN\" NULL",
"DATABASE db2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE db2.t1 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r3\" SCHEDULE=\"EVEN\" PENDING",
))
err = tk1.QueryToErr("show placement for database test")
require.EqualError(t, err, executor.ErrDBaccessDenied.GenWithStackByArgs("user1", "%", "test").Error())
// do revoke
tk.MustExec(fmt.Sprintf("revoke %s from 'user1'@'%%'", priv))
err = tk1.QueryToErr("show placement for database db2")
require.EqualError(t, err, executor.ErrDBaccessDenied.GenWithStackByArgs("user1", "%", "db2").Error())
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 PRIMARY_REGION=\"r1\" REGIONS=\"r1,r3\" SCHEDULE=\"EVEN\" NULL",
))
}
}
func TestShowPlacementForTableAndPartitionPrivilege(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1,t2,t3,t4,db2.t1")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop user if exists user1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
// prepare user
tk.MustExec("create user user1")
defer tk.MustExec("drop user user1")
// prepare database
tk.MustExec("create database db2")
defer tk.MustExec("drop database db2")
// prepare policy
tk.MustExec("create placement policy p1 " +
"PRIMARY_REGION=\"cn-east-1\" " +
"REGIONS=\"cn-east-1,cn-east-2\"" +
"SCHEDULE=\"EVEN\"")
defer tk.MustExec("drop placement policy p1")
tk.MustExec("create placement policy p2 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWER_CONSTRAINTS=\"[+region=sh]\" FOLLOWERS=4")
defer tk.MustExec("drop placement policy p2")
// prepare tables
tk.MustExec("create table t1 (id int) placement policy p1 PARTITION BY RANGE (id) (" +
"PARTITION p1 VALUES LESS THAN (1000) PLACEMENT POLICY p2" +
")")
defer tk.MustExec("drop table if exists t1")
tk.MustExec("create table t2 (id int) PLACEMENT POLICY p2")
defer tk.MustExec("drop table if exists t2")
tk.MustExec("create table t3 (id int)")
defer tk.MustExec("drop table if exists t3")
tk.MustExec("create table db2.t1 (id int) PLACEMENT POLICY p2")
defer tk.MustExec("drop table if exists db2.t1")
tk1 := testkit.NewTestKit(t, store)
require.NoError(t, tk1.Session().Auth(&auth.UserIdentity{Username: "user1", Hostname: "%"}, nil, nil))
// before grant
err := tk1.ExecToErr("show placement for table test.t1")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t1").Error())
err = tk1.ExecToErr("show placement for table test.t1 partition p1")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t1").Error())
err = tk1.ExecToErr("show placement for table test.t2")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t2").Error())
err = tk1.ExecToErr("show placement for table test.t3")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t3").Error())
err = tk1.ExecToErr("show placement for table db2.t1")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t1").Error())
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=sh]\" NULL",
))
privs := []string{
"create on test.t1",
"alter on test.t1",
"drop on test.t1",
"select on test.t1",
"insert on test.t1",
"create on test.t1",
"delete on test.t1",
}
for _, priv := range privs {
// do grant
tk.MustExec(fmt.Sprintf("grant %s to 'user1'@'%%'", priv))
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=sh]\" NULL",
"TABLE test.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
"TABLE test.t1 PARTITION p1 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=sh]\" PENDING",
))
tk1.MustQuery("show placement for table test.t1").Check(testkit.Rows(
"TABLE test.t1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" PENDING",
))
tk1.MustQuery("show placement for table test.t1 partition p1").Check(testkit.Rows(
"TABLE test.t1 PARTITION p1 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=sh]\" PENDING",
))
err = tk1.ExecToErr("show placement for table test.t2")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t2").Error())
err = tk1.ExecToErr("show placement for table test.t3")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t3").Error())
err = tk1.ExecToErr("show placement for table db2.t1")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t1").Error())
// do revoke
tk.MustExec(fmt.Sprintf("revoke %s from 'user1'@'%%'", priv))
err = tk1.ExecToErr("show placement for table test.t1")
require.EqualError(t, err, core.ErrTableaccessDenied.GenWithStackByArgs("SHOW", "user1", "%", "t1").Error())
tk1.MustQuery("show placement").Check(testkit.Rows(
"POLICY p1 PRIMARY_REGION=\"cn-east-1\" REGIONS=\"cn-east-1,cn-east-2\" SCHEDULE=\"EVEN\" NULL",
"POLICY p2 LEADER_CONSTRAINTS=\"[+region=bj]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=sh]\" NULL",
))
}
}