-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test secrets on all platforms
- Loading branch information
Showing
5 changed files
with
126 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import pytest | ||
import os | ||
|
||
from tests.utils import TEST_STORAGE_ROOT | ||
from tests.load.utils import ( | ||
destinations_configs, | ||
DestinationTestConfiguration, | ||
AWS_BUCKET, | ||
) | ||
from dlt.common.utils import uniq_id | ||
|
||
|
||
@pytest.mark.essential | ||
@pytest.mark.parametrize( | ||
"destination_config", | ||
destinations_configs(all_buckets_filesystem_configs=True, bucket_subset=(AWS_BUCKET,)), | ||
ids=lambda x: x.name, | ||
) | ||
def test_secrets_management( | ||
destination_config: DestinationTestConfiguration, capfd: pytest.CaptureFixture | ||
) -> None: | ||
"""Test the handling of secrets by the sql_client, we only need to do this on s3 | ||
as the other destinations work accordingly""" | ||
warning_mesage = "You are persisting duckdb secrets but are storing them in the default folder" | ||
|
||
pipeline = destination_config.setup_pipeline( | ||
"read_pipeline", | ||
dataset_name="read_test", | ||
) | ||
pipeline.run([1, 2, 3], table_name="items") | ||
|
||
import duckdb | ||
from duckdb import HTTPException | ||
from dlt.destinations.impl.filesystem.sql_client import ( | ||
FilesystemSqlClient, | ||
DuckDbCredentials, | ||
) | ||
|
||
duck_db_location = TEST_STORAGE_ROOT + "/" + uniq_id() | ||
secrets_dir = f"{TEST_STORAGE_ROOT}/duck_secrets_{uniq_id()}" | ||
|
||
def _external_duckdb_connection() -> duckdb.DuckDBPyConnection: | ||
external_db = duckdb.connect(duck_db_location) | ||
external_db.sql(f"SET secret_directory = '{secrets_dir}';") | ||
external_db.execute("CREATE SCHEMA IF NOT EXISTS first;") | ||
return external_db | ||
|
||
def _fs_sql_client_for_external_db( | ||
connection: duckdb.DuckDBPyConnection, | ||
) -> FilesystemSqlClient: | ||
return FilesystemSqlClient( | ||
dataset_name="second", | ||
fs_client=pipeline.destination_client(), # type: ignore | ||
credentials=DuckDbCredentials(connection), | ||
) | ||
|
||
def _secrets_exist() -> bool: | ||
return os.path.isdir(secrets_dir) and len(os.listdir(secrets_dir)) > 0 | ||
|
||
# first test what happens if there are no external secrets | ||
external_db = _external_duckdb_connection() | ||
fs_sql_client = _fs_sql_client_for_external_db(external_db) | ||
with fs_sql_client as sql_client: | ||
sql_client.create_views_for_tables({"items": "items"}) | ||
external_db.close() | ||
|
||
# no secrets will not work | ||
external_db = _external_duckdb_connection() | ||
with pytest.raises(HTTPException): | ||
assert len(external_db.sql("SELECT * FROM second.items").fetchall()) == 3 | ||
external_db.close() | ||
|
||
# add secrets and check that they are there | ||
external_db = _external_duckdb_connection() | ||
fs_sql_client = _fs_sql_client_for_external_db(external_db) | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.create_authentication(persistent=True) | ||
assert len(external_db.sql("SELECT * FROM second.items").fetchall()) == 3 | ||
assert _secrets_exist() | ||
|
||
# remove secrets and check that they are removed | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.drop_authentication() | ||
assert not _secrets_exist() | ||
external_db.close() | ||
|
||
# prevent creating persistent secrets on in mem databases | ||
fs_sql_client = FilesystemSqlClient( | ||
dataset_name="second", | ||
fs_client=pipeline.destination_client(), # type: ignore | ||
) | ||
with pytest.raises(Exception): | ||
with fs_sql_client as sql_client: | ||
fs_sql_client.create_authentication(persistent=True) | ||
|
||
# check that no warning was logged | ||
assert warning_mesage not in capfd.readouterr().err | ||
|
||
# check that warning is logged when secrets are persisted in the default folder | ||
duck_db_location = TEST_STORAGE_ROOT + "/" + uniq_id() | ||
secrets_dir = f"{TEST_STORAGE_ROOT}/duck_secrets_{uniq_id()}" | ||
duck_db = duckdb.connect(duck_db_location) | ||
fs_sql_client = _fs_sql_client_for_external_db(duck_db) | ||
with fs_sql_client as sql_client: | ||
sql_client.create_authentication(persistent=True) | ||
assert warning_mesage in capfd.readouterr().err |