| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 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 plugins.OpenStack.Gnocchi import metrics as metric_req |
| 33 | |
| 34 | from plugins.OpenStack.common import Common |
| 35 | |
| 36 | __author__ = "Helena McGough" |
| 37 | |
| 38 | log = logging.getLogger(__name__) |
| 39 | |
| 40 | # Mock auth_token and endpoint |
| 41 | endpoint = mock.ANY |
| 42 | auth_token = mock.ANY |
| 43 | |
| 44 | # Mock a valid metric list for some tests, and a resultant list |
| 45 | metric_list = [{"name": "disk_write_ops", |
| 46 | "id": "metric_id", |
| 47 | "unit": "units", |
| 48 | "resource_id": "r_id"}] |
| 49 | result_list = ["metric_id", "r_id", "units", "disk_write_ops"] |
| 50 | |
| 51 | |
| 52 | class Response(object): |
| 53 | """Mock a response object for requests.""" |
| 54 | |
| 55 | def __init__(self): |
| 56 | """Initialise test and status code values.""" |
| 57 | self.text = json.dumps("mock_response_text") |
| 58 | self.status_code = "STATUS_CODE" |
| 59 | |
| 60 | |
| 61 | class TestMetricCalls(unittest.TestCase): |
| 62 | """Integration test for metric request keys.""" |
| 63 | |
| 64 | def setUp(self): |
| 65 | """Setup the tests for metric request keys.""" |
| 66 | super(TestMetricCalls, self).setUp() |
| 67 | self.metrics = metric_req.Metrics() |
| 68 | self.metrics._common = Common() |
| 69 | |
| 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"} |
| 78 | |
| 79 | m_id, r_id, status = self.metrics.configure_metric( |
| 80 | endpoint, auth_token, values) |
| 81 | |
| 82 | perf_req.assert_not_called |
| 83 | self.assertEqual(m_id, None) |
| 84 | self.assertEqual(r_id, None) |
| 85 | self.assertEqual(status, False) |
| 86 | |
| 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 |
| 90 | |
| 91 | m_id, r_id, status = self.metrics.configure_metric( |
| 92 | endpoint, auth_token, values) |
| 93 | |
| 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() |
| 99 | |
| 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" |
| 103 | |
| 104 | m_id, r_id, status = self.metrics.configure_metric( |
| 105 | endpoint, auth_token, values) |
| 106 | |
| 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) |
| 111 | |
| 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", |
| 127 | "unit": "units"}}} |
| 128 | |
| 129 | self.metrics.configure_metric(endpoint, auth_token, values) |
| 130 | |
| 131 | perf_req.assert_called_with( |
| 132 | "<ANY>/v1/resource/generic", auth_token, req_type="post", |
| 133 | payload=json.dumps(payload)) |
| 134 | |
| 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") |
| 139 | |
| 140 | perf_req.assert_called_with( |
| 141 | "<ANY>/v1/metric/metric_id", auth_token, req_type="delete") |
| 142 | |
| 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" |
| 147 | |
| 148 | status = self.metrics.delete_metric(endpoint, auth_token, "metric_id") |
| 149 | |
| 150 | self.assertEqual(status, False) |
| 151 | |
| 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 |
| 157 | values = {} |
| 158 | resp = Response() |
| 159 | perf_req.return_value = resp |
| 160 | self.metrics.list_metrics(endpoint, auth_token, values) |
| 161 | |
| 162 | perf_req.assert_called_with( |
| 163 | "<ANY>/v1/metric/", auth_token, req_type="get") |
| 164 | resp_list.assert_called_with("mock_response_text") |
| 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 | resp = Response() |
| 173 | perf_req.return_value = resp |
| 174 | self.metrics.list_metrics(endpoint, auth_token, values) |
| 175 | |
| 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") |
| 180 | |
| 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"} |
| 187 | resp = Response() |
| 188 | perf_req.return_value = resp |
| 189 | self.metrics.list_metrics(endpoint, auth_token, values) |
| 190 | |
| 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") |
| 195 | |
| 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"} |
| 203 | resp = Response() |
| 204 | perf_req.return_value = resp |
| 205 | self.metrics.list_metrics(endpoint, auth_token, values) |
| 206 | |
| 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") |
| 212 | |
| 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") |
| 217 | |
| 218 | perf_req.assert_called_with( |
| 219 | "<ANY>/v1/resource/generic/r_id", auth_token, req_type="get") |
| 220 | |
| 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"} |
| 225 | |
| 226 | metric_name, norm_name = self.metrics.get_metric_name(values) |
| 227 | |
| 228 | self.assertEqual(metric_name, "disk_write_ops") |
| 229 | self.assertEqual(norm_name, "disk.disk_ops") |
| 230 | |
| 231 | # test with an invalid metric name |
| 232 | values = {"metric_name": "my_invalid_metric"} |
| 233 | |
| 234 | metric_name, norm_name = self.metrics.get_metric_name(values) |
| 235 | |
| 236 | self.assertEqual(metric_name, "my_invalid_metric") |
| 237 | self.assertEqual(norm_name, None) |
| 238 | |
| 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} |
| 245 | |
| 246 | self.metrics.read_metric_data(endpoint, auth_token, values) |
| 247 | |
| 248 | perf_req.assert_called_once |
| 249 | |
| 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 |
| 254 | values = {} |
| 255 | |
| 256 | times, data = self.metrics.read_metric_data( |
| 257 | endpoint, auth_token, values) |
| 258 | |
| 259 | self.assertEqual(times, []) |
| 260 | self.assertEqual(data, []) |
| 261 | |
| 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) |
| 266 | |
| 267 | # Check for the expected values in the resulting list |
| 268 | for l in result_list: |
| 269 | self.assertIn(l, resp_list[0]) |
| 270 | |
| 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) |
| 278 | |
| 279 | self.assertEqual(resp_list, []) |
| 280 | |
| 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) |
| 285 | |
| 286 | # Check for the expected values in the resulting list |
| 287 | for l in result_list: |
| 288 | self.assertIn(l, resp_list[0]) |
| 289 | |
| 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) |
| 296 | |
| 297 | self.assertEqual(resp_list, []) |
| 298 | |
| 299 | # Test with a resource on the list |
| 300 | valid_id = "r_id" |
| 301 | resp_list = self.metrics.response_list(metric_list, resource=valid_id) |
| 302 | |
| 303 | # Check for the expected values in the resulting list |
| 304 | for l in result_list: |
| 305 | self.assertIn(l, resp_list[0]) |
| 306 | |
| 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" |
| 312 | valid_id = "r_id" |
| 313 | resp_list = self.metrics.response_list( |
| 314 | metric_list, metric_name=valid_name, resource=valid_id) |
| 315 | |
| 316 | # Check for the expected values in the resulting list |
| 317 | for l in result_list: |
| 318 | self.assertIn(l, resp_list[0]) |
| 319 | |
| 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) |
| 324 | |
| 325 | self.assertEqual(resp_list, []) |
| 326 | |
| 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) |
| 331 | |
| 332 | self.assertEqual(resp_list, []) |