mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
Merge remote-tracking branch 'origin/main' into feature/store-i18n
# Conflicts: # scripts/lint/mermaid.sh # tests/unit/python/utils/docker/test_image_discovery_lifecycle.py # utils/docker/image/discovery.py
This commit is contained in:
commit
5e2a5fcc31
24 files changed
+593
-85
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
@@ -8,7 +9,7 @@ from utils.docker.image.discovery import iter_role_images, role_lifecycle
|
||||
from utils.roles.mapping import ROLE_FILE_META_SERVICES
|
||||
|
||||
SUPPORTED = """---
|
||||
engine:
|
||||
demo:
|
||||
image: libretranslate/libretranslate
|
||||
version: v1.9.6
|
||||
lifecycle: stable
|
||||
@@ -18,7 +19,7 @@ helper:
|
||||
"""
|
||||
|
||||
RETIRED = """---
|
||||
store:
|
||||
demo:
|
||||
image: quay.io/minio/minio
|
||||
version: RELEASE.2025-09-07T16-13-09Z
|
||||
lifecycle: eol
|
||||
@@ -28,24 +29,31 @@ client:
|
||||
"""
|
||||
|
||||
UNSTAGED = """---
|
||||
engine:
|
||||
demo:
|
||||
image: libretranslate/libretranslate
|
||||
version: v1.9.6
|
||||
"""
|
||||
|
||||
|
||||
class TestImageDiscoveryLifecycle(unittest.TestCase):
|
||||
def _images(self, services_yaml: str) -> set[str]:
|
||||
def _images(self, services_yaml: str, *, relative: bool = False) -> set[str]:
|
||||
"""Discover the images of one role written into a temporary repository.
|
||||
|
||||
Args:
|
||||
services_yaml: the role's ``meta/services.yml``.
|
||||
relative: pass the repository root relative to the working directory.
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
services = Path(tmp) / "roles" / "web-app-demo" / ROLE_FILE_META_SERVICES
|
||||
services.parent.mkdir(parents=True)
|
||||
services.write_text(services_yaml, encoding="utf-8")
|
||||
return {ref.service for ref in iter_role_images(Path(tmp))}
|
||||
root = Path(os.path.relpath(tmp)) if relative else Path(tmp)
|
||||
return {ref.service for ref in iter_role_images(root)}
|
||||
|
||||
def test_a_supported_role_yields_every_service(self) -> None:
|
||||
self.assertEqual({"engine", "helper"}, self._images(SUPPORTED))
|
||||
self.assertEqual({"demo", "helper"}, self._images(SUPPORTED))
|
||||
|
||||
def test_a_retired_role_yields_nothing(self) -> None:
|
||||
def test_a_retired_role_yields_none_of_its_services(self) -> None:
|
||||
"""Its upstream may withdraw the images; no deploy can reach them."""
|
||||
self.assertEqual(set(), self._images(RETIRED))
|
||||
|
||||
@@ -54,7 +62,10 @@ class TestImageDiscoveryLifecycle(unittest.TestCase):
|
||||
self.assertNotIn("client", self._images(RETIRED))
|
||||
|
||||
def test_a_role_that_names_no_stage_is_kept(self) -> None:
|
||||
self.assertEqual({"engine"}, self._images(UNSTAGED))
|
||||
self.assertEqual({"demo"}, self._images(UNSTAGED))
|
||||
|
||||
def test_a_relative_root_still_leaves_a_retired_role_out(self) -> None:
|
||||
self.assertEqual(set(), self._images(RETIRED, relative=True))
|
||||
|
||||
|
||||
class TestRoleLifecycle(unittest.TestCase):
|
||||
|
||||
@@ -34,12 +34,30 @@ class TestStampLogic(unittest.TestCase):
|
||||
with (
|
||||
mock.patch.object(cli, "PROJECT_ROOT", root),
|
||||
mock.patch.object(cli.shutil, "which", return_value="/usr/bin/x"),
|
||||
mock.patch.object(
|
||||
cli.mermaid_cli, "browser_installed", return_value=True
|
||||
),
|
||||
mock.patch.object(cli, "_install_all") as inst,
|
||||
):
|
||||
rc = cli.main([])
|
||||
self.assertEqual(rc, 0)
|
||||
inst.assert_not_called()
|
||||
|
||||
def test_missing_mermaid_browser_invalidates_stamp(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
root = _make_repo_root(tmp, stamp_age_offset=60.0)
|
||||
with (
|
||||
mock.patch.object(cli, "PROJECT_ROOT", root),
|
||||
mock.patch.object(cli.shutil, "which", return_value="/usr/bin/x"),
|
||||
mock.patch.object(
|
||||
cli.mermaid_cli, "browser_installed", return_value=False
|
||||
),
|
||||
mock.patch.object(cli, "_install_all") as inst,
|
||||
):
|
||||
rc = cli.main([])
|
||||
self.assertEqual(rc, 0)
|
||||
inst.assert_called_once()
|
||||
|
||||
def test_missing_tool_invalidates_stamp(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
root = _make_repo_root(tmp, stamp_age_offset=60.0)
|
||||
@@ -81,9 +99,10 @@ class TestStampLogic(unittest.TestCase):
|
||||
self.assertTrue((root / cli._STAMP).exists())
|
||||
|
||||
def test_force_drops_stamp(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
with TemporaryDirectory() as tmp, TemporaryDirectory() as cache:
|
||||
root = _make_repo_root(tmp, stamp_age_offset=60.0)
|
||||
with (
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(cli, "PROJECT_ROOT", root),
|
||||
mock.patch.object(cli, "_install_all") as inst,
|
||||
):
|
||||
@@ -91,6 +110,19 @@ class TestStampLogic(unittest.TestCase):
|
||||
self.assertEqual(rc, 0)
|
||||
inst.assert_called_once()
|
||||
|
||||
def test_force_drops_the_mermaid_browser(self) -> None:
|
||||
with TemporaryDirectory() as tmp, TemporaryDirectory() as cache:
|
||||
root = _make_repo_root(tmp, stamp_age_offset=60.0)
|
||||
browser = Path(cache) / "chrome-headless-shell" / "linux-1"
|
||||
browser.mkdir(parents=True)
|
||||
with (
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(cli, "PROJECT_ROOT", root),
|
||||
mock.patch.object(cli, "_install_all"),
|
||||
):
|
||||
cli.main(["--force"])
|
||||
self.assertFalse(browser.exists())
|
||||
|
||||
|
||||
class TestGroupDispatch(unittest.TestCase):
|
||||
def test_group_only_bypasses_stamp(self) -> None:
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
"""Unit tests for :mod:`utils.install.lint.mermaid_cli`."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from utils.install.lint import mermaid_cli
|
||||
|
||||
|
||||
def _done(returncode: int) -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.CompletedProcess([], returncode, stdout="out\n", stderr="err\n")
|
||||
|
||||
|
||||
class TestBrowserProvisioning(unittest.TestCase):
|
||||
def provision(self, *, euid: int, results: list[int]) -> tuple[list, list[str]]:
|
||||
"""Run ``ensure()`` with mmdc present and a stubbed puppeteer.
|
||||
|
||||
Args:
|
||||
euid: effective uid the run pretends to have.
|
||||
results: exit codes the successive puppeteer calls return.
|
||||
|
||||
Returns:
|
||||
The recorded subprocess calls and the warnings emitted.
|
||||
"""
|
||||
warnings: list[str] = []
|
||||
with (
|
||||
TemporaryDirectory() as cache,
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(mermaid_cli, "which", return_value=True),
|
||||
mock.patch.object(mermaid_cli.os, "geteuid", return_value=euid),
|
||||
mock.patch.object(mermaid_cli, "warn", side_effect=warnings.append),
|
||||
mock.patch.object(
|
||||
mermaid_cli.subprocess,
|
||||
"run",
|
||||
side_effect=[_done(code) for code in results],
|
||||
) as run,
|
||||
):
|
||||
mermaid_cli.ensure()
|
||||
return run.call_args_list, warnings
|
||||
|
||||
def test_root_installs_the_browser_with_its_libraries(self) -> None:
|
||||
calls, warnings = self.provision(euid=0, results=[0])
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(
|
||||
calls[0].args[0][-2:], ["chrome-headless-shell", "--install-deps"]
|
||||
)
|
||||
self.assertEqual(warnings, [])
|
||||
|
||||
def test_a_user_installs_the_browser_without_libraries(self) -> None:
|
||||
calls, _ = self.provision(euid=1000, results=[0])
|
||||
self.assertNotIn("--install-deps", calls[0].args[0])
|
||||
|
||||
def test_the_browser_install_downloads_despite_the_mmdc_skip_flag(self) -> None:
|
||||
with mock.patch.dict(os.environ, {"PUPPETEER_SKIP_DOWNLOAD": "1"}):
|
||||
calls, _ = self.provision(euid=0, results=[0])
|
||||
self.assertNotIn("PUPPETEER_SKIP_DOWNLOAD", calls[0].kwargs["env"])
|
||||
|
||||
def test_a_failed_install_clears_the_browser_cache_and_retries_once(self) -> None:
|
||||
with TemporaryDirectory() as cache:
|
||||
folder = Path(cache) / "chrome-headless-shell" / "linux-1"
|
||||
folder.mkdir(parents=True)
|
||||
seen: list[bool] = []
|
||||
|
||||
def run(*_args, **_kwargs):
|
||||
seen.append(folder.exists())
|
||||
return _done(1 if len(seen) == 1 else 0)
|
||||
|
||||
with (
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(mermaid_cli, "which", return_value=True),
|
||||
mock.patch.object(mermaid_cli.subprocess, "run", side_effect=run),
|
||||
):
|
||||
mermaid_cli.ensure()
|
||||
self.assertEqual(seen, [True, False])
|
||||
|
||||
def test_a_second_failure_warns_with_puppeteers_output(self) -> None:
|
||||
calls, warnings = self.provision(euid=0, results=[1, 1])
|
||||
self.assertEqual(len(calls), 2)
|
||||
self.assertEqual(len(warnings), 1)
|
||||
self.assertIn("out\nerr\n", warnings[0])
|
||||
|
||||
def test_an_installed_browser_is_not_provisioned_again(self) -> None:
|
||||
with TemporaryDirectory() as cache:
|
||||
build = Path(cache) / "chrome-headless-shell" / "linux-1" / "linux64"
|
||||
build.mkdir(parents=True)
|
||||
(build / "chrome-headless-shell").write_text("", encoding="utf-8")
|
||||
with (
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(mermaid_cli, "which", return_value=True),
|
||||
mock.patch.object(mermaid_cli.subprocess, "run") as run,
|
||||
):
|
||||
mermaid_cli.ensure()
|
||||
run.assert_not_called()
|
||||
|
||||
def test_a_timeout_warns_instead_of_raising(self) -> None:
|
||||
with (
|
||||
TemporaryDirectory() as cache,
|
||||
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
|
||||
mock.patch.object(mermaid_cli, "which", return_value=True),
|
||||
mock.patch.object(
|
||||
mermaid_cli.subprocess,
|
||||
"run",
|
||||
side_effect=subprocess.TimeoutExpired("npx", 900),
|
||||
),
|
||||
mock.patch.object(mermaid_cli, "warn") as warn,
|
||||
):
|
||||
mermaid_cli.ensure()
|
||||
warn.assert_called_once()
|
||||
|
||||
|
||||
class TestBrowserInstalled(unittest.TestCase):
|
||||
def test_the_executable_in_the_puppeteer_cache_counts(self) -> None:
|
||||
with TemporaryDirectory() as cache:
|
||||
build = Path(cache) / "chrome-headless-shell" / "linux-1" / "linux64"
|
||||
build.mkdir(parents=True)
|
||||
with mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}):
|
||||
self.assertFalse(mermaid_cli.browser_installed())
|
||||
(build / "chrome-headless-shell").write_text("", encoding="utf-8")
|
||||
self.assertTrue(mermaid_cli.browser_installed())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user