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>
251 lines
7.9 KiB
Python
251 lines
7.9 KiB
Python
"""Unit tests for ``plugins/lookup/service_extensions.py``.
|
|
|
|
The lookup walks ``group_names`` and returns a
|
|
``{consumer_id: [extension, …]}`` map sourced from each role's merged
|
|
``meta/services.yml``. The ``applications`` lookup is mocked via
|
|
``lookup_loader`` so the tests stay hermetic.
|
|
"""
|
|
|
|
import importlib.util
|
|
import unittest
|
|
import unittest.mock as mock
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
|
|
def _load_module(rel_path: str, name: str):
|
|
path = PROJECT_ROOT / rel_path
|
|
spec = importlib.util.spec_from_file_location(name, str(path))
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec and spec.loader
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
class _DummyTemplar:
|
|
def __init__(self, available_variables):
|
|
self.available_variables = available_variables
|
|
|
|
|
|
class ServiceExtensionsLookupTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.mod = _load_module(
|
|
"plugins/lookup/service_extensions.py",
|
|
"lookup_service_extensions",
|
|
)
|
|
|
|
def _make_lookup(self, available_vars: dict | None = None):
|
|
lm = self.mod.LookupModule()
|
|
lm._templar = _DummyTemplar(available_vars or {})
|
|
lm._loader = mock.MagicMock()
|
|
return lm
|
|
|
|
def _run(
|
|
self,
|
|
terms,
|
|
applications: dict,
|
|
group_names: list[str],
|
|
):
|
|
lookup = self._make_lookup({"group_names": group_names})
|
|
with patch.object(self.mod, "lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(
|
|
run=lambda *_a, **_k: [applications]
|
|
)
|
|
return lookup.run(terms, variables={"group_names": group_names})
|
|
|
|
def test_zero_terms_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run([], {}, [])
|
|
|
|
def test_two_terms_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run(["postgres", "extra"], {}, [])
|
|
|
|
def test_blank_service_name_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run([" "], {}, [])
|
|
|
|
def test_non_list_group_names_raises(self):
|
|
lookup = self._make_lookup({"group_names": "not-a-list"})
|
|
with (
|
|
patch.object(self.mod, "lookup_loader") as loader_mock,
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
lookup.run(
|
|
["postgres"],
|
|
variables={"group_names": "not-a-list"},
|
|
)
|
|
|
|
def test_role_with_extensions_is_included(self):
|
|
applications = {
|
|
"web-app-bookwyrm": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-bookwyrm"])
|
|
self.assertEqual(result, [{"web-app-bookwyrm": ["bloom"]}])
|
|
|
|
def test_role_with_extensions_and_shared_false_is_included(self):
|
|
applications = {
|
|
"web-app-bookwyrm": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"shared": False,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-bookwyrm"])
|
|
self.assertEqual(result, [{"web-app-bookwyrm": ["bloom"]}])
|
|
|
|
def test_role_not_in_group_names_is_skipped(self):
|
|
applications = {
|
|
"web-app-bookwyrm": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, [])
|
|
self.assertEqual(result, [{}])
|
|
|
|
def test_role_with_disabled_service_is_skipped(self):
|
|
applications = {
|
|
"web-app-bookwyrm": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": False,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-bookwyrm"])
|
|
self.assertEqual(result, [{}])
|
|
|
|
def test_role_without_extensions_field_is_omitted(self):
|
|
applications = {
|
|
"web-app-foo": {
|
|
"services": {"postgres": {"enabled": True}},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-foo"])
|
|
self.assertEqual(result, [{}])
|
|
|
|
def test_role_with_empty_extensions_list_is_omitted(self):
|
|
applications = {
|
|
"web-app-foo": {
|
|
"services": {
|
|
"postgres": {"enabled": True, "extensions": []},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-foo"])
|
|
self.assertEqual(result, [{}])
|
|
|
|
def test_multiple_roles_are_aggregated(self):
|
|
applications = {
|
|
"web-app-bookwyrm": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
"web-app-discourse": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["pg_trgm", "vector"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(
|
|
["postgres"],
|
|
applications,
|
|
["web-app-bookwyrm", "web-app-discourse"],
|
|
)
|
|
self.assertEqual(
|
|
result,
|
|
[
|
|
{
|
|
"web-app-bookwyrm": ["bloom"],
|
|
"web-app-discourse": ["pg_trgm", "vector"],
|
|
}
|
|
],
|
|
)
|
|
|
|
def test_different_service_keys_resolve_independently(self):
|
|
applications = {
|
|
"web-app-foo": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom"],
|
|
},
|
|
"mariadb": {
|
|
"enabled": True,
|
|
"extensions": ["someplugin"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
postgres = self._run(["postgres"], applications, ["web-app-foo"])[0]
|
|
mariadb = self._run(["mariadb"], applications, ["web-app-foo"])[0]
|
|
self.assertEqual(postgres, {"web-app-foo": ["bloom"]})
|
|
self.assertEqual(mariadb, {"web-app-foo": ["someplugin"]})
|
|
|
|
def test_non_dict_app_config_is_skipped(self):
|
|
applications = {
|
|
"web-app-foo": "not-a-dict",
|
|
"web-app-bar": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom"],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(
|
|
["postgres"],
|
|
applications,
|
|
["web-app-foo", "web-app-bar"],
|
|
)
|
|
self.assertEqual(result, [{"web-app-bar": ["bloom"]}])
|
|
|
|
def test_extensions_with_blanks_are_filtered(self):
|
|
applications = {
|
|
"web-app-foo": {
|
|
"services": {
|
|
"postgres": {
|
|
"enabled": True,
|
|
"extensions": ["bloom", " ", ""],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
result = self._run(["postgres"], applications, ["web-app-foo"])
|
|
self.assertEqual(result, [{"web-app-foo": ["bloom"]}])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|