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
.core
.auth
import AuthManager
33 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
35 from osm_mon
.plugins
.OpenStack
.common
import Common
37 log
= logging
.getLogger(__name__
)
39 # Mock auth_token and endpoint
43 # Mock a valid metric list for some tests, and a resultant list
44 metric_list
= [{"name": "disk.write.requests",
47 "resource_id": "r_id"}]
48 result_list
= ["metric_id", "r_id", "units", "disk_write_ops"]
51 class Response(object):
52 """Mock a response object for requests."""
55 """Initialise test and status code values."""
56 self
.text
= json
.dumps([{"id": "test_id"}])
57 self
.status_code
= "STATUS_CODE"
60 def perform_request_side_effect(*args
, **kwargs
):
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'}})
69 class TestMetricCalls(unittest
.TestCase
):
70 """Integration test for metric request keys."""
73 """Setup the tests for metric request keys."""
74 super(TestMetricCalls
, self
).setUp()
75 self
.metrics
= metric_req
.Metrics()
76 self
.metrics
._common
= Common()
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"}
86 m_id
, r_id
, status
= self
.metrics
.configure_metric(
87 endpoint
, auth_token
, values
, verify_ssl
=False)
89 perf_req
.assert_not_called()
90 self
.assertEqual(status
, False)
92 # Test with an invalid metric name, will not perform request
93 values
= {"resource_uuid": "r_id"}
95 m_id
, r_id
, status
= self
.metrics
.configure_metric(
96 endpoint
, auth_token
, values
, verify_ssl
=False)
98 perf_req
.assert_not_called()
99 self
.assertEqual(status
, False)
101 # If metric exists, it won't be recreated
102 get_metric
.return_value
= "metric_id"
104 m_id
, r_id
, status
= self
.metrics
.configure_metric(
105 endpoint
, auth_token
, values
, verify_ssl
=False)
107 perf_req
.assert_not_called()
108 self
.assertEqual(status
, False)
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",
128 perf_req
.return_value
= type('obj', (object,), {'text': '{"metrics":{"cpu_util":1}, "id":1}'})
130 self
.metrics
.configure_metric(endpoint
, auth_token
, values
, verify_ssl
=False)
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))
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)
141 perf_req
.assert_called_with(
142 "<ANY>/v1/metric/metric_id", auth_token
, req_type
="delete", verify_ssl
=False)
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"})
149 status
= self
.metrics
.delete_metric(endpoint
, auth_token
, "metric_id", verify_ssl
=False)
151 self
.assertEqual(status
, False)
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
159 perf_req
.side_effect
= perform_request_side_effect
160 self
.metrics
.list_metrics(endpoint
, auth_token
, values
, verify_ssl
=False)
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'}])
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)
175 perf_req
.assert_any_call(
176 "<ANY>/v1/metric/test_id", auth_token
, req_type
="get", verify_ssl
=False)
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)
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")
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
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)
203 perf_req
.assert_any_call(
204 "<ANY>/v1/metric/test_id", auth_token
, req_type
="get", verify_ssl
=False)
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)
212 perf_req
.assert_called_with(
213 "<ANY>/v1/resource/generic/r_id", auth_token
, req_type
="get", verify_ssl
=False)
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}
224 perf_req
.return_value
= type('obj', (object,), {'text': '{"metric_data":"[]"}'})
226 get_metric
.return_value
= "metric_id"
227 self
.metrics
.read_metric_data(endpoint
, auth_token
, values
, verify_ssl
=False)
229 perf_req
.assert_called_once()
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
237 times
, data
= self
.metrics
.read_metric_data(
238 endpoint
, auth_token
, values
, verify_ssl
=False)
240 self
.assertEqual(times
, [])
241 self
.assertEqual(data
, [])
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
)
248 # Check for the expected values in the resulting list
249 for l
in result_list
:
250 self
.assertIn(l
, resp_list
[0].values())
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
)
260 self
.assertEqual(resp_list
, [])
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
)
267 # Check for the expected values in the resulting list
268 for l
in result_list
:
269 self
.assertIn(l
, resp_list
[0].values())
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
)
278 self
.assertEqual(resp_list
, [])
280 # Test with a resource on the list
282 resp_list
= self
.metrics
.response_list(metric_list
, resource
=valid_id
)
284 # Check for the expected values in the resulting list
285 for l
in result_list
:
286 self
.assertIn(l
, resp_list
[0].values())
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"
294 resp_list
= self
.metrics
.response_list(
295 metric_list
, metric_name
=valid_name
, resource
=valid_id
)
297 # Check for the expected values in the resulting list
298 for l
in result_list
:
299 self
.assertIn(l
, resp_list
[0].values())
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
)
306 self
.assertEqual(resp_list
, [])
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
)
313 self
.assertEqual(resp_list
, [])