|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ci: gate |
| 3 | +"""Keep the PR gate on scripts/ci small and honest. |
| 4 | +
|
| 5 | +Prevents: the required python-tests job silently dropping the data-loss and |
| 6 | +outage tests, or growing back into a suite that is red on every PR (59 of 60 |
| 7 | +Validate PR runs before 2026-10-31 failed on brittle tests, not real catches). |
| 8 | +
|
| 9 | +Only files carrying the exact line `# ci: gate` right after the shebang block |
| 10 | +PRs. Every other test runs in the advisory job, which never blocks. Asserts: |
| 11 | +
|
| 12 | + 1. The gated files are exactly EXPECTED_GATE (owner-approved 2026-10-31; |
| 13 | + changing it is an owner decision), at most MAX_GATE, and each docstring |
| 14 | + has a `Prevents:` line naming the data loss or outage it guards. |
| 15 | + 2. validate.yaml's `python-tests` job runs only gate files, fails when it |
| 16 | + finds none, has no continue-on-error, and is in `success` needs. |
| 17 | + 3. `python-tests-advisory` runs only non-gate files, has |
| 18 | + `continue-on-error: true`, and is NOT in `success` needs. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import ast |
| 24 | +import sys |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | +import yaml |
| 28 | + |
| 29 | +ROOT = Path(__file__).resolve().parents[2] |
| 30 | +CI = ROOT / "scripts" / "ci" |
| 31 | +VALIDATE = ROOT / ".github" / "workflows" / "validate.yaml" |
| 32 | +MARKER = "# ci: gate" |
| 33 | +MAX_GATE = 15 |
| 34 | +EXPECTED_GATE = { |
| 35 | + "alert-coverage-gaps-test.py", |
| 36 | + "backup-silent-failure-alerting-test.py", |
| 37 | + "ci-test-gate-test.py", |
| 38 | + "corrupt-claim-recreation-contract-test.py", |
| 39 | + "flux-annotation-values-test.py", |
| 40 | + "hostpolicy-ceph-selector-test.py", |
| 41 | + "kopiur-stage0-test.py", |
| 42 | + "kopiur-stage1-test.py", |
| 43 | + "kopiur-stage3-test.py", |
| 44 | + "kopiur-stage5-test.py", |
| 45 | + "pvc-mover-readable-check-test.py", |
| 46 | + "pvc-writable-check-test.py", |
| 47 | + "volsync-retired-expiry-test.py", |
| 48 | +} |
| 49 | + |
| 50 | +failures: list[str] = [] |
| 51 | + |
| 52 | + |
| 53 | +def check(cond: bool, msg: str) -> None: |
| 54 | + print(("[PASS] " if cond else "[FAIL] ") + msg) |
| 55 | + if not cond: |
| 56 | + failures.append(msg) |
| 57 | + |
| 58 | + |
| 59 | +def gate_files() -> list[Path]: |
| 60 | + out = [] |
| 61 | + for path in sorted(CI.glob("*-test.py")): |
| 62 | + raw = path.read_bytes() |
| 63 | + second_line = raw.split(b"\n", 2)[1] if raw.count(b"\n") >= 2 else b"" |
| 64 | + if second_line.endswith(b"\r"): |
| 65 | + failures.append(f"{path.name}: CRLF line 2 is invisible to `grep -x`") |
| 66 | + print(f"[FAIL] {path.name}: CRLF line 2 is invisible to `grep -x`") |
| 67 | + lines = raw.decode().splitlines() |
| 68 | + if len(lines) > 1 and lines[0].startswith("#!") and lines[1] == MARKER: |
| 69 | + out.append(path) |
| 70 | + elif MARKER in lines: |
| 71 | + failures.append(f"{path.name}: `{MARKER}` must be line 2, right after the shebang") |
| 72 | + print(f"[FAIL] {path.name}: `{MARKER}` must be line 2, right after the shebang") |
| 73 | + return out |
| 74 | + |
| 75 | + |
| 76 | +def run_script(job: dict) -> str: |
| 77 | + return "\n".join(step.get("run", "") for step in job.get("steps", [])) |
| 78 | + |
| 79 | + |
| 80 | +def main() -> int: |
| 81 | + gates = gate_files() |
| 82 | + names = {p.name for p in gates} |
| 83 | + check(len(gates) <= MAX_GATE, f"{len(gates)} gate files (<= {MAX_GATE})") |
| 84 | + check(names == EXPECTED_GATE, |
| 85 | + f"gate set matches the approved list (missing {sorted(EXPECTED_GATE - names)}, " |
| 86 | + f"unexpected {sorted(names - EXPECTED_GATE)})") |
| 87 | + for path in gates: |
| 88 | + doc = ast.get_docstring(ast.parse(path.read_text())) or "" |
| 89 | + check( |
| 90 | + any(line.startswith("Prevents:") for line in doc.splitlines()), |
| 91 | + f"{path.name} docstring has a `Prevents:` line", |
| 92 | + ) |
| 93 | + |
| 94 | + jobs = yaml.safe_load(VALIDATE.read_text())["jobs"] |
| 95 | + gate_job = jobs.get("python-tests") or {} |
| 96 | + advisory = jobs.get("python-tests-advisory") or {} |
| 97 | + success_needs = (jobs.get("success") or {}).get("needs") or [] |
| 98 | + |
| 99 | + gate_run = run_script(gate_job) |
| 100 | + check(f"grep -lx '{MARKER}'" in gate_run, "python-tests selects files by the gate marker") |
| 101 | + check("${#gates[@]} == 0" in gate_run, "python-tests fails when it selects no gate files") |
| 102 | + check("continue-on-error" not in gate_job, "python-tests has no continue-on-error") |
| 103 | + check("python-tests" in success_needs, "python-tests is in success.needs") |
| 104 | + |
| 105 | + adv_run = run_script(advisory) |
| 106 | + check(f"grep -Lx '{MARKER}'" in adv_run, "python-tests-advisory selects the non-gate files") |
| 107 | + check(advisory.get("continue-on-error") is True, "python-tests-advisory has continue-on-error: true") |
| 108 | + check("python-tests-advisory" not in success_needs, "python-tests-advisory is not in success.needs") |
| 109 | + |
| 110 | + print(f"Summary: {len(failures)} failed") |
| 111 | + return 1 if failures else 0 |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + sys.exit(main()) |
0 commit comments