mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-30 06:46:31 +00:00
refactor(unit_name): build unit names from SYS_SERVICE_VERSION_SUFFIX
The lookup assembled '.<version>.<software domain>' itself, a second copy of the infix the unlock and prune phases now read from the SPOT. It reads SYS_SERVICE_VERSION_SUFFIX instead and refuses one that does not start with a dot or carries an empty segment. The direct version-plugin fallback goes with it: it existed because the plugin rendered the untrusted literal "{{ lookup('version') }}", while the SPOT arrives as a trusted group_vars string. Every unit name rendered from group_vars is byte-identical before and after.
The timer description names the versioned service it starts instead of the legacy unversioned name.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
723818cbd7
commit
ef246a6e03
3 files changed
+52
-140
No files matched your search
@@ -1,13 +1,3 @@
|
||||
#
|
||||
# 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
|
||||
@@ -16,73 +6,54 @@ 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"):
|
||||
def __init__(self, available_variables=None, rendered=None):
|
||||
self.available_variables = available_variables or {}
|
||||
self._version = version
|
||||
self._rendered = rendered or {}
|
||||
|
||||
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
|
||||
return self._rendered.get(s, s)
|
||||
|
||||
|
||||
class UnitNameLookupTests(unittest.TestCase):
|
||||
def _mk_lookup(
|
||||
self, software_domain="infinito.nexus", version="1.2.3"
|
||||
) -> LookupModule:
|
||||
def _mk_lookup(self, version_suffix=".2.0.0.infinito.nexus", rendered=None):
|
||||
lm = LookupModule()
|
||||
|
||||
lm._templar = DummyTemplar(
|
||||
available_variables={"SOFTWARE_DOMAIN": software_domain},
|
||||
version=version,
|
||||
available_variables={"SYS_SERVICE_VERSION_SUFFIX": version_suffix},
|
||||
rendered=rendered,
|
||||
)
|
||||
|
||||
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"])
|
||||
res = self._mk_lookup().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"])
|
||||
res = self._mk_lookup().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")
|
||||
res = self._mk_lookup().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")
|
||||
res = self._mk_lookup().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)
|
||||
res = self._mk_lookup().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@"])
|
||||
res = self._mk_lookup().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)
|
||||
res = self._mk_lookup().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")
|
||||
res = self._mk_lookup().run(["a", "b"], suffix=".timer")
|
||||
self.assertEqual(
|
||||
res,
|
||||
[
|
||||
@@ -92,63 +63,41 @@ class UnitNameLookupTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
def test_missing_terms_raises(self):
|
||||
lm = self._mk_lookup()
|
||||
with self.assertRaises(AnsibleError) as ctx:
|
||||
lm.run([])
|
||||
self._mk_lookup().run([])
|
||||
self.assertIn("at least one term", str(ctx.exception))
|
||||
|
||||
def test_missing_software_name_raises(self):
|
||||
def test_missing_version_suffix_raises(self):
|
||||
lm = LookupModule()
|
||||
lm._templar = DummyTemplar(available_variables={}, version="1.2.3")
|
||||
lm._templar = DummyTemplar(available_variables={})
|
||||
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))
|
||||
self.assertIn("SYS_SERVICE_VERSION_SUFFIX 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):
|
||||
def test_templated_version_suffix_is_rendered_and_lowered(self):
|
||||
template = ".{{ lookup('version') | lower }}.{{ SOFTWARE_DOMAIN }}"
|
||||
lm = self._mk_lookup(
|
||||
software_domain="infinito.nexus", version="{{ lookup('version') }}"
|
||||
version_suffix=template,
|
||||
rendered={template: ".2.0.0.Infinito.Nexus"},
|
||||
)
|
||||
res = lm.run(["svc-foo"])
|
||||
self.assertEqual(lm.run(["svc-foo"]), ["svc-foo.2.0.0.infinito.nexus.service"])
|
||||
|
||||
self.assertRegex(
|
||||
res[0],
|
||||
r"^svc-foo\.[0-9A-Za-z._+-]+\.infinito\.nexus\.service$",
|
||||
def test_unresolved_version_suffix_template_raises(self):
|
||||
lm = self._mk_lookup(version_suffix="{{ UNKNOWN_VAR }}")
|
||||
with self.assertRaises(AnsibleError) as ctx:
|
||||
lm.run(["svc-foo"])
|
||||
self.assertIn(
|
||||
"unresolved template for 'SYS_SERVICE_VERSION_SUFFIX'", str(ctx.exception)
|
||||
)
|
||||
|
||||
def test_malformed_version_suffix_raises(self):
|
||||
for malformed in ("2.0.0.infinito.nexus", "..infinito.nexus"):
|
||||
lm = self._mk_lookup(version_suffix=malformed)
|
||||
with self.assertRaises(AnsibleError) as ctx:
|
||||
lm.run(["svc-foo"])
|
||||
self.assertIn("must read '.<version>.<domain>'", str(ctx.exception))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user