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

170 lines
5.1 KiB
Python

"""Unit tests for the compose_volumes lookup plugin.
Pins the contract for the lookup that wraps the `compose_volumes`
rendering function and auto-wires `application_id`, the `applications`
registry, DEPLOYMENT_MODE, and `storage` from the templating context.
"""
from __future__ import annotations
import importlib.util
import os
import unittest
import unittest.mock as mock
from ansible.errors import AnsibleError
from . import PROJECT_ROOT
_DIR_VAR_LIB = os.environ["INFINITO_DIR_VAR_LIB"]
def _load_lookup():
spec = importlib.util.spec_from_file_location(
"lookup_compose_volumes",
str(PROJECT_ROOT / "plugins/lookup/compose_volumes.py"),
)
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=None):
self.available_variables = available_variables or {}
def template(self, value):
return value
def _vars(**extra):
base = {
"application_id": "web-app-x",
"DEPLOYMENT_MODE": "compose",
"DIR_VAR_LIB": _DIR_VAR_LIB,
}
base.update(extra)
return base
class TestComposeVolumesLookup(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.module = _load_lookup()
cls.LookupModule = cls.module.LookupModule
def _make(self, variables):
lm = self.LookupModule()
lm._templar = _DummyTemplar(variables)
lm._loader = None
return lm
def _run(self, vars_, render, **kwargs):
lm = self._make(vars_)
with (
mock.patch.object(self.module, "lookup_loader") as loader_mock,
mock.patch.object(self.module, "compose_volumes", side_effect=render),
):
loader_mock.get.return_value = mock.MagicMock(
run=lambda *_a, **_k: [{"web-app-x": {}}]
)
return lm.run([], variables=vars_, **kwargs)
def test_application_id_auto_wires_from_vars(self):
result = self._run(
_vars(),
lambda apps, app_id, **kw: (
f"called({app_id}, mode={kw.get('deployment_mode')})"
),
)
self.assertEqual(result, ["called(web-app-x, mode=compose)"])
def test_deployment_mode_auto_wires_from_vars(self):
captured = {}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(_vars(DEPLOYMENT_MODE="swarm"), _render)
self.assertEqual(captured.get("deployment_mode"), "swarm")
def test_compose_mode_force_overrides_deployment_mode(self):
captured = {}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(_vars(DEPLOYMENT_MODE="swarm", compose_mode_force="compose"), _render)
self.assertEqual(captured.get("deployment_mode"), "compose")
def test_storage_auto_wires_from_vars(self):
captured = {}
storage = {"backend": "nfs", "nfs": {"server": "10.0.0.1"}}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(_vars(DEPLOYMENT_MODE="swarm", storage=storage), _render)
self.assertEqual(captured.get("storage"), storage)
def test_explicit_kwargs_override_auto_wired(self):
captured = {}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(
_vars(DEPLOYMENT_MODE="swarm"),
_render,
deployment_mode="compose",
storage={"backend": "local"},
)
self.assertEqual(captured.get("deployment_mode"), "compose")
self.assertEqual(captured.get("storage"), {"backend": "local"})
def test_extra_volumes_kwarg_passes_through(self):
captured = {}
extra = {"data": {"name": "my-data"}}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(_vars(), _render, extra_volumes=extra)
self.assertEqual(captured.get("extra_volumes"), extra)
def test_missing_deployment_mode_defaults_to_compose(self):
captured = {}
vars_ = {"application_id": "web-app-x", "DIR_VAR_LIB": _DIR_VAR_LIB}
def _render(apps, app_id, **kw):
captured.update(kw)
return ""
self._run(vars_, _render)
self.assertEqual(captured.get("deployment_mode"), "compose")
def test_terms_raise(self):
lm = self._make(_vars())
with self.assertRaises(AnsibleError):
lm.run(["web-app-x"], variables=_vars())
def test_missing_application_id_raises(self):
lm = self._make({"DEPLOYMENT_MODE": "compose", "DIR_VAR_LIB": _DIR_VAR_LIB})
with self.assertRaises(AnsibleError):
lm.run([], variables={"DEPLOYMENT_MODE": "compose"})
def test_empty_application_id_raises(self):
lm = self._make(_vars(application_id=""))
with self.assertRaises(AnsibleError):
lm.run([], variables=_vars(application_id=""))
if __name__ == "__main__":
unittest.main(verbosity=2)