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
.core
.message_bus
.producer
import KafkaProducer
32 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
as metric_req
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 @mock.patch
.object(KafkaProducer
, 'publish', mock
.Mock())
58 class TestMetricReq(unittest
.TestCase
):
59 """Integration test for metric request keys."""
62 """Setup the tests for metric request keys."""
63 super(TestMetricReq
, self
).setUp()
64 self
.metrics
= metric_req
.Metrics()
66 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
67 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
68 @mock.patch
.object(metric_req
.Metrics
, "delete_metric")
69 @mock.patch
.object(metric_req
.Metrics
, "get_metric_id")
70 @mock.patch
.object(AuthManager
, "get_credentials")
71 def test_delete_metric_key(self
, get_creds
, get_metric_id
, del_metric
):
72 """Test the functionality for a delete metric request."""
73 # Mock a message value and key
75 message
.key
= "delete_metric_request"
76 message
.value
= json
.dumps({"metric_name": "disk_write_ops", "resource_uuid": "my_r_id", "correlation_id": 1})
78 get_creds
.return_value
= type('obj', (object,), {
79 'config': '{"insecure":true}'
81 del_metric
.return_value
= True
83 # Call the metric functionality and check delete request
84 get_metric_id
.return_value
= "my_metric_id"
85 self
.metrics
.metric_calls(message
, 'test_id')
86 del_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, "my_metric_id", False)
88 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
89 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
90 @mock.patch
.object(metric_req
.Metrics
, "list_metrics")
91 @mock.patch
.object(AuthManager
, "get_credentials")
92 def test_list_metric_key(self
, get_creds
, list_metrics
):
93 """Test the functionality for a list metric request."""
94 # Mock a message with list metric key and value
96 message
.key
= "list_metric_request"
97 message
.value
= json
.dumps({"metrics_list_request": {"correlation_id": 1}})
99 get_creds
.return_value
= type('obj', (object,), {
100 'config': '{"insecure":true}'
103 list_metrics
.return_value
= []
105 # Call the metric functionality and check list functionality
106 self
.metrics
.metric_calls(message
, 'test_id')
107 list_metrics
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 1}, False)
109 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
110 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
111 @mock.patch
.object(AuthManager
, "get_credentials")
112 @mock.patch
.object(Common
, "perform_request")
113 def test_update_metric_key(self
, perf_req
, get_creds
):
114 """Test the functionality for an update metric request."""
115 # Mock a message with update metric key and value
117 message
.key
= "update_metric_request"
118 message
.value
= json
.dumps({"metric_update_request":
119 {"correlation_id": 1,
120 "metric_name": "my_metric",
121 "resource_uuid": "my_r_id"}})
123 get_creds
.return_value
= type('obj', (object,), {
124 'config': '{"insecure":true}'
127 mock_response
= Response()
128 mock_response
.text
= json
.dumps({'metrics': {'my_metric': 'id'}})
129 perf_req
.return_value
= mock_response
131 # Call metric functionality and confirm no function is called
132 # Gnocchi does not support updating a metric configuration
133 self
.metrics
.metric_calls(message
, 'test_id')
135 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
136 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
137 @mock.patch
.object(metric_req
.Metrics
, "configure_metric")
138 @mock.patch
.object(AuthManager
, "get_credentials")
139 def test_config_metric_key(self
, get_credentials
, config_metric
):
140 """Test the functionality for a create metric request."""
141 # Mock a message with create metric key and value
143 message
.key
= "create_metric_request"
144 message
.value
= json
.dumps({"metric_create_request": {"correlation_id": 123}})
145 get_credentials
.return_value
= type('obj', (object,), {'config': '{"insecure":true}'})
146 # Call metric functionality and check config metric
147 config_metric
.return_value
= "metric_id", "resource_id"
148 self
.metrics
.metric_calls(message
, 'test_id')
149 config_metric
.assert_called_with(mock
.ANY
, mock
.ANY
, {"correlation_id": 123}, False)
151 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
152 @mock.patch
.object(Common
, 'get_endpoint', mock
.Mock())
153 @mock.patch
.object(metric_req
.Metrics
, "read_metric_data")
154 @mock.patch
.object(AuthManager
, "get_credentials")
155 @mock.patch
.object(Common
, "perform_request")
156 def test_read_data_key(self
, perf_req
, get_creds
, read_data
):
157 """Test the functionality for a read metric data request."""
158 # Mock a message with a read data key and value
160 message
.key
= "read_metric_data_request"
161 message
.value
= json
.dumps({"correlation_id": 123, "metric_name": "cpu_utilization", "resource_uuid": "uuid"})
163 get_creds
.return_value
= type('obj', (object,), {
164 'config': '{"insecure":true}'
167 mock_response
= Response()
168 mock_response
.text
= json
.dumps({'metrics': {'cpu_util': 'id'}})
169 perf_req
.return_value
= mock_response
171 # Call metric functionality and check read data metrics
172 read_data
.return_value
= "time_stamps", "data_values"
173 self
.metrics
.metric_calls(message
, 'test_id')
174 read_data
.assert_called_with(
175 mock
.ANY
, mock
.ANY
, json
.loads(message
.value
), False)