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

205 lines
6.5 KiB
Python

# nocheck: workflow-references synthetic fixture workflows, not repository files
from __future__ import annotations
import textwrap
import unittest
import unittest.mock as mock
from pathlib import Path
from tempfile import TemporaryDirectory
from cli.meta.ci import slots
_SETTINGS = {
"INFINITO_CI_JOB_LIMIT": "256",
"INFINITO_CI_CONCURRENCY": "20",
"INFINITO_CI_QUEUE_HOURS": "24",
"INFINITO_CI_MAX_CHUNKS": "3",
}
def _write(root: Path, rel: str, content: str) -> None:
path = root / rel
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(textwrap.dedent(content), encoding="utf-8")
def _tree(timeout: int = 355) -> TemporaryDirectory:
tmp = TemporaryDirectory()
root = Path(tmp.name)
_write(
root,
slots._ORCHESTRATOR,
"""
jobs:
gate: {}
build:
needs: [gate]
test-deploy-chunk-0:
needs: [build]
uses: ./.github/workflows/call-test-deploy.yml
test-deploy-chunk-1:
needs: [test-deploy-chunk-0]
uses: ./.github/workflows/call-test-deploy.yml
installs:
needs: [gate]
uses: ./.github/workflows/installs.yml
done:
needs: [test-deploy-chunk-0, test-deploy-chunk-1]
""",
)
_write(
root,
slots._DEPLOY_WORKFLOW,
f"""
jobs:
discover: {{}}
deploy:
timeout-minutes: {timeout}
strategy:
matrix:
include: ${{{{ fromJson(needs.discover.outputs.apps) }}}}
""",
)
_write(
root,
".github/workflows/installs.yml",
"""
jobs:
static:
strategy:
matrix:
distro: [a, b, c]
dynamic:
strategy:
matrix:
include: ${{ fromJson(needs.x.outputs.y) }}
""",
)
_write(
root,
".github/workflows/entry-small.yml",
"""
jobs:
orchestrate:
uses: ./.github/workflows/call-orchestrator.yml
""",
)
_write(
root,
".github/workflows/entry-big.yml",
"""
jobs:
policy: {}
release:
uses: ./.github/workflows/installs.yml
orchestrate:
uses: ./.github/workflows/call-orchestrator.yml
""",
)
return tmp
class TestReservedSlots(unittest.TestCase):
def test_counts_the_whole_chain_but_not_chunk_matrices(self) -> None:
with _tree() as tmp:
self.assertEqual(
slots.reserved_slots(Path(tmp)),
2 + 1 + 1 + (3 + slots._DYNAMIC_MATRIX_ESTIMATE) + 1,
)
def test_a_chunk_block_reserves_only_its_discover_job(self) -> None:
with _tree() as tmp:
root = Path(tmp)
jobs = slots._jobs(slots._load_workflow(root / slots._ORCHESTRATOR))
self.assertEqual(
slots._job_slots(
root, jobs["test-deploy-chunk-0"], count_dynamic=False
),
1,
)
def test_a_block_outside_the_chunk_prefix_is_charged_its_matrix(self) -> None:
with _tree() as tmp:
root = Path(tmp)
jobs = slots._jobs(slots._load_workflow(root / slots._ORCHESTRATOR))
self.assertEqual(
slots._job_slots(root, jobs["test-deploy-chunk-0"], count_dynamic=True),
1 + slots._DYNAMIC_MATRIX_ESTIMATE,
)
class TestEntryOverhead(unittest.TestCase):
def test_worst_entry_counts_jobs_around_its_orchestrator_call(self) -> None:
with _tree() as tmp:
self.assertEqual(
slots.entry_overhead(Path(tmp)),
1 + (3 + slots._DYNAMIC_MATRIX_ESTIMATE),
)
class TestJobTimeout(unittest.TestCase):
def test_read_from_the_deploy_workflow(self) -> None:
with _tree(timeout=120) as tmp:
self.assertEqual(slots.job_timeout_minutes(Path(tmp)), 120)
def test_a_missing_timeout_fails_loudly(self) -> None:
with _tree() as tmp:
root = Path(tmp)
_write(root, slots._DEPLOY_WORKFLOW, "jobs:\n deploy: {}\n")
with self.assertRaises(SystemExit):
slots.job_timeout_minutes(root)
class TestChunkArithmetic(unittest.TestCase):
def test_waves_fit_inside_the_queue_window(self) -> None:
with _tree(timeout=355) as tmp, mock.patch.dict("os.environ", _SETTINGS):
self.assertEqual(slots.waves(Path(tmp)), 4)
self.assertEqual(slots.chunk_size(Path(tmp)), 80)
def test_a_timeout_at_the_window_leaves_one_wave(self) -> None:
with _tree(timeout=24 * 60) as tmp, mock.patch.dict("os.environ", _SETTINGS):
self.assertEqual(slots.waves(Path(tmp)), 1)
def test_a_timeout_beyond_the_window_still_leaves_one_wave(self) -> None:
with _tree(timeout=30 * 60) as tmp, mock.patch.dict("os.environ", _SETTINGS):
self.assertEqual(slots.waves(Path(tmp)), 1)
def test_chunk_count_is_capped_by_the_declared_blocks(self) -> None:
with (
_tree() as tmp,
mock.patch.dict("os.environ", {**_SETTINGS, "INFINITO_CI_MAX_CHUNKS": "2"}),
):
self.assertEqual(slots.chunk_count(Path(tmp)), 2)
def test_a_small_budget_needs_a_single_chunk(self) -> None:
with (
_tree() as tmp,
mock.patch.dict("os.environ", {**_SETTINGS, "INFINITO_CI_JOB_LIMIT": "30"}),
):
root = Path(tmp)
self.assertEqual(slots.chunk_count(root), 1)
self.assertEqual(slots.rows_per_sweep(root), slots.available(root))
def test_rows_per_sweep_never_exceeds_the_run_job_cap(self) -> None:
with _tree() as tmp, mock.patch.dict("os.environ", _SETTINGS):
root = Path(tmp)
self.assertLessEqual(slots.rows_per_sweep(root), slots.available(root))
class TestRenderMatrix(unittest.TestCase):
def test_table_lists_every_job_and_the_chunk_arithmetic(self) -> None:
with (
_tree() as tmp,
mock.patch.object(slots, "PROJECT_ROOT", Path(tmp)),
mock.patch.dict("os.environ", _SETTINGS),
):
table = slots.render_matrix()
self.assertIn("installs", table)
self.assertRegex(table, r"job limit \(INFINITO_CI_JOB_LIMIT\)\s+256\n")
for row in ("chunk size", "chunks filled", "rows per sweep", "waves"):
self.assertIn(row, table)
if __name__ == "__main__":
unittest.main()