mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +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>
152 lines
5.3 KiB
Python
152 lines
5.3 KiB
Python
"""Unit tests for plugins/lookup/container_networks.py.
|
|
|
|
Mocks the Ansible-side machinery and asserts that the plugin glue
|
|
resolves variables and chains the underlying ``render_container_networks``
|
|
function correctly. Rendering correctness itself is covered by
|
|
tests.unit.utils.networks.test_render and the integration snapshot test.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
import unittest.mock as mock
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.container_networks import LookupModule
|
|
|
|
|
|
class _Templar:
|
|
def __init__(self, vars_=None):
|
|
self.available_variables = vars_ or {}
|
|
|
|
def template(self, value, **_):
|
|
return value
|
|
|
|
|
|
def _make_lookup(vars_=None):
|
|
lm = LookupModule()
|
|
lm._templar = _Templar(vars_ or {})
|
|
lm._loader = mock.MagicMock()
|
|
return lm
|
|
|
|
|
|
class TestContainerNetworksLookup(unittest.TestCase):
|
|
def test_raises_when_positional_terms_supplied(self):
|
|
lm = _make_lookup({"application_id": "x", "DEPLOYMENT_MODE": "swarm"})
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["unexpected"], variables=lm._templar.available_variables)
|
|
|
|
def test_raises_when_application_id_missing(self):
|
|
lm = _make_lookup({"DEPLOYMENT_MODE": "swarm"})
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run([], variables={})
|
|
|
|
def test_chains_render_with_resolved_vars(self):
|
|
vars_ = {
|
|
"application_id": "web-app-baserow",
|
|
"DEPLOYMENT_MODE": "compose",
|
|
}
|
|
lm = _make_lookup(vars_)
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
return_value="RENDERED",
|
|
) as render_mock,
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
|
|
result = lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(result, ["RENDERED"])
|
|
kwargs = render_mock.call_args.kwargs
|
|
self.assertEqual(kwargs["application_id"], "web-app-baserow")
|
|
self.assertEqual(kwargs["deployment_mode"], "compose")
|
|
|
|
def test_falls_back_to_compose_when_deployment_mode_unset(self):
|
|
vars_ = {"application_id": "web-app-x"}
|
|
lm = _make_lookup(vars_)
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
return_value="RENDERED",
|
|
) as render_mock,
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(render_mock.call_args.kwargs["deployment_mode"], "compose")
|
|
|
|
def test_lookup_closures_pass_arguments_in_expected_order(self):
|
|
"""config_lookup.run([app, path, default]) and
|
|
database_lookup.run([app, key]), both with variables=vars_."""
|
|
vars_ = {
|
|
"application_id": "web-app-x",
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
}
|
|
lm = _make_lookup(vars_)
|
|
|
|
config_calls: list[tuple] = []
|
|
database_calls: list[tuple] = []
|
|
|
|
def _config_run(args, **kwargs):
|
|
config_calls.append((tuple(args), kwargs))
|
|
return [args[2] if len(args) > 2 else None]
|
|
|
|
def _database_run(args, **kwargs):
|
|
database_calls.append((tuple(args), kwargs))
|
|
return [""]
|
|
|
|
def _exercise(*_, lookup_config, lookup_database, **__):
|
|
lookup_config("web-app-x", "services.ldap.enabled", False)
|
|
lookup_database("web-app-x", "shared")
|
|
return "RENDERED"
|
|
|
|
with (
|
|
mock.patch(
|
|
"utils.networks.lookup_context.build_service_registry_from_applications",
|
|
return_value={},
|
|
),
|
|
mock.patch("utils.networks.lookup_context.lookup_loader") as loader_mock,
|
|
mock.patch(
|
|
"plugins.lookup.container_networks.render_container_networks",
|
|
side_effect=_exercise,
|
|
),
|
|
):
|
|
|
|
def _get(name, **_):
|
|
if name == "applications":
|
|
return mock.MagicMock(run=lambda *_a, **_k: [{}])
|
|
if name == "config":
|
|
return mock.MagicMock(run=_config_run)
|
|
return mock.MagicMock(run=_database_run)
|
|
|
|
loader_mock.get.side_effect = _get
|
|
lm.run([], variables=vars_)
|
|
|
|
self.assertEqual(len(config_calls), 1)
|
|
args, kwargs = config_calls[0]
|
|
self.assertEqual(args, ("web-app-x", "services.ldap.enabled", False))
|
|
self.assertEqual(kwargs.get("variables"), vars_)
|
|
|
|
self.assertEqual(len(database_calls), 1)
|
|
args, kwargs = database_calls[0]
|
|
self.assertEqual(args, ("web-app-x", "shared"))
|
|
self.assertEqual(kwargs.get("variables"), vars_)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|