Files
seismo-relay/tests/test_micromate_execute.py
T

86 lines
2.8 KiB
Python

"""execute(plan): the only write path -- exact frames, one call's permission, a ceiling."""
from __future__ import annotations
import os
import sys
import pytest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from micromate.framing import FLAGS_THOR, build_request # noqa: E402
from micromate.opcodes import ENABLEABLE_WRITES, OpcodeRefused # noqa: E402
from micromate.protocol import ACK_DATA_LEN, UnexpectedResponse # noqa: E402
from micromate.writes import ( # noqa: E402
plan_call_home_enabled, plan_start_monitoring, plan_stop_monitoring,
)
from test_micromate_protocol import frame, proto # noqa: E402
ACK_START = frame(0x69, bytes(ACK_DATA_LEN))
POLL_RSP = frame(0xA4, bytes(48))
def _ready(responses):
"""A protocol that has already seen one Micromate-flagged reply (a POLL)."""
p, t = proto([POLL_RSP] + responses)
p.poll()
t.written.clear()
return p, t
def test_the_ceiling_is_start_and_stop_only():
assert ENABLEABLE_WRITES == frozenset({0x96, 0x97})
def test_execute_sends_exactly_the_plans_frame_and_returns_the_ack():
p, t = _ready([ACK_START])
acks = p.execute(plan_start_monitoring())
assert t.written == [build_request(0x96, 0xFFFF)]
assert [a.sub for a in acks] == [0x69]
def test_permission_ends_with_the_call():
p, t = _ready([ACK_START])
p.execute(plan_start_monitoring())
with pytest.raises(OpcodeRefused):
p._send(build_request(0x96, 0xFFFF))
assert len(t.written) == 1
def test_a_plan_beyond_the_ceiling_is_refused_and_nothing_is_sent():
content = bytes(5) + b"\x05" + bytes(120)
p, t = _ready([])
with pytest.raises(OpcodeRefused, match="ceiling"):
p.execute(plan_call_home_enabled(content, False))
assert t.written == []
def test_execute_refuses_before_any_micromate_reply_has_been_seen():
p, t = proto([ACK_START])
with pytest.raises(OpcodeRefused, match="Micromate"):
p.execute(plan_start_monitoring())
assert t.written == []
def test_execute_refuses_after_a_non_micromate_flags_reply():
p, t = proto([frame(0xA4, bytes(48), flags=0x10), ACK_START], strict_checksums=False)
p.poll()
t.written.clear()
with pytest.raises(OpcodeRefused):
p.execute(plan_stop_monitoring())
assert t.written == []
def test_a_wrong_ack_sub_raises_after_one_send():
p, t = _ready([frame(0x68, bytes(ACK_DATA_LEN))]) # stop's ack, for a start
with pytest.raises(UnexpectedResponse):
p.execute(plan_start_monitoring())
assert len(t.written) == 1
def test_thor_flags_are_accepted_too():
p, t = proto([frame(0xA4, bytes(48), flags=FLAGS_THOR), frame(0x68, bytes(ACK_DATA_LEN))])
p.poll()
p.execute(plan_stop_monitoring())
assert t.written[-1] == build_request(0x97, 0xFFFF)