Files
NODEDC_MISSION_CORE/tests/test_board_uplink_fallback.py
T

79 lines
3.0 KiB
Python

import copy
import pytest
from k1link.fleet.uplinks import Uplinks
from tests.test_board_uplinks import PAYLOAD, ROW
def setup():
clock=[1000]
u=Uplinks(lambda:clock[0])
p=copy.deepcopy(PAYLOAD)
p['uplink']['fallback']={'enabled':True,'revision':0,'operation_id':''}
u.heartbeat(ROW,p)
command=dict(enabled=False,revision=0,runtime='runtime',operation_id='e'*32)
return u,p,clock,command
def test_setting_is_not_optimistically_applied_and_receipt_survives_core_restart():
u,p,clock,command=setup()
assert u.set_fallback(ROW,command)['phase']=='queued'
assert u.view(ROW)['fallback']['enabled'] is True
sent=u.heartbeat(ROW,p)['uplink_change']
assert sent['kind']=='fallback' and sent['enabled'] is False
p['uplink']['fallback']={'enabled':False,'revision':1,'operation_id':command['operation_id']}
assert not u.heartbeat(ROW,p)
assert u.view(ROW)['fallback_operation']['phase']=='applied'
new=Uplinks(lambda:clock[0])
new.heartbeat(ROW,p)
assert new.view(ROW)['fallback']['enabled'] is False
def test_timeout_or_old_node_never_pretends_setting_saved():
u,p,clock,command=setup()
u.set_fallback(ROW,command)
clock[0]+=31
assert not u.heartbeat(ROW,p)
assert u.view(ROW)['fallback_operation']['phase']=='failed'
p['uplink'].pop('fallback')
u.heartbeat(ROW,p)
with pytest.raises(ValueError):
u.set_fallback(ROW,{**command,'operation_id':'f'*32})
def test_new_runtime_receives_no_stale_policy_command():
u,p,clock,command=setup()
u.set_fallback(ROW,command)
p['execution_binding']['agent_instance_id']='new'
assert not u.heartbeat(ROW,p)
assert u.view(ROW)['fallback_operation']['phase']=='failed'
def test_api_preserves_explicit_false_and_rejects_foreign_origin_and_bad_revision():
import threading
from types import SimpleNamespace
from fastapi import APIRouter, FastAPI
from fastapi.testclient import TestClient
from k1link.web.fleet_api import local_operator
from k1link.web.fleet_uplink_api import register
u, p, clock, command = setup()
app, router = FastAPI(), APIRouter(prefix='/api/v1/fleet')
register(router, local_operator)
app.include_router(router)
app.state.fleet = SimpleNamespace(lock=threading.RLock(), find=lambda _:ROW,
uplinks=u, network_profiles=SimpleNamespace(view=lambda _:{}))
with TestClient(app, base_url='http://127.0.0.1', client=('127.0.0.1', 12345)) as client:
path='/api/v1/fleet/test/uplink/fallback'
rejected = client.patch(path, json=command,
headers={'Origin':'https://other.invalid'})
assert rejected.status_code == 403
assert client.patch(path, json={**command, 'enabled':'false'}).status_code == 422
assert client.patch(path, json={**command, 'revision':False}).status_code == 422
assert client.patch(path, json=command).json()['phase'] == 'queued'
assert u.heartbeat(ROW,p)['uplink_change']['enabled'] is False