Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmeireles committed Jan 9, 2025
1 parent e8d3a6c commit 780c23d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .github/scripts/create_env_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ DREMIO_DATABASE=dbt_test
DBT_TEST_USER_1=dbt_test_user_1
DBT_TEST_USER_2=dbt_test_user_2
DBT_TEST_USER_3=dbt_test_user_3
DBT_TEST_ROLE_1=PUBLIC
DBT_TEST_ROLE_2=ADMIN
EOF

echo ".env file created successfully."
34 changes: 18 additions & 16 deletions tests/functional/adapter/grants/test_model_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class TestViewGrantsDremio(BaseGrantsDremio, BaseModelGrants):

# Overridden to only include view materialization
def test_view_table_grants(self, project, get_test_users):
# we want the test to fail, not silently skip
Expand All @@ -46,9 +45,10 @@ def test_view_table_grants(self, project, get_test_users):
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
expected = {select_privilege_name: [test_users[0]]}
expected_grants = {select_privilege_name: ["user: " + test_users[0]]}
assert model.config.grants == expected
assert model.config.materialized == "view"
self.assert_expected_grants_match_actual(project, "my_model", expected)
self.assert_expected_grants_match_actual(project, "my_model", expected_grants)

# View materialization, change select grant user
updated_yaml = self.interpolate_name_overrides(user2_model_schema_yml)
Expand All @@ -74,6 +74,7 @@ def get_grants_on_relation(self, project, relation_name):

# Overridden to only include table materializations
def test_view_table_grants(self, project, get_test_users):
# we want the test to fail, not silently skip
test_users = get_test_users
select_privilege_name = self.privilege_grantee_name_overrides()["select"]
insert_privilege_name = self.privilege_grantee_name_overrides()["insert"]
Expand All @@ -87,7 +88,9 @@ def test_view_table_grants(self, project, get_test_users):
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
assert model.config.materialized == "table"
expected = {select_privilege_name: [test_users[0]]}
expected = {select_privilege_name: ["user:" + test_users[0]]}
project.adapter.config.credentials.database = "dbt_test_source"
project.adapter.config.credentials.schema = "dbtdremios3" + "." + project.created_schemas[0]
self.assert_expected_grants_match_actual(project, "my_model", expected)

# Table materialization, change select grant user
Expand All @@ -98,34 +101,33 @@ def test_view_table_grants(self, project, get_test_users):
manifest = get_manifest(project.project_root)
model = manifest.nodes[model_id]
assert model.config.materialized == "table"
expected = {select_privilege_name: [test_users[1]]}
expected = {select_privilege_name: ["user:" + test_users[1]]}
project.adapter.config.credentials.database = "dbt_test_source"
project.adapter.config.credentials.schema = "dbtdremios3" + "." + project.created_schemas[0]
self.assert_expected_grants_match_actual(project, "my_model", expected)

# Table materialization, multiple grantees
updated_yaml = self.interpolate_name_overrides(
multiple_users_table_model_schema_yml
)
updated_yaml = self.interpolate_name_overrides(multiple_users_table_model_schema_yml)
write_file(updated_yaml, project.project_root, "models", "schema.yml")
(results, log_output) = run_dbt_and_capture(["--debug", "run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model = manifest.nodes[model_id]
assert model.config.materialized == "table"
expected = {select_privilege_name: [test_users[0], test_users[1]]}
expected = {select_privilege_name: ["user:" + test_users[0], "user:" + test_users[1]]}
project.adapter.config.credentials.database = "dbt_test_source"
project.adapter.config.credentials.schema = "dbtdremios3" + "." + project.created_schemas[0]
self.assert_expected_grants_match_actual(project, "my_model", expected)

# Table materialization, multiple privileges
updated_yaml = self.interpolate_name_overrides(
multiple_privileges_table_model_schema_yml
)
updated_yaml = self.interpolate_name_overrides(multiple_privileges_table_model_schema_yml)
write_file(updated_yaml, project.project_root, "models", "schema.yml")
(results, log_output) = run_dbt_and_capture(["--debug", "run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model = manifest.nodes[model_id]
assert model.config.materialized == "table"
expected = {
select_privilege_name: [test_users[0]],
insert_privilege_name: [test_users[1]],
}
self.assert_expected_grants_match_actual(project, "my_model", expected)
expected = {select_privilege_name: ["user:" + test_users[0]], insert_privilege_name: ["user:" + test_users[1]]}
project.adapter.config.credentials.database = "dbt_test_source"
project.adapter.config.credentials.schema = "dbtdremios3" + "." + project.created_schemas[0]
self.assert_expected_grants_match_actual(project, "my_model", expected)
115 changes: 115 additions & 0 deletions tests/functional/adapter/grants/test_roles_model_grants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import pytest, os
from dbt.tests.adapter.grants.test_model_grants import (
BaseModelGrants,
user2_model_schema_yml,
user2_table_model_schema_yml,
table_model_schema_yml,
multiple_users_table_model_schema_yml,
multiple_privileges_table_model_schema_yml,
)
from dbt.tests.util import (
run_dbt,
get_manifest,
write_file,
get_connection,
run_dbt_and_capture
)
from tests.functional.adapter.grants.base_grants import BaseGrantsDremio
from tests.utils.util import relation_from_name

TEST_ROLE_ENV_VARS = ["DBT_TEST_ROLE_1", "DBT_TEST_ROLE_2"]

my_model_sql = """
select 1 as fun
"""

role1_model_schema_yml = """
version: 2
models:
- name: my_model
config:
grants:
select: ["role:{{ env_var('DBT_TEST_ROLE_1') }}"]
"""

multiple_roles_model_schema_yml = """
version: 2
models:
- name: my_model
config:
grants:
select: ["role:{{ env_var('DBT_TEST_ROLE_1') }}", "role:{{ env_var('DBT_TEST_ROLE_2') }}"]
"""

users_and_roles_model_schema_yml = """
version: 2
models:
- name: my_model
config:
grants:
select: ["user:{{ env_var('DBT_TEST_USER_1') }}", "user:{{ env_var('DBT_TEST_USER_2') }}", "role:{{ env_var('DBT_TEST_ROLE_1') }}", "role:{{ env_var('DBT_TEST_ROLE_2') }}"]
"""

class TestViewRolesGrantsDremio(BaseGrantsDremio, BaseModelGrants):
@pytest.fixture(scope="class", autouse=True)
def get_test_roles(self, project):
test_roles = []
for env_var in TEST_ROLE_ENV_VARS:
role = os.getenv(env_var)
if role:
test_roles.append(role)
return test_roles

def test_view_role1_grants(self, project, get_test_roles):
test_roles = get_test_roles
select_privilege_name = self.privilege_grantee_name_overrides()["select"]
assert len(test_roles) == 2

# View materialization, single select grant
updated_yaml = self.interpolate_name_overrides(role1_model_schema_yml)
write_file(updated_yaml, project.project_root, "models", "schema.yml")
(results, log_output) = run_dbt_and_capture(["--debug", "run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
assert model.config.materialized == "view"
expected = {select_privilege_name: ["role:" + test_roles[0]]}
self.assert_expected_grants_match_actual(project, "my_model", expected)

def test_view_multiple_roles_grants(self, project, get_test_roles):
test_roles = get_test_roles
select_privilege_name = self.privilege_grantee_name_overrides()["select"]
assert len(test_roles) == 2

# View materialization, single select grant
updated_yaml = self.interpolate_name_overrides(multiple_roles_model_schema_yml)
write_file(updated_yaml, project.project_root, "models", "schema.yml")
(results, log_output) = run_dbt_and_capture(["--debug", "run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
assert model.config.materialized == "view"
expected = {select_privilege_name: ["role:" + test_roles[0], "role:" + test_roles[1]]}
self.assert_expected_grants_match_actual(project, "my_model", expected)

def test_view_multiple_users_and_roles(self, project, get_test_users, get_test_roles):
test_users = get_test_users
assert len(test_users) == 3
test_roles = get_test_roles
assert len(test_roles) == 2

select_privilege_name = self.privilege_grantee_name_overrides()["select"]

# View materialization, multiple select grants
updated_yaml = self.interpolate_name_overrides(users_and_roles_model_schema_yml)
write_file(updated_yaml, project.project_root, "models", "schema.yml")
(results, log_output) = run_dbt_and_capture(["--debug", "run"])
assert len(results) == 1
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model"
model = manifest.nodes[model_id]
assert model.config.materialized == "view"
expected = {select_privilege_name: ["user:" + test_users[0], "user:" + test_users[1], "role:" + test_roles[0], "role:" + test_roles[1]]}
self.assert_expected_grants_match_actual(project, "my_model", expected)

0 comments on commit 780c23d

Please sign in to comment.