"""DeviceAccess routes call-home by series (phase 4): a read, and an audited change that is never retried, verified over a short config window (3 reads, 1 s apart), not the monitoring one.""" from __future__ import annotations import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import micromate.protocol as mm_proto # noqa: E402 from device_access_fakes import FakeMiniMateClient, FakeMicromateClient, um12947_call_home # noqa: E402 from sfm.device_access.access import DeviceAccess # noqa: E402 from sfm.device_access.errors import DeviceError # noqa: E402 from sfm.device_access.models import UnitAddress # noqa: E402 ADDR = UnitAddress(host="10.0.0.5", tcp_port=9034, port=None, baud=None) TCP3 = UnitAddress(host="10.0.0.6", tcp_port=12345, port=None, baud=None) class Audit: def __init__(self): self.rows = {} def begin(self, **f): cid = f"c{len(self.rows) + 1}" self.rows[cid] = dict(f, outcome="pending") return cid def finish(self, cid, **f): self.rows[cid].update(f) def access(*, s4=None, s3=None, sleeps=None): return DeviceAccess(s4_client_factory=lambda a, **kw: s4, s3_client_factory=lambda a, **kw: s3, start_reaper=False, audit=Audit(), sleep=(sleeps.append if sleeps is not None else (lambda s: None)), verify_timeout_s=60.0, verify_interval_s=5.0) def test_call_home_state_dispatches_by_series(): assert access(s4=FakeMicromateClient()).call_home_state(ADDR, "UM20147").series == 4 assert access(s3=FakeMiniMateClient()).call_home_state(TCP3, "BE18193").series == 3 def test_set_call_home_dispatches_by_series(): s4 = FakeMicromateClient() r = access(s4=s4).set_call_home(ADDR, "UM20147", enabled=False) assert (r.series, r.outcome, r.verified) == (4, "sent", "ok") and len(s4.executed) == 1 s3 = FakeMiniMateClient() r = access(s3=s3).set_call_home(TCP3, "BE18193", enabled=False) assert (r.series, r.outcome, r.verified) == (3, "sent", "ok") and s3.writes == ["call_home"] def test_the_verify_window_is_the_config_one_not_the_monitoring_one(): sleeps = [] s4 = FakeMicromateClient() s4.protocol.execute = lambda plan: [type("Ack", (), {"sub": st.ack_sub, "data": bytes(11)})() for st in plan.steps] # acked, but nothing changes r = access(s4=s4, sleeps=sleeps).set_call_home(ADDR, "UM20147", enabled=False) assert (r.outcome, r.verified) == ("failed", "mismatch") assert sleeps == [1.0, 1.0, 1.0] def test_set_call_home_on_a_dropped_held_line_is_not_retried(): fake = FakeMicromateClient(call_home=um12947_call_home(True)) da = access(s4=fake) da.status(ADDR, "UM20147") # hold the line fake.fail = [mm_proto.TimeoutError("peer gone")] # next call fails with pytest.raises(DeviceError): # the failure, not a retry da.set_call_home(ADDR, "UM20147", enabled=False) assert fake.executed == [] def test_a_dry_run_needs_no_audit(): da = DeviceAccess(s4_client_factory=lambda a, **kw: FakeMicromateClient(), start_reaper=False, audit=None, sleep=lambda s: None) assert da.set_call_home(ADDR, "UM20147", enabled=False, dry_run=True).outcome == "dry_run"