"""``get_radio_status``: each radio's configuration and whether it runs (NetOrk/netork#31). On 2026-07-16 a rejected UCI value kept every radio of the fleet in a netifd restart loop. The configuration said ``enabled``; only netifd's own state said otherwise: ``ubus call network.wireless status`` answers per radio with ``up``, ``pending`` and ``retry_setup_failed``. The driver reports those three as they are and leaves them out where ubus gives no answer; what they mean is netOrk's to say. ``WIRELESS_STATUS`` is the shape OpenWrt 23.05 prints, trimmed to the keys read: radio0 in the incident's state (setup given up), radio1 running. """ from __future__ import annotations import json from unittest.mock import MagicMock, patch from napalm_device_types import CommandResult from napalm_openwrt.openwrt import OpenWrtDriver UBUS = "ubus call network.wireless status" UCI = """\ wireless.radio0=wifi-device wireless.radio0.channel='1' wireless.radio0.band='2g' wireless.radio0.htmode='HT20' wireless.radio1=wifi-device wireless.radio1.channel='36' wireless.radio1.band='5g' wireless.radio1.htmode='VHT80' wireless.radio2=wifi-device wireless.radio2.band='6g' wireless.radio2.disabled='1' """ WIRELESS_STATUS = { "radio0": { "up": False, "pending": False, "autostart": True, "disabled": False, "retry_setup_failed": True, "config": {"channel": "1", "band": "2g"}, "interfaces": [], }, "radio1": { "up": True, "pending": False, "autostart": True, "disabled": False, "retry_setup_failed": False, "config": {"channel": "36", "band": "5g"}, "interfaces": [{"section": "default_radio1", "ifname": "phy1-ap0"}], }, "radio2": { "up": False, "pending": False, "autostart": True, "disabled": True, "retry_setup_failed": False, "interfaces": [], }, } def _driver(ubus: CommandResult) -> tuple[OpenWrtDriver, list[str]]: with patch("napalm_openwrt.openwrt.ConnectHandler"): drv = OpenWrtDriver(hostname="192.0.2.15", username="root", password="") drv.device = MagicMock() sent: list[str] = [] def send_command(command: str) -> str: return UCI if command == "uci show wireless" else "" def run_command(command: str, **_kw: object) -> CommandResult: sent.append(command) return ubus drv._send_command = send_command # type: ignore[method-assign] drv.run_command = run_command # type: ignore[method-assign] return drv, sent def _answer(status: dict) -> CommandResult: return CommandResult(json.dumps(status, indent="\t"), "", 0) def test_a_radio_that_gave_up_its_setup_says_so(): radios = _driver(_answer(WIRELESS_STATUS))[0].get_radio_status() assert radios["radio0"]["enabled"] is True assert (radios["radio0"]["up"], radios["radio0"]["pending"]) == (False, False) assert radios["radio0"]["setup_failed"] is True def test_a_running_radio_says_it_is_up(): radio = _driver(_answer(WIRELESS_STATUS))[0].get_radio_status()["radio1"] assert (radio["up"], radio["pending"], radio["setup_failed"]) == (True, False, False) def test_a_disabled_radio_keeps_its_configuration_and_its_state(): radio = _driver(_answer(WIRELESS_STATUS))[0].get_radio_status()["radio2"] assert radio["enabled"] is False assert radio["up"] is False def test_the_state_is_read_with_one_ubus_call(): drv, sent = _driver(_answer(WIRELESS_STATUS)) drv.get_radio_status() assert sent == [UBUS] def test_no_answer_from_ubus_leaves_the_state_out(): radios = _driver(CommandResult("", "Command failed: Not found\n", 4))[0].get_radio_status() assert set(radios) == {"radio0", "radio1", "radio2"} assert not any({"up", "pending", "setup_failed"} & set(r) for r in radios.values()) def test_output_that_is_no_json_leaves_the_state_out(): radios = _driver(CommandResult("Permission denied\n", "", 0))[0].get_radio_status() assert "up" not in radios["radio0"] def test_a_key_ubus_does_not_give_is_left_out_not_guessed(): # Before OpenWrt 19.07 netifd had no retry_setup_failed. old = {"radio0": {"up": False, "pending": True}} radio = _driver(_answer(old))[0].get_radio_status()["radio0"] assert (radio["up"], radio["pending"]) == (False, True) assert "setup_failed" not in radio assert "up" not in _driver(_answer(old))[0].get_radio_status()["radio1"] def test_a_value_that_is_no_boolean_is_left_out(): odd = {"radio0": {"up": "yes", "pending": None, "retry_setup_failed": 1}} radio = _driver(_answer(odd))[0].get_radio_status()["radio0"] assert not {"up", "pending", "setup_failed"} & set(radio) def test_a_radio_only_ubus_knows_is_not_added(): extra = {**WIRELESS_STATUS, "radio9": {"up": True}} radios = _driver(_answer(extra))[0].get_radio_status() assert "radio9" not in radios def test_a_call_that_breaks_off_keeps_the_configuration(): """Like the iwinfo read: the configuration was read, a lost runtime state loses nothing else.""" drv, _ = _driver(_answer(WIRELESS_STATUS)) def broken(command: str, **_kw: object) -> CommandResult: raise TimeoutError("channel closed") drv.run_command = broken # type: ignore[method-assign] radios = drv.get_radio_status() assert radios["radio0"]["enabled"] is True assert "up" not in radios["radio0"]