Skip to content
Open
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
30 changes: 16 additions & 14 deletions craft_providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,22 +846,24 @@ def _mount_shared_cache_dirs(self, executor: Executor) -> None:
)
guest_base_cache_path = pathlib.Path(guest_cache_proc.stdout)

# PIP cache
host_pip_cache_path = host_base_cache_path / "pip"
host_pip_cache_path.mkdir(parents=True, exist_ok=True)
for tool in ("pip", "uv"):
host_tool_cache_path = host_base_cache_path / tool
host_tool_cache_path.mkdir(parents=True, exist_ok=True)

guest_pip_cache_path = guest_base_cache_path / "pip"
executor.execute_run(
["mkdir", "-p", guest_pip_cache_path.as_posix()],
)

try:
executor.mount(host_source=host_pip_cache_path, target=guest_pip_cache_path)
except ProviderError as exc:
logger.warning(
"Failed to mount cache in instance. Proceeding without cache."
guest_tool_cache_path = guest_base_cache_path / tool
executor.execute_run(
["mkdir", "-p", guest_tool_cache_path.as_posix()],
)
logger.debug(exc)

try:
executor.mount(
host_source=host_tool_cache_path, target=guest_tool_cache_path
)
except ProviderError as exc:
logger.warning(
"Failed to mount cache in instance. Proceeding without cache."
)
logger.debug(exc)

def _pre_setup_packages(self, executor: Executor) -> None:
"""Do anything before setting up the packages.
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/bases/test_ubuntu_buildd.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,18 @@ def test_mount_cache_dirs(fake_process, fake_executor, cache_path: pathlib.Path,
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/pip"],
)
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/uv"],
)

base._mount_shared_cache_dirs(fake_executor)

expected_mounts = [
{
"host_source": host_cache_dir / "pip",
"target": user_cache_dir / "pip",
},
"host_source": host_cache_dir / tool,
"target": user_cache_dir / tool,
}
for tool in ("pip", "uv")
]
assert fake_executor.records_of_mount == expected_mounts

Expand Down
25 changes: 20 additions & 5 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,23 @@ def test_mount_shared_cache_dirs(fake_process, fake_base, fake_executor, cache_d
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/pip"],
)
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/uv"],
)

fake_base._mount_shared_cache_dirs(fake_executor)

expected = {
"host_source": cache_dir.resolve() / "base-v7" / "FakeBaseAlias.TREBLE" / "pip",
"target": user_cache_dir / "pip",
}
assert fake_executor.records_of_mount == [expected]
expected = [
{
"host_source": cache_dir.resolve()
/ "base-v7"
/ "FakeBaseAlias.TREBLE"
/ tool,
"target": user_cache_dir / tool,
}
for tool in ("pip", "uv")
]
assert fake_executor.records_of_mount == expected


@pytest.mark.parametrize("cache_dir", [pathlib.Path("/tmp/fake-cache-dir")])
Expand All @@ -181,6 +190,9 @@ def test_mount_shared_cache_dirs_mkdir_failed(
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/pip"],
)
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/uv"],
)

mocker.patch("pathlib.Path.mkdir", side_effect=OSError)

Expand All @@ -205,6 +217,9 @@ def test_mount_shared_cache_dirs_mount_failed(
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/pip"],
)
fake_process.register(
[*DEFAULT_FAKE_CMD, "mkdir", "-p", "/root/.cache/uv"],
)
mocker.patch("pathlib.Path.mkdir") # don't try to create the directory
mocker.patch.object(fake_executor, "mount", side_effect=ProviderError(error_msg))

Expand Down
Loading