@@ -45,25 +45,6 @@ def _generic_notification_test(
4545
4646
4747class TestNotifications (CreateConnectionsMixin , BaseTestNotification , TestCase ):
48- def test_connection_working_notification (self ):
49- self .assertEqual (Notification .objects .count (), 0 )
50- device_connection = DeviceConnection .objects .create (
51- credentials = self .creds , device = self .d , is_working = False
52- )
53- device_connection .is_working = True
54- device_connection .save ()
55- self .assertEqual (Notification .objects .count (), 1 )
56- self ._generic_notification_test (
57- exp_level = 'info' ,
58- exp_type = 'connection_is_working' ,
59- exp_verb = 'working' ,
60- exp_message = (
61- '(SSH) connection to device <a href="{target_link}">'
62- '{n.target}</a> is {n.verb}.'
63- ),
64- exp_email_subject = '[example.com] RECOVERY: Connection to device {n.target}' ,
65- )
66-
6748 def test_connection_is_working_none (self ):
6849 self .assertEqual (Notification .objects .count (), 0 )
6950
@@ -87,48 +68,6 @@ def test_connection_is_working_none(self):
8768 device_connection .save ()
8869 self .assertEqual (Notification .objects .count (), 0 )
8970
90- def test_connection_not_working_notification (self ):
91- device_connection = DeviceConnection .objects .create (
92- credentials = self .creds , device = self .d , is_working = True
93- )
94- self .assertEqual (Notification .objects .count (), 0 )
95- device_connection .is_working = False
96- device_connection .save ()
97- self .assertEqual (Notification .objects .count (), 1 )
98- self ._generic_notification_test (
99- exp_level = 'error' ,
100- exp_type = 'connection_is_not_working' ,
101- exp_verb = 'not working' ,
102- exp_message = (
103- '(SSH) connection to device <a href="{target_link}">'
104- '{n.target}</a> is {n.verb}.'
105- ),
106- exp_email_subject = '[example.com] PROBLEM: Connection to device {n.target}' ,
107- )
108-
109- def test_unreachable_after_upgrade_notification (self ):
110- device_connection = DeviceConnection .objects .create (
111- credentials = self .creds , device = self .d , is_working = True
112- )
113- self .assertEqual (Notification .objects .count (), 0 )
114- device_connection .is_working = False
115- device_connection .failure_reason = (
116- 'Giving up, device not reachable anymore after upgrade'
117- )
118- device_connection .save ()
119- self .assertEqual (Notification .objects .count (), 1 )
120- self ._generic_notification_test (
121- exp_level = 'error' ,
122- exp_type = 'connection_is_not_working' ,
123- exp_verb = 'not working' ,
124- exp_message = (
125- '(SSH) connection to device <a href="{target_link}">'
126- '{n.target}</a> is {n.verb}. '
127- 'Giving up, device not reachable anymore after upgrade'
128- ),
129- exp_email_subject = '[example.com] PROBLEM: Connection to device {n.target}' ,
130- )
131-
13271 def test_default_notification_type_already_unregistered (self ):
13372 # Simulates if 'default notification type is already unregistered
13473 # by some other module
@@ -147,74 +86,115 @@ def test_default_notification_type_already_unregistered(self):
14786 @patch (
14887 'openwisp_controller.connection.apps.ConnectionConfig'
14988 '._ignore_connection_notification_reasons' ,
150- ['timed out ' ],
89+ ['Unable to connect ' ],
15190 )
15291 @patch .object (notify , 'send' )
153- def test_connection_is_working_changed_timed_out (self , notify_send , * args ):
92+ def test_connection_is_working_changed_unable_to_connect (self , notify_send , * args ):
15493 credentials = self ._create_credentials_with_key (port = self .ssh_server .port )
15594 self ._create_config (device = self .d )
15695 device_conn = self ._create_device_connection (
15796 credentials = credentials , device = self .d , is_working = True
15897 )
159- self .assertEqual (device_conn .is_working , True )
98+ device_conn .failure_reason = (
99+ '[Errno None] Unable to connect to port 5555 on 127.0.0.1'
100+ )
160101 device_conn .is_working = False
161- device_conn .failure_reason = 'timed out'
162102 device_conn .full_clean ()
163103 device_conn .save ()
164104 notify_send .assert_not_called ()
165- # Connection recovers, device is reachable again
166- device_conn .is_working = True
105+ # Connection makes recovery.
167106 device_conn .failure_reason = ''
107+ device_conn .is_working = True
168108 device_conn .full_clean ()
169109 device_conn .save ()
170110 notify_send .assert_not_called ()
171111
112+
113+ class TestNotificationTransaction (
114+ CreateConnectionsMixin , BaseTestNotification , TransactionTestCase
115+ ):
116+ def test_connection_working_notification (self ):
117+ self .assertEqual (Notification .objects .count (), 0 )
118+ device_connection = DeviceConnection .objects .create (
119+ credentials = self .creds , device = self .d , is_working = False
120+ )
121+ device_connection .is_working = True
122+ device_connection .save ()
123+ self .assertEqual (Notification .objects .count (), 1 )
124+ self ._generic_notification_test (
125+ exp_level = 'info' ,
126+ exp_type = 'connection_is_working' ,
127+ exp_verb = 'working' ,
128+ exp_message = (
129+ '(SSH) connection to device <a href="{target_link}">'
130+ '{n.target}</a> is {n.verb}.'
131+ ),
132+ exp_email_subject = '[example.com] RECOVERY: Connection to device {n.target}' ,
133+ )
134+
172135 @patch (
173136 'openwisp_controller.connection.apps.ConnectionConfig'
174137 '._ignore_connection_notification_reasons' ,
175- ['Unable to connect ' ],
138+ ['timed out ' ],
176139 )
177140 @patch .object (notify , 'send' )
178- def test_connection_is_working_changed_unable_to_connect (self , notify_send , * args ):
141+ def test_connection_is_working_changed_timed_out (self , notify_send , * args ):
179142 credentials = self ._create_credentials_with_key (port = self .ssh_server .port )
180143 self ._create_config (device = self .d )
181144 device_conn = self ._create_device_connection (
182145 credentials = credentials , device = self .d , is_working = True
183146 )
184- device_conn .failure_reason = (
185- '[Errno None] Unable to connect to port 5555 on 127.0.0.1'
186- )
147+ self .assertEqual (device_conn .is_working , True )
187148 device_conn .is_working = False
149+ device_conn .failure_reason = 'timed out'
188150 device_conn .full_clean ()
189151 device_conn .save ()
190152 notify_send .assert_not_called ()
191- # Connection makes recovery.
192- device_conn .failure_reason = ''
153+ # Connection recovers, device is reachable again
193154 device_conn .is_working = True
155+ device_conn .failure_reason = ''
194156 device_conn .full_clean ()
195157 device_conn .save ()
196158 notify_send .assert_not_called ()
197159
198-
199- class TestNotificationTransaction (
200- CreateConnectionsMixin , BaseTestNotification , TransactionTestCase
201- ):
202- def test_unreachable_after_upgrade_notification (self ):
203- failure_reason = 'A failure reason'
160+ def test_connection_not_working_notification (self ):
204161 device_connection = DeviceConnection .objects .create (
205162 credentials = self .creds , device = self .d , is_working = True
206163 )
207164 self .assertEqual (Notification .objects .count (), 0 )
208165 device_connection .is_working = False
209- device_connection .failure_reason = failure_reason
210166 device_connection .save ()
211167 self .assertEqual (Notification .objects .count (), 1 )
212- notification = Notification .objects .get (type = 'connection_is_not_working' )
213- self .assertIn (failure_reason , notification .message )
168+ self ._generic_notification_test (
169+ exp_level = 'error' ,
170+ exp_type = 'connection_is_not_working' ,
171+ exp_verb = 'not working' ,
172+ exp_message = (
173+ '(SSH) connection to device <a href="{target_link}">'
174+ '{n.target}</a> is {n.verb}.'
175+ ),
176+ exp_email_subject = '[example.com] PROBLEM: Connection to device {n.target}' ,
177+ )
214178
215- device_connection .is_working = True
216- device_connection .failure_reason = ''
179+ def test_unreachable_after_upgrade_notification (self ):
180+ device_connection = DeviceConnection .objects .create (
181+ credentials = self .creds , device = self .d , is_working = True
182+ )
183+ self .assertEqual (Notification .objects .count (), 0 )
184+ device_connection .is_working = False
185+ device_connection .failure_reason = (
186+ 'Giving up, device not reachable anymore after upgrade'
187+ )
217188 device_connection .save ()
218- self .assertEqual (Notification .objects .count (), 2 )
219- notification = Notification .objects .get (type = 'connection_is_working' )
220- self .assertNotIn (failure_reason , notification .message )
189+ self .assertEqual (Notification .objects .count (), 1 )
190+ self ._generic_notification_test (
191+ exp_level = 'error' ,
192+ exp_type = 'connection_is_not_working' ,
193+ exp_verb = 'not working' ,
194+ exp_message = (
195+ '(SSH) connection to device <a href="{target_link}">'
196+ '{n.target}</a> is {n.verb}. '
197+ 'Giving up, device not reachable anymore after upgrade'
198+ ),
199+ exp_email_subject = '[example.com] PROBLEM: Connection to device {n.target}' ,
200+ )
0 commit comments