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>
179 lines
6.0 KiB
Python
179 lines
6.0 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
import unittest.mock as mock
|
|
from typing import Any, ClassVar
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from plugins.lookup.container_depends_on import LookupModule
|
|
from utils.cache.yaml import load_yaml_str
|
|
|
|
|
|
def _applications_loader(applications):
|
|
def _get(name, *_a, **_k):
|
|
if name == "applications":
|
|
return mock.MagicMock(run=lambda *_ra, **_rk: [applications])
|
|
return mock.MagicMock(run=lambda *_ra, **_rk: [None])
|
|
|
|
return _get
|
|
|
|
|
|
def _apps(*, db_enabled=False, db_shared=False, redis=False, oauth2=False):
|
|
return {
|
|
"app": {
|
|
"services": {
|
|
"postgres": {"enabled": db_enabled, "shared": db_shared},
|
|
"redis": {"enabled": redis},
|
|
"sso": {"enabled": oauth2, "flavor": "oauth2"},
|
|
}
|
|
},
|
|
"svc-db-postgres": {"services": {"postgres": {"name": "postgres-central"}}},
|
|
}
|
|
|
|
|
|
def _run(app_id, applications, *, variables=None, **kwargs):
|
|
lookup = LookupModule()
|
|
lookup._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.container_depends_on.lookup_loader") as loader_mock:
|
|
loader_mock.get.side_effect = _applications_loader(applications)
|
|
return lookup.run([app_id], variables=variables or {}, **kwargs)
|
|
|
|
|
|
def _parse(rendered: str) -> dict[str, Any]:
|
|
if not rendered.strip():
|
|
return {}
|
|
data = load_yaml_str(rendered.lstrip())
|
|
return data if isinstance(data, dict) else {}
|
|
|
|
|
|
class TestContainerDependsOnLookup(unittest.TestCase):
|
|
def test_missing_or_extra_terms_raise(self):
|
|
with self.assertRaises(AnsibleError):
|
|
LookupModule().run([], variables={})
|
|
with self.assertRaises(AnsibleError):
|
|
LookupModule().run(["a", "b"], variables={})
|
|
with self.assertRaises(AnsibleError):
|
|
LookupModule().run([""], variables={})
|
|
|
|
def test_unknown_application_raises(self):
|
|
with self.assertRaises(AnsibleError):
|
|
_run("missing", _apps())
|
|
|
|
def test_no_db_no_redis_returns_empty_string(self):
|
|
out = _run("app", _apps())[0]
|
|
self.assertEqual(out, "")
|
|
|
|
def test_local_db_only_emits_database(self):
|
|
out = _run("app", _apps(db_enabled=True, db_shared=False))[0]
|
|
self.assertEqual(
|
|
_parse(out),
|
|
{"depends_on": {"database": {"condition": "service_healthy"}}},
|
|
)
|
|
|
|
def test_shared_db_does_not_emit_database(self):
|
|
out = _run("app", _apps(db_enabled=True, db_shared=True))[0]
|
|
self.assertEqual(out, "")
|
|
|
|
def test_redis_only_emits_redis(self):
|
|
out = _run("app", _apps(redis=True))[0]
|
|
self.assertEqual(
|
|
_parse(out),
|
|
{"depends_on": {"redis": {"condition": "service_healthy"}}},
|
|
)
|
|
|
|
def test_oauth2_implies_redis(self):
|
|
out = _run("app", _apps(oauth2=True))[0]
|
|
self.assertIn("redis", _parse(out)["depends_on"])
|
|
|
|
def test_local_db_plus_redis(self):
|
|
out = _run("app", _apps(db_enabled=True, db_shared=False, redis=True))[0]
|
|
self.assertEqual(
|
|
_parse(out),
|
|
{
|
|
"depends_on": {
|
|
"database": {"condition": "service_healthy"},
|
|
"redis": {"condition": "service_healthy"},
|
|
}
|
|
},
|
|
)
|
|
|
|
def test_extra_entries_appended(self):
|
|
out = _run(
|
|
"app",
|
|
_apps(db_enabled=True),
|
|
extra={"init": {"condition": "service_completed_successfully"}},
|
|
)[0]
|
|
body = _parse(out)["depends_on"]
|
|
self.assertEqual(body["init"]["condition"], "service_completed_successfully")
|
|
|
|
def test_extra_only_emits_depends_on_block(self):
|
|
out = _run(
|
|
"app",
|
|
_apps(),
|
|
extra={"resolver": {"condition": "service_started"}},
|
|
)[0]
|
|
self.assertEqual(
|
|
_parse(out),
|
|
{"depends_on": {"resolver": {"condition": "service_started"}}},
|
|
)
|
|
|
|
def test_indent_default_is_column_zero(self):
|
|
out = _run("app", _apps(redis=True))[0]
|
|
self.assertTrue(out.startswith("depends_on:"))
|
|
|
|
def test_indent_explicit_four_spaces(self):
|
|
out = _run("app", _apps(redis=True), indent=4)[0]
|
|
self.assertTrue(out.startswith(" depends_on:"))
|
|
|
|
def test_swarm_mode_emits_list_form_without_conditions(self):
|
|
out = _run(
|
|
"app",
|
|
_apps(db_enabled=True, redis=True),
|
|
variables={"DEPLOYMENT_MODE": "swarm"},
|
|
)[0]
|
|
parsed = _parse(out)
|
|
self.assertEqual(set(parsed.keys()), {"depends_on"})
|
|
self.assertEqual(sorted(parsed["depends_on"]), ["database", "redis"])
|
|
self.assertNotIn("condition", out)
|
|
|
|
def test_compose_mode_keeps_map_form_with_conditions(self):
|
|
out = _run(
|
|
"app",
|
|
_apps(db_enabled=True),
|
|
variables={"DEPLOYMENT_MODE": "compose"},
|
|
)[0]
|
|
self.assertEqual(
|
|
_parse(out),
|
|
{"depends_on": {"database": {"condition": "service_healthy"}}},
|
|
)
|
|
|
|
def test_unresolved_jinja_deployment_mode_is_templated(self):
|
|
jinja_expr = (
|
|
"{{ 'swarm' if (groups['svc-swarm-node'] | default([]) | length) > 1 "
|
|
"else 'compose' }}"
|
|
)
|
|
|
|
class _StubTemplar:
|
|
available_variables: ClassVar[dict[str, Any]] = {}
|
|
|
|
def template(self, value):
|
|
return "swarm" if value == jinja_expr else value
|
|
|
|
lookup = LookupModule()
|
|
lookup._templar = _StubTemplar()
|
|
lookup._loader = mock.MagicMock()
|
|
with patch("plugins.lookup.container_depends_on.lookup_loader") as loader_mock:
|
|
loader_mock.get.side_effect = _applications_loader(
|
|
_apps(db_enabled=True, redis=True)
|
|
)
|
|
out = lookup.run(["app"], variables={"DEPLOYMENT_MODE": jinja_expr})[0]
|
|
parsed = _parse(out)
|
|
self.assertEqual(sorted(parsed["depends_on"]), ["database", "redis"])
|
|
self.assertNotIn("condition", out)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|