Files
computer-playbook/tests/unit/plugins/lookup/test_proxy_pass.py
T
kevinveenbirkenbachandClaude Opus 4.8 238bc285c7 refactor(lookups): reach merged config via the lookup-loader SPOT
Consumer lookup plugins now reach the merged config through the Ansible
lookup loader instead of importing get_merged_applications /
get_merged_domains from utils.cache. The merge stays one cached
implementation surfaced via the applications/config/domains/users lookups;
consumers call lookup_loader.get(...).run() (the compose_networks idiom).

- get_merged_applications walks the whole meta/ tree recursively
  (meta/**/*.yml to applications.<app>.<path>, addons and any depth) and
  folds the raw meta/volumes.yml subtree onto applications.<app>.volumes
  after the templar render so the Jinja source: strings stay unrendered.
- 31 consumer lookups rerouted; providers (applications/config/
  applications_current_play/users/domains) and the raw-volume accessors
  (volume/nginx) keep the cache import behind # nocheck: lookup-cache-import.
- New lint test_no_cache_imports_in_lookups enforces the contract.
- nfs_flavor is now the flavor SPOT (svc-storage-nfs-server + nfs_client_src
  resolve via lookup('nfs_flavor')); removed the unused STORAGE_NFS_FLAVOR
  group_var. Extracted extract_default_credentials to
  utils/cache/schema_credentials.py to keep applications.py under the cap.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-28 18:35:06 +02:00

105 lines
3.1 KiB
Python

"""Tests for the proxy_pass lookup wiring (DEPLOYMENT_MODE + overrides)."""
from __future__ import annotations
import unittest
from typing import Any
from unittest import mock
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_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()