refactor(ping): the ping output parser lives with the other CLI parsers (#28)
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / test (3.13) (pull_request) Successful in 46s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 57s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m0s
CI / driver (pull_request) Successful in 2m13s
driver / publish (pull_request) Skipped

The checks against line breaks and double quotes made procurve.py 20
lines longer than the quality ratchet allows (1365, recorded 1345). The
module-level ping parser moves to parsers.py, which holds every other
pure CLI parser, as parse_ping_output; its two result dicts become one
helper, so it is no longer over 50 lines. Behaviour is unchanged; tests
for an unreachable target and an unknown answer are added. The baseline
is lowered to what is measured now (procurve.py 1300, the ping entry
gone).
This commit is contained in:
Christian Manivong committed 2026-11-13 08:57:42 +01:00
1 parent e06811de50
commit 23e799ee8d
4 files changed
+63 -72

No files matched your search

+46
View File
@@ -911,3 +911,49 @@ def parse_cli_answer(answer: dict[str, str]) -> str:
"""The output in a REST ``/cli`` answer: base64, the switch ends it with a NUL byte."""
encoded = answer.get("result_base64_encoded", "")
return base64.b64decode(encoded).decode("utf-8", "replace").rstrip("\x00")
# ---------------------------------------------------------------------------
# ping
# ---------------------------------------------------------------------------
def _ping_result(sent: int, received: int, rtt: tuple[float, float, float]) -> dict:
"""NAPALM's ping ``success`` dict; *rtt* is (min, avg, max)."""
rtt_min, rtt_avg, rtt_max = rtt
return {
"success": {
"probes_sent": sent,
"packet_loss": sent - received,
"rtt_min": rtt_min,
"rtt_max": rtt_max,
"rtt_avg": rtt_avg,
"rtt_stddev": 0.0,
"results": [{"ip_address": "", "rtt": rtt_avg}] * received,
}
}
def parse_ping_output(output: str, count: int) -> dict:
"""Parse ProCurve CLI ping output into NAPALM ping result dict.
ProCurve ping output::
PING 10.0.0.1: 100 data bytes
!!!!
----10.0.0.1 PING Statistics----
5 packets transmitted, 5 packets received, 0% packet loss
round-trip (ms) min/avg/max = 0/0/1
"""
m = re.search(r"(\d+)\s+packets\s+transmitted,\s+(\d+)\s+packets\s+received", output, re.I)
if not m:
if "unreachable" in output.lower() or "no route" in output.lower():
return _ping_result(count, 0, (0.0, 0.0, 0.0))
return {"error": "Could not parse ping output"}
# RTT line: "round-trip (ms) min/avg/max = 0/0/1"
rtt_m = re.search(r"min/avg/max\s*=\s*([\d.]+)/([\d.]+)/([\d.]+)", output, re.I)
rtt = (0.0, 0.0, 0.0)
if rtt_m:
rtt = (float(rtt_m.group(1)), float(rtt_m.group(2)), float(rtt_m.group(3)))
return _ping_result(int(m.group(1)), int(m.group(2)), rtt)
+1 -66
View File
@@ -1108,7 +1108,7 @@ class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver):
if source:
cmd += f" source {source}"
out = self._send_command(cmd)
return _parse_ping_output(out, count)
return parsers.parse_ping_output(out, count)
# ------------------------------------------------------------------
# NAPALM: cli
@@ -1298,68 +1298,3 @@ class ProcurveDriver(ProcurveTimeSyncMixin, ConfigLifecycleMixin, SwitchDriver):
if re.search(rf"^\s*{re.escape(settings['community'])}\s", shown, re.M):
return {"ok": True, "output": "configuration saved; the community is active"}
return {"ok": False, "output": f"saved, but the community is not shown: {shown[:200]}"}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _parse_ping_output(output: str, count: int) -> dict:
"""Parse ProCurve CLI ping output into NAPALM ping result dict.
ProCurve ping output::
PING 10.0.0.1: 100 data bytes
!!!!
----10.0.0.1 PING Statistics----
5 packets transmitted, 5 packets received, 0% packet loss
round-trip (ms) min/avg/max = 0/0/1
"""
# Packet statistics line
m = re.search(
r"(\d+)\s+packets\s+transmitted,\s+(\d+)\s+packets\s+received",
output,
re.I,
)
if not m:
# Check for total failure
if "unreachable" in output.lower() or "no route" in output.lower():
return {
"success": {
"probes_sent": count,
"packet_loss": count,
"rtt_min": 0.0,
"rtt_max": 0.0,
"rtt_avg": 0.0,
"rtt_stddev": 0.0,
"results": [],
}
}
return {"error": "Could not parse ping output"}
sent = int(m.group(1))
received = int(m.group(2))
loss = sent - received
# RTT line: "round-trip (ms) min/avg/max = 0/0/1"
rtt_min = rtt_avg = rtt_max = 0.0
rtt_m = re.search(r"min/avg/max\s*=\s*([\d.]+)/([\d.]+)/([\d.]+)", output, re.I)
if rtt_m:
rtt_min = float(rtt_m.group(1))
rtt_avg = float(rtt_m.group(2))
rtt_max = float(rtt_m.group(3))
results = [{"ip_address": "", "rtt": rtt_avg}] * received
return {
"success": {
"probes_sent": sent,
"packet_loss": loss,
"rtt_min": rtt_min,
"rtt_max": rtt_max,
"rtt_avg": rtt_avg,
"rtt_stddev": 0.0,
"results": results,
}
}
+2 -3
View File
@@ -18,8 +18,7 @@
"napalm_procurve/parsers.py::parse_ip_interfaces": 58,
"napalm_procurve/parsers.py::parse_lldp_neighbors_detail": 59,
"napalm_procurve/parsers.py::parse_system_info": 80,
"napalm_procurve/parsers.py::parse_vlans_from_running_config": 79,
"napalm_procurve/procurve.py::_parse_ping_output": 58
"napalm_procurve/parsers.py::parse_vlans_from_running_config": 79
},
"mypy_relaxed": [
"napalm_procurve.api_client",
@@ -27,7 +26,7 @@
"napalm_procurve.procurve"
],
"python_file_lines": {
"napalm_procurve/procurve.py": 1345
"napalm_procurve/procurve.py": 1300
},
"untyped_public": []
}
+14 -3
View File
@@ -6,7 +6,7 @@ import pytest
from napalm.base.exceptions import ConnectionException
from napalm_procurve import parsers
from napalm_procurve.procurve import _SSH_DISABLED_STANDARD, ProcurveDriver, _parse_ping_output
from napalm_procurve.procurve import _SSH_DISABLED_STANDARD, ProcurveDriver
# ===========================================================================
# Sample CLI output fixtures (captured from real ProCurve devices)
@@ -362,16 +362,27 @@ class TestParseIpInterfaces:
class TestParsePingOutput:
def test_success(self):
result = _parse_ping_output(PING_OUTPUT_SUCCESS, 5)
result = parsers.parse_ping_output(PING_OUTPUT_SUCCESS, 5)
assert result["success"]["probes_sent"] == 5
assert result["success"]["packet_loss"] == 0
assert result["success"]["rtt_avg"] == 1.0
def test_failure(self):
result = _parse_ping_output(PING_OUTPUT_FAILURE, 5)
result = parsers.parse_ping_output(PING_OUTPUT_FAILURE, 5)
assert result["success"]["probes_sent"] == 5
assert result["success"]["packet_loss"] == 5
def test_unreachable_counts_every_probe_lost(self):
result = parsers.parse_ping_output("Request timed out: host unreachable", 3)
assert result["success"]["probes_sent"] == 3
assert result["success"]["packet_loss"] == 3
assert result["success"]["results"] == []
def test_unknown_output_is_an_error(self):
assert parsers.parse_ping_output("Invalid input: pong", 3) == {
"error": "Could not parse ping output"
}
# ===========================================================================
# Driver integration tests (mocked netmiko)