Skip to content
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
22 changes: 16 additions & 6 deletions ltvm_pkg/release_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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)],
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 45 additions & 0 deletions tests/test_fetch_podman_preflight.py
Original file line number Diff line number Diff line change
@@ -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()
Loading