Skip to content

Commit

Permalink
test_python_model.py::TestSecrets::test_secrets is failing in some …
Browse files Browse the repository at this point in the history
…scenarios (#1096)

* attempt adding sleep to allow secrets to build to fix test

* increase timeout

* attempt_using uuid to make instances different

* add manual drop statements

* attach suffix to all variables and drop all with suffix

* update missed ref to test_secret

* remove unused import

* pull out variables for secrets to gloabls so they can also be applied to the python model fixture
  • Loading branch information
McKnight-42 authored Jun 28, 2024
1 parent 2576a08 commit 220f087
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions tests/functional/adapter/test_python_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import uuid
from dbt.tests.util import run_dbt, write_file
from dbt.tests.adapter.python_model.test_python_model import (
BasePythonModelTests,
Expand Down Expand Up @@ -174,20 +175,24 @@ def test_external_access_integration(self, project):
run_dbt(["run"])


SECRETS_MODE = """
TEST_RUN_ID = uuid.uuid4().hex
TEST_SECRET = f"test_secret_{TEST_RUN_ID}"
TEST_NETWORK_RULE = f"test_network_rule_{TEST_RUN_ID}"
TEST_EXTERNAL_ACCESS_INTEGRATION = f"test_external_access_integration_{TEST_RUN_ID}"
SECRETS_MODE = f"""
import pandas
import snowflake.snowpark as snowpark
def model(dbt, session: snowpark.Session):
dbt.config(
materialized="table",
secrets={"secret_variable_name": "test_secret"},
external_access_integrations=["test_external_access_integration"],
secrets={{"secret_variable_name": "{TEST_SECRET}"}},
external_access_integrations=["{TEST_EXTERNAL_ACCESS_INTEGRATION}"],
)
import _snowflake
return session.create_dataframe(
pandas.DataFrame(
[{"secret_value": _snowflake.get_generic_secret_string('secret_variable_name')}]
[{{"secret_value": _snowflake.get_generic_secret_string('secret_variable_name')}}]
)
)
"""
Expand All @@ -198,23 +203,29 @@ class TestSecrets:
def models(self):
return {"secret_python_model.py": SECRETS_MODE}

# This test can be flaky because of delays in the integration being set up before trying to use it.
@pytest.fixture(scope="class")
def profiles_config_update(self):
return {"retry_all": True, "connect_retries": 3}

def test_secrets(self, project):
project.run_sql(
"create or replace secret test_secret type = generic_string secret_string='secret value';"
f"create or replace secret {TEST_SECRET} type = generic_string secret_string='secret value';"
)

# The secrets you specify as values must also be specified in the external access integration.
# See https://docs.snowflake.com/en/developer-guide/external-network-access/creating-using-external-network-access#using-the-external-access-integration-in-a-function-or-procedure

project.run_sql(
"create or replace network rule test_network_rule type = host_port mode = egress value_list= ('www.google.com:443');"
f"create or replace network rule {TEST_NETWORK_RULE} type = host_port mode = egress value_list= ('www.google.com:443');"
)

project.run_sql(
"create or replace external access integration test_external_access_integration allowed_network_rules = (test_network_rule) allowed_authentication_secrets = (test_secret) enabled = true;"
f"create or replace external access integration {TEST_EXTERNAL_ACCESS_INTEGRATION} "
f"allowed_network_rules = ({TEST_NETWORK_RULE}) "
f"allowed_authentication_secrets = ({TEST_SECRET}) enabled = true;"
)

run_dbt(["run"])

project.run_sql(f"drop secret if exists {TEST_SECRET};")
project.run_sql(f"drop network rule if exists {TEST_NETWORK_RULE};")
project.run_sql(
f"drop external access integration if exists {TEST_EXTERNAL_ACCESS_INTEGRATION};"
)

0 comments on commit 220f087

Please sign in to comment.