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
This commit is contained in:
Christian Manivong committed 2026-11-01 21:43:56 +01:00
1 parent 45f5b65427
commit 16e0fa9d5e
3 files changed
+78 -3

No files matched your search

+55
View File
@@ -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 == []