|
1 | 1 | import io |
| 2 | +from unittest.mock import MagicMock |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | from databricks.sdk.errors import NotFound |
5 | | -from databricks.sdk.service.iam import User |
| 6 | +from databricks.sdk.service.iam import ComplexValue, User |
| 7 | +from databricks.sdk.service.jobs import ( |
| 8 | + BaseRun, |
| 9 | + RunLifeCycleState, |
| 10 | + RunResultState, |
| 11 | + RunState, |
| 12 | +) |
6 | 13 |
|
7 | 14 | from databricks.labs.ucx.framework.parallel import ManyError |
| 15 | +from databricks.labs.ucx.install import WorkspaceInstaller |
8 | 16 | from databricks.labs.ucx.installer import InstallationManager |
9 | 17 |
|
10 | 18 |
|
@@ -53,3 +61,62 @@ def test_corrupt_config(mocker): |
53 | 61 | installation_manager = InstallationManager(ws) |
54 | 62 | user_installations = installation_manager.user_installations() |
55 | 63 | assert len(user_installations) == 0 |
| 64 | + |
| 65 | + |
| 66 | +def test_validate_assessment(mocker): |
| 67 | + ws = mocker.patch("databricks.sdk.WorkspaceClient.__init__") |
| 68 | + current_user = MagicMock() |
| 69 | + current_user.me.return_value = User(user_name="foo", groups=[ComplexValue(display="admins")]) |
| 70 | + |
| 71 | + state = MagicMock() |
| 72 | + state.jobs = {"assessment": 123} |
| 73 | + |
| 74 | + ws.current_user = current_user |
| 75 | + ws.jobs.list_runs.return_value = [ |
| 76 | + BaseRun(run_id=123, state=RunState(result_state=RunResultState.SUCCESS)), |
| 77 | + BaseRun(run_id=111, state=RunState(result_state=RunResultState.FAILED)), |
| 78 | + ] |
| 79 | + ws.jobs.wait_get_run_job_terminated_or_skipped = MagicMock(return_value=None) |
| 80 | + installation_manager = WorkspaceInstaller(ws) |
| 81 | + installation_manager._state = state |
| 82 | + |
| 83 | + assert installation_manager.validate_step("assessment") |
| 84 | + |
| 85 | + ws.jobs.list_runs.return_value = [ |
| 86 | + BaseRun(run_id=123, state=RunState(result_state=RunResultState.FAILED)), |
| 87 | + BaseRun(run_id=111, state=RunState(result_state=RunResultState.FAILED)), |
| 88 | + ] |
| 89 | + |
| 90 | + assert not installation_manager.validate_step("assessment") |
| 91 | + |
| 92 | + ws.jobs.list_runs.return_value = [ |
| 93 | + BaseRun(run_id=123, state=RunState(result_state=RunResultState.FAILED)), |
| 94 | + BaseRun(run_id=111, state=RunState(life_cycle_state=RunLifeCycleState.RUNNING)), |
| 95 | + ] |
| 96 | + |
| 97 | + installation_manager.validate_step("assessment") |
| 98 | + ws.jobs.wait_get_run_job_terminated_or_skipped.assert_called() |
| 99 | + |
| 100 | + |
| 101 | +def test_validate_run_assessment(mocker): |
| 102 | + ws = mocker.patch("databricks.sdk.WorkspaceClient.__init__") |
| 103 | + current_user = MagicMock() |
| 104 | + current_user.me.return_value = User(user_name="foo", groups=[ComplexValue(display="admins")]) |
| 105 | + |
| 106 | + state = MagicMock() |
| 107 | + state.jobs = {"assessment": 123} |
| 108 | + |
| 109 | + ws.current_user = current_user |
| 110 | + installation_manager = WorkspaceInstaller(ws) |
| 111 | + installation_manager._state = state |
| 112 | + installation_manager.validate_step = MagicMock(return_value=True) |
| 113 | + # Test a use case where assessment ran successfully |
| 114 | + installation_manager.validate_and_run("assessment") |
| 115 | + installation_manager.validate_step.assert_called_with("assessment") |
| 116 | + |
| 117 | + # Test a use case where assessment didn't run successfully |
| 118 | + installation_manager.run_workflow = MagicMock() |
| 119 | + installation_manager.validate_step = MagicMock(return_value=False) |
| 120 | + installation_manager.validate_and_run("assessment") |
| 121 | + installation_manager.validate_step.assert_called_with("assessment") |
| 122 | + installation_manager.run_workflow.assert_called_with("assessment") |
0 commit comments