Skip to content

Commit f6c7591

Browse files
nemesifiernepython
authored andcommitted
[change] Add "timed out" to ignored connection failures
Plus minor cleanup.
1 parent 985eaff commit f6c7591

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

openwisp_monitoring/device/apps.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ def is_working_changed_receiver(
121121
return
122122
# if device is down because of connectivity issues, it's probably due
123123
# to reboot caused by firmware upgrade, avoid notifications
124-
if 'Unable to connect' in failure_reason:
125-
device_monitoring.save()
126-
return
124+
ignored_failures = ['Unable to connect', 'timed out']
125+
for message in ignored_failures:
126+
if message in failure_reason:
127+
device_monitoring.save()
128+
return
127129
initial_status = device_monitoring.status
128130
status = 'ok' if is_working else 'problem'
129131
# do not send notificatons if recovery made after firmware upgrade

openwisp_monitoring/device/tests/test_models.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import json
2-
import socket
32
from copy import deepcopy
43
from unittest.mock import patch
54

65
from django.core.exceptions import ObjectDoesNotExist, ValidationError
76
from openwisp_notifications.signals import notify
8-
from paramiko.ssh_exception import NoValidConnectionsError
97
from swapper import load_model
108

119
from openwisp_controller.config.signals import config_modified
@@ -23,6 +21,7 @@
2321

2422

2523
class BaseTestCase(DeviceMonitoringTestCase):
24+
_PING = 'openwisp_monitoring.check.classes.Ping'
2625
_sample_data = {
2726
"type": "DeviceMonitoring",
2827
"general": {
@@ -589,8 +588,7 @@ def test_is_working_false_true(self, notify_send, perform_check):
589588
dm = d.monitoring
590589
dm.status = 'unknown'
591590
dm.save()
592-
ping_path = 'openwisp_monitoring.check.classes.Ping'
593-
Check.objects.create(name='Check', content_object=d, check=ping_path)
591+
Check.objects.create(name='Check', content_object=d, check=self._PING)
594592
c = Credentials.objects.create()
595593
dc = DeviceConnection.objects.create(credentials=c, device=d, is_working=False)
596594
self.assertFalse(dc.is_working)
@@ -606,8 +604,7 @@ def test_is_working_changed_to_false(self, notify_send, perform_check):
606604
dm = d.monitoring
607605
dm.status = 'ok'
608606
dm.save()
609-
ping_path = 'openwisp_monitoring.check.classes.Ping'
610-
Check.objects.create(name='Check', content_object=d, check=ping_path)
607+
Check.objects.create(name='Check', content_object=d, check=self._PING)
611608
c = Credentials.objects.create()
612609
dc = DeviceConnection.objects.create(credentials=c, device=d)
613610
dc.is_working = False
@@ -622,8 +619,7 @@ def test_is_working_none_true(self, notify_send, perform_check):
622619
dm = d.monitoring
623620
dm.status = 'unknown'
624621
dm.save()
625-
ping_path = 'openwisp_monitoring.check.classes.Ping'
626-
Check.objects.create(name='Check', content_object=d, check=ping_path)
622+
Check.objects.create(name='Check', content_object=d, check=self._PING)
627623
c = Credentials.objects.create()
628624
dc = DeviceConnection.objects.create(credentials=c, device=d)
629625
self.assertIsNone(dc.is_working)
@@ -634,24 +630,43 @@ def test_is_working_none_true(self, notify_send, perform_check):
634630

635631
@patch.object(Check, 'perform_check')
636632
@patch.object(notify, 'send')
637-
def test_is_working_connectivity_failure(self, notify_send, perform_check):
633+
def test_is_working_changed_unable_to_connect(self, notify_send, perform_check):
638634
ckey = self._create_credentials_with_key(port=self.ssh_server.port)
639635
dc = self._create_device_connection(credentials=ckey)
636+
dc.is_working = True
637+
dc.save()
638+
notify_send.assert_not_called()
639+
perform_check.assert_not_called()
640+
640641
d = self.device_model.objects.first()
641-
d.monitoring.update_status('unknown')
642-
ping_path = 'openwisp_monitoring.check.classes.Ping'
643-
Check.objects.create(name='Check', content_object=d, check=ping_path)
644-
self.assertIsNone(dc.is_working)
642+
d.monitoring.update_status('ok')
643+
Check.objects.create(name='Check', content_object=d, check=self._PING)
644+
dc.is_working = False
645+
dc.failure_reason = '[Errno None] Unable to connect to port 5555 on 127.0.0.1'
646+
dc.full_clean()
647+
dc.save()
648+
649+
notify_send.assert_not_called()
650+
perform_check.assert_not_called()
651+
652+
@patch.object(Check, 'perform_check')
653+
@patch.object(notify, 'send')
654+
def test_is_working_changed_timed_out(self, notify_send, perform_check):
655+
ckey = self._create_credentials_with_key(port=self.ssh_server.port)
656+
dc = self._create_device_connection(credentials=ckey)
645657
dc.is_working = True
646-
e = NoValidConnectionsError({'error': socket.error})
647-
self.assertEqual(dc.failure_reason, '')
648-
with patch.object(dc.connector_instance, 'connect', return_value=e):
649-
dc.save()
650-
dc.connect()
651-
self.assertEqual(
652-
dc.failure_reason,
653-
'[Errno None] Unable to connect to port 5555 on 127.0.0.1',
654-
)
658+
dc.save()
659+
notify_send.assert_not_called()
660+
perform_check.assert_not_called()
661+
662+
d = self.device_model.objects.first()
663+
d.monitoring.update_status('ok')
664+
Check.objects.create(name='Check', content_object=d, check=self._PING)
665+
dc.is_working = False
666+
dc.failure_reason = 'timed out'
667+
dc.full_clean()
668+
dc.save()
669+
655670
notify_send.assert_not_called()
656671
perform_check.assert_not_called()
657672

@@ -663,8 +678,7 @@ def test_is_working_no_recovery_notification(self, notify_send, perform_check):
663678
d = self.device_model.objects.first()
664679
d.monitoring.update_status('ok')
665680
dc.refresh_from_db()
666-
ping_path = 'openwisp_monitoring.check.classes.Ping'
667-
Check.objects.create(name='Check', content_object=d, check=ping_path)
681+
Check.objects.create(name='Check', content_object=d, check=self._PING)
668682
failure_reason = '[Errno None] Unable to connect to port 5555 on 127.0.0.1'
669683
self.assertTrue(dc.is_working)
670684
dc.failure_reason = failure_reason

0 commit comments

Comments
 (0)