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>
205 lines
7.0 KiB
Python
205 lines
7.0 KiB
Python
import importlib.util
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from . import PROJECT_ROOT
|
|
|
|
|
|
def _load_module(rel_path: str, name: str):
|
|
path = PROJECT_ROOT / rel_path
|
|
spec = importlib.util.spec_from_file_location(name, str(path))
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec and spec.loader
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
class _TlsResolveStub:
|
|
def __init__(self, enabled: bool, mode: str):
|
|
self._enabled = enabled
|
|
self._mode = mode
|
|
|
|
def run(self, terms, variables=None, **kwargs):
|
|
return [{"enabled": self._enabled, "mode": self._mode}]
|
|
|
|
|
|
class _DomainsResolveStub:
|
|
def run(self, terms, variables=None, **kwargs):
|
|
return [(variables or {}).get("domains", {})]
|
|
|
|
|
|
class TestComposeFArgs(unittest.TestCase):
|
|
def setUp(self):
|
|
self.m = _load_module(
|
|
"plugins/lookup/compose_file_args.py",
|
|
"compose_file_args_mod",
|
|
)
|
|
self.lookup = self.m.LookupModule()
|
|
|
|
self.lookup._loader = object()
|
|
self.lookup._templar = object()
|
|
|
|
self.vars = {
|
|
"DIR_COMPOSITIONS": "/x/",
|
|
"domains": {
|
|
"web-app-a": "example.invalid",
|
|
},
|
|
}
|
|
|
|
def _loader_dispatch(self, tls_stub):
|
|
# "domains" resolves through variables['domains'] to keep tests hermetic;
|
|
# "tls" resolves through the per-test stub.
|
|
def _get(name, *args, **kwargs):
|
|
if name == "domains":
|
|
return _DomainsResolveStub()
|
|
if name == "tls":
|
|
return tls_stub
|
|
raise AssertionError(f"unexpected lookup name: {name}")
|
|
|
|
return _get
|
|
|
|
def _stub_get_docker_paths(self, application_id: str, base_dir: str) -> dict:
|
|
self.assertEqual(application_id, "web-app-a")
|
|
self.assertEqual(base_dir, "/x/")
|
|
return {
|
|
"files": {
|
|
"compose": "/x/compose.yml",
|
|
"compose_override": "/x/compose.override.yml",
|
|
"compose_ca_override": "/x/compose.ca.override.yml",
|
|
}
|
|
}
|
|
|
|
def test_includes_base_and_override_when_role_provides_override_and_tls_off(self):
|
|
with (
|
|
patch.object(
|
|
self.m, "get_docker_paths", side_effect=self._stub_get_docker_paths
|
|
),
|
|
patch.object(self.m, "_role_provides_override", return_value=True),
|
|
patch.object(
|
|
self.m.lookup_loader,
|
|
"get",
|
|
side_effect=self._loader_dispatch(_TlsResolveStub(False, "off")),
|
|
),
|
|
):
|
|
out = self.lookup.run(["web-app-a"], variables=self.vars)[0]
|
|
|
|
self.assertEqual(out, "-f /x/compose.yml -f /x/compose.override.yml")
|
|
|
|
def test_flag_kwarg_emits_compose_file_for_stack_deploy(self):
|
|
with (
|
|
patch.object(
|
|
self.m, "get_docker_paths", side_effect=self._stub_get_docker_paths
|
|
),
|
|
patch.object(self.m, "_role_provides_override", return_value=True),
|
|
patch.object(
|
|
self.m.lookup_loader,
|
|
"get",
|
|
side_effect=self._loader_dispatch(_TlsResolveStub(False, "off")),
|
|
),
|
|
):
|
|
out = self.lookup.run(
|
|
["web-app-a"], variables=self.vars, flag="--compose-file"
|
|
)[0]
|
|
|
|
self.assertEqual(
|
|
out, "--compose-file /x/compose.yml --compose-file /x/compose.override.yml"
|
|
)
|
|
|
|
def test_invalid_flag_rejected(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run(["web-app-a"], variables=self.vars, flag="-bad")
|
|
|
|
def test_includes_ca_override_when_self_signed_and_domain_exists(self):
|
|
with (
|
|
patch.object(
|
|
self.m, "get_docker_paths", side_effect=self._stub_get_docker_paths
|
|
),
|
|
patch.object(self.m, "_role_provides_override", return_value=True),
|
|
patch.object(
|
|
self.m.lookup_loader,
|
|
"get",
|
|
side_effect=self._loader_dispatch(_TlsResolveStub(True, "self_signed")),
|
|
),
|
|
):
|
|
out = self.lookup.run(["web-app-a"], variables=self.vars)[0]
|
|
|
|
self.assertEqual(
|
|
out,
|
|
"-f /x/compose.yml -f /x/compose.override.yml -f /x/compose.ca.override.yml",
|
|
)
|
|
|
|
def test_fails_when_ca_override_missing_but_required(self):
|
|
def stub_missing_ca(application_id: str, base_dir: str) -> dict:
|
|
return {
|
|
"files": {
|
|
"compose": "/x/compose.yml",
|
|
"compose_override": "/x/compose.override.yml",
|
|
"compose_ca_override": "",
|
|
}
|
|
}
|
|
|
|
with (
|
|
patch.object(self.m, "get_docker_paths", side_effect=stub_missing_ca),
|
|
patch.object(self.m, "_role_provides_override", return_value=True),
|
|
patch.object(
|
|
self.m.lookup_loader,
|
|
"get",
|
|
side_effect=self._loader_dispatch(_TlsResolveStub(True, "self_signed")),
|
|
),
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
self.lookup.run(["web-app-a"], variables=self.vars)
|
|
|
|
def test_includes_only_base_when_role_does_not_provide_override(self):
|
|
with (
|
|
patch.object(
|
|
self.m, "get_docker_paths", side_effect=self._stub_get_docker_paths
|
|
),
|
|
patch.object(self.m, "_role_provides_override", return_value=False),
|
|
patch.object(
|
|
self.m.lookup_loader,
|
|
"get",
|
|
side_effect=self._loader_dispatch(_TlsResolveStub(False, "off")),
|
|
),
|
|
):
|
|
out = self.lookup.run(["web-app-a"], variables=self.vars)[0]
|
|
|
|
self.assertEqual(out, "-f /x/compose.yml")
|
|
|
|
def test_requires_one_term(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run([], variables=self.vars)
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run(["a", "b"], variables=self.vars)
|
|
|
|
def test_requires_path_docker_compose_instances(self):
|
|
with self.assertRaises(AnsibleError):
|
|
self.lookup.run(
|
|
["web-app-a"], variables={"domains": {"web-app-a": "example.invalid"}}
|
|
)
|
|
|
|
def test_requires_docker_compose_structure_from_get_docker_paths(self):
|
|
with (
|
|
patch.object(self.m, "get_docker_paths", return_value="nope"),
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
self.lookup.run(["web-app-a"], variables=self.vars)
|
|
|
|
with (
|
|
patch.object(self.m, "get_docker_paths", return_value={}),
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
self.lookup.run(["web-app-a"], variables=self.vars)
|
|
|
|
with (
|
|
patch.object(self.m, "get_docker_paths", return_value={"files": "nope"}),
|
|
self.assertRaises(AnsibleError),
|
|
):
|
|
self.lookup.run(["web-app-a"], variables=self.vars)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|