|
| 1 | +"""Execute the real CI aggregation shell with GitHub dependency outcomes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import os |
| 8 | +import re |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +import unittest |
| 13 | +from pathlib import Path |
| 14 | +from typing import ClassVar |
| 15 | + |
| 16 | +ROOT = Path(__file__).resolve().parents[2] |
| 17 | +HISTORICAL_COMMIT = "be0892d55187c2ebb9ee24109020558241eaab14" |
| 18 | +HISTORICAL = False |
| 19 | +JOBS = ( |
| 20 | + "changes", |
| 21 | + "compose-validate", |
| 22 | + "python", |
| 23 | + "cli", |
| 24 | + "launcher", |
| 25 | + "docker-build", |
| 26 | + "docker-build-arm64", |
| 27 | + "security", |
| 28 | + "telemetry", |
| 29 | + "actionlint", |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def aggregate_script(historical: bool) -> str: |
| 34 | + """Read the single literal shell block in the actual ci-ok job. |
| 35 | +
|
| 36 | + This deliberately accepts only this job's existing shape, rather than |
| 37 | + pretending to parse arbitrary YAML. A changed/ambiguous shape is an |
| 38 | + infrastructure error, never a successful historical reproduction. |
| 39 | + """ |
| 40 | + for binary in ("bash", "jq"): |
| 41 | + if shutil.which(binary) is None: |
| 42 | + raise RuntimeError(f"Required local executable missing: {binary}") |
| 43 | + content = ( |
| 44 | + subprocess.check_output( |
| 45 | + ["git", "show", f"{HISTORICAL_COMMIT}:.github/workflows/ci.yml"], |
| 46 | + cwd=ROOT, |
| 47 | + text=True, |
| 48 | + ) |
| 49 | + if historical |
| 50 | + else (ROOT / ".github/workflows/ci.yml").read_text() |
| 51 | + ) |
| 52 | + lines = content.splitlines() |
| 53 | + starts = [i for i, line in enumerate(lines) if line == " ci-ok:"] |
| 54 | + if len(starts) != 1: |
| 55 | + raise RuntimeError("Expected exactly one ci-ok job") |
| 56 | + start = starts[0] |
| 57 | + end = next( |
| 58 | + (i for i in range(start + 1, len(lines)) if re.match(r"^ [\w-]+:", lines[i])), |
| 59 | + len(lines), |
| 60 | + ) |
| 61 | + job = lines[start:end] |
| 62 | + blocks = [i for i, line in enumerate(job) if line == " run: |"] |
| 63 | + if len(blocks) != 1: |
| 64 | + raise RuntimeError("Expected one literal aggregation shell block") |
| 65 | + body: list[str] = [] |
| 66 | + for line in job[blocks[0] + 1 :]: |
| 67 | + if line and not line.startswith(" "): |
| 68 | + break |
| 69 | + body.append(line[10:] if line else "") |
| 70 | + script = "\n".join(body) |
| 71 | + if not script.strip(): |
| 72 | + raise RuntimeError("Aggregation shell block is empty") |
| 73 | + subprocess.run(["bash", "-n", "-c", script], check=True, capture_output=True) |
| 74 | + return script |
| 75 | + |
| 76 | + |
| 77 | +class CiAggregateTests(unittest.TestCase): |
| 78 | + script: ClassVar[str] |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def setUpClass(cls) -> None: |
| 82 | + cls.script = aggregate_script(HISTORICAL) |
| 83 | + |
| 84 | + def assert_outcome(self, payload: str, expected: bool) -> None: |
| 85 | + result = subprocess.run( |
| 86 | + ["bash", "-e", "-c", self.script], |
| 87 | + cwd=ROOT, |
| 88 | + env={**os.environ, "RESULTS": payload}, |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + timeout=5, |
| 92 | + ) |
| 93 | + if result.returncode in (126, 127): |
| 94 | + raise RuntimeError("Aggregation shell could not execute a required command") |
| 95 | + self.assertEqual(result.returncode == 0, expected, result.stdout + result.stderr) |
| 96 | + |
| 97 | + def test_success_and_intentional_scope_skips(self) -> None: |
| 98 | + for status in ("success", "skipped"): |
| 99 | + with self.subTest(status=status): |
| 100 | + self.assert_outcome( |
| 101 | + json.dumps({job: {"result": status, "outputs": {}} for job in JOBS}), |
| 102 | + True, |
| 103 | + ) |
| 104 | + mixed = {job: {"result": "success"} for job in JOBS} |
| 105 | + for job in ("python", "docker-build", "docker-build-arm64", "telemetry"): |
| 106 | + mixed[job] = {"result": "skipped"} |
| 107 | + self.assert_outcome(json.dumps(mixed), True) |
| 108 | + |
| 109 | + def test_unfinished_and_unknown_outcomes_are_refused(self) -> None: |
| 110 | + for status in ( |
| 111 | + "failure", |
| 112 | + "cancelled", |
| 113 | + "abandoned", |
| 114 | + "timed_out", |
| 115 | + "neutral", |
| 116 | + "stale", |
| 117 | + "action_required", |
| 118 | + "queued", |
| 119 | + "in_progress", |
| 120 | + "unknown", |
| 121 | + "", |
| 122 | + ): |
| 123 | + with self.subTest(status=status): |
| 124 | + needs = {job: {"result": "success"} for job in JOBS} |
| 125 | + needs["launcher"] = {"result": status} |
| 126 | + self.assert_outcome(json.dumps(needs), False) |
| 127 | + |
| 128 | + def test_missing_or_invalid_results_are_refused(self) -> None: |
| 129 | + for payload in ( |
| 130 | + "{}", |
| 131 | + "[]", |
| 132 | + "null", |
| 133 | + '"success"', |
| 134 | + "not json", |
| 135 | + "", |
| 136 | + '{"launcher":{}}', |
| 137 | + '{"launcher":{"result":null}}', |
| 138 | + ): |
| 139 | + with self.subTest(payload=payload): |
| 140 | + self.assert_outcome(payload, False) |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + parser = argparse.ArgumentParser() |
| 145 | + parser.add_argument("--historical", action="store_true") |
| 146 | + HISTORICAL = parser.parse_args().historical |
| 147 | + suite = unittest.defaultTestLoader.loadTestsFromTestCase(CiAggregateTests) |
| 148 | + outcome = unittest.TextTestRunner(verbosity=2).run(suite) |
| 149 | + # Setup, executable, parse and timeout errors cannot prove a regression. |
| 150 | + sys.exit(2 if outcome.errors else 0 if outcome.wasSuccessful() else 1) |
0 commit comments