Skip to content

Commit d7fb040

Browse files
authored
Use watchdog timeout to catalog properties (#48)
## Changes Current implementation does not mark catalogs as being in use for the watchdog to skip. This PR fills the gap by adding a RemoveAfter entry to catalog properties ### Linked issues PR https://github.com/databrickslabs/watchdog/pull/58 ### Tests Not tested --------- Co-authored-by: Eric Vergnaud <[email protected]>
1 parent 53f69c0 commit d7fb040

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Create and provide a SQL backend for executing statements.
330330

331331
Requires the environment variable `DATABRICKS_WAREHOUSE_ID` to be set.
332332

333-
See also [`make_catalog`](#make_catalog-fixture), [`make_schema`](#make_schema-fixture), [`make_table`](#make_table-fixture), [`make_udf`](#make_udf-fixture), [`sql_exec`](#sql_exec-fixture), [`sql_fetch_all`](#sql_fetch_all-fixture), [`ws`](#ws-fixture), [`env_or_skip`](#env_or_skip-fixture).
333+
See also [`make_schema`](#make_schema-fixture), [`make_table`](#make_table-fixture), [`make_udf`](#make_udf-fixture), [`sql_exec`](#sql_exec-fixture), [`sql_fetch_all`](#sql_fetch_all-fixture), [`ws`](#ws-fixture), [`env_or_skip`](#env_or_skip-fixture).
334334

335335

336336
[[back to top](#python-testing-for-databricks)]
@@ -815,7 +815,7 @@ def test_catalog_fixture(make_catalog, make_schema, make_table):
815815
logger.info(f"Created new schema: {from_table_1}")
816816
```
817817

818-
See also [`ws`](#ws-fixture), [`sql_backend`](#sql_backend-fixture), [`make_random`](#make_random-fixture).
818+
See also [`ws`](#ws-fixture), [`make_random`](#make_random-fixture), [`watchdog_remove_after`](#watchdog_remove_after-fixture).
819819

820820

821821
[[back to top](#python-testing-for-databricks)]
@@ -1104,7 +1104,7 @@ See also [`ws`](#ws-fixture).
11041104
### `watchdog_remove_after` fixture
11051105
Purge time for test objects, representing the (UTC-based) hour from which objects may be purged.
11061106

1107-
See also [`make_cluster`](#make_cluster-fixture), [`make_instance_pool`](#make_instance_pool-fixture), [`make_job`](#make_job-fixture), [`make_model`](#make_model-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_schema`](#make_schema-fixture), [`make_serving_endpoint`](#make_serving_endpoint-fixture), [`make_table`](#make_table-fixture), [`make_warehouse`](#make_warehouse-fixture), [`watchdog_purge_suffix`](#watchdog_purge_suffix-fixture).
1107+
See also [`make_catalog`](#make_catalog-fixture), [`make_cluster`](#make_cluster-fixture), [`make_instance_pool`](#make_instance_pool-fixture), [`make_job`](#make_job-fixture), [`make_model`](#make_model-fixture), [`make_pipeline`](#make_pipeline-fixture), [`make_query`](#make_query-fixture), [`make_schema`](#make_schema-fixture), [`make_serving_endpoint`](#make_serving_endpoint-fixture), [`make_table`](#make_table-fixture), [`make_warehouse`](#make_warehouse-fixture), [`watchdog_purge_suffix`](#watchdog_purge_suffix-fixture).
11081108

11091109

11101110
[[back to top](#python-testing-for-databricks)]

src/databricks/labs/pytester/fixtures/catalog.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
22
from collections.abc import Generator, Callable
3+
from unittest.mock import Mock
4+
35
from pytest import fixture
46
from databricks.labs.blueprint.commands import CommandExecutor
57
from databricks.sdk.errors import DatabricksError
@@ -278,7 +280,9 @@ def remove(schema_info: SchemaInfo):
278280

279281

280282
@fixture
281-
def make_catalog(ws, sql_backend, make_random, log_workspace_link) -> Generator[Callable[..., CatalogInfo], None, None]:
283+
def make_catalog(
284+
ws, make_random, watchdog_remove_after, log_workspace_link
285+
) -> Generator[Callable[..., CatalogInfo], None, None]:
282286
"""
283287
Create a catalog and return its info. Remove it after the test.
284288
Returns instance of `databricks.sdk.service.catalog.CatalogInfo`.
@@ -294,11 +298,10 @@ def test_catalog_fixture(make_catalog, make_schema, make_table):
294298
"""
295299

296300
def create() -> CatalogInfo:
297-
# Warning: As of 2024-09-04 there is no way to mark this catalog for protection against the watchdog.
298-
# Ref: https://github.com/databrickslabs/watchdog/blob/cdc97afdac1567e89d3b39d938f066fd6267b3ba/scan/objects/uc.go#L68
299301
name = f"dummy_C{make_random(4)}".lower()
300-
sql_backend.execute(f"CREATE CATALOG {name}")
301-
catalog_info = ws.catalogs.get(name)
302+
catalog_info = ws.catalogs.create(name=name, properties={"RemoveAfter": watchdog_remove_after})
303+
if isinstance(catalog_info, Mock):
304+
catalog_info.name = name
302305
log_workspace_link(f'{name} catalog', f'explore/data/{name}')
303306
return catalog_info
304307

tests/unit/fixtures/test_catalog.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ def test_make_table_custom_schema():
102102

103103

104104
def test_make_catalog():
105-
ctx, _ = call_stateful(make_catalog)
106-
107-
ctx['ws'].catalogs.get.assert_called_with('dummy_crandom')
108-
109-
assert ctx['sql_backend'].queries == ['CREATE CATALOG dummy_crandom']
105+
ctx, info = call_stateful(make_catalog)
106+
ctx['ws'].catalogs.create.assert_called() # can't specify call params accurately
107+
assert info.properties and info.properties.get("RemoveAfter", None)
110108

111109

112110
def test_make_udf():

0 commit comments

Comments
 (0)