Merge remote-tracking branch 'origin/main' into fix/lldp-keyvalue
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 20s
driver / types (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m6s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Successful in 2m21s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 20s
driver / types (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m6s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m3s
CI / driver (pull_request) Successful in 2m21s
driver / publish (pull_request) Skipped
# Conflicts: # CHANGELOG.md # tests/quality_baseline.json
This commit is contained in:
commit
c7317925ed
8 files changed
+614
-40
No files matched your search
@@ -4,7 +4,7 @@
|
||||
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 24,
|
||||
"napalm_linux/linux.py::LinuxDriver._parse_cron_line": 12,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_networks": 13,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 14,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 13,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_snmp_config": 13,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 28,
|
||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 26
|
||||
@@ -12,7 +12,7 @@
|
||||
"function_lines": {
|
||||
"napalm_linux/linux.py::LinuxDriver._collect_platform_info": 106,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_networks": 52,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 103,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 101,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152,
|
||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 106,
|
||||
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
|
||||
@@ -21,7 +21,7 @@
|
||||
"napalm_linux.linux"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_linux/linux.py": 2122
|
||||
"napalm_linux/linux.py": 2118
|
||||
},
|
||||
"untyped_public": []
|
||||
}
|
||||
+263
-2
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from napalm_linux._routes import IpRoute, parse_ip_route
|
||||
from napalm_linux.linux import (
|
||||
_YUM_SECURITY_COMMAND,
|
||||
LinuxDriver,
|
||||
@@ -516,6 +517,48 @@ class TestPingFormats:
|
||||
assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"}
|
||||
|
||||
|
||||
class TestPingArguments:
|
||||
"""``ping`` types its arguments into a shell: each one stays one word (#55)."""
|
||||
|
||||
def _sent(self, driver, *args, **kwargs):
|
||||
with patch.object(driver, "_send", return_value=PING_OUTPUT) as send:
|
||||
driver.ping(*args, **kwargs)
|
||||
return send.call_args[0][0]
|
||||
|
||||
def test_the_destination_is_one_quoted_word(self, driver):
|
||||
sent = self._sent(driver, "1.2.3.4; id")
|
||||
|
||||
assert sent == "ping -c 5 -W 2 -s 100 -t 255 '1.2.3.4; id' 2>&1"
|
||||
|
||||
def test_the_source_is_one_quoted_word(self, driver):
|
||||
sent = self._sent(driver, "192.0.2.1", source="$(id)")
|
||||
|
||||
assert sent == "ping -c 5 -W 2 -s 100 -t 255 -I '$(id)' 192.0.2.1 2>&1"
|
||||
|
||||
def test_the_numbers_are_numbers(self, driver):
|
||||
sent = self._sent(driver, "192.0.2.1", count="3", timeout=1.0, size="56", ttl=64)
|
||||
|
||||
assert sent == "ping -c 3 -W 1 -s 56 -t 64 192.0.2.1 2>&1"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("destination", "source"), [("-f", ""), ("192.0.2.1", "-f"), ("-p ff", "")]
|
||||
)
|
||||
def test_an_option_in_place_of_a_name_is_refused_before_anything_is_sent(
|
||||
self, driver, destination, source
|
||||
):
|
||||
"""Quoting keeps a word one word, and ping still reads ``-f`` as its option (#63)."""
|
||||
with patch.object(driver, "_send") as send, pytest.raises(ValueError, match="option"):
|
||||
driver.ping(destination, source=source)
|
||||
|
||||
send.assert_not_called()
|
||||
|
||||
def test_a_number_that_is_not_one_is_refused_before_anything_is_sent(self, driver):
|
||||
with patch.object(driver, "_send") as send, pytest.raises(ValueError):
|
||||
driver.ping("192.0.2.1", count="5; id")
|
||||
|
||||
send.assert_not_called()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# apply_updates – input validation
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1571,7 +1614,7 @@ class TestManageService:
|
||||
|
||||
driver.manage_service("cron", "enable")
|
||||
|
||||
assert self._sent(driver)[1].startswith("sudo -n timeout 45 systemctl")
|
||||
assert self._sent(driver)[1].startswith("sudo -n sh -c 'timeout 45 systemctl")
|
||||
|
||||
def test_a_missing_sudo_password_is_explained(self, driver):
|
||||
driver._device.send_command.side_effect = [
|
||||
@@ -1802,7 +1845,7 @@ class TestRefreshAvailableUpdates:
|
||||
result = driver.refresh_available_updates()
|
||||
|
||||
assert result["success"] is False
|
||||
assert self._sent(driver)[1].startswith("sudo -n apt-get update")
|
||||
assert self._sent(driver)[1].startswith("sudo -n sh -c 'apt-get update")
|
||||
|
||||
def test_dnf_refreshes_its_metadata(self, driver):
|
||||
driver._pkg_manager = "dnf"
|
||||
@@ -2311,3 +2354,221 @@ class TestRouteFamilies:
|
||||
)
|
||||
|
||||
assert "2001:db8:2::7/128" in routes
|
||||
|
||||
|
||||
#: ``ip -4 route show`` / ``ip -6 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint
|
||||
#: 22.3), recorded in a network namespace set up for it: a multipath default per
|
||||
#: family, and unreachable, prohibit, throw and blackhole routes (#57). Documentation
|
||||
#: addresses only. The trailing blanks and the tabs are ip's.
|
||||
_IP4_ROUTE_TYPES = (
|
||||
"default proto static metric 100 \n"
|
||||
"\tnexthop via 192.0.2.1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 198.51.100.1 dev eth1 weight 1 \n"
|
||||
"unreachable 10.0.0.0/8 metric 1024 \n"
|
||||
"prohibit 172.16.0.0/12 \n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||
"throw 192.168.0.0/16 \n"
|
||||
"198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.10 \n"
|
||||
"blackhole 203.0.113.0/24 proto static \n"
|
||||
"203.0.113.7 via 192.0.2.1 dev eth0 \n"
|
||||
)
|
||||
_IP6_ROUTE_TYPES = (
|
||||
"2001:db8:1::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"2001:db8:2::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"blackhole 2001:db8:beef::/48 dev lo metric 1024 pref medium\n"
|
||||
"unreachable 2001:db8:dead::/48 dev lo metric 1024 pref medium\n"
|
||||
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
|
||||
"fe80::/64 dev eth1 proto kernel metric 256 pref medium\n"
|
||||
"default proto static metric 1024 pref medium\n"
|
||||
"\tnexthop via 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||
)
|
||||
|
||||
|
||||
class TestRouteTypesAndMultipath:
|
||||
"""A route type in front of the destination is the type, and a multipath
|
||||
route's ``nexthop`` lines are its next hops (#57)."""
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
if "ip -4 route" in command:
|
||||
return _IP4_ROUTE_TYPES
|
||||
if "ip -6 route" in command:
|
||||
return _IP6_ROUTE_TYPES
|
||||
return ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_every_destination_is_an_address(self, routes):
|
||||
assert sorted(routes) == sorted(
|
||||
[
|
||||
"0.0.0.0/0",
|
||||
"10.0.0.0/8",
|
||||
"172.16.0.0/12",
|
||||
"192.0.2.0/24",
|
||||
"192.168.0.0/16",
|
||||
"198.51.100.0/24",
|
||||
"203.0.113.0/24",
|
||||
"203.0.113.7/32",
|
||||
"2001:db8:1::/64",
|
||||
"2001:db8:2::/64",
|
||||
"2001:db8:beef::/48",
|
||||
"2001:db8:dead::/48",
|
||||
"fe80::/64",
|
||||
"::/0",
|
||||
]
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "route_type", "metric"),
|
||||
[
|
||||
("10.0.0.0/8", "unreachable", 1024),
|
||||
("172.16.0.0/12", "prohibit", 0),
|
||||
("192.168.0.0/16", "throw", 0),
|
||||
("203.0.113.0/24", "blackhole", 0),
|
||||
("2001:db8:beef::/48", "blackhole", 1024),
|
||||
("2001:db8:dead::/48", "unreachable", 1024),
|
||||
],
|
||||
)
|
||||
def test_a_route_that_forwards_nothing_says_its_type(self, routes, network, route_type, metric):
|
||||
(entry,) = routes[network]
|
||||
|
||||
assert entry["protocol_attributes"] == {"type": route_type}
|
||||
assert entry["preference"] == metric
|
||||
# Nothing leaves through an interface; IPv6 names lo for it.
|
||||
assert (entry["next_hop"], entry["outgoing_interface"]) == ("", "")
|
||||
|
||||
def test_the_rest_of_a_typed_line_is_read_as_before(self, routes):
|
||||
assert routes["203.0.113.0/24"][0]["protocol"] == "static"
|
||||
|
||||
def test_a_unicast_route_has_no_type(self, routes):
|
||||
assert routes["192.0.2.0/24"][0]["protocol_attributes"] == {}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("network", "hops"),
|
||||
[
|
||||
("0.0.0.0/0", [("192.0.2.1", "eth0"), ("198.51.100.1", "eth1")]),
|
||||
("::/0", [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]),
|
||||
],
|
||||
)
|
||||
def test_a_multipath_route_has_one_entry_per_next_hop(self, routes, network, hops):
|
||||
entries = routes[network]
|
||||
|
||||
assert [(e["next_hop"], e["outgoing_interface"]) for e in entries] == hops
|
||||
assert {(e["protocol"], e["preference"]) for e in entries} == {
|
||||
("static", 100 if network == "0.0.0.0/0" else 1024)
|
||||
}
|
||||
|
||||
def test_no_next_hop_line_is_a_route_of_its_own(self, routes):
|
||||
assert not any(network.startswith("nexthop") for network in routes)
|
||||
|
||||
|
||||
class TestParseIpRoute:
|
||||
"""``parse_ip_route`` line by line (#57)."""
|
||||
|
||||
def test_a_typed_route_that_forwards_keeps_its_interface(self):
|
||||
# ``ip route show table local``: a local route names the interface it is on.
|
||||
line = "local 192.0.2.10 dev eth0 proto kernel scope host src 192.0.2.10\n"
|
||||
|
||||
assert list(parse_ip_route(line, "ipv4")) == [
|
||||
IpRoute("192.0.2.10/32", "kernel", "", "eth0", 0, "local")
|
||||
]
|
||||
|
||||
def test_a_next_hop_line_without_a_route_is_dropped(self):
|
||||
out = "\tnexthop via 192.0.2.1 dev eth0 weight 1\n192.0.2.0/24 dev eth0 proto kernel\n"
|
||||
|
||||
assert [r.network for r in parse_ip_route(out, "ipv4")] == ["192.0.2.0/24"]
|
||||
|
||||
def test_a_route_without_next_hop_lines_is_one_route(self):
|
||||
out = "default via 192.0.2.1 dev eth0 proto dhcp metric 100\n"
|
||||
|
||||
assert list(parse_ip_route(out, "ipv4")) == [
|
||||
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100)
|
||||
]
|
||||
|
||||
|
||||
class TestFrrHostRoutes:
|
||||
"""The FRR enrichment completes a prefix without a length by its family (#64)."""
|
||||
|
||||
# FRR installs its routes as "proto zebra"; vtysh names the real source.
|
||||
_IP6 = "2001:db8:2::7 via fe80::1 dev eth0 proto zebra metric 20 pref medium\n"
|
||||
_VTYSH = (
|
||||
"O>* 2001:db8:2::7 [110/20] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||
"O>* 2001:db8:3::9 [110/30] via fe80::1, eth0, weight 1, 00:01:02\n"
|
||||
"O>* 10.10.0.7 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||
"O>* 10.10.1.0/24 [110/20] via 10.255.255.2, wg0, weight 1, 00:01:02\n"
|
||||
)
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
if command.startswith("command -v vtysh"):
|
||||
return "/usr/bin/vtysh"
|
||||
if command.startswith("vtysh"):
|
||||
return self._VTYSH
|
||||
if "ip -6 route" in command:
|
||||
return self._IP6
|
||||
return ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
def test_an_ipv6_host_route_is_a_128(self, routes):
|
||||
assert "2001:db8:3::9/128" in routes
|
||||
assert not any(
|
||||
network.endswith("::7/32") or network.endswith("::9/32") for network in routes
|
||||
)
|
||||
|
||||
def test_it_meets_the_kernel_route_of_the_same_host(self, routes):
|
||||
assert [e["protocol"] for e in routes["2001:db8:2::7/128"]] == ["ospf"]
|
||||
|
||||
def test_an_ipv4_host_route_stays_a_32(self, routes):
|
||||
assert "10.10.0.7/32" in routes
|
||||
|
||||
def test_a_prefix_with_a_length_keeps_it(self, routes):
|
||||
assert "10.10.1.0/24" in routes
|
||||
|
||||
|
||||
#: ``ip -4 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint 22.3), recorded in a
|
||||
#: network namespace set up for it: IPv4 routes with IPv6 next hops (RFC 8950), single
|
||||
#: and multipath, as BGP unnumbered installs them (#65). Documentation addresses only.
|
||||
_IP4_VIA_INET6 = (
|
||||
"default via inet6 2001:db8:1::1 dev eth0 \n"
|
||||
"10.20.0.0/16 proto bgp metric 20 \n"
|
||||
"\tnexthop via inet6 2001:db8:1::1 dev eth0 weight 1 \n"
|
||||
"\tnexthop via inet6 2001:db8:2::1 dev eth1 weight 1 \n"
|
||||
"10.40.0.0/16 via inet6 2001:db8:1::1 dev eth0 proto bgp metric 20 \n"
|
||||
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
|
||||
)
|
||||
|
||||
|
||||
class TestIpv6NextHops:
|
||||
"""``via inet6 <address>``: the next hop is the address (#65)."""
|
||||
|
||||
@pytest.fixture()
|
||||
def routes(self, driver):
|
||||
def send(command, **_kw):
|
||||
return _IP4_VIA_INET6 if "ip -4 route" in command else ""
|
||||
|
||||
with patch.object(driver, "_send", side_effect=send):
|
||||
return driver.get_route_to()
|
||||
|
||||
@pytest.mark.parametrize("network", ["0.0.0.0/0", "10.40.0.0/16"])
|
||||
def test_the_next_hop_is_the_address(self, routes, network):
|
||||
assert [e["next_hop"] for e in routes[network]] == ["2001:db8:1::1"]
|
||||
|
||||
def test_a_multipath_route_has_each_address(self, routes):
|
||||
hops = [(e["next_hop"], e["outgoing_interface"]) for e in routes["10.20.0.0/16"]]
|
||||
|
||||
assert hops == [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]
|
||||
|
||||
def test_the_route_stays_ipv4(self, routes):
|
||||
"""The family is the destination's; an IPv6 next hop does not change it."""
|
||||
assert {e["family"] for entries in routes.values() for e in entries} == {"ipv4"}
|
||||
|
||||
def test_a_family_word_for_ipv4_is_skipped_too(self):
|
||||
out = "10.50.0.0/16 via inet 192.0.2.1 dev eth0\n"
|
||||
|
||||
assert [r.nexthop for r in parse_ip_route(out, "ipv4")] == ["192.0.2.1"]
|
||||
@@ -0,0 +1,199 @@
|
||||
"""Without a sudo password the whole command runs as root, in one ``sh -c`` (#56).
|
||||
|
||||
A login that is not root and has no sudo password (NOPASSWD sudo, e.g. netOrk's
|
||||
own account from NetOrk/netork#1817) types its privileged commands into the
|
||||
interactive shell. ``sudo -n <command>`` covered only the first simple command of
|
||||
the line, and a command that starts with a brace group did not even parse:
|
||||
``sudo -n { LC_ALL=C dnf …; echo __DNF_RC=$?; } | cat`` is a syntax error, so dnf's
|
||||
and yum's security advisories were always unknown.
|
||||
|
||||
The command now goes to ``sudo -n sh -c`` as one argument, as ``_as_root`` does.
|
||||
A status echo at its end (``; echo __NETORK_RC=$?``) stays outside: it reports
|
||||
sh's status, which is the command's, and still arrives when sudo refuses.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from shlex import quote
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import DNF_SECURITY_COMMAND
|
||||
|
||||
from napalm_linux import LinuxDriver
|
||||
from napalm_linux.linux import _YUM_SECURITY_COMMAND
|
||||
|
||||
BASH = shutil.which("bash")
|
||||
needs_bash = pytest.mark.skipif(BASH is None, reason="needs bash")
|
||||
|
||||
#: dnf's answer on a Fedora 40 host with one pending security advisory.
|
||||
_ADVISORY = "FEDORA-2024-1a2b3c4d5e Important/Sec. openssl-libs-1:3.1.4-2.fc40.x86_64"
|
||||
|
||||
|
||||
def _driver(pkg_manager: str = "dnf", answers: list[str] | None = None) -> LinuxDriver:
|
||||
"""A driver logged in as a user that is not root, without a sudo password."""
|
||||
d = LinuxDriver.__new__(LinuxDriver)
|
||||
d.hostname = "testhost"
|
||||
d._sudo_password = None
|
||||
d._pkg_manager = pkg_manager
|
||||
d._root = False
|
||||
d._device = MagicMock()
|
||||
d._device.send_command.side_effect = answers or []
|
||||
return d
|
||||
|
||||
|
||||
def _sent(driver: LinuxDriver) -> list[str]:
|
||||
return [c[0][0] for c in driver._device.send_command.call_args_list]
|
||||
|
||||
|
||||
def _bash_parses(line: str) -> str:
|
||||
"""bash's complaint about *line*, empty when it parses."""
|
||||
assert BASH is not None
|
||||
checked = subprocess.run( # noqa: S603 - a fixed interpreter, the line only parsed
|
||||
[BASH, "-n", "-c", line], capture_output=True, text=True, check=False
|
||||
)
|
||||
return checked.stderr
|
||||
|
||||
|
||||
class TestTheSecurityAdvisories:
|
||||
@needs_bash
|
||||
@pytest.mark.parametrize("manager", ["dnf", "yum"])
|
||||
def test_the_line_parses(self, manager):
|
||||
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
driver._rpm_security_names()
|
||||
|
||||
assert _bash_parses(_sent(driver)[0]) == ""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("manager", "command"), [("dnf", DNF_SECURITY_COMMAND), ("yum", _YUM_SECURITY_COMMAND)]
|
||||
)
|
||||
def test_all_of_it_runs_as_root_and_the_status_stays_outside(self, manager, command):
|
||||
driver = _driver(manager, [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
driver._rpm_security_names()
|
||||
|
||||
assert _sent(driver) == [f"sudo -n sh -c {quote(command)}; echo __NETORK_RC=$?"]
|
||||
|
||||
def test_the_advisories_are_read(self):
|
||||
driver = _driver("dnf", [f"{_ADVISORY}\n__DNF_RC=0\n__NETORK_RC=0"])
|
||||
|
||||
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||
|
||||
|
||||
#: Every caller of ``_run_privileged``, with an answer that satisfies it.
|
||||
_CALLERS = {
|
||||
"reboot_host": (lambda d: d.reboot_host(), ["__NETORK_RC=0"]),
|
||||
"refresh apt": (lambda d: d.refresh_available_updates(), ["__NETORK_RC=0"]),
|
||||
"check-update": (
|
||||
lambda d: d.get_available_updates(),
|
||||
["__NETORK_RC=0", "__DNF_RC=0\n__NETORK_RC=0"],
|
||||
),
|
||||
"manage_service": (lambda d: d.manage_service("wg-quick@wg0", "restart"), ["__SVC_RC=0"]),
|
||||
"listening sockets": (
|
||||
lambda d: d._run_listening_sockets_command("sh -c 'ss -H -ltn'", privileged=True),
|
||||
[""],
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@needs_bash
|
||||
@pytest.mark.parametrize("caller", sorted(_CALLERS))
|
||||
def test_every_privileged_line_parses_and_runs_whole_as_root(caller):
|
||||
call, answers = _CALLERS[caller]
|
||||
driver = _driver("apt" if caller == "refresh apt" else "dnf", answers)
|
||||
|
||||
call(driver)
|
||||
|
||||
for line in _sent(driver):
|
||||
assert line.startswith("sudo -n sh -c '")
|
||||
assert _bash_parses(line) == ""
|
||||
|
||||
|
||||
def test_the_service_status_stays_outside():
|
||||
driver = _driver(answers=["__SVC_RC=0"])
|
||||
|
||||
driver.manage_service("cron", "restart")
|
||||
|
||||
assert _sent(driver) == [
|
||||
"sudo -n sh -c 'timeout 45 systemctl --no-ask-password restart -- cron.service 2>&1';"
|
||||
" echo __SVC_RC=$?"
|
||||
]
|
||||
|
||||
|
||||
def test_a_command_without_a_status_echo_is_wrapped_whole():
|
||||
driver = _driver(answers=[""])
|
||||
|
||||
driver._run_privileged("ss -H -ltnp | sort")
|
||||
|
||||
assert _sent(driver) == ["sudo -n sh -c 'ss -H -ltnp | sort'"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The lines run in a real bash, with a stand-in sudo and package manager
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_SUDO_OK = '#!/bin/sh\n[ "$1" = -n ] && shift\nexec "$@"\n'
|
||||
_SUDO_REFUSES = "#!/bin/sh\necho 'sudo: a password is required' >&2\nexit 1\n"
|
||||
_DNF = f"#!/bin/sh\necho '{_ADVISORY}'\n"
|
||||
|
||||
|
||||
def _shell_host(tmp_path: Path, sudo: str) -> LinuxDriver:
|
||||
"""A driver whose interactive shell is a real bash with stand-ins on its PATH.
|
||||
|
||||
Like netmiko, it reads until the command's expect pattern matches and fails
|
||||
when it never does.
|
||||
"""
|
||||
for name, script in (("sudo", sudo), ("dnf", _DNF)):
|
||||
stub = tmp_path / name
|
||||
stub.write_text(script)
|
||||
stub.chmod(0o755)
|
||||
env = {"PATH": f"{tmp_path}:/usr/bin:/bin", "LC_ALL": "C"}
|
||||
|
||||
def send_command(command, *, expect_string, **_kwargs):
|
||||
assert BASH is not None
|
||||
ran = subprocess.run( # noqa: S603 - a fixed interpreter and stand-ins
|
||||
[BASH, "-c", command],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, # one stream, in order, as on the PTY
|
||||
text=True,
|
||||
env=env,
|
||||
check=False,
|
||||
)
|
||||
output = f"{ran.stdout}user@host:~$ "
|
||||
if not re.search(expect_string, output):
|
||||
raise TimeoutError(f"pattern not detected: {expect_string!r} in {output!r}")
|
||||
return output
|
||||
|
||||
driver = _driver("dnf")
|
||||
driver._device.send_command.side_effect = send_command
|
||||
return driver
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_real_shell_reads_the_advisories(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_OK)
|
||||
|
||||
assert driver._rpm_security_names() == {"openssl-libs"}
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_refused_sudo_still_reports_its_status(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||
|
||||
result = driver.refresh_available_updates()
|
||||
|
||||
assert result["success"] is False
|
||||
assert "a password is required" in result["output"]
|
||||
assert "NOPASSWD" in result["output"] # the hint
|
||||
|
||||
|
||||
@needs_bash
|
||||
def test_a_refused_sudo_leaves_the_advisories_unknown(tmp_path):
|
||||
driver = _shell_host(tmp_path, _SUDO_REFUSES)
|
||||
|
||||
assert driver._rpm_security_names() is None
|
||||
Reference in new issue
Block a user