Files
computer-playbook/tests/unit/python/plugins/lookup/test_role_templates_expanded.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
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>
2026-08-22 01:31:09 +02:00

141 lines
5.3 KiB
Python

import importlib.util
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from ansible.errors import AnsibleError
from . import PROJECT_ROOT
_REL = "plugins/lookup/role_templates_expanded.py"
_APP = "web-app-demo"
def _load_module():
path = PROJECT_ROOT / _REL
spec = importlib.util.spec_from_file_location("role_templates_expanded", str(path))
mod = importlib.util.module_from_spec(spec)
assert spec and spec.loader
spec.loader.exec_module(mod)
return mod
MODULE = _load_module()
class _Tree:
"""A throwaway repo root holding `roles/<app>/templates/<names>`."""
def __init__(self, *names: str):
self._tmp = tempfile.TemporaryDirectory()
self.root = Path(self._tmp.name)
templates = self.root / "roles" / _APP / "templates"
templates.mkdir(parents=True)
for name in names:
if name.endswith("/"):
(templates / name.rstrip("/")).mkdir()
else:
(templates / name).write_text("x")
def __enter__(self):
return self
def __exit__(self, *exc):
self._tmp.cleanup()
def _run(entries, tree):
with patch.object(MODULE, "PROJECT_ROOT", tree.root):
return MODULE.LookupModule().run([_APP, entries])[0]
class TestRoleTemplatesExpanded(unittest.TestCase):
def test_a_glob_entry_without_notify_gets_the_compose_up_handler(self) -> None:
with _Tree("a.conf.j2") as tree:
out = _run([{"src_glob": "*.conf.j2", "dest_dir": "/opt/x"}], tree)
self.assertEqual([item["notify"] for item in out], [["compose-up"]])
def test_a_pass_through_entry_without_notify_gets_it_too(self) -> None:
with _Tree() as tree:
out = _run([{"src": "a.j2", "dest": "/opt/x/a"}], tree)
self.assertEqual(out[0]["notify"], ["compose-up"])
def test_dest_drops_the_j2_suffix_and_one_trailing_slash(self) -> None:
with _Tree("a.conf.j2") as tree:
out = _run([{"src_glob": "*.conf.j2", "dest_dir": "/opt/x/"}], tree)
self.assertEqual(out[0]["dest"], "/opt/x/a.conf")
def test_matches_are_sorted_and_directories_are_not_matched(self) -> None:
with _Tree("b.j2", "a.j2", "c.j2/") as tree:
out = _run([{"src_glob": "*.j2", "dest_dir": "/opt/x"}], tree)
self.assertEqual([Path(item["src"]).name for item in out], ["a.j2", "b.j2"])
def test_explicit_values_beat_the_defaults_on_both_paths(self) -> None:
with _Tree("a.j2") as tree:
globbed = _run(
[
{
"src_glob": "*.j2",
"dest_dir": "/opt/x",
"mode": "0600",
"notify": ["container-recreate"],
}
],
tree,
)
passed = _run([{"src": "s", "dest": "d", "owner": "www-data"}], tree)
self.assertEqual(globbed[0]["mode"], "0600")
self.assertEqual(globbed[0]["notify"], ["container-recreate"])
self.assertEqual(passed[0]["owner"], "www-data")
def test_an_explicit_empty_notify_is_kept_and_not_refilled(self) -> None:
with _Tree("a.j2") as tree:
globbed = _run(
[{"src_glob": "*.j2", "dest_dir": "/opt/x", "notify": []}], tree
)
passed = _run([{"src": "s", "dest": "d", "notify": []}], tree)
self.assertEqual(globbed[0]["notify"], [])
self.assertEqual(passed[0]["notify"], [])
def test_a_false_condition_survives_the_defaulting(self) -> None:
with _Tree("a.j2") as tree:
out = _run(
[{"src_glob": "*.j2", "dest_dir": "/opt/x", "condition": False}], tree
)
self.assertIs(out[0]["condition"], False)
def test_the_glob_keys_do_not_leak_into_the_rendered_spec(self) -> None:
with _Tree("a.j2") as tree:
out = _run([{"src_glob": "*.j2", "dest_dir": "/opt/x"}], tree)
self.assertNotIn("src_glob", out[0])
self.assertNotIn("dest_dir", out[0])
def test_a_glob_matching_nothing_contributes_no_entry(self) -> None:
with _Tree("a.j2") as tree:
out = _run([{"src_glob": "*.absent", "dest_dir": "/opt/x"}], tree)
self.assertEqual(out, [])
def test_a_glob_entry_without_dest_dir_is_rejected(self) -> None:
with _Tree("a.j2") as tree, self.assertRaises(AnsibleError):
_run([{"src_glob": "*.j2"}], tree)
def test_wrong_term_count_and_bad_shapes_are_rejected(self) -> None:
with _Tree() as tree, patch.object(MODULE, "PROJECT_ROOT", tree.root):
with self.assertRaises(AnsibleError):
MODULE.LookupModule().run([_APP])
with self.assertRaises(AnsibleError):
MODULE.LookupModule().run(["", []])
with self.assertRaises(AnsibleError):
MODULE.LookupModule().run([_APP, "not-a-list"])
with self.assertRaises(AnsibleError):
MODULE.LookupModule().run([_APP, ["not-a-dict"]])
def test_no_templates_at_all_yields_an_empty_list(self) -> None:
with _Tree() as tree, patch.object(MODULE, "PROJECT_ROOT", tree.root):
self.assertEqual(MODULE.LookupModule().run([_APP, None])[0], [])
if __name__ == "__main__":
unittest.main()