Files
computer-playbook/tests/unit/python/plugins/lookup/test_service.py
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

361 lines
12 KiB
Python

from __future__ import annotations
import unittest
import unittest.mock as mock
from unittest.mock import patch
from ansible.errors import AnsibleError
from plugins.lookup.service import LookupModule
_SERVICE_REGISTRY = {
"matomo": {"role": "web-app-matomo"},
"cdn": {"role": "web-svc-cdn"},
"css": {"role": "web-svc-cdn", "canonical": "cdn"},
"logout": {"role": "web-svc-logout"},
"collab": {"role": "web-svc-collab"},
}
def _run(
terms,
applications,
group_names,
service_registry=None,
static_registry=None,
**kwargs,
):
loader_patch = patch("plugins.lookup.service.lookup_loader")
patches = [
loader_patch,
patch(
"plugins.lookup.service.build_service_registry_from_roles_dir",
return_value=static_registry or {},
),
]
if service_registry is not None:
patches.append(
patch(
"plugins.lookup.service.build_service_registry_from_applications",
return_value=service_registry,
)
)
started = [p.start() for p in patches]
loader_mock = started[0]
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [applications])
try:
lm = LookupModule()
lm._loader = mock.MagicMock()
return lm.run(
terms,
variables={"group_names": group_names},
**kwargs,
)
finally:
for p in reversed(patches):
p.stop()
class TestServiceDirect(unittest.TestCase):
def setUp(self):
self.applications = {
"web-app-foo": {
"services": {
"matomo": {"enabled": True, "shared": True},
"css": {"enabled": True, "shared": False},
"logout": {"enabled": False, "shared": True},
}
},
"web-app-bar": {"services": {}},
}
def _get(self, term, group_names=None):
return _run(
[term],
self.applications,
group_names if group_names is not None else ["web-app-foo"],
service_registry=_SERVICE_REGISTRY,
)[0]
def test_required_true_when_enabled_and_shared(self):
self.assertTrue(self._get("matomo")["required"])
def test_required_false_when_enabled_only(self):
self.assertFalse(self._get("cdn")["required"])
def test_required_false_when_shared_only(self):
self.assertFalse(self._get("logout")["required"])
def test_required_false_when_service_absent(self):
self.assertFalse(self._get("collab")["required"])
def test_required_false_when_group_names_do_not_match(self):
self.assertFalse(self._get("matomo", ["web-app-unknown"])["required"])
def test_required_true_when_any_app_qualifies(self):
self.assertTrue(self._get("matomo", ["web-app-bar", "web-app-foo"])["required"])
def test_enabled_shared_flags_are_aggregated_via_canonical_alias(self):
result = self._get("cdn")
self.assertTrue(result["enabled"])
self.assertFalse(result["shared"])
def test_lookup_by_role_name_uses_same_primary_service(self):
by_key = self._get("cdn")
by_role = self._get("web-svc-cdn")
self.assertEqual(by_key, by_role)
def test_multiple_terms(self):
results = _run(
["matomo", "cdn", "logout"],
self.applications,
["web-app-foo"],
service_registry=_SERVICE_REGISTRY,
)
self.assertEqual(len(results), 3)
self.assertTrue(results[0]["required"])
self.assertFalse(results[1]["required"])
self.assertFalse(results[2]["required"])
def test_single_list_term_is_flattened(self):
results = _run(
[["matomo", "cdn", "logout"]],
self.applications,
["web-app-foo"],
service_registry=_SERVICE_REGISTRY,
)
self.assertEqual(len(results), 3)
self.assertTrue(results[0]["required"])
self.assertFalse(results[1]["required"])
self.assertFalse(results[2]["required"])
def test_local_true_when_enabled_and_not_shared(self):
self.assertTrue(self._get("cdn")["local"])
def test_local_false_when_enabled_and_shared(self):
self.assertFalse(self._get("matomo")["local"])
def test_local_false_when_shared_only(self):
self.assertFalse(self._get("logout")["local"])
def test_local_false_when_service_absent(self):
self.assertFalse(self._get("collab")["local"])
def test_local_false_when_group_names_do_not_match(self):
self.assertFalse(self._get("cdn", ["web-app-unknown"])["local"])
def test_local_and_required_are_mutually_exclusive(self):
for term in ("matomo", "cdn", "logout", "collab"):
result = self._get(term)
if result["enabled"]:
self.assertFalse(
result["local"] and result["required"],
msg=f"{term}: local and required must not both be True",
)
def test_empty_terms_returns_empty(self):
self.assertEqual(
_run(
[],
self.applications,
["web-app-foo"],
service_registry=_SERVICE_REGISTRY,
),
[],
)
class TestServiceTransitive(unittest.TestCase):
def setUp(self):
self.applications = {
"web-app-nextcloud": {"services": {"collab": {"enabled": True}}},
"web-svc-collab": {
"services": {
"matomo": {"enabled": True, "shared": True},
}
},
}
def test_transitive_required_resolved(self):
result = _run(
["matomo"],
self.applications,
["web-app-nextcloud"],
service_registry=_SERVICE_REGISTRY,
)[0]
self.assertTrue(result["required"])
def test_transitive_requires_shared_at_target(self):
applications = {
"web-app-nextcloud": {"services": {"collab": {"enabled": True}}},
"web-svc-collab": {"services": {"matomo": {"enabled": True}}},
}
result = _run(
["matomo"],
applications,
["web-app-nextcloud"],
service_registry=_SERVICE_REGISTRY,
)[0]
self.assertFalse(result["required"])
def test_direct_provider_is_not_required_without_shared_flag(self):
result = _run(
["collab"],
self.applications,
["web-app-nextcloud"],
service_registry=_SERVICE_REGISTRY,
)[0]
self.assertFalse(result["required"])
class TestServiceDiscoveryWithoutExplicitRegistry(unittest.TestCase):
def test_role_local_provider_metadata_is_discovered_from_applications(self):
applications = {
"web-app-dashboard": {
"services": {
"dashboard": {"enabled": False, "shared": True},
}
},
"web-app-foo": {
"services": {
"dashboard": {"enabled": True, "shared": True},
}
},
}
result = _run(["dashboard"], applications, ["web-app-foo"])[0]
self.assertEqual(result["role"], "web-app-dashboard")
self.assertTrue(result["required"])
def test_provides_and_canonical_are_discovered_from_provider_configs(self):
applications = {
"web-app-keycloak": {
"services": {
"keycloak": {
"enabled": False,
"shared": True,
"provides": "oidc",
}
}
},
"web-svc-cdn": {
"services": {
"cdn": {"enabled": False, "shared": True},
"css": {"enabled": False, "shared": True, "canonical": "cdn"},
}
},
"web-app-foo": {
"services": {
"oidc": {"enabled": True, "shared": True},
"css": {"enabled": True, "shared": False},
}
},
}
oidc = _run(["oidc"], applications, ["web-app-foo"])[0]
cdn = _run(["cdn"], applications, ["web-app-foo"])[0]
self.assertEqual(oidc["role"], "web-app-keycloak")
self.assertTrue(oidc["required"])
self.assertEqual(cdn["role"], "web-svc-cdn")
self.assertTrue(cdn["enabled"])
self.assertFalse(cdn["required"])
class TestServiceCycleGuard(unittest.TestCase):
def test_cycle_does_not_loop(self):
applications = {
"svc-a": {"services": {"svc-b": {"enabled": True}}},
"svc-b": {"services": {"svc-a": {"enabled": True}}},
}
service_registry = {
"svc-a": {"role": "svc-a"},
"svc-b": {"role": "svc-b"},
"logout": {"role": "web-svc-logout"},
}
result = _run(
["logout"],
applications,
["svc-a"],
service_registry=service_registry,
)[0]
self.assertFalse(result["required"])
def test_cycle_found_if_service_present(self):
applications = {
"svc-a": {
"services": {
"svc-b": {"enabled": True},
"logout": {"enabled": True, "shared": True},
}
},
"svc-b": {"services": {"svc-a": {"enabled": True}}},
}
service_registry = {
"svc-a": {"role": "svc-a"},
"svc-b": {"role": "svc-b"},
"logout": {"role": "web-svc-logout"},
}
result = _run(
["logout"],
applications,
["svc-b"],
service_registry=service_registry,
)[0]
self.assertTrue(result["required"])
class TestServiceErrors(unittest.TestCase):
def test_raises_when_group_names_not_list(self):
with (
patch("plugins.lookup.service.lookup_loader") as loader_mock,
self.assertRaises(AnsibleError),
):
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
lm = LookupModule()
lm._loader = mock.MagicMock()
lm.run(
["matomo"],
variables={"group_names": "not-a-list"},
)
def test_raises_when_term_empty(self):
with self.assertRaises(AnsibleError):
_run([" "], {}, [], service_registry=_SERVICE_REGISTRY)
def test_raises_when_term_unknown_in_both_registries(self):
with self.assertRaises(AnsibleError):
_run(["totally-unknown-key"], {}, [], service_registry=_SERVICE_REGISTRY)
class TestServiceStaticFallback(unittest.TestCase):
def test_out_of_play_key_resolves_via_static_registry(self):
result = _run(
["css"],
{"web-app-foo": {"services": {}}},
["web-app-foo"],
service_registry={"matomo": {"role": "web-app-matomo"}},
static_registry=_SERVICE_REGISTRY,
)[0]
self.assertEqual(result["id"], "css")
self.assertEqual(result["role"], "web-svc-cdn")
self.assertFalse(result["enabled"])
self.assertFalse(result["shared"])
self.assertFalse(result["required"])
self.assertFalse(result["local"])
def test_out_of_play_alias_aggregates_canonical_consumer_flags(self):
result = _run(
["css"],
{"web-app-foo": {"services": {"cdn": {"enabled": True, "shared": True}}}},
["web-app-foo"],
service_registry={"matomo": {"role": "web-app-matomo"}},
static_registry=_SERVICE_REGISTRY,
)[0]
self.assertTrue(result["enabled"])
self.assertTrue(result["required"])
if __name__ == "__main__":
unittest.main()