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 osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
34 from osm_mon
.plugins
.OpenStack
.common
import Common
36 log
= logging
.getLogger(__name__
)
38 # Mock auth_token and endpoint
42 # Mock a valid metric list for some tests, and a resultant list
43 metric_list
= [{"name": "disk_write_ops",
46 "resource_id": "r_id"}]
47 result_list
= ["metric_id", "r_id", "units", "disk_write_ops"]
50 class Response(object):
51 """Mock a response object for requests."""
54 """Initialise test and status code values."""
55 self
.text
= json
.dumps("mock_response_text")
56 self
.status_code
= "STATUS_CODE"
59 class TestMetricCalls(unittest
.TestCase
):
60 """Integration test for metric request keys."""
63 """Setup the tests for metric request keys."""
64 super(TestMetricCalls
, self
).setUp()
65 self
.metrics
= metric_req
.Metrics()
66 self
.metrics
._common
= Common()
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"}
77 m_id
, r_id
, status
= self
.metrics
.configure_metric(
78 endpoint
, auth_token
, values
)
80 perf_req
.assert_not_called
81 self
.assertEqual(m_id
, None)
82 self
.assertEqual(r_id
, None)
83 self
.assertEqual(status
, False)
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
89 m_id
, r_id
, status
= self
.metrics
.configure_metric(
90 endpoint
, auth_token
, values
)
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()
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"
102 m_id
, r_id
, status
= self
.metrics
.configure_metric(
103 endpoint
, auth_token
, values
)
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)
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",
127 self
.metrics
.configure_metric(endpoint
, auth_token
, values
)
129 perf_req
.assert_called_with(
130 "<ANY>/v1/resource/generic", auth_token
, req_type
="post",
131 payload
=json
.dumps(payload
))
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")
138 perf_req
.assert_called_with(
139 "<ANY>/v1/metric/metric_id", auth_token
, req_type
="delete")
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"
146 status
= self
.metrics
.delete_metric(endpoint
, auth_token
, "metric_id")
148 self
.assertEqual(status
, False)
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
157 perf_req
.return_value
= resp
158 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
160 perf_req
.assert_called_with(
161 "<ANY>/v1/metric/", auth_token
, req_type
="get")
162 resp_list
.assert_called_with("mock_response_text")
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"}
171 perf_req
.return_value
= resp
172 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
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")
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"}
186 perf_req
.return_value
= resp
187 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
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")
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"}
202 perf_req
.return_value
= resp
203 self
.metrics
.list_metrics(endpoint
, auth_token
, values
)
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")
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")
216 perf_req
.assert_called_with(
217 "<ANY>/v1/resource/generic/r_id", auth_token
, req_type
="get")
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"}
224 metric_name
, norm_name
= self
.metrics
.get_metric_name(values
)
226 self
.assertEqual(metric_name
, "disk_write_ops")
227 self
.assertEqual(norm_name
, "disk.disk_ops")
229 # test with an invalid metric name
230 values
= {"metric_name": "my_invalid_metric"}
232 metric_name
, norm_name
= self
.metrics
.get_metric_name(values
)
234 self
.assertEqual(metric_name
, "my_invalid_metric")
235 self
.assertEqual(norm_name
, None)
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}
244 self
.metrics
.read_metric_data(endpoint
, auth_token
, values
)
246 perf_req
.assert_called_once
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
254 times
, data
= self
.metrics
.read_metric_data(
255 endpoint
, auth_token
, values
)
257 self
.assertEqual(times
, [])
258 self
.assertEqual(data
, [])
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
)
265 # Check for the expected values in the resulting list
266 for l
in result_list
:
267 self
.assertIn(l
, resp_list
[0])
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
)
277 self
.assertEqual(resp_list
, [])
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
)
284 # Check for the expected values in the resulting list
285 for l
in result_list
:
286 self
.assertIn(l
, resp_list
[0])
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
)
295 self
.assertEqual(resp_list
, [])
297 # Test with a resource on the list
299 resp_list
= self
.metrics
.response_list(metric_list
, resource
=valid_id
)
301 # Check for the expected values in the resulting list
302 for l
in result_list
:
303 self
.assertIn(l
, resp_list
[0])
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"
311 resp_list
= self
.metrics
.response_list(
312 metric_list
, metric_name
=valid_name
, resource
=valid_id
)
314 # Check for the expected values in the resulting list
315 for l
in result_list
:
316 self
.assertIn(l
, resp_list
[0])
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
)
323 self
.assertEqual(resp_list
, [])
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
)
330 self
.assertEqual(resp_list
, [])