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>
177 lines
6.0 KiB
Python
177 lines
6.0 KiB
Python
"""Unit tests for `utils.inventory.bundle_apps`."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import unittest
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
from unittest.mock import patch
|
|
|
|
from utils.cache.yaml import _reset_cache_for_tests
|
|
from utils.inventory import bundle_apps
|
|
|
|
|
|
class TestSplitCsv(unittest.TestCase):
|
|
def test_splits_and_trims(self):
|
|
self.assertEqual(
|
|
bundle_apps._split_csv(" a , b,c "),
|
|
["a", "b", "c"],
|
|
)
|
|
|
|
def test_drops_empty_segments(self):
|
|
self.assertEqual(bundle_apps._split_csv("a,,b,"), ["a", "b"])
|
|
|
|
def test_empty_string(self):
|
|
self.assertEqual(bundle_apps._split_csv(""), [])
|
|
|
|
|
|
class _BundleFsMixin:
|
|
"""Build a fake inventories/bundles tree and patch SEARCH_DIRS."""
|
|
|
|
def setUp(self) -> None: # type: ignore[override]
|
|
_reset_cache_for_tests()
|
|
self._tmp = TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup) # type: ignore[attr-defined]
|
|
root = Path(self._tmp.name)
|
|
self.servers = root / "servers"
|
|
self.workstations = root / "workstations"
|
|
self.servers.mkdir()
|
|
self.workstations.mkdir()
|
|
patcher = patch.object(
|
|
bundle_apps,
|
|
"SEARCH_DIRS",
|
|
(self.servers, self.workstations),
|
|
)
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop) # type: ignore[attr-defined]
|
|
|
|
def write_bundle(self, base: Path, name: str, apps: list[str]) -> Path:
|
|
bundle_dir = base / name
|
|
bundle_dir.mkdir()
|
|
inv = bundle_dir / "inventory.yml"
|
|
children = "".join(f" {a}:\n" for a in apps) or " {}\n"
|
|
inv.write_text(f"all:\n children:\n{children}", encoding="utf-8")
|
|
return inv
|
|
|
|
|
|
class TestLocate(_BundleFsMixin, unittest.TestCase):
|
|
def test_finds_in_servers(self):
|
|
self.write_bundle(self.servers, "alpha", ["a"])
|
|
self.assertEqual(
|
|
bundle_apps._locate("alpha"),
|
|
self.servers / "alpha" / "inventory.yml",
|
|
)
|
|
|
|
def test_finds_in_workstations(self):
|
|
self.write_bundle(self.workstations, "beta", ["b"])
|
|
self.assertEqual(
|
|
bundle_apps._locate("beta"),
|
|
self.workstations / "beta" / "inventory.yml",
|
|
)
|
|
|
|
def test_servers_wins_over_workstations(self):
|
|
self.write_bundle(self.servers, "dup", ["s"])
|
|
self.write_bundle(self.workstations, "dup", ["w"])
|
|
self.assertEqual(
|
|
bundle_apps._locate("dup"),
|
|
self.servers / "dup" / "inventory.yml",
|
|
)
|
|
|
|
def test_missing_returns_none(self):
|
|
self.assertIsNone(bundle_apps._locate("ghost"))
|
|
|
|
|
|
class TestResolve(_BundleFsMixin, unittest.TestCase):
|
|
def test_returns_apps_in_declaration_order(self):
|
|
self.write_bundle(self.servers, "edu", ["app_b", "app_a", "app_c"])
|
|
self.assertEqual(
|
|
bundle_apps.resolve(["edu"]),
|
|
["app_b", "app_a", "app_c"],
|
|
)
|
|
|
|
def test_deduplicates_across_bundles(self):
|
|
self.write_bundle(self.servers, "one", ["shared", "uniq1"])
|
|
self.write_bundle(self.workstations, "two", ["uniq2", "shared"])
|
|
self.assertEqual(
|
|
bundle_apps.resolve(["one", "two"]),
|
|
["shared", "uniq1", "uniq2"],
|
|
)
|
|
|
|
def test_missing_bundle_raises(self):
|
|
self.write_bundle(self.servers, "present", ["a"])
|
|
with self.assertRaises(FileNotFoundError) as ctx:
|
|
bundle_apps.resolve(["present", "absent"])
|
|
self.assertIn("absent", str(ctx.exception))
|
|
self.assertNotIn("present", str(ctx.exception))
|
|
|
|
def test_multiple_missing_listed(self):
|
|
with self.assertRaises(FileNotFoundError) as ctx:
|
|
bundle_apps.resolve(["x", "y"])
|
|
msg = str(ctx.exception)
|
|
self.assertIn("x", msg)
|
|
self.assertIn("y", msg)
|
|
|
|
def test_empty_children_yields_empty_list(self):
|
|
path = self.servers / "empty" / "inventory.yml"
|
|
path.parent.mkdir()
|
|
path.write_text("all:\n children: {}\n", encoding="utf-8")
|
|
self.assertEqual(bundle_apps.resolve(["empty"]), [])
|
|
|
|
def test_missing_all_key_tolerated(self):
|
|
path = self.servers / "weird" / "inventory.yml"
|
|
path.parent.mkdir()
|
|
path.write_text("other: 1\n", encoding="utf-8")
|
|
self.assertEqual(bundle_apps.resolve(["weird"]), [])
|
|
|
|
def test_null_children_tolerated(self):
|
|
path = self.servers / "nullkids" / "inventory.yml"
|
|
path.parent.mkdir()
|
|
path.write_text("all:\n children: ~\n", encoding="utf-8")
|
|
self.assertEqual(bundle_apps.resolve(["nullkids"]), [])
|
|
|
|
|
|
class TestMain(_BundleFsMixin, unittest.TestCase):
|
|
def _run(self, argv):
|
|
out = io.StringIO()
|
|
err = io.StringIO()
|
|
with patch("sys.stdout", out), patch("sys.stderr", err):
|
|
code = bundle_apps.main(argv)
|
|
return code, out.getvalue(), err.getvalue()
|
|
|
|
def test_success_prints_csv(self):
|
|
self.write_bundle(self.servers, "edu", ["nextcloud", "openwebui"])
|
|
code, out, err = self._run(["edu"])
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(out, "nextcloud,openwebui\n")
|
|
self.assertEqual(err, "")
|
|
|
|
def test_multiple_bundles_csv_arg(self):
|
|
self.write_bundle(self.servers, "a", ["x"])
|
|
self.write_bundle(self.workstations, "b", ["y"])
|
|
code, out, _ = self._run(["a,b"])
|
|
self.assertEqual(code, 0)
|
|
self.assertEqual(out, "x,y\n")
|
|
|
|
def test_blank_arg_returns_2(self):
|
|
code, _, err = self._run([" , "])
|
|
self.assertEqual(code, 2)
|
|
self.assertIn("no bundle names", err)
|
|
|
|
def test_missing_bundle_returns_2(self):
|
|
code, _, err = self._run(["ghost"])
|
|
self.assertEqual(code, 2)
|
|
self.assertIn("ghost", err)
|
|
|
|
def test_no_apps_resolved_returns_2(self):
|
|
path = self.servers / "empty" / "inventory.yml"
|
|
path.parent.mkdir()
|
|
path.write_text("all:\n children: {}\n", encoding="utf-8")
|
|
code, _, err = self._run(["empty"])
|
|
self.assertEqual(code, 2)
|
|
self.assertIn("no role groups", err)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|