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>
133 lines
4.4 KiB
Python
133 lines
4.4 KiB
Python
"""Unit tests for the compose_only lookup plugin.
|
|
|
|
Pins the contract for the generic SPOT lookup that gates compose-only
|
|
YAML keys (container_name, pull_policy, ...) per DEPLOYMENT_MODE.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import unittest
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
|
|
def _load_lookup():
|
|
spec = importlib.util.spec_from_file_location(
|
|
"lookup_compose_only",
|
|
str(PROJECT_ROOT / "plugins/lookup/compose_only.py"),
|
|
)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec and spec.loader
|
|
spec.loader.exec_module(mod)
|
|
return mod.LookupModule
|
|
|
|
|
|
class _DummyTemplar:
|
|
def __init__(self, available_variables=None):
|
|
self.available_variables = available_variables or {}
|
|
|
|
def template(self, value):
|
|
return value
|
|
|
|
|
|
class TestComposeOnlyLookup(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.LookupModule = _load_lookup()
|
|
|
|
def _make(self, variables):
|
|
lm = self.LookupModule()
|
|
lm._templar = _DummyTemplar(variables)
|
|
lm._loader = None
|
|
return lm
|
|
|
|
def test_compose_mode_emits_container_name(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["container_name", "myapp"], variables=vars_)
|
|
self.assertEqual(result, ['container_name: "myapp"'])
|
|
|
|
def test_compose_mode_emits_pull_policy(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["pull_policy", "never"], variables=vars_)
|
|
self.assertEqual(result, ['pull_policy: "never"'])
|
|
|
|
def test_compose_mode_emits_arbitrary_key(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["some_other_key", "some_value"], variables=vars_)
|
|
self.assertEqual(result, ['some_other_key: "some_value"'])
|
|
|
|
def test_swarm_mode_emits_empty(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm"}
|
|
lm = self._make(vars_)
|
|
for key, value in [
|
|
("container_name", "myapp"),
|
|
("pull_policy", "never"),
|
|
("anything", "anyvalue"),
|
|
]:
|
|
with self.subTest(key=key):
|
|
self.assertEqual(lm.run([key, value], variables=vars_), [""])
|
|
|
|
def test_swarm_mode_with_surrounding_whitespace(self):
|
|
vars_ = {"DEPLOYMENT_MODE": " swarm "}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["pull_policy", "never"], variables=vars_)
|
|
self.assertEqual(result, [""])
|
|
|
|
def test_missing_deployment_mode_defaults_to_compose(self):
|
|
vars_ = {}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["container_name", "myapp"], variables=vars_)
|
|
self.assertEqual(result, ['container_name: "myapp"'])
|
|
|
|
def test_empty_terms_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run([], variables=vars_)
|
|
|
|
def test_none_terms_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(None, variables=vars_)
|
|
|
|
def test_single_term_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["container_name"], variables=vars_)
|
|
|
|
def test_too_many_terms_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["a", "b", "c"], variables=vars_)
|
|
|
|
def test_empty_key_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["", "value"], variables=vars_)
|
|
|
|
def test_non_string_value_coerced_to_string(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["container_name", 42], variables=vars_)
|
|
self.assertEqual(result, ['container_name: "42"'])
|
|
|
|
def test_compose_mode_force_overrides_swarm_cluster_mode(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm", "compose_mode_force": "compose"}
|
|
lm = self._make(vars_)
|
|
result = lm.run(["container_name", "mdad"], variables=vars_)
|
|
self.assertEqual(result, ['container_name: "mdad"'])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|