Files
OBLITERATUS/tests/test_release_partitions.py

249 lines
9.7 KiB
Python

"""Exact-node, revision, outcome, and coverage rejection contracts."""
from copy import deepcopy
import json
import shutil
from xml.etree import ElementTree as ET
from coverage import CoverageData
import pytest
from scripts import check_release_partitions as checker
from tests.release_partition_cases import NODES, VERSION, evidence, refresh, write_coverage, write_partition
@pytest.fixture
def bundle(tmp_path):
return evidence(tmp_path)
def core(bundle, index=0):
return bundle[1]["artifacts"] / f"release-core-py{VERSION}-head-{index}"
def mutate_status(directory, **changes):
path = directory / "status.json"
status = json.loads(path.read_text())
status.update(changes)
path.write_text(json.dumps(status))
def test_complete_inventory_accepts_empty_shards_and_all_executed_nodes(bundle):
result = checker.validate(**bundle[1])
assert result["nodes"] == NODES
assert result["node_counts"] == {"core": 1, "gloo": 1}
assert len(result["coverage"]) == 2
assert len(result["junit"]) == 14
@pytest.mark.parametrize("index", [4, 5, 6, 7])
def test_additional_release_gloo_shards_require_exact_execution_and_presence(bundle, index):
root, options = bundle
original = options["artifacts"] / f"release-gloo-py{VERSION}-head-0"
destination = options["artifacts"] / f"release-gloo-py{VERSION}-head-{index}"
for directory, shard, names in [
(original, 0, {"core": [], "gloo": []}),
(destination, index, {"core": [], "gloo": NODES["gloo"]}),
]:
shutil.rmtree(directory)
write_partition(directory, root / "obliteratus/example.py", kind="gloo", index=shard, names=names)
assert checker.validate(**options)["nodes"]["gloo"] == NODES["gloo"]
shutil.rmtree(destination)
with pytest.raises(ValueError, match="artifact set"):
checker.validate(**options)
@pytest.mark.parametrize("which", ["partition", "collection", "all"])
@pytest.mark.parametrize("field,value", [
("bytecode_policy", None), ("bytecode_policy", {}),
("bytecode_policy", {"name": "release-import-bytecode-v1", "sha256": "a" * 64,
"normal": True, "ignore_environment": True, "isolated": True}),
("bytecode_policy_path", "relative.pth"),
])
def test_startup_policy_is_required_even_if_every_manifest_agrees(bundle, which, field, value):
_, options = bundle
directories = ([core(bundle)] if which == "partition" else [options["expected"]] if which == "collection"
else [options["expected"], *options["artifacts"].iterdir()])
for directory in directories:
mutate_status(directory, **{field: value})
with pytest.raises(ValueError, match="bytecode policy"):
checker.validate(**options)
@pytest.mark.parametrize("job", ["core_result", "gloo_result"])
@pytest.mark.parametrize("result", ["failure", "cancelled", "skipped", "", None])
def test_upstream_failure_cannot_be_replaced_by_valid_artifacts(bundle, job, result):
with pytest.raises(ValueError, match="upstream"):
checker.validate(**{**bundle[1], job: result})
@pytest.mark.parametrize("change", ["missing", "extra", "duplicate"])
def test_exact_partition_set_required(bundle, change):
path = core(bundle)
if change == "missing":
shutil.rmtree(path)
else:
shutil.copytree(path, path.with_name(path.name + ("-copy" if change == "duplicate" else "9")))
with pytest.raises(ValueError, match="artifact set"):
checker.validate(**bundle[1])
@pytest.mark.parametrize("change", [
{"schema_version": True}, {"schema_version": 2}, {"candidate_sha": "b" * 40},
{"target_sha": "b" * 40}, {"target": "base"}, {"python_version": "9.9"},
{"kind": "gloo"}, {"shard": 1}, {"shard": False}, {"shard_count": 4},
{"extras": ["dev"]}, {"exit_code": 1}, {"exit_code": 2}, {"exit_code": 3},
{"exit_code": 4}, {"exit_code": True}, {"exit_code": None}, {"exit_code": 5},
])
def test_invalid_status_and_target_bindings_rejected(bundle, change):
mutate_status(core(bundle), **change)
with pytest.raises(ValueError):
checker.validate(**bundle[1])
@pytest.mark.parametrize("filename", ["status.json", "collection.json", "junit.xml", "coverage.data"])
@pytest.mark.parametrize("change", ["missing", "corrupt"])
def test_missing_or_corrupt_bound_files_rejected(bundle, filename, change):
path = core(bundle) / filename
if change == "missing":
path.unlink()
else:
path.write_text("not valid evidence")
with pytest.raises((ValueError, OSError)):
checker.validate(**bundle[1])
@pytest.mark.parametrize("change", ["corrupt", "empty", "line-only", "no-arcs"])
def test_invalid_database_rejected_even_with_matching_digest(bundle, change):
directory = core(bundle)
path = directory / "coverage.data"
path.unlink()
if change in {"corrupt", "empty"}:
path.write_bytes(b"broken sqlite" if change == "corrupt" else b"")
else:
data = CoverageData(basename=str(path))
if change == "line-only":
data.add_lines({"source.py": [1]})
else:
data.add_arcs({"source.py": []})
data.write()
refresh(directory)
with pytest.raises(ValueError):
checker.validate(**bundle[1])
def test_sidecars_are_bound_validated_and_combined(bundle):
directory = core(bundle)
child = directory / "coverage.data.host.12.random"
write_coverage(child, bundle[0] / "obliteratus/example.py")
with pytest.raises(ValueError, match="unbound"):
checker.validate(**bundle[1])
refresh(directory)
assert str(child.resolve()) in checker.validate(**bundle[1])["coverage"]
child.write_bytes(b"changed after status")
with pytest.raises(ValueError, match="altered"):
checker.validate(**bundle[1])
@pytest.mark.parametrize("change", ["missing", "duplicate", "substitute", "failed", "error", "no-property", "two-properties"])
def test_junit_exact_nodes_and_outcomes_cannot_be_spoofed(bundle, change):
directory = core(bundle)
path = directory / "junit.xml"
root = ET.parse(path).getroot()
case = root.find("testcase")
if change == "missing":
root.remove(case)
elif change == "duplicate":
root.append(deepcopy(case))
elif change in {"failed", "error"}:
ET.SubElement(case, "failure" if change == "failed" else "error")
else:
properties = case.find("properties")
prop = properties.find("property")
if change == "substitute":
prop.set("value", "tests/test_other.py::test_other")
elif change == "no-property":
properties.remove(prop)
else:
properties.append(deepcopy(prop))
ET.ElementTree(root).write(path)
refresh(directory)
with pytest.raises(ValueError):
checker.validate(**bundle[1])
def test_equal_global_counts_with_replaced_node_still_fail(bundle):
directory = core(bundle)
path = directory / "collection.json"
payload = json.loads(path.read_text())
payload["nodes"]["core"] = ["tests/test_other.py::test_other"]
path.write_text(json.dumps(payload))
tree = ET.parse(directory / "junit.xml")
tree.find(".//property").set("value", payload["nodes"]["core"][0])
tree.write(directory / "junit.xml")
refresh(directory)
with pytest.raises(ValueError, match="global node"):
checker.validate(**bundle[1])
def test_duplicate_across_partitions_fails_even_if_each_partition_is_self_consistent(bundle):
destination = core(bundle, 1)
shutil.rmtree(destination)
shutil.copytree(core(bundle), destination)
mutate_status(destination, shard=1)
with pytest.raises(ValueError, match="global node"):
checker.validate(**bundle[1])
def test_failed_independent_collection_is_not_an_empty_success(bundle):
mutate_status(bundle[1]["expected"], exit_code=2)
with pytest.raises(ValueError, match="independent"):
checker.validate(**bundle[1])
@pytest.mark.parametrize("wrapper", ["testsuites", "testsuite"])
def test_nested_slow_suite_cannot_bypass_serial_duration_budget(bundle, wrapper):
directory = core(bundle)
suite = ET.parse(directory / "junit.xml").getroot()
suite.set("time", "1200")
outer = ET.Element("testsuites")
nested = ET.SubElement(outer, wrapper, {"time": "0.25"})
nested.append(suite)
ET.ElementTree(outer).write(directory / "junit.xml")
refresh(directory)
with pytest.raises(ValueError, match="nested JUnit"):
checker.validate(**bundle[1])
def test_all_skipped_target_cannot_establish_success(bundle):
for directory in bundle[1]["artifacts"].iterdir():
tree = ET.parse(directory / "junit.xml")
for case in tree.iter("testcase"):
ET.SubElement(case, "skipped", {"message": "unavailable"})
tree.write(directory / "junit.xml")
refresh(directory)
with pytest.raises(ValueError, match="all selected testcases were skipped"):
checker.validate(**bundle[1])
def test_independent_inventory_must_be_nonempty(bundle):
directory = bundle[1]["expected"]
(directory / "collection.json").write_text(json.dumps({
"schema_version": 1, "nodes": {"core": [], "gloo": []}, "collection_skips": [],
}))
refresh(directory)
with pytest.raises(ValueError, match="independent target collection is empty"):
checker.validate(**bundle[1])
def test_cli_clears_stale_acceptance_on_rejection(bundle, tmp_path, monkeypatch):
output = tmp_path / "accepted.json"
output.write_text("old success")
args = ["check_release_partitions", "--output", str(output)]
for key, value in bundle[1].items():
args += ["--" + key.replace("_", "-"), str(value)]
mutate_status(core(bundle), exit_code=1)
monkeypatch.setattr("sys.argv", args)
assert checker.main() == 1
assert not output.exists()