Files
OBLITERATUS/tests/test_check_pr_partitions.py
Joseph MaglyandClaude Opus 5.5 636fd5c19a ci: shard PR core tests across a duration-balanced matrix
Since #272 the "PR core tests" job runs every selected non-Gloo test in
one runner under a 10-minute timeout, and broad selections now exceed it.
Shard it like the Gloo tests: pr-select emits core_shards from a single
CORE_SHARD_COUNT constant and writes selected-core-N.txt once with
scripts/shard_pr_tests.py --kind core, balanced by per-file durations
measured from CI JUnit (ci/core-test-durations.json; unlisted files use
default_seconds). pr-core-tests becomes a matrix whose empty shards record
exit code 5, and check_pr_partitions.py requires the exact core-N and
gloo-N artifact set before "Pull request core" combines coverage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 17:41:18 -04:00

284 lines
11 KiB
Python

"""Fail-closed contracts for PR partition artifacts and upstream job results."""
from __future__ import annotations
import json
import sys
import pytest
from coverage import CoverageData
from scripts import check_pr_partitions as checker
SHA = "a" * 40
SUCCESS = dict.fromkeys(checker.NEEDS_ENV, "success")
def write_partition(root, partition_name, code=0, **overrides):
partition = partition_name
directory = root / f"pr-tests-{partition}"
directory.mkdir(parents=True, exist_ok=True)
status = {"schema_version": 1, "partition": partition, "exit_code": code, "candidate_sha": SHA}
status.update(overrides)
(directory / f"status-{partition}.json").write_text(json.dumps(status))
# A collected-empty partition can still emit data. It must be excluded.
write_coverage(directory / f"coverage-{partition}.data")
return directory
def write_coverage(path):
data = CoverageData(basename=str(path))
data.add_lines({str(path.with_suffix(".py")): [1]})
data.write()
@pytest.fixture
def artifacts(tmp_path):
root = tmp_path / "partitions"
write_partition(root, "core-0")
write_partition(root, "gloo-0")
return root
def check(root, **overrides):
args = {"candidate_sha": SHA, "core_shards": [0], "shards": [0], "needs_results": SUCCESS}
args.update(overrides)
return checker.check_partitions(root, **args)
def test_success_prints_every_partition_and_returns_only_their_data(artifacts, capsys):
assert check(artifacts) == [
artifacts / "pr-tests-core-0/coverage-core-0.data",
artifacts / "pr-tests-gloo-0/coverage-gloo-0.data",
]
assert capsys.readouterr().out.splitlines() == [
"core-0 pytest exit code: 0", "gloo-0 pytest exit code: 0",
]
@pytest.mark.parametrize("partition", ["core-0", "gloo-0"])
def test_missing_artifact_fails_closed(artifacts, partition):
directory = artifacts / f"pr-tests-{partition}"
for path in directory.iterdir():
path.unlink()
directory.rmdir()
with pytest.raises(ValueError, match="artifact set mismatch"):
check(artifacts)
@pytest.mark.parametrize("partition", ["core-0", "gloo-0"])
@pytest.mark.parametrize("empty", [False, True])
def test_passing_partition_requires_nonempty_coverage(artifacts, partition, empty):
data = artifacts / f"pr-tests-{partition}/coverage-{partition}.data"
if empty:
data.write_bytes(b"")
else:
data.unlink()
with pytest.raises(ValueError, match="passed without non-empty coverage"):
check(artifacts)
@pytest.mark.parametrize("override,match", [
({"schema_version": 2}, "schema_version"),
({"schema_version": True}, "schema_version"),
({"partition": "gloo-1"}, "partition mismatch"),
({"candidate_sha": "b" * 40}, "candidate_sha mismatch"),
({"candidate_sha": ""}, "candidate_sha mismatch"),
*[({"exit_code": code}, "invalid exit_code") for code in [1, 2, 3, 4, 6, -1, True, "0", None]],
])
def test_bad_status_fails_closed(artifacts, override, match):
write_partition(artifacts, "core-0", **override)
with pytest.raises(ValueError, match=match):
check(artifacts)
def test_all_empty_partitions_fail(artifacts):
for partition in ["core-0", "gloo-0"]:
write_partition(artifacts, partition, 5)
with pytest.raises(ValueError, match="no tests ran"):
check(artifacts)
@pytest.mark.parametrize("empty", ["core-0", "gloo-0"])
def test_one_empty_partition_succeeds_and_excludes_its_data(artifacts, empty):
write_partition(artifacts, empty, 5)
passing = "gloo-0" if empty == "core-0" else "core-0"
assert check(artifacts) == [artifacts / f"pr-tests-{passing}/coverage-{passing}.data"]
(artifacts / f"pr-tests-{empty}/coverage-{empty}.data").unlink()
assert len(check(artifacts)) == 1
def test_unexpected_extra_partition_fails(artifacts):
write_partition(artifacts, "gloo-1")
with pytest.raises(ValueError, match="unexpected=.*pr-tests-gloo-1"):
check(artifacts)
assert len(check(artifacts, shards=[0, 1])) == 3
def test_multiple_core_shards_require_exact_inventory(artifacts):
write_partition(artifacts, "core-1")
write_partition(artifacts, "core-2")
with pytest.raises(ValueError, match="unexpected=.*pr-tests-core-1"):
check(artifacts)
assert check(artifacts, core_shards=[0, 1, 2]) == [
artifacts / f"pr-tests-{partition}/coverage-{partition}.data"
for partition in ["core-0", "core-1", "core-2", "gloo-0"]
]
def test_missing_additional_core_shard_fails_closed(artifacts):
with pytest.raises(ValueError, match="missing=.*pr-tests-core-1"):
check(artifacts, core_shards=[0, 1])
def test_wrong_sha_additional_core_shard_fails_closed(artifacts):
write_partition(artifacts, "core-1", candidate_sha="b" * 40)
with pytest.raises(ValueError, match="core-1: candidate_sha mismatch"):
check(artifacts, core_shards=[0, 1])
def test_additional_core_shard_ran_without_coverage_fails_closed(artifacts):
directory = write_partition(artifacts, "core-1")
(directory / "coverage-core-1.data").unlink()
with pytest.raises(ValueError, match="core-1: passed without non-empty coverage"):
check(artifacts, core_shards=[0, 1])
def test_all_empty_core_and_gloo_matrices_fail(artifacts):
for partition in ["core-0", "core-1", "gloo-0", "gloo-1"]:
write_partition(artifacts, partition, 5)
with pytest.raises(ValueError, match="no tests ran"):
check(artifacts, core_shards=[0, 1], shards=[0, 1])
@pytest.mark.parametrize("inventory", [[], [1], [0, 0], [0, 2], [False], "0", {}, None])
def test_invalid_core_inventory_fails_closed_in_api(artifacts, inventory):
with pytest.raises(ValueError, match="core_shards"):
check(artifacts, core_shards=inventory)
@pytest.mark.parametrize("job", checker.NEEDS_ENV)
@pytest.mark.parametrize("result", ["failure", "cancelled", "skipped", "", None])
def test_non_success_upstream_fails_even_with_valid_artifacts(artifacts, job, result):
with pytest.raises(ValueError, match="result is not success"):
check(artifacts, needs_results={**SUCCESS, job: result})
@pytest.mark.parametrize("raw", [
"", "[]", "[1]", "[0,0]", "[0,2]", "[false]", '"0"', "{}", "bad",
])
@pytest.mark.parametrize("label", ["core_shards", "gloo_shards"])
def test_bad_shard_inventory_fails_closed(raw, label):
with pytest.raises(ValueError, match=label):
checker.parse_shards(raw, label)
@pytest.mark.parametrize("contents", ["not-json", "[]", "null"])
def test_bad_status_contents_fail(artifacts, contents):
(artifacts / "pr-tests-core-0/status-core-0.json").write_text(contents)
with pytest.raises(ValueError):
check(artifacts)
def test_missing_and_unexpected_status_files_fail(artifacts):
status = artifacts / "pr-tests-core-0/status-core-0.json"
status.unlink()
with pytest.raises(ValueError, match="status file"):
check(artifacts)
write_partition(artifacts, "core-0")
(status.parent / "status-unexpected.json").write_text("{}")
with pytest.raises(ValueError, match="status file"):
check(artifacts)
def test_cli_writes_manifest_only_after_validation(artifacts, tmp_path, monkeypatch, capsys):
manifest = tmp_path / "accepted.txt"
monkeypatch.setattr(sys, "argv", [
"check_pr_partitions.py", "--artifacts", str(artifacts),
"--candidate-sha", SHA, "--gloo-shards", "[0]", "--core-shards", "[0]",
"--coverage-list", str(manifest),
])
for variable in checker.NEEDS_ENV.values():
monkeypatch.setenv(variable, "success")
write_partition(artifacts, "gloo-0", 5)
assert checker.main() == 0
assert manifest.read_text().splitlines() == [
str(artifacts / "pr-tests-core-0/coverage-core-0.data"),
]
monkeypatch.setenv("PR_GLOO_TESTS_RESULT", "failure")
assert checker.main() == 1
assert not manifest.exists()
assert "result is not success" in capsys.readouterr().out
@pytest.mark.parametrize("raw", ["", "[]", "[1]", "[0,2]", "[false]", "bad"])
@pytest.mark.parametrize("source", ["argument", "environment"])
def test_cli_rejects_missing_or_malformed_core_inventory(
artifacts, tmp_path, monkeypatch, capsys, raw, source,
):
manifest = tmp_path / "accepted.txt"
manifest.write_text("stale manifest")
argv = [
"check_pr_partitions.py", "--artifacts", str(artifacts),
"--candidate-sha", SHA, "--gloo-shards", "[0]", "--coverage-list", str(manifest),
]
monkeypatch.delenv("CORE_SHARDS", raising=False)
if source == "argument":
argv.extend(["--core-shards", raw])
elif raw:
monkeypatch.setenv("CORE_SHARDS", raw)
monkeypatch.setattr(sys, "argv", argv)
for variable in checker.NEEDS_ENV.values():
monkeypatch.setenv(variable, "success")
assert checker.main() == 1
assert not manifest.exists()
assert "core_shards" in capsys.readouterr().out
def test_needs_only_requires_no_artifacts_or_shard_output(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, "argv", ["check_pr_partitions.py", "--needs-only"])
for variable in checker.NEEDS_ENV.values():
monkeypatch.setenv(variable, "success")
assert checker.main() == 0
monkeypatch.delenv("PR_SELECT_RESULT")
assert checker.main() == 1
def test_checker_changes_select_focused_and_ci_policy_tests():
from pathlib import Path
from scripts import select_pr_tests
root = Path(__file__).parents[1]
risk = json.loads((root / "ci/test-risk-map.json").read_text())
path = "scripts/check_pr_partitions.py"
expected = {"tests/test_check_pr_partitions.py", "tests/test_ci_policy.py"}
surface = next(item for item in risk["contract_surfaces"] if path in item["paths"])
assert expected <= set(surface["required_tests"])
selected = select_pr_tests.select_tests(
[path], policy=json.loads((root / "ci/pr-test-policy.json").read_text()),
risk_maps=[risk], project_root=root,
)
assert expected <= set(selected)
@pytest.mark.parametrize("partition", ["core-0", "gloo-0"])
@pytest.mark.parametrize("payload", [b"not a database", b"SQLite format 3\x00" + b"\x00" * 64])
def test_unreadable_coverage_database_fails_closed(artifacts, partition, payload):
# coverage combine merely warns on such input and combines the rest.
(artifacts / f"pr-tests-{partition}/coverage-{partition}.data").write_bytes(payload)
with pytest.raises(ValueError, match="unreadable or measured no files"):
check(artifacts)
def test_coverage_database_without_measured_files_fails_closed(artifacts):
import sqlite3
path = artifacts / "pr-tests-gloo-0/coverage-gloo-0.data"
with sqlite3.connect(path) as connection:
connection.execute("delete from line_bits")
connection.execute("delete from file")
connection.close()
with pytest.raises(ValueError, match="unreadable or measured no files"):
check(artifacts)