Files
computer-playbook/tests/unit/python/cli/meta/ci/test_validate.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

121 lines
4.5 KiB
Python

from __future__ import annotations
import io
import unittest
import unittest.mock as mock
from contextlib import redirect_stderr, redirect_stdout
from cli.meta.ci import validate
from utils.github.variant import axes
_ROWS = [
{"name": "web-app-a", "variant": 0, "test_compose": True, "test_swarm": True},
{"name": "web-app-a", "variant": 1, "test_compose": True, "test_swarm": False},
{"name": "web-app-b", "variant": 0, "test_compose": False, "test_swarm": True},
]
_VARIANTS = {
"web-app-a": [{"services": {}}, {"services": {"tor": {"enabled": False}}}],
"web-app-b": [{"services": {}}],
}
def _problems(tokens: str, *, tor_mode: str = "auto") -> tuple[list[str], list[str]]:
with (
mock.patch.object(validate.query, "discover_rows", return_value=_ROWS),
mock.patch.object(validate, "get_variants", return_value=_VARIANTS),
):
return validate.problems(
tokens,
modes=("compose", "swarm", "host"),
tor_mode=tor_mode,
distros=axes.DISTROS,
filesystems=axes.FILESYSTEMS,
lifecycles="",
label="priority",
)
class TestProblems(unittest.TestCase):
def test_nothing_to_check_is_not_a_problem(self) -> None:
self.assertEqual(_problems(""), ([], []))
def test_a_token_the_branch_can_deploy_passes(self) -> None:
self.assertEqual(_problems("web-app-a#0@swarm+tor"), ([], []))
def test_a_variant_the_role_no_longer_declares_is_an_error(self) -> None:
errors, warnings = _problems("web-app-a#7@compose+tor")
self.assertEqual(warnings, [])
self.assertIn("matches no discovered row", errors[0])
self.assertIn("2 variant(s)", errors[0])
def test_a_bare_role_that_matches_nothing_only_warns(self) -> None:
errors, warnings = _problems("web-app-gone")
self.assertEqual(errors, [])
self.assertIn("web-app-gone", warnings[0])
def test_a_mode_the_row_does_not_offer_is_an_error(self) -> None:
errors, _warnings = _problems("web-app-a#1@swarm+clearnet")
self.assertIn("pinned mode 'swarm' is not available", errors[0])
def test_an_onion_state_the_variant_rules_out_is_an_error(self) -> None:
errors, _warnings = _problems("web-app-a#1@compose+tor")
self.assertIn("pinned onion state tor is impossible", errors[0])
def test_an_onion_state_the_runs_tor_axis_rules_out_is_an_error(self) -> None:
errors, _warnings = _problems(
"web-app-a#0@compose+clearnet", tor_mode="enforced"
)
self.assertIn("pinned onion state clearnet is impossible", errors[0])
def test_every_offender_is_reported_not_just_the_first(self) -> None:
errors, _warnings = _problems(
"web-app-a#7@compose+tor web-app-a#1@swarm+tor web-app-b#9@swarm+tor"
)
self.assertEqual(len(errors), 3)
def test_a_csv_token_is_checked_variant_by_variant(self) -> None:
errors, _warnings = _problems("web-app-a#0,7@compose+clearnet")
self.assertEqual(len(errors), 1)
self.assertIn("matches no discovered row", errors[0])
class TestMain(unittest.TestCase):
def _main(self, argv: list[str]) -> tuple[int, str, str]:
out, err = io.StringIO(), io.StringIO()
with (
mock.patch.object(validate.query, "discover_rows", return_value=_ROWS),
mock.patch.object(validate, "get_variants", return_value=_VARIANTS),
redirect_stdout(out),
redirect_stderr(err),
):
code = validate.main(argv)
return code, out.getvalue(), err.getvalue()
def test_a_valid_selection_exits_zero(self) -> None:
code, out, _err = self._main(["--priority", "web-app-a#0@swarm+tor"])
self.assertEqual(code, 0)
self.assertIn("valid for this branch", out)
def test_an_unusable_selection_exits_non_zero(self) -> None:
code, _out, err = self._main(["--priority", "web-app-a#7"])
self.assertEqual(code, 1)
self.assertIn("::error::", err)
def test_both_inputs_are_checked_and_named_in_the_message(self) -> None:
code, _out, err = self._main(
["--whitelist", "web-app-a#7", "--priority", "web-app-b#9"]
)
self.assertEqual(code, 1)
self.assertIn("whitelist:", err)
self.assertIn("priority:", err)
def test_a_warning_alone_does_not_fail_the_run(self) -> None:
code, out, _err = self._main(["--whitelist", "web-app-gone"])
self.assertEqual(code, 0)
self.assertIn("::warning::", out)
if __name__ == "__main__":
unittest.main()