2020-10-15 20:23:49 +00:00
|
|
|
import uuid
|
2020-10-23 22:59:38 +00:00
|
|
|
|
2021-05-07 13:14:40 +00:00
|
|
|
import testflows.settings as settings
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2020-10-23 22:59:38 +00:00
|
|
|
from testflows.core.name import basename, parentname
|
|
|
|
from testflows._core.testtype import TestSubType
|
2020-10-15 20:23:49 +00:00
|
|
|
from testflows.core import *
|
|
|
|
|
2022-01-22 09:41:54 +00:00
|
|
|
from helpers.common import instrument_clickhouse_server_log
|
2020-10-15 20:23:49 +00:00
|
|
|
from rbac.helper.tables import table_types
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
def permutations(table_count=1):
|
2022-03-22 16:39:58 +00:00
|
|
|
return [*range((1 << table_count) - 1)]
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
|
|
|
|
def getuid():
|
2020-10-23 22:59:38 +00:00
|
|
|
if current().subtype == TestSubType.Example:
|
2022-03-22 16:39:58 +00:00
|
|
|
testname = (
|
|
|
|
f"{basename(parentname(current().name)).replace(' ', '_').replace(',','')}"
|
|
|
|
)
|
2020-10-21 22:04:09 +00:00
|
|
|
else:
|
|
|
|
testname = f"{basename(current().name).replace(' ', '_').replace(',','')}"
|
2022-03-22 16:39:58 +00:00
|
|
|
return testname + "_" + str(uuid.uuid1()).replace("-", "_")
|
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def table(node, name, table_type_name="MergeTree"):
|
|
|
|
table_type = table_types[table_type_name]
|
|
|
|
try:
|
|
|
|
names = name.split(",")
|
|
|
|
for name in names:
|
|
|
|
with Given(f"I have {name} with engine {table_type_name}"):
|
|
|
|
node.query(f"DROP TABLE IF EXISTS {name}")
|
|
|
|
node.query(table_type.create_statement.format(name=name))
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for name in names:
|
|
|
|
with Finally(f"I drop the table {name}"):
|
|
|
|
if table_type.cluster:
|
2022-03-22 16:39:58 +00:00
|
|
|
node.query(
|
|
|
|
f"DROP TABLE IF EXISTS {name} ON CLUSTER {table_type.cluster}"
|
|
|
|
)
|
2020-10-15 20:23:49 +00:00
|
|
|
else:
|
|
|
|
node.query(f"DROP TABLE IF EXISTS {name}")
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
@contextmanager
|
|
|
|
def user(node, name):
|
|
|
|
try:
|
|
|
|
names = name.split(",")
|
|
|
|
for name in names:
|
|
|
|
with Given("I have a user"):
|
|
|
|
node.query(f"CREATE USER OR REPLACE {name}")
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for name in names:
|
|
|
|
with Finally("I drop the user"):
|
|
|
|
node.query(f"DROP USER IF EXISTS {name}")
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
@contextmanager
|
|
|
|
def role(node, role):
|
|
|
|
try:
|
|
|
|
roles = role.split(",")
|
|
|
|
for role in roles:
|
|
|
|
with Given("I have a role"):
|
|
|
|
node.query(f"CREATE ROLE OR REPLACE {role}")
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for role in roles:
|
|
|
|
with Finally("I drop the role"):
|
|
|
|
node.query(f"DROP ROLE IF EXISTS {role}")
|
2021-03-01 16:29:42 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2021-03-01 16:29:42 +00:00
|
|
|
@TestStep(Given)
|
|
|
|
def row_policy(self, name, table, node=None):
|
2022-03-22 16:39:58 +00:00
|
|
|
"""Create a row policy with a given name on a given table."""
|
2021-03-01 16:29:42 +00:00
|
|
|
if node is None:
|
|
|
|
node = self.context.node
|
|
|
|
|
|
|
|
try:
|
|
|
|
with Given(f"I create row policy {name}"):
|
|
|
|
node.query(f"CREATE ROW POLICY {name} ON {table}")
|
|
|
|
yield
|
|
|
|
|
|
|
|
finally:
|
|
|
|
with Finally(f"I delete row policy {name}"):
|
|
|
|
node.query(f"DROP ROW POLICY IF EXISTS {name} ON {table}")
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
tables = {
|
2022-03-22 16:39:58 +00:00
|
|
|
"table0": 1 << 0,
|
|
|
|
"table1": 1 << 1,
|
|
|
|
"table2": 1 << 2,
|
|
|
|
"table3": 1 << 3,
|
|
|
|
"table4": 1 << 4,
|
|
|
|
"table5": 1 << 5,
|
|
|
|
"table6": 1 << 6,
|
|
|
|
"table7": 1 << 7,
|
2020-10-15 20:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
@contextmanager
|
|
|
|
def grant_select_on_table(node, grants, target_name, *table_names):
|
|
|
|
try:
|
|
|
|
tables_granted = []
|
|
|
|
for table_number in range(len(table_names)):
|
2020-11-13 07:59:50 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
if grants & tables[f"table{table_number}"]:
|
2020-11-13 07:59:50 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
with When(f"I grant select privilege on {table_names[table_number]}"):
|
2022-03-22 16:39:58 +00:00
|
|
|
node.query(
|
|
|
|
f"GRANT SELECT ON {table_names[table_number]} TO {target_name}"
|
|
|
|
)
|
2020-11-13 07:59:50 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
tables_granted.append(f"{table_names[table_number]}")
|
2020-11-13 07:59:50 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
yield (", ").join(tables_granted)
|
2020-11-13 07:59:50 +00:00
|
|
|
|
2020-10-15 20:23:49 +00:00
|
|
|
finally:
|
|
|
|
for table_number in range(len(table_names)):
|
2022-03-22 16:39:58 +00:00
|
|
|
with Finally(
|
|
|
|
f"I revoke the select privilege on {table_names[table_number]}"
|
|
|
|
):
|
|
|
|
node.query(
|
|
|
|
f"REVOKE SELECT ON {table_names[table_number]} FROM {target_name}"
|
|
|
|
)
|