From 16e0fa9d5ecc653c4cfadc3e033f5c114079b109 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:43:56 +0100 Subject: [PATCH] fix(services): only the exit status decides manage_service manage_service returned success for every action, so a failed start or a missing init script showed up as done. The command now prints its exit status after a marker, and only that status decides. Closes #3 --- CHANGELOG.md | 3 ++ napalm_openwrt/system_mixin.py | 23 ++++++++++++-- tests/unit/test_driver.py | 55 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index feb7de6..25e6730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ recorded here; their history is the git log. `ip neigh` nor `/proc/net/arp` has an age (NetOrk/netork#1327). - The driver no longer removes `port` from the `optional_args` dict it is given: the caller's dict stays as it was (#19). +- `manage_service()` reports a failed action or a missing init script as failed: + only the init script's exit status decides, where any action used to come back + as a success (#3). ## [0.7.0] - 2026-10-23 diff --git a/napalm_openwrt/system_mixin.py b/napalm_openwrt/system_mixin.py index 63b0b9b..a491345 100644 --- a/napalm_openwrt/system_mixin.py +++ b/napalm_openwrt/system_mixin.py @@ -32,6 +32,19 @@ def _split_status(raw: str) -> tuple[str, int | None]: return output, int(statuses[-1]) if statuses else None +def _status_result(raw: str, command: str) -> dict[str, Any]: + """``{"success", "output"}`` from what a command marked with ``_RC_MARKER`` printed. + + Only the exit status decides; output without a status is no success. + """ + output, status = _split_status(raw) + if status is None: + return {"success": False, "output": output or "No exit status came back from the device."} + if status: + return {"success": False, "output": output or f"{command} exited with status {status}."} + return {"success": True, "output": output} + + class OpenWrtSystemMixin: """Mixin providing system-level NAPALM getters. @@ -389,15 +402,19 @@ class OpenWrtSystemMixin: return result def manage_service(self, name: str, action: str) -> dict[str, Any]: - """Execute a lifecycle action (start/stop/restart/enable/disable) on a service.""" + """Execute a lifecycle action (start/stop/restart/enable/disable) on a service. + + Only the init script's exit status decides ``success``; a missing script + or a failed action comes back with what the device printed. + """ import re as _re if not _re.match(r"^[a-zA-Z0-9_\-]+$", name): raise ValueError(f"Invalid service name: {name!r}") if action not in ("start", "stop", "restart", "enable", "disable"): raise ValueError(f"Invalid action: {action!r}") - output = self._send_command(f"/etc/init.d/{name} {action} 2>&1") - return {"success": True, "output": output} + command = f"/etc/init.d/{name} {action}" + return _status_result(self._send_command(f"{command} 2>&1; echo {_RC_MARKER}$?"), command) def get_available_updates(self) -> list[dict[str, Any]]: """Return list of upgradable packages from the local package manager cache. diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py index 179f792..acaeeb0 100644 --- a/tests/unit/test_driver.py +++ b/tests/unit/test_driver.py @@ -1950,3 +1950,58 @@ class TestOptionalArgs: assert drv.port == 2222 assert optional_args == {"port": 2222, "conn_timeout": 15} + + +class TestManageService: + """Only the init script's exit status decides whether an action succeeded (#3).""" + + def _answer(self, driver, reply: str) -> list: + sent: list = [] + + def send(cmd, **kw): + sent.append(cmd) + return reply + + driver._send_command = send + return sent + + def test_a_clean_exit_is_a_success(self, driver): + sent = self._answer(driver, "__OWRT_RC=0\n") + + assert driver.manage_service("dnsmasq", "restart") == {"success": True, "output": ""} + assert sent[0].startswith("/etc/init.d/dnsmasq restart 2>&1") + + def test_a_failed_action_is_no_success(self, driver): + self._answer(driver, "Command failed: Not found\n__OWRT_RC=1\n") + + result = driver.manage_service("dnsmasq", "start") + + assert result == {"success": False, "output": "Command failed: Not found"} + + def test_a_missing_init_script_is_no_success(self, driver): + self._answer(driver, "ash: /etc/init.d/nope: not found\n__OWRT_RC=127\n") + + result = driver.manage_service("nope", "start") + + assert result["success"] is False + assert "not found" in result["output"] + + def test_a_silent_failure_names_the_status(self, driver): + self._answer(driver, "__OWRT_RC=1\n") + + result = driver.manage_service("lldpd", "enable") + + assert result["success"] is False + assert "status 1" in result["output"] + + def test_output_without_a_status_is_no_success(self, driver): + self._answer(driver, "") + + assert driver.manage_service("dnsmasq", "stop")["success"] is False + + def test_an_invalid_name_is_refused_before_anything_is_sent(self, driver): + sent = self._answer(driver, "__OWRT_RC=0\n") + + with pytest.raises(ValueError): + driver.manage_service("x; reboot", "start") + assert sent == []