Skip to content

Commit

Permalink
Update testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
kyan001 committed Aug 9, 2021
1 parent c9cd2ce commit 55ee2ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
24 changes: 12 additions & 12 deletions tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def test_dest_addr_0(self):
def test_dest_addr_1(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
command_line.main(["127.0.0.1"])
self.assertTrue("127.0.0.1" in fake_out.getvalue())
self.assertIn("127.0.0.1", fake_out.getvalue())

def test_dest_addr_2(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
command_line.main(["127.0.0.1", "8.8.8.8"])
self.assertTrue("127.0.0.1" in fake_out.getvalue())
self.assertTrue("8.8.8.8" in fake_out.getvalue())
self.assertIn("127.0.0.1", fake_out.getvalue())
self.assertIn("8.8.8.8", fake_out.getvalue())

def test_count(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
Expand All @@ -49,7 +49,7 @@ def test_timeout(self):
def test_ttl(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
command_line.main(['-T', '1', 'example.com'])
self.assertRegex(fake_out.getvalue(), r".*Timeout.*")
self.assertRegex(fake_out.getvalue(), r".*Error.*")

def test_size(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
Expand All @@ -61,10 +61,10 @@ def test_size(self):
def test_interval(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
start_time = time.time()
command_line.main(['-i', '1.7', 'example.com'])
command_line.main(['-i', '1', 'example.com'])
end_time = time.time()
self.assertTrue((end_time - start_time) >= 5.1) # time_expect = (count - 1) * interval
self.assertFalse('Timeout' in fake_out.getvalue())
self.assertTrue((end_time - start_time) >= 3) # time_expect = (count - 1) * interval
self.assertNotIn('Timeout', fake_out.getvalue())

@unittest.skipUnless(sys.platform == 'linux', "Linux only")
def test_interface(self):
Expand All @@ -85,9 +85,10 @@ def test_interface(self):
def test_src_addr(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
my_ip = socket.gethostbyname(socket.gethostname())
dest_addr = "example.com"
if my_ip == "127.0.0.1" or my_ip == "127.0.1.1": # This may caused by /etc/hosts settings.
if my_ip in ("127.0.0.1", "127.0.1.1"): # This may caused by /etc/hosts settings.
dest_addr = my_ip # only localhost can send and receive from 127.0.0.1 (or 127.0.1.1 on Ubuntu).
else:
dest_addr = "example.com"
command_line.main(['-S', my_ip, dest_addr])
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")

Expand All @@ -97,9 +98,8 @@ def test_debug(self):
self.assertIn("[DEBUG]", fake_err.getvalue())

def test_exceptions(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
with self.assertRaises(errors.Timeout):
command_line.main(['--exceptions', '-t', '0.0001', 'example.com'])
with self.assertRaises(errors.Timeout):
command_line.main(['--exceptions', '-t', '0.0001', 'example.com'])


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions tests/test_ping3.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ def test_verbose_ping_count(self):
def test_verbose_ping_interval(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
delay = ping3.ping("example.com")
self.assertIsInstance(delay, float)
self.assertTrue(0 < delay < 0.75) # If interval does not work, the total delay should be < 3s (4 * 0.75s)
start_time = time.time()
ping3.verbose_ping("example.com", interval=1) # If interval does work, the total delay should be > 3s (3 * 1s)
end_time = time.time()
self.assertTrue((end_time - start_time) >= 3) # time_expect = (count - 1) * interval
self.assertRegex(fake_out.getvalue(), r".*[0-9]+.*")
self.assertNotIn('Timeout', fake_out.getvalue()) # Ensure no timeout

def test_DEBUG(self):
with patch("ping3.DEBUG", True), patch("sys.stderr", new=io.StringIO()):
Expand Down

0 comments on commit 55ee2ba

Please sign in to comment.