Files
computer-playbook/tests/unit/python/utils/inventory/test_groups.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

133 lines
3.8 KiB
Python

"""Unit tests for utils.inventory.groups.inventory_has_group."""
from __future__ import annotations
import textwrap
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from utils.inventory.groups import inventory_has_group
class TestInventoryHasGroupYaml(unittest.TestCase):
def setUp(self) -> None:
self.tmp = TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
def _write(self, name: str, body: str) -> str:
path = self.root / name
path.write_text(textwrap.dedent(body).lstrip("\n"), encoding="utf-8")
return str(path)
def test_yaml_finds_nested_group(self) -> None:
path = self._write(
"inv.yml",
"""
all:
children:
svc-swarm-node:
hosts:
swarm-mgr-01: {}
svc-swarm-manager:
hosts:
swarm-mgr-01: {}
""",
)
self.assertTrue(inventory_has_group(path, "svc-swarm-manager"))
self.assertTrue(inventory_has_group(path, "svc-swarm-node"))
def test_yaml_missing_group_returns_false(self) -> None:
path = self._write(
"inv.yml",
"""
all:
children:
svc-swarm-node:
hosts:
swarm-mgr-01: {}
""",
)
self.assertFalse(inventory_has_group(path, "svc-swarm-manager"))
def test_yaml_skips_scalar_key(self) -> None:
path = self._write(
"inv.yml",
"""
all:
children:
some-key: just-a-string
actual-group:
hosts: {}
""",
)
self.assertFalse(inventory_has_group(path, "some-key"))
self.assertTrue(inventory_has_group(path, "actual-group"))
class TestInventoryHasGroupIni(unittest.TestCase):
def setUp(self) -> None:
self.tmp = TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
def _write(self, name: str, body: str) -> str:
path = self.root / name
path.write_text(textwrap.dedent(body).lstrip("\n"), encoding="utf-8")
return str(path)
def test_ini_finds_section_header(self) -> None:
path = self._write(
"hosts",
"""
[svc-swarm-manager]
mgr-01
[web-app-mediawiki]
mgr-01
""",
)
self.assertTrue(inventory_has_group(path, "svc-swarm-manager"))
self.assertTrue(inventory_has_group(path, "web-app-mediawiki"))
def test_ini_finds_host_in_section(self) -> None:
path = self._write(
"hosts",
"""
[grp]
host-a, host-b host-c
""",
)
self.assertTrue(inventory_has_group(path, "host-a"))
self.assertTrue(inventory_has_group(path, "host-b"))
self.assertTrue(inventory_has_group(path, "host-c"))
def test_ini_skips_comments(self) -> None:
path = self._write(
"hosts",
"""
# [comment-group]
; [also-comment]
[real-group]
mgr-01
""",
)
self.assertFalse(inventory_has_group(path, "comment-group"))
self.assertFalse(inventory_has_group(path, "also-comment"))
self.assertTrue(inventory_has_group(path, "real-group"))
def test_ini_missing_group_returns_false(self) -> None:
path = self._write(
"hosts",
"""
[other]
mgr-01
""",
)
self.assertFalse(inventory_has_group(path, "missing"))
if __name__ == "__main__":
unittest.main()