feat(power): shutdown_host through HostShutdownMixin, 0.25.0 (NetOrk/netork#2841)
The host's clean shutdown next to reboot_host: /sbin/poweroff as root, detached and two seconds late like the restart, so the exit status comes back before the session drops. A refusal raises RuntimeError with what the host said, plus the sudo password hint where sudo wanted one. poweroff rather than shutdown -h now: systemd links it to systemctl poweroff, sysvinit and BusyBox ship it themselves, while BusyBox has no shutdown and sysvinit's -h may only halt.
This commit is contained in:
1 parent
a8c57a61e4
commit
f876ac4598
5 files changed
+83
-8
No files matched your search
@@ -1896,6 +1896,51 @@ class TestRebootHost:
|
||||
driver.reboot_host()
|
||||
|
||||
|
||||
class TestShutdownHost:
|
||||
"""``shutdown_host`` (napalm-device-types' ``HostShutdownMixin``) powers the host off.
|
||||
|
||||
netOrk offers a clean shutdown where the driver defines ``shutdown_host``
|
||||
(NetOrk/netork#2841). Like the restart it is detached and a moment late, so
|
||||
the launcher's exit status comes back before the host goes down.
|
||||
"""
|
||||
|
||||
def test_the_driver_can_shut_its_host_down(self):
|
||||
assert callable(getattr(LinuxDriver, "shutdown_host", None))
|
||||
|
||||
def test_the_shutdown_is_detached_and_privileged(self, driver):
|
||||
driver._root = False
|
||||
driver._device.send_command.return_value = "\n__NETORK_RC=0"
|
||||
|
||||
driver.shutdown_host()
|
||||
|
||||
sent = driver._device.send_command.call_args[0][0]
|
||||
assert sent.startswith("sudo -n sh -c ")
|
||||
assert "/sbin/poweroff" in sent and "trap" in sent and "&" in sent
|
||||
assert "reboot" not in sent
|
||||
assert sent.endswith("echo __NETORK_RC=$?")
|
||||
|
||||
def test_a_refusal_is_raised_with_what_the_host_said(self, driver):
|
||||
driver._root = False
|
||||
driver._device.send_command.return_value = "sudo: a password is required\n__NETORK_RC=1"
|
||||
|
||||
with pytest.raises(RuntimeError, match="password is required"):
|
||||
driver.shutdown_host()
|
||||
|
||||
def test_a_nonzero_status_without_output_names_the_status(self, driver):
|
||||
driver._root = True
|
||||
driver._device.send_command.return_value = "__NETORK_RC=127"
|
||||
|
||||
with pytest.raises(RuntimeError, match="status 127"):
|
||||
driver.shutdown_host()
|
||||
|
||||
def test_no_exit_status_is_no_success(self, driver):
|
||||
driver._root = True
|
||||
driver._device.send_command.return_value = "something else"
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
driver.shutdown_host()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Listening sockets: ss and the processes' cgroups, as root when it can
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user