diff --git a/pingparsing/_pingtransmitter.py b/pingparsing/_pingtransmitter.py index 3cc2f2d..112a80b 100644 --- a/pingparsing/_pingtransmitter.py +++ b/pingparsing/_pingtransmitter.py @@ -45,6 +45,12 @@ class PingTransmitter(object): built-in ``ping`` command. Defaults to 1 [sec]. + .. py:attribute:: count + + Number of sending ICMP packets. + The value will be ignored if the value is ``None``. + Defaults to ``None``. + .. py:attribute:: ping_option Additional ``ping`` command option. @@ -58,6 +64,7 @@ class PingTransmitter(object): def __init__(self): self.destination_host = "" self.waittime = 1 + self.count = None self.ping_option = "" self.auto_codepage = True @@ -80,6 +87,7 @@ def ping(self): command_list.append(self.ping_option) command_list.append(self.__get_waittime_option()) + command_list.append(self.__get_count_option()) ping_proc = subprocess.Popen( " ".join(command_list), shell=True, @@ -93,6 +101,7 @@ def __validate_ping_param(self): raise ValueError("required destination_host") self.__validate_waittime() + self.__validate_count() def __validate_waittime(self): if self.waittime is None: @@ -106,6 +115,18 @@ def __validate_waittime(self): if waittime <= 0: raise ValueError("wait time must be greater than zero") + def __validate_count(self): + if self.count is None: + return + + count = dp.IntegerType(self.count).try_convert() + if count is None: + raise ValueError("count must be an integer: actual={}".format( + self.count)) + + if count <= 0: + raise ValueError("count must be greater than zero") + def __get_base_ping_command(self): command_list = [] @@ -128,3 +149,13 @@ def __get_waittime_option(self): return "-n {:d}".format(waittime) else: return "-q -w {:d}".format(waittime) + + def __get_count_option(self): + count = dp.IntegerType(self.count).try_convert() + if count is None: + return "" + + if platform.system() == "Windows": + return "-n {:d}".format(count) + else: + return "-c {:d}".format(count) diff --git a/test/test_pingtransmitter.py b/test/test_pingtransmitter.py index 022ae2d..99c0b13 100644 --- a/test/test_pingtransmitter.py +++ b/test/test_pingtransmitter.py @@ -15,13 +15,28 @@ def transmitter(): class Test_PingTransmitter_ping: @pytest.mark.xfail - @pytest.mark.parametrize(["host", "waittime", "expected"], [ - ["localhost", 3, ValueError], + @pytest.mark.parametrize(["host", "waittime"], [ + ["localhost", 3], ]) - def test_normal(self, transmitter, host, waittime, expected): + def test_normal_waittime(self, transmitter, host, waittime): transmitter.destination_host = host transmitter.waittime = waittime result = transmitter.ping() + + assert result.returncode == 0 + assert len(result.stdout) > 0 + + @pytest.mark.xfail + @pytest.mark.parametrize(["host", "count", "waittime"], [ + ["localhost", 1, None], + ["localhost", 1, 1000], + ]) + def test_normal_count(self, transmitter, host, count, waittime): + transmitter.destination_host = host + transmitter.waittime = waittime + transmitter.count = count + result = transmitter.ping() + assert result.returncode == 0 assert len(result.stdout) > 0 @@ -32,8 +47,19 @@ def test_normal(self, transmitter, host, waittime, expected): ["localhost", "a", ValueError], [None, 1, ValueError], ]) - def test_except(self, transmitter, host, waittime, expected): + def test_except_waittime(self, transmitter, host, waittime, expected): transmitter.destination_host = host transmitter.waittime = waittime with pytest.raises(expected): transmitter.ping() + + @pytest.mark.parametrize(["host", "count", "expected"], [ + ["localhost", 0, ValueError], + ["localhost", -1, ValueError], + ["localhost", "a", ValueError], + ]) + def test_except_count(self, transmitter, host, count, expected): + transmitter.destination_host = host + transmitter.count = count + with pytest.raises(expected): + transmitter.ping()