Repository navigation
Expand file tree
/
Copy pathtest_network_checker.py
More file actions
39 lines (31 loc) · 1.62 KB
/
Copy pathtest_network_checker.py
File metadata and controls
39 lines (31 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import contextlib
import io
import subprocess
import unittest
from unittest.mock import MagicMock, patch
import network_checker_pro as checker
class DiagnosticTests(unittest.TestCase):
def test_missing_ping_command_is_reported_as_unavailable(self):
with patch.object(checker.subprocess, 'run', side_effect=FileNotFoundError):
self.assertFalse(checker.ping('example.com'))
def test_ping_timeout_does_not_crash_diagnostics(self):
with patch.object(checker.subprocess, 'run', side_effect=subprocess.TimeoutExpired('ping', 5)):
self.assertFalse(checker.ping('example.com'))
def test_icmp_failure_does_not_mean_internet_is_down_when_tcp_works(self):
output = io.StringIO()
with patch.object(checker, 'ping', return_value=False), \
patch.object(checker, 'tcp_test', return_value=True), \
patch.object(checker, 'http_test', return_value=False), \
patch.object(checker, 'get_local_ip', return_value='192.0.2.10'), \
patch.object(checker.time, 'sleep'), contextlib.redirect_stdout(output):
checker.main()
self.assertNotIn('Internet seems DOWN', output.getvalue())
self.assertIn('TCP connectivity is available', output.getvalue())
def test_non_http_response_is_not_reported_as_http_success(self):
sock = MagicMock()
sock.__enter__.return_value = sock
sock.recv.return_value = b'not an HTTP response'
with patch.object(checker.socket, 'socket', return_value=sock):
self.assertFalse(checker.http_test('example.com'))
if __name__ == '__main__':
unittest.main()