mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +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>
113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
"""Unit tests for the container_deploy lookup plugin.
|
|
|
|
Pins the swarm ``deploy:`` head contract:
|
|
|
|
* non-swarm mode emits empty string, so callers need no mode guard.
|
|
* ``compose_mode_force`` overrides ``DEPLOYMENT_MODE``.
|
|
* the replica count is delegated to the compose_replicas SPOT: an explicit
|
|
``replicas=`` wins, an empty one falls back to the topology default.
|
|
* a role whose primary entity declares a placement gains the constraint
|
|
block; one that declares none gains nothing.
|
|
* children are indented two spaces under ``deploy:`` so the caller's
|
|
``| indent(N)`` lands the whole block at N.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import unittest
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
SWARM = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["mgr-01", "wrk-01", "wrk-02"]},
|
|
}
|
|
|
|
|
|
def _load_lookup():
|
|
spec = importlib.util.spec_from_file_location(
|
|
"lookup_container_deploy",
|
|
str(PROJECT_ROOT / "plugins/lookup/container_deploy.py"),
|
|
)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec and spec.loader
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
class _DummyTemplar:
|
|
def __init__(self, available_variables=None):
|
|
self.available_variables = available_variables or {}
|
|
|
|
def template(self, value):
|
|
return value
|
|
|
|
|
|
class TestContainerDeployLookup(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.mod = _load_lookup()
|
|
|
|
def setUp(self):
|
|
self._real_placement = self.mod.get_role_placement
|
|
self.placement = None
|
|
self.mod.get_role_placement = lambda _role: self.placement
|
|
|
|
def tearDown(self):
|
|
self.mod.get_role_placement = self._real_placement
|
|
|
|
def _run(self, variables, **kwargs):
|
|
lm = self.mod.LookupModule()
|
|
lm._templar = _DummyTemplar(variables)
|
|
lm._loader = None
|
|
return lm.run([], variables=variables, **kwargs)[0]
|
|
|
|
def test_compose_mode_emits_nothing(self):
|
|
self.assertEqual(self._run({**SWARM, "DEPLOYMENT_MODE": "compose"}), "")
|
|
|
|
def test_missing_deployment_mode_emits_nothing(self):
|
|
self.assertEqual(self._run({"application_id": "web-app-x"}), "")
|
|
|
|
def test_compose_mode_force_overrides_deployment_mode(self):
|
|
vars_ = {**SWARM, "DEPLOYMENT_MODE": "compose", "compose_mode_force": "swarm"}
|
|
self.assertEqual(self._run(vars_), "deploy:\n replicas: 3")
|
|
|
|
def test_topology_default_without_an_override(self):
|
|
self.assertEqual(self._run(SWARM), "deploy:\n replicas: 3")
|
|
|
|
def test_empty_override_falls_back_to_the_topology_default(self):
|
|
self.assertEqual(self._run(SWARM, replicas=""), "deploy:\n replicas: 3")
|
|
|
|
def test_explicit_override_wins(self):
|
|
self.assertEqual(self._run(SWARM, replicas=1), "deploy:\n replicas: 1")
|
|
|
|
def test_placement_is_appended_when_the_role_declares_one(self):
|
|
self.placement = "manager"
|
|
self.assertEqual(
|
|
self._run(SWARM, replicas=1),
|
|
"deploy:\n"
|
|
" replicas: 1\n"
|
|
" placement:\n"
|
|
" constraints:\n"
|
|
" - node.role == manager",
|
|
)
|
|
|
|
def test_a_positional_term_is_rejected(self):
|
|
lm = self.mod.LookupModule()
|
|
lm._templar = _DummyTemplar(SWARM)
|
|
lm._loader = None
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run([1], variables=SWARM)
|
|
|
|
def test_missing_application_id_is_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self._run({"DEPLOYMENT_MODE": "swarm"})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|