Adds support for OSMMON_DATABASE_COMMONKEY to decrypt vim passwords
[osm/MON.git] / osm_mon / test / plugins / 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 alarm_handler 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.OpenstackAlarmHandler()
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 resp = Response({})
156 perf_req.return_value = resp
157
158 self.alarming.update_alarm_state(alarm_endpoint, auth_token, "my_alarm_id", True)
159
160 perf_req.assert_called_with(
161 "alarm_endpoint/v2/alarms/my_alarm_id/state", auth_token, req_type="put",
162 payload=json.dumps("ok"), verify_ssl=True)
163
164 @mock.patch.object(Common, "perform_request")
165 def test_update_alarm_invalid(self, perf_req):
166 """Test update alarm with invalid get response."""
167 values = {"alarm_uuid": "my_alarm_id"}
168
169 perf_req.return_value = type('obj', (object,), {'invalid_prop': 'Invalid response'})
170
171 with self.assertRaises(Exception):
172 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
173 perf_req.assert_called_with(mock.ANY, auth_token, req_type="get")
174
175 @mock.patch.object(Common, "perform_request")
176 def test_update_alarm_invalid_payload(self, perf_req):
177 """Test update alarm with invalid payload."""
178 resp = Response({"name": "my_alarm",
179 "state": "alarm",
180 "gnocchi_resources_threshold_rule":
181 {"resource_id": "my_resource_id",
182 "metric": "my_metric"}})
183 perf_req.return_value = resp
184 values = {"alarm_uuid": "my_alarm_id"}
185
186 with self.assertRaises(Exception):
187 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
188 perf_req.assert_called_with(mock.ANY, auth_token, req_type="get")
189 self.assertEqual(perf_req.call_count, 1)
190
191 @mock.patch.object(alarm_req.OpenstackAlarmHandler, "check_payload")
192 @mock.patch.object(Common, "perform_request")
193 def test_update_alarm_valid(self, perf_req, check_pay):
194 """Test valid update alarm request."""
195 resp = Response({"alarm_id": "1",
196 "name": "my_alarm",
197 "state": "alarm",
198 "gnocchi_resources_threshold_rule":
199 {"resource_id": "my_resource_id",
200 "metric": "disk.write.requests"}})
201 perf_req.return_value = resp
202 values = {"alarm_uuid": "my_alarm_id"}
203
204 self.alarming.update_alarm(alarm_endpoint, auth_token, values, {}, True)
205
206 check_pay.assert_called_with(values, "disk_write_ops", "my_resource_id",
207 "my_alarm", alarm_state="alarm")
208
209 self.assertEqual(perf_req.call_count, 2)
210 # Second call is the update request
211 perf_req.assert_called_with(
212 'alarm_endpoint/v2/alarms/my_alarm_id', auth_token,
213 req_type="put", payload=check_pay.return_value, verify_ssl=True)
214
215 @mock.patch.object(Config, "instance")
216 def test_check_valid_payload(self, cfg):
217 """Test the check payload function for a valid payload."""
218 values = {"severity": "warning",
219 "statistic": "COUNT",
220 "threshold_value": 12,
221 "operation": "GT",
222 "granularity": 300,
223 "resource_type": "generic"}
224 cfg.return_value.OS_NOTIFIER_URI = "http://localhost:8662"
225 payload = self.alarming.check_payload(
226 values, "disk_write_ops", "r_id", "alarm_name")
227
228 self.assertDictEqual(
229 json.loads(payload), {"name": "alarm_name",
230 "gnocchi_resources_threshold_rule":
231 {"resource_id": "r_id",
232 "metric": "disk.write.requests",
233 "comparison_operator": "gt",
234 "aggregation_method": "count",
235 "threshold": 12,
236 "granularity": 300,
237 "resource_type": "generic"},
238 "severity": "low",
239 "state": "ok",
240 "type": "gnocchi_resources_threshold",
241 "alarm_actions": ["http://localhost:8662"],
242 "repeat_actions": True})
243
244 @mock.patch.object(Config, "instance")
245 @mock.patch.object(Common, "perform_request")
246 def test_check_valid_state_payload(self, perform_req, cfg):
247 """Test the check payload function for a valid payload with state."""
248 values = {"severity": "warning",
249 "statistic": "COUNT",
250 "threshold_value": 12,
251 "operation": "GT",
252 "granularity": 300,
253 "resource_type": "generic"}
254 cfg.return_value.OS_NOTIFIER_URI = "http://localhost:8662"
255 payload = self.alarming.check_payload(
256 values, "disk_write_ops", "r_id", "alarm_name", alarm_state="alarm")
257
258 self.assertEqual(
259 json.loads(payload), {"name": "alarm_name",
260 "gnocchi_resources_threshold_rule":
261 {"resource_id": "r_id",
262 "metric": "disk.write.requests",
263 "comparison_operator": "gt",
264 "aggregation_method": "count",
265 "threshold": 12,
266 "granularity": 300,
267 "resource_type": "generic"},
268 "severity": "low",
269 "state": "alarm",
270 "type": "gnocchi_resources_threshold",
271 "alarm_actions": ["http://localhost:8662"],
272 "repeat_actions": True})
273
274 def test_check_invalid_payload(self):
275 """Test the check payload function for an invalid payload."""
276 values = {"alarm_values": "mock_invalid_details"}
277 with self.assertRaises(Exception):
278 self.alarming.check_payload(values, "my_metric", "r_id", "alarm_name")
279
280 @mock.patch.object(Common, "perform_request")
281 def test_get_alarm_state(self, perf_req):
282 """Test the get alarm state function."""
283 perf_req.return_value = type('obj', (object,), {'text': '{"alarm_id":"1"}'})
284
285 self.alarming.get_alarm_state(alarm_endpoint, auth_token, "alarm_id")
286
287 perf_req.assert_called_with(
288 "alarm_endpoint/v2/alarms/alarm_id/state", auth_token, req_type="get")
289
290 @mock.patch.object(Common, "perform_request")
291 def test_check_for_metric(self, perf_req):
292 """Test the check for metric function."""
293 mock_perf_req_return_value = {"metrics": {"cpu_util": 123}}
294 perf_req.return_value = type('obj', (object,), {'text': json.dumps(mock_perf_req_return_value)})
295
296 self.alarming.check_for_metric(auth_token, metric_endpoint, "cpu_utilization", "r_id", True)
297
298 perf_req.assert_called_with(
299 "metric_endpoint/v1/resource/generic/r_id", auth_token, req_type="get", verify_ssl=True)