mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-06 01:40:19 +00:00
test(lookup): add deterministic Jinja templating for docker_cards unit tests
- Render WEB_PROTOCOL and domain_url via templar in docker_cards lookup - Add DummyTemplar with Ansible-like bool filter for stable unit testing - Refactor tests to use templar injection via _run_lookup helper - Add coverage for Jinja-based WEB_PROTOCOL and domain rendering This ensures lookup behavior matches real Ansible runtime while keeping unit tests deterministic. https://chatgpt.com/share/696ac8bf-8c0c-800f-a3e7-db4bfb2a8f3f
This commit is contained in:
2 files changed
+116
-32
No files matched your search
@@ -111,10 +111,14 @@ class LookupModule(LookupBase):
|
||||
elif isinstance(domain_url, dict):
|
||||
domain_url = next(iter(domain_url.values()))
|
||||
|
||||
# Construct the URL using the domain_url if available.
|
||||
if "WEB_PROTOCOL" not in variables:
|
||||
raise AnsibleError("WEB_PROTOCOL is required to build application URLs")
|
||||
protocol = variables["WEB_PROTOCOL"]
|
||||
|
||||
protocol = self._templar.template(variables["WEB_PROTOCOL"]).strip()
|
||||
|
||||
# domain_url kann list/dict sein; nach deiner Normalisierung:
|
||||
domain_url = self._templar.template(domain_url).strip() if domain_url else ""
|
||||
|
||||
url = f"{protocol}://{domain_url}" if domain_url else ""
|
||||
|
||||
iframe = get_app_conf(
|
||||
|
||||
@@ -3,7 +3,10 @@ import sys
|
||||
import tempfile
|
||||
import shutil
|
||||
import unittest
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from jinja2 import Environment, StrictUndefined
|
||||
|
||||
|
||||
# Adjust the PYTHONPATH to include the lookup_plugins folder from the web-app-desktop role.
|
||||
sys.path.insert(
|
||||
@@ -16,6 +19,54 @@ sys.path.insert(
|
||||
from docker_cards import LookupModule
|
||||
|
||||
|
||||
def _ansible_bool(value):
|
||||
"""
|
||||
Minimal Ansible-like bool filter for unit tests.
|
||||
Mirrors common Ansible truthy/falsey handling for strings.
|
||||
"""
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if value is None:
|
||||
return False
|
||||
if isinstance(value, (int, float)):
|
||||
return value != 0
|
||||
if isinstance(value, str):
|
||||
v = value.strip().lower()
|
||||
if v in {"y", "yes", "true", "on", "1"}:
|
||||
return True
|
||||
if v in {"n", "no", "false", "off", "0", ""}:
|
||||
return False
|
||||
return bool(value)
|
||||
|
||||
|
||||
class DummyTemplar:
|
||||
"""
|
||||
Small, deterministic templating stub for unit tests.
|
||||
It is intentionally minimal: only supports rendering Jinja strings
|
||||
and provides an Ansible-like `bool` filter.
|
||||
"""
|
||||
|
||||
def __init__(self, variables):
|
||||
self._vars = variables
|
||||
self._env = Environment(undefined=StrictUndefined)
|
||||
self._env.filters["bool"] = _ansible_bool
|
||||
|
||||
def template(self, value):
|
||||
if value is None:
|
||||
return value
|
||||
|
||||
# Keep non-strings untouched
|
||||
if not isinstance(value, str):
|
||||
return value
|
||||
|
||||
# Only render if it looks like a Jinja template
|
||||
if "{{" in value and "}}" in value:
|
||||
tmpl = self._env.from_string(value)
|
||||
return tmpl.render(**self._vars)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
class TestDockerCardsLookup(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create a temporary directory to simulate the roles directory.
|
||||
@@ -60,11 +111,14 @@ galaxy_info:
|
||||
"group_names": ["portfolio"],
|
||||
}
|
||||
|
||||
def _run_lookup(self, lookup_module, fake_variables):
|
||||
# Provide deterministic templating behavior for unit tests.
|
||||
lookup_module._templar = DummyTemplar(fake_variables)
|
||||
return lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
|
||||
def test_lookup_when_group_includes_application_id(self):
|
||||
# Instantiate the LookupModule.
|
||||
lookup_module = LookupModule()
|
||||
|
||||
# Define dummy variables including group_names that contain the application_id "portfolio".
|
||||
fake_variables = {
|
||||
"domains": {"portfolio": "myportfolio.com"},
|
||||
"applications": {
|
||||
@@ -74,15 +128,13 @@ galaxy_info:
|
||||
"WEB_PROTOCOL": "https",
|
||||
}
|
||||
|
||||
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
# The result is a list containing one list of card dictionaries.
|
||||
self.assertIsInstance(result, list)
|
||||
self.assertEqual(len(result), 1)
|
||||
|
||||
cards = result[0]
|
||||
self.assertIsInstance(cards, list)
|
||||
# Since "portfolio" is in group_names, one card should be present.
|
||||
self.assertEqual(len(cards), 1)
|
||||
|
||||
card = cards[0]
|
||||
@@ -97,40 +149,27 @@ galaxy_info:
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["WEB_PROTOCOL"] = "https"
|
||||
|
||||
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertIsInstance(result, list)
|
||||
self.assertEqual(len(result), 1)
|
||||
|
||||
cards = result[0]
|
||||
self.assertIsInstance(cards, list)
|
||||
self.assertEqual(len(cards), 1)
|
||||
|
||||
card = cards[0]
|
||||
self.assertEqual(card["url"], "https://myportfolio.com")
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_uses_http_when_web_protocol_is_http(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables["WEB_PROTOCOL"] = "http"
|
||||
|
||||
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertIsInstance(result, list)
|
||||
self.assertEqual(len(result), 1)
|
||||
|
||||
cards = result[0]
|
||||
self.assertIsInstance(cards, list)
|
||||
self.assertEqual(len(cards), 1)
|
||||
|
||||
card = cards[0]
|
||||
self.assertEqual(card["url"], "http://myportfolio.com")
|
||||
self.assertEqual(result[0][0]["url"], "http://myportfolio.com")
|
||||
|
||||
def test_lookup_raises_error_when_web_protocol_is_missing(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
fake_variables.pop("WEB_PROTOCOL", None)
|
||||
|
||||
# Still set templar so the plugin behaves like runtime (even if it errors before templating).
|
||||
lookup_module._templar = DummyTemplar(fake_variables)
|
||||
|
||||
with self.assertRaises(AnsibleError) as ctx:
|
||||
lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
|
||||
@@ -147,14 +186,55 @@ galaxy_info:
|
||||
"WEB_PROTOCOL": "https",
|
||||
}
|
||||
|
||||
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertIsInstance(result, list)
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertEqual(len(result[0]), 0)
|
||||
|
||||
cards = result[0]
|
||||
self.assertIsInstance(cards, list)
|
||||
self.assertEqual(len(cards), 0)
|
||||
def test_lookup_url_renders_web_protocol_jinja_https(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["TLS_ENABLED"] = True
|
||||
fake_variables["WEB_PROTOCOL"] = "{{ 'https' if TLS_ENABLED | bool else 'http' }}"
|
||||
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_renders_web_protocol_jinja_http(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["TLS_ENABLED"] = False
|
||||
fake_variables["WEB_PROTOCOL"] = "{{ 'https' if TLS_ENABLED | bool else 'http' }}"
|
||||
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "http://myportfolio.com")
|
||||
|
||||
def test_lookup_url_renders_domain_url_jinja(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["domains"] = {"portfolio": "{{ DOMAIN_PRIMARY }}"}
|
||||
fake_variables["DOMAIN_PRIMARY"] = "myportfolio.com"
|
||||
fake_variables["WEB_PROTOCOL"] = "https"
|
||||
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
def test_lookup_url_renders_web_protocol_jinja_when_tls_enabled_is_string(self):
|
||||
lookup_module = LookupModule()
|
||||
fake_variables = self._base_fake_variables()
|
||||
|
||||
fake_variables["TLS_ENABLED"] = "true"
|
||||
fake_variables["WEB_PROTOCOL"] = "{{ 'https' if TLS_ENABLED | bool else 'http' }}"
|
||||
|
||||
result = self._run_lookup(lookup_module, fake_variables)
|
||||
|
||||
self.assertEqual(result[0][0]["url"], "https://myportfolio.com")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user