fix(ping): read BusyBox's ping and IPv6 replies
ping() only knew the iputils summary. BusyBox, the ping on Alpine,
writes "2 packets received", so every target came back as an error and
the ping sweep counted it as down. Its probe lines ("seq=") and every
IPv6 reply were dropped by an IPv4-only per-probe pattern, and its
"round-trip min/avg/max" line without a deviation was not read.
The parse moves to napalm_linux/_ping.py and accepts both summaries
(also iputils' with "+N errors"), both RTT lines, IPv6 and named
replies. The shared Unix ping parser is still open as
NAPALM/napalm-device-types#12.
Closes #15
This commit is contained in:
1 parent
024f68c543
commit
69f1701ea6
5 files changed
+149
-41
No files matched your search
@@ -14,7 +14,6 @@
|
||||
"napalm_linux/linux.py::LinuxDriver.get_networks": 52,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_route_to": 138,
|
||||
"napalm_linux/linux.py::LinuxDriver.get_vpn_tunnels": 152,
|
||||
"napalm_linux/linux.py::LinuxDriver.ping": 52,
|
||||
"napalm_linux/linux.py::LinuxDriver.search_packages": 106,
|
||||
"napalm_linux/linux.py::LinuxDriver.uninstall_package": 51
|
||||
},
|
||||
@@ -22,7 +21,7 @@
|
||||
"napalm_linux.linux"
|
||||
],
|
||||
"python_file_lines": {
|
||||
"napalm_linux/linux.py": 2213
|
||||
"napalm_linux/linux.py": 2176
|
||||
},
|
||||
"untyped_public": []
|
||||
}
|
||||
@@ -423,6 +423,99 @@ def test_ping_parses_output(driver):
|
||||
assert result["success"]["rtt_avg"] == pytest.approx(12.1)
|
||||
|
||||
|
||||
#: BusyBox ping (Alpine 3.22), IPv4: "packets received", "seq=", no mdev (#15).
|
||||
PING_BUSYBOX = """\
|
||||
PING 192.0.2.1 (192.0.2.1): 100 data bytes
|
||||
108 bytes from 192.0.2.1: seq=0 ttl=64 time=0.412 ms
|
||||
108 bytes from 192.0.2.1: seq=1 ttl=64 time=0.388 ms
|
||||
|
||||
--- 192.0.2.1 ping statistics ---
|
||||
2 packets transmitted, 2 packets received, 0% packet loss
|
||||
round-trip min/avg/max = 0.388/0.400/0.412 ms
|
||||
"""
|
||||
|
||||
#: BusyBox ping, IPv6 link-local.
|
||||
PING_BUSYBOX_V6 = """\
|
||||
PING fe80::1%eth0 (fe80::1%eth0): 100 data bytes
|
||||
108 bytes from fe80::1%eth0: seq=0 ttl=64 time=0.511 ms
|
||||
|
||||
--- fe80::1%eth0 ping statistics ---
|
||||
2 packets transmitted, 1 packets received, 50% packet loss
|
||||
round-trip min/avg/max = 0.511/0.511/0.511 ms
|
||||
"""
|
||||
|
||||
#: iputils ping (Debian 13), IPv6.
|
||||
PING_IPUTILS_V6 = """\
|
||||
PING 2001:db8::53 (2001:db8::53) 100 data bytes
|
||||
108 bytes from 2001:db8::53: icmp_seq=1 ttl=58 time=10.5 ms
|
||||
108 bytes from 2001:db8::53: icmp_seq=2 ttl=58 time=10.9 ms
|
||||
|
||||
--- 2001:db8::53 ping statistics ---
|
||||
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
|
||||
rtt min/avg/max/mdev = 10.500/10.700/10.900/0.200 ms
|
||||
"""
|
||||
|
||||
#: iputils ping to a name: the reply names the host, the address in brackets;
|
||||
#: unreachable probes add an "errors" field to the summary.
|
||||
PING_IPUTILS_NAME = """\
|
||||
PING gw.example.net (192.0.2.1) 100(128) bytes of data.
|
||||
108 bytes from gw.example.net (192.0.2.1): icmp_seq=1 ttl=64 time=0.301 ms
|
||||
From 192.0.2.254 icmp_seq=2 Destination Host Unreachable
|
||||
|
||||
--- gw.example.net ping statistics ---
|
||||
2 packets transmitted, 1 received, +1 errors, 50% packet loss, time 1001ms
|
||||
rtt min/avg/max/mdev = 0.301/0.301/0.301/0.000 ms
|
||||
"""
|
||||
|
||||
|
||||
class TestPingFormats:
|
||||
"""``ping`` reads BusyBox's and iputils' output, IPv4 and IPv6 (#15)."""
|
||||
|
||||
def _ping(self, driver, output):
|
||||
with patch.object(driver, "_send", return_value=output):
|
||||
return driver.ping("192.0.2.1", count=2)["success"]
|
||||
|
||||
def test_busybox_summary_and_rtt(self, driver):
|
||||
reply = self._ping(driver, PING_BUSYBOX)
|
||||
|
||||
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 0)
|
||||
assert (reply["rtt_min"], reply["rtt_avg"], reply["rtt_max"]) == (0.388, 0.4, 0.412)
|
||||
assert reply["rtt_stddev"] == 0.0
|
||||
|
||||
def test_busybox_probes(self, driver):
|
||||
reply = self._ping(driver, PING_BUSYBOX)
|
||||
|
||||
assert reply["results"] == [
|
||||
{"ip_address": "192.0.2.1", "rtt": 0.412},
|
||||
{"ip_address": "192.0.2.1", "rtt": 0.388},
|
||||
]
|
||||
|
||||
def test_busybox_ipv6(self, driver):
|
||||
reply = self._ping(driver, PING_BUSYBOX_V6)
|
||||
|
||||
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1)
|
||||
assert reply["results"] == [{"ip_address": "fe80::1%eth0", "rtt": 0.511}]
|
||||
|
||||
def test_iputils_ipv6(self, driver):
|
||||
reply = self._ping(driver, PING_IPUTILS_V6)
|
||||
|
||||
assert reply["results"] == [
|
||||
{"ip_address": "2001:db8::53", "rtt": 10.5},
|
||||
{"ip_address": "2001:db8::53", "rtt": 10.9},
|
||||
]
|
||||
assert (reply["rtt_avg"], reply["rtt_stddev"]) == (10.7, 0.2)
|
||||
|
||||
def test_iputils_to_a_name_with_errors(self, driver):
|
||||
reply = self._ping(driver, PING_IPUTILS_NAME)
|
||||
|
||||
assert (reply["probes_sent"], reply["packet_loss"]) == (2, 1)
|
||||
assert reply["results"] == [{"ip_address": "192.0.2.1", "rtt": 0.301}]
|
||||
|
||||
def test_no_summary_is_an_error(self, driver):
|
||||
with patch.object(driver, "_send", return_value="ping: unknown host nosuch"):
|
||||
assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# apply_updates – input validation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user