Adds vdu_id to message bus models
[osm/MON.git] / osm_mon / test / OpenStack / unit / test_notifier.py
1 # Copyright 2017 Intel 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 common OpenStack methods."""
23
24 import json
25 import unittest
26
27 import mock
28 from six.moves.BaseHTTPServer import BaseHTTPRequestHandler
29
30 from osm_mon.core.message_bus.producer import KafkaProducer
31 from osm_mon.core.settings import Config
32 from osm_mon.plugins.OpenStack.Aodh.alarming import Alarming
33 from osm_mon.plugins.OpenStack.common import Common
34 from osm_mon.plugins.OpenStack.response import OpenStack_Response
35
36 # Mock data from post request
37 post_data = json.dumps({"severity": "critical",
38 "alarm_name": "my_alarm",
39 "current": "current_state",
40 "alarm_id": "my_alarm_id",
41 "reason": "Threshold has been broken",
42 "reason_data": {"count": 1,
43 "most_recent": "null",
44 "type": "threshold",
45 "disposition": "unknown"},
46 "previous": "previous_state"})
47
48 valid_get_resp = '{"gnocchi_resources_threshold_rule":\
49 {"resource_id": "my_resource_id"}}'
50
51 invalid_get_resp = '{"gnocchi_resources_threshold_rule":\
52 {"resource_id": null}}'
53
54 valid_notify_resp = '{"notify_details": {"status": "current_state",\
55 "severity": "critical",\
56 "resource_uuid": "my_resource_id",\
57 "alarm_uuid": "my_alarm_id",\
58 "vim_type": "OpenStack",\
59 "start_date": "dd-mm-yyyy 00:00"},\
60 "schema_version": "1.0",\
61 "schema_type": "notify_alarm"}'
62
63 invalid_notify_resp = '{"notify_details": {"invalid":"mock_details"}'
64
65
66 class Response(object):
67 """Mock a response class for generating responses."""
68
69 def __init__(self, text):
70 """Initialise a mock response with a text attribute."""
71 self.text = text
72
73
74 class NotifierHandler(BaseHTTPRequestHandler):
75 """Mock the NotifierHandler class for testing purposes."""
76
77 def __init__(self, request, client_address, server):
78 """Initilase mock NotifierHandler."""
79 self.request = request
80 self.client_address = client_address
81 self.server = server
82 self.setup()
83 try:
84 self.handle()
85 finally:
86 self.finish()
87
88 def setup(self):
89 """Mock setup function."""
90 pass
91
92 def handle(self):
93 """Mock handle function."""
94 pass
95
96 def finish(self):
97 """Mock finish function."""
98 pass
99
100 def _set_headers(self):
101 """Mock getting the request headers."""
102 pass
103
104 def do_GET(self):
105 """Mock functionality for GET request."""
106 self._set_headers()
107 pass
108
109 def do_POST(self):
110 """Mock functionality for a POST request."""
111 self._set_headers()
112 self.notify_alarm(json.loads(post_data))
113
114 def notify_alarm(self, values):
115 """Mock the notify_alarm functionality to generate a valid response."""
116 config = Config.instance()
117 config.read_environ()
118 self._alarming = Alarming()
119 self._common = Common()
120 self._response = OpenStack_Response()
121 self._producer = KafkaProducer('alarm_response')
122 alarm_id = values['alarm_id']
123
124 vim_uuid = 'test_id'
125
126 auth_token = Common.get_auth_token(vim_uuid)
127 endpoint = Common.get_endpoint("alarming", vim_uuid)
128
129 # If authenticated generate and send response message
130 if auth_token is not None and endpoint is not None:
131 url = "{}/v2/alarms/%s".format(endpoint) % alarm_id
132
133 # Get the resource_id of the triggered alarm and the date
134 result = Common.perform_request(
135 url, auth_token, req_type="get")
136 alarm_details = json.loads(result.text)
137 gnocchi_rule = alarm_details['gnocchi_resources_threshold_rule']
138 resource_id = gnocchi_rule['resource_id']
139 a_date = "dd-mm-yyyy 00:00"
140
141 # Process an alarm notification if resource_id is valid
142 if resource_id is not None:
143 # Try generate and send response
144 try:
145 resp_message = self._response.generate_response(
146 'notify_alarm', a_id=alarm_id,
147 r_id=resource_id,
148 sev=values['severity'], date=a_date,
149 state=values['current'], vim_type="OpenStack")
150 self._producer.notify_alarm(
151 'notify_alarm', resp_message, 'alarm_response')
152 except Exception:
153 pass
154
155
156 class TestNotifier(unittest.TestCase):
157 """Test the NotifierHandler class for requests from aodh."""
158
159 def setUp(self):
160 """Setup tests."""
161 super(TestNotifier, self).setUp()
162 self.handler = NotifierHandler(
163 "mock_request", "mock_address", "mock_server")
164
165 @mock.patch.object(NotifierHandler, "_set_headers")
166 def test_do_GET(self, set_head):
167 """Test do_GET, generates headers for get request."""
168 self.handler.do_GET()
169
170 set_head.assert_called_once()
171
172 @mock.patch.object(NotifierHandler, "notify_alarm")
173 @mock.patch.object(NotifierHandler, "_set_headers")
174 def test_do_POST(self, set_head, notify):
175 """Test do_POST functionality for a POST request."""
176 self.handler.do_POST()
177
178 set_head.assert_called_once()
179 notify.assert_called_with(json.loads(post_data))
180
181 @mock.patch.object(Common, "get_endpoint")
182 @mock.patch.object(Common, "get_auth_token")
183 @mock.patch.object(Common, "perform_request")
184 def test_notify_alarm_unauth(self, perf_req, auth, endpoint):
185 """Test notify alarm when not authenticated with keystone."""
186 # Response request will not be performed unless there is a valid
187 # auth_token and endpoint
188 # Invalid auth_token and endpoint
189 auth.return_value = None
190 endpoint.return_value = None
191 self.handler.notify_alarm(json.loads(post_data))
192
193 perf_req.assert_not_called()
194
195 # Valid endpoint
196 auth.return_value = None
197 endpoint.return_value = "my_endpoint"
198 self.handler.notify_alarm(json.loads(post_data))
199
200 perf_req.assert_not_called()
201
202 # Valid auth_token
203 auth.return_value = "my_auth_token"
204 endpoint.return_value = None
205 self.handler.notify_alarm(json.loads(post_data))
206
207 perf_req.assert_not_called()
208
209 @mock.patch.object(Common, "get_endpoint")
210 @mock.patch.object(OpenStack_Response, "generate_response")
211 @mock.patch.object(Common, "get_auth_token")
212 @mock.patch.object(Common, "perform_request")
213 def test_notify_alarm_invalid_alarm(self, perf_req, auth, resp, endpoint):
214 """Test valid authentication, invalid alarm details."""
215 # Mock valid auth_token and endpoint
216 auth.return_value = "my_auth_token"
217 endpoint.return_value = "my_endpoint"
218 perf_req.return_value = Response(invalid_get_resp)
219
220 self.handler.notify_alarm(json.loads(post_data))
221
222 # Response is not generated
223 resp.assert_not_called()
224
225 @mock.patch.object(KafkaProducer, "notify_alarm")
226 @mock.patch.object(Common, "get_endpoint")
227 @mock.patch.object(OpenStack_Response, "generate_response")
228 @mock.patch.object(Common, "get_auth_token")
229 @mock.patch.object(Common, "perform_request")
230 def test_notify_alarm_resp_call(self, perf_req, auth, response, endpoint, notify):
231 """Test notify_alarm tries to generate a response for SO."""
232 # Mock valid auth token and endpoint, valid response from aodh
233 auth.return_value = "my_auth_token"
234 endpoint.returm_value = "my_endpoint"
235 perf_req.return_value = Response(valid_get_resp)
236 self.handler.notify_alarm(json.loads(post_data))
237
238 notify.assert_called()
239 response.assert_called_with('notify_alarm', a_id="my_alarm_id",
240 r_id="my_resource_id", sev="critical",
241 date="dd-mm-yyyy 00:00",
242 state="current_state",
243 vim_type="OpenStack")
244
245 @mock.patch.object(Common, "get_endpoint")
246 @mock.patch.object(KafkaProducer, "notify_alarm")
247 @mock.patch.object(OpenStack_Response, "generate_response")
248 @mock.patch.object(Common, "get_auth_token")
249 @mock.patch.object(Common, "perform_request")
250 @unittest.skip("Schema validation not implemented yet.")
251 def test_notify_alarm_invalid_resp(
252 self, perf_req, auth, response, notify, endpoint):
253 """Test the notify_alarm function, sends response to the producer."""
254 # Generate return values for valid notify_alarm operation
255 auth.return_value = "my_auth_token"
256 endpoint.return_value = "my_endpoint"
257 perf_req.return_value = Response(valid_get_resp)
258 response.return_value = invalid_notify_resp
259
260 self.handler.notify_alarm(json.loads(post_data))
261
262 notify.assert_not_called()
263
264 @mock.patch.object(Common, "get_endpoint")
265 @mock.patch.object(KafkaProducer, "notify_alarm")
266 @mock.patch.object(OpenStack_Response, "generate_response")
267 @mock.patch.object(Common, "get_auth_token")
268 @mock.patch.object(Common, "perform_request")
269 def test_notify_alarm_valid_resp(
270 self, perf_req, auth, response, notify, endpoint):
271 """Test the notify_alarm function, sends response to the producer."""
272 # Generate return values for valid notify_alarm operation
273 auth.return_value = "my_auth_token"
274 endpoint.return_value = "my_endpoint"
275 perf_req.return_value = Response(valid_get_resp)
276 response.return_value = valid_notify_resp
277
278 self.handler.notify_alarm(json.loads(post_data))
279
280 notify.assert_called_with(
281 "notify_alarm", valid_notify_resp, "alarm_response")