164 lines
6.5 KiB
Python
164 lines
6.5 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
SPEC = importlib.util.spec_from_file_location(
|
|
"evidence_closeout", Path(__file__).parents[1] / "simulation/ai-polygon/evidence_closeout.py"
|
|
)
|
|
evidence = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(evidence)
|
|
|
|
|
|
def setup(tmp_path):
|
|
job, store = tmp_path / "job", tmp_path / "archives"
|
|
job.mkdir()
|
|
(job / "urban-map.json").write_text('{"features":[]}')
|
|
(job / "observations.bin").write_bytes(b"preserved observations" * 1000)
|
|
return job, store
|
|
|
|
|
|
def test_lossless_closeout_keeps_map_and_restores_exact_source(tmp_path):
|
|
job, store = setup(tmp_path)
|
|
original = (job / "observations.bin").read_bytes()
|
|
result = evidence.closeout(job, store, "job-1", keep=("urban-map.json",), reserve=0)
|
|
assert result["phase"] == "complete"
|
|
assert (job / "urban-map.json").exists()
|
|
assert not (job / "observations.bin").exists()
|
|
target = tmp_path / "restored.bin"
|
|
evidence.restore_file(store / "job-1/closeout.json", "observations.bin", target)
|
|
assert target.read_bytes() == original
|
|
assert evidence.closeout(job, store, "job-1", reserve=0) == result
|
|
|
|
|
|
def test_capacity_refusal_preserves_every_input(tmp_path):
|
|
job, store = setup(tmp_path)
|
|
with pytest.raises(RuntimeError, match="quota"):
|
|
evidence.closeout(job, store, "job-1", limit=10, reserve=0)
|
|
assert (job / "observations.bin").exists()
|
|
assert not (store / "job-1/evidence.zip").exists()
|
|
|
|
|
|
def test_link_and_pin_refused_without_removal(tmp_path):
|
|
job, store = setup(tmp_path)
|
|
(job / "RETAIN.json").write_text("{}")
|
|
with pytest.raises(ValueError, match="pinned"):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
(job / "RETAIN.json").unlink()
|
|
(job / "linked").symlink_to(tmp_path / "elsewhere")
|
|
with pytest.raises((ValueError, FileNotFoundError)):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert (job / "observations.bin").exists()
|
|
|
|
|
|
def test_crash_after_unlink_reconciles_only_journaled_path(tmp_path, monkeypatch):
|
|
job, store = setup(tmp_path)
|
|
original = Path.unlink
|
|
|
|
def interrupted(path, *args, **kwargs):
|
|
original(path, *args, **kwargs)
|
|
if path == job / "observations.bin":
|
|
raise OSError("lost acknowledgement")
|
|
|
|
monkeypatch.setattr(Path, "unlink", interrupted)
|
|
with pytest.raises(OSError):
|
|
evidence.closeout(job, store, "job-1", keep=("urban-map.json",), reserve=0)
|
|
monkeypatch.setattr(Path, "unlink", original)
|
|
assert evidence.closeout(job, store, "job-1", reserve=0)["phase"] == "complete"
|
|
|
|
|
|
def test_changed_source_and_corrupt_archive_are_preserved(tmp_path, monkeypatch):
|
|
job, store = setup(tmp_path)
|
|
original = Path.unlink
|
|
|
|
def interrupted(path, *args, **kwargs):
|
|
if path == job / "observations.bin":
|
|
raise OSError("interrupted")
|
|
return original(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(Path, "unlink", interrupted)
|
|
with pytest.raises(OSError):
|
|
evidence.closeout(job, store, "job-1", keep=("urban-map.json",), reserve=0)
|
|
monkeypatch.setattr(Path, "unlink", original)
|
|
(job / "observations.bin").write_bytes(b"new evidence")
|
|
with pytest.raises(ValueError, match="changed after"):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert (job / "observations.bin").read_bytes() == b"new evidence"
|
|
(store / "job-1/evidence.zip").write_bytes(b"broken")
|
|
with pytest.raises(ValueError, match="checksum"):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert (job / "observations.bin").exists()
|
|
|
|
|
|
def test_root_link_and_control_link_are_rejected(tmp_path):
|
|
job, store = setup(tmp_path)
|
|
link = tmp_path / "job-alias"
|
|
link.symlink_to(job, target_is_directory=True)
|
|
with pytest.raises(ValueError, match="Linked"):
|
|
evidence.closeout(link, store, "job-1", reserve=0)
|
|
store.mkdir(exist_ok=True)
|
|
(store / "job-1").symlink_to(job, target_is_directory=True)
|
|
with pytest.raises(ValueError, match="Linked"):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert (job / "observations.bin").exists()
|
|
|
|
|
|
def test_resume_honors_new_pin_and_capacity(tmp_path, monkeypatch):
|
|
job, store = setup(tmp_path)
|
|
original = evidence.zipfile.ZipFile
|
|
monkeypatch.setattr(
|
|
evidence.zipfile, "ZipFile", lambda *a, **kw: (_ for _ in ()).throw(OSError("interrupt"))
|
|
)
|
|
with pytest.raises(OSError):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
monkeypatch.setattr(evidence.zipfile, "ZipFile", original)
|
|
with pytest.raises(RuntimeError, match="quota"):
|
|
evidence.closeout(job, store, "job-1", limit=1, reserve=0)
|
|
(job / "RETAIN.json").write_text("{}")
|
|
with pytest.raises(ValueError, match="pinned"):
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert (job / "observations.bin").exists()
|
|
|
|
|
|
def test_complete_retry_repairs_missing_archive_locator(tmp_path):
|
|
job, store = setup(tmp_path)
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
(job / "ARCHIVED.json").unlink()
|
|
evidence.closeout(job, store, "job-1", reserve=0)
|
|
assert json.loads((job / "ARCHIVED.json").read_text())["owner"] == "job-1"
|
|
|
|
|
|
def test_partial_final_journal_record_is_recovered(tmp_path, monkeypatch):
|
|
job, store = setup(tmp_path)
|
|
original = Path.unlink
|
|
|
|
def interrupted(path, *args, **kw):
|
|
original(path, *args, **kw)
|
|
if path == job / "observations.bin":
|
|
raise OSError("crash")
|
|
|
|
monkeypatch.setattr(Path, "unlink", interrupted)
|
|
with pytest.raises(OSError):
|
|
evidence.closeout(job, store, "job-1", keep=("urban-map.json",), reserve=0)
|
|
monkeypatch.setattr(Path, "unlink", original)
|
|
with (store / "job-1/deletions.jsonl").open("ab") as f:
|
|
f.write(b'{"action":"rem')
|
|
assert evidence.closeout(job, store, "job-1", reserve=0)["phase"] == "complete"
|
|
|
|
|
|
def test_published_archive_before_state_commit_resumes_without_second_budget(tmp_path, monkeypatch):
|
|
job, store = setup(tmp_path)
|
|
original = evidence.atomic_json
|
|
|
|
def interrupted(path, value):
|
|
if path.name == "closeout.json" and value.get("phase") == "archived":
|
|
raise OSError("crash after zip publish")
|
|
return original(path, value)
|
|
|
|
monkeypatch.setattr(evidence, "atomic_json", interrupted)
|
|
with pytest.raises(OSError):
|
|
evidence.closeout(job, store, "job-1", keep=("urban-map.json",), reserve=0)
|
|
monkeypatch.setattr(evidence, "atomic_json", original)
|
|
assert evidence.closeout(job, store, "job-1", limit=1, reserve=0)["phase"] == "complete"
|