#!/usr/bin/env python3 """Fail closed on PR job results and candidate-bound partition coverage evidence.""" from __future__ import annotations import argparse import json import os import sqlite3 from pathlib import Path NEEDS_ENV = { "pr-select": "PR_SELECT_RESULT", "pr-core-tests": "PR_CORE_TESTS_RESULT", "pr-gloo-tests": "PR_GLOO_TESTS_RESULT", } def coverage_database_is_usable(path: Path) -> bool: """Return whether *path* is a readable coverage.py database that measured files. ``coverage combine`` only warns about an unreadable input and still combines the rest, so a corrupt partition database must be rejected here. """ try: connection = sqlite3.connect(f"{path.resolve().as_uri()}?mode=ro", uri=True) except sqlite3.Error: return False try: tables = { row[0] for row in connection.execute("select name from sqlite_master where type='table'") } if not {"coverage_schema", "meta", "file"} <= tables: return False return connection.execute("select count(*) from file").fetchone()[0] > 0 except sqlite3.Error: return False finally: connection.close() def check_needs(results: dict[str, str]) -> None: """Require successful completion of all upstream jobs, including the matrix.""" for job in NEEDS_ENV: if results.get(job) != "success": raise ValueError(f"upstream {job} result is not success: {results.get(job)!r}") def parse_shards(raw: str, label: str) -> list[int]: """Require the selector's nonempty contiguous, zero-based shard inventory.""" try: shards = json.loads(raw) except ValueError as exc: raise ValueError(f"{label} must be valid JSON") from exc if (not isinstance(shards, list) or not shards or any(type(shard) is not int for shard in shards) or shards != list(range(len(shards)))): raise ValueError(f"{label} must be a nonempty array of consecutive integers from 0") return shards def check_partitions( artifacts: Path, *, candidate_sha: str, core_shards: list[int], shards: list[int], needs_results: dict[str, str], ) -> list[Path]: """Validate the exact artifact set and return only passing partitions' data.""" check_needs(needs_results) parse_shards(json.dumps(core_shards), "core_shards") parse_shards(json.dumps(shards), "gloo_shards") if not candidate_sha: raise ValueError("candidate_sha must not be empty") partitions = [ *(f"core-{shard}" for shard in core_shards), *(f"gloo-{shard}" for shard in shards), ] expected = {f"pr-tests-{partition}" for partition in partitions} if not artifacts.is_dir(): raise ValueError("missing partition artifacts directory") actual = {path.name for path in artifacts.iterdir()} if actual != expected: raise ValueError( f"partition artifact set mismatch: missing={sorted(expected - actual)}, " f"unexpected={sorted(actual - expected)}" ) accepted = [] for partition in partitions: directory = artifacts / f"pr-tests-{partition}" status_path = directory / f"status-{partition}.json" if not directory.is_dir() or list(directory.glob("status-*.json")) != [status_path]: raise ValueError(f"{partition}: missing or unexpected status file") status = json.loads(status_path.read_text(encoding="utf-8")) if not isinstance(status, dict): raise ValueError(f"{partition}: status must be an object") if type(status.get("schema_version")) is not int or status["schema_version"] != 1: raise ValueError(f"{partition}: status schema_version must be 1") if status.get("partition") != partition: raise ValueError(f"{partition}: status partition mismatch") if status.get("candidate_sha") != candidate_sha: raise ValueError(f"{partition}: candidate_sha mismatch") code = status.get("exit_code") print(f"{partition} pytest exit code: {code}") if type(code) is not int or code not in {0, 5}: raise ValueError(f"{partition}: invalid exit_code {code!r}") if code == 0: data = directory / f"coverage-{partition}.data" if not data.is_file() or data.stat().st_size == 0: raise ValueError(f"{partition}: passed without non-empty coverage data") if not coverage_database_is_usable(data): raise ValueError(f"{partition}: coverage data is unreadable or measured no files") accepted.append(data) if not accepted: raise ValueError("all partitions returned exit_code 5; no tests ran") return accepted def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--needs-only", action="store_true") parser.add_argument("--artifacts", type=Path, default=Path("test-results/partitions")) parser.add_argument("--candidate-sha", default=os.environ.get("CANDIDATE_SHA", "")) parser.add_argument("--gloo-shards", default=os.environ.get("GLOO_SHARDS", "")) parser.add_argument("--core-shards", default=os.environ.get("CORE_SHARDS", "")) parser.add_argument("--coverage-list", type=Path, default=Path("test-results/accepted-coverage.txt")) args = parser.parse_args() results = {job: os.environ.get(variable, "") for job, variable in NEEDS_ENV.items()} try: # A stale manifest cannot survive a rejected validation. args.coverage_list.unlink(missing_ok=True) check_needs(results) if not args.needs_only: accepted = check_partitions( args.artifacts, candidate_sha=args.candidate_sha, core_shards=parse_shards(args.core_shards, "core_shards"), shards=parse_shards(args.gloo_shards, "gloo_shards"), needs_results=results, ) args.coverage_list.parent.mkdir(parents=True, exist_ok=True) args.coverage_list.write_text( "".join(f"{path.as_posix()}\n" for path in accepted), encoding="utf-8", ) except (OSError, ValueError) as exc: print(f"PR partition validation failed: {exc}") return 1 return 0 if __name__ == "__main__": raise SystemExit(main())