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>
214 lines
7.6 KiB
Python
214 lines
7.6 KiB
Python
"""Tests for the proxy_pass lookup wiring (DEPLOYMENT_MODE + overrides)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
import unittest.mock as mock
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.proxy_pass import LookupModule
|
|
|
|
|
|
class _Templar:
|
|
def __init__(self, vars_=None):
|
|
self.available_variables = vars_ or {}
|
|
|
|
def template(self, value, **_):
|
|
return value
|
|
|
|
|
|
def _apps() -> dict[str, Any]:
|
|
return {
|
|
"web-app-baserow": {
|
|
"services": {
|
|
"baserow": {
|
|
"ports": {"local": {"http": 8017}, "internal": {"http": 80}},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def _run(terms, *, variables, **kwargs):
|
|
lm = LookupModule()
|
|
lm._templar = _Templar(variables)
|
|
lm._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [_apps()])
|
|
return lm.run(terms, variables=variables, **kwargs)
|
|
|
|
|
|
class TestProxyPassLookup(unittest.TestCase):
|
|
def test_missing_application_id_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
_run([], variables={"DEPLOYMENT_MODE": "swarm"})
|
|
|
|
def test_swarm_app_directive_defaults_service_key_to_entity(self):
|
|
out = _run(
|
|
["web-app-baserow"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
tail="request",
|
|
location="/",
|
|
)
|
|
self.assertEqual(
|
|
out[0],
|
|
'set $proxy_pass_upstream "baserow:80";\n'
|
|
" proxy_pass http://$proxy_pass_upstream$request_uri;",
|
|
)
|
|
|
|
def test_compose_app_directive(self):
|
|
out = _run(
|
|
["web-app-baserow", "application", "http"],
|
|
variables={"DEPLOYMENT_MODE": "compose"},
|
|
tail="request",
|
|
location="/",
|
|
)
|
|
self.assertEqual(out[0], "proxy_pass http://127.0.0.1:8017/;")
|
|
|
|
def test_oauth2_internal_port_override_swarm(self):
|
|
out = _run(
|
|
["web-app-baserow", "sso-proxy", "http"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
tail="oauth2",
|
|
internal_port=4180,
|
|
)
|
|
self.assertEqual(
|
|
out[0],
|
|
'set $proxy_pass_upstream "baserow_sso-proxy:4180";\n'
|
|
" proxy_pass http://$proxy_pass_upstream$uri$is_args$args;",
|
|
)
|
|
|
|
def test_swarm_force_bridge_bool_uses_host_gateway_literal(self):
|
|
apps = {
|
|
"web-app-matrix": {
|
|
"services": {
|
|
"matrix": {"ports": {"local": {"http": 8021}}},
|
|
},
|
|
"networks": {"local": {"force_bridge": True}},
|
|
}
|
|
}
|
|
lm = LookupModule()
|
|
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
|
lm._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
|
out = lm.run(
|
|
["web-app-matrix", "matrix", "http"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
)
|
|
self.assertEqual(out[0], "proxy_pass http://host.docker.internal:8021/;")
|
|
|
|
def test_swarm_force_bridge_template_string_is_resolved(self):
|
|
apps = {
|
|
"web-app-matrix": {
|
|
"services": {"matrix": {"ports": {"local": {"http": 8021}}}},
|
|
"networks": {"local": {"force_bridge": "{{ flavor_is_ansible }}"}},
|
|
}
|
|
}
|
|
|
|
class _ResolvingTemplar(_Templar):
|
|
def template(self, value, **_):
|
|
return True if value == "{{ flavor_is_ansible }}" else value
|
|
|
|
lm = LookupModule()
|
|
lm._templar = _ResolvingTemplar({"DEPLOYMENT_MODE": "swarm"})
|
|
lm._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
|
out = lm.run(
|
|
["web-app-matrix", "matrix", "http"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
)
|
|
self.assertEqual(out[0], "proxy_pass http://host.docker.internal:8021/;")
|
|
|
|
def test_unresolved_force_bridge_template_hard_fails(self):
|
|
apps = {
|
|
"web-app-matrix": {
|
|
"services": {"matrix": {"ports": {"local": {"http": 8021}}}},
|
|
"networks": {"local": {"force_bridge": "{{ never_resolves }}"}},
|
|
}
|
|
}
|
|
lm = LookupModule()
|
|
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
|
lm._loader = mock.MagicMock()
|
|
with (
|
|
patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
|
lm.run(
|
|
["web-app-matrix", "matrix", "http"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
)
|
|
|
|
def test_force_bridge_host_gateway_survives_compose_mode_force(self):
|
|
apps = {
|
|
"web-app-matrix": {
|
|
"services": {"matrix": {"ports": {"local": {"http": 8021}}}},
|
|
"networks": {"local": {"force_bridge": True}},
|
|
}
|
|
}
|
|
lm = LookupModule()
|
|
lm._templar = _Templar(
|
|
{"DEPLOYMENT_MODE": "swarm", "compose_mode_force": "compose"}
|
|
)
|
|
lm._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
|
out = lm.run(
|
|
["web-app-matrix", "matrix", "http"],
|
|
variables={
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"compose_mode_force": "compose",
|
|
},
|
|
)
|
|
self.assertEqual(out[0], "proxy_pass http://host.docker.internal:8021/;")
|
|
|
|
def test_force_bridge_compose_real_mode_stays_loopback(self):
|
|
apps = {
|
|
"web-app-matrix": {
|
|
"services": {"matrix": {"ports": {"local": {"http": 8021}}}},
|
|
"networks": {"local": {"force_bridge": True}},
|
|
}
|
|
}
|
|
lm = LookupModule()
|
|
lm._templar = _Templar(
|
|
{"DEPLOYMENT_MODE": "compose", "compose_mode_force": "compose"}
|
|
)
|
|
lm._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock:
|
|
loader_mock.get.return_value = mock.MagicMock(run=lambda *_a, **_k: [apps])
|
|
out = lm.run(
|
|
["web-app-matrix", "matrix", "http"],
|
|
variables={
|
|
"DEPLOYMENT_MODE": "compose",
|
|
"compose_mode_force": "compose",
|
|
},
|
|
)
|
|
self.assertEqual(out[0], "proxy_pass http://127.0.0.1:8021/;")
|
|
|
|
def test_resolution_error_becomes_ansible_error(self):
|
|
lm = LookupModule()
|
|
lm._templar = _Templar({"DEPLOYMENT_MODE": "swarm"})
|
|
lm._loader = mock.MagicMock()
|
|
with (
|
|
patch("plugins.lookup.proxy_pass.lookup_loader") as loader_mock,
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
loader_mock.get.return_value = mock.MagicMock(
|
|
run=lambda *_a, **_k: [
|
|
{"web-app-x": {"services": {"x": {"ports": {"local": {}}}}}}
|
|
]
|
|
)
|
|
lm.run(
|
|
["web-app-x", "application", "http"],
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|