Files
NODEDC_MISSION_CORE/tests/fleet/test_power.py
T

129 lines
4.5 KiB
Python

"""No real power action: exercise expiry, identity, handshake and HTTP admission."""
import threading
from types import SimpleNamespace
import pytest
from fastapi import APIRouter, FastAPI
from fastapi.testclient import TestClient
from k1link.fleet.power import SCHEMA, BoardPower
from k1link.web.fleet_api import local_operator
from k1link.web.fleet_power_api import register
EPOCH = "a" * 43
ID = "12345678-1234-4234-8234-123456789abc"
ROW = {"id": "vehicle", "enrollment": "paired", "binding": {"binding_id": "owner"}}
@pytest.fixture
def power(monkeypatch):
clock = [1000.0]
monkeypatch.setattr("k1link.fleet.power.time.time", lambda: clock[0])
p = BoardPower()
p.heartbeat(ROW, {"power": dict(schema=SCHEMA, epoch=EPOCH, available=True)})
return p, clock
def test_operator_handshake_needs_fresh_capability_and_preserves_command_identity(power):
p, clock = power
assert p.view(ROW)["available"]
p.start(ROW, EPOCH, ID)
assert not p.view(ROW)["available"]
assert p.start(ROW, EPOCH, ID)["operation"]["id"] == ID
command = p.heartbeat(ROW, {"power": dict(schema=SCHEMA, epoch=EPOCH, available=True)})
assert command["power_command"]["action"] == "poweroff"
assert command["power_command"]["binding_id"] == "owner"
assert "power_ack" not in command
ack = p.heartbeat(
ROW,
{
"power": dict(
schema=SCHEMA, epoch=EPOCH, available=True, result=dict(id=ID, state="accepted")
)
},
)
assert ack == {"power_ack": ID}
assert p.view(ROW)["operation"]["state"] == "accepted"
clock[0] += 25
assert p.view(ROW)["operation"]["state"] == "unknown"
assert not p.view(ROW)["available"]
@pytest.mark.parametrize(
"change", [{"epoch": "b" * 43}, {"binding": {"binding_id": "new"}}, {"enrollment": "revoked"}]
)
def test_wrong_epoch_rebinding_and_revocation_cannot_queue_shutdown(power, change):
p, _ = power
with pytest.raises(ValueError):
p.start(
ROW | {k: v for k, v in change.items() if k != "epoch"}, change.get("epoch", EPOCH), ID
)
assert not p.operations
def test_expiry_and_missing_capability_never_replay_after_reconnection(power):
p, clock = power
p.start(ROW, EPOCH, ID)
clock[0] += 21
assert p.heartbeat(ROW, {"power": dict(schema=SCHEMA, epoch=EPOCH, available=True)}) == {}
assert p.view(ROW)["operation"]["state"] == "expired"
p.heartbeat(ROW, {})
with pytest.raises(ValueError):
p.start(ROW, EPOCH, "new")
assert not BoardPower().view(ROW)["available"]
def test_failed_os_call_is_visible_without_resubmitting(power):
p, _ = power
p.start(ROW, EPOCH, ID)
assert (
p.heartbeat(
ROW,
{
"power": dict(
schema=SCHEMA, epoch=EPOCH, available=True, result=dict(id=ID, state="failed")
)
},
)
== {}
)
assert p.view(ROW)["operation"]["state"] == "failed"
def test_offline_or_restarted_node_requires_new_confirmation(power):
p, clock = power
clock[0] += 13
with pytest.raises(ValueError):
p.start(ROW, EPOCH, ID)
p.heartbeat(ROW, {"power": dict(schema=SCHEMA, epoch="b" * 43, available=True)})
with pytest.raises(ValueError):
p.start(ROW, EPOCH, ID)
def test_http_requires_local_operator_and_exact_body(power):
p, _ = power
app = FastAPI()
router = APIRouter(prefix="/api/v1/fleet")
register(router, local_operator)
app.include_router(router)
app.state.fleet = SimpleNamespace(power=p, lock=threading.RLock(), find=lambda _: ROW)
body = {"expected_epoch": EPOCH, "request_id": ID}
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as web:
path = "/api/v1/fleet/vehicle/power/shutdown"
assert (
web.post(path, json=body, headers={"Origin": "https://unrelated.example"}).status_code
== 403
)
assert (
web.post(path, json=body, headers={"Sec-Fetch-Site": "cross-site"}).status_code == 403
)
assert web.post(path, json=body | {"command": "reboot"}).status_code == 422
assert web.post(path, json=body | {"request_id": "bad"}).status_code == 422
assert not p.operations
assert web.post(path, json=body).status_code == 202
assert web.post(path, json=body).status_code == 202
result = web.get("/api/v1/fleet/vehicle/power")
assert result.headers["Cache-Control"] == "no-store"
assert result.json()["operation"]["state"] == "queued"