Both drivers mix in napalm-device-types' ServicesRestartMixin. The command goes over the exec path, root's login as a rule, behind an id -u guard: a login other than root would see only its own processes and read as clean, so it prints nothing and the read raises. A running VM's QEMU is listed as a process; an LXC container's processes are left out by the parse, since a container updates itself. No stdin.
77 lines
2.9 KiB
Python
77 lines
2.9 KiB
Python
"""`get_services_need_restart`: the services on the node that still run replaced code.
|
|
|
|
An openssl or glibc update replaces the files on disk; every daemon that had them
|
|
loaded keeps the old code until it restarts (NetOrk/netork#2750). On a hypervisor that
|
|
is where a restart instead of a reboot matters most. The command and its parse are
|
|
napalm-device-types' (``ServicesRestartMixin``); the driver only carries the command
|
|
over its exec path, which runs as root. A login that is not root gets no reading: it
|
|
would see only its own processes and read as clean.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from napalm_device_types import ServicesRestartMixin
|
|
|
|
from napalm_proxmox import ProxmoxBackupDriver
|
|
from napalm_proxmox.driver import ProxmoxDriver
|
|
|
|
#: A PVE node after an openssl update, read by the /proc scan (no needrestart):
|
|
#: pveproxy and a running VM's QEMU still map the old library, and so does a
|
|
#: container's own nginx, which the container updates itself.
|
|
WIRE = (
|
|
"RST_BEGIN\n[maps]\n"
|
|
"2101 pveproxy worker\n"
|
|
"3100 kvm\n"
|
|
"4100 nginx\n"
|
|
"[cgroups]\n"
|
|
"2101 0::/system.slice/pveproxy.service\n"
|
|
"3100 0::/qemu.slice/101.scope\n"
|
|
"4100 0::/lxc/200/ns/system.slice/nginx.service\n"
|
|
"RST_END\n"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("driver_class", [ProxmoxDriver, ProxmoxBackupDriver])
|
|
def test_both_drivers_declare_the_contract(driver_class):
|
|
assert issubclass(driver_class, ServicesRestartMixin)
|
|
|
|
|
|
def test_it_reads_as_root_over_the_exec_path(driver):
|
|
with patch.object(driver, "_exec_ssh_command", return_value=WIRE) as exec_:
|
|
reading = driver.get_services_need_restart()
|
|
|
|
[command] = [c.args[0] for c in exec_.call_args_list]
|
|
assert command.startswith('[ "$(id -u)" = 0 ] && sh -c \'')
|
|
assert reading == {
|
|
"source": "maps",
|
|
"units": ["pveproxy"],
|
|
"processes": [{"pid": 3100, "name": "kvm"}],
|
|
}
|
|
|
|
|
|
def test_the_backup_server_reads_it_the_same_way():
|
|
drv = ProxmoxBackupDriver("192.0.2.80", "root", "", optional_args={"token_name": "a!b"})
|
|
wire = "RST_BEGIN\n[needrestart]\nNEEDRESTART-SVC: proxmox-backup-proxy.service\nRST_END\n"
|
|
|
|
with patch.object(drv, "_exec_ssh_command", return_value=wire):
|
|
reading = drv.get_services_need_restart()
|
|
|
|
assert reading["units"] == ["proxmox-backup-proxy"]
|
|
|
|
|
|
def test_a_login_other_than_root_gets_no_reading(driver):
|
|
"""The guard runs nothing but as root, so no report comes back and the parse raises."""
|
|
|
|
def as_this_user(command: str) -> str:
|
|
return subprocess.run(["sh", "-c", command], capture_output=True, text=True).stdout
|
|
|
|
if subprocess.run(["id", "-u"], capture_output=True, text=True).stdout.strip() == "0":
|
|
pytest.skip("runs as root here")
|
|
with patch.object(driver, "_exec_ssh_command", side_effect=as_this_user):
|
|
with pytest.raises(ValueError):
|
|
driver.get_services_need_restart()
|