Files
napalm-tplink-jetstream/tests/unit/test_ping.py
T
Christian Manivong f515859472 fix(ping): list replies only, not timed-out requests
Each "Request timed out" line went into results with the timeout as its
RTT. NAPALM's results hold replies, and a sweep treats a non-empty
results list as a live host, so dead hosts showed up in LAN scans.

Closes #2
2026-11-01 21:52:30 +01:00

52 lines
1.4 KiB
Python

"""ping's results hold replies only (#2).
A "Request timed out" line was added to ``results`` with the timeout as its
RTT, and a sweep treats a non-empty ``results`` as a live host, so hosts that
never answered appeared in LAN scans.
"""
from unittest.mock import MagicMock
from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver
DEAD = """\
Pinging 10.0.0.99 with 64 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 10.0.0.99:
Packets: Sent = 3 , Received = 0 , Lost = 3 (100% loss).
"""
HALF = """\
Pinging 10.0.0.1 with 64 bytes of data:
Reply from 10.0.0.1: bytes=64 time<16ms TTL=64
Request timed out.
Ping statistics for 10.0.0.1:
Packets: Sent = 2 , Received = 1 , Lost = 1 (50% loss).
Approximate round trip times in milli-seconds:
Minimum = 0ms , Maximum = 0ms , Average = 0ms
"""
def _ping(output: str, destination: str, count: int) -> dict:
driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw")
driver._send_command = MagicMock(return_value=output)
return driver.ping(destination, count=count)
def test_a_host_that_only_times_out_has_no_results():
result = _ping(DEAD, "10.0.0.99", 3)["success"]
assert result["results"] == []
assert result["packet_loss"] == 3
def test_only_the_replies_are_results():
result = _ping(HALF, "10.0.0.1", 2)["success"]
assert result["results"] == [{"ip_address": "10.0.0.1", "rtt": 16.0}]
assert result["packet_loss"] == 1