11import json
2- import socket
32from copy import deepcopy
43from unittest .mock import patch
54
65from django .core .exceptions import ObjectDoesNotExist , ValidationError
76from openwisp_notifications .signals import notify
8- from paramiko .ssh_exception import NoValidConnectionsError
97from swapper import load_model
108
119from openwisp_controller .config .signals import config_modified
2321
2422
2523class 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