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

140 lines
5.3 KiB
Python

import sys
import unittest
from unittest import mock
from unittest.mock import patch
from ansible.errors import AnsibleError
# Make "ansible.module_utils.tls_common" importable during plain unit tests.
import utils.tls_common as _tls_common
from plugins.lookup.tls import LookupModule
sys.modules.setdefault("ansible.module_utils.tls_common", _tls_common)
class TestTlsResolveLookup(unittest.TestCase):
def setUp(self):
self.lookup = LookupModule()
self.lookup._loader = mock.MagicMock()
self.domains = {
"web-app-a": "a.example",
"web-app-b": ["b.example", "b-alt.example"],
"web-app-c": {"primary": "c.example", "api": "api.c.example"},
}
self.applications = {
"web-app-a": {},
"web-app-b": {"server": {"tls": {"flavor": "self_signed"}}},
"web-app-c": {"server": {"tls": {"enabled": False}}},
}
self.base_vars = {
"domains": self.domains,
"applications": self.applications,
"TLS_ENABLED": True,
"TLS_MODE": "letsencrypt",
}
# Route the "domains"/"applications" lookups through
# variables['domains'] / variables['applications'] so tests stay hermetic.
def _get(name, *_a, **_k):
def _run(_terms, variables=None, **__):
return [(variables or {}).get(name, {})]
return mock.MagicMock(run=_run)
loader_patcher = patch("plugins.lookup.tls.lookup_loader")
loader_mock = loader_patcher.start()
loader_mock.get.side_effect = _get
self.addCleanup(loader_patcher.stop)
def test_domain_term_auto_mode(self):
out = self.lookup.run(["a.example"], variables=self.base_vars)[0]
self.assertEqual(out["application_id"], "web-app-a")
self.assertEqual(out["domain"], "a.example")
self.assertTrue(out["enabled"])
self.assertEqual(out["mode"], "letsencrypt")
self.assertEqual(out["protocols"]["web"], "https")
self.assertEqual(out["ports"]["web"], 443)
self.assertEqual(out["url"]["base"], "https://a.example/")
def test_app_term_forced_app(self):
out = self.lookup.run(["web-app-a"], variables=self.base_vars, mode="app")[0]
self.assertEqual(out["application_id"], "web-app-a")
self.assertEqual(out["domain"], "a.example")
def test_app_override_flavor(self):
out = self.lookup.run(["web-app-b"], variables=self.base_vars, mode="app")[0]
self.assertEqual(out["mode"], "self_signed")
def test_app_override_enabled_false(self):
out = self.lookup.run(["web-app-c"], variables=self.base_vars, mode="app")[0]
self.assertFalse(out["enabled"])
self.assertEqual(out["mode"], "off")
self.assertEqual(out["protocols"]["web"], "http")
self.assertEqual(out["ports"]["web"], 80)
def test_domains_all_contains_primary_first(self):
out = self.lookup.run(["web-app-b"], variables=self.base_vars, mode="app")[0]
self.assertEqual(out["domains"]["primary"], "b.example")
self.assertEqual(out["domains"]["all"], ["b.example", "b-alt.example"])
def test_want_path_returns_scalar(self):
val = self.lookup.run(
["web-app-a", "url.base"], variables=self.base_vars, mode="app"
)[0]
self.assertEqual(val, "https://a.example/")
def test_missing_required_vars(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["web-app-a"], variables={})
def test_invalid_tls_mode_default(self):
bad = dict(self.base_vars)
bad["TLS_MODE"] = "invalid"
with self.assertRaises(AnsibleError):
self.lookup.run(["web-app-a"], variables=bad, mode="app")
def test_invalid_forced_mode(self):
with self.assertRaises(AnsibleError):
self.lookup.run(["web-app-a"], variables=self.base_vars, mode="nope")
def test_www_prefix_falls_back_to_bare_domain(self):
out = self.lookup.run(["www.a.example"], variables=self.base_vars)[0]
self.assertEqual(out["application_id"], "web-app-a")
self.assertEqual(out["domain"], "a.example")
def test_www_prefix_with_unknown_bare_still_raises(self):
with self.assertRaises(AnsibleError) as ctx:
self.lookup.run(["www.unknown.example"], variables=self.base_vars)
self.assertIn("not found", str(ctx.exception))
def test_non_www_unregistered_still_raises(self):
with self.assertRaises(AnsibleError) as ctx:
self.lookup.run(["unknown.example"], variables=self.base_vars)
self.assertIn("not found", str(ctx.exception))
def test_term_with_unresolved_jinja_is_templated(self):
class _FakeTemplar:
def __init__(self, mapping):
self._mapping = mapping
self.calls = []
def template(self, value):
self.calls.append(value)
return self._mapping.get(value, value)
fake = _FakeTemplar({"{{ MY_DOMAIN }}": "a.example"})
self.lookup._templar = fake
out = self.lookup.run(["{{ MY_DOMAIN }}"], variables=self.base_vars)[0]
self.assertEqual(out["application_id"], "web-app-a")
self.assertEqual(out["domain"], "a.example")
self.assertIn("{{ MY_DOMAIN }}", fake.calls)
if __name__ == "__main__":
unittest.main()