Files
computer-playbook/tests/unit/python/plugins/lookup/test_container_volumes_render.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
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>
2026-08-22 01:31:09 +02:00

295 lines
9.7 KiB
Python

from __future__ import annotations
import unittest
import yaml
from plugins.lookup.container_volumes import container_volumes
def _apps(volumes_meta):
return {"my-app": {"volumes": volumes_meta}}
def _parse(out: str) -> dict:
if not out:
return {}
parsed = yaml.safe_load(out) # nocheck: direct-yaml plugin output string
return parsed or {}
class TestContainerVolumesShortFormBind(unittest.TestCase):
def test_canonical_bind_renders_short_form(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"mounts": [{"service": "app", "target": "/app/foo.conf"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(data, {"volumes": ["/etc/foo.conf:/app/foo.conf"]})
def test_bind_read_only_emits_ro_suffix(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"read_only": True,
"mounts": [{"service": "app", "target": "/app/foo.conf"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(data, {"volumes": ["/etc/foo.conf:/app/foo.conf:ro"]})
def test_per_mount_read_only_overrides_volume_level(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"read_only": True,
"mounts": [
{"service": "app", "target": "/app/foo.conf", "read_only": False}
],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(data, {"volumes": ["/etc/foo.conf:/app/foo.conf"]})
class TestContainerVolumesNamed(unittest.TestCase):
def test_canonical_volume_with_mount_uses_semantic_name(self) -> None:
"""Per-service mount references the SEMANTIC name (YAML key), so
the top-level `volumes:` key from compose_volumes (also keyed by
semantic name) resolves the mount. Using the docker name here would
produce a compose project referencing an undefined top-level key
and `docker compose up` would fail with 'undefined volume'."""
meta = {
"data": {
"type": "volume",
"name": "myapp_data",
"mounts": [{"service": "app", "target": "/data"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(data, {"volumes": ["data:/data"]})
class TestContainerVolumesConfigSecret(unittest.TestCase):
def test_config_renders_per_service_reference(self) -> None:
meta = {
"appcfg": {
"type": "config",
"source": "/opt/render/foo.yaml",
"mode": "0440",
"mounts": [{"service": "app", "target": "/app/foo.yaml"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(
data,
{
"configs": [
{"source": "appcfg", "target": "/app/foo.yaml", "mode": 0o440}
]
},
)
def test_secret_renders_per_service_reference(self) -> None:
meta = {
"tlskey": {
"type": "secret",
"source": "/etc/ssl/key.pem",
"mode": "0400",
"mounts": [{"service": "app", "target": "/run/secrets/key"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(
data,
{
"secrets": [
{"source": "tlskey", "target": "/run/secrets/key", "mode": 0o400}
]
},
)
class TestContainerVolumesTmpfs(unittest.TestCase):
def test_tmpfs_renders_long_form_dict(self) -> None:
meta = {
"scratch": {
"type": "tmpfs",
"mounts": [{"service": "app", "target": "/scratch", "size": "64m"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
data = _parse(out)
self.assertEqual(
data,
{
"volumes": [
{
"type": "tmpfs",
"target": "/scratch",
"tmpfs": {"size": "64m"},
}
]
},
)
class TestContainerVolumesServiceFilter(unittest.TestCase):
def test_only_emits_mounts_for_requested_service(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"mounts": [
{"service": "a", "target": "/etc/foo.conf"},
{"service": "b", "target": "/opt/foo.conf"},
],
}
}
out_a = container_volumes(_apps(meta), "my-app", "a")
out_b = container_volumes(_apps(meta), "my-app", "b")
self.assertEqual(_parse(out_a), {"volumes": ["/etc/foo.conf:/etc/foo.conf"]})
self.assertEqual(_parse(out_b), {"volumes": ["/etc/foo.conf:/opt/foo.conf"]})
class TestContainerVolumesWhen(unittest.TestCase):
def test_when_false_filters_out(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"mounts": [
{"service": "app", "target": "/etc/foo.conf", "when": False},
],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
self.assertEqual(out, "")
def test_when_true_keeps(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"mounts": [
{"service": "app", "target": "/etc/foo.conf", "when": True},
],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
self.assertEqual(_parse(out), {"volumes": ["/etc/foo.conf:/etc/foo.conf"]})
def test_when_string_falsy_filters_out(self) -> None:
meta = {
"cfg": {
"type": "bind",
"source": "/etc/foo.conf",
"mounts": [
{"service": "app", "target": "/etc/foo.conf", "when": "false"},
],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
self.assertEqual(out, "")
def test_when_jinja_evaluated_via_render_callable(self) -> None:
"""Callers (lookup plugin) pass a templar-backed render callable;
verifies the contract: a Jinja expression resolves to a usable
truthy/falsy value through that callable."""
rendered_calls: list[str] = []
def render_jinja(expr: str) -> str:
rendered_calls.append(expr)
return "true" if "FLAG" in expr else "false"
meta = {
"cfg": {
"type": "bind",
"source": "/a",
"mounts": [
{
"service": "app",
"target": "/a",
"when": "{{ FLAG | bool }}",
},
{
"service": "app",
"target": "/b",
"when": "{{ OTHER | bool }}",
},
],
}
}
out = container_volumes(_apps(meta), "my-app", "app", render_jinja=render_jinja)
self.assertEqual(_parse(out), {"volumes": ["/a:/a"]})
self.assertEqual(rendered_calls, ["{{ FLAG | bool }}", "{{ OTHER | bool }}"])
class TestContainerVolumesExtras(unittest.TestCase):
def test_extra_volumes_appended(self) -> None:
meta = {
"data": {
"type": "volume",
"name": "myapp_data",
"mounts": [{"service": "app", "target": "/data"}],
}
}
out = container_volumes(
_apps(meta), "my-app", "app", extra_volumes=["/host:/extra:ro"]
)
self.assertEqual(
_parse(out),
{"volumes": ["data:/data", "/host:/extra:ro"]},
)
def test_extra_configs_and_secrets(self) -> None:
out = container_volumes(
_apps(None),
"my-app",
"app",
extra_configs=[{"source": "cfg", "target": "/etc/x"}],
extra_secrets=[{"source": "s", "target": "/run/secrets/y"}],
)
data = _parse(out)
self.assertEqual(
data,
{
"configs": [{"source": "cfg", "target": "/etc/x"}],
"secrets": [{"source": "s", "target": "/run/secrets/y"}],
},
)
class TestContainerVolumesOutputShape(unittest.TestCase):
def test_empty_meta_returns_empty_string(self) -> None:
out = container_volumes(_apps(None), "my-app", "app")
self.assertEqual(out, "")
def test_output_starts_at_column_0(self) -> None:
meta = {
"data": {
"type": "volume",
"name": "x",
"mounts": [{"service": "app", "target": "/data"}],
}
}
out = container_volumes(_apps(meta), "my-app", "app")
first_line = out.splitlines()[0]
self.assertFalse(first_line.startswith(" "))
self.assertEqual(first_line, "volumes:")
if __name__ == "__main__": # pragma: no cover
unittest.main()