blob: 0f96e7187672207ab3d8a1849efe87f0b4f55dac [file] [log] [blame]
Helena McGough94f93f72017-11-23 17:29:54 +00001# 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
24import json
Helena McGough94f93f72017-11-23 17:29:54 +000025import unittest
Helena McGough94f93f72017-11-23 17:29:54 +000026from BaseHTTPServer import BaseHTTPRequestHandler
27
Helena McGough94f93f72017-11-23 17:29:54 +000028import mock
29
Helena McGougheffeb7c2017-11-23 15:54:08 +000030from osm_mon.core.message_bus.producer import KafkaProducer
Benjamin Diaz75512472018-04-27 14:32:31 -030031from osm_mon.core.settings import Config
Helena McGougheffeb7c2017-11-23 15:54:08 +000032from osm_mon.plugins.OpenStack.Aodh.alarming import Alarming
33from osm_mon.plugins.OpenStack.common import Common
34from osm_mon.plugins.OpenStack.response import OpenStack_Response
Helena McGough94f93f72017-11-23 17:29:54 +000035
36# Mock data from post request
37post_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
48valid_get_resp = '{"gnocchi_resources_threshold_rule":\
49 {"resource_id": "my_resource_id"}}'
50
51invalid_get_resp = '{"gnocchi_resources_threshold_rule":\
Benjamin Diaz181cce82018-03-28 21:12:11 -030052 {"resource_id": null}}'
Helena McGough94f93f72017-11-23 17:29:54 +000053
54valid_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
63invalid_notify_resp = '{"notify_details": {"invalid":"mock_details"}'
64
65
66class 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
74class 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()
Benjamin Diaz181cce82018-03-28 21:12:11 -0300117 config.read_environ()
Helena McGough94f93f72017-11-23 17:29:54 +0000118 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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300124 vim_uuid = 'test_id'
125
126 auth_token = Common.get_auth_token(vim_uuid)
127 endpoint = Common.get_endpoint("alarming", vim_uuid)
Helena McGough94f93f72017-11-23 17:29:54 +0000128
129 # If authenticated generate and send response message
Benjamin Diaz181cce82018-03-28 21:12:11 -0300130 if auth_token is not None and endpoint is not None:
Helena McGough94f93f72017-11-23 17:29:54 +0000131 url = "{}/v2/alarms/%s".format(endpoint) % alarm_id
132
133 # Get the resource_id of the triggered alarm and the date
Benjamin Diaz181cce82018-03-28 21:12:11 -0300134 result = Common.perform_request(
Helena McGough94f93f72017-11-23 17:29:54 +0000135 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
156class 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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300170 set_head.assert_called_once()
Helena McGough94f93f72017-11-23 17:29:54 +0000171
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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300178 set_head.assert_called_once()
Helena McGough94f93f72017-11-23 17:29:54 +0000179 notify.assert_called_with(json.loads(post_data))
180
181 @mock.patch.object(Common, "get_endpoint")
Benjamin Diaz181cce82018-03-28 21:12:11 -0300182 @mock.patch.object(Common, "get_auth_token")
183 @mock.patch.object(Common, "perform_request")
Helena McGough94f93f72017-11-23 17:29:54 +0000184 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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300193 perf_req.assert_not_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000194
195 # Valid endpoint
196 auth.return_value = None
197 endpoint.return_value = "my_endpoint"
198 self.handler.notify_alarm(json.loads(post_data))
199
Benjamin Diaz181cce82018-03-28 21:12:11 -0300200 perf_req.assert_not_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000201
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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300207 perf_req.assert_not_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000208
209 @mock.patch.object(Common, "get_endpoint")
210 @mock.patch.object(OpenStack_Response, "generate_response")
Benjamin Diaz181cce82018-03-28 21:12:11 -0300211 @mock.patch.object(Common, "get_auth_token")
212 @mock.patch.object(Common, "perform_request")
Helena McGough94f93f72017-11-23 17:29:54 +0000213 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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300223 resp.assert_not_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000224
Benjamin Diaz181cce82018-03-28 21:12:11 -0300225 @mock.patch.object(KafkaProducer, "notify_alarm")
Helena McGough94f93f72017-11-23 17:29:54 +0000226 @mock.patch.object(Common, "get_endpoint")
227 @mock.patch.object(OpenStack_Response, "generate_response")
Benjamin Diaz181cce82018-03-28 21:12:11 -0300228 @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):
Helena McGough94f93f72017-11-23 17:29:54 +0000231 """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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300238 notify.assert_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000239 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")
Benjamin Diaz181cce82018-03-28 21:12:11 -0300248 @mock.patch.object(Common, "get_auth_token")
249 @mock.patch.object(Common, "perform_request")
250 @unittest.skip("Schema validation not implemented yet.")
Helena McGough94f93f72017-11-23 17:29:54 +0000251 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
Benjamin Diaz181cce82018-03-28 21:12:11 -0300262 notify.assert_not_called()
Helena McGough94f93f72017-11-23 17:29:54 +0000263
264 @mock.patch.object(Common, "get_endpoint")
265 @mock.patch.object(KafkaProducer, "notify_alarm")
266 @mock.patch.object(OpenStack_Response, "generate_response")
Benjamin Diaz181cce82018-03-28 21:12:11 -0300267 @mock.patch.object(Common, "get_auth_token")
268 @mock.patch.object(Common, "perform_request")
Helena McGough94f93f72017-11-23 17:29:54 +0000269 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")