Skip to content

Commit 3d89c45

Browse files
authored
Update unit tests to configure log-capturing when verifying logs (#4030)
## Changes This PR updates unit tests that verify logs to ensure they set up log-capturing to capture the level of the logs they're looking for. Previously this wasn't the case, and they relied on the underlying logging configuration have a level that would emit the record. ### Linked issues Relates databrickslabs/blueprint#238. ### Tests - updated unit tests
1 parent 69f7f37 commit 3d89c45

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

tests/unit/test_cli.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ def get_workspace_client(workspace: Workspace) -> WorkspaceClient:
182182
return acc_client
183183

184184

185-
def test_workflow(ws, caplog):
186-
workflows(ws)
185+
def test_workflow(ws, caplog) -> None:
186+
with caplog.at_level(logging.INFO, logger='databricks.labs.ucx'):
187+
workflows(ws)
187188
assert "Fetching deployed jobs..." in caplog.messages
188189
ws.jobs.list_runs.assert_called()
189190

@@ -419,7 +420,7 @@ def test_no_step_in_repair_run(ws, caplog):
419420
assert '--step is a required parameter' in caplog.messages
420421

421422

422-
def test_revert_migrated_tables(ws, caplog):
423+
def test_revert_migrated_tables(ws, caplog) -> None:
423424
# test with no schema and no table, user confirm to not retry
424425
prompts = MockPrompts({'.*': 'no'})
425426
ctx = WorkspaceContext(ws).replace(
@@ -432,7 +433,8 @@ def test_revert_migrated_tables(ws, caplog):
432433

433434
# test with no schema and no table, user confirm to retry, but no ucx installation found
434435
prompts = MockPrompts({'.*': 'yes'})
435-
assert revert_migrated_tables(ws, prompts, schema=None, table=None, ctx=ctx) is None
436+
with caplog.at_level(logging.INFO, logger='databricks.labs.ucx.hive_metastore'):
437+
assert revert_migrated_tables(ws, prompts, schema=None, table=None, ctx=ctx) is None
436438
assert 'No migrated tables were found.' in caplog.messages
437439

438440

@@ -902,22 +904,24 @@ def test_create_catalogs_schemas_handles_existing(ws, caplog) -> None:
902904
ws.schemas.get.assert_called()
903905

904906

905-
def test_cluster_remap(ws, caplog):
907+
def test_cluster_remap(ws, caplog) -> None:
906908
prompts = MockPrompts({"Please provide the cluster id's as comma separated value from the above list.*": "1"})
907909
ws.clusters.get.return_value = ClusterDetails(cluster_id="123", cluster_name="test_cluster")
908910
ws.clusters.list.return_value = [
909911
ClusterDetails(cluster_id="123", cluster_name="test_cluster", cluster_source=ClusterSource.UI),
910912
ClusterDetails(cluster_id="1234", cluster_name="test_cluster1", cluster_source=ClusterSource.JOB),
911913
]
912-
cluster_remap(ws, prompts)
914+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx"):
915+
cluster_remap(ws, prompts)
913916
assert "Remapping the Clusters to UC" in caplog.messages
914917

915918

916-
def test_cluster_remap_error(ws, caplog):
919+
def test_cluster_remap_error(ws, caplog) -> None:
917920
prompts = MockPrompts({"Please provide the cluster id's as comma separated value from the above list.*": "1"})
918921
ws.clusters.list.return_value = []
919922
cluster_remap(ws, prompts)
920-
assert "No cluster information present in the workspace" in caplog.messages
923+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx"):
924+
assert "No cluster information present in the workspace" in caplog.messages
921925

922926

923927
def test_revert_cluster_remap(caplog):
@@ -929,9 +933,10 @@ def test_revert_cluster_remap(caplog):
929933
revert_cluster_remap(workspace_client, prompts)
930934

931935

932-
def test_revert_cluster_remap_empty(ws, caplog):
936+
def test_revert_cluster_remap_empty(ws, caplog) -> None:
933937
prompts = MockPrompts({"Please provide the cluster id's as comma separated value from the above list.*": "1"})
934-
revert_cluster_remap(ws, prompts)
938+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx"):
939+
revert_cluster_remap(ws, prompts)
935940
assert "There is no cluster files in the backup folder. Skipping the reverting process" in caplog.messages
936941
ws.workspace.list.assert_called_once()
937942

@@ -945,7 +950,8 @@ def test_relay_logs(ws, caplog) -> None:
945950
],
946951
[ObjectInfo(path='/Users/foo/.ucx/logs/run-123-1/foo.log-123')],
947952
]
948-
logs(ws)
953+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx"):
954+
logs(ws)
949955
assert 'Something is logged' in caplog.messages
950956

951957

@@ -965,16 +971,17 @@ def test_migrate_local_code(ws) -> None:
965971
mock_apply.assert_called_once_with(Path.cwd())
966972

967973

968-
def test_show_all_metastores(acc_client, caplog):
969-
show_all_metastores(acc_client)
974+
def test_show_all_metastores(acc_client, caplog) -> None:
975+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx.account"):
976+
show_all_metastores(acc_client)
970977
assert 'Matching metastores are:' in caplog.messages
971978

972979

973980
def test_assign_metastore_logs_account_id_and_assigns_metastore(caplog, acc_client) -> None:
974981
ctx = AccountContext(acc_client)
975982
acc_client.metastores.list.return_value = [MetastoreInfo(name="test", metastore_id="123")]
976983

977-
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx.cli"):
984+
with caplog.at_level(logging.INFO, logger="databricks.labs.ucx"):
978985
assign_metastore(acc_client, "456", ctx=ctx)
979986

980987
assert "Account ID: 123" in caplog.messages

0 commit comments

Comments
 (0)