Files
NODEDC_MISSION_CORE/tests/fleet/test_observation_configuration.py
T

72 lines
3.6 KiB
Python

import copy
import json
import threading
import time
from concurrent.futures import ThreadPoolExecutor
import pytest
from k1link.fleet import observation_configuration as observation
from k1link.fleet.rover_configuration import RoverConfigurations, SCHEMA, Unavailable
DOCUMENT = {"layouts": {"version": 1, "selected": "auto", "layout": {"version": 1, "hidden": ["map"]}, "presets": []}, "cameras": {"camera-a": {"rotations": {"left": 90, "right": 270}, "panoramaYaw": 45}}}
def snapshot(revision=0, document=None, change=None):
value={"schema":SCHEMA,"node_id":"node-a","name":"Rover 006","revision":revision,"observation":document}
if change:value["last_change"]=change
return {"rover_configuration":{"state":"ready","configuration":value}}
def dispatch(store):
for _ in range(100):
reply=store.heartbeat("node-a",snapshot())
if reply:return reply["rover_configuration_change"]
time.sleep(.005)
raise AssertionError("No pending request")
def test_browser_proposal_is_not_saved_before_onboard_receipt(tmp_path):
store=RoverConfigurations(tmp_path)
with pytest.raises(Unavailable):store.update("node-a","Rover 006",DOCUMENT,0)
assert not store.path("node-a").exists()
store.heartbeat("node-a",snapshot())
with ThreadPoolExecutor(max_workers=1) as pool:
result=pool.submit(store.update,"node-a","Rover 006",DOCUMENT,0)
change=dispatch(store)
assert not result.done()
assert json.loads(store.path("node-a").read_text())["observation"] is None
store.heartbeat("node-a",snapshot(1,DOCUMENT,change["id"]))
assert result.result(timeout=1)["document"]==DOCUMENT
assert store.path("node-a").stat().st_mode&0o777==0o600
assert RoverConfigurations(tmp_path).view("node-a")["document"]==DOCUMENT
assert not RoverConfigurations(tmp_path).view("node-a")["writable"]
assert not store.view("node-b")["available"]
def test_conflicts_timeout_and_late_onboard_commit_reconcile(tmp_path):
store=RoverConfigurations(tmp_path);store.heartbeat("node-a",snapshot())
with ThreadPoolExecutor(max_workers=1) as pool:
result=pool.submit(store.update,"node-a","Rover 006",DOCUMENT,0,timeout=.15)
change=dispatch(store)
with pytest.raises(observation.RevisionConflict):store.update("node-a","Rover 006",DOCUMENT,0)
with pytest.raises(Unavailable):result.result(timeout=1)
assert store.view("node-a")["document"] is None
store.heartbeat("node-a",snapshot(1,DOCUMENT,change["id"]))
assert store.view("node-a")["document"]==DOCUMENT
with pytest.raises(observation.RevisionConflict):store.update("node-a","Rover 006",DOCUMENT,0)
def test_wrong_node_or_corrupt_mirror_cannot_confirm_save(tmp_path):
store=RoverConfigurations(tmp_path);store.heartbeat("node-a",snapshot())
store.heartbeat("node-b",snapshot())
assert not store.path("node-b").exists()
store.path("node-a").write_text("{broken")
store.heartbeat("node-a",snapshot(1,DOCUMENT))
assert store.path("node-a").read_text()=="{broken"
assert not store.live["node-a"]["ready"]
@pytest.mark.parametrize("rotation",[45,True,"90",-90,360])
def test_invalid_rotations_rejected(rotation):
invalid=copy.deepcopy(DOCUMENT);invalid["cameras"]["camera-a"]["rotations"]["left"]=rotation
with pytest.raises(ValueError):observation.validate(invalid)
def test_unsupported_node_cannot_accept_writes_and_mirror_survives(tmp_path):
store=RoverConfigurations(tmp_path);store.heartbeat("node-a",snapshot(1,DOCUMENT))
store.heartbeat("node-a",{})
assert store.view("node-a")["document"]==DOCUMENT
with pytest.raises(Unavailable):store.update("node-a","Rover 006",DOCUMENT,1)