Files
computer-playbook/tests/unit/python/cli/administration/deploy/ci/test_status.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

100 lines
3.5 KiB
Python

from __future__ import annotations
import io
import unittest
import unittest.mock as mock
from contextlib import redirect_stdout
from cli.administration.deploy.ci import runs
from cli.administration.deploy.ci.status import __main__ as status
from tests.utils.ci.job_names import deploy_job_name
def _job(mode: str, app: str, conclusion: str, url: str) -> dict:
return {
"name": deploy_job_name(mode, app, "0,1"),
"status": "completed",
"conclusion": conclusion,
"url": url,
}
_JOBS = [
_job("docker", "web-app-x", "success", "https://gh/cx"),
_job("swarm", "web-app-x", "failure", "https://gh/sx"),
_job("swarm", "web-app-y", "success", "https://gh/sy"),
_job("docker", "web-app-z", "failure", "https://gh/cz"),
_job("swarm", "web-app-z", "success", "https://gh/sz"),
]
_URL = "https://github.com/o/r/actions/runs/123" # nocheck: url
class TestBuildRows(unittest.TestCase):
def test_run_column_joins_available_job_urls(self) -> None:
statuses = runs.parse_role_statuses(_JOBS)
urls = runs.parse_role_urls(_JOBS)
rows = {r[0]: r for r in status._build_rows(statuses, urls)}
self.assertEqual(rows["web-app-x"][4], "https://gh/cx https://gh/sx")
self.assertEqual(rows["web-app-y"][4], "https://gh/sy")
def test_emoji_cells_and_total(self) -> None:
statuses = runs.parse_role_statuses(_JOBS)
rows = {r[0]: r for r in status._build_rows(statuses, {})}
self.assertEqual(rows["web-app-x"][1:4], (runs.PASS, runs.FAIL, runs.FAIL))
self.assertEqual(rows["web-app-y"][1:4], (runs.MISSING, runs.PASS, runs.PASS))
self.assertEqual(rows["web-app-z"][1:4], (runs.FAIL, runs.PASS, runs.FAIL))
class TestRender(unittest.TestCase):
def test_table_has_run_header_after_total(self) -> None:
rows = status._build_rows(
runs.parse_role_statuses(_JOBS), runs.parse_role_urls(_JOBS)
)
out = status._render_table(rows)
header = out.splitlines()[0]
self.assertRegex(header, r"docker\s+swarm\s+total\s+run")
self.assertIn("https://gh/sx", out)
def test_string_format_fields(self) -> None:
rows = status._build_rows(
runs.parse_role_statuses(_JOBS), runs.parse_role_urls(_JOBS)
)
out = status._render_string(rows)
line = next(line for line in out.splitlines() if line.startswith("web-app-y"))
self.assertEqual(
line, f"web-app-y {runs.MISSING} {runs.PASS} {runs.PASS} https://gh/sy"
)
class TestMain(unittest.TestCase):
def _run(self, argv: list[str]) -> str:
buf = io.StringIO()
with (
mock.patch.object(runs, "fetch_jobs", return_value=_JOBS),
redirect_stdout(buf),
):
rc = status.main([*argv, "--url", _URL])
self.assertEqual(rc, 0)
return buf.getvalue()
def test_url_path_renders_all_roles(self) -> None:
out = self._run([])
self.assertIn("web-app-x", out)
self.assertIn("web-app-y", out)
def test_failed_swarm_filters_to_non_green_swarm(self) -> None:
out = self._run(["--failed", "swarm"])
self.assertIn("web-app-x", out)
self.assertNotIn("web-app-y", out)
def test_failed_compose_filters_to_non_green_docker(self) -> None:
out = self._run(["--failed", "compose"])
self.assertIn("web-app-z", out)
self.assertNotIn("web-app-x", out)
self.assertNotIn("web-app-y", out)
if __name__ == "__main__":
unittest.main()