162 lines
5.8 KiB
Python
162 lines
5.8 KiB
Python
"""Core/VPS ownership survives lost HTTP, restarts and per-attempt timeouts."""
|
|
|
|
import ast
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from test_equipment_command_runtime import ready # noqa: F401
|
|
from test_equipment_network import context # noqa: F401
|
|
|
|
from k1link.fleet import equipment_command_journal as journal
|
|
from k1link.fleet import equipment_command_runtime as runtime
|
|
from k1link.fleet import equipment_network as network
|
|
from k1link.fleet.independent_equipment import EquipmentStore
|
|
|
|
|
|
def shipped_version():
|
|
source = Path(__file__).resolve().parents[1] / "apps/vps-host/ndc_vps/__init__.py"
|
|
module = ast.parse(source.read_text())
|
|
return next(
|
|
ast.literal_eval(n.value)
|
|
for n in module.body
|
|
if isinstance(n, ast.Assign)
|
|
and any(isinstance(t, ast.Name) and t.id == "VERSION" for t in n.targets)
|
|
)
|
|
|
|
|
|
@pytest.fixture(params=sorted({"0.1.32", shipped_version()}))
|
|
def durable(ready, request): # noqa: F811
|
|
equipment, hosts, row = ready
|
|
for host in hosts.rows():
|
|
host["status"]["version"] = request.param
|
|
host["status"]["capabilities"] = ["arnavi-b2-durable-operations-v1"]
|
|
hosts.save(host)
|
|
return equipment, hosts, row
|
|
|
|
|
|
def test_lost_post_recovers_after_core_restart_without_job_deadline(durable, monkeypatch):
|
|
equipment, hosts, row = durable
|
|
calls = []
|
|
|
|
def lost(host, method, path, body=None):
|
|
calls.append((method, path, body))
|
|
raise OSError("lost HTTP")
|
|
|
|
monkeypatch.setattr(network, "request", lost)
|
|
job = runtime.enqueue(hosts, row["id"], "1" * 32, "position")
|
|
assert job["durable"] and "expires_at" not in job
|
|
runtime.pump(hosts)
|
|
assert calls[0][1] == "/v1/operations" and "not_after" not in calls[0][2]
|
|
original = calls[0][2]
|
|
now = time.time() + 86400
|
|
monkeypatch.setattr(time, "time", lambda: now)
|
|
hosts.equipment = EquipmentStore(equipment.root.parent, {})
|
|
source = next(h for h in hosts.rows() if h["id"] == "current")
|
|
network.refresh(hosts, source)
|
|
entry = journal.listing(hosts.equipment, row["id"])["items"][0]
|
|
assert entry["state"] == "submitting" and entry["reconcile_pending"]
|
|
assert calls[1][0] == "GET" and calls[-1][0] == "POST" and calls[-1][2] == original
|
|
|
|
def restored(host, method, path, body=None):
|
|
calls.append((method, path, body))
|
|
return {
|
|
"id": job["id"],
|
|
"kind": "request-position",
|
|
"durable": True,
|
|
"state": "device_confirmed",
|
|
"runtime_phase": "complete",
|
|
"attempt_count": 2,
|
|
"device_code": 0,
|
|
"received_at": now,
|
|
}
|
|
|
|
monkeypatch.setattr(network, "request", restored)
|
|
network.refresh(hosts, source)
|
|
entry = journal.listing(hosts.equipment, row["id"])["items"][0]
|
|
assert entry["state"] == "device_confirmed" and not entry["reconcile_pending"]
|
|
assert calls[-1][0] == "GET" and entry["attempt_count"] == 2
|
|
with hosts.equipment.db() as db:
|
|
assert db.execute("SELECT count(*) FROM network_outbox").fetchone()[0] == 0
|
|
|
|
|
|
def test_attempt_facts_are_replaced_and_history_keeps_acceptance(durable, monkeypatch):
|
|
equipment, hosts, row = durable
|
|
|
|
def queued(host, method, path, body=None):
|
|
return {
|
|
"id": body["id"],
|
|
"kind": body["kind"],
|
|
"durable": True,
|
|
"state": "queued",
|
|
"runtime_phase": "waiting-link",
|
|
"attempt_count": 0,
|
|
}
|
|
|
|
monkeypatch.setattr(network, "request", queued)
|
|
job = runtime.enqueue(hosts, row["id"], "2" * 32, "position")
|
|
runtime.pump(hosts)
|
|
accepted = {
|
|
"id": job["id"],
|
|
"kind": "request-position",
|
|
"durable": True,
|
|
"state": "dispatched",
|
|
"runtime_phase": "executing",
|
|
"attempt_count": 1,
|
|
"device_code": 6,
|
|
"device_accepted_at": time.time(),
|
|
"dispatched_at": time.time(),
|
|
}
|
|
network.record(hosts, row["id"], job["id"], accepted)
|
|
network.record(
|
|
hosts,
|
|
row["id"],
|
|
job["id"],
|
|
{
|
|
"id": job["id"],
|
|
"kind": "request-position",
|
|
"durable": True,
|
|
"state": "queued",
|
|
"runtime_phase": "executing",
|
|
"attempt_count": 2,
|
|
},
|
|
)
|
|
entry = journal.listing(equipment, row["id"])["items"][0]
|
|
assert entry["attempt_count"] == 2 and "device_code" not in entry
|
|
assert "device_accepted_at" not in entry and "dispatched_at" not in entry
|
|
assert any(e["state"] == "device_accepted" for e in entry["events"])
|
|
assert [e["attempt"] for e in entry["events"] if e["state"] == "attempt_started"] == [1, 2]
|
|
|
|
|
|
def test_stop_is_persisted_before_vps_contact_and_not_reposted(durable, monkeypatch):
|
|
equipment, hosts, row = durable
|
|
|
|
def lost(*args):
|
|
raise OSError("lost")
|
|
|
|
monkeypatch.setattr(network, "request", lost)
|
|
job = runtime.enqueue(hosts, row["id"], "3" * 32, "position")
|
|
runtime.pump(hosts)
|
|
runtime.cancel(equipment, row["id"], job["id"], hosts)
|
|
hosts.equipment = EquipmentStore(equipment.root.parent, {})
|
|
calls = []
|
|
|
|
def stop(host, method, path, body=None):
|
|
calls.append((method, path, body))
|
|
return {
|
|
"id": job["id"],
|
|
"kind": "request-position",
|
|
"durable": True,
|
|
"state": "stopped",
|
|
"runtime_phase": "stopped",
|
|
"stop_requested": True,
|
|
"attempt_count": 0,
|
|
}
|
|
|
|
monkeypatch.setattr(network, "request", stop)
|
|
network.refresh(hosts, next(h for h in hosts.rows() if h["id"] == "current"))
|
|
assert calls == [("POST", f"/v1/operations/{job['id']}/cancel", {"kind": "request-position"})]
|
|
assert journal.listing(hosts.equipment, row["id"])["items"][0]["state"] == "stopped"
|
|
assert "password" not in json.dumps(journal.listing(hosts.equipment, row["id"]))
|