def test_parsing_without_args_at_all(self): cmd_parser = tcping.create_cmd_parser() args = cmd_parser.parse_args(['google.com']) tcp_ping = ping.TCPing(destination=args.destination) self.assertEqual(tcp_ping.use_ipv6, False) self.assertEqual(tcp_ping.timeout, 0) self.assertEqual(tcp_ping.port, 80)
def test_parsing_with_args(self): cmd_parser = tcping.create_cmd_parser() args = cmd_parser.parse_args(['google.com', '-t', '10', '-p', '443', '-6']) tcp_ping = ping.TCPing(destination=args.destination, port=args.port, timeout=args.timeout, use_ipv6=args.use_ipv6) self.assertEqual(tcp_ping.use_ipv6, True) self.assertEqual(tcp_ping.timeout, 10) self.assertEqual(tcp_ping.port, 443)
def test_ping_ip_incorrect(self): with mock.patch('socket.socket') as mock_socket: mock_socket.return_value.recv.return_value = b"" cmd_parser = tcping.create_cmd_parser() args = cmd_parser.parse_args(['64.233.165.101.123.214']) tcp_ping = ping.TCPing(destination=args.destination, port=args.port, timeout=args.timeout, use_ipv6=args.use_ipv6) with self.assertRaises(errors.InvalidIpOrDomain): tcp_ping.do_ping()
def test_ping_ip_standart(self): with mock.patch('socket.socket') as mock_socket: mock_socket.return_value.recv.return_value = b"" cmd_parser = tcping.create_cmd_parser() args = cmd_parser.parse_args(['64.233.165.101', '-c', '10']) tcp_ping = ping.TCPing(destination=args.destination, port=args.port, timeout=args.timeout, use_ipv6=args.use_ipv6) tcp_ping.do_ping() self.assertEqual(len(tcp_ping.measures), 1)
def get_pings(self) -> list: """Method, make ping objects from destinations and return list of pings.""" pings = [] for destination in self.destinations: wd_ping = ping.TCPing(destination=destination[0], port=destination[1], timeout=self.timeout, use_ipv6=self.use_ipv6) pings.append(wd_ping) return pings
def test_get_pings(self): raw_destinations = ['google.com', 'google.com:443'] pings_expected = [ping.TCPing(destination='google.com', port=80, use_ipv6=False, timeout=1), ping.TCPing(destination='google.com', port=443, use_ipv6=False, timeout=1)] parsed_destinations = watchdog_ping \ .WatchdogPingData \ .parse_destinations(raw_destinations) watchdog_ping_object = watchdog_ping \ .WatchdogPingData(destinations=parsed_destinations, timeout=1, use_ipv6=False) pings = watchdog_ping_object.get_pings() self.assertEqual(len(pings_expected), len(pings)) self.assertEqual(type(pings[0]), ping.TCPing) self.assertEqual(type(pings[1]), ping.TCPing)
help='to use ipv6') parser.add_argument('-o', '--output-level', default='2', dest="output_level", help='0 - only summary statistics; ' '1 - write only info for single pings; ' '2 - write all; ') return parser if __name__ == '__main__': cmd_parser = create_cmd_parser() args = cmd_parser.parse_args() tcp_ping = ping.TCPing(destination=args.destination, port=args.port, timeout=args.timeout, use_ipv6=args.use_ipv6) pings_count = int(args.pings_count) delay = float(args.delay) output_level = int(args.output_level) try: i = 0 while i != pings_count: measure = tcp_ping.do_ping() if output_level > 0: print(str(measure)) if delay > measure: time.sleep(delay - measure) i += 1 except errors.PingError as e: logging.basicConfig(level=logging.INFO)