fix(ping): parse each reply into results
The probe pattern expected "from" after icmp_seq, but iputils prints
"bytes from <ip>: icmp_seq=... time=... ms", so results was always
empty. Replies by name ("from host (ip)") and over IPv6 are read too.
Closes #12
This commit is contained in:
1 parent
29ed59c8d1
commit
10cf788b86
3 files changed
+34
-3
No files matched your search
@@ -71,6 +71,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
logged as a warning and the password used (#17).
|
||||
- `destroy_vm()` returns once Proxmox has deleted the VM, not as soon as the
|
||||
delete was queued, and raises when the delete fails (#15).
|
||||
- `ping()` fills `results` with each reply's address and round-trip time; it
|
||||
was always empty (#12).
|
||||
|
||||
## [0.8.0] - 2026-10-23
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ import napalm.base.constants as C
|
||||
|
||||
_JsonDict = dict[str, Any]
|
||||
|
||||
#: One reply in iputils' ping output, by address or by name with the address
|
||||
#: in brackets: "64 bytes from dns.google (8.8.4.4): icmp_seq=1 ttl=118 time=10.5 ms".
|
||||
_PING_REPLY = re.compile(r"bytes from (?:\S+ \()?([0-9a-fA-F.:]+)\)?: .*?time=([\d.]+) ms")
|
||||
|
||||
|
||||
class ProxmoxRoutingMixin:
|
||||
"""Mixin providing routing NAPALM methods (get_route_to, ping, traceroute)."""
|
||||
@@ -90,9 +94,9 @@ class ProxmoxRoutingMixin:
|
||||
rtt_std = float(m_rtt.group(4)) if m_rtt else 0.0
|
||||
|
||||
# Individual probe lines
|
||||
probes = []
|
||||
for m_probe in re.finditer(r"icmp_seq=\d+.*?time=([\d.]+) ms.*?from ([\d.a-fA-F:]+)", raw):
|
||||
probes.append({"ip_address": m_probe.group(2), "rtt": float(m_probe.group(1))})
|
||||
probes = [
|
||||
{"ip_address": m.group(1), "rtt": float(m.group(2))} for m in _PING_REPLY.finditer(raw)
|
||||
]
|
||||
|
||||
return {
|
||||
"success": {
|
||||
|
||||
@@ -175,6 +175,31 @@ class TestPing:
|
||||
assert result["success"]["packet_loss"] == 0
|
||||
assert result["success"]["rtt_min"] == pytest.approx(10.5, abs=0.01)
|
||||
|
||||
def test_every_reply_is_a_result(self, driver):
|
||||
"""iputils puts "from <ip>" before icmp_seq (napalm-proxmox#12)."""
|
||||
driver._node_api().execute.post.return_value = {"data": self.PING_SUCCESS}
|
||||
result = driver.ping("8.8.8.8")
|
||||
assert result["success"]["results"] == [
|
||||
{"ip_address": "8.8.8.8", "rtt": 10.5},
|
||||
{"ip_address": "8.8.8.8", "rtt": 11.2},
|
||||
]
|
||||
|
||||
def test_a_reply_by_name_and_over_ipv6(self, driver):
|
||||
output = (
|
||||
"PING dns.google (8.8.4.4) 56(84) bytes of data.\n"
|
||||
"64 bytes from dns.google (8.8.4.4): icmp_seq=1 ttl=118 time=123 ms\n"
|
||||
"64 bytes from 2001:db8::1: icmp_seq=2 ttl=64 time=0.045 ms\n"
|
||||
"--- dns.google ping statistics ---\n"
|
||||
"2 packets transmitted, 2 received, 0% packet loss, time 1001ms\n"
|
||||
"rtt min/avg/max/mdev = 0.045/61.5/123.0/61.4 ms\n"
|
||||
)
|
||||
driver._node_api().execute.post.return_value = {"data": output}
|
||||
result = driver.ping("dns.google")
|
||||
assert result["success"]["results"] == [
|
||||
{"ip_address": "8.8.4.4", "rtt": 123.0},
|
||||
{"ip_address": "2001:db8::1", "rtt": 0.045},
|
||||
]
|
||||
|
||||
def test_failure(self, driver):
|
||||
driver._node_api().execute.post.return_value = {"data": self.PING_FAIL}
|
||||
result = driver.ping("badhost")
|
||||
|
||||
Reference in new issue
Block a user