fix(vlans): set_vlan refuses a name with line breaks or double quotes (#28) #29
No files matched your search
@@ -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)
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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": []
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user