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

Flip filesinrepos to true if false #42

Merged
merged 11 commits into from
Feb 13, 2024
11 changes: 11 additions & 0 deletions src/databricks/labs/blueprint/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def load(self, type_ref: type[T], *, filename: str | None = None) -> T:
the expected version using a method named `v{actual_version}_migrate` on the `type_ref` class. If the migration
is successful, the method will return the migrated object. If the migration is not successful, the method will
raise an `IllegalState` exception."""
self._enable_files_in_repos()
pritishpai marked this conversation as resolved.
Show resolved Hide resolved
filename = self._get_filename(filename, type_ref)
logger.debug(f"Loading {type_ref.__name__} from {filename}")
as_dict = self._load_content(filename)
Expand Down Expand Up @@ -669,6 +670,16 @@ def _load_csv(raw: BinaryIO) -> list[Json]:
out.append(row)
return out

def _enable_files_in_repos(self):
# check if "enableWorkspaceFilesystem" is set to false
workspace_file_system = self._ws.workspace_conf.get_status("enableWorkspaceFilesystem")

logger.debug("Checking Files In Repos configuration")

if workspace_file_system["enableWorkspaceFilesystem"] == "false":
logger.debug("enableWorkspaceFilesystem is False, enabling the config")
self._ws.workspace_conf.set_status({"enableWorkspaceFilesystem": "true"})


class MockInstallation(Installation):
"""Install state testing toolbelt
Expand Down
59 changes: 54 additions & 5 deletions tests/unit/test_installation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import random
from dataclasses import dataclass
from unittest.mock import create_autospec

Expand All @@ -18,6 +19,15 @@
)


@pytest.fixture()
def mock_install(mocker):
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
ws.workspace_conf.get_status = mocker.patch(
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)


def test_current_not_found():
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
Expand Down Expand Up @@ -173,7 +183,7 @@ def test_save_typed_file_array_csv():
)


def test_load_typed_file():
def test_load_typed_file(mocker):
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
ws.workspace.download.return_value = io.StringIO(
Expand All @@ -186,6 +196,9 @@ def test_load_typed_file():
}
)
)
ws.workspace_conf.get_status = mocker.patch(
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)
installation = Installation(ws, "blueprint")

cfg = installation.load(WorkspaceConfig)
Expand All @@ -210,7 +223,9 @@ def test_load_csv_file():


@pytest.mark.parametrize("ext", ["json", "csv"])
def test_load_typed_list_file(ext):
def test_load_typed_list_file(ext, mocker):
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
installation = MockInstallation(
{
f"workspaces.{ext}": [
Expand All @@ -219,7 +234,10 @@ def test_load_typed_list_file(ext):
]
}
)

installation._ws = ws
ws.workspace_conf.get_status = mocker.patch(
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)
workspaces = installation.load(list[Workspace], filename=f"workspaces.{ext}")

assert 2 == len(workspaces)
Expand Down Expand Up @@ -296,8 +314,14 @@ def v2_migrate(raw: dict) -> dict:
return raw


def test_migrations_on_load():
def test_migrations_on_load(mocker):
installation = MockInstallation({"config.yml": {"initial": 999}})
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
ws.workspace_conf.get_status = mocker.patch(
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)
installation._ws = ws

cfg = installation.load(EvolvedConfig)

Expand All @@ -322,8 +346,33 @@ def v1_migrate(raw: dict) -> dict:
return {}


def test_migrations_broken():
def test_migrations_broken(mocker):
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
installation = MockInstallation({"config.yml": {"initial": 999}})
installation._ws = ws
ws.workspace_conf.get_status = mocker.patch(
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)

with pytest.raises(IllegalState):
installation.load(BrokenConfig)


def mock_get_status(*args):
random_return_value = random.choice(["true", "false"])
pritishpai marked this conversation as resolved.
Show resolved Hide resolved
if args[0] == "enableWorkspaceFilesystem":
return {"enableWorkspaceFilesystem": random_return_value}


def test_enable_files_in_repos(mocker):
ws = create_autospec(WorkspaceClient)
ws.current_user.me().user_name = "foo"
installation = Installation(ws, "ucx")

ws.workspace_conf.get_status = mocker.patch(
pritishpai marked this conversation as resolved.
Show resolved Hide resolved
"databricks.sdk.service.settings.WorkspaceConfAPI.get_status", side_effect=mock_get_status
)

installation._enable_files_in_repos()
assert True
Loading