1
1
import json
2
- import socket
3
2
from copy import deepcopy
4
3
from unittest .mock import patch
5
4
6
5
from django .core .exceptions import ObjectDoesNotExist , ValidationError
7
6
from openwisp_notifications .signals import notify
8
- from paramiko .ssh_exception import NoValidConnectionsError
9
7
from swapper import load_model
10
8
11
9
from openwisp_controller .config .signals import config_modified
23
21
24
22
25
23
class BaseTestCase (DeviceMonitoringTestCase ):
24
+ _PING = 'openwisp_monitoring.check.classes.Ping'
26
25
_sample_data = {
27
26
"type" : "DeviceMonitoring" ,
28
27
"general" : {
@@ -589,8 +588,7 @@ def test_is_working_false_true(self, notify_send, perform_check):
589
588
dm = d .monitoring
590
589
dm .status = 'unknown'
591
590
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 )
594
592
c = Credentials .objects .create ()
595
593
dc = DeviceConnection .objects .create (credentials = c , device = d , is_working = False )
596
594
self .assertFalse (dc .is_working )
@@ -606,8 +604,7 @@ def test_is_working_changed_to_false(self, notify_send, perform_check):
606
604
dm = d .monitoring
607
605
dm .status = 'ok'
608
606
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 )
611
608
c = Credentials .objects .create ()
612
609
dc = DeviceConnection .objects .create (credentials = c , device = d )
613
610
dc .is_working = False
@@ -622,8 +619,7 @@ def test_is_working_none_true(self, notify_send, perform_check):
622
619
dm = d .monitoring
623
620
dm .status = 'unknown'
624
621
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 )
627
623
c = Credentials .objects .create ()
628
624
dc = DeviceConnection .objects .create (credentials = c , device = d )
629
625
self .assertIsNone (dc .is_working )
@@ -634,24 +630,43 @@ def test_is_working_none_true(self, notify_send, perform_check):
634
630
635
631
@patch .object (Check , 'perform_check' )
636
632
@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 ):
638
634
ckey = self ._create_credentials_with_key (port = self .ssh_server .port )
639
635
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
+
640
641
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 )
645
657
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
+
655
670
notify_send .assert_not_called ()
656
671
perform_check .assert_not_called ()
657
672
@@ -663,8 +678,7 @@ def test_is_working_no_recovery_notification(self, notify_send, perform_check):
663
678
d = self .device_model .objects .first ()
664
679
d .monitoring .update_status ('ok' )
665
680
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 )
668
682
failure_reason = '[Errno None] Unable to connect to port 5555 on 127.0.0.1'
669
683
self .assertTrue (dc .is_working )
670
684
dc .failure_reason = failure_reason
0 commit comments