Mock tests for vROPs plugin receiver module
[osm/MON.git] / osm_mon / test / OpenStack / test_metric_calls.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 metric request message keys."""
23
24 import json
25
26 import logging
27
28 import unittest
29
30 import mock
31
32 from osm_mon.plugins.OpenStack.Gnocchi import metrics as metric_req
33
34 from osm_mon.plugins.OpenStack.common import Common
35
36 log = logging.getLogger(__name__)
37
38 # Mock auth_token and endpoint
39 endpoint = mock.ANY
40 auth_token = mock.ANY
41
42 # Mock a valid metric list for some tests, and a resultant list
43 metric_list = [{"name": "disk_write_ops",
44 "id": "metric_id",
45 "unit": "units",
46 "resource_id": "r_id"}]
47 result_list = ["metric_id", "r_id", "units", "disk_write_ops"]
48
49
50 class Response(object):
51 """Mock a response object for requests."""
52
53 def __init__(self):
54 """Initialise test and status code values."""
55 self.text = json.dumps("mock_response_text")
56 self.status_code = "STATUS_CODE"
57
58
59 class TestMetricCalls(unittest.TestCase):
60 """Integration test for metric request keys."""
61
62 def setUp(self):
63 """Setup the tests for metric request keys."""
64 super(TestMetricCalls, self).setUp()
65 self.metrics = metric_req.Metrics()
66 self.metrics._common = Common()
67
68 @mock.patch.object(metric_req.Metrics, "get_metric_name")
69 @mock.patch.object(metric_req.Metrics, "get_metric_id")
70 @mock.patch.object(Common, "_perform_request")
71 def test_invalid_config_metric_req(
72 self, perf_req, get_metric, get_metric_name):
73 """Test the configure metric function, for an invalid metric."""
74 # Test invalid configuration for creating a metric
75 values = {"metric_details": "invalid_metric"}
76
77 m_id, r_id, status = self.metrics.configure_metric(
78 endpoint, auth_token, values)
79
80 perf_req.assert_not_called
81 self.assertEqual(m_id, None)
82 self.assertEqual(r_id, None)
83 self.assertEqual(status, False)
84
85 # Test with an invalid metric name, will not perform request
86 values = {"resource_uuid": "r_id"}
87 get_metric_name.return_value = "metric_name", None
88
89 m_id, r_id, status = self.metrics.configure_metric(
90 endpoint, auth_token, values)
91
92 perf_req.assert_not_called
93 self.assertEqual(m_id, None)
94 self.assertEqual(r_id, "r_id")
95 self.assertEqual(status, False)
96 get_metric_name.reset_mock()
97
98 # If metric exists, it won't be recreated
99 get_metric_name.return_value = "metric_name", "norm_name"
100 get_metric.return_value = "metric_id"
101
102 m_id, r_id, status = self.metrics.configure_metric(
103 endpoint, auth_token, values)
104
105 perf_req.assert_not_called
106 self.assertEqual(m_id, "metric_id")
107 self.assertEqual(r_id, "r_id")
108 self.assertEqual(status, False)
109
110 @mock.patch.object(metric_req.Metrics, "get_metric_name")
111 @mock.patch.object(metric_req.Metrics, "get_metric_id")
112 @mock.patch.object(Common, "_perform_request")
113 def test_valid_config_metric_req(
114 self, perf_req, get_metric, get_metric_name):
115 """Test the configure metric function, for a valid metric."""
116 # Test valid configuration and payload for creating a metric
117 values = {"resource_uuid": "r_id",
118 "metric_unit": "units"}
119 get_metric_name.return_value = "metric_name", "norm_name"
120 get_metric.return_value = None
121 payload = {"id": "r_id",
122 "metrics": {"metric_name":
123 {"archive_policy_name": "high",
124 "name": "metric_name",
125 "unit": "units"}}}
126
127 self.metrics.configure_metric(endpoint, auth_token, values)
128
129 perf_req.assert_called_with(
130 "<ANY>/v1/resource/generic", auth_token, req_type="post",
131 payload=json.dumps(payload))
132
133 @mock.patch.object(Common, "_perform_request")
134 def test_delete_metric_req(self, perf_req):
135 """Test the delete metric function."""
136 self.metrics.delete_metric(endpoint, auth_token, "metric_id")
137
138 perf_req.assert_called_with(
139 "<ANY>/v1/metric/metric_id", auth_token, req_type="delete")
140
141 @mock.patch.object(Common, "_perform_request")
142 def test_delete_metric_invalid_status(self, perf_req):
143 """Test invalid response for delete request."""
144 perf_req.return_value = "404"
145
146 status = self.metrics.delete_metric(endpoint, auth_token, "metric_id")
147
148 self.assertEqual(status, False)
149
150 @mock.patch.object(metric_req.Metrics, "response_list")
151 @mock.patch.object(Common, "_perform_request")
152 def test_complete_list_metric_req(self, perf_req, resp_list):
153 """Test the complete list metric function."""
154 # Test listing metrics without any configuration options
155 values = {}
156 resp = Response()
157 perf_req.return_value = resp
158 self.metrics.list_metrics(endpoint, auth_token, values)
159
160 perf_req.assert_called_with(
161 "<ANY>/v1/metric/", auth_token, req_type="get")
162 resp_list.assert_called_with("mock_response_text")
163
164 @mock.patch.object(metric_req.Metrics, "response_list")
165 @mock.patch.object(Common, "_perform_request")
166 def test_resource_list_metric_req(self, perf_req, resp_list):
167 """Test the resource list metric function."""
168 # Test listing metrics with a resource id specified
169 values = {"resource_uuid": "resource_id"}
170 resp = Response()
171 perf_req.return_value = resp
172 self.metrics.list_metrics(endpoint, auth_token, values)
173
174 perf_req.assert_called_with(
175 "<ANY>/v1/metric/", auth_token, req_type="get")
176 resp_list.assert_called_with(
177 "mock_response_text", resource="resource_id")
178
179 @mock.patch.object(metric_req.Metrics, "response_list")
180 @mock.patch.object(Common, "_perform_request")
181 def test_name_list_metric_req(self, perf_req, resp_list):
182 """Test the metric_name list metric function."""
183 # Test listing metrics with a metric_name specified
184 values = {"metric_name": "disk_write_bytes"}
185 resp = Response()
186 perf_req.return_value = resp
187 self.metrics.list_metrics(endpoint, auth_token, values)
188
189 perf_req.assert_called_with(
190 "<ANY>/v1/metric/", auth_token, req_type="get")
191 resp_list.assert_called_with(
192 "mock_response_text", metric_name="disk_write_bytes")
193
194 @mock.patch.object(metric_req.Metrics, "response_list")
195 @mock.patch.object(Common, "_perform_request")
196 def test_combined_list_metric_req(self, perf_req, resp_list):
197 """Test the combined resource and metric list metric function."""
198 # Test listing metrics with a resource id and metric name specified
199 values = {"resource_uuid": "resource_id",
200 "metric_name": "packets_sent"}
201 resp = Response()
202 perf_req.return_value = resp
203 self.metrics.list_metrics(endpoint, auth_token, values)
204
205 perf_req.assert_called_with(
206 "<ANY>/v1/metric/", auth_token, req_type="get")
207 resp_list.assert_called_with(
208 "mock_response_text", resource="resource_id",
209 metric_name="packets_sent")
210
211 @mock.patch.object(Common, "_perform_request")
212 def test_get_metric_id(self, perf_req):
213 """Test get_metric_id function."""
214 self.metrics.get_metric_id(endpoint, auth_token, "my_metric", "r_id")
215
216 perf_req.assert_called_with(
217 "<ANY>/v1/resource/generic/r_id", auth_token, req_type="get")
218
219 def test_get_metric_name(self):
220 """Test the result from the get_metric_name function."""
221 # test with a valid metric_name
222 values = {"metric_name": "disk_write_ops"}
223
224 metric_name, norm_name = self.metrics.get_metric_name(values)
225
226 self.assertEqual(metric_name, "disk_write_ops")
227 self.assertEqual(norm_name, "disk.disk_ops")
228
229 # test with an invalid metric name
230 values = {"metric_name": "my_invalid_metric"}
231
232 metric_name, norm_name = self.metrics.get_metric_name(values)
233
234 self.assertEqual(metric_name, "my_invalid_metric")
235 self.assertEqual(norm_name, None)
236
237 @mock.patch.object(Common, "_perform_request")
238 def test_valid_read_data_req(self, perf_req):
239 """Test the read metric data function, for a valid call."""
240 values = {"metric_uuid": "metric_id",
241 "collection_unit": "DAY",
242 "collection_period": 1}
243
244 self.metrics.read_metric_data(endpoint, auth_token, values)
245
246 perf_req.assert_called_once
247
248 @mock.patch.object(Common, "_perform_request")
249 def test_invalid_read_data_req(self, perf_req):
250 """Test the read metric data function, for an invalid call."""
251 # Teo empty lists wil be returned because the values are invalid
252 values = {}
253
254 times, data = self.metrics.read_metric_data(
255 endpoint, auth_token, values)
256
257 self.assertEqual(times, [])
258 self.assertEqual(data, [])
259
260 def test_complete_response_list(self):
261 """Test the response list function for formating metric lists."""
262 # Mock a list for testing purposes, with valid OSM metric
263 resp_list = self.metrics.response_list(metric_list)
264
265 # Check for the expected values in the resulting list
266 for l in result_list:
267 self.assertIn(l, resp_list[0])
268
269 def test_name_response_list(self):
270 """Test the response list with metric name configured."""
271 # Mock the metric name to test a metric name list
272 # Test with a name that is not in the list
273 invalid_name = "my_metric"
274 resp_list = self.metrics.response_list(
275 metric_list, metric_name=invalid_name)
276
277 self.assertEqual(resp_list, [])
278
279 # Test with a name on the list
280 valid_name = "disk_write_ops"
281 resp_list = self.metrics.response_list(
282 metric_list, metric_name=valid_name)
283
284 # Check for the expected values in the resulting list
285 for l in result_list:
286 self.assertIn(l, resp_list[0])
287
288 def test_resource_response_list(self):
289 """Test the response list with resource_id configured."""
290 # Mock a resource_id to test a resource list
291 # Test with resource not on the list
292 invalid_id = "mock_resource"
293 resp_list = self.metrics.response_list(metric_list, resource=invalid_id)
294
295 self.assertEqual(resp_list, [])
296
297 # Test with a resource on the list
298 valid_id = "r_id"
299 resp_list = self.metrics.response_list(metric_list, resource=valid_id)
300
301 # Check for the expected values in the resulting list
302 for l in result_list:
303 self.assertIn(l, resp_list[0])
304
305 def test_combined_response_list(self):
306 """Test the response list function with resource_id and metric_name."""
307 # Test for a combined resource and name list
308 # resource and name are on the lisat
309 valid_name = "disk_write_ops"
310 valid_id = "r_id"
311 resp_list = self.metrics.response_list(
312 metric_list, metric_name=valid_name, resource=valid_id)
313
314 # Check for the expected values in the resulting list
315 for l in result_list:
316 self.assertIn(l, resp_list[0])
317
318 # resource not on list
319 invalid_id = "mock_resource"
320 resp_list = self.metrics.response_list(
321 metric_list, metric_name=valid_name, resource=invalid_id)
322
323 self.assertEqual(resp_list, [])
324
325 # metric name not on list
326 invalid_name = "mock_metric"
327 resp_list = self.metrics.response_list(
328 metric_list, metric_name=invalid_name, resource=valid_id)
329
330 self.assertEqual(resp_list, [])