1 # Copyright 2017 Intel 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 # __author__ = "Helena McGough"
23 """Test an end to end Openstack metric requests."""
30 from kafka
.errors
import KafkaError
32 from osm_mon
.core
.auth
import AuthManager
33 from osm_mon
.core
.database
import VimCredentials
34 from osm_mon
.core
.message_bus
.producer
import KafkaProducer
as Producer
36 from kafka
import KafkaConsumer
37 from kafka
import KafkaProducer
41 from osm_mon
.plugins
.OpenStack
import response
43 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
45 from osm_mon
.plugins
.OpenStack
.common
import Common
47 log
= logging
.getLogger(__name__
)
49 mock_creds
= VimCredentials()
50 mock_creds
.config
= '{}'
53 @mock.patch
.object(Producer
, "publish_alarm_request", mock
.Mock())
54 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
55 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
56 class MetricIntegrationTest(unittest
.TestCase
):
58 # Set up common and alarming class instances
59 self
.metric_req
= metrics
.Metrics()
60 self
.openstack_auth
= Common()
63 self
.producer
= KafkaProducer(bootstrap_servers
='localhost:9092',
64 key_serializer
=str.encode
,
65 value_serializer
=str.encode
67 self
.req_consumer
= KafkaConsumer(bootstrap_servers
='localhost:9092',
68 key_deserializer
=bytes
.decode
,
69 value_deserializer
=bytes
.decode
,
70 auto_offset_reset
='earliest',
71 consumer_timeout_ms
=60000)
72 self
.req_consumer
.subscribe(['metric_request'])
74 self
.skipTest('Kafka server not present.')
76 @mock.patch
.object(Common
, "perform_request")
77 @mock.patch
.object(AuthManager
, 'get_credentials')
78 @mock.patch
.object(metrics
.Metrics
, "configure_metric")
79 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
80 def test_create_metric_req(self
, resp
, config_metric
, get_creds
, perf_req
):
81 """Test Gnocchi create metric request message from producer."""
82 # Set-up message, producer and consumer for tests
83 payload
= {"metric_create_request": {"correlation_id": 123,
84 "metric_name": "cpu_utilization",
85 "resource_uuid": "resource_id"}}
87 get_creds
.return_value
= mock_creds
88 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps({"metrics": {"cpu_util": "1"}})})
89 resp
.return_value
= ''
91 self
.producer
.send('metric_request', key
="create_metric_request",
92 value
=json
.dumps(payload
))
94 for message
in self
.req_consumer
:
95 if message
.key
== "create_metric_request":
96 # A valid metric is created
97 config_metric
.return_value
= "metric_id", "resource_id", True
98 self
.metric_req
.metric_calls(message
, 'test_id')
100 # A response message is generated and sent by MON's producer
101 resp
.assert_called_with(
102 'create_metric_response', status
=True, cor_id
=123,
103 metric_id
="metric_id", r_id
="resource_id")
106 self
.fail("No message received in consumer")
108 @mock.patch
.object(Common
, "perform_request")
109 @mock.patch
.object(AuthManager
, 'get_credentials')
110 @mock.patch
.object(metrics
.Metrics
, "delete_metric")
111 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
112 def test_delete_metric_req(self
, resp
, del_metric
, get_creds
, perf_req
):
113 """Test Gnocchi delete metric request message from producer."""
114 # Set-up message, producer and consumer for tests
115 payload
= {"vim_type": "OpenSTACK",
117 "correlation_id": 123,
118 "metric_name": "cpu_utilization",
119 "resource_uuid": "resource_id"}
121 get_creds
.return_value
= mock_creds
122 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps({"metrics": {"cpu_util": "1"}})})
123 resp
.return_value
= ''
125 self
.producer
.send('metric_request', key
="delete_metric_request",
126 value
=json
.dumps(payload
))
128 for message
in self
.req_consumer
:
129 if message
.key
== "delete_metric_request":
130 # Metric has been deleted
131 del_metric
.return_value
= True
132 self
.metric_req
.metric_calls(message
, 'test_id')
134 # A response message is generated and sent by MON's producer
135 resp
.assert_called_with(
136 'delete_metric_response', m_id
='1',
137 m_name
="cpu_utilization", status
=True, r_id
="resource_id",
141 self
.fail("No message received in consumer")
143 @mock.patch
.object(Common
, "perform_request")
144 @mock.patch
.object(AuthManager
, 'get_credentials')
145 @mock.patch
.object(metrics
.Metrics
, "read_metric_data")
146 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
147 def test_read_metric_data_req(self
, resp
, read_data
, get_creds
, perf_req
):
148 """Test Gnocchi read metric data request message from producer."""
149 # Set-up message, producer and consumer for tests
150 payload
= {"vim_type": "OpenSTACK",
151 "vim_uuid": "test_id",
152 "correlation_id": 123,
153 "metric_name": "cpu_utilization",
154 "resource_uuid": "resource_id"}
156 get_creds
.return_value
= mock_creds
157 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps({"metrics": {"cpu_util": "1"}})})
158 resp
.return_value
= ''
160 self
.producer
.send('metric_request', key
="read_metric_data_request",
161 value
=json
.dumps(payload
))
163 for message
in self
.req_consumer
:
164 # Check the vim desired by the message
165 if message
.key
== "read_metric_data_request":
166 # Mock empty lists generated by the request message
167 read_data
.return_value
= [], []
168 self
.metric_req
.metric_calls(message
, 'test_id')
170 # A response message is generated and sent by MON's producer
171 resp
.assert_called_with(
172 'read_metric_data_response', m_id
='1',
173 m_name
="cpu_utilization", r_id
="resource_id", cor_id
=123, times
=[],
177 self
.fail("No message received in consumer")
179 @mock.patch
.object(Common
, "perform_request")
180 @mock.patch
.object(AuthManager
, 'get_credentials')
181 @mock.patch
.object(metrics
.Metrics
, "list_metrics")
182 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
183 def test_list_metrics_req(self
, resp
, list_metrics
, get_creds
, perf_req
):
184 """Test Gnocchi list metrics request message from producer."""
185 # Set-up message, producer and consumer for tests
186 payload
= {"vim_type": "OpenSTACK",
188 "metrics_list_request":
189 {"correlation_id": 123, }}
191 get_creds
.return_value
= mock_creds
192 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps({"metrics": {"cpu_util": "1"}})})
193 resp
.return_value
= ''
195 self
.producer
.send('metric_request', key
="list_metric_request",
196 value
=json
.dumps(payload
))
198 for message
in self
.req_consumer
:
199 # Check the vim desired by the message
200 if message
.key
== "list_metric_request":
201 # Mock an empty list generated by the request
202 list_metrics
.return_value
= []
203 self
.metric_req
.metric_calls(message
, 'test_id')
205 # A response message is generated and sent by MON's producer
206 resp
.assert_called_with(
207 'list_metric_response', m_list
=[], cor_id
=123)
210 self
.fail("No message received in consumer")
212 @mock.patch
.object(Common
, "perform_request")
213 @mock.patch
.object(AuthManager
, 'get_credentials')
214 @mock.patch
.object(metrics
.Metrics
, "get_metric_id")
215 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
216 def test_update_metrics_req(self
, resp
, get_id
, get_creds
, perf_req
):
217 """Test Gnocchi update metric request message from KafkaProducer."""
218 # Set-up message, producer and consumer for tests
219 payload
= {"metric_create_request": {"metric_name": "my_metric",
220 "correlation_id": 123,
221 "resource_uuid": "resource_id", }}
223 get_creds
.return_value
= mock_creds
224 perf_req
.return_value
= type('obj', (object,), {'text': json
.dumps({"metrics": {"cpu_util": "1"}})})
225 resp
.return_value
= ''
227 self
.producer
.send('metric_request', key
="update_metric_request",
228 value
=json
.dumps(payload
))
230 for message
in self
.req_consumer
:
231 # Check the vim desired by the message
232 if message
.key
== "update_metric_request":
233 # Gnocchi doesn't support metric updates
234 get_id
.return_value
= "metric_id"
235 self
.metric_req
.metric_calls(message
, 'test_id')
237 # Response message is generated and sent via MON's producer
238 # No metric update has taken place
239 resp
.assert_called_with(
240 'update_metric_response', status
=False, cor_id
=123,
241 r_id
="resource_id", m_id
="metric_id")
244 self
.fail("No message received in consumer")