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."""
30 from osm_mon
.core
.auth
import AuthManager
31 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metric_handler
as metric_req
32 from osm_mon
.plugins
.OpenStack
.Gnocchi
.metric_handler
import OpenstackMetricHandler
33 from osm_mon
.plugins
.OpenStack
.common
import Common
35 log
= logging
.getLogger(__name__
)
38 class Response(object):
39 """Mock a response object for requests."""
42 """Initialise test and status code values."""
43 self
.text
= json
.dumps([{"id": "test_id"}])
44 self
.status_code
= "STATUS_CODE"
47 class Message(object):
48 """A class to mock a message object value for metric requests."""
51 """Initialize a mocked message instance."""
52 self
.topic
= "metric_request"
54 self
.value
= json
.dumps({"mock_message": "message_details"})
57 class TestMetricReq(unittest
.TestCase
):
58 """Integration test for metric request keys."""
61 """Setup the tests for metric request keys."""
62 super(TestMetricReq
, self
).setUp()
63 self
.metrics
= metric_req
.OpenstackMetricHandler()
65 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
66 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
67 @mock.patch
.object(metric_req
.OpenstackMetricHandler
, "delete_metric")
68 @mock.patch
.object(metric_req
.OpenstackMetricHandler
, "get_metric_id")
69 @mock.patch
.object(AuthManager
, "get_credentials")
70 def test_delete_metric_key(self
, get_creds
, get_metric_id
, del_metric
):
71 """Test the functionality for a delete metric request."""
72 value
= {"metric_name": "disk_write_ops", "resource_uuid": "my_r_id", "correlation_id": 1}
74 get_creds
.return_value
= type('obj', (object,), {
75 'config': '{"insecure":true}'
77 del_metric
.return_value
= True
79 # Call the metric functionality and check delete request
80 get_metric_id
.return_value
= "my_metric_id"
81 self
.metrics
.handle_request('delete_metric_request', value
, 'test_id')
82 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id", False)
84 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
85 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
86 @mock.patch
.object(metric_req
.OpenstackMetricHandler
, "list_metrics")
87 @mock.patch
.object(AuthManager
, "get_credentials")
88 def test_list_metric_key(self
, get_creds
, list_metrics
):
89 """Test the functionality for a list metric request."""
90 value
= {"metrics_list_request": {"correlation_id": 1}}
92 get_creds
.return_value
= type('obj', (object,), {
93 'config': '{"insecure":true}'
96 list_metrics
.return_value
= []
98 # Call the metric functionality and check list functionality
99 self
.metrics
.handle_request('list_metric_request', value
, 'test_id')
100 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 1}, False)
102 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
103 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
104 @mock.patch
.object(AuthManager
, "get_credentials")
105 @mock.patch
.object(Common
, "perform_request")
106 def test_update_metric_key(self
, perf_req
, get_creds
):
107 """Test the functionality for an update metric request."""
108 value
= {"metric_update_request":
109 {"correlation_id": 1,
110 "metric_name": "my_metric",
111 "resource_uuid": "my_r_id"}}
113 get_creds
.return_value
= type('obj', (object,), {
114 'config': '{"insecure":true}'
117 mock_response
= Response()
118 mock_response
.text
= json
.dumps({'metrics': {'my_metric': 'id'}})
119 perf_req
.return_value
= mock_response
121 # Call metric functionality and confirm no function is called
122 # Gnocchi does not support updating a metric configuration
123 self
.metrics
.handle_request('update_metric_request', value
, 'test_id')
125 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
126 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
127 @mock.patch
.object(OpenstackMetricHandler
, "configure_metric")
128 @mock.patch
.object(AuthManager
, "get_credentials")
129 def test_config_metric_key(self
, get_credentials
, config_metric
):
130 """Test the functionality for a create metric request."""
131 value
= {"metric_create_request": {"correlation_id": 123}}
132 get_credentials
.return_value
= type('obj', (object,), {'config': '{"insecure":true}'})
133 # Call metric functionality and check config metric
134 config_metric
.return_value
= "metric_id", "resource_id"
135 self
.metrics
.handle_request('create_metric_request', value
, 'test_id')
136 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 123}, False)
138 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
139 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
140 @mock.patch
.object(OpenstackMetricHandler
, "read_metric_data")
141 @mock.patch
.object(AuthManager
, "get_credentials")
142 @mock.patch
.object(Common
, "perform_request")
143 def test_read_data_key(self
, perf_req
, get_creds
, read_data
):
144 """Test the functionality for a read metric data request."""
145 value
= {"correlation_id": 123, "metric_name": "cpu_utilization", "resource_uuid": "uuid"}
147 get_creds
.return_value
= type('obj', (object,), {
148 'config': '{"insecure":true}'
151 mock_response
= Response()
152 mock_response
.text
= json
.dumps({'metrics': {'cpu_util': 'id'}})
153 perf_req
.return_value
= mock_response
155 # Call metric functionality and check read data metrics
156 read_data
.return_value
= "time_stamps", "data_values"
157 self
.metrics
.handle_request('read_metric_data_request', value
, 'test_id')
158 read_data
.assert_called_with(
159 mock
.ANY
, mock
.ANY
, value
, False)