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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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):
32 with self.assertRaises(NotImplementedError) as context:
33 alarm.raise_alarm_for_node('node-a')
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')
42 mock_logging.warning.assert_called_once()
43 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_not_called()
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')
52 mock_logging.warning.assert_called_once()
53 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_not_called()
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')
61 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
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'})
72 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
75 {'some': 'additional info'})
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')
83 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
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')
94 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
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'})
105 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
108 {'some': 'additional info'})
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')
116 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
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')
127 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
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')
138 mock_alarm_handler.return_value.raise_alarm_with_dn.assert_called_once_with(
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')
149 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
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')
160 mock_alarm_handler.return_value.cancel_alarm_with_dn.assert_called_once_with(
165 if __name__ == '__main__':