Files
NODEDC_MISSION_CORE/tests/fleet/test_rover_passive.py
T

115 lines
4.8 KiB
Python

import pytest
from missioncore_plugin_sdk.diagnostic_options import PASSIVE_OPTIONS, diagnostic_options
from k1link.fleet.rover_diagnostics import RoverDiagnostics
def ready():
d = RoverDiagnostics(lambda: 10.)
d.supports_options = d.supports_sources = d.supports_passive = True
return d
def test_default_capable_capture_is_passive_and_sleeps_after_start_ack():
d = ready()
d.set_enabled(True)
assert d.options == PASSIVE_OPTIONS
assert d.traffic_pending() and not d.watch_required()
identity = d.trace.id
d.ingest({'id': 'f'*32, 'active': True})
assert d.traffic_pending()
reply = d.exchange({'diagnostic_passive_v1': True, 'diagnostics': {
'id': identity, 'active': True, 'storage': {'closed': False}}})
assert reply['diagnostics_pending'] is False
assert d.enabled and d.passive_confirmed
receipts = [r for r in d.trace.local.snapshot()['frames']
if r['event'] == 'board_diagnostics_exchange']
assert len(receipts) == 1
for _ in range(5):
d.trace.add('telemetry_received', sequence=1)
assert not d.traffic_pending() and not d.watch_required()
assert not d.desired()['core_events']
d.set_enabled(False)
assert d.traffic_pending() and d.closing and not d.watch_required()
d.ingest({'id': identity, 'active': False, 'storage': {'closed': True}})
assert not d.traffic_pending() and not d.closing
d.set_enabled(True)
assert d.trace.id != identity and d.traffic_pending()
def test_passive_requires_capability_and_disallows_hidden_event_transmission():
d = ready()
d.supports_passive = False
with pytest.raises(ValueError, match='обновите'):
d.set_enabled(True, PASSIVE_OPTIONS)
for key in ('live', 'core_trace'):
with pytest.raises(ValueError, match='Passive'):
diagnostic_options({**PASSIVE_OPTIONS, key: True})
def test_export_is_explicit_and_reactivates_diagnostics_only_while_requested():
d = ready()
d.set_enabled(True)
d.ingest({'id': d.trace.id, 'active': True, 'storage': {'closed': False}})
assert not d.traffic_pending()
token = d.export.start(d.trace.id)
assert d.traffic_pending()
d.export.finish(token)
assert not d.traffic_pending()
def test_recovery_of_passive_capture_does_not_resume_periodic_transmission():
d = ready()
d.ingest({'id': 'a'*32, 'active': True, 'options': PASSIVE_OPTIONS,
'storage': {'closed': False}})
assert d.enabled and d.passive_confirmed
assert not d.traffic_pending() and not d.watch_required()
@pytest.mark.parametrize('recording', [False, True])
def test_fresh_core_requests_one_status_exchange_and_can_stop_surviving_capture(recording):
# The old Core has disappeared; Node's passive lane was already asleep.
# Recovery must be initiated by the existing telemetry reply, not by new
# periodic DEV polling or by starting a replacement capture.
d = ready()
assert d.traffic_pending() and not d.watch_required()
identity = 'b' * 32
packet = {'id': identity, 'active': True, 'options': PASSIVE_OPTIONS,
'storage': {'closed': False}, 'frames': []} if recording else None
reply = d.exchange({'diagnostic_passive_v1': True, 'diagnostics': packet})
assert reply['diagnostics_pending'] is False
assert not d.traffic_pending() and not d.watch_required()
assert d.enabled is recording
if recording:
assert d.trace.id == identity and not d.desired()['core_events']
d.set_enabled(False)
assert d.traffic_pending() and d.desired()['enabled'] is False
d.ingest({'id': identity, 'active': False, 'storage': {'closed': True}})
assert not d.traffic_pending() and not d.closing
else:
assert d.desired() is None
def test_passive_capture_does_not_request_a_control_watch_or_change_commands():
from k1link.fleet.rover_control import RoverControl
h = RoverControl(lambda: 10.)
relay = 'a'*32
body = {'relay_id': relay, 'rover': {'supported': True, 'instance': 'driver',
'state': 'observing'}, 'diagnostic_lane_v1': True,
'diagnostic_options_v1': True, 'diagnostic_sources_v1': True,
'diagnostic_passive_v1': True}
h.exchange('node', body)
d = h.diagnostics('node', 'rover')
d.set_enabled(True)
assert d.trace.local.active()
for confirmed in (False, True):
if confirmed:
d.ingest({'id': d.trace.id, 'active': True, 'storage': {'closed': False}})
value = h.exchange('node', body)
assert value['diagnostics_pending'] is (not confirmed)
assert value['watch'] is False and value['control_active'] is False
assert value['command'] is None and value['diagnostics'] is None
assert h.stream('node', relay)['watch'] is False
d.set_enabled(False)
assert h.exchange('node', body)['watch'] is False