mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 13:57:43 +00:00
Merge remote-tracking branch 'origin/main' into fix/lint/mermaid-browser-revision
This commit is contained in:
13 files changed
+303
-23
No files matched your search
+18
-2
@@ -9,7 +9,13 @@ import cli.meta.env.__main__ as env_main
|
||||
|
||||
|
||||
class TestDotenvHostOwned(unittest.TestCase):
|
||||
def _run(self, *, dotenv_exists: bool, host_owned: str | None) -> tuple[int, bool]:
|
||||
def _run(
|
||||
self,
|
||||
*,
|
||||
dotenv_exists: bool,
|
||||
host_owned: str | None,
|
||||
argv: list[str] | None = None,
|
||||
) -> tuple[int, bool]:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "default.env").write_text("INFINITO_DISTRO=debian\n")
|
||||
@@ -28,7 +34,7 @@ class TestDotenvHostOwned(unittest.TestCase):
|
||||
patch.object(env_main, "write_dotenv") as write_dotenv,
|
||||
):
|
||||
build_env.return_value.values = {}
|
||||
rc = env_main.main()
|
||||
rc = env_main.main(argv)
|
||||
return rc, write_dotenv.called
|
||||
|
||||
def test_a_host_owned_dotenv_is_kept(self) -> None:
|
||||
@@ -40,6 +46,16 @@ class TestDotenvHostOwned(unittest.TestCase):
|
||||
def test_without_the_marker_the_dotenv_is_regenerated(self) -> None:
|
||||
self.assertEqual(self._run(dotenv_exists=True, host_owned=None), (0, True))
|
||||
|
||||
def test_an_explicit_output_is_written_even_when_host_owned(self) -> None:
|
||||
self.assertEqual(
|
||||
self._run(
|
||||
dotenv_exists=True,
|
||||
host_owned="true",
|
||||
argv=["--output", "/nonexistent/distro.env"],
|
||||
),
|
||||
(0, True),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
unittest.main()
|
||||
Whitespace-only changes.
+136
@@ -0,0 +1,136 @@
|
||||
"""The distro-derived keys follow the distro the caller asks for.
|
||||
|
||||
``scripts/tests/deploy/distros.sh`` exports ``INFINITO_DISTRO`` per iteration
|
||||
and sources the loader again. The keys composed from the distro must then name
|
||||
that distro, not the one ``.env`` was generated for, and ``.env`` itself must
|
||||
stay as it was.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import filecmp
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from utils.cache.files import PROJECT_ROOT
|
||||
|
||||
LOADER_DIR = Path(PROJECT_ROOT) / "scripts" / "meta" / "env"
|
||||
SHOWN = (
|
||||
"INFINITO_DISTRO",
|
||||
"INFINITO_CONTAINER",
|
||||
"INFINITO_PARENT_IMAGE",
|
||||
"INFINITO_IMAGE",
|
||||
)
|
||||
SHOW = "printf '%s\\n' " + " ".join(f'"{key}=${{{key}:-}}"' for key in SHOWN)
|
||||
|
||||
|
||||
def clean_env(**overrides: str) -> dict[str, str]:
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if not k.startswith("INFINITO_")
|
||||
and k not in ("BASH_ENV", "PYTHONPATH", "VENV", "PYTHON", "VIRTUAL_ENV")
|
||||
}
|
||||
env["PYTHONPATH"] = str(PROJECT_ROOT)
|
||||
env.update(overrides)
|
||||
return env
|
||||
|
||||
|
||||
def bash(script: str, cwd: Path) -> dict[str, str]:
|
||||
proc = subprocess.run(
|
||||
["bash", "-c", f"{script}\n{SHOW}"],
|
||||
env=clean_env(),
|
||||
cwd=cwd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
return dict(line.split("=", 1) for line in proc.stdout.splitlines())
|
||||
|
||||
|
||||
class TestDistroDerivedKeys(unittest.TestCase):
|
||||
scratch: ClassVar[Path]
|
||||
expected: ClassVar[dict[str, dict[str, str]]]
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
cls.scratch = Path(tempfile.mkdtemp())
|
||||
cls.expected = {}
|
||||
for distro in ("debian", "centos"):
|
||||
path = cls.scratch / f"{distro}.env"
|
||||
subprocess.run(
|
||||
[sys.executable, "-m", "cli.meta.env", "--output", str(path)],
|
||||
env=clean_env(INFINITO_DISTRO=distro),
|
||||
cwd=PROJECT_ROOT,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
cls.expected[distro] = bash(f'set -a; source "{path}"', cls.scratch)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
shutil.rmtree(cls.scratch)
|
||||
|
||||
def run_loader(self, steps: str) -> dict[str, str]:
|
||||
"""Source a copy of the loader in a checkout whose ``.env`` is debian's.
|
||||
|
||||
Args:
|
||||
steps: bash run before the keys are shown; ``load`` sources the
|
||||
loader.
|
||||
|
||||
Returns:
|
||||
The keys as the steps leave them.
|
||||
"""
|
||||
checkout = Path(tempfile.mkdtemp())
|
||||
self.addCleanup(shutil.rmtree, checkout)
|
||||
(checkout / "scripts" / "meta" / "env").mkdir(parents=True)
|
||||
for name in ("load.sh", "python.sh"):
|
||||
shutil.copy(LOADER_DIR / name, checkout / "scripts" / "meta" / "env")
|
||||
dotenv = checkout / ".env"
|
||||
shutil.copy(self.scratch / "debian.env", dotenv)
|
||||
shown = bash(
|
||||
f"load() {{ source scripts/meta/env/load.sh; }}\n{steps}", checkout
|
||||
)
|
||||
self.assertTrue(
|
||||
filecmp.cmp(dotenv, self.scratch / "debian.env", shallow=False),
|
||||
".env was rewritten",
|
||||
)
|
||||
return shown
|
||||
|
||||
def test_the_two_distros_derive_different_keys(self):
|
||||
self.assertNotEqual(
|
||||
self.expected["debian"]["INFINITO_PARENT_IMAGE"],
|
||||
self.expected["centos"]["INFINITO_PARENT_IMAGE"],
|
||||
)
|
||||
|
||||
def test_a_distro_exported_after_the_first_load_gets_its_own_keys(self):
|
||||
shown = self.run_loader("load\nexport INFINITO_DISTRO=centos\nload")
|
||||
self.assertEqual(shown, self.expected["centos"])
|
||||
|
||||
def test_a_distro_exported_before_the_first_load_gets_its_own_keys(self):
|
||||
shown = self.run_loader("export INFINITO_DISTRO=centos\nload")
|
||||
self.assertEqual(shown, self.expected["centos"])
|
||||
|
||||
def test_switching_back_restores_the_keys_of_the_dotenv(self):
|
||||
shown = self.run_loader(
|
||||
"load\nexport INFINITO_DISTRO=centos\nload\n"
|
||||
"export INFINITO_DISTRO=debian\nload"
|
||||
)
|
||||
self.assertEqual(shown, self.expected["debian"])
|
||||
|
||||
def test_a_caller_value_for_the_loaded_distro_still_wins(self):
|
||||
shown = self.run_loader(
|
||||
"load\nexport INFINITO_PARENT_IMAGE=caller/image:pinned\n"
|
||||
"touch -d '+1 minute' .env\nload"
|
||||
)
|
||||
self.assertEqual(shown["INFINITO_PARENT_IMAGE"], "caller/image:pinned")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Whitespace-only changes.
@@ -0,0 +1,74 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from utils.cache.files import PROJECT_ROOT
|
||||
|
||||
SCRIPT = PROJECT_ROOT / "scripts" / "tests" / "deploy" / "act" / "down_act_outer.sh"
|
||||
DOCKER_STUB = (
|
||||
"#!/usr/bin/env bash\n"
|
||||
'printf "%s\\n" "$*" >>"${STUB_LOG}"\n'
|
||||
'if [ "$1" = ps ]; then\n'
|
||||
' case "$*" in *"label=infinito.worktree=own"*) echo own-id ;; esac\n'
|
||||
"fi\n"
|
||||
)
|
||||
|
||||
|
||||
def teardown(container: str | None) -> tuple[subprocess.CompletedProcess, list[str]]:
|
||||
"""Run the teardown against a ``docker`` stub that only knows ``own-id``.
|
||||
|
||||
Args:
|
||||
container: value of ``INFINITO_CONTAINER``, or None to leave it unset.
|
||||
|
||||
Returns:
|
||||
The finished process and the docker invocations it made.
|
||||
"""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
stub = Path(tmp) / "docker"
|
||||
stub.write_text(DOCKER_STUB)
|
||||
stub.chmod(0o755)
|
||||
log = Path(tmp) / "docker.log"
|
||||
log.touch()
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if k not in ("BASH_ENV", "INFINITO_CONTAINER")
|
||||
}
|
||||
env.update(PATH=f"{tmp}{os.pathsep}{os.environ['PATH']}", STUB_LOG=str(log))
|
||||
if container is not None:
|
||||
env["INFINITO_CONTAINER"] = container
|
||||
done = subprocess.run(
|
||||
["bash", str(SCRIPT)],
|
||||
env=env,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
return done, log.read_text().splitlines() # nocheck: cache-read stub call log
|
||||
|
||||
|
||||
class TestDownActOuter(unittest.TestCase):
|
||||
def test_only_the_own_worktrees_act_containers_are_listed(self) -> None:
|
||||
_, calls = teardown("own")
|
||||
self.assertIn("label=infinito.worktree=own", calls[0])
|
||||
|
||||
def test_the_own_act_container_is_removed(self) -> None:
|
||||
_, calls = teardown("own")
|
||||
self.assertEqual(calls[1], "rm -f own-id")
|
||||
|
||||
def test_a_foreign_worktrees_act_container_is_left_alone(self) -> None:
|
||||
_, calls = teardown("foreign")
|
||||
self.assertEqual(len(calls), 1)
|
||||
|
||||
def test_an_unset_container_name_removes_nothing(self) -> None:
|
||||
done, calls = teardown(None)
|
||||
self.assertNotEqual(done.returncode, 0)
|
||||
self.assertEqual(calls, [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -20,6 +20,7 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from utils.cache.files import PROJECT_ROOT, read_text
|
||||
from utils.distros import pkgmgr_image
|
||||
from utils.env.parser import parse_static_env
|
||||
|
||||
LOOP = PROJECT_ROOT / "scripts" / "tests" / "deploy" / "distros.sh"
|
||||
@@ -76,6 +77,19 @@ class TestDistrosLoop(unittest.TestCase):
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
self.assertEqual(sorted(record.split()), sorted(DISTROS.split()))
|
||||
|
||||
def test_every_distro_runs_with_a_parent_image_of_its_own(self) -> None:
|
||||
proc, record = self._run(
|
||||
DISTROS,
|
||||
'echo "${INFINITO_DISTRO} ${INFINITO_PARENT_IMAGE}'
|
||||
' ${INFINITO_PARENT_IMAGE_OWNER} ${INFINITO_PARENT_IMAGE_TAG}" >> "${RECORD}"',
|
||||
INFINITO_CI_DISTRO_BUDGET_SECONDS="600",
|
||||
)
|
||||
self.assertEqual(proc.returncode, 0, proc.stderr)
|
||||
rows = [line.split() for line in record.splitlines()]
|
||||
self.assertEqual(sorted(row[0] for row in rows), sorted(DISTROS.split()))
|
||||
for distro, image, owner, tag in rows:
|
||||
self.assertEqual(image, pkgmgr_image(distro, owner=owner, tag=tag))
|
||||
|
||||
def test_the_command_inherits_the_env_layer(self) -> None:
|
||||
proc, record = self._run(
|
||||
"debian",
|
||||
|
||||
Reference in new issue
Block a user