mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 02:54:26 +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>
235 lines
6.5 KiB
Python
235 lines
6.5 KiB
Python
import os
|
|
import json
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
from io import StringIO
|
|
from contextlib import redirect_stdout
|
|
|
|
from utils.roles.mapping import ROLE_FILE_META_MAIN, ROLE_FILE_META_SERVICES, ROLE_FILE_TASKS_MAIN
|
|
|
|
from cli.build.graph import (
|
|
load_meta,
|
|
load_tasks,
|
|
build_mappings,
|
|
output_graph,
|
|
ALL_KEYS,
|
|
)
|
|
|
|
|
|
class TestGraphHelpers(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
self.addCleanup(lambda: shutil.rmtree(self.tmpdir, ignore_errors=True))
|
|
|
|
def _write_file(self, rel_path: str, content: str) -> str:
|
|
path = os.path.join(self.tmpdir, rel_path)
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return path
|
|
|
|
def test_load_meta_parses_run_after_and_dependencies(self):
|
|
meta_path = self._write_file(
|
|
f"roles/role_a/{ROLE_FILE_META_MAIN}",
|
|
"""
|
|
galaxy_info:
|
|
author: Test Author
|
|
dependencies:
|
|
- role_d
|
|
- role_e
|
|
""",
|
|
)
|
|
self._write_file(
|
|
f"roles/role_a/{ROLE_FILE_META_SERVICES}",
|
|
"""
|
|
role_a:
|
|
run_after:
|
|
- role_b
|
|
- role_c
|
|
""",
|
|
)
|
|
|
|
meta = load_meta(meta_path)
|
|
|
|
self.assertIn("galaxy_info", meta)
|
|
self.assertEqual(meta["galaxy_info"]["author"], "Test Author")
|
|
self.assertEqual(meta["run_after"], ["role_b", "role_c"])
|
|
self.assertEqual(meta["dependencies"], ["role_d", "role_e"])
|
|
|
|
def test_load_tasks_filters_out_jinja_and_reads_names(self):
|
|
tasks_path = self._write_file(
|
|
f"roles/role_a/{ROLE_FILE_TASKS_MAIN}",
|
|
"""
|
|
- name: include plain file
|
|
include_tasks: "subtasks.yml"
|
|
|
|
- name: include with dict
|
|
include_tasks:
|
|
name: "other.yml"
|
|
|
|
- name: include jinja, should be ignored
|
|
include_tasks: "{{ dynamic_file }}"
|
|
|
|
- name: import plain file
|
|
import_tasks: "legacy.yml"
|
|
|
|
- name: import with dict
|
|
import_tasks:
|
|
name: "more.yml"
|
|
|
|
- name: import jinja, should be ignored
|
|
import_tasks: "{{ legacy_file }}"
|
|
""",
|
|
)
|
|
|
|
include_files = load_tasks(tasks_path, "include_tasks")
|
|
import_files = load_tasks(tasks_path, "import_tasks")
|
|
|
|
self.assertEqual(sorted(include_files), ["other.yml", "subtasks.yml"])
|
|
self.assertEqual(sorted(import_files), ["legacy.yml", "more.yml"])
|
|
|
|
|
|
class TestBuildMappings(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.roles_dir = tempfile.mkdtemp()
|
|
self.addCleanup(lambda: shutil.rmtree(self.roles_dir, ignore_errors=True))
|
|
|
|
def _write_file(self, rel_path: str, content: str) -> str:
|
|
path = os.path.join(self.roles_dir, rel_path)
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(content)
|
|
return path
|
|
|
|
def _create_minimal_role(self, name: str, with_meta: bool = False) -> None:
|
|
os.makedirs(os.path.join(self.roles_dir, name), exist_ok=True)
|
|
if with_meta:
|
|
self._write_file(
|
|
f"{name}/{ROLE_FILE_META_MAIN}",
|
|
"""
|
|
galaxy_info:
|
|
author: Minimal
|
|
""",
|
|
)
|
|
|
|
def test_build_mappings_collects_all_dependency_types(self):
|
|
self._create_minimal_role("role_b")
|
|
self._create_minimal_role("role_c")
|
|
self._create_minimal_role("role_d")
|
|
self._create_minimal_role("role_e")
|
|
|
|
self._write_file(
|
|
f"role_a/{ROLE_FILE_META_MAIN}",
|
|
"""
|
|
galaxy_info:
|
|
author: Role A Author
|
|
dependencies:
|
|
- role_c
|
|
""",
|
|
)
|
|
self._write_file(
|
|
f"role_a/{ROLE_FILE_META_SERVICES}",
|
|
"""
|
|
role_a:
|
|
run_after:
|
|
- role_b
|
|
""",
|
|
)
|
|
|
|
self._write_file(
|
|
f"role_a/{ROLE_FILE_TASKS_MAIN}",
|
|
"""
|
|
- name: use docker style role
|
|
include_role:
|
|
name: role_d
|
|
|
|
- name: use import role
|
|
import_role:
|
|
name: role_e
|
|
|
|
- name: include static tasks file
|
|
include_tasks: "subtasks.yml"
|
|
|
|
- name: import static tasks file
|
|
import_tasks:
|
|
name: "legacy.yml"
|
|
""",
|
|
)
|
|
|
|
self._create_minimal_role("role_a")
|
|
|
|
graphs = build_mappings("role_a", self.roles_dir, max_depth=2)
|
|
|
|
for key in ALL_KEYS:
|
|
self.assertIn(key, graphs, msg=f"Missing graph key {key!r} in result")
|
|
|
|
# Helper to find links in a graph
|
|
def links_of(key: str):
|
|
return graphs[key]["links"]
|
|
|
|
run_after_links = links_of("run_after_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "role_b", "type": "run_after"},
|
|
run_after_links,
|
|
)
|
|
|
|
dep_links = links_of("dependencies_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "role_c", "type": "dependencies"},
|
|
dep_links,
|
|
)
|
|
|
|
inc_role_links = links_of("include_role_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "role_d", "type": "include_role"},
|
|
inc_role_links,
|
|
)
|
|
|
|
imp_role_links = links_of("import_role_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "role_e", "type": "import_role"},
|
|
imp_role_links,
|
|
)
|
|
|
|
inc_tasks_links = links_of("include_tasks_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "subtasks.yml", "type": "include_tasks"},
|
|
inc_tasks_links,
|
|
)
|
|
|
|
imp_tasks_links = links_of("import_tasks_to")
|
|
self.assertIn(
|
|
{"source": "role_a", "target": "legacy.yml", "type": "import_tasks"},
|
|
imp_tasks_links,
|
|
)
|
|
|
|
def test_output_graph_console_prints_header_and_yaml(self):
|
|
graph_data = {"nodes": [{"id": "role_a"}], "links": []}
|
|
buf = StringIO()
|
|
with redirect_stdout(buf):
|
|
output_graph(graph_data, "console", "role_a", "include_role_to")
|
|
|
|
out = buf.getvalue()
|
|
self.assertIn("--- role_a_include_role_to ---", out)
|
|
self.assertIn("nodes:", out)
|
|
self.assertIn("role_a", out)
|
|
|
|
def test_output_graph_writes_json_file(self):
|
|
graph_data = {"nodes": [{"id": "role_a"}], "links": []}
|
|
fname = "role_a_include_role_to.json"
|
|
try:
|
|
output_graph(graph_data, "json", "role_a", "include_role_to")
|
|
self.assertTrue(os.path.exists(fname))
|
|
|
|
with open(fname, "r", encoding="utf-8") as f:
|
|
loaded = json.load(f)
|
|
self.assertEqual(graph_data, loaded)
|
|
finally:
|
|
if os.path.exists(fname):
|
|
os.remove(fname)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|