mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +00:00
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).
This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.
Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:
* 7 `src:` values built from a role variable holding a bare filename
(svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
The variable doubles as the destination name inside the container, so the
language segment belongs at the call site, not in the variable.
* `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
18 roles ship exactly that and have no template fallback: the play would
have died on each.
* `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
interface now reads `files/javascript/javascript.js`.
* `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
path through `path_absolute(... 'files', ...)`.
* 4 `ansible.builtin.script:` invocations naming a bare command
(pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
* `dev-python/files/shell/install.sh` pointed at a sibling role's script.
Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
"""The apt wrapper must not reach the network for packages already present.
|
|
|
|
Runs 32265854076 and 32265543004 lost 40 deploy jobs to a single step: the
|
|
Azure Ubuntu mirror stalled, `apt-get update` burned the 10m timeout and exited
|
|
124, although the only requested package (jq) ships with the runner image.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import stat
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from utils.cache.files import PROJECT_ROOT
|
|
|
|
APT = PROJECT_ROOT / "scripts" / "install" / "apt.sh"
|
|
|
|
DPKG_QUERY_STUB = """#!/usr/bin/env bash
|
|
for arg in "$@"; do
|
|
case "${arg}" in
|
|
-*) continue ;;
|
|
esac
|
|
grep -qx "${arg}" "${STUB_INSTALLED}" || exit 1
|
|
printf installed
|
|
done
|
|
"""
|
|
|
|
SUDO_STUB = """#!/usr/bin/env bash
|
|
exec "$@"
|
|
"""
|
|
|
|
APT_GET_STUB = """#!/usr/bin/env bash
|
|
printf '%s\\n' "$*" >>"${STUB_APT_CALLS}"
|
|
"""
|
|
|
|
|
|
def _stub(directory: Path, name: str, body: str) -> None:
|
|
path = directory / name
|
|
path.write_text(body)
|
|
path.chmod(path.stat().st_mode | stat.S_IXUSR)
|
|
|
|
|
|
def _run(installed: list[str], packages: list[str]) -> tuple[int, list[str]]:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
bin_dir = Path(tmp)
|
|
_stub(bin_dir, "dpkg-query", DPKG_QUERY_STUB)
|
|
_stub(bin_dir, "sudo", SUDO_STUB)
|
|
_stub(bin_dir, "apt-get", APT_GET_STUB)
|
|
|
|
installed_file = bin_dir / "installed.txt"
|
|
installed_file.write_text("".join(f"{name}\n" for name in installed))
|
|
calls_file = bin_dir / "calls.txt"
|
|
calls_file.touch()
|
|
|
|
env = dict(
|
|
os.environ,
|
|
PATH=f"{bin_dir}:{os.environ['PATH']}",
|
|
STUB_INSTALLED=str(installed_file),
|
|
STUB_APT_CALLS=str(calls_file),
|
|
)
|
|
proc = subprocess.run(
|
|
["bash", str(APT), *packages],
|
|
capture_output=True,
|
|
text=True,
|
|
env=env,
|
|
check=False,
|
|
)
|
|
text = calls_file.read_text() # nocheck: cache-read - stub writes it per run
|
|
return proc.returncode, text.splitlines()
|
|
|
|
|
|
class AptShortCircuitTests(unittest.TestCase):
|
|
def test_a_present_package_skips_apt_entirely(self):
|
|
code, calls = _run(installed=["jq"], packages=["jq"])
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(calls, [])
|
|
|
|
def test_a_missing_package_still_updates_and_installs(self):
|
|
code, calls = _run(installed=[], packages=["jq"])
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(len(calls), 2)
|
|
self.assertTrue(calls[0].endswith("update"))
|
|
self.assertTrue(calls[1].endswith("install -y jq"))
|
|
|
|
def test_a_partial_hit_installs_only_what_is_missing(self):
|
|
code, calls = _run(installed=["jq"], packages=["jq", "gettext-base"])
|
|
self.assertEqual(code, 0)
|
|
self.assertTrue(calls[1].endswith("install -y gettext-base"))
|
|
|
|
def test_no_package_argument_is_rejected(self):
|
|
code, calls = _run(installed=[], packages=[])
|
|
self.assertEqual(code, 1)
|
|
self.assertEqual(calls, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|