1 # Copyright 2017 iIntel Research and Development Ireland Limited
2 # **************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
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
22 """Tests for all alarm request message keys."""
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
34 log
= logging
.getLogger(__name__
)
37 alarm_endpoint
= "alarm_endpoint"
38 metric_endpoint
= "metric_endpoint"
41 class Response(object):
42 """Mock a response message class."""
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"
50 class TestAlarming(unittest
.TestCase
):
51 """Tests for alarming class functions."""
56 """Setup for tests."""
57 super(TestAlarming
, self
).setUp()
58 self
.alarming
= alarm_req
.OpenstackAlarmHandler()
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()
72 # Configuring with missing metric name results in failure
73 values
= {"alarm_name": "disk_write_ops",
74 "resource_uuid": "my_r_id"}
76 with self
.assertRaises(KeyError):
77 self
.alarming
.configure_alarm(alarm_endpoint
, auth_token
, values
, {}, True)
78 perf_req
.assert_not_called()
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,
90 perf_req
.return_value
= type('obj', (object,), {'text': '{"alarm_id":"1"}'})
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)
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)
109 perf_req
.assert_called_with(
110 "alarm_endpoint/v2/alarms/my_alarm_id", auth_token
, req_type
="delete", verify_ssl
=True)
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()
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"}
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
)})
133 alarm_list
= self
.alarming
.list_alarms(alarm_endpoint
, auth_token
, list_details
, True)
135 self
.assertDictEqual(alarm_list
[0], mock_perf_req_return_value
[0])
137 perf_req
.assert_called_with(
138 "alarm_endpoint/v2/alarms/", auth_token
, req_type
="get", verify_ssl
=True)
139 perf_req
.reset_mock()
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)
147 self
.assertDictEqual(alarm_list
[0], mock_perf_req_return_value
[0])
149 perf_req
.assert_called_with(
150 "alarm_endpoint/v2/alarms/", auth_token
, req_type
="get", verify_ssl
=True)
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."""
156 perf_req
.return_value
= resp
158 self
.alarming
.update_alarm_state(alarm_endpoint
, auth_token
, "my_alarm_id", True)
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)
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"}
169 perf_req
.return_value
= type('obj', (object,), {'invalid_prop': 'Invalid response'})
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")
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",
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"}
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)
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",
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"}
204 self
.alarming
.update_alarm(alarm_endpoint
, auth_token
, values
, {}, True)
206 check_pay
.assert_called_with(values
, "disk_write_ops", "my_resource_id",
207 "my_alarm", alarm_state
="alarm")
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)
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,
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")
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",
237 "resource_type": "generic"},
240 "type": "gnocchi_resources_threshold",
241 "alarm_actions": ["http://localhost:8662"],
242 "repeat_actions": True})
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,
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")
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",
267 "resource_type": "generic"},
270 "type": "gnocchi_resources_threshold",
271 "alarm_actions": ["http://localhost:8662"],
272 "repeat_actions": True})
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")
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"}'})
285 self
.alarming
.get_alarm_state(alarm_endpoint
, auth_token
, "alarm_id")
287 perf_req
.assert_called_with(
288 "alarm_endpoint/v2/alarms/alarm_id/state", auth_token
, req_type
="get")
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
)})
296 self
.alarming
.check_for_metric(auth_token
, metric_endpoint
, "cpu_utilization", "r_id", True)
298 perf_req
.assert_called_with(
299 "metric_endpoint/v1/resource/generic/r_id", auth_token
, req_type
="get", verify_ssl
=True)