115 lines
4.0 KiB
Python
115 lines
4.0 KiB
Python
"""Late results survive Core restart without reissuing physical commands."""
|
|
|
|
# ruff: noqa: F811 -- imported pytest fixtures
|
|
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
from test_equipment_command_runtime import ready, sender # 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 defer(ready, monkeypatch):
|
|
equipment, hosts, row = ready
|
|
sent = sender(monkeypatch)
|
|
op = runtime.enqueue(hosts, row["id"], "1" * 32, "position")
|
|
runtime.pump(hosts)
|
|
now = [op["expires_at"] + 1]
|
|
monkeypatch.setattr(time, "time", lambda: now[0])
|
|
network.record(
|
|
hosts,
|
|
row["id"],
|
|
op["id"],
|
|
{
|
|
"id": op["id"],
|
|
"kind": "request-position",
|
|
"state": "uncertain",
|
|
},
|
|
reconcile=True,
|
|
)
|
|
host = next(h for h in hosts.rows() if h["id"] == "current")
|
|
return equipment, hosts, row, op, now, host, sent
|
|
|
|
|
|
@pytest.mark.parametrize("response", ["unavailable", "queued", "wrong_id", "wrong_kind"])
|
|
def test_invalid_or_pending_late_result_keeps_unknown_and_persistent_backoff(
|
|
ready, monkeypatch, response
|
|
):
|
|
equipment, hosts, row, op, now, host, sent = defer(ready, monkeypatch)
|
|
calls = []
|
|
|
|
def get_result(source, method, path, body=None):
|
|
calls.append((source["id"], method))
|
|
if response == "unavailable":
|
|
raise OSError("unavailable")
|
|
return {
|
|
"id": "another" if response == "wrong_id" else op["id"],
|
|
"kind": "restart" if response == "wrong_kind" else "request-position",
|
|
"state": "queued" if response == "queued" else "device_confirmed",
|
|
}
|
|
|
|
monkeypatch.setattr(network, "request", get_result)
|
|
network.refresh(hosts, host)
|
|
hosts.equipment = EquipmentStore(equipment.root.parent, {})
|
|
network.refresh(hosts, host)
|
|
assert calls == [("current", "GET")] and len(sent) == 1
|
|
item = journal.listing(hosts.equipment, row["id"])["items"][0]
|
|
assert item["state"] == "uncertain" and item["reconcile_pending"]
|
|
now[0] += 5
|
|
network.refresh(hosts, {**host, "state": "replaced"})
|
|
network.refresh(hosts, {**host, "id": "different-host"})
|
|
assert len(calls) == 1
|
|
network.refresh(hosts, host)
|
|
assert calls == [("current", "GET")] * 2
|
|
|
|
|
|
def test_old_result_is_in_tracked_page_until_authoritative_outcome(ready, monkeypatch):
|
|
equipment, hosts, row, op, _, host, _ = defer(ready, monkeypatch)
|
|
with equipment.db() as db:
|
|
for n in range(40):
|
|
journal.put(db, row["id"], {"id": f"later-{n}", "state": "expired"})
|
|
page = journal.listing(equipment, row["id"])
|
|
assert op["id"] not in {i["id"] for i in page["items"]}
|
|
assert [i["id"] for i in page["tracked_items"]] == [op["id"]]
|
|
monkeypatch.setattr(
|
|
network,
|
|
"request",
|
|
lambda *args: {
|
|
"id": op["id"],
|
|
"kind": "request-position",
|
|
"state": "unconfirmed",
|
|
},
|
|
)
|
|
network.refresh(hosts, host)
|
|
page = journal.listing(equipment, row["id"], tracked=[op["id"]])
|
|
assert page["tracked_items"][0]["state"] == "unconfirmed"
|
|
assert not page["tracked_items"][0]["reconcile_pending"]
|
|
with equipment.db() as db:
|
|
assert db.execute("SELECT count(*) FROM command_result_probes").fetchone()[0] == 0
|
|
assert "password" not in json.dumps(page)
|
|
|
|
|
|
def test_vps_terminal_uncertainty_does_not_start_endless_reconciliation(ready, monkeypatch):
|
|
equipment, hosts, row = ready
|
|
sender(monkeypatch)
|
|
op = runtime.enqueue(hosts, row["id"], "1" * 32, "position")
|
|
runtime.pump(hosts)
|
|
network.record(
|
|
hosts,
|
|
row["id"],
|
|
op["id"],
|
|
{
|
|
"id": op["id"],
|
|
"kind": "request-position",
|
|
"state": "uncertain",
|
|
},
|
|
)
|
|
with equipment.db() as db:
|
|
assert db.execute("SELECT count(*) FROM command_result_probes").fetchone()[0] == 0
|