No files matched your search
@@ -19,6 +19,9 @@ recorded here; their history is the git log.
|
|||||||
`ip neigh` nor `/proc/net/arp` has an age (NetOrk/netork#1327).
|
`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 driver no longer removes `port` from the `optional_args` dict it is given:
|
||||||
the caller's dict stays as it was (#19).
|
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
|
## [0.7.0] - 2026-10-23
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,19 @@ def _split_status(raw: str) -> tuple[str, int | None]:
|
|||||||
return output, int(statuses[-1]) if statuses else 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:
|
class OpenWrtSystemMixin:
|
||||||
"""Mixin providing system-level NAPALM getters.
|
"""Mixin providing system-level NAPALM getters.
|
||||||
|
|
||||||
@@ -389,15 +402,19 @@ class OpenWrtSystemMixin:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def manage_service(self, name: str, action: str) -> dict[str, Any]:
|
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
|
import re as _re
|
||||||
|
|
||||||
if not _re.match(r"^[a-zA-Z0-9_\-]+$", name):
|
if not _re.match(r"^[a-zA-Z0-9_\-]+$", name):
|
||||||
raise ValueError(f"Invalid service name: {name!r}")
|
raise ValueError(f"Invalid service name: {name!r}")
|
||||||
if action not in ("start", "stop", "restart", "enable", "disable"):
|
if action not in ("start", "stop", "restart", "enable", "disable"):
|
||||||
raise ValueError(f"Invalid action: {action!r}")
|
raise ValueError(f"Invalid action: {action!r}")
|
||||||
output = self._send_command(f"/etc/init.d/{name} {action} 2>&1")
|
command = f"/etc/init.d/{name} {action}"
|
||||||
return {"success": True, "output": output}
|
return _status_result(self._send_command(f"{command} 2>&1; echo {_RC_MARKER}$?"), command)
|
||||||
|
|
||||||
def get_available_updates(self) -> list[dict[str, Any]]:
|
def get_available_updates(self) -> list[dict[str, Any]]:
|
||||||
"""Return list of upgradable packages from the local package manager cache.
|
"""Return list of upgradable packages from the local package manager cache.
|
||||||
|
|||||||
@@ -1950,3 +1950,58 @@ class TestOptionalArgs:
|
|||||||
|
|
||||||
assert drv.port == 2222
|
assert drv.port == 2222
|
||||||
assert optional_args == {"port": 2222, "conn_timeout": 15}
|
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 == []
|
||||||
Reference in new issue
Block a user