Files
computer-playbook/tests/unit/python/utils/roles/validation/test_invokable.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

160 lines
5.5 KiB
Python

# tests/unit/python/utils/test_invokable.py
from __future__ import annotations
import re
import shutil
import tempfile
from pathlib import Path
from unittest import TestCase, main
from unittest.mock import patch
import utils.roles.validation.invokable as inv
from utils.cache.yaml import dump_yaml
from utils.roles.categories import categories_file
from utils.roles.mapping import ROLE_FILE_VARS_MAIN
class TestInvokable(TestCase):
def setUp(self) -> None:
self.tmp = Path(tempfile.mkdtemp(prefix="invokable_modutils_test_"))
self.roles_dir = self.tmp / "roles"
self.roles_dir.mkdir(parents=True, exist_ok=True)
self.categories_file = categories_file(self.tmp)
dump_yaml(
self.categories_file,
{"categories": [{"invokable_paths": ["web-app", "update", "util-desk"]}]},
)
def mk_role(name: str, application_id: str | None = None) -> None:
rd = self.roles_dir / name
(rd / "vars").mkdir(parents=True, exist_ok=True)
if application_id is not None:
dump_yaml(rd / ROLE_FILE_VARS_MAIN, {"application_id": application_id})
else:
dump_yaml(rd / ROLE_FILE_VARS_MAIN, {})
mk_role("web-app-nextcloud", "web-app-nextcloud")
mk_role("web-app-matomo", "matomo-app")
mk_role("web-svc-nginx", None) # not invokable by our patched paths
mk_role("update", None) # exact match
mk_role("util-desk-custom", None) # prefix match
def tearDown(self) -> None:
shutil.rmtree(self.tmp)
def test_list_invokable_app_ids(self) -> None:
with (
patch.object(inv, "PROJECT_ROOT", self.tmp),
patch.object(
inv,
"_get_invokable_paths",
return_value=["web-app", "update", "util-desk"],
),
):
got = inv.list_invokable_app_ids()
self.assertEqual(
got,
sorted(["web-app-nextcloud", "matomo-app", "update", "util-desk-custom"]),
)
def test_list_invokables_by_type(self) -> None:
with (
patch.object(inv, "PROJECT_ROOT", self.tmp),
patch.object(
inv,
"_get_invokable_paths",
return_value=["web-app", "update", "util-desk"],
),
):
grouped = inv.list_invokables_by_type()
self.assertIn("server", grouped)
self.assertIn("workstation", grouped)
self.assertIn("universal", grouped)
self.assertEqual(grouped["server"], sorted(["web-app-nextcloud", "matomo-app"]))
self.assertEqual(grouped["workstation"], sorted(["util-desk-custom"]))
self.assertEqual(grouped["universal"], ["update"])
def test_list_invokables_by_type_exclude(self) -> None:
"""A synthetic rule exercises the exclude_re path of
DeploymentTypeRule. The historical DEFAULT_RULES exclude (the now
dissolved oauth2-proxy helper role) was removed when its sidecar
logic was absorbed into web-app-keycloak."""
rd = self.roles_dir / "web-app-excluded"
(rd / "vars").mkdir(parents=True, exist_ok=True)
dump_yaml(rd / ROLE_FILE_VARS_MAIN, {})
rules = (
inv.DeploymentTypeRule(
name="server",
include_re=re.compile(r"^(web-app-|web-svc-)"),
exclude_re=re.compile(r"^web-app-excluded$"),
),
inv.DeploymentTypeRule(
name="universal", include_re=re.compile(r".*"), exclude_re=None
),
)
with (
patch.object(inv, "PROJECT_ROOT", self.tmp),
patch.object(
inv,
"_get_invokable_paths",
return_value=["web-app", "update", "util-desk"],
),
):
grouped = inv.list_invokables_by_type(rules=rules)
self.assertNotIn("web-app-excluded", grouped["server"])
def test_list_invokables_by_type_skip_mode(self) -> None:
def fake_skip(role_dir):
return ["compose"] if role_dir.name == "web-app-matomo" else []
with (
patch.object(inv, "PROJECT_ROOT", self.tmp),
patch.object(
inv,
"_get_invokable_paths",
return_value=["web-app", "update", "util-desk"],
),
patch.object(inv, "_role_skip_modes", side_effect=fake_skip),
):
excluded = inv.list_invokables_by_type(skip_mode="compose")
kept = inv.list_invokables_by_type(skip_mode="swarm")
self.assertNotIn("matomo-app", excluded["server"])
self.assertIn("matomo-app", kept["server"])
self.assertIn("web-app-nextcloud", excluded["server"])
def test_types_from_group_names(self) -> None:
"""``all`` and ``foo`` are not invokable and drop out; the three
invokable names map to server, workstation and the universal
leftover respectively."""
with (
patch.object(inv, "PROJECT_ROOT", self.tmp),
patch.object(
inv,
"_get_invokable_paths",
return_value=["web-app", "update", "util-desk"],
),
):
got = inv.types_from_group_names(
[
"all",
"web-app-nextcloud",
"util-desk-custom",
"update",
"foo",
]
)
self.assertEqual(got, ["server", "universal", "workstation"])
if __name__ == "__main__":
main()