7368f13e25109b2f9a40b5e9a04a02ad1d529ac8
[osm/MON.git] / osm_mon / test / OpenStack / unit / test_alarming.py
1 # Copyright 2017 iIntel Research and Development Ireland Limited
2 # **************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Tests for all alarm request message keys."""
23
24 import json
25 import logging
26 import unittest
27
28 import mock
29
30 from osm_mon.core.settings import Config
31 from osm_mon.plugins.OpenStack.Aodh import alarming as alarm_req
32 from osm_mon.plugins.OpenStack.common import Common
33
34 log = logging.getLogger(__name__)
35
36 auth_token = mock.ANY
37 alarm_endpoint = "alarm_endpoint"
38 metric_endpoint = "metric_endpoint"
39
40
41 class Response(object):
42 """Mock a response message class."""
43
44 def __init__(self, result):
45 """Initialise the response text and status code."""
46 self.text = json.dumps(result)
47 self.status_code = "MOCK_STATUS_CODE"
48
49
50 class TestAlarming(unittest.TestCase):
51 """Tests for alarming class functions."""
52
53 maxDiff = None
54
55 def setUp(self):
56 """Setup for tests."""
57 super(TestAlarming, self).setUp()
58 self.alarming = alarm_req.Alarming()
59
60 @mock.patch.object(Common, "perform_request")
61 def test_config_invalid_alarm_req(self, perf_req):
62 """Test configure an invalid alarm request."""
63 # Configuring with invalid metric name results in failure
64 values = {"alarm_name": "my_alarm",
65 "metric_name": "my_metric",
66 "resource_uuid": "my_r_id"}
67 with self.assertRaises(KeyError):
68 self.alarming.configure_alarm(alarm_endpoint, auth_token, values, {}, True)
69 perf_req.assert_not_called()
70 perf_req.reset_mock()
71
72 # Configuring with missing metric name results in failure
73 values = {"alarm_name": "disk_write_ops",
74 "resource_uuid": "my_r_id"}
75
76 with self.assertRaises(KeyError):
77 self.alarming.configure_alarm(alarm_endpoint, auth_token, values, {}, True)
78 perf_req.assert_not_called()
79
80 @mock.patch.object(Common, "perform_request")
81 def test_config_valid_alarm_req(self, perf_req):
82 """Test config a valid alarm."""
83 values = {"alarm_name": "disk_write_ops",
84 "metric_name": "disk_write_ops",
85 "resource_uuid": "my_r_id",
86 "statistic": "AVERAGE",
87 "threshold_value": 60,
88 "operation": "GT"}
89
90 perf_req.return_value = type('obj', (object,), {'text': '{"alarm_id":"1"}'})
91
92 self.alarming.configure_alarm(alarm_endpoint, auth_token, values, {}, True)
93 payload = {"name": "disk_write_ops",
94 "gnocchi_resources_threshold_rule": {"resource_type": "generic", "comparison_operator": "gt",
95 "granularity": "300", "metric": "disk.write.requests",
96 "aggregation_method": "mean", "threshold": 60,
97 "resource_id": "my_r_id"},
98 "alarm_actions": ["http://localhost:8662"], "repeat_actions": True, "state": "ok", "type": "gnocchi_resources_threshold",
99 "severity": "critical"}
100 perf_req.assert_called_with(
101 "alarm_endpoint/v2/alarms/", auth_token,
102 req_type="post", payload=json.dumps(payload, sort_keys=True), verify_ssl=True)
103
104 @mock.patch.object(Common, "perform_request")
105 def test_delete_alarm_req(self, perf_req):
106 """Test delete alarm request."""
107 self.alarming.delete_alarm(alarm_endpoint, auth_token, "my_alarm_id", True)
108
109 perf_req.assert_called_with(
110 "alarm_endpoint/v2/alarms/my_alarm_id", auth_token, req_type="delete", verify_ssl=True)
111
112 @mock.patch.object(Common, "perform_request")
113 def test_invalid_list_alarm_req(self, perf_req):
114 """Test invalid list alarm_req."""
115 # Request will not be performed without a resource_id
116 list_details = {"mock_details": "invalid_details"}
117 with self.assertRaises(KeyError):
118 self.alarming.list_alarms(alarm_endpoint, auth_token, list_details, True)
119 perf_req.assert_not_called()
120
121 @mock.patch.object(Common, "perform_request")
122 def test_valid_list_alarm_req(self, perf_req):
123 """Test valid list alarm request."""
124 # Minimum requirement for an alarm list is resource_id
125 list_details = {"resource_uuid": "mock_r_id", "alarm_name": "mock_alarm", "severity": "critical"}
126
127 mock_perf_req_return_value = [
128 {"alarm_id": "1", "name": "mock_alarm", "severity": "critical",
129 "gnocchi_resources_threshold_rule": {"resource_id": "mock_r_id"}}]
130 perf_req.return_value = type('obj', (object,),
131 {'text': json.dumps(mock_perf_req_return_value)})
132
133 alarm_list = self.alarming.list_alarms(alarm_endpoint, auth_token, list_details, True)
134
135 self.assertDictEqual(alarm_list[0], mock_perf_req_return_value[0])
136
137 perf_req.assert_called_with(
138 "alarm_endpoint/v2/alarms/", auth_token, req_type="get", verify_ssl=True)
139 perf_req.reset_mock()
140
141 # Check list with alarm_name defined
142 list_details = {"resource_uuid": "mock_r_id",
143 "alarm_name": "mock_alarm",
144 "severity": "critical"}
145 alarm_list = self.alarming.list_alarms(alarm_endpoint, auth_token, list_details, True)
146
147 self.assertDictEqual(alarm_list[0], mock_perf_req_return_value[0])
148
149 perf_req.assert_called_with(
150 "alarm_endpoint/v2/alarms/", auth_token, req_type="get", verify_ssl=True)
151
152 @mock.patch.object(Common, "perform_request")
153 def test_ack_alarm_req(self, perf_req):
154 """Test update alarm state for acknowledge alarm request."""
155 self.alarming.update_alarm_state(alarm_endpoint, auth_token, "my_alarm_id", True)
156
157 perf_req.assert_called_with(
158 "alarm_endpoint/v2/alarms/my_alarm_id/state", auth_token, req_type="put",
159 payload=json.dumps("ok"), verify_ssl=True)
160
161 @mock.patch.object(Common, "perform_request")
162 def test_update_alarm_invalid(self, perf_req):
163 """Test update alarm with invalid get response."""
164 values = {"alarm_uuid": "my_alarm_id"}
165
166 perf_req.return_value = type('obj', (object,), {'invalid_prop': 'Invalid response'})
167
168 with self.assertRaises(Exception):
169 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
170 perf_req.assert_called_with(mock.ANY, auth_token, req_type="get")
171
172 @mock.patch.object(Common, "perform_request")
173 def test_update_alarm_invalid_payload(self, perf_req):
174 """Test update alarm with invalid payload."""
175 resp = Response({"name": "my_alarm",
176 "state": "alarm",
177 "gnocchi_resources_threshold_rule":
178 {"resource_id": "my_resource_id",
179 "metric": "my_metric"}})
180 perf_req.return_value = resp
181 values = {"alarm_uuid": "my_alarm_id"}
182
183 with self.assertRaises(Exception):
184 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
185 perf_req.assert_called_with(mock.ANY, auth_token, req_type="get")
186 self.assertEqual(perf_req.call_count, 1)
187
188 @mock.patch.object(alarm_req.Alarming, "check_payload")
189 @mock.patch.object(Common, "perform_request")
190 def test_update_alarm_valid(self, perf_req, check_pay):
191 """Test valid update alarm request."""
192 resp = Response({"alarm_id": "1",
193 "name": "my_alarm",
194 "state": "alarm",
195 "gnocchi_resources_threshold_rule":
196 {"resource_id": "my_resource_id",
197 "metric": "disk.write.requests"}})
198 perf_req.return_value = resp
199 values = {"alarm_uuid": "my_alarm_id"}
200
201 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
202
203 check_pay.assert_called_with(values, "disk_write_ops", "my_resource_id",
204 "my_alarm", alarm_state="alarm")
205
206 self.assertEqual(perf_req.call_count, 2)
207 # Second call is the update request
208 perf_req.assert_called_with(
209 'alarm_endpoint/v2/alarms/my_alarm_id', auth_token,
210 req_type="put", payload=check_pay.return_value, verify_ssl=True)
211
212 @mock.patch.object(Config, "instance")
213 def test_check_valid_payload(self, cfg):
214 """Test the check payload function for a valid payload."""
215 values = {"severity": "warning",
216 "statistic": "COUNT",
217 "threshold_value": 12,
218 "operation": "GT",
219 "granularity": 300,
220 "resource_type": "generic"}
221 cfg.return_value.OS_NOTIFIER_URI = "http://localhost:8662"
222 payload = self.alarming.check_payload(
223 values, "disk_write_ops", "r_id", "alarm_name")
224
225 self.assertDictEqual(
226 json.loads(payload), {"name": "alarm_name",
227 "gnocchi_resources_threshold_rule":
228 {"resource_id": "r_id",
229 "metric": "disk.write.requests",
230 "comparison_operator": "gt",
231 "aggregation_method": "count",
232 "threshold": 12,
233 "granularity": 300,
234 "resource_type": "generic"},
235 "severity": "low",
236 "state": "ok",
237 "type": "gnocchi_resources_threshold",
238 "alarm_actions": ["http://localhost:8662"],
239 "repeat_actions": True})
240
241 @mock.patch.object(Config, "instance")
242 @mock.patch.object(Common, "perform_request")
243 def test_check_valid_state_payload(self, perform_req, cfg):
244 """Test the check payload function for a valid payload with state."""
245 values = {"severity": "warning",
246 "statistic": "COUNT",
247 "threshold_value": 12,
248 "operation": "GT",
249 "granularity": 300,
250 "resource_type": "generic"}
251 cfg.return_value.OS_NOTIFIER_URI = "http://localhost:8662"
252 payload = self.alarming.check_payload(
253 values, "disk_write_ops", "r_id", "alarm_name", alarm_state="alarm")
254
255 self.assertEqual(
256 json.loads(payload), {"name": "alarm_name",
257 "gnocchi_resources_threshold_rule":
258 {"resource_id": "r_id",
259 "metric": "disk.write.requests",
260 "comparison_operator": "gt",
261 "aggregation_method": "count",
262 "threshold": 12,
263 "granularity": 300,
264 "resource_type": "generic"},
265 "severity": "low",
266 "state": "alarm",
267 "type": "gnocchi_resources_threshold",
268 "alarm_actions": ["http://localhost:8662"],
269 "repeat_actions": True})
270
271 def test_check_invalid_payload(self):
272 """Test the check payload function for an invalid payload."""
273 values = {"alarm_values": "mock_invalid_details"}
274 with self.assertRaises(Exception):
275 self.alarming.check_payload(values, "my_metric", "r_id", "alarm_name")
276
277 @mock.patch.object(Common, "perform_request")
278 def test_get_alarm_state(self, perf_req):
279 """Test the get alarm state function."""
280 perf_req.return_value = type('obj', (object,), {'text': '{"alarm_id":"1"}'})
281
282 self.alarming.get_alarm_state(alarm_endpoint, auth_token, "alarm_id")
283
284 perf_req.assert_called_with(
285 "alarm_endpoint/v2/alarms/alarm_id/state", auth_token, req_type="get")
286
287 @mock.patch.object(Common, "perform_request")
288 def test_check_for_metric(self, perf_req):
289 """Test the check for metric function."""
290 mock_perf_req_return_value = {"metrics": {"cpu_util": 123}}
291 perf_req.return_value = type('obj', (object,), {'text': json.dumps(mock_perf_req_return_value)})
292
293 self.alarming.check_for_metric(auth_token, metric_endpoint, "cpu_utilization", "r_id", True)
294
295 perf_req.assert_called_with(
296 "metric_endpoint/v1/resource/generic/r_id", auth_token, req_type="get", verify_ssl=True)