| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- """Round-15: detached replay sidecar evidence completeness must fail-close."""
- from __future__ import annotations
- import copy
- import hashlib
- import json
- import sys
- import zipfile
- from pathlib import Path
- from unittest import mock
- from tests.unit.test_mse_r11_release_validation_bind import (
- _matching_sidecars,
- _validate,
- _write_identity_zip,
- )
- from tools import release_validation as rv
- from tools import replay_semantic as rs
- def _patch_actual_replay(**kwargs):
- """Patch the replay module actually imported by release_validation."""
- mod = sys.modules[rv.bind_independent_replay.__module__]
- return mock.patch.object(mod, "_run_actual_software_replay", **kwargs)
- def _setup(tmp_path: Path):
- review = tmp_path / "review.zip"
- firmware = tmp_path / "firmware.zip"
- _write_identity_zip(review)
- _write_identity_zip(firmware, extra="fw")
- return review, firmware, _matching_sidecars(tmp_path, review, firmware)
- def _mutate_and_validate(tmp_path: Path, mutate):
- review, firmware, sidecars = _setup(tmp_path)
- doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
- mutate(doc)
- sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
- return _validate(tmp_path, review, firmware, sidecars)
- def _assert_fail(result: dict, *codes: str) -> None:
- assert result["status"] != "pass"
- joined = " ".join(result.get("failures") or [])
- for code in codes:
- assert code in joined, f"missing {code} in {result.get('failures')}"
- def test_r15_happy_complete_sidecar_still_pass(tmp_path: Path):
- review, firmware, sidecars = _setup(tmp_path)
- result = _validate(tmp_path, review, firmware, sidecars)
- assert result["status"] == "pass", result.get("failures")
- assert result.get("failures") == []
- def test_r15_empty_unit_modules_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["unit"]["modules"] = []
- _assert_fail(_mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_modules_empty")
- def test_r15_partial_unit_modules_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["unit"]["modules"] = [
- {"module": "tests.unit.test_protocol", "status": "pass", "returncode": 0}
- ]
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_modules_incomplete"
- )
- def test_r15_wrong_unit_module_fail(tmp_path: Path):
- def mutate(doc):
- mods = doc["detached_software"]["unit"]["modules"]
- mods[0]["module"] = "tests.unit.test_not_a_required_module"
- _assert_fail(_mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_unknown")
- def test_r15_duplicate_unit_module_fail(tmp_path: Path):
- def mutate(doc):
- mods = doc["detached_software"]["unit"]["modules"]
- mods[1] = copy.deepcopy(mods[0])
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_duplicate"
- )
- def test_r15_unit_pass_nonzero_rc_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["unit"]["modules"][0]["returncode"] = 13
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_sidecar_unit_module_not_pass"
- )
- def test_r15_minimal_nested_pass_fail(tmp_path: Path):
- def mutate(doc):
- doc["package_content"] = {
- "status": "pass",
- "package_content_replay": "pass",
- "checked_files": 1,
- "volumes": doc["package_content"]["volumes"],
- }
- doc["detached_software"] = {
- "status": "pass",
- "git_mode": "detached",
- "layout": {"status": "pass"},
- "provenance": {"status": "pass"},
- "unit": {"status": "pass", "modules": []},
- "board": {"status": "pass"},
- }
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate),
- "replay_sidecar_unit_modules_empty",
- "replay_sidecar_layout_checks_missing",
- "replay_volume_manifest_file_count_mismatch",
- )
- def test_r15_board_status_only_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["board"] = {"status": "pass"}
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_sidecar_board_mode_not_canonical"
- )
- def test_r15_board_mode_bogus_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["board"]["mode"] = "bogus"
- doc["detached_software"]["board"]["returncode"] = 99
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate),
- "replay_sidecar_board_mode_not_canonical",
- "replay_sidecar_board_returncode_nonzero",
- )
- def test_r15_layout_checks_false_fail(tmp_path: Path):
- def mutate(doc):
- for key in doc["detached_software"]["layout"]["checks"]:
- doc["detached_software"]["layout"]["checks"][key] = False
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_sidecar_layout_required_check_false"
- )
- def test_r15_provenance_details_false_fail(tmp_path: Path):
- def mutate(doc):
- doc["detached_software"]["provenance"]["head_txt"] = False
- doc["detached_software"]["provenance"]["tree_manifest"] = False
- doc["detached_software"]["provenance"]["sanitized_snapshot_status"] = False
- doc["detached_software"]["provenance"]["raw_git_required"] = True
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate),
- "replay_sidecar_provenance_details_false",
- "replay_sidecar_provenance_raw_git_required",
- )
- def test_r15_checked_files_one_fail(tmp_path: Path):
- def mutate(doc):
- doc["package_content"]["checked_files"] = 1
- _assert_fail(
- _mutate_and_validate(tmp_path, mutate), "replay_volume_manifest_file_count_mismatch"
- )
- def _write_source_review_zip(path: Path) -> None:
- identity = {
- "schema": "prod2u-release-set-identity-v1",
- "delivery_commit": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
- "release_set_id": "bb" * 32,
- }
- members = {
- "meta/release-set-identity.json": json.dumps(identity).encode("utf-8"),
- "payload.txt": b"review-source",
- "CPLD/source/hello.py": b"x = 1\n",
- }
- files = [
- {
- "archive_path": name,
- "size": len(blob),
- "sha256": hashlib.sha256(blob).hexdigest(),
- }
- for name, blob in members.items()
- ]
- manifest = {
- "schema": "prod2u-volume-manifest-v1",
- "file_count": len(files),
- "uncompressed_bytes": sum(int(row["size"]) for row in files),
- "files": files,
- }
- with zipfile.ZipFile(path, "w") as zf:
- for name, blob in members.items():
- zf.writestr(name, blob)
- zf.writestr("CPLD/HANDOFF-METADATA/VOLUME-MANIFEST.json", json.dumps(manifest))
- def _matching_source_software() -> dict:
- return {
- "status": "pass",
- "git_mode": "detached",
- "layout": {
- "status": "pass",
- "checks": {
- "source_dir": True,
- "meta_dir": True,
- "evidence_dir": True,
- "meta_git_dir": True,
- "no_raw_git_under_source": True,
- "no_raw_git_under_cpld": True,
- "project_contract_present": True,
- },
- },
- "provenance": {
- "status": "pass",
- "head_txt": True,
- "tree_manifest": True,
- "sanitized_snapshot_status": True,
- "raw_git_required": False,
- },
- "unit": {
- "status": "pass",
- "modules": [
- {"module": "tests.unit.test_protocol", "status": "pass", "returncode": 0},
- {"module": "tests.unit.test_project_config", "status": "pass", "returncode": 0},
- {
- "module": "tests.unit.test_product_contract_coverage",
- "status": "pass",
- "returncode": 0,
- },
- ],
- },
- "board": {
- "status": "pass",
- "mode": "validate_detached_derived",
- "returncode": 0,
- "reference_regeneration_replay": "not_packaged_by_policy",
- },
- }
- def test_r15_dummy_zip_skips_independent_rerun(tmp_path: Path):
- review, firmware, sidecars = _setup(tmp_path)
- assert rs.review_zip_has_source_tree(review) is False
- result = _validate(tmp_path, review, firmware, sidecars)
- assert result["status"] == "pass", result.get("failures")
- def test_r15_independent_rerun_matches_sidecar(tmp_path: Path):
- review = tmp_path / "review.zip"
- firmware = tmp_path / "firmware.zip"
- _write_source_review_zip(review)
- _write_identity_zip(firmware, extra="fw")
- sidecars = _matching_sidecars(tmp_path, review, firmware)
- doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
- actual = _matching_source_software()
- claimed = rs.semantic_object(
- content=doc["package_content"],
- software=doc["detached_software"],
- inventory=rs.verify_exact_inventory(review)[1],
- )
- with _patch_actual_replay(return_value=actual):
- result = _validate(tmp_path, review, firmware, sidecars)
- assert result["status"] == "pass", result.get("failures")
- actual_sem = rs.semantic_object(
- content=doc["package_content"],
- software=actual,
- inventory=rs.verify_exact_inventory(review)[1],
- )
- assert claimed["unit_modules"] == actual_sem["unit_modules"]
- def test_r15_independent_rerun_unit_mismatch_fail(tmp_path: Path):
- review = tmp_path / "review.zip"
- firmware = tmp_path / "firmware.zip"
- _write_source_review_zip(review)
- _write_identity_zip(firmware, extra="fw")
- sidecars = _matching_sidecars(tmp_path, review, firmware)
- doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
- sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
- actual = _matching_source_software()
- actual["unit"]["modules"][0]["module"] = "tests.unit.test_not_replayed"
- with _patch_actual_replay(return_value=actual):
- result = _validate(tmp_path, review, firmware, sidecars)
- _assert_fail(result, "replay_semantic_unit_mismatch")
- def test_r15_mkdtemp_then_extract_does_not_file_exists(tmp_path: Path):
- """Regression: mkdtemp() + mkdir(exist_ok=False) on the same path."""
- review = tmp_path / "review.zip"
- _write_source_review_zip(review)
- result = rs._run_actual_software_replay(review)
- assert isinstance(result, dict)
- assert "status" in result
- assert "layout" in result
- def test_r15_independent_rerun_error_fail(tmp_path: Path):
- review = tmp_path / "review.zip"
- firmware = tmp_path / "firmware.zip"
- _write_source_review_zip(review)
- _write_identity_zip(firmware, extra="fw")
- sidecars = _matching_sidecars(tmp_path, review, firmware)
- doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
- sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
- with _patch_actual_replay(side_effect=RuntimeError("extract")):
- result = _validate(tmp_path, review, firmware, sidecars)
- _assert_fail(result, "replay_actual_rerun_error")
- def test_r15_semantic_checked_files_mismatch_fail(tmp_path: Path):
- review = tmp_path / "review.zip"
- firmware = tmp_path / "firmware.zip"
- _write_source_review_zip(review)
- _write_identity_zip(firmware, extra="fw")
- sidecars = _matching_sidecars(tmp_path, review, firmware)
- doc = json.loads(sidecars["review_detached_replay_json"].read_text(encoding="utf-8"))
- doc["package_content"]["checked_files"] = 1
- sidecars["review_detached_replay_json"].write_text(json.dumps(doc), encoding="utf-8")
- with _patch_actual_replay(return_value=_matching_source_software()):
- result = _validate(tmp_path, review, firmware, sidecars)
- _assert_fail(
- result,
- "replay_volume_manifest_file_count_mismatch",
- )
|