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
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:
1 parent
e06811de50
commit
23e799ee8d
4 files changed
+63
-72
No files matched your search
@@ -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,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user