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>
199 lines
6.8 KiB
Python
199 lines
6.8 KiB
Python
"""Tests for utils.networks.proxy: resolve_upstream + render_proxy_pass."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from utils.networks.proxy import render_proxy_pass, resolve_upstream
|
|
|
|
|
|
def _apps():
|
|
return {
|
|
"web-app-baserow": {
|
|
"services": {
|
|
"baserow": {
|
|
"ports": {"local": {"http": 8017}, "internal": {"http": 80}},
|
|
},
|
|
},
|
|
},
|
|
"web-app-keycloak": {
|
|
"services": {
|
|
"keycloak": {
|
|
"ports": {"local": {"http": 8032}, "internal": {"http": 8080}},
|
|
},
|
|
},
|
|
},
|
|
"web-app-noport": {"services": {"noport": {"ports": {"local": {}}}}},
|
|
}
|
|
|
|
|
|
class TestResolveUpstream(unittest.TestCase):
|
|
def test_compose_loopback_local_port(self):
|
|
out = resolve_upstream(
|
|
_apps(), "web-app-baserow", "application", "http", "compose"
|
|
)
|
|
self.assertEqual(out, "127.0.0.1:8017")
|
|
|
|
def test_swarm_frontend_uses_entity_alias(self):
|
|
out = resolve_upstream(_apps(), "web-app-baserow", "", "http", "swarm")
|
|
self.assertEqual(out, "baserow:80")
|
|
|
|
def test_swarm_entity_service_key_uses_alias(self):
|
|
out = resolve_upstream(_apps(), "web-app-baserow", "baserow", "http", "swarm")
|
|
self.assertEqual(out, "baserow:80")
|
|
|
|
def test_swarm_sidecar_service_key_uses_full_name(self):
|
|
out = resolve_upstream(
|
|
_apps(), "web-app-baserow", "application", "http", "swarm"
|
|
)
|
|
self.assertEqual(out, "baserow_application:80")
|
|
|
|
def test_swarm_keycloak_entity_uses_alias(self):
|
|
out = resolve_upstream(_apps(), "web-app-keycloak", "keycloak", "http", "swarm")
|
|
self.assertEqual(out, "keycloak:8080")
|
|
|
|
def test_local_port_override_compose(self):
|
|
out = resolve_upstream(
|
|
_apps(),
|
|
"web-app-baserow",
|
|
"application",
|
|
"http",
|
|
"compose",
|
|
local_port="16495",
|
|
)
|
|
self.assertEqual(out, "127.0.0.1:16495")
|
|
|
|
def test_internal_port_override_swarm(self):
|
|
out = resolve_upstream(
|
|
_apps(),
|
|
"web-app-baserow",
|
|
"sso-proxy",
|
|
"http",
|
|
"swarm",
|
|
internal_port="4180",
|
|
)
|
|
self.assertEqual(out, "baserow_sso-proxy:4180")
|
|
|
|
def test_swarm_missing_internal_port_raises(self):
|
|
with self.assertRaises(ValueError):
|
|
resolve_upstream(_apps(), "web-app-noport", "application", "http", "swarm")
|
|
|
|
def test_compose_missing_local_port_raises(self):
|
|
with self.assertRaises(ValueError):
|
|
resolve_upstream(
|
|
_apps(), "web-app-noport", "application", "http", "compose"
|
|
)
|
|
|
|
def test_host_gateway_uses_host_gateway_and_local_port(self):
|
|
out = resolve_upstream(
|
|
_apps(),
|
|
"web-app-baserow",
|
|
"baserow",
|
|
"http",
|
|
"swarm",
|
|
host_gateway=True,
|
|
)
|
|
self.assertEqual(out, "host.docker.internal:8017")
|
|
|
|
def test_host_gateway_wins_over_compose_forced_mode(self):
|
|
out = resolve_upstream(
|
|
_apps(),
|
|
"web-app-baserow",
|
|
"baserow",
|
|
"http",
|
|
"compose",
|
|
host_gateway=True,
|
|
)
|
|
self.assertEqual(out, "host.docker.internal:8017")
|
|
|
|
def test_compose_without_host_gateway_stays_loopback(self):
|
|
out = resolve_upstream(_apps(), "web-app-baserow", "baserow", "http", "compose")
|
|
self.assertEqual(out, "127.0.0.1:8017")
|
|
|
|
|
|
class TestRenderProxyPassSwarm(unittest.TestCase):
|
|
def test_app_request_uri(self):
|
|
out = render_proxy_pass("baserow_application:80", "swarm", tail="request")
|
|
self.assertEqual(
|
|
out,
|
|
'set $proxy_pass_upstream "baserow_application:80";\n'
|
|
" proxy_pass http://$proxy_pass_upstream$request_uri;",
|
|
)
|
|
|
|
def test_plain_request_uri(self):
|
|
out = render_proxy_pass("baserow_application:80", "swarm", tail="plain")
|
|
self.assertTrue(out.endswith("$request_uri;"))
|
|
|
|
def test_oauth2_uri_args(self):
|
|
out = render_proxy_pass("baserow_sso-proxy:4180", "swarm", tail="oauth2")
|
|
self.assertEqual(
|
|
out,
|
|
'set $proxy_pass_upstream "baserow_sso-proxy:4180";\n'
|
|
" proxy_pass http://$proxy_pass_upstream$uri$is_args$args;",
|
|
)
|
|
|
|
def test_literal_path(self):
|
|
out = render_proxy_pass("logout_logout:8000", "swarm", tail="/logout")
|
|
self.assertEqual(
|
|
out,
|
|
'set $proxy_pass_upstream "logout_logout:8000";\n'
|
|
" proxy_pass http://$proxy_pass_upstream/logout;",
|
|
)
|
|
|
|
def test_location_ignored_in_swarm(self):
|
|
a = render_proxy_pass("x_app:80", "swarm", tail="request", location="/sub")
|
|
b = render_proxy_pass("x_app:80", "swarm", tail="request", location="/")
|
|
self.assertEqual(a, b)
|
|
|
|
def test_host_gateway_renders_literal_not_resolver(self):
|
|
out = render_proxy_pass(
|
|
"host.docker.internal:8021", "swarm", tail="request", host_gateway=True
|
|
)
|
|
self.assertEqual(out, "proxy_pass http://host.docker.internal:8021/;")
|
|
self.assertNotIn("set $", out)
|
|
|
|
|
|
class TestRenderProxyPassCompose(unittest.TestCase):
|
|
def test_app_root_location_keeps_slash(self):
|
|
out = render_proxy_pass(
|
|
"127.0.0.1:8017", "compose", tail="request", location="/"
|
|
)
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:8017/;")
|
|
|
|
def test_app_regex_location_no_suffix(self):
|
|
out = render_proxy_pass(
|
|
"127.0.0.1:8032",
|
|
"compose",
|
|
tail="request",
|
|
location="~ ^/realms/[^/]+/protocol/",
|
|
)
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:8032;")
|
|
|
|
def test_app_modifier_stripped(self):
|
|
out = render_proxy_pass(
|
|
"127.0.0.1:80", "compose", tail="request", location="^~ /assets"
|
|
)
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:80/assets;")
|
|
|
|
def test_plain_no_suffix(self):
|
|
out = render_proxy_pass("127.0.0.1:8017", "compose", tail="plain", location="/")
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:8017;")
|
|
|
|
def test_oauth2_no_suffix(self):
|
|
out = render_proxy_pass("127.0.0.1:16495", "compose", tail="oauth2")
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:16495;")
|
|
|
|
def test_literal_path_appended(self):
|
|
out = render_proxy_pass("127.0.0.1:8048", "compose", tail="/logout")
|
|
self.assertEqual(out, "proxy_pass http://127.0.0.1:8048/logout;")
|
|
|
|
|
|
class TestRenderProxyPassGuards(unittest.TestCase):
|
|
def test_empty_authority_raises(self):
|
|
with self.assertRaises(ValueError):
|
|
render_proxy_pass("", "swarm")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|