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>
155 lines
5.5 KiB
Python
155 lines
5.5 KiB
Python
#
|
|
# Unit tests for plugins/lookup/unit_name.py using Python's built-in unittest.
|
|
#
|
|
# Run with:
|
|
# python -m unittest -v
|
|
#
|
|
# Notes:
|
|
# - These tests do NOT require a full Ansible runtime; we mock the templar.
|
|
# - They validate the naming format and error handling.
|
|
|
|
import unittest
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.unit_name import LookupModule
|
|
|
|
|
|
class DummyTemplar:
|
|
"""Minimal templar stub to satisfy the lookup plugin."""
|
|
|
|
def __init__(self, available_variables=None, version="1.2.3"):
|
|
self.available_variables = available_variables or {}
|
|
self._version = version
|
|
|
|
def template(self, s: str, **kwargs):
|
|
if "lookup('version')" in s:
|
|
return self._version
|
|
if "{{ SOFTWARE_NAME | lower }}" in s:
|
|
return str(self.available_variables.get("SOFTWARE_NAME", "")).lower()
|
|
return s
|
|
|
|
|
|
class UnitNameLookupTests(unittest.TestCase):
|
|
def _mk_lookup(
|
|
self, software_domain="infinito.nexus", version="1.2.3"
|
|
) -> LookupModule:
|
|
lm = LookupModule()
|
|
|
|
lm._templar = DummyTemplar(
|
|
available_variables={"SOFTWARE_DOMAIN": software_domain},
|
|
version=version,
|
|
)
|
|
|
|
lm.set_options = lambda *args, **kwargs: None
|
|
|
|
return lm
|
|
|
|
def test_default_suffix_service(self):
|
|
lm = self._mk_lookup(software_domain="Infinito.Nexus", version="2.0.0")
|
|
res = lm.run(["svc-foo"])
|
|
self.assertEqual(res, ["svc-foo.2.0.0.infinito.nexus.service"])
|
|
|
|
def test_sys_ctl_prefix_is_removed(self):
|
|
lm = self._mk_lookup(software_domain="Infinito.Nexus", version="2.0.0")
|
|
res = lm.run(["sys-ctl-cln-bkps"])
|
|
self.assertEqual(res, ["cln-bkps.2.0.0.infinito.nexus.service"])
|
|
|
|
def test_explicit_timer_suffix_with_dot(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["svc-foo"], suffix=".timer")
|
|
self.assertEqual(res, ["svc-foo.2.0.0.infinito.nexus.timer"])
|
|
|
|
def test_explicit_timer_suffix_without_dot(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["svc-foo"], suffix="timer")
|
|
self.assertEqual(res, ["svc-foo.2.0.0.infinito.nexus.timer"])
|
|
|
|
def test_suffix_false_means_no_suffix(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["svc-foo"], suffix=False)
|
|
self.assertEqual(res, ["svc-foo.2.0.0.infinito.nexus"])
|
|
|
|
def test_template_unit_id_endswith_at(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["alarm@"])
|
|
self.assertEqual(res, ["alarm.2.0.0.infinito.nexus@.service"])
|
|
|
|
def test_template_unit_id_endswith_at_with_sys_ctl_prefix(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["sys-ctl-alm-compose@"], suffix=False)
|
|
self.assertEqual(res, ["alm-compose.2.0.0.infinito.nexus@"])
|
|
|
|
def test_multiple_terms(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version="2.0.0")
|
|
res = lm.run(["a", "b"], suffix=".timer")
|
|
self.assertEqual(
|
|
res,
|
|
[
|
|
"a.2.0.0.infinito.nexus.timer",
|
|
"b.2.0.0.infinito.nexus.timer",
|
|
],
|
|
)
|
|
|
|
def test_missing_terms_raises(self):
|
|
lm = self._mk_lookup()
|
|
with self.assertRaises(AnsibleError) as ctx:
|
|
lm.run([])
|
|
self.assertIn("at least one term", str(ctx.exception))
|
|
|
|
def test_missing_software_name_raises(self):
|
|
lm = LookupModule()
|
|
lm._templar = DummyTemplar(available_variables={}, version="1.2.3")
|
|
lm.set_options = lambda *args, **kwargs: None
|
|
|
|
with self.assertRaises(AnsibleError) as ctx:
|
|
lm.run(["svc-foo"])
|
|
self.assertIn("SOFTWARE_DOMAIN is not defined", str(ctx.exception))
|
|
|
|
def test_empty_version_raises(self):
|
|
lm = self._mk_lookup(software_domain="infinito.nexus", version=" ")
|
|
with self.assertRaises(AnsibleError) as ctx:
|
|
lm.run(["svc-foo"])
|
|
self.assertIn("lookup('version') returned an empty value", str(ctx.exception))
|
|
|
|
def test_templated_software_domain_is_rendered(self):
|
|
lm = LookupModule()
|
|
lm._templar = DummyTemplar(
|
|
available_variables={
|
|
"SOFTWARE_DOMAIN": "{{ SOFTWARE_NAME | lower }}",
|
|
"SOFTWARE_NAME": "Infinito.Nexus",
|
|
},
|
|
version="2.0.0",
|
|
)
|
|
lm.set_options = lambda *args, **kwargs: None
|
|
|
|
res = lm.run(["svc-foo"])
|
|
self.assertEqual(res, ["svc-foo.2.0.0.infinito.nexus.service"])
|
|
|
|
def test_unresolved_software_domain_template_raises(self):
|
|
lm = LookupModule()
|
|
lm._templar = DummyTemplar(
|
|
available_variables={"SOFTWARE_DOMAIN": "{{ UNKNOWN_VAR | lower }}"},
|
|
version="2.0.0",
|
|
)
|
|
lm.set_options = lambda *args, **kwargs: None
|
|
|
|
with self.assertRaises(AnsibleError) as ctx:
|
|
lm.run(["svc-foo"])
|
|
self.assertIn("unresolved template for 'SOFTWARE_DOMAIN'", str(ctx.exception))
|
|
|
|
def test_version_fallback_direct_lookup_plugin(self):
|
|
lm = self._mk_lookup(
|
|
software_domain="infinito.nexus", version="{{ lookup('version') }}"
|
|
)
|
|
res = lm.run(["svc-foo"])
|
|
|
|
self.assertRegex(
|
|
res[0],
|
|
r"^svc-foo\.[0-9A-Za-z._+-]+\.infinito\.nexus\.service$",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|