Removed obsolete url
[ta/config-manager.git] / cmframework / test / cmalarm_test.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import unittest
16 import mock
17 from mock import call
18 import json
19
20 from cmframework.utils.cmalarm import CMAlarm
21 from cmframework.utils.cmalarm import CMRebootRequestAlarm
22 from cmframework.utils.cmalarm import CMActivationFailedAlarm
23 from cmframework.apis.cmerror import CMError
24
25
26 class CMAlarmTest(unittest.TestCase):
27     @mock.patch('cmframework.utils.cmalarm.logging')
28     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
29     def test_abstract_alarm(self, mock_alarm_handler, mock_logging):
30         alarm = CMAlarm()
31
32         with self.assertRaises(NotImplementedError) as context:
33             alarm.raise_alarm_for_node('node-a')
34
35     @mock.patch('cmframework.utils.cmalarm.logging')
36     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
37     def test_raise_and_fail(self, mock_alarm_handler, mock_logging):
38         mock_alarm_handler.side_effect = Exception
39         alarm = CMRebootRequestAlarm()
40         alarm.raise_alarm_for_node('node-a')
41
42         mock_logging.warning.assert_called_once()
43         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_not_called()
44
45     @mock.patch('cmframework.utils.cmalarm.logging')
46     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
47     def test_cancel_and_fail(self, mock_alarm_handler, mock_logging):
48         mock_alarm_handler.side_effect = Exception
49         alarm = CMRebootRequestAlarm()
50         alarm.cancel_alarm_for_node('node-a')
51
52         mock_logging.warning.assert_called_once()
53         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_not_called()
54
55     @mock.patch('cmframework.utils.cmalarm.logging')
56     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
57     def test_raise_rebootrequestalarm_for_node(self, mock_alarm_handler, mock_logging):
58         alarm = CMRebootRequestAlarm()
59         alarm.raise_alarm_for_node('node-a')
60
61         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
62             '45001',
63             'NODE-node-a',
64             {})
65
66     @mock.patch('cmframework.utils.cmalarm.logging')
67     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
68     def test_raise_rebootrequestalarm_for_node_with_info(self, mock_alarm_handler, mock_logging):
69         alarm = CMRebootRequestAlarm()
70         alarm.raise_alarm_for_node('node-a', {'some': 'additional info'})
71
72         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
73             '45001',
74             'NODE-node-a',
75             {'some': 'additional info'})
76
77     @mock.patch('cmframework.utils.cmalarm.logging')
78     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
79     def test_raise_rebootrequestalarm_for_sg(self, mock_alarm_handler, mock_logging):
80         alarm = CMRebootRequestAlarm()
81         alarm.raise_alarm_for_sg('config-manager')
82
83         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
84             '45001',
85             'SG-config-manager',
86             {})
87
88     @mock.patch('cmframework.utils.cmalarm.logging')
89     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
90     def test_cancel_rebootrequestalarm_for_node(self, mock_alarm_handler, mock_logging):
91         alarm = CMRebootRequestAlarm()
92         alarm.cancel_alarm_for_node('node-a')
93
94         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
95             '45001',
96             'NODE-node-a',
97             {})
98
99     @mock.patch('cmframework.utils.cmalarm.logging')
100     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
101     def test_cancel_rebootrequestalarm_for_node_with_info(self, mock_alarm_handler, mock_logging):
102         alarm = CMRebootRequestAlarm()
103         alarm.cancel_alarm_for_node('node-a', {'some': 'additional info'})
104
105         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
106             '45001',
107             'NODE-node-a',
108             {'some': 'additional info'})
109
110     @mock.patch('cmframework.utils.cmalarm.logging')
111     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
112     def test_cancel_rebootrequestalarm_for_sg(self, mock_alarm_handler, mock_logging):
113         alarm = CMRebootRequestAlarm()
114         alarm.cancel_alarm_for_sg('config-manager')
115
116         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
117             '45001',
118             'SG-config-manager',
119             {})
120
121     @mock.patch('cmframework.utils.cmalarm.logging')
122     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
123     def test_raise_activationfailed_for_node(self, mock_alarm_handler, mock_logging):
124         alarm = CMActivationFailedAlarm()
125         alarm.raise_alarm_for_node('node-a')
126
127         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
128             '45002',
129             'NODE-node-a',
130             {})
131
132     @mock.patch('cmframework.utils.cmalarm.logging')
133     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
134     def test_raise_activationfailed_for_sg(self, mock_alarm_handler, mock_logging):
135         alarm = CMActivationFailedAlarm()
136         alarm.raise_alarm_for_sg('config-manager')
137
138         mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
139             '45002',
140             'SG-config-manager',
141             {})
142
143     @mock.patch('cmframework.utils.cmalarm.logging')
144     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
145     def test_cancel_activationfailed_for_node(self, mock_alarm_handler, mock_logging):
146         alarm = CMActivationFailedAlarm()
147         alarm.cancel_alarm_for_node('node-a')
148
149         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
150             '45002',
151             'NODE-node-a',
152             {})
153
154     @mock.patch('cmframework.utils.cmalarm.logging')
155     @mock.patch('cmframework.utils.cmalarm.AlarmHandler')
156     def test_cancel_activationfailed_for_sg(self, mock_alarm_handler, mock_logging):
157         alarm = CMActivationFailedAlarm()
158         alarm.cancel_alarm_for_sg('config-manager')
159
160         mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
161             '45002',
162             'SG-config-manager',
163             {})
164
165 if __name__ == '__main__':
166     unittest.main()