976 lines
37 KiB
Python
976 lines
37 KiB
Python
from __future__ import annotations
|
|
|
|
from contextlib import redirect_stdout
|
|
from dataclasses import dataclass, replace
|
|
from decimal import Decimal
|
|
from io import StringIO
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from dx_productivity.part_replace.certification import CertificationStore, interface_fingerprint
|
|
from dx_productivity.part_replace.cli import main as cli_main
|
|
from dx_productivity.part_replace.controller import BatchController
|
|
from dx_productivity.part_replace.models import (
|
|
ComponentSnapshot,
|
|
DesignSnapshot,
|
|
ExternalChecks,
|
|
MappingRule,
|
|
PinSnapshot,
|
|
ProjectRef,
|
|
ProjectState,
|
|
Scope,
|
|
UnitSnapshot,
|
|
)
|
|
from dx_productivity.part_replace.protocol import (
|
|
BridgeCommand,
|
|
atomic_write_text,
|
|
load_snapshot,
|
|
load_status,
|
|
snapshot_fingerprint,
|
|
write_snapshot,
|
|
)
|
|
from dx_productivity.part_replace.run_store import TreeFile, TreeManifest, verify_tree
|
|
from dx_productivity.part_replace.verification import eligible_logical_keys, verify_replacement
|
|
|
|
|
|
RULE = MappingRule("OLD-1", "NEW-1")
|
|
LIBRARY_IDENTITY = "lib-r42"
|
|
_COMMAND_FIELDS = {
|
|
"action",
|
|
"command_id",
|
|
"expected_count",
|
|
"expected_fingerprint",
|
|
"expected_logical_keys",
|
|
"format_version",
|
|
"job_id",
|
|
"new_part_number",
|
|
"old_part_number",
|
|
"project_id",
|
|
"scope",
|
|
}
|
|
|
|
|
|
class BridgeStatusEvidenceError(ValueError):
|
|
"""The operator could not correlate durable bridge status evidence."""
|
|
|
|
|
|
def _pin_coordinates(
|
|
origin_x: Decimal,
|
|
origin_y: Decimal,
|
|
rotation: int,
|
|
mirrored: bool,
|
|
local_x: Decimal,
|
|
local_y: Decimal,
|
|
) -> tuple[Decimal, Decimal]:
|
|
transformed_x = -local_x if mirrored else local_x
|
|
transformed_y = local_y
|
|
if rotation == 0:
|
|
delta_x, delta_y = transformed_x, transformed_y
|
|
elif rotation == 90:
|
|
delta_x, delta_y = -transformed_y, transformed_x
|
|
elif rotation == 180:
|
|
delta_x, delta_y = -transformed_x, -transformed_y
|
|
elif rotation == 270:
|
|
delta_x, delta_y = transformed_y, -transformed_x
|
|
else: # pragma: no cover - fixtures use only DxDesigner quarter turns
|
|
raise ValueError(f"unsupported fixture rotation: {rotation}")
|
|
return origin_x + delta_x, origin_y + delta_y
|
|
|
|
|
|
def _component(
|
|
index: int,
|
|
part_number: str,
|
|
*,
|
|
rotation: int = 0,
|
|
mirrored: bool = False,
|
|
) -> ComponentSnapshot:
|
|
origin_x = Decimal(index * 10)
|
|
origin_y = Decimal(index * 20)
|
|
symbol_origin_x, symbol_origin_y = _pin_coordinates(
|
|
origin_x,
|
|
origin_y,
|
|
rotation,
|
|
mirrored,
|
|
Decimal("-0.5"),
|
|
Decimal("-0.5"),
|
|
)
|
|
pins = []
|
|
for number, name, local_x, local_y, net in (
|
|
("1", "IN", Decimal("0.25"), Decimal("0"), f"N{index}-IN"),
|
|
("2", "OUT", Decimal("0.50"), Decimal("0"), f"N{index}-OUT"),
|
|
):
|
|
x, y = _pin_coordinates(origin_x, origin_y, rotation, mirrored, local_x, local_y)
|
|
pins.append(PinSnapshot(number, name, x, y, net))
|
|
return ComponentSnapshot(
|
|
logical_key=f"/MAIN|U{index}",
|
|
refdes=f"U{index}",
|
|
part_number=part_number,
|
|
attributes=(("manufacturer", "Acme"),),
|
|
units=(
|
|
UnitSnapshot(
|
|
unit_id="A",
|
|
sheet="/MAIN",
|
|
symbol="LIB:NEW" if part_number != RULE.old_part_number else "LIB:OLD",
|
|
origin_x=symbol_origin_x,
|
|
origin_y=symbol_origin_y,
|
|
x=origin_x,
|
|
y=origin_y,
|
|
rotation=rotation,
|
|
mirrored=mirrored,
|
|
pins=tuple(pins),
|
|
),
|
|
),
|
|
)
|
|
|
|
|
|
def _fingerprinted(snapshot: DesignSnapshot) -> DesignSnapshot:
|
|
return replace(snapshot, fingerprint=snapshot_fingerprint(snapshot))
|
|
|
|
|
|
def _snapshot(project_id: str, marker: Path, part_number: str, count: int) -> DesignSnapshot:
|
|
placements = ((0, False), (90, True), (270, False))
|
|
snapshot = DesignSnapshot(
|
|
format_version=2,
|
|
project_id=project_id,
|
|
project_path=str(marker.resolve()),
|
|
fingerprint="pending",
|
|
components=(_component(0, "KEEP-1"),)
|
|
+ tuple(
|
|
_component(
|
|
index,
|
|
part_number,
|
|
rotation=placements[index - 1][0],
|
|
mirrored=placements[index - 1][1],
|
|
)
|
|
for index in range(1, count + 1)
|
|
),
|
|
)
|
|
return _fingerprinted(snapshot)
|
|
|
|
|
|
def _json_object(path: Path) -> dict[str, Any]:
|
|
def reject_duplicates(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
|
|
result: dict[str, Any] = {}
|
|
for key, value in pairs:
|
|
if key in result:
|
|
raise AssertionError(f"published command repeats {key!r}")
|
|
result[key] = value
|
|
return result
|
|
|
|
data = json.loads(path.read_text(encoding="utf-8"), object_pairs_hook=reject_duplicates)
|
|
assert isinstance(data, dict)
|
|
return data
|
|
|
|
|
|
def _load_command(path: Path) -> BridgeCommand:
|
|
data = _json_object(path)
|
|
assert set(data) == _COMMAND_FIELDS
|
|
return BridgeCommand(
|
|
format_version=data["format_version"],
|
|
command_id=data["command_id"],
|
|
action=data["action"],
|
|
job_id=data["job_id"],
|
|
project_id=data["project_id"],
|
|
scope=Scope(data["scope"]),
|
|
old_part_number=data["old_part_number"],
|
|
new_part_number=data["new_part_number"],
|
|
expected_logical_keys=tuple(data["expected_logical_keys"]),
|
|
expected_count=data["expected_count"],
|
|
expected_fingerprint=data["expected_fingerprint"],
|
|
)
|
|
|
|
|
|
def _invoke_cli(*arguments: str) -> tuple[int, dict[str, Any]]:
|
|
output = StringIO()
|
|
with redirect_stdout(output):
|
|
exit_code = cli_main(["--json", *arguments])
|
|
payload = json.loads(output.getvalue())
|
|
assert isinstance(payload, dict)
|
|
return exit_code, payload
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class JobInputs:
|
|
mapping: Path
|
|
projects: Path
|
|
certifications: Path
|
|
markers: dict[str, Path]
|
|
|
|
@classmethod
|
|
def create(cls, root: Path) -> JobInputs:
|
|
root.mkdir(parents=True)
|
|
markers: dict[str, Path] = {}
|
|
for name in ("p1", "p2"):
|
|
project_root = root / "projects" / name
|
|
project_root.mkdir(parents=True)
|
|
marker = project_root / f"{name}.prj"
|
|
marker.write_text(f"DxDesigner project {name}\n", encoding="utf-8")
|
|
(project_root / "design.sch").write_text("native design: OLD-1\n", encoding="utf-8")
|
|
markers[name] = marker
|
|
|
|
mapping = root / "mapping.csv"
|
|
mapping.write_text(
|
|
"old_part_number,new_part_number\nOLD-1,NEW-1\n",
|
|
encoding="utf-8",
|
|
)
|
|
projects = root / "projects.txt"
|
|
projects.write_text(
|
|
"".join(f"{markers[name].resolve()}\n" for name in ("p1", "p2")),
|
|
encoding="utf-8",
|
|
)
|
|
certifications = root / "certifications.json"
|
|
cls._record_certification(root, certifications)
|
|
return cls(mapping, projects, certifications, markers)
|
|
|
|
@staticmethod
|
|
def _record_certification(root: Path, certifications: Path) -> None:
|
|
trial_root = root / "trial"
|
|
trial_root.mkdir()
|
|
marker = trial_root / "trial.prj"
|
|
marker.write_text("representative trial\n", encoding="utf-8")
|
|
before = _snapshot("trial", marker, RULE.old_part_number, 1)
|
|
after = _snapshot("trial", marker, RULE.new_part_number, 1)
|
|
before_path = trial_root / "before.json"
|
|
after_path = trial_root / "after.json"
|
|
write_snapshot(before_path, before)
|
|
write_snapshot(after_path, after)
|
|
evidence_paths = [before_path, after_path]
|
|
for name in ("packager", "erc", "netlist"):
|
|
path = trial_root / f"{name}.txt"
|
|
path.write_text("PASS\n", encoding="utf-8")
|
|
evidence_paths.append(path)
|
|
expected_keys = ("/MAIN|U1",)
|
|
verification = verify_replacement(before, after, RULE, expected_keys)
|
|
CertificationStore(
|
|
certifications,
|
|
before=before,
|
|
after=after,
|
|
verification=verification,
|
|
evidence_paths=tuple(str(path.resolve()) for path in evidence_paths),
|
|
).record(
|
|
RULE,
|
|
interface_fingerprint(after, expected_keys),
|
|
LIBRARY_IDENTITY,
|
|
ExternalChecks(True, True, True),
|
|
)
|
|
|
|
|
|
class MockDxDesigner:
|
|
def __init__(
|
|
self,
|
|
run_dir: Path,
|
|
project: ProjectRef,
|
|
marker: Path,
|
|
hit_count: int,
|
|
job_id: str,
|
|
) -> None:
|
|
self.run_dir = Path(run_dir)
|
|
self.project = project
|
|
self.marker = Path(marker)
|
|
self.hit_count = hit_count
|
|
self.job_id = job_id
|
|
self.mailbox = self.run_dir / "mailbox" / project.project_id
|
|
self.mailbox.mkdir(parents=True, exist_ok=True)
|
|
self.current_snapshot = self.mailbox / "inventory.json"
|
|
self.persisted_snapshot = self.mailbox / "persisted.json"
|
|
self.native_project = self.marker.parent / "design.sch"
|
|
self._current = _snapshot(project.project_id, self.marker, RULE.old_part_number, hit_count)
|
|
write_snapshot(self.native_project, self._current)
|
|
self._before_apply: DesignSnapshot | None = None
|
|
self._active_command: BridgeCommand | None = None
|
|
self._apply_command: BridgeCommand | None = None
|
|
self.is_open = True
|
|
self.saved = False
|
|
self.apply_groups: list[tuple[str, str, int]] = []
|
|
|
|
@classmethod
|
|
def resume(cls, run_dir: Path, project: ProjectRef, hit_count: int, job_id: str) -> MockDxDesigner:
|
|
instance = cls.__new__(cls)
|
|
instance.run_dir = Path(run_dir)
|
|
instance.project = project
|
|
instance.marker = Path(project.project_path)
|
|
instance.hit_count = hit_count
|
|
instance.job_id = job_id
|
|
instance.mailbox = instance.run_dir / "mailbox" / project.project_id
|
|
instance.current_snapshot = instance.mailbox / "inventory.json"
|
|
instance.persisted_snapshot = instance.mailbox / "persisted.json"
|
|
instance.native_project = instance.marker.parent / "design.sch"
|
|
available = next(
|
|
path
|
|
for path in (
|
|
instance.persisted_snapshot,
|
|
instance.mailbox / "snapshot.json",
|
|
instance.current_snapshot,
|
|
instance.native_project,
|
|
)
|
|
if path.exists()
|
|
)
|
|
instance._current = load_snapshot(available)
|
|
instance._before_apply = None
|
|
command_path = instance.mailbox / "command.json"
|
|
instance._active_command = _load_command(command_path) if command_path.exists() else None
|
|
instance._apply_command = (
|
|
instance._active_command
|
|
if instance._active_command is not None
|
|
and instance._active_command.action == "replace_part"
|
|
else None
|
|
)
|
|
instance.is_open = True
|
|
instance.saved = instance.persisted_snapshot.exists()
|
|
instance.apply_groups = []
|
|
return instance
|
|
|
|
@property
|
|
def project_id(self) -> str:
|
|
return self.project.project_id
|
|
|
|
def inventory(self) -> Path:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action == "inventory"
|
|
assert command.job_id == self.job_id
|
|
assert command.project_id == self.project_id
|
|
assert command.expected_count == 0
|
|
assert command.expected_logical_keys == ()
|
|
assert self.is_open
|
|
self._active_command = command
|
|
write_snapshot(self.current_snapshot, self._current)
|
|
observed = load_snapshot(self.current_snapshot)
|
|
assert observed == self._current
|
|
assert observed.fingerprint == snapshot_fingerprint(observed)
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, observed)
|
|
self._write_status("inventory_exported", snapshot_path=snapshot_path)
|
|
return snapshot_path
|
|
|
|
def close(self) -> Path:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action in {"close_project", "close_without_save"}
|
|
assert command.job_id == self.job_id
|
|
assert command.project_id == self.project_id
|
|
assert command.expected_count == 0
|
|
assert command.expected_logical_keys == ()
|
|
assert self.is_open
|
|
if command.action == "close_project":
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
self._active_command = command
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, self._current)
|
|
self._write_status("project_closed", snapshot_path=snapshot_path)
|
|
self.is_open = False
|
|
if command.action == "close_without_save":
|
|
self._current = load_snapshot(self.native_project)
|
|
return snapshot_path
|
|
|
|
def open(self) -> Path:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action == "open_project"
|
|
assert command.job_id == self.job_id
|
|
assert command.project_id == self.project_id
|
|
assert command.expected_count == 0
|
|
assert command.expected_logical_keys == ()
|
|
assert not self.is_open
|
|
self._current = load_snapshot(self.native_project)
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
self._active_command = command
|
|
self.is_open = True
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, self._current)
|
|
self._write_status("project_opened", snapshot_path=snapshot_path)
|
|
return snapshot_path
|
|
|
|
def consume_command(self) -> BridgeCommand:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
selected = eligible_logical_keys(self._current, RULE)
|
|
assert command.format_version == 1
|
|
assert command.command_id
|
|
assert command.job_id == self.job_id
|
|
assert command.project_id == self.project_id
|
|
assert command.action == "replace_part"
|
|
assert command.scope is Scope.FULL_PROJECT
|
|
assert (command.old_part_number, command.new_part_number) == (
|
|
RULE.old_part_number,
|
|
RULE.new_part_number,
|
|
)
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
assert command.expected_logical_keys == selected
|
|
assert command.expected_count == len(selected) == self.hit_count
|
|
self._active_command = command
|
|
self._apply_command = command
|
|
self.apply_groups.append((self.marker.stem, command.old_part_number, command.expected_count))
|
|
self._write_status("applying", snapshot_path=None)
|
|
return command
|
|
|
|
def replace(self, rule: MappingRule, fault: str | None = None) -> None:
|
|
command = self._active_command
|
|
assert command is not None
|
|
assert rule == RULE
|
|
if fault not in {None, "wrong_target"}:
|
|
raise ValueError(f"unsupported isolated fault: {fault}")
|
|
self._before_apply = self._current
|
|
expected = set(command.expected_logical_keys)
|
|
changed: list[ComponentSnapshot] = []
|
|
for component in self._current.components:
|
|
if component.logical_key not in expected:
|
|
changed.append(component)
|
|
continue
|
|
changed.append(
|
|
replace(
|
|
component,
|
|
part_number=rule.new_part_number,
|
|
units=tuple(replace(unit, symbol="LIB:NEW") for unit in component.units),
|
|
)
|
|
)
|
|
if fault == "wrong_target":
|
|
first = changed.index(next(component for component in changed if component.logical_key in expected))
|
|
changed[first] = replace(changed[first], part_number="WRONG-1")
|
|
self._current = _fingerprinted(replace(self._current, components=tuple(changed)))
|
|
after_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(after_path, self._current)
|
|
self._write_status("applied", snapshot_path=after_path)
|
|
|
|
def undo(self) -> None:
|
|
assert self._before_apply is not None
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action == "undo"
|
|
assert self._apply_command is not None
|
|
assert command.command_id == self._apply_command.command_id
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
self._active_command = command
|
|
self._current = self._before_apply
|
|
undo_path = self.mailbox / "undo.json"
|
|
write_snapshot(undo_path, self._current)
|
|
self.current_snapshot = undo_path
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, self._current)
|
|
self._write_status(
|
|
"undo_verified",
|
|
snapshot_path=snapshot_path,
|
|
status_action="replace_part",
|
|
)
|
|
|
|
def save(self) -> None:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action == "save"
|
|
assert self._apply_command is not None
|
|
assert command.command_id == self._apply_command.command_id
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
self._active_command = command
|
|
write_snapshot(self.native_project, self._current)
|
|
persisted = load_snapshot(self.native_project)
|
|
write_snapshot(self.persisted_snapshot, persisted)
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, persisted)
|
|
self.saved = True
|
|
self._write_status(
|
|
"saved",
|
|
snapshot_path=snapshot_path,
|
|
status_action="replace_part",
|
|
)
|
|
|
|
def external_check(self, check: str, *, change_design: bool = False) -> None:
|
|
command = _load_command(self.mailbox / "command.json")
|
|
assert command.action == check
|
|
assert check in {"packager", "erc", "netlist"}
|
|
assert command.expected_count == 0
|
|
assert command.expected_logical_keys == ()
|
|
assert command.expected_fingerprint == self._current.fingerprint
|
|
self._active_command = command
|
|
if change_design:
|
|
first = self._current.components[0]
|
|
self._current = _fingerprinted(
|
|
replace(
|
|
self._current,
|
|
components=(replace(first, part_number="CHECK-CHANGED"),)
|
|
+ self._current.components[1:],
|
|
)
|
|
)
|
|
snapshot_path = self.mailbox / "snapshot.json"
|
|
write_snapshot(snapshot_path, self._current)
|
|
self._write_status("external_check_passed", snapshot_path=snapshot_path)
|
|
|
|
def _write_status(
|
|
self,
|
|
state: str,
|
|
*,
|
|
snapshot_path: Path | None,
|
|
status_action: str | None = None,
|
|
) -> None:
|
|
command = self._active_command
|
|
assert command is not None
|
|
path = self.mailbox / "status.json"
|
|
data = {
|
|
"command_id": command.command_id,
|
|
"details": [
|
|
["action", status_action or command.action],
|
|
["expected_count", str(command.expected_count)],
|
|
["job_id", command.job_id],
|
|
],
|
|
"error_code": None,
|
|
"error_message": None,
|
|
"format_version": 1,
|
|
"project_id": self.project_id,
|
|
"snapshot_path": str(snapshot_path.resolve()) if snapshot_path is not None else None,
|
|
"state": state,
|
|
}
|
|
atomic_write_text(
|
|
path,
|
|
json.dumps(data, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n",
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class HarnessResult:
|
|
run_dir: Path
|
|
project_states: dict[str, str]
|
|
apply_groups: list[tuple[str, str, int]]
|
|
backups_verified: bool
|
|
saved_projects: set[str]
|
|
undo_verified: bool
|
|
exit_codes: tuple[tuple[str, int], ...]
|
|
report_paths: tuple[Path, ...]
|
|
projects: dict[str, MockDxDesigner]
|
|
|
|
|
|
class MockBatchHarness:
|
|
def __init__(self, root: Path, inputs: JobInputs) -> None:
|
|
self.root = Path(root)
|
|
self.inputs = inputs
|
|
self.run_dir = self.root / "run"
|
|
self.exit_codes: list[tuple[str, int]] = []
|
|
code, created = self._cli(
|
|
"create",
|
|
str(self.run_dir),
|
|
"--mapping",
|
|
str(inputs.mapping),
|
|
"--projects",
|
|
str(inputs.projects),
|
|
"--certifications",
|
|
str(inputs.certifications),
|
|
"--suffix",
|
|
".prj",
|
|
"--library-identity",
|
|
LIBRARY_IDENTITY,
|
|
)
|
|
assert code == 0
|
|
self.job_id = created["job_id"]
|
|
refs = {
|
|
project["display_name"]: ProjectRef(
|
|
project["project_id"],
|
|
project["project_path"],
|
|
project["display_name"],
|
|
)
|
|
for project in created["projects"]
|
|
}
|
|
self.projects = {
|
|
"p1": MockDxDesigner(self.run_dir, refs["p1"], inputs.markers["p1"], 3, self.job_id),
|
|
"p2": MockDxDesigner(self.run_dir, refs["p2"], inputs.markers["p2"], 1, self.job_id),
|
|
}
|
|
self.next_action: tuple[str, str] | None = None
|
|
self.project_states: dict[str, str] = created["states"]
|
|
|
|
def _cli(self, *arguments: str) -> tuple[int, dict[str, Any]]:
|
|
code, payload = _invoke_cli(*arguments)
|
|
self.exit_codes.append((arguments[0], code))
|
|
return code, payload
|
|
|
|
def _ingest_all(self) -> None:
|
|
for mock in self.projects.values():
|
|
code, _ = self._cli("prepare-inventory", str(self.run_dir), mock.project_id)
|
|
assert code == 0
|
|
mock.inventory()
|
|
code, _ = self._cli("ingest", str(self.run_dir), mock.project_id)
|
|
assert code == 0
|
|
|
|
def _next(self) -> tuple[str, dict[str, Any]] | None:
|
|
code, payload = self._cli("next", str(self.run_dir))
|
|
if payload["complete"]:
|
|
assert code == 0
|
|
return None
|
|
assert code == 4
|
|
project = payload["project"]
|
|
return project["display_name"], payload
|
|
|
|
def _consume_status(
|
|
self,
|
|
name: str,
|
|
state: str,
|
|
snapshot_name: str | None,
|
|
) -> Path | None:
|
|
mock = self.projects[name]
|
|
|
|
def require(condition: bool, message: str) -> None:
|
|
if not condition:
|
|
raise BridgeStatusEvidenceError(f"bridge_status: {message}")
|
|
|
|
try:
|
|
command = _load_command(mock.mailbox / "command.json")
|
|
status = load_status(mock.mailbox / "status.json")
|
|
require(command.format_version == 1, "command format version does not match")
|
|
require(bool(command.command_id), "command ID is empty")
|
|
require(command.job_id == self.job_id, "command job ID does not match")
|
|
require(command.project_id == mock.project_id, "command project ID does not match")
|
|
status_action = (
|
|
"replace_part" if command.action in {"undo", "save"} else command.action
|
|
)
|
|
expected_count = (
|
|
mock.hit_count
|
|
if command.action in {"replace_part", "undo", "save"}
|
|
else 0
|
|
)
|
|
require(command.expected_count == expected_count, "command expected count does not match")
|
|
require(status.format_version == 1, "status format version does not match")
|
|
require(status.command_id == command.command_id, "status command ID does not match")
|
|
require(status.project_id == command.project_id, "status project ID does not match")
|
|
require(status.state == state, "status state does not match")
|
|
require(status.error_code is None, "status contains an error code")
|
|
require(status.error_message is None, "status contains an error message")
|
|
require(
|
|
status.details
|
|
== (
|
|
("action", status_action),
|
|
("expected_count", str(command.expected_count)),
|
|
("job_id", command.job_id),
|
|
),
|
|
"status details do not match the command",
|
|
)
|
|
|
|
expected_path = mock.mailbox / snapshot_name if snapshot_name is not None else None
|
|
expected_status_path = str(expected_path.resolve()) if expected_path is not None else None
|
|
require(status.snapshot_path == expected_status_path, "status snapshot path does not match")
|
|
if expected_path is None:
|
|
return None
|
|
require(expected_path.is_file(), "status snapshot is missing")
|
|
snapshot = load_snapshot(expected_path)
|
|
require(snapshot.project_id == mock.project_id, "snapshot project ID does not match")
|
|
require(
|
|
Path(snapshot.project_path).resolve(strict=False) == mock.marker.resolve(strict=False),
|
|
"snapshot project path does not match",
|
|
)
|
|
require(
|
|
snapshot.fingerprint == snapshot_fingerprint(snapshot),
|
|
"snapshot fingerprint does not match its contents",
|
|
)
|
|
return expected_path
|
|
except BridgeStatusEvidenceError:
|
|
raise
|
|
except (AssertionError, KeyError, OSError, TypeError, UnicodeError, ValueError) as error:
|
|
raise BridgeStatusEvidenceError(
|
|
f"bridge_status: invalid evidence for {mock.project_id} in state {state}"
|
|
) from error
|
|
|
|
def _apply(self, name: str, fault: str | None = None) -> tuple[int, dict[str, Any]]:
|
|
mock = self.projects[name]
|
|
code, _ = self._cli("prepare", str(self.run_dir), mock.project_id, "0")
|
|
assert code == 4
|
|
mock.consume_command()
|
|
mock.replace(RULE, fault=fault)
|
|
return self._cli("verify", str(self.run_dir), mock.project_id, "0")
|
|
|
|
def _close_and_open(self, name: str) -> None:
|
|
mock = self.projects[name]
|
|
code, command = self._cli(
|
|
"prepare-close",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert command["action"] == "close_project"
|
|
mock.close()
|
|
code, closed = self._cli(
|
|
"confirm-close",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert closed["state"] == ProjectState.APPLY_PENDING.value
|
|
|
|
code, command = self._cli(
|
|
"prepare-open",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert command["action"] == "open_project"
|
|
mock.open()
|
|
code, opened = self._cli(
|
|
"confirm-open",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert opened["state"] == ProjectState.APPLY_PENDING.value
|
|
|
|
def _save(self, name: str) -> None:
|
|
mock = self.projects[name]
|
|
code, command = self._cli(
|
|
"prepare-save",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert command["action"] == "save"
|
|
mock.save()
|
|
code, payload = self._cli(
|
|
"confirm-saved",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert payload["state"] == ProjectState.AWAITING_EXTERNAL_CHECKS.value
|
|
for index, check in enumerate(("packager", "erc", "netlist")):
|
|
code, command = self._cli(
|
|
"prepare-check",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
check,
|
|
)
|
|
assert code == 4
|
|
assert command["action"] == check
|
|
mock.external_check(check)
|
|
code, payload = self._cli(
|
|
"confirm-check",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
check,
|
|
)
|
|
assert code == (0 if index == 2 else 4)
|
|
assert payload["state"] == ProjectState.VERIFIED_SAVED.value
|
|
|
|
def run_successfully(self) -> HarnessResult:
|
|
self._ingest_all()
|
|
while True:
|
|
next_item = self._next()
|
|
if next_item is None:
|
|
break
|
|
name, payload = next_item
|
|
assert payload["action"] == "prepare-close"
|
|
self._close_and_open(name)
|
|
code, verified = self._apply(name)
|
|
assert code == 0 and verified["passed"] is True
|
|
self._save(name)
|
|
return self._result(undo_verified=False)
|
|
|
|
def run_with_fault(self, project_name: str, fault: str) -> HarnessResult:
|
|
self._ingest_all()
|
|
next_item = self._next()
|
|
assert next_item is not None and next_item[0] == project_name
|
|
assert next_item[1]["action"] == "prepare-close"
|
|
self._close_and_open(project_name)
|
|
code, verified = self._apply(project_name, fault=fault)
|
|
assert code == 3 and verified["passed"] is False
|
|
mock = self.projects[project_name]
|
|
code, command = self._cli(
|
|
"prepare-undo",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert command["action"] == "undo"
|
|
mock.undo()
|
|
code, _ = self._cli(
|
|
"confirm-undo",
|
|
str(self.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
undo_verified = True
|
|
return self._result(undo_verified=undo_verified)
|
|
|
|
def crash_during_apply(self, project_name: str) -> Path:
|
|
self._ingest_all()
|
|
while True:
|
|
next_item = self._next()
|
|
assert next_item is not None
|
|
name, payload = next_item
|
|
assert payload["action"] == "prepare-close"
|
|
mock = self.projects[name]
|
|
self._close_and_open(name)
|
|
code, _ = self._cli("prepare", str(self.run_dir), mock.project_id, "0")
|
|
assert code == 4
|
|
mock.consume_command()
|
|
if name == project_name:
|
|
return self.run_dir
|
|
mock.replace(RULE)
|
|
code, verified = self._cli("verify", str(self.run_dir), mock.project_id, "0")
|
|
assert code == 0 and verified["passed"] is True
|
|
self._save(name)
|
|
|
|
@classmethod
|
|
def resume(cls, run_dir: Path) -> MockBatchHarness:
|
|
controller = BatchController.open(run_dir)
|
|
harness = cls.__new__(cls)
|
|
harness.root = Path(run_dir).parent
|
|
harness.run_dir = Path(run_dir)
|
|
harness.exit_codes = []
|
|
harness.job_id = controller.job_id
|
|
harness.inputs = JobInputs(
|
|
harness.run_dir / "inputs" / "mapping.csv",
|
|
harness.run_dir / "inputs" / "projects.txt",
|
|
controller.certification_path,
|
|
{project.display_name: Path(project.project_path) for project in controller.projects},
|
|
)
|
|
counts = {"p1": 3, "p2": 1}
|
|
harness.projects = {
|
|
project.display_name: MockDxDesigner.resume(
|
|
harness.run_dir,
|
|
project,
|
|
counts[project.display_name],
|
|
harness.job_id,
|
|
)
|
|
for project in controller.projects
|
|
}
|
|
code, next_payload = harness._cli("next", str(harness.run_dir))
|
|
assert code == 4
|
|
assert next_payload["action"] == "reconcile"
|
|
next_name = next_payload["project"]["display_name"]
|
|
harness.next_action = (next_name, next_payload["action"])
|
|
harness._consume_status(next_name, "applying", None)
|
|
mock = harness.projects[next_name]
|
|
code, blocked = harness._cli(
|
|
"reconcile",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 3
|
|
assert blocked["error"]["code"] == "recovery_evidence_missing"
|
|
|
|
code, close_command = harness._cli(
|
|
"prepare-close",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert close_command["action"] == "close_without_save"
|
|
mock.close()
|
|
code, closed = harness._cli(
|
|
"confirm-close",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert closed["state"] == ProjectState.BLOCKED.value
|
|
|
|
code, reconciled = harness._cli(
|
|
"reconcile",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert reconciled["state"] == ProjectState.INVENTORY_PENDING.value
|
|
|
|
code, open_command = harness._cli(
|
|
"prepare-open",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 4
|
|
assert open_command["action"] == "open_project"
|
|
mock.open()
|
|
code, opened = harness._cli(
|
|
"confirm-open",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert opened["state"] == ProjectState.INVENTORY_PENDING.value
|
|
|
|
code, inventory_command = harness._cli(
|
|
"prepare-inventory",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
assert inventory_command["action"] == "inventory"
|
|
mock.inventory()
|
|
code, _ = harness._cli(
|
|
"ingest",
|
|
str(harness.run_dir),
|
|
mock.project_id,
|
|
)
|
|
assert code == 0
|
|
code, status = harness._cli("status", str(harness.run_dir))
|
|
assert code == 0
|
|
harness.project_states = {
|
|
project["display_name"]: project["state"] for project in status["projects"]
|
|
}
|
|
return harness
|
|
|
|
def _result(self, *, undo_verified: bool) -> HarnessResult:
|
|
code, status = self._cli("status", str(self.run_dir))
|
|
assert code == 0
|
|
states = {project["display_name"]: project["state"] for project in status["projects"]}
|
|
code, report = self._cli("report", str(self.run_dir))
|
|
assert code == 0
|
|
return HarnessResult(
|
|
run_dir=self.run_dir,
|
|
project_states=states,
|
|
apply_groups=[group for mock in self.projects.values() for group in mock.apply_groups],
|
|
backups_verified=self._backups_verified(),
|
|
saved_projects={name for name, mock in self.projects.items() if mock.saved},
|
|
undo_verified=undo_verified,
|
|
exit_codes=tuple(self.exit_codes),
|
|
report_paths=tuple(Path(path) for path in report.values()),
|
|
projects=self.projects,
|
|
)
|
|
|
|
def _backups_verified(self) -> bool:
|
|
try:
|
|
records = [
|
|
json.loads(line)
|
|
for line in (self.run_dir / "events.jsonl").read_text(encoding="utf-8").splitlines()
|
|
]
|
|
backup_events = [record for record in records if record["event"] == "backup_verified"]
|
|
projects_by_id = {mock.project_id: mock for mock in self.projects.values()}
|
|
if len(backup_events) != len(projects_by_id):
|
|
return False
|
|
if {event["project_id"] for event in backup_events} != set(projects_by_id):
|
|
return False
|
|
|
|
referenced_backups: set[Path] = set()
|
|
for event in backup_events:
|
|
project_id = event["project_id"]
|
|
mock = projects_by_id[project_id]
|
|
payload = event["payload"]
|
|
if not isinstance(payload, dict) or set(payload) != {"backup_path", "manifest_path"}:
|
|
return False
|
|
backup_relative = Path(payload["backup_path"])
|
|
if (
|
|
backup_relative.is_absolute()
|
|
or len(backup_relative.parts) != 2
|
|
or backup_relative.parts[0] != "backups"
|
|
or not backup_relative.name.startswith(f"{project_id}-")
|
|
):
|
|
return False
|
|
manifest_relative = Path(payload["manifest_path"])
|
|
if (
|
|
manifest_relative.is_absolute()
|
|
or len(manifest_relative.parts) != 3
|
|
or manifest_relative.parts[:2] != ("evidence", project_id)
|
|
or not manifest_relative.name.startswith("backup-")
|
|
or manifest_relative.suffix != ".json"
|
|
):
|
|
return False
|
|
|
|
backup = self.run_dir / backup_relative
|
|
referenced_backups.add(backup)
|
|
data = _json_object(self.run_dir / manifest_relative)
|
|
manifest = TreeManifest(
|
|
data["project_root"],
|
|
tuple(
|
|
TreeFile(entry["path"], entry["size"], entry["sha256"])
|
|
for entry in data["files"]
|
|
),
|
|
)
|
|
if Path(manifest.project_root).resolve() != mock.marker.parent.resolve():
|
|
return False
|
|
if not verify_tree(backup, manifest):
|
|
return False
|
|
if load_snapshot(backup / "design.sch") != load_snapshot(mock.current_snapshot):
|
|
return False
|
|
|
|
actual_backups = set((self.run_dir / "backups").iterdir())
|
|
return actual_backups == referenced_backups
|
|
except (AssertionError, KeyError, OSError, TypeError, UnicodeError, ValueError):
|
|
return False
|