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