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>
163 lines
5.2 KiB
Python
163 lines
5.2 KiB
Python
"""Unit tests for the compose_replicas lookup plugin.
|
|
|
|
Pins the SPOT contract for swarm replica calculation:
|
|
|
|
* swarm mode: emits `replicas: N` where N defaults to
|
|
`len(groups[application_id])` or the override term.
|
|
* non-swarm mode: emits empty string.
|
|
* missing group / empty topology default: clamped to 1.
|
|
* an explicit override term is respected as-is, including 0 (lets a
|
|
service stay unscheduled until the role scales it up).
|
|
"""
|
|
|
|
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_replicas",
|
|
str(PROJECT_ROOT / "plugins/lookup/compose_replicas.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 TestComposeReplicasLookup(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_no_arg_emits_empty(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "compose",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["h1", "h2", "h3"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), [""])
|
|
|
|
def test_compose_mode_with_arg_emits_empty(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "compose"}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([3], variables=vars_), [""])
|
|
|
|
def test_missing_deployment_mode_defaults_to_compose(self):
|
|
vars_ = {
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["h1", "h2"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), [""])
|
|
|
|
def test_swarm_mode_default_uses_group_length(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["mgr-01", "wrk-01", "wrk-02"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), ["replicas: 3"])
|
|
|
|
def test_swarm_mode_single_host_yields_one(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["h1"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), ["replicas: 1"])
|
|
|
|
def test_swarm_mode_empty_group_falls_back_to_one(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": []},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), ["replicas: 1"])
|
|
|
|
def test_swarm_mode_missing_group_falls_back_to_one(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), ["replicas: 1"])
|
|
|
|
def test_swarm_mode_explicit_override(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["h1", "h2", "h3"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([5], variables=vars_), ["replicas: 5"])
|
|
|
|
def test_swarm_mode_override_string_coerced(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm"}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run(["2"], variables=vars_), ["replicas: 2"])
|
|
|
|
def test_swarm_mode_override_zero_respected(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm"}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([0], variables=vars_), ["replicas: 0"])
|
|
|
|
def test_too_many_terms_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run([1, 2], variables=vars_)
|
|
|
|
def test_invalid_override_raises(self):
|
|
vars_ = {"DEPLOYMENT_MODE": "swarm"}
|
|
lm = self._make(vars_)
|
|
with self.assertRaises(AnsibleError):
|
|
lm.run(["not-a-number"], variables=vars_)
|
|
|
|
def test_swarm_mode_whitespace_in_mode_normalised(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": " swarm ",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["h1", "h2"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run([], variables=vars_), ["replicas: 2"])
|
|
|
|
def test_none_terms_treated_as_no_args(self):
|
|
vars_ = {
|
|
"DEPLOYMENT_MODE": "swarm",
|
|
"application_id": "web-app-x",
|
|
"groups": {"web-app-x": ["a", "b"]},
|
|
}
|
|
lm = self._make(vars_)
|
|
self.assertEqual(lm.run(None, variables=vars_), ["replicas: 2"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|