Skip to content

Commit

Permalink
Fix parsing when ping results with pipe field: #45
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jun 2, 2021
1 parent 00ee8d9 commit 08461c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pingparsing/_parser.py
Expand Up @@ -276,7 +276,19 @@ def parse(self, ping_message: Sequence[str]) -> PingStats:
+ pp.Word(pp.nums + ".")
+ pp.Word(pp.nums + "ms")
)
parse_list = rtt_pattern.parseString(_to_unicode(rtt_line))
try:
parse_list = rtt_pattern.parseString(_to_unicode(rtt_line))
except pp.ParseException:
if not re.search(r"\s*pipe \d+", rtt_line):
raise ValueError

return PingStats(
destination=destination,
packet_transmit=packet_transmit,
packet_receive=packet_receive,
duplicates=duplicates,
icmp_replies=icmp_replies,
)

return PingStats(
destination=destination,
Expand Down
57 changes: 57 additions & 0 deletions test/test_pingparsing.py
Expand Up @@ -585,6 +585,62 @@
],
)

LINUX_PIPE = PingTestData(
dedent(
r"""\
PING 91.221.122.179 (91.221.122.179) 64(92) bytes of data.
[1622145167.999326] no answer yet for icmp_seq=1
[1622145168.201746] no answer yet for icmp_seq=2
[1622145168.405761] no answer yet for icmp_seq=3
[1622145168.609750] no answer yet for icmp_seq=4
[1622145170.826836] From 91.221.122.179 icmp_seq=1 Destination Host Unreachable
[1622145170.826888] From 91.221.122.179 icmp_seq=2 Destination Host Unreachable
[1622145170.826896] From 91.221.122.179 icmp_seq=3 Destination Host Unreachable
[1622145170.826901] From 91.221.122.179 icmp_seq=4 Destination Host Unreachable
[1622145170.826907] From 91.221.122.179 icmp_seq=5 Destination Host Unreachable
--- 91.221.122.179 ping statistics ---
5 packets transmitted, 0 received, +5 errors, 100% packet loss, time 811ms
pipe 5
"""
),
{
"destination": "91.221.122.179",
"packet_transmit": 5,
"packet_receive": 0,
"packet_loss_count": 5,
"packet_loss_rate": 100.0,
"rtt_min": None,
"rtt_avg": None,
"rtt_max": None,
"rtt_mdev": None,
"packet_duplicate_count": 0,
"packet_duplicate_rate": None,
},
[
{
"timestamp": datetime(2021, 5, 27, 19, 52, 47, 999326, tzinfo=pytz.UTC),
"icmp_seq": 1,
"duplicate": False,
},
{
"timestamp": datetime(2021, 5, 27, 19, 52, 48, 201746, tzinfo=pytz.UTC),
"icmp_seq": 2,
"duplicate": False,
},
{
"timestamp": datetime(2021, 5, 27, 19, 52, 48, 405761, tzinfo=pytz.UTC),
"icmp_seq": 3,
"duplicate": False,
},
{
"timestamp": datetime(2021, 5, 27, 19, 52, 48, 609750, tzinfo=pytz.UTC),
"icmp_seq": 4,
"duplicate": False,
},
],
)

WINDOWS10_LOSS = PingTestData(
dedent(
"""\
Expand Down Expand Up @@ -693,6 +749,7 @@ class Test_PingParsing_parse:
[WINDOWS_UNREACHABLE_1, "Windows"],
[WINDOWS_UNREACHABLE_2, "Windows"],
[IPV6_LINUX, "Linux"],
[LINUX_PIPE, "Linux"],
],
)
def test_normal_text(self, ping_parser, test_data, parser_name):
Expand Down

0 comments on commit 08461c8

Please sign in to comment.