diff --git a/sfm/device_access/access.py b/sfm/device_access/access.py index 84f7755..72e2c34 100644 --- a/sfm/device_access/access.py +++ b/sfm/device_access/access.py @@ -91,6 +91,8 @@ class DeviceAccess: even_if_already=even_if_already, caller=caller) def _change_monitoring(self, address, serial, *, start, dry_run, even_if_already, caller): + serial = normalise_serial(serial) + series_for_serial(serial) # BadRequest (422) before the audit check if not dry_run and self._audit is None: raise NotSupported("no audit log is configured -- SFM refuses to change a unit " "without one (nothing sent)") diff --git a/sfm/device_access/s4.py b/sfm/device_access/s4.py index 837b0b8..0534015 100644 --- a/sfm/device_access/s4.py +++ b/sfm/device_access/s4.py @@ -366,7 +366,7 @@ def s4_change_monitoring(client: Any, serial: str, *, start: bool, dry_run: bool try: acks = client.protocol.execute(plan) - except (OpcodeRefused, TypeError) as exc: # refused by the gate: nothing went out + except OpcodeRefused as exc: # refused by the gate: nothing went out audit.finish(cid, outcome="failed", verified="not_checked", ack=None, error=f"refused before sending: {exc}") raise diff --git a/tests/test_device_access_change.py b/tests/test_device_access_change.py index 321146b..8d5ccd8 100644 --- a/tests/test_device_access_change.py +++ b/tests/test_device_access_change.py @@ -177,6 +177,7 @@ def test_calling_home_seen_while_polling_releases_the_line(): with pytest.raises(CallHomeInProgress): da.start_monitoring(ADDR, "UM20147") assert len(fake.executed) == 1 + assert fake.calls.count("read_run_state") == 2 # the read before + the one poll assert fake.is_open() is False and ADDR.key not in da._sessions (row,) = da._audit.rows.values() assert (row["outcome"], row["verified"]) == ("sent", "not_checked") @@ -247,3 +248,38 @@ def test_bad_verify_timings_are_rejected(): DeviceAccess(start_reaper=False, verify_interval_s=0) with pytest.raises(ValueError): DeviceAccess(start_reaper=False, verify_interval_s=5, verify_timeout_s=2) + + +def test_a_ring_on_the_follow_up_read_after_a_lost_ack_finishes_the_row(): + fake = FakeMicromateClient(run_states=[0x00], execute_error=mm_proto.TimeoutError("no ack")) + da = access(fake) + orig, n = fake.read_run_state, [] + + def ringing(): + n.append(1) + if len(n) == 2: # the first follow-up read + raise RingDetected("RADIO RING") + return orig() + fake.read_run_state = ringing + with pytest.raises(CallHomeInProgress): + da.start_monitoring(ADDR, "UM20147") + assert len(fake.executed) == 1 + (row,) = da._audit.rows.values() + assert (row["outcome"], row["verified"]) == ("failed", "not_checked") + assert "a call-home began" in row["error"] + assert fake.is_open() is False and ADDR.key not in da._sessions + + +def test_a_type_error_after_sending_is_recorded_as_no_clean_ack(): + fake = FakeMicromateClient(run_states=[0x00, 0x02], execute_error=TypeError("parser bug")) + da = access(fake) + r = da.start_monitoring(ADDR, "UM20147") + assert len(fake.executed) == 1 and r.sent is True + assert "no clean ack" in da._audit.rows[r.audit_id]["error"] + assert "refused before sending" not in da._audit.rows[r.audit_id]["error"] + + +def test_a_malformed_serial_is_422_even_without_an_audit_sink(): + from sfm.device_access.errors import BadRequest + with pytest.raises(BadRequest): + access(FakeMicromateClient(), audit=None).start_monitoring(ADDR, "XX123")