def test_parse_no_packet_line_match(self): output = textwrap.dedent(''' PING google.com (172.217.3.206) 56(84) bytes of data. 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=1 ttl=63 time=27.3 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=2 ttl=63 time=26.7 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=3 ttl=63 time=27.8 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=4 ttl=63 time=29.0 ms --- google.com ping statistics --- 4 packets transmitted, b received, 0% packet loss, time 3008ms rtt min/avg/max/mdev = 26.780/27.731/29.014/0.832 ms ''').strip().encode('ascii') packet_line = '4 packets transmitted, b received, ' \ '0% packet loss, time 3008ms' expected_error_msg = 'Could not parse packet line:' \ '\n{packet_line}'.format(packet_line=packet_line) expected = ping.PingCommandResult( error=expected_error_msg, host_or_ip='google.com', num_packets=4, stats=None, ) actual = ping.parse_ping_output(output, '', self.param) self.assertEqual(expected, actual)
def test_parse_with_errors(self): param = ping.PingCommandParams(host_or_ip='localhost', num_packets=None, timeout_secs=None) actual = ping.parse_ping_output('test', 'test', param) expected = ping.PingCommandResult(error='test', host_or_ip='localhost', num_packets=ping.DEFAULT_NUM_PACKETS, stats=None) self.assertEqual(expected, actual)
def test_parse_deadline_reached_no_results(self): output = textwrap.dedent(''' PING google.com (172.217.3.206) 56(84) bytes of data. --- google.com ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms ''').strip().encode('ascii') expected_error_msg = 'Not enough output lines in ping output. ' \ 'The ping may have timed out.' expected = ping.PingCommandResult( error=expected_error_msg, host_or_ip='google.com', num_packets=4, stats=None, ) actual = ping.parse_ping_output(output, '', self.param) self.assertEqual(expected, actual)
def test_parse_no_header_line(self): output = textwrap.dedent(''' PING google.com (172.217.3.206) 56(84) bytes of data. 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=1 ttl=63 time=27.3 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=2 ttl=63 time=26.7 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=3 ttl=63 time=27.8 ms 64 bytes from sea15s12-in-f206.1e100.net (172.217.3.206): icmp_seq=4 ttl=63 time=29.0 ms 4 packets transmitted, 4 received, 0% packet loss, time 3008ms rtt min/avg/max/mdev = 26.780/27.731/29.014/0.832 ms ''').strip().encode('ascii') expected = ping.PingCommandResult( error='Could not find statistics header in ping output', host_or_ip='google.com', num_packets=4, stats=None, ) actual = ping.parse_ping_output(output, '', self.param) self.assertEqual(expected, actual)