Keep operator monitoring in bounded live snapshots and provide scoped replica retirement. Update the board dashboard and retirement regressions.
117 lines
4.3 KiB
Python
117 lines
4.3 KiB
Python
import importlib.util
|
|
import json
|
|
import sqlite3
|
|
from contextlib import closing
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
spec = importlib.util.spec_from_file_location(
|
|
"retire_monitor_replica",
|
|
Path(__file__).resolve().parents[1] / "scripts/retire_monitor_replica.py",
|
|
)
|
|
retire = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(retire)
|
|
|
|
|
|
@pytest.fixture
|
|
def root(tmp_path):
|
|
fleet = tmp_path / ".runtime/mission-core/fleet"
|
|
fleet.mkdir(parents=True)
|
|
with closing(sqlite3.connect(fleet / "monitor.sqlite3")) as db:
|
|
for table, columns in retire.TABLES.items():
|
|
db.execute(f"CREATE TABLE {table} ({', '.join(columns)})")
|
|
db.commit()
|
|
(fleet / "monitor.sqlite3-wal").write_bytes(b"closed legacy sidecar")
|
|
(fleet / "monitor.sqlite3-shm").write_bytes(b"closed legacy sidecar")
|
|
return tmp_path
|
|
|
|
|
|
def accepted():
|
|
return {"mode": "live", "core_operational": True, "verified_vehicles": 1}
|
|
|
|
|
|
def test_only_exact_obsolete_files_removed_after_live_acceptance(root):
|
|
fleet = root / ".runtime/mission-core/fleet"
|
|
unrelated = fleet / "registry.json"
|
|
unrelated.write_text("durable fleet data")
|
|
snapshot = retire.plan(root)
|
|
receipt = retire.apply(snapshot, probe=accepted, closed=lambda _: None)
|
|
assert receipt["status"] == "complete" and len(receipt["removed"]) == 3
|
|
assert unrelated.read_text() == "durable fleet data"
|
|
assert retire.plan(root)["files"] == []
|
|
assert json.loads(Path(receipt["receipt"]).read_text())["removed"] == receipt["removed"]
|
|
|
|
|
|
def test_stale_plan_does_not_remove_any_files(root):
|
|
snapshot = retire.plan(root)
|
|
fleet = root / ".runtime/mission-core/fleet"
|
|
(fleet / "monitor.sqlite3-wal").write_bytes(b"changed")
|
|
with pytest.raises(ValueError, match="Stale"):
|
|
retire.apply(snapshot, probe=accepted, closed=lambda _: None)
|
|
assert all((fleet / name).exists() for name in retire.FILES)
|
|
|
|
|
|
def test_open_reader_or_old_running_core_blocks_retirement(root):
|
|
snapshot = retire.plan(root)
|
|
|
|
def blocked(*_):
|
|
raise ValueError("not accepted")
|
|
|
|
with pytest.raises(ValueError, match="accepted"):
|
|
retire.apply(snapshot, probe=blocked, closed=lambda _: None)
|
|
with pytest.raises(ValueError, match="accepted"):
|
|
retire.apply(snapshot, probe=accepted, closed=blocked)
|
|
assert len(retire.plan(root)["files"]) == 3
|
|
|
|
|
|
def test_unexpected_schema_or_symlink_is_never_deleted(root):
|
|
fleet = root / ".runtime/mission-core/fleet"
|
|
with closing(sqlite3.connect(fleet / "monitor.sqlite3")) as db:
|
|
db.execute("CREATE TABLE recordings (id)")
|
|
db.commit()
|
|
with pytest.raises(ValueError, match="tables"):
|
|
retire.plan(root)
|
|
side = fleet / "monitor.sqlite3-wal"
|
|
side.unlink(missing_ok=True)
|
|
side.symlink_to(fleet / "monitor.sqlite3")
|
|
with pytest.raises(ValueError, match="regular"):
|
|
retire.plan(root)
|
|
|
|
|
|
def test_partial_retirement_keeps_main_last_and_can_be_replanned(root):
|
|
snapshot = retire.plan(root)
|
|
unlink = Path.unlink
|
|
|
|
def fail(path, *args, **kwargs):
|
|
if path.name == "monitor.sqlite3-wal":
|
|
raise OSError("simulated interruption")
|
|
return unlink(path, *args, **kwargs)
|
|
|
|
with patch.object(Path, "unlink", fail), pytest.raises(OSError, match="interruption"):
|
|
retire.apply(snapshot, probe=accepted, closed=lambda _: None)
|
|
fleet = root / ".runtime/mission-core/fleet"
|
|
assert (fleet / "monitor.sqlite3").exists()
|
|
assert not (fleet / "monitor.sqlite3-shm").exists()
|
|
receipt = json.loads(
|
|
next((root / ".runtime/storage-maintenance/monitor-retirement").glob("*.json")).read_text()
|
|
)
|
|
assert receipt["status"] == "interrupted" and len(receipt["removed"]) == 1
|
|
assert (
|
|
len(retire.apply(retire.plan(root), probe=accepted, closed=lambda _: None)["removed"]) == 2
|
|
)
|
|
|
|
|
|
def test_orphan_sidecar_is_not_assumed_disposable(root):
|
|
(root / ".runtime/mission-core/fleet/monitor.sqlite3").unlink()
|
|
with pytest.raises(ValueError, match="orphan"):
|
|
retire.plan(root)
|
|
|
|
|
|
def test_actual_open_file_check_refuses_a_reader_and_accepts_closed_file(root):
|
|
target = root / ".runtime/mission-core/fleet/monitor.sqlite3"
|
|
with target.open("rb"), pytest.raises(ValueError, match="open"):
|
|
retire.assert_closed([target])
|
|
retire.assert_closed([target])
|