Files
OBLITERATUS/tests/test_shard_pr_tests.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

282 lines
12 KiB
Python

"""Contracts for duration balancing independent of the core and Gloo duration inventories."""
from __future__ import annotations
import json
from pathlib import Path
import sys
import pytest
from scripts import select_pr_tests, shard_pr_tests
ROOT = Path(__file__).parents[1]
def _files(root, names, *, marked=()):
for name in names:
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("pytestmark = pytest.mark.gloo\n" if name in marked else "", encoding="utf-8")
def _table(path, **overrides):
table = {"schema_version": 1, "source": "fixture", "default_seconds": 80, "files": {}}
table.update(overrides)
path.write_text(json.dumps(table), encoding="utf-8")
return path
def test_measured_lpt_balances_known_files_instead_of_alphabetical_halves(tmp_path):
durations = {
"tests/test_distributed_gloo.py": 141,
"tests/test_distributed_model_gloo.py": 71,
"tests/test_distributed_probe_gloo.py": 71,
}
_files(tmp_path, durations, marked=durations)
default, durations = shard_pr_tests.load_durations(
_table(tmp_path / "durations.json", files=durations),
)
assert default == 80
shards = shard_pr_tests.shard_tests(list(durations), default, durations, root=tmp_path)
assert shards == (
["tests/test_distributed_gloo.py"],
["tests/test_distributed_model_gloo.py", "tests/test_distributed_probe_gloo.py"],
)
assert [sum(durations[name] for name in shard) for shard in shards] == [141, 142]
def test_unlisted_marked_files_get_default_but_all_files_are_assigned_once(tmp_path):
names = ["tests/test_known.py", "tests/test_new.py", "tests/nested/test_core.py"]
_files(tmp_path, names, marked=[names[1]])
assert shard_pr_tests.shard_tests(names, 80, {names[0]: 100}, root=tmp_path) == (
[names[0]], [names[1], names[2]],
)
# Removing every measurement changes balance, never membership.
shards = shard_pr_tests.shard_tests(names, 80, {}, root=tmp_path)
assert sorted(shards[0] + shards[1]) == sorted(names)
def test_core_unknown_files_use_default_and_balance_with_measured_durations(tmp_path):
names = ["tests/test_known.py", "tests/test_new.py", "tests/nested/test_core.py"]
_files(tmp_path, names, marked=[names[1]])
shards = shard_pr_tests.shard_tests(
names * 2, 80, {names[0]: 160}, root=tmp_path, kind="core",
)
assert shards == ([names[0]], [names[2], names[1]])
assert [sum({names[0]: 160}.get(name, 80) for name in shard) for shard in shards] == [160, 160]
def test_marker_mentioned_only_in_strings_or_comments_has_no_weight(tmp_path):
names = ["tests/test_policy.py", "tests/test_worker.py"]
_files(tmp_path, names, marked=[names[1]])
(tmp_path / names[0]).write_text(
'# pytest.mark.gloo\nFIXTURE = "pytestmark = pytest.mark.gloo"\n', encoding="utf-8"
)
assert not shard_pr_tests.has_gloo_marker((tmp_path / names[0]).read_text())
assert shard_pr_tests.has_gloo_marker((tmp_path / names[1]).read_text())
assert shard_pr_tests.shard_tests(names, 80, {}, root=tmp_path) == (
[names[1]], [names[0]],
)
def test_real_selection_keeps_policy_tests_out_of_gloo_balance():
default, durations = shard_pr_tests.load_durations(ROOT / "ci/gloo-test-durations.json")
policy_tests = ["tests/test_ci_policy.py", "tests/test_shard_pr_tests.py"]
weighted_shards = shard_pr_tests.shard_tests(list(durations), default, durations, root=ROOT)
names = [*durations, *policy_tests]
shards = shard_pr_tests.shard_tests(names, default, durations, root=ROOT)
assert tuple([name for name in shard if name in durations] for shard in shards) == weighted_shards
assert sorted(name for shard in shards for name in shard) == sorted(names)
def test_equal_weights_use_path_order_and_equal_loads_choose_zero(tmp_path):
names = [f"tests/test_{letter}.py" for letter in "dcba"]
_files(tmp_path, names)
durations = dict.fromkeys(names, 10)
assert shard_pr_tests.shard_tests(names, 80, durations, root=tmp_path) == (
["tests/test_a.py", "tests/test_c.py"], ["tests/test_b.py", "tests/test_d.py"],
)
def test_zero_weight_files_are_collected_once_and_duplicates_are_deduplicated(tmp_path):
names = ["tests/test_b.py", "tests/test_a.py"]
_files(tmp_path, names)
assert shard_pr_tests.shard_tests(names * 2, 80, {}, root=tmp_path) == (
sorted(names), [],
)
@pytest.mark.parametrize("name", [
"tests/missing.py", "tests", "scripts/tool.py", "tests/../outside.py",
"tests//test_ok.py", "./tests/test_ok.py", "tests/test_ok.py::test_one", "",
])
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_missing_outside_noncanonical_and_nonfile_paths_are_rejected(tmp_path, name, kind):
_files(tmp_path, ["tests/test_ok.py", "scripts/tool.py", "outside.py"])
with pytest.raises(ValueError, match="file inside tests/"):
shard_pr_tests.shard_tests([name], 80, {}, root=tmp_path, kind=kind)
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_absolute_and_symlink_escape_paths_are_rejected(tmp_path, kind):
_files(tmp_path, ["tests/test_ok.py", "outside.py"])
(tmp_path / "tests/link.py").symlink_to(tmp_path / "outside.py")
for name in [str(tmp_path / "tests/test_ok.py"), "tests/link.py"]:
with pytest.raises(ValueError, match="file inside tests/"):
shard_pr_tests.shard_tests([name], 80, {}, root=tmp_path, kind=kind)
@pytest.mark.parametrize("overrides", [
{"schema_version": 2}, {"source": ""}, {"files": []},
{"default_seconds": -1}, {"default_seconds": True}, {"default_seconds": "80"},
{"files": {"tests/test_a.py": float("inf")}},
{"files": {"tests/test_a.py": float("nan")}},
{"files": {"tests/test_a.py": -1}},
])
def test_invalid_duration_tables_fail(tmp_path, overrides):
with pytest.raises(ValueError):
shard_pr_tests.load_durations(_table(tmp_path / "durations.json", **overrides))
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_cli_outputs_are_byte_identical_for_reordered_input_and_empty_input(
tmp_path, monkeypatch, kind,
):
monkeypatch.chdir(tmp_path)
names = ["tests/test_b.py", "tests/test_a.py", "tests/test_core.py"]
_files(tmp_path, names, marked=names[:2])
table = _table(tmp_path / "durations.json")
selected = tmp_path / "selected.txt"
output = tmp_path / "out"
monkeypatch.setattr(sys, "argv", [
"shard_pr_tests.py", "--kind", kind, "--selected", str(selected), "--durations", str(table),
"--output-dir", str(output),
])
selected.write_text("\n".join(names) + "\n")
assert shard_pr_tests.main() == 0
files = [output / f"selected-{kind}-{side}.txt" for side in (0, 1)]
first = [path.read_bytes() for path in files]
selected.write_text("\n".join(reversed(names)) + "\n")
assert shard_pr_tests.main() == 0
assert [path.read_bytes() for path in files] == first
selected.write_text("")
assert shard_pr_tests.main() == 0
assert [path.read_bytes() for path in files] == [b"", b""]
def test_cli_rejects_invalid_input_before_writing_shards(tmp_path, monkeypatch, capsys):
monkeypatch.chdir(tmp_path)
(tmp_path / "ci").mkdir()
_table(tmp_path / "ci/gloo-test-durations.json")
(tmp_path / "test-results").mkdir()
selected = tmp_path / "test-results/selected-tests.txt"
selected.write_text("tests/missing.py\n")
monkeypatch.setattr(sys, "argv", ["shard_pr_tests.py"])
assert shard_pr_tests.main() == 1
assert "PR test sharding failed:" in capsys.readouterr().out
assert not (tmp_path / "test-results/selected-gloo-0.txt").exists()
@pytest.mark.parametrize("changed", [
"scripts/shard_pr_tests.py", "ci/gloo-test-durations.json", "ci/core-test-durations.json",
])
def test_sharder_and_duration_changes_select_focused_and_policy_tests(changed):
risk_map = json.loads((ROOT / "ci/test-risk-map.json").read_text())
surface = next(item for item in risk_map["contract_surfaces"] if changed in item["paths"])
expected_tests = {"tests/test_shard_pr_tests.py", "tests/test_ci_policy.py"}
assert expected_tests <= set(surface["required_tests"])
selected = select_pr_tests.select_tests(
[changed],
policy=json.loads((ROOT / "ci/pr-test-policy.json").read_text()),
risk_maps=[risk_map],
project_root=ROOT,
)
assert expected_tests <= set(selected)
def test_one_shard_assigns_all_selected_files():
default, durations = shard_pr_tests.load_durations(ROOT / "ci/gloo-test-durations.json")
selected = [*durations, "tests/test_ci_policy.py", "tests/test_shard_pr_tests.py"]
shards = shard_pr_tests.shard_tests(selected, default, durations, root=ROOT, shards=1)
assert len(shards) == 1
assert sorted(shards[0]) == sorted(selected)
@pytest.mark.parametrize("count", [1, 2, 3, 5])
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_n_shards_are_deterministic_and_assign_every_file_once(tmp_path, count, kind):
names = [f"tests/test_{index}.py" for index in range(4)]
_files(tmp_path, names, marked=names[:3])
durations = {names[0]: 100, names[1]: 70}
first = shard_pr_tests.shard_tests(names, 80, durations, root=tmp_path, shards=count, kind=kind)
second = shard_pr_tests.shard_tests(
list(reversed(names)), 80, durations, root=tmp_path, shards=count, kind=kind,
)
assert first == second
assert len(first) == count
assigned = [name for shard in first for name in shard]
assert sorted(assigned) == sorted(names)
assert len(assigned) == len(set(assigned))
assert shard_pr_tests.shard_tests([], 80, {}, root=tmp_path, shards=count, kind=kind) == tuple(
[] for _ in range(count)
)
if count == 5:
assert any(not shard for shard in first)
@pytest.mark.parametrize("count", [0, -1, True, 1.5])
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_invalid_shard_count_is_rejected(tmp_path, count, kind):
with pytest.raises(ValueError, match="positive integer"):
shard_pr_tests.shard_tests([], 80, {}, root=tmp_path, shards=count, kind=kind)
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_cli_respects_explicit_shard_count(tmp_path, monkeypatch, kind):
monkeypatch.chdir(tmp_path)
names = ["tests/test_a.py", "tests/test_b.py"]
_files(tmp_path, names, marked=names)
selected = tmp_path / "selected.txt"
selected.write_text("\n".join(names) + "\n")
table = _table(tmp_path / "durations.json")
output = tmp_path / "output"
argv = [
"shard_pr_tests.py", "--selected", str(selected), "--durations", str(table),
"--output-dir", str(output), "--shards", "3",
]
if kind == "core":
argv.extend(["--kind", kind])
monkeypatch.setattr(sys, "argv", argv)
assert shard_pr_tests.main() == 0
assert sorted(path.name for path in output.iterdir()) == [
f"selected-{kind}-{side}.txt" for side in range(3)
]
assert (output / f"selected-{kind}-2.txt").read_bytes() == b""
@pytest.mark.parametrize("kind", ["gloo", "core"])
def test_cli_duration_default_follows_kind(tmp_path, monkeypatch, kind):
monkeypatch.chdir(tmp_path)
names = [f"tests/test_{letter}.py" for letter in "abc"]
_files(tmp_path, names)
(tmp_path / "ci").mkdir()
_table(tmp_path / f"ci/{kind}-test-durations.json", files=dict(zip(names, [20, 10, 10])))
results = tmp_path / "test-results"
results.mkdir()
(results / "selected-tests.txt").write_text("\n".join(names) + "\n")
monkeypatch.setattr(sys, "argv", ["shard_pr_tests.py", "--kind", kind])
assert shard_pr_tests.main() == 0
assert (results / f"selected-{kind}-0.txt").read_text().splitlines() == names[:1]
assert (results / f"selected-{kind}-1.txt").read_text().splitlines() == names[1:]
def test_invalid_kind_is_rejected_by_api_and_cli(tmp_path, monkeypatch):
with pytest.raises(ValueError, match="kind must be"):
shard_pr_tests.shard_tests([], 80, {}, root=tmp_path, kind="other")
monkeypatch.setattr(sys, "argv", ["shard_pr_tests.py", "--kind", "other"])
with pytest.raises(SystemExit) as exc:
shard_pr_tests.main()
assert exc.value.code == 2