Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved test coverage for installer #371

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/databricks/labs/ucx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ def _current_config(self):
def _name(self, name: str) -> str:
return f"[{self._prefix.upper()}][{self._short_name}] {name}"

def _configure_inventory_database(self):
counter = 0
inventory_database = None
while True:
inventory_database = self._question("Inventory Database stored in hive_metastore", default="ucx")
if re.match(r"^\w+$", inventory_database):
break
else:
print(f"{inventory_database} is not a valid database name")
counter = counter + 1
if counter > NUM_USER_ATTEMPTS:
msg = "Exceeded max tries to get a valid database name, try again later."
raise SystemExit(msg)
return inventory_database

def _configure(self):
ws_file_url = self._notebook_link(self._config_file)
try:
Expand All @@ -180,17 +195,7 @@ def _configure(self):
raise err

logger.info("Please answer a couple of questions to configure Unity Catalog migration")
counter = 0
while True:
inventory_database = self._question("Inventory Database stored in hive_metastore", default="ucx")
if re.match(r"^\w+$", inventory_database):
break
else:
print(f"{inventory_database} is not a valid database name")
counter = counter + 1
if counter > NUM_USER_ATTEMPTS:
msg = "Exceeded max tries to get a valid database name, try again later."
raise SystemExit(msg)
inventory_database = self._configure_inventory_database()

pro_warehouses = {"[Create new PRO SQL warehouse]": "create_new"} | {
f"{_.name} ({_.id}, {_.warehouse_type.value}, {_.state.value})": _.id
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,54 @@
from databricks.labs.ucx.install import WorkspaceInstaller


def mock_ws(mocker):
ws = mocker.Mock()
ws = mocker.patch("databricks.sdk.WorkspaceClient.__init__")

ws.current_user.me = lambda: iam.User(user_name="me@example.com", groups=[iam.ComplexValue(display="admins")])
ws.config.host = "https://foo"
ws.config.is_aws = True
config_bytes = yaml.dump(WorkspaceConfig(inventory_database="a", groups=GroupsConfig(auto=True)).as_dict()).encode(
"utf8"
)
ws.workspace.download = lambda _: io.BytesIO(config_bytes)
ws.workspace.get_status = lambda _: ObjectInfo(object_id=123)
ws.data_sources.list = lambda: [DataSource(id="bcd", warehouse_id="abc")]
ws.warehouses.list = lambda **_: [EndpointInfo(id="abc", warehouse_type=EndpointInfoWarehouseType.PRO)]
ws.dashboards.create.return_value = Dashboard(id="abc")
ws.queries.create.return_value = Query(id="abc")
ws.query_visualizations.create.return_value = Visualization(id="abc")
ws.dashboard_widgets.create.return_value = Widget(id="abc")
return ws


def test_run_for_config(mocker, tmp_path):
# run_for_config(ws: WorkspaceClient, config: WorkspaceConfig, *, prefix="ucx") -> "WorkspaceInstaller":
ws = mock_ws(mocker)

install = WorkspaceInstaller(ws)
wc = WorkspaceConfig(inventory_database="a", groups=GroupsConfig(auto=True))
return_value = install.run_for_config(ws, wc)
assert return_value


def test_install_database_happy(mocker, tmp_path):
ws = mocker.Mock()
install = WorkspaceInstaller(ws)
mocker.patch("builtins.input", return_value="ucx")
res = install._configure_inventory_database()
assert "ucx" == res


def test_install_database_unhappy(mocker, tmp_path):
ws = mocker.Mock()
install = WorkspaceInstaller(ws)
mocker.patch("builtins.input", return_value="main.ucx")

with pytest.raises(SystemExit):
install._configure_inventory_database()


def test_build_wheel(mocker, tmp_path):
ws = mocker.Mock()
install = WorkspaceInstaller(ws)
Expand Down
Loading