Skip to content

Commit

Permalink
feat: use platform name in build instance name
Browse files Browse the repository at this point in the history
This ensures each platform is built in its own build environment.

Signed-off-by: Callahan Kovacs <callahan.kovacs@canonical.com>
  • Loading branch information
mr-cal committed Jan 10, 2025
1 parent aa1a69b commit d93c378
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
9 changes: 5 additions & 4 deletions craft_application/services/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ def _get_instance_name(
self, work_dir: pathlib.Path, build_info: models.BuildInfo
) -> str:
work_dir_inode = work_dir.stat().st_ino
return (
f"{self._app.name}-{self._project.name}-on-{build_info.build_on}-"
f"for-{build_info.build_for}-{work_dir_inode}"
)

# craft-providers will remove invalid characters from the name but replacing
# characters improves readability for multi-base platforms like "ubuntu@24.04:amd64"
platform = build_info.platform.replace(":", "-").replace("@", "-")
return f"{self._app.name}-{self._project.name}-{platform}-{work_dir_inode}"

def _get_provider_by_name(self, name: str) -> craft_providers.Provider:
"""Get a provider by its name."""
Expand Down
8 changes: 7 additions & 1 deletion docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
Changelog
*********

4.8.0 (2025-MMM-DD)
4.8.0 (2025-Jan-10)
-------------------

Services
========

- Fix a bug where the same build environment was reused for platforms with
the same build-on and build-for architectures.

Utils
=====

Expand Down
31 changes: 27 additions & 4 deletions tests/unit/services/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ def test_get_lxd_provider(monkeypatch, provider_service, lxd_remote, check):
)


@pytest.mark.parametrize(
("platform", "platform_str"),
[
pytest.param("test-platform", "test-platform", id="simple"),
pytest.param(
"ubuntu@24.04:amd64", "ubuntu-24.04-amd64", id="special-characters"
),
],
)
def test_get_instance_name(platform, platform_str, new_dir, provider_service):
build_info = models.BuildInfo(
platform, "riscv64", "riscv64", bases.BaseName("ubuntu", "24.04")
)
inode_number = str(new_dir.stat().st_ino)
provider_service._build_plan = [build_info]
expected_name = f"testcraft-full-project-{platform_str}-{inode_number}"

assert (
provider_service._get_instance_name(work_dir=new_dir, build_info=build_info)
== expected_name
)


class TestGetProvider:
"""Test cases for `get_provider()`."""

Expand Down Expand Up @@ -492,7 +515,7 @@ def test_instance_clean_existing(

if clean_existing:
work_dir_inode = tmp_path.stat().st_ino
expected_name = f"testcraft-full-project-on-{arch}-for-{arch}-{work_dir_inode}"
expected_name = f"testcraft-full-project-{build_info.platform}-{work_dir_inode}"
mock_provider.clean_project_environments.assert_called_once_with(
instance_name=expected_name
)
Expand Down Expand Up @@ -693,15 +716,15 @@ def test_instance_fetch_logs_missing_file(
# A single build info, matching the current architecture
(
[models.BuildInfo("foo", "current", "current", _test_base)],
["on-current-for-current"],
["foo"],
),
# Two build infos, both matching the current architecture
(
[
models.BuildInfo("foo", "current", "current", _test_base),
models.BuildInfo("foo2", "current", "other", _test_base),
],
["on-current-for-current", "on-current-for-other"],
["foo", "foo2"],
),
# Three build infos, only one matches current architecture
(
Expand All @@ -710,7 +733,7 @@ def test_instance_fetch_logs_missing_file(
models.BuildInfo("foo2", "other", "other", _test_base),
models.BuildInfo("foo3", "other", "other2", _test_base),
],
["on-current-for-current"],
["foo"],
),
# Two build infos, none matches current architecture
(
Expand Down

0 comments on commit d93c378

Please sign in to comment.