"""The firmware OpenWrt's sysupgrade server offers, and installing it with owut. netOrk upgrades an OpenWrt device to the newest release of its own release line instead of piling package upgrades into the overlay (NetOrk/netork#2615). The offer comes from the attended sysupgrade server's ``overview.json`` (its shape here is the real one, cut down to what matters); owut on the device builds the image there and runs sysupgrade. """ from __future__ import annotations from unittest.mock import MagicMock, patch import pytest from napalm_device_types import CommandResult from napalm_openwrt import firmware_mixin from napalm_openwrt.openwrt import OpenWrtDriver OVERVIEW = { "latest": ["25.12.6", "24.10.8"], "branches": { "SNAPSHOT": { "enabled": True, "snapshot": True, "versions": ["SNAPSHOT"], "name": "SNAPSHOT", "targets": ["ath79/generic", "ramips/mt7621"], }, "25.12": { "enabled": True, "snapshot": False, "versions": ["25.12.6", "25.12.5", "25.12.2", "25.12.0", "25.12.0-rc5"], "name": "25.12", "targets": ["ath79/generic", "ramips/mt7621"], }, "24.10": { "enabled": True, "snapshot": False, "versions": ["24.10.8", "24.10.2", "24.10.0"], "name": "24.10", "targets": ["ath79/generic", "ramips/mt7621"], }, "23.05": { "enabled": True, "snapshot": False, "versions": ["23.05.6", "23.05.3", "23.05.0-rc4"], "name": "23.05", "targets": ["ath79/generic"], }, }, "upstream_url": "https://downloads.openwrt.org", } def _release(version: str, target: str = "ath79/generic") -> str: return ( "DISTRIB_ID='OpenWrt'\n" f"DISTRIB_RELEASE='{version}'\n" "DISTRIB_REVISION='r32802-f505120278'\n" f"DISTRIB_TARGET='{target}'\n" "DISTRIB_ARCH='mips_24kc'\n" f"DISTRIB_DESCRIPTION='OpenWrt {version} r32802-f505120278'\n" ) @pytest.fixture(autouse=True) def _no_cached_overviews(): firmware_mixin._OVERVIEWS.clear() yield firmware_mixin._OVERVIEWS.clear() @pytest.fixture def driver(): with patch("napalm_openwrt.openwrt.ConnectHandler"): drv = OpenWrtDriver(hostname="192.0.2.1", username="root", password="") drv.device = MagicMock() yield drv def _device(driver, version: str, *, owut: bool = True, server: str = "", target="ath79/generic"): """The device answers the offer's one reading; every command is kept.""" sent: list[str] = [] def run(command, *, privileged=False, timeout=60, stdin=None): sent.append(command) out = _release(version, target) + f"__ASU_URL={server}\n" + ("__OWUT=1\n" if owut else "") return CommandResult(exit_code=0, stdout=out, stderr="") driver.run_command = run return sent def _offer(driver, fetched=OVERVIEW): with patch.object(firmware_mixin, "_fetch_json", return_value=fetched) as fetch: return driver.get_firmware_offer(), fetch class TestFirmwareOffer: def test_the_newest_point_release_of_its_own_line_is_offered(self, driver): _device(driver, "25.12.2") offer, _ = _offer(driver) assert offer == { "current_version": "25.12.2", "latest_version": "25.12.6", "newer": True, "installable": True, } def test_a_device_on_the_newest_release_is_offered_nothing_newer(self, driver): _device(driver, "25.12.6") offer, _ = _offer(driver) assert offer is not None assert offer["newer"] is False assert offer["latest_version"] == "25.12.6" def test_a_later_release_line_is_named_but_not_offered_as_the_upgrade(self, driver): _device(driver, "24.10.2") offer, _ = _offer(driver) assert offer is not None assert offer["latest_version"] == "24.10.8" assert offer["latest_major_version"] == "25.12.6" assert offer["newer"] is True def test_a_release_candidate_is_offered_the_release(self, driver): _device(driver, "25.12.0-rc5") offer, _ = _offer(driver) assert offer is not None assert (offer["latest_version"], offer["newer"]) == ("25.12.6", True) def test_release_candidates_are_not_offered_over_a_release(self, driver): overview = { **OVERVIEW, "branches": { "25.12": {**OVERVIEW["branches"]["25.12"], "versions": ["25.12.7-rc1", "25.12.6"]} }, } _device(driver, "25.12.2") offer, _ = _offer(driver, overview) assert offer is not None assert offer["latest_version"] == "25.12.6" def test_without_owut_it_cannot_be_installed_yet(self, driver): _device(driver, "25.12.2", owut=False) offer, _ = _offer(driver) assert offer is not None assert offer["installable"] is False def test_a_release_before_owut_existed_never_says_installable(self, driver): """owut needs 24.10 or later; on 23.05 there is nothing to install it with.""" _device(driver, "23.05.3", owut=False) offer, _ = _offer(driver) assert offer is not None assert "installable" not in offer assert offer["latest_version"] == "23.05.6" def test_a_snapshot_has_no_offer(self, driver): _device(driver, "SNAPSHOT") assert _offer(driver)[0] is None def test_a_target_the_line_no_longer_builds_has_no_offer(self, driver): _device(driver, "23.05.3", target="ramips/mt7621") assert _offer(driver)[0] is None def test_a_server_that_does_not_answer_is_no_offer(self, driver): _device(driver, "25.12.2") with patch.object(firmware_mixin, "_fetch_json", side_effect=OSError("timed out")): assert driver.get_firmware_offer() is None def test_the_server_is_asked_once_for_every_device(self, driver): _device(driver, "25.12.2") _, fetch = _offer(driver) with patch.object(firmware_mixin, "_fetch_json", return_value=OVERVIEW) as again: assert driver.get_firmware_offer() is not None fetch.assert_called_once_with("https://sysupgrade.openwrt.org/json/v1/overview.json") again.assert_not_called() def test_a_failed_answer_is_not_asked_again_at_once(self, driver): _device(driver, "25.12.2") with patch.object(firmware_mixin, "_fetch_json", side_effect=OSError("down")) as fetch: driver.get_firmware_offer() driver.get_firmware_offer() fetch.assert_called_once() def test_the_server_the_device_is_set_to_is_asked(self, driver): _device(driver, "25.12.2", server="https://asu.example.net/") _, fetch = _offer(driver) fetch.assert_called_once_with("https://asu.example.net/json/v1/overview.json") def test_a_server_that_is_not_http_is_not_asked(self, driver): _device(driver, "25.12.2", server="file:///etc/shadow") _, fetch = _offer(driver) fetch.assert_called_once_with("https://sysupgrade.openwrt.org/json/v1/overview.json") class TestStartFirmwareUpgrade: def _run(self, driver, result: CommandResult): calls: list[tuple[str, bool]] = [] def run(command, *, privileged=False, timeout=60, stdin=None): calls.append((command, privileged)) return result driver.run_command = run return calls def test_owut_upgrades_to_the_version_detached_from_the_session(self, driver): calls = self._run(driver, CommandResult(exit_code=0, stdout="", stderr="")) driver.start_firmware_upgrade("25.12.6") ((command, privileged),) = calls assert privileged is True assert "owut upgrade --version-to 25.12.6" in command assert 'trap "" HUP' in command assert "