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>
239 lines
7.8 KiB
Python
239 lines
7.8 KiB
Python
# tests/unit/python/utils/test_templating.py
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.filter.ca_trust_paths import ca_cert_host
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from utils.templating.ansible import (
|
|
render_ansible_strict,
|
|
)
|
|
|
|
|
|
class _FakeTemplarLookupUndefined:
|
|
"""
|
|
Simulates the earlier failure:
|
|
- templar exists
|
|
- but cannot evaluate lookup()
|
|
- returns string unchanged => must trigger fallback evaluator
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.available_variables = {}
|
|
|
|
def template(self, s, *args, **kwargs):
|
|
return s
|
|
|
|
|
|
class _FakeTemplarRejectsVariablesKwarg:
|
|
"""
|
|
Simulates the earlier failure:
|
|
Templar.template() got an unexpected keyword argument 'variables'
|
|
We ensure our helper does NOT pass variables kwarg.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.available_variables = {}
|
|
|
|
def template(self, s, fail_on_undefined=False):
|
|
if s.strip() == "{{ CA_ROOT.cert_host }}":
|
|
ca_root = self.available_variables.get("CA_ROOT", {})
|
|
return ca_root.get("cert_host", s)
|
|
return s
|
|
|
|
|
|
class TestTemplatingRenderStrict(unittest.TestCase):
|
|
def test_plain_string_passthrough(self):
|
|
out = render_ansible_strict(
|
|
templar=None,
|
|
raw="/etc/infinito/ca/root-ca.crt",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables={},
|
|
)
|
|
self.assertEqual(out, "/etc/infinito/ca/root-ca.crt")
|
|
|
|
def test_none_hard_fails(self):
|
|
with self.assertRaises(AnsibleError):
|
|
render_ansible_strict(
|
|
templar=None,
|
|
raw=None,
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables={},
|
|
)
|
|
|
|
def test_empty_after_render_hard_fails(self):
|
|
with self.assertRaises(AnsibleError):
|
|
render_ansible_strict(
|
|
templar=None,
|
|
raw=" ",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables={},
|
|
)
|
|
|
|
def test_env_lookup_fallback_when_lookup_undefined_like(self):
|
|
with patch.dict(os.environ, {"domain": "infinito.localhost"}, clear=False):
|
|
out = render_ansible_strict(
|
|
templar=_FakeTemplarLookupUndefined(),
|
|
raw="{{ lookup('env', 'domain') | default('infinito.localhost', true) }}",
|
|
var_name="CA_TRUST.trust_name",
|
|
err_prefix="compose_ca_inject_cmd",
|
|
variables={},
|
|
)
|
|
self.assertEqual(out, "infinito.localhost")
|
|
|
|
def test_env_lookup_default_when_env_missing(self):
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
out = render_ansible_strict(
|
|
templar=_FakeTemplarLookupUndefined(),
|
|
raw="{{ lookup('env', 'DOMAIN') | default('infinito.localhost', true) }}",
|
|
var_name="CA_TRUST.trust_name",
|
|
err_prefix="compose_ca_inject_cmd",
|
|
variables={},
|
|
)
|
|
self.assertEqual(out, "infinito.localhost")
|
|
|
|
def test_ca_root_nested_var_resolves_without_templar(self):
|
|
#
|
|
variables = {
|
|
"SOFTWARE_DOMAIN": "infinito.nexus",
|
|
"CA_ROOT": {
|
|
"cert_host": "/etc/{{ SOFTWARE_DOMAIN }}/ca/root-ca.crt",
|
|
},
|
|
}
|
|
out = render_ansible_strict(
|
|
templar=None,
|
|
raw="{{ CA_ROOT.cert_host }}",
|
|
var_name="CA_TRUST.cert_host",
|
|
err_prefix="compose_ca_inject_cmd",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out, ca_cert_host("infinito.nexus"))
|
|
|
|
def test_templar_available_variables_is_used_no_variables_kwarg(self):
|
|
#
|
|
variables = {"CA_ROOT": {"cert_host": "/etc/infinito/ca/root-ca.crt"}}
|
|
out = render_ansible_strict(
|
|
templar=_FakeTemplarRejectsVariablesKwarg(),
|
|
raw="{{ CA_ROOT.cert_host }}",
|
|
var_name="CA_TRUST.cert_host",
|
|
err_prefix="compose_ca_inject_cmd",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out, "/etc/infinito/ca/root-ca.crt")
|
|
|
|
def test_templar_merges_variables_onto_available_variables(self):
|
|
# Regression guard: prev available_variables (e.g. ansible_facts,
|
|
# hostvars) must survive the call — caller-supplied variables are
|
|
# layered on top, not substituted for the existing map.
|
|
class _FakeTemplarRecordingAvail:
|
|
def __init__(self, preset):
|
|
self.available_variables = dict(preset)
|
|
self.seen_on_call = None
|
|
|
|
def template(self, s, fail_on_undefined=False):
|
|
self.seen_on_call = dict(self.available_variables)
|
|
if s.strip() == "{{ CA_ROOT.cert_host }}":
|
|
return self.available_variables.get("CA_ROOT", {}).get(
|
|
"cert_host", s
|
|
)
|
|
if s.strip() == "{{ ANSIBLE_FACT }}":
|
|
return self.available_variables.get("ANSIBLE_FACT", s)
|
|
return s
|
|
|
|
preset = {"ANSIBLE_FACT": "from-prev-avail"}
|
|
templar = _FakeTemplarRecordingAvail(preset)
|
|
variables = {"CA_ROOT": {"cert_host": "/etc/infinito/ca/root-ca.crt"}}
|
|
|
|
out = render_ansible_strict(
|
|
templar=templar,
|
|
raw="{{ CA_ROOT.cert_host }}",
|
|
var_name="CA_TRUST.cert_host",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out, "/etc/infinito/ca/root-ca.crt")
|
|
|
|
self.assertIn("ANSIBLE_FACT", templar.seen_on_call)
|
|
self.assertEqual(templar.seen_on_call["ANSIBLE_FACT"], "from-prev-avail")
|
|
self.assertIn("CA_ROOT", templar.seen_on_call)
|
|
|
|
self.assertEqual(templar.available_variables, preset)
|
|
|
|
def test_unresolved_jinja_hard_fails(self):
|
|
with self.assertRaises(AnsibleError):
|
|
render_ansible_strict(
|
|
templar=_FakeTemplarLookupUndefined(),
|
|
raw="{{ DOES_NOT_EXIST }}",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables={},
|
|
)
|
|
|
|
def test_multi_round_rendering(self):
|
|
variables = {
|
|
"SOFTWARE_DOMAIN": "infinito.nexus",
|
|
"CA_ROOT": {"cert_host": "/etc/{{ SOFTWARE_DOMAIN }}/ca/root-ca.crt"},
|
|
}
|
|
out = render_ansible_strict(
|
|
templar=_FakeTemplarLookupUndefined(),
|
|
raw="{{ CA_ROOT.cert_host }}",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out, ca_cert_host("infinito.nexus"))
|
|
|
|
def test_embedded_expression_in_string(self):
|
|
variables = {"SOFTWARE_DOMAIN": "infinito.nexus"}
|
|
out = render_ansible_strict(
|
|
templar=None,
|
|
raw="/etc/{{ SOFTWARE_DOMAIN }}/ca/root-ca.crt",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out, ca_cert_host("infinito.nexus"))
|
|
|
|
def test_filters_lower_upper_default(self):
|
|
variables = {"NAME": "MiXeD", "EMPTY": ""}
|
|
out1 = render_ansible_strict(
|
|
templar=None,
|
|
raw="{{ NAME | lower }}",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out1, "mixed")
|
|
|
|
out2 = render_ansible_strict(
|
|
templar=None,
|
|
raw="{{ NAME | upper }}",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out2, "MIXED")
|
|
|
|
out3 = render_ansible_strict(
|
|
templar=None,
|
|
raw="{{ EMPTY | default('fallback', true) }}",
|
|
var_name="x",
|
|
err_prefix="t",
|
|
variables=variables,
|
|
)
|
|
self.assertEqual(out3, "fallback")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|