Merge branch 'main' into feature/designer

# Conflicts:
#	docs/contributing/artefact/files/plugins/lookup/applications.md
This commit is contained in:
kevinveenbirkenbach committed 2026-10-16 11:44:29 +02:00
commit 78bdb6e515
25 files changed
+888 -153

No files matched your search

@@ -56,6 +56,48 @@ class TestApplicationsCarrierActionPlugin(unittest.TestCase):
self.assertEqual(variables, {"applications": {}})
self.assertTrue(kwargs["carrier"])
def test_revalidates_the_tokens_file_before_rendering(self):
order = []
lookup = _FakeLookup({"key": ["roles", []], "applications": {}})
lookup.run = lambda *args, **kwargs: order.append("render") or [{}]
action = _make_action(_FakeTask())
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.lookup_loader.get",
return_value=lookup,
),
patch(
"plugins.action.applications_carrier.revalidate_tokens_file",
side_effect=lambda variables: order.append(("revalidate", variables)),
),
):
action.run(task_vars={"FILE_TOKENS": "/x/tokens.yml"})
self.assertEqual(
order, [("revalidate", {"FILE_TOKENS": "/x/tokens.yml"}), "render"]
)
def test_clear_does_not_revalidate(self):
action = _make_action(_FakeTask(args={"clear": True}))
with (
patch(
"plugins.action.applications_carrier.ActionBase.run",
autospec=True,
return_value={},
),
patch(
"plugins.action.applications_carrier.revalidate_tokens_file"
) as revalidate,
):
action.run(task_vars={})
revalidate.assert_not_called()
def test_rejects_arguments(self):
action = _make_action(_FakeTask(args={"foo": 1}))
with (
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,115 @@
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
from utils.cache.files import PROJECT_ROOT
SCRIPT = PROJECT_ROOT / "scripts" / "docker" / "cache" / "package-frontend" / "ca.sh"
NAME = "infinito-package-frontend.crt"
STUB = '#!/usr/bin/env bash\nprintf "%s\\n" "$0 $*" >>"${STUB_LOG}"\n'
PKI = "etc/pki/ca-trust/source/anchors"
ARCH = "etc/ca-certificates/trust-source/anchors"
TOOLS = ("bash", "cmp", "dirname", "install")
class TestPackageFrontendCa(unittest.TestCase):
def fake_root(self, tool: str, anchor_dirs: tuple[str, ...]) -> Path:
"""Build a fake root that offers one trust tool and a frontend CA.
Args:
tool: the trust rebuild command the fake system provides.
anchor_dirs: anchor directories that already exist in the fake root.
Returns:
The fake root.
"""
root = Path(self.enterContext(tempfile.TemporaryDirectory()))
(root / "opt").mkdir()
(root / "opt" / "package-frontend-ca.crt").write_text("CA\n", encoding="utf-8")
for anchor in anchor_dirs:
(root / anchor).mkdir(parents=True)
(root / "bin").mkdir()
for needed in TOOLS:
(root / "bin" / needed).symlink_to(shutil.which(needed))
(root / "bin" / tool).write_text(STUB, encoding="utf-8")
(root / "bin" / tool).chmod(0o755)
return root
def run_script(self, root: Path) -> subprocess.CompletedProcess:
"""Run ca.sh against a fake root.
Args:
root: fake root from ``fake_root``.
Returns:
The finished process.
"""
env = {k: v for k, v in os.environ.items() if k not in ("BASH_ENV", "CA_ROOT")}
env.update(
CA_ROOT=str(root),
PATH=str(root / "bin"),
STUB_LOG=str(root / "stub.log"),
)
return subprocess.run(
[str(root / "bin" / "bash"), str(SCRIPT)],
env=env,
capture_output=True,
text=True,
check=False,
)
def rebuilds(self, root: Path) -> int:
log = root / "stub.log"
if not log.exists():
return 0
text = log.read_text(encoding="utf-8") # nocheck: cache-read per-run tempdir
return len(text.splitlines())
def test_the_redhat_family_gets_the_pki_anchor(self) -> None:
root = self.fake_root("update-ca-trust", (PKI,))
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / PKI / NAME).is_file())
self.assertEqual(self.rebuilds(root), 1)
def test_arch_with_both_anchor_dirs_gets_its_trust_source_anchor(self) -> None:
root = self.fake_root("update-ca-trust", (PKI, ARCH))
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / ARCH / NAME).is_file())
def test_a_fresh_update_ca_trust_root_gets_both_anchors(self) -> None:
root = self.fake_root("update-ca-trust", ())
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / PKI / NAME).is_file())
self.assertTrue((root / ARCH / NAME).is_file())
def test_debian_uses_update_ca_certificates(self) -> None:
root = self.fake_root("update-ca-certificates", ())
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / "usr/local/share/ca-certificates" / NAME).is_file())
self.assertFalse((root / "etc").exists())
def test_a_second_run_does_not_rebuild(self) -> None:
root = self.fake_root("update-ca-trust", (PKI,))
self.run_script(root)
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertEqual(self.rebuilds(root), 1)
def test_no_trust_tool_fails_loudly(self) -> None:
root = self.fake_root("unrelated-tool", ())
done = self.run_script(root)
self.assertNotEqual(done.returncode, 0)
self.assertFalse(any(root.glob(f"**/{NAME}")))
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,119 @@
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path
from utils.cache.files import PROJECT_ROOT
SCRIPT = PROJECT_ROOT / "scripts" / "docker" / "cache" / "registry-ca.sh"
NAME = "infinito-registry-cache.crt"
STUB = '#!/usr/bin/env bash\nprintf "%s\\n" "$0 $*" >>"${STUB_LOG}"\n'
PKI = "etc/pki/ca-trust/source/anchors"
ARCH = "etc/ca-certificates/trust-source/anchors"
TOOLS = ("bash", "cmp", "dirname", "install")
class TestRegistryCacheCa(unittest.TestCase):
def fake_root(
self, tool: str, anchor_dirs: tuple[str, ...], ca: str | None
) -> Path:
"""Build a fake root that offers one trust tool.
Args:
tool: the trust rebuild command the fake system provides.
anchor_dirs: anchor directories that already exist in the fake root.
ca: content of the mounted ``ca.crt``; ``None`` leaves the CA dir unmounted.
Returns:
The fake root.
"""
root = Path(self.enterContext(tempfile.TemporaryDirectory()))
if ca is not None:
(root / "opt" / "registry-cache-ca").mkdir(parents=True)
(root / "opt" / "registry-cache-ca" / "ca.crt").write_text(
ca, encoding="utf-8"
)
for anchor in anchor_dirs:
(root / anchor).mkdir(parents=True)
(root / "bin").mkdir()
for needed in TOOLS:
(root / "bin" / needed).symlink_to(shutil.which(needed))
(root / "bin" / tool).write_text(STUB, encoding="utf-8")
(root / "bin" / tool).chmod(0o755)
return root
def run_script(self, root: Path) -> subprocess.CompletedProcess:
"""Run registry-ca.sh against a fake root.
Args:
root: fake root from ``fake_root``.
Returns:
The finished process.
"""
env = {k: v for k, v in os.environ.items() if k not in ("BASH_ENV", "CA_ROOT")}
env.update(
CA_ROOT=str(root),
PATH=str(root / "bin"),
STUB_LOG=str(root / "stub.log"),
)
return subprocess.run(
[str(root / "bin" / "bash"), str(SCRIPT)],
env=env,
capture_output=True,
text=True,
check=False,
)
def rebuilds(self, root: Path) -> int:
log = root / "stub.log"
if not log.exists():
return 0
text = log.read_text(encoding="utf-8") # nocheck: cache-read per-run tempdir
return len(text.splitlines())
def test_the_redhat_family_gets_the_pki_anchor(self) -> None:
root = self.fake_root("update-ca-trust", (PKI,), "CA\n")
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / PKI / NAME).is_file())
self.assertEqual(self.rebuilds(root), 1)
def test_arch_with_both_anchor_dirs_gets_its_trust_source_anchor(self) -> None:
root = self.fake_root("update-ca-trust", (PKI, ARCH), "CA\n")
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / ARCH / NAME).is_file())
def test_debian_uses_update_ca_certificates(self) -> None:
root = self.fake_root("update-ca-certificates", (), "CA\n")
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertTrue((root / "usr/local/share/ca-certificates" / NAME).is_file())
def test_a_second_run_does_not_rebuild(self) -> None:
root = self.fake_root("update-ca-trust", (ARCH,), "CA\n")
self.run_script(root)
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertEqual(self.rebuilds(root), 1)
def test_an_unmounted_ca_dir_is_skipped(self) -> None:
root = self.fake_root("update-ca-trust", (PKI,), None)
done = self.run_script(root)
self.assertEqual(done.returncode, 0, done.stderr)
self.assertEqual(self.rebuilds(root), 0)
def test_a_mounted_dir_without_ca_is_refused(self) -> None:
root = self.fake_root("update-ca-trust", (PKI,), "")
done = self.run_script(root)
self.assertNotEqual(done.returncode, 0)
self.assertFalse(any(root.glob(f"**/{NAME}")))
if __name__ == "__main__":
unittest.main()
+39 -4
View File
@@ -10,9 +10,11 @@ files; this file pins the contract of base itself.
from __future__ import annotations
import os
import tempfile
import textwrap
import unittest
import unittest.mock as mock
from pathlib import Path
from utils.cache import _reset_cache_for_tests, base
@@ -153,11 +155,13 @@ class TestStableVariablesSignature(unittest.TestCase):
class TestTokensFileSignature(unittest.TestCase):
def test_missing_file_returns_zeroed_signature(self):
def setUp(self):
base._reset()
def test_missing_file_returns_an_empty_digest(self):
with tempfile.TemporaryDirectory() as tmp:
sig = base._tokens_file_signature(Path(tmp) / "absent.yml")
self.assertEqual(sig[1], 0)
self.assertEqual(sig[2], 0)
path = Path(tmp) / "absent.yml"
self.assertEqual(base._tokens_file_signature(path), (str(path), ""))
def test_signature_changes_when_file_changes(self):
with tempfile.TemporaryDirectory() as tmp:
@@ -168,6 +172,37 @@ class TestTokensFileSignature(unittest.TestCase):
sig_after = base._tokens_file_signature(path)
self.assertNotEqual(sig_before, sig_after)
def test_a_touch_keeps_the_signature(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "tokens.yml"
path.write_text("a: 1\n", encoding="utf-8")
sig_before = base._tokens_file_signature(path)
later = path.stat().st_mtime_ns + 5_000_000_000
os.utime(path, ns=(later, later))
self.assertEqual(base._tokens_file_signature(path), sig_before)
def test_an_unchanged_stat_serves_the_digest_without_reading(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "tokens.yml"
path.write_text("a: 1\n", encoding="utf-8")
sig_before = base._tokens_file_signature(path)
with mock.patch.object(Path, "open", side_effect=AssertionError):
self.assertEqual(base._tokens_file_signature(path), sig_before)
class TestRevalidateTokensFile(unittest.TestCase):
def test_opens_the_file(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "tokens.yml"
path.write_text("a: 1\n", encoding="utf-8")
with mock.patch.object(Path, "open", wraps=path.open) as opened:
base._revalidate_tokens_file(path)
opened.assert_called_once_with("rb")
def test_a_missing_file_is_ignored(self):
with tempfile.TemporaryDirectory() as tmp:
base._revalidate_tokens_file(Path(tmp) / "absent.yml")
class TestRenderWithTemplar(unittest.TestCase):
"""`_render_with_templar` is the single ansible-coupled symbol in
@@ -17,10 +17,17 @@ if TYPE_CHECKING:
from collections.abc import Iterator
REVISION = "155.0.8059.39"
LDD = (
"\tlinux-vdso.so.1 (0x00007f5999895000)\n"
"\tlibnss3.so => not found\n"
"\tlibglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0x00007f598d09d000)\n"
"\tlibgbm.so.1 => not found\n"
"\tlibnss3.so => not found\n"
)
def _done(returncode: int) -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess([], returncode, stdout="out\n", stderr="err\n")
def _done(returncode: int, stdout: str = "out\n") -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess([], returncode, stdout=stdout, stderr="err\n")
def _write_core(node_modules: Path, revision: str) -> None:
@@ -64,10 +71,12 @@ def _fake_mmdc(
return mmdc
def _install_browser(cache: Path, revision: str) -> None:
def _install_browser(cache: Path, revision: str) -> Path:
build = cache / "chrome-headless-shell" / f"linux-{revision}" / "linux64"
build.mkdir(parents=True)
(build / "chrome-headless-shell").write_text("", encoding="utf-8")
build.mkdir(parents=True, exist_ok=True)
executable = build / "chrome-headless-shell"
executable.write_text("", encoding="utf-8")
return executable
@contextlib.contextmanager
@@ -89,45 +98,64 @@ def _environment(
mock.patch.dict(os.environ, {"PUPPETEER_CACHE_DIR": cache}),
mock.patch.object(mermaid_cli, "which", return_value=True),
mock.patch.object(mermaid_cli.shutil, "which", return_value=str(mmdc)),
mock.patch.object(mermaid_cli, "missing_libraries", return_value=[]),
):
yield Path(prefix), Path(cache)
class TestBrowserProvisioning(unittest.TestCase):
def provision(self, *, euid: int, results: list[int]) -> tuple[list, list[str]]:
def provision(
self, *, euid: int, results: list[int], apt: bool = True
) -> list[mock._Call]:
"""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.
results: exit codes the successive puppeteer calls return; a 0
lays the pinned browser into the cache.
apt: whether apt-get is on the PATH.
Returns:
The recorded subprocess calls and the warnings emitted.
The recorded subprocess calls.
"""
warnings: list[str] = []
with (
_environment(),
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
calls: list[mock._Call] = []
codes = iter(results)
with _environment() as (_, cache):
def test_root_installs_the_browser_with_its_libraries(self) -> None:
calls, warnings = self.provision(euid=0, results=[0])
def run(*args, **kwargs):
calls.append(mock.call(*args, **kwargs))
code = next(codes)
if code == 0:
_install_browser(cache, REVISION)
return _done(code)
with (
mock.patch.object(mermaid_cli.os, "geteuid", return_value=euid),
mock.patch.object(
mermaid_cli,
"which",
side_effect=lambda cmd: apt or cmd != "apt-get",
),
mock.patch.object(mermaid_cli.subprocess, "run", side_effect=run),
):
mermaid_cli.ensure()
return calls
def test_root_on_apt_installs_the_browser_with_its_libraries(self) -> None:
calls = 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_root_without_apt_leaves_the_libraries_to_the_soname_install(
self,
) -> None:
calls = self.provision(euid=0, results=[0], apt=False)
self.assertNotIn("--install-deps", calls[0].args[0])
def test_the_browser_comes_from_the_puppeteer_next_to_mmdc(self) -> None:
calls, _ = self.provision(euid=0, results=[0])
calls = self.provision(euid=0, results=[0])
self.assertTrue(
calls[0]
.args[0][0]
@@ -135,12 +163,12 @@ class TestBrowserProvisioning(unittest.TestCase):
)
def test_a_user_installs_the_browser_without_libraries(self) -> None:
calls, _ = self.provision(euid=1000, results=[0])
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])
calls = self.provision(euid=0, results=[0])
self.assertNotIn("PUPPETEER_SKIP_DOWNLOAD", calls[0].kwargs["env"])
def test_a_failed_install_clears_its_revision_and_retries_once(self) -> None:
@@ -153,17 +181,18 @@ class TestBrowserProvisioning(unittest.TestCase):
def run(*_args, **_kwargs):
seen.append((partial.exists(), other.exists()))
return _done(1 if len(seen) == 1 else 0)
if len(seen) == 1:
return _done(1)
_install_browser(cache, REVISION)
return _done(0)
with mock.patch.object(mermaid_cli.subprocess, "run", side_effect=run):
mermaid_cli.ensure()
self.assertEqual(seen, [(True, True), (False, True)])
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_a_second_failure_raises_with_puppeteers_output(self) -> None:
with self.assertRaisesRegex(RuntimeError, "out\nerr\n"):
self.provision(euid=0, results=[1, 1])
def test_the_pinned_browser_is_not_provisioned_again(self) -> None:
with _environment() as (_, cache):
@@ -175,23 +204,35 @@ class TestBrowserProvisioning(unittest.TestCase):
def test_a_browser_of_another_revision_is_replaced(self) -> None:
with _environment() as (_, cache):
_install_browser(cache, "155.0.8059.79")
with mock.patch.object(
mermaid_cli.subprocess, "run", return_value=_done(0)
) as run:
mermaid_cli.ensure()
run.assert_called_once()
def test_mmdc_without_its_own_puppeteer_warns(self) -> None:
def run(*_args, **_kwargs):
_install_browser(cache, REVISION)
return _done(0)
with mock.patch.object(
mermaid_cli.subprocess, "run", side_effect=run
) as stub:
mermaid_cli.ensure()
stub.assert_called_once()
def test_a_reported_success_without_the_browser_raises(self) -> None:
with (
_environment(),
mock.patch.object(mermaid_cli.subprocess, "run", return_value=_done(0)),
self.assertRaisesRegex(RuntimeError, "still missing"),
):
mermaid_cli.ensure()
def test_mmdc_without_its_own_puppeteer_raises(self) -> None:
with (
_environment(revision=None),
mock.patch.object(mermaid_cli, "warn") as warn,
mock.patch.object(mermaid_cli.subprocess, "run") as run,
self.assertRaisesRegex(RuntimeError, "no puppeteer next to mmdc"),
):
mermaid_cli.ensure()
run.assert_not_called()
warn.assert_called_once()
def test_a_timeout_warns_instead_of_raising(self) -> None:
def test_a_timeout_propagates(self) -> None:
with (
_environment(),
mock.patch.object(
@@ -199,10 +240,112 @@ class TestBrowserProvisioning(unittest.TestCase):
"run",
side_effect=subprocess.TimeoutExpired("puppeteer", 900),
),
mock.patch.object(mermaid_cli, "warn") as warn,
self.assertRaises(subprocess.TimeoutExpired),
):
mermaid_cli.ensure()
warn.assert_called_once()
class TestMmdcInstall(unittest.TestCase):
def test_a_failed_npm_install_propagates(self) -> None:
with (
mock.patch.object(mermaid_cli, "which", return_value=False),
mock.patch.object(mermaid_cli, "install_command_via_pkg"),
mock.patch.object(
mermaid_cli, "npm_install_global", side_effect=RuntimeError("npm")
),
mock.patch.object(mermaid_cli, "_ensure_browser") as browser,
self.assertRaisesRegex(RuntimeError, "npm"),
):
mermaid_cli.ensure()
browser.assert_not_called()
def test_mmdc_still_missing_after_npm_raises(self) -> None:
with (
mock.patch.object(mermaid_cli, "which", return_value=False),
mock.patch.object(mermaid_cli, "install_command_via_pkg"),
mock.patch.object(mermaid_cli, "npm_install_global"),
mock.patch.object(mermaid_cli, "_ensure_browser") as browser,
self.assertRaisesRegex(RuntimeError, "mmdc"),
):
mermaid_cli.ensure()
browser.assert_not_called()
class TestBrowserLibraries(unittest.TestCase):
def test_ldd_not_found_lines_become_sorted_sonames(self) -> None:
with mock.patch.object(
mermaid_cli.subprocess, "run", return_value=_done(0, LDD)
) as run:
missing = mermaid_cli.missing_libraries(Path("/opt/chs"))
self.assertEqual(missing, ["libgbm.so.1", "libnss3.so"])
self.assertEqual(run.call_args.args[0], ["ldd", "/opt/chs"])
def ensure_libraries(
self, *, manager: str, missing: list[list[str]], files: dict[str, str]
) -> mock.Mock:
"""Run the library step with stubbed ldd, package manager and file DB.
Args:
manager: the package manager the host reports.
missing: successive ``missing_libraries`` results.
files: ``pacman -Fq`` stdout per queried path.
Returns:
The stubbed ``run_privileged``.
"""
def query(argv, **_kwargs):
return _done(0, files.get(argv[-1], ""))
with (
mock.patch.object(mermaid_cli, "missing_libraries", side_effect=missing),
mock.patch.object(
mermaid_cli, "detect_package_manager", return_value=manager
),
mock.patch.object(mermaid_cli.subprocess, "run", side_effect=query),
mock.patch.object(mermaid_cli, "run_privileged") as privileged,
):
mermaid_cli._ensure_browser_libraries(Path("/opt/chs"))
return privileged
def test_resolved_libraries_install_nothing(self) -> None:
privileged = self.ensure_libraries(manager="dnf", missing=[[]], files={})
privileged.assert_not_called()
def test_the_redhat_family_installs_by_soname_provide(self) -> None:
privileged = self.ensure_libraries(
manager="dnf", missing=[["libgbm.so.1", "libnss3.so"], []], files={}
)
privileged.assert_called_once_with(
["dnf", "-y", "install", "libgbm.so.1()(64bit)", "libnss3.so()(64bit)"]
)
def test_arch_takes_the_first_package_owning_each_soname(self) -> None:
privileged = self.ensure_libraries(
manager="pacman",
missing=[["libgbm.so.1", "libnss3.so"], []],
files={
"usr/lib/libgbm.so.1": "extra/mesa\nextra/mesa-amber\n",
"usr/lib/libnss3.so": "core/nss\n",
},
)
self.assertEqual(
privileged.call_args_list,
[
mock.call(["pacman", "-Fy"]),
mock.call(["pacman", "-Syu", "--noconfirm", "--needed", "mesa", "nss"]),
],
)
def test_apt_without_libraries_fails_without_installing(self) -> None:
with self.assertRaisesRegex(RuntimeError, "--install-deps"):
self.ensure_libraries(manager="apt-get", missing=[["libnss3.so"]], files={})
def test_libraries_still_missing_after_the_install_raise(self) -> None:
with self.assertRaisesRegex(RuntimeError, "still lacks libnss3.so"):
self.ensure_libraries(
manager="dnf", missing=[["libnss3.so"], ["libnss3.so"]], files={}
)
class TestBrowserInstalled(unittest.TestCase):
@@ -213,6 +356,14 @@ class TestBrowserInstalled(unittest.TestCase):
_install_browser(cache, REVISION)
self.assertTrue(mermaid_cli.browser_installed())
def test_a_browser_with_missing_libraries_is_not_installed(self) -> None:
with _environment() as (_, cache):
_install_browser(cache, REVISION)
with mock.patch.object(
mermaid_cli, "missing_libraries", return_value=["libnss3.so"]
):
self.assertFalse(mermaid_cli.browser_installed())
def test_the_revision_is_read_from_mmdcs_puppeteer_core(self) -> None:
with _environment():
self.assertEqual(mermaid_cli.browser_revision(), REVISION)
+59 -3
View File
@@ -9,19 +9,75 @@ from utils.install import ruby as ruby_mod
class TestEnsureRubyPresent(unittest.TestCase):
def test_delegates_to_the_shared_provisioner(self) -> None:
with mock.patch.object(ruby_mod, "ensure_command_present") as ensure:
def test_provisions_ruby_then_minitest(self) -> None:
calls = mock.Mock()
with (
mock.patch.object(ruby_mod, "ensure_command_present", calls.ruby),
mock.patch.object(ruby_mod, "ensure_minitest_present", calls.minitest),
):
ruby_mod.ensure_ruby_present()
ensure.assert_called_once_with("ruby")
self.assertEqual(
calls.mock_calls, [mock.call.ruby("ruby"), mock.call.minitest()]
)
def test_propagates_the_hard_failure(self) -> None:
with (
mock.patch.object(
ruby_mod, "ensure_command_present", side_effect=RuntimeError("boom")
),
mock.patch.object(ruby_mod, "ensure_minitest_present") as minitest,
self.assertRaises(RuntimeError),
):
ruby_mod.ensure_ruby_present()
minitest.assert_not_called()
class TestEnsureMinitestPresent(unittest.TestCase):
def run_with(self, *, loadable: list[bool], manager: str) -> mock.Mock:
"""Run ``ensure_minitest_present`` with stubbed probe and installer.
Args:
loadable: successive results of the minitest probe.
manager: package manager the host reports.
Returns:
The stubbed ``install_package_candidates``.
"""
with (
mock.patch.object(ruby_mod, "minitest_loadable", side_effect=loadable),
mock.patch.object(ruby_mod, "detect_package_manager", return_value=manager),
mock.patch.object(ruby_mod, "install_package_candidates") as install,
mock.patch.object(ruby_mod, "log"),
):
ruby_mod.ensure_minitest_present()
return install
def test_a_loadable_minitest_installs_nothing(self) -> None:
install = self.run_with(loadable=[True], manager="pacman")
install.assert_not_called()
def test_arch_installs_ruby_minitest(self) -> None:
install = self.run_with(loadable=[False, True], manager="pacman")
install.assert_called_once_with("pacman", ["ruby-minitest"])
def test_the_redhat_family_installs_rubygem_minitest(self) -> None:
install = self.run_with(loadable=[False, True], manager="dnf")
install.assert_called_once_with("dnf", ["rubygem-minitest"])
def test_still_missing_after_the_install_fails_loudly(self) -> None:
with self.assertRaises(RuntimeError):
self.run_with(loadable=[False, False], manager="pacman")
def test_an_unmapped_manager_fails_without_installing(self) -> None:
with (
mock.patch.object(ruby_mod, "minitest_loadable", return_value=False),
mock.patch.object(ruby_mod, "detect_package_manager", return_value="brew"),
mock.patch.object(ruby_mod, "install_package_candidates") as install,
mock.patch.object(ruby_mod, "log"),
self.assertRaises(RuntimeError),
):
ruby_mod.ensure_minitest_present()
install.assert_not_called()
if __name__ == "__main__":