75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
import time
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from k1link.fleet.equipment_bootstrap import apply
|
|
from k1link.fleet.independent_equipment import EquipmentError
|
|
|
|
|
|
def context():
|
|
item = {
|
|
"id": "device",
|
|
"revision": 1,
|
|
"vehicle_id": "rover",
|
|
"settings": {"endpoint": {"slot": 1, "host": "owner.example", "port": 9922}},
|
|
}
|
|
host = {
|
|
"state": "paired",
|
|
"vehicle_ids": ["rover"],
|
|
"status": {"version": "0.1.15", "tracker": {"host": "owner.example", "port": 9922}},
|
|
"last_sync_at": time.time(),
|
|
}
|
|
import threading
|
|
|
|
equipment = SimpleNamespace(
|
|
listing=lambda: [item], require_idle=Mock(), apply=Mock(return_value={"ok": True})
|
|
)
|
|
hosts = SimpleNamespace(
|
|
equipment=equipment, lock=threading.RLock(), rows=lambda: [host], sync=Mock()
|
|
)
|
|
return hosts, item, host
|
|
|
|
|
|
def test_initial_usb_apply_admits_the_device_to_paired_vps_before_write():
|
|
hosts, item, host = context()
|
|
hosts.equipment.apply.side_effect = lambda *args: (
|
|
hosts.sync.assert_called_once_with(host) or {"ok": True}
|
|
)
|
|
assert apply(hosts, "device", 1) == {"ok": True}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"case", ["stale", "wrong_vehicle", "not_paired", "old_host", "unqualified_host", "no_endpoint"]
|
|
)
|
|
def test_initial_usb_apply_rejects_unowned_or_incompatible_destination(case):
|
|
hosts, item, host = context()
|
|
if case == "stale":
|
|
item["revision"] = 2
|
|
if case == "wrong_vehicle":
|
|
host["vehicle_ids"] = []
|
|
if case == "not_paired":
|
|
host["state"] = "offline"
|
|
if case == "old_host":
|
|
host["status"]["version"] = "0.1.14"
|
|
if case == "unqualified_host":
|
|
host["status"]["version"] = "0.1.999"
|
|
if case == "no_endpoint":
|
|
item["settings"]["endpoint"] = None
|
|
with pytest.raises(EquipmentError):
|
|
apply(hosts, "device", 1)
|
|
hosts.equipment.apply.assert_not_called()
|
|
|
|
|
|
def test_initial_usb_apply_accepts_current_shipped_host():
|
|
import json
|
|
from pathlib import Path
|
|
|
|
source = Path(__file__).resolve().parent / "fixtures/arnavi_vps/client-v1.json"
|
|
version = json.loads(source.read_text())["server_version"]
|
|
hosts, _, host = context()
|
|
host["status"]["version"] = version
|
|
assert apply(hosts, "device", 1) == {"ok": True}
|
|
hosts.equipment.apply.assert_called_once_with("device", 1)
|