Commit 2aeda2a2 authored by Łukasz Nowak's avatar Łukasz Nowak

network: Catch and react on TimeoutError

This is low level timeout error, which can happend outside of socket.timeout,
see https://docs.python.org/3/library/exceptions.html#TimeoutError

/reviewed-on nexedi/surykatka!8
parent bbc1e993
...@@ -88,7 +88,7 @@ def isTcpPortOpen(db, ip, port, status_id, timeout=TIMEOUT): ...@@ -88,7 +88,7 @@ def isTcpPortOpen(db, ip, port, status_id, timeout=TIMEOUT):
is_open = True is_open = True
except ConnectionRefusedError: except ConnectionRefusedError:
state = "closed" state = "closed"
except socket.timeout: except (socket.timeout, TimeoutError):
state = "filtered" state = "filtered"
except OSError as e: except OSError as e:
if e.errno == errno.EHOSTUNREACH: if e.errno == errno.EHOSTUNREACH:
......
...@@ -271,6 +271,37 @@ class SurykatkaNetworkTestCase(unittest.TestCase): ...@@ -271,6 +271,37 @@ class SurykatkaNetworkTestCase(unittest.TestCase):
assert self.db.NetworkChange.get().status_id == status_id assert self.db.NetworkChange.get().status_id == status_id
assert result == False assert result == False
def test_isTcpPortOpen_timeouterror(self):
ip = "127.0.0.1"
port = 1234
status_id = logStatus(self.db, "foo")
with mock.patch("surykatka.network.socket.socket") as mock_socket:
def sideEffect(*args, **kw):
raise TimeoutError
mock_socket.return_value.connect.side_effect = sideEffect
result = isTcpPortOpen(self.db, ip, port, status_id)
assert mock_socket.call_count == 1
assert mock_socket.return_value.settimeout.call_count == 1
mock_socket.return_value.settimeout.assert_called_with(2)
assert mock_socket.return_value.connect.call_count == 1
mock_socket.return_value.connect.assert_called_with((ip, port))
assert mock_socket.return_value.close.call_count == 1
assert self.db.NetworkChange.select().count() == 1
assert self.db.NetworkChange.get().ip == ip
assert self.db.NetworkChange.get().port == port
assert self.db.NetworkChange.get().transport == "TCP"
assert self.db.NetworkChange.get().state == "filtered"
assert self.db.NetworkChange.get().status_id == status_id
assert result == False
def test_isTcpPortOpen_noRoute(self): def test_isTcpPortOpen_noRoute(self):
ip = "127.0.0.1" ip = "127.0.0.1"
port = 1234 port = 1234
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment