Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
107 lines
5.3 KiB
Python
107 lines
5.3 KiB
Python
"""Failure evidence survives cleanup without granting hardware authority."""
|
|
|
|
import json
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "src"))
|
|
from runtime.diagnostics import failure
|
|
from runtime.serial import Attachment
|
|
from runtime.service import Device
|
|
|
|
|
|
class FailureTests(unittest.TestCase):
|
|
def test_original_transport_failure_and_bounded_preceding_calls(self):
|
|
error = OSError("Native query timed out or disconnected")
|
|
error.native_rpc = {"command": 31, "attachment_present": True,
|
|
"transport": {"request_emitted": True, "packets_received": 0}}
|
|
error.native_history = [{"command": i} for i in range(32)]
|
|
with self.assertLogs("missioncore.vesc.failure") as capture:
|
|
failure(error, "remote_control", session_id="a" * 32, release_confirmed=False)
|
|
record = json.loads(capture.records[0].getMessage())
|
|
self.assertEqual(record["native_rpc"], error.native_rpc)
|
|
self.assertEqual(record["native_history"], error.native_history[-8:])
|
|
self.assertIs(record["release_confirmed"], False)
|
|
self.assertEqual(record["session_id"], "a" * 32)
|
|
|
|
def test_native_startup_evidence_survives_cleanup(self):
|
|
error = OSError("Native VESC Tool exited")
|
|
error.native_startup = {"pid": 123, "exit_code": 1,
|
|
"stderr_tail": "synthetic missing runtime component",
|
|
"stderr_truncated": False}
|
|
with self.assertLogs("missioncore.vesc.failure") as capture:
|
|
failure(error, "connection")
|
|
record = json.loads(capture.records[0].getMessage())
|
|
self.assertEqual(record["native_startup"], error.native_startup)
|
|
error.native_startup = {"stderr_tail": "x" * 64000}
|
|
with self.assertLogs("missioncore.vesc.failure") as capture:
|
|
failure(error, "connection")
|
|
self.assertLess(len(capture.records[0].getMessage().encode()), 16000)
|
|
|
|
def test_journal_size_bound_preserves_summary(self):
|
|
error = OSError("x" * 20000)
|
|
error.native_rpc = {"unexpected": "x" * 32000}
|
|
error.native_history = [{"unexpected": "x" * 32000}] * 32
|
|
with self.assertLogs("missioncore.vesc.failure") as capture:
|
|
failure(error, "remote_control")
|
|
raw = capture.records[0].getMessage()
|
|
self.assertLess(len(raw.encode()), 16000)
|
|
record = json.loads(raw)
|
|
self.assertTrue(record["history_omitted"])
|
|
self.assertTrue(record["rpc_omitted"])
|
|
self.assertEqual(record["type"], "OSError")
|
|
|
|
def test_logging_failure_cannot_prevent_owner_cleanup(self):
|
|
with patch("runtime.diagnostics.LOGGER.warning",
|
|
side_effect=OSError("journal unavailable")):
|
|
failure(OSError("read failed"), "remote_control")
|
|
failure(OSError("read failed"), "connection", device=SimpleNamespace())
|
|
|
|
def test_connection_failures_remain_backed_off_and_rate_limited(self):
|
|
device = Device(Attachment("1-2", "3", "ttyACM0", "12"))
|
|
def factory(_):
|
|
raise OSError("Native VESC Tool did not connect")
|
|
with (patch("runtime.service.time.monotonic", return_value=100),
|
|
self.assertLogs("missioncore.vesc.failure") as capture):
|
|
device.connect(factory)
|
|
device.connect(factory)
|
|
self.assertEqual(len(capture.records), 1)
|
|
record = json.loads(capture.records[0].getMessage())
|
|
self.assertEqual(record["attachment"]["tty"], "ttyACM0")
|
|
self.assertIsNone(device.link)
|
|
self.assertEqual(device.retry_at, 104)
|
|
|
|
|
|
class ConnectionEvidenceTests(unittest.TestCase):
|
|
def test_detach_keeps_old_uuid_session_and_last_sample(self):
|
|
device = Device(Attachment("1-2", "3", "ttyACM0", "12"))
|
|
device.identity = {"uuid": "a"*24, "version": "5.02"}
|
|
device.link = SimpleNamespace(close=lambda: None)
|
|
device.publish_sample({"erpm": 0, "fault_code": 0}, None)
|
|
old_session = device.session
|
|
with self.assertLogs("missioncore.vesc.failure", level="INFO") as capture:
|
|
device.close()
|
|
data = json.loads(capture.records[0].getMessage())
|
|
self.assertEqual(data["transition"], "usb_detached")
|
|
self.assertEqual(data["session_id"], old_session)
|
|
self.assertEqual(data["identity"]["uuid"], "a"*24)
|
|
self.assertEqual(data["last_values"]["erpm"], 0)
|
|
self.assertIsNone(device.sample)
|
|
self.assertNotEqual(device.session, old_session)
|
|
|
|
def test_recovery_logs_first_sample_only_and_failure_keeps_old_session(self):
|
|
device = Device(Attachment("1-2", "3", "ttyACM0", "12"))
|
|
device.link = SimpleNamespace(close=lambda: None)
|
|
old = device.session
|
|
with self.assertLogs("missioncore.vesc.failure", level="INFO") as capture:
|
|
device.publish_sample({"erpm":0}, None)
|
|
device.publish_sample({"erpm":0}, None)
|
|
device.failed(OSError("USB went away"), "observation")
|
|
data = [json.loads(x.getMessage()) for x in capture.records]
|
|
self.assertEqual([x.get("transition") for x in data], ["sample_restored", "read_failed", None])
|
|
self.assertTrue(all(x["session_id"] == old for x in data))
|