Files
computer-playbook/tests/unit/python/utils/templating/test_jinja.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
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>
2026-08-22 01:31:09 +02:00

143 lines
4.4 KiB
Python

# tests/unit/python/utils/test_jinja_strict.py
#
# Unit tests for utils/templating/jinja.py
#
# Focus:
# - strict Jinja2 rendering
# - recursive multi-pass expansion (nested Jinja)
# - hard-fail when unresolved markers remain or vars missing
# - hostvars overlay semantics
import unittest
from ansible.errors import AnsibleError
from utils.templating.jinja import (
build_render_context,
looks_like_jinja,
render_jinja2_strict_recursive,
render_strict,
)
class TestJinjaStrict(unittest.TestCase):
def test_looks_like_jinja(self):
self.assertFalse(looks_like_jinja("plain"))
self.assertTrue(looks_like_jinja("{{ X }}"))
self.assertTrue(looks_like_jinja("{% if x %}y{% endif %}"))
def test_build_render_context_prefers_hostvars_overlay(self):
variables = {
"inventory_hostname": "host1",
"SOFTWARE_NAME": "Outer",
"hostvars": {
"host1": {
"SOFTWARE_NAME": "Inner",
"EXTRA": "value",
}
},
}
ctx, inv_host = build_render_context(variables)
self.assertEqual(inv_host, "host1")
self.assertEqual(ctx["SOFTWARE_NAME"], "Inner")
self.assertEqual(ctx["EXTRA"], "value")
def test_render_strict_returns_raw_when_no_markers(self):
variables = {"inventory_hostname": "localhost"}
self.assertEqual(
render_strict(
"/etc/ssl/cert.pem",
variables=variables,
var_name="x",
err_prefix="t",
),
"/etc/ssl/cert.pem",
)
def test_render_strict_single_pass_success(self):
variables = {
"inventory_hostname": "localhost",
"SOFTWARE_DOMAIN": "infinito",
}
out = render_strict(
"/etc/{{ SOFTWARE_DOMAIN }}/ca/root-ca.crt",
variables=variables,
var_name="CA_ROOT.cert_host",
err_prefix="t",
)
self.assertEqual(out, "/etc/infinito/ca/root-ca.crt")
def test_render_strict_recursive_nested_vars_success(self):
variables = {
"inventory_hostname": "localhost",
"SOFTWARE_DOMAIN": "infinito",
"CA_ROOT": {"cert_host": "/etc/{{ SOFTWARE_DOMAIN }}/ca/root-ca.crt"},
"CA_TRUST": {"cert_host": "{{ CA_ROOT.cert_host }}"},
}
out = render_strict(
variables["CA_TRUST"]["cert_host"],
variables=variables,
var_name="CA_TRUST.cert_host",
err_prefix="compose_ca_inject_cmd",
)
self.assertEqual(out, "/etc/infinito/ca/root-ca.crt")
def test_render_jinja2_strict_recursive_fails_on_missing_var(self):
variables = {"inventory_hostname": "localhost"}
ctx, inv_host = build_render_context(variables)
with self.assertRaises(AnsibleError):
render_jinja2_strict_recursive(
"/etc/{{ DOES_NOT_EXIST }}/x",
ctx=ctx,
inv_host=inv_host,
var_name="missing",
err_prefix="t",
max_passes=3,
)
def test_render_strict_fails_when_markers_remain_after_stabilization(self):
variables = {
"inventory_hostname": "localhost",
"A": "{{ B }}",
}
with self.assertRaises(AnsibleError):
render_strict(
"{{ A }}",
variables=variables,
var_name="test",
err_prefix="t",
max_passes=5,
)
def test_render_strict_fails_when_markers_remain_after_max_passes(self):
variables = {"inventory_hostname": "localhost"}
chain_len = 8
for i in range(chain_len):
k = chr(ord("A") + i)
nxt = chr(ord("A") + i + 1) if i < chain_len - 1 else "FINAL"
variables[k] = "{{ " + nxt + " }}"
variables["FINAL"] = "ok"
with self.assertRaises(AnsibleError):
render_strict(
"{{ A }}",
variables=variables,
var_name="chain",
err_prefix="t",
max_passes=3,
)
ok = render_strict(
"{{ A }}",
variables=variables,
var_name="chain",
err_prefix="t",
max_passes=20,
)
self.assertEqual(ok, "ok")
if __name__ == "__main__":
unittest.main()