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 metric request message keys."""
32 from plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
34 from plugins
.OpenStack
.common
import Common
36 __author__
= "Helena McGough"
38 log
= logging
.getLogger(__name__
)
40 # Mock auth_token and endpoint
44 # Mock a valid metric list for some tests, and a resultant list
45 metric_list
= [{"name": "disk_write_ops",
48 "resource_id": "r_id"}]
49 result_list
= ["metric_id", "r_id", "units", "disk_write_ops"]
52 class Response(object):
53 """Mock a response object for requests."""
56 """Initialise test and status code values."""
57 self
.text
= json
.dumps("mock_response_text")
58 self
.status_code
= "STATUS_CODE"
61 class TestMetricCalls(unittest
.TestCase
):
62 """Integration test for metric request keys."""
65 """Setup the tests for metric request keys."""
66 super(TestMetricCalls
, self
).setUp()
67 self
.metrics
= metric_req
.Metrics()
68 self
.metrics
._common
= Common()
70 @mock.patch
.object(metric_req
.Metrics
, "get_metric_name")
71 @mock.patch
.object(metric_req
.Metrics
, "get_metric_id")
72 @mock.patch
.object(Common
, "_perform_request")
73 def test_invalid_config_metric_req(
74 self
, perf_req
, get_metric
, get_metric_name
):
75 """Test the configure metric function, for an invalid metric."""
76 # Test invalid configuration for creating a metric
77 values
= {"metric_details": "invalid_metric"}
79 m_id
, r_id
, status
= self
.metrics
.configure_metric(
80 endpoint
, auth_token
, values
)
82 perf_req
.assert_not_called
83 self
.assertEqual(m_id
, None)
84 self
.assertEqual(r_id
, None)
85 self
.assertEqual(status
, False)
87 # Test with an invalid metric name, will not perform request
88 values
= {"resource_uuid": "r_id"}
89 get_metric_name
.return_value
= "metric_name", None
91 m_id
, r_id
, status
= self
.metrics
.configure_metric(
92 endpoint
, auth_token
, values
)
94 perf_req
.assert_not_called
95 self
.assertEqual(m_id
, None)
96 self
.assertEqual(r_id
, "r_id")
97 self
.assertEqual(status
, False)
98 get_metric_name
.reset_mock()
100 # If metric exists, it won't be recreated
101 get_metric_name
.return_value
= "metric_name", "norm_name"
102 get_metric
.return_value
= "metric_id"
104 m_id
, r_id
, status
= self
.metrics
.configure_metric(
105 endpoint
, auth_token
, values
)
107 perf_req
.assert_not_called
108 self
.assertEqual(m_id
, "metric_id")
109 self
.assertEqual(r_id
, "r_id")
110 self
.assertEqual(status
, False)
112 @mock.patch
.object(metric_req
.Metrics
, "get_metric_name")
113 @mock.patch
.object(metric_req
.Metrics
, "get_metric_id")
114 @mock.patch
.object(Common
, "_perform_request")
115 def test_valid_config_metric_req(
116 self
, perf_req
, get_metric
, get_metric_name
):
117 """Test the configure metric function, for a valid metric."""
118 # Test valid configuration and payload for creating a metric
119 values
= {"resource_uuid": "r_id",
120 "metric_unit": "units"}
121 get_metric_name
.return_value
= "metric_name", "norm_name"
122 get_metric
.return_value
= None
123 payload
= {"id": "r_id",
124 "metrics": {"metric_name":
125 {"archive_policy_name": "high",
126 "name": "metric_name",
129 self
.metrics
.configure_metric(endpoint
, auth_token
, values
)
131 perf_req
.assert_called_with(
132 "<ANY>/v1/resource/generic", auth_token
, req_type
="post",
133 payload
=json
.dumps(payload
))
135 @mock.patch
.object(Common
, "_perform_request")
136 def test_delete_metric_req(self
, perf_req
):
137 """Test the delete metric function."""
138 self
.metrics
.delete_metric(endpoint
, auth_token
, "metric_id")
140 perf_req
.assert_called_with(
141 "<ANY>/v1/metric/metric_id", auth_token
, req_type
="delete")
143 @mock.patch
.object(Common
, "_perform_request")
144 def test_delete_metric_invalid_status(self
, perf_req
):
145 """Test invalid response for delete request."""
146 perf_req
.return_value
= "404"
148 status
= self
.metrics
.delete_metric(endpoint
, auth_token
, "metric_id")
150 self
.assertEqual(status
, False)
152 @mock.patch
.object(metric_req
.Metrics
, "response_list")
153 @mock.patch
.object(Common
, "_perform_request")
154 def test_complete_list_metric_req(self
, perf_req
, resp_list
):
155 """Test the complete list metric function."""
156 # Test listing metrics without any configuration options
159 perf_req
.return_value
= resp
160 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
162 perf_req
.assert_called_with(
163 "<ANY>/v1/metric/", auth_token
, req_type
="get")
164 resp_list
.assert_called_with("mock_response_text")
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"}
173 perf_req
.return_value
= resp
174 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
176 perf_req
.assert_called_with(
177 "<ANY>/v1/metric/", auth_token
, req_type
="get")
178 resp_list
.assert_called_with(
179 "mock_response_text", resource
="resource_id")
181 @mock.patch
.object(metric_req
.Metrics
, "response_list")
182 @mock.patch
.object(Common
, "_perform_request")
183 def test_name_list_metric_req(self
, perf_req
, resp_list
):
184 """Test the metric_name list metric function."""
185 # Test listing metrics with a metric_name specified
186 values
= {"metric_name": "disk_write_bytes"}
188 perf_req
.return_value
= resp
189 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
191 perf_req
.assert_called_with(
192 "<ANY>/v1/metric/", auth_token
, req_type
="get")
193 resp_list
.assert_called_with(
194 "mock_response_text", metric_name
="disk_write_bytes")
196 @mock.patch
.object(metric_req
.Metrics
, "response_list")
197 @mock.patch
.object(Common
, "_perform_request")
198 def test_combined_list_metric_req(self
, perf_req
, resp_list
):
199 """Test the combined resource and metric list metric function."""
200 # Test listing metrics with a resource id and metric name specified
201 values
= {"resource_uuid": "resource_id",
202 "metric_name": "packets_sent"}
204 perf_req
.return_value
= resp
205 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
207 perf_req
.assert_called_with(
208 "<ANY>/v1/metric/", auth_token
, req_type
="get")
209 resp_list
.assert_called_with(
210 "mock_response_text", resource
="resource_id",
211 metric_name
="packets_sent")
213 @mock.patch
.object(Common
, "_perform_request")
214 def test_get_metric_id(self
, perf_req
):
215 """Test get_metric_id function."""
216 self
.metrics
.get_metric_id(endpoint
, auth_token
, "my_metric", "r_id")
218 perf_req
.assert_called_with(
219 "<ANY>/v1/resource/generic/r_id", auth_token
, req_type
="get")
221 def test_get_metric_name(self
):
222 """Test the result from the get_metric_name function."""
223 # test with a valid metric_name
224 values
= {"metric_name": "disk_write_ops"}
226 metric_name
, norm_name
= self
.metrics
.get_metric_name(values
)
228 self
.assertEqual(metric_name
, "disk_write_ops")
229 self
.assertEqual(norm_name
, "disk.disk_ops")
231 # test with an invalid metric name
232 values
= {"metric_name": "my_invalid_metric"}
234 metric_name
, norm_name
= self
.metrics
.get_metric_name(values
)
236 self
.assertEqual(metric_name
, "my_invalid_metric")
237 self
.assertEqual(norm_name
, None)
239 @mock.patch
.object(Common
, "_perform_request")
240 def test_valid_read_data_req(self
, perf_req
):
241 """Test the read metric data function, for a valid call."""
242 values
= {"metric_uuid": "metric_id",
243 "collection_unit": "DAY",
244 "collection_period": 1}
246 self
.metrics
.read_metric_data(endpoint
, auth_token
, values
)
248 perf_req
.assert_called_once
250 @mock.patch
.object(Common
, "_perform_request")
251 def test_invalid_read_data_req(self
, perf_req
):
252 """Test the read metric data function, for an invalid call."""
253 # Teo empty lists wil be returned because the values are invalid
256 times
, data
= self
.metrics
.read_metric_data(
257 endpoint
, auth_token
, values
)
259 self
.assertEqual(times
, [])
260 self
.assertEqual(data
, [])
262 def test_complete_response_list(self
):
263 """Test the response list function for formating metric lists."""
264 # Mock a list for testing purposes, with valid OSM metric
265 resp_list
= self
.metrics
.response_list(metric_list
)
267 # Check for the expected values in the resulting list
268 for l
in result_list
:
269 self
.assertIn(l
, resp_list
[0])
271 def test_name_response_list(self
):
272 """Test the response list with metric name configured."""
273 # Mock the metric name to test a metric name list
274 # Test with a name that is not in the list
275 invalid_name
= "my_metric"
276 resp_list
= self
.metrics
.response_list(
277 metric_list
, metric_name
=invalid_name
)
279 self
.assertEqual(resp_list
, [])
281 # Test with a name on the list
282 valid_name
= "disk_write_ops"
283 resp_list
= self
.metrics
.response_list(
284 metric_list
, metric_name
=valid_name
)
286 # Check for the expected values in the resulting list
287 for l
in result_list
:
288 self
.assertIn(l
, resp_list
[0])
290 def test_resource_response_list(self
):
291 """Test the response list with resource_id configured."""
292 # Mock a resource_id to test a resource list
293 # Test with resource not on the list
294 invalid_id
= "mock_resource"
295 resp_list
= self
.metrics
.response_list(metric_list
, resource
=invalid_id
)
297 self
.assertEqual(resp_list
, [])
299 # Test with a resource on the list
301 resp_list
= self
.metrics
.response_list(metric_list
, resource
=valid_id
)
303 # Check for the expected values in the resulting list
304 for l
in result_list
:
305 self
.assertIn(l
, resp_list
[0])
307 def test_combined_response_list(self
):
308 """Test the response list function with resource_id and metric_name."""
309 # Test for a combined resource and name list
310 # resource and name are on the lisat
311 valid_name
= "disk_write_ops"
313 resp_list
= self
.metrics
.response_list(
314 metric_list
, metric_name
=valid_name
, resource
=valid_id
)
316 # Check for the expected values in the resulting list
317 for l
in result_list
:
318 self
.assertIn(l
, resp_list
[0])
320 # resource not on list
321 invalid_id
= "mock_resource"
322 resp_list
= self
.metrics
.response_list(
323 metric_list
, metric_name
=valid_name
, resource
=invalid_id
)
325 self
.assertEqual(resp_list
, [])
327 # metric name not on list
328 invalid_name
= "mock_metric"
329 resp_list
= self
.metrics
.response_list(
330 metric_list
, metric_name
=invalid_name
, resource
=valid_id
)
332 self
.assertEqual(resp_list
, [])