diff --git a/ltvm_pkg/release_package.py b/ltvm_pkg/release_package.py index ac90a06..3351e93 100644 --- a/ltvm_pkg/release_package.py +++ b/ltvm_pkg/release_package.py @@ -90,6 +90,20 @@ def _schema_id() -> str: return f"{SCHEMA_NAME}/{SCHEMA_VERSION}" +def _ensure_podman() -> None: + """Make podman usable before a fetch or publish runs it. + + On macOS this starts a stopped machine -- the idle stop leaves it + stopped half an hour after the last build -- and holds the usage + lock, as the build preflight does. Taking only the lock left every + `target fetch` after an idle stop failing `podman load` with "Cannot + connect to Podman". + """ + from ltvm_pkg.host_setup import check_podman_machine_macos + + check_podman_machine_macos() + + def _producer_metadata() -> dict[str, str]: """Descriptive info about which ltvm wrote this manifest. @@ -510,9 +524,7 @@ def export_build_container( container_dir.mkdir(parents=True, exist_ok=True) image_tar = container_dir / "image.tar" - from ltvm_pkg.host_setup import hold_podman_usage - - hold_podman_usage() + _ensure_podman() try: check = subprocess.run( ["podman", "image", "exists", container_tag], @@ -1554,9 +1566,7 @@ def fetch_target( f"fetched asset set missing container image at {container_image}" ) print(f" Loading build container from {container_image}") - from ltvm_pkg.host_setup import hold_podman_usage - - hold_podman_usage() + _ensure_podman() try: load = subprocess.run( ["podman", "load", "-i", str(container_image)], diff --git a/tests/conftest.py b/tests/conftest.py index 44dfd3d..c99c0b3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -230,8 +230,12 @@ def _neutralize_podman_preflight() -> object: would fail tests that don't mock podman. The tests already stub the actual build functions, so the preflight is orthogonal. """ - with patch( - "ltvm_pkg.cli.build.check_podman_machine_macos", return_value=None + with ( + patch( + "ltvm_pkg.cli.build.check_podman_machine_macos", + return_value=None, + ), + patch("ltvm_pkg.release_package._ensure_podman", return_value=None), ): yield diff --git a/tests/test_fetch_podman_preflight.py b/tests/test_fetch_podman_preflight.py new file mode 100644 index 0000000..93bf762 --- /dev/null +++ b/tests/test_fetch_podman_preflight.py @@ -0,0 +1,45 @@ +"""fetch and publish start a stopped macOS podman machine before using it. + +They took only the usage lock, so once the idle stop had stopped the +machine every `target fetch` failed `podman load` with "Cannot connect to +Podman". +""" + +from __future__ import annotations + +from pathlib import Path +from unittest.mock import MagicMock, patch + +import pytest + +from ltvm_pkg import release_package + +# Taken at import, before conftest's autouse stub replaces it. +real_ensure_podman = release_package._ensure_podman + + +def test_export_build_container_starts_podman_first(tmp_path: Path) -> None: + calls: list[str] = [] + + def run(cmd: list[str], **_kw: object) -> MagicMock: + calls.append(" ".join(cmd[:2])) + return MagicMock(returncode=1, stdout="", stderr="") + + with ( + patch.object( + release_package, + "_ensure_podman", + side_effect=lambda: calls.append("preflight"), + ), + patch.object(release_package.subprocess, "run", side_effect=run), + pytest.raises(RuntimeError, match="not found in podman storage"), + ): + release_package.export_build_container("rocky9", tmp_path) + assert calls[:2] == ["preflight", "podman image"] + + +def test_ensure_podman_runs_the_machine_preflight() -> None: + # The suite stubs _ensure_podman (conftest); this is the real one. + with patch("ltvm_pkg.host_setup.check_podman_machine_macos") as check: + real_ensure_podman() + check.assert_called_once_with()