Skip to content

Commit

Permalink
Add count property to PingTransmitter class: #22
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Dec 11, 2016
1 parent 1758d17 commit deb5cf7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
31 changes: 31 additions & 0 deletions pingparsing/_pingtransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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 = []

Expand All @@ -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)
34 changes: 30 additions & 4 deletions test/test_pingtransmitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

0 comments on commit deb5cf7

Please sign in to comment.