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
.message_bus
.producer
import KafkaProducer
as prod
34 from kafka
import KafkaConsumer
35 from kafka
import KafkaProducer
39 from osm_mon
.plugins
.OpenStack
import response
41 from osm_mon
.plugins
.OpenStack
.Gnocchi
import metrics
43 from osm_mon
.plugins
.OpenStack
.common
import Common
45 log
= logging
.getLogger(__name__
)
48 class MetricIntegrationTest(unittest
.TestCase
):
50 # Set up common and alarming class instances
51 self
.metric_req
= metrics
.Metrics()
52 self
.openstack_auth
= Common()
55 self
.producer
= KafkaProducer(bootstrap_servers
='localhost:9092',
56 key_serializer
=str.encode
,
57 value_serializer
=str.encode
59 self
.req_consumer
= KafkaConsumer(bootstrap_servers
='localhost:9092',
60 key_deserializer
=bytes
.decode
,
61 value_deserializer
=bytes
.decode
,
62 auto_offset_reset
='earliest',
63 consumer_timeout_ms
=60000)
64 self
.req_consumer
.subscribe(['metric_request'])
66 self
.skipTest('Kafka server not present.')
68 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
69 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
70 @mock.patch
.object(metrics
.Metrics
, "configure_metric")
71 @mock.patch
.object(prod
, "create_metrics_resp")
72 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
73 def test_create_metric_req(self
, resp
, create_resp
, config_metric
):
74 """Test Gnocchi create metric request message from producer."""
75 # Set-up message, producer and consumer for tests
76 payload
= {"vim_type": "OpenSTACK",
78 "correlation_id": 123,
80 {"metric_name": "cpu_utilization",
81 "resource_uuid": "resource_id"}}
83 self
.producer
.send('metric_request', key
="create_metric_request",
84 value
=json
.dumps(payload
))
86 for message
in self
.req_consumer
:
87 # Check the vim desired by the message
88 vim_type
= json
.loads(message
.value
)["vim_type"].lower()
89 if vim_type
== "openstack":
90 # A valid metric is created
91 config_metric
.return_value
= "metric_id", "resource_id", True
92 self
.metric_req
.metric_calls(message
)
94 # A response message is generated and sent by MON's producer
95 resp
.assert_called_with(
96 'create_metric_response', status
=True, cor_id
=123,
97 metric_id
="metric_id", r_id
="resource_id")
98 create_resp
.assert_called_with(
99 'create_metric_response', resp
.return_value
)
102 self
.fail("No message received in consumer")
104 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
105 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
106 @mock.patch
.object(metrics
.Metrics
, "delete_metric")
107 @mock.patch
.object(prod
, "delete_metric_response")
108 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
109 def test_delete_metric_req(self
, resp
, del_resp
, del_metric
):
110 """Test Gnocchi delete metric request message from producer."""
111 # Set-up message, producer and consumer for tests
112 payload
= {"vim_type": "OpenSTACK",
114 "correlation_id": 123,
115 "metric_name": "cpu_utilization",
116 "resource_uuid": "resource_id"}
118 self
.producer
.send('metric_request', key
="delete_metric_request",
119 value
=json
.dumps(payload
))
121 for message
in self
.req_consumer
:
122 if message
.key
== "delete_metric_request":
123 # Metric has been deleted
124 del_metric
.return_value
= True
125 self
.metric_req
.metric_calls(message
)
127 # A response message is generated and sent by MON's producer
128 resp
.assert_called_with(
129 'delete_metric_response', m_id
=None,
130 m_name
="cpu_utilization", status
=True, r_id
="resource_id",
132 del_resp
.assert_called_with(
133 'delete_metric_response', resp
.return_value
)
136 self
.fail("No message received in consumer")
138 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
139 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
140 @mock.patch
.object(metrics
.Metrics
, "read_metric_data")
141 @mock.patch
.object(prod
, "read_metric_data_response")
142 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
143 def test_read_metric_data_req(self
, resp
, read_resp
, read_data
):
144 """Test Gnocchi read metric data request message from producer."""
145 # Set-up message, producer and consumer for tests
146 payload
= {"vim_type": "OpenSTACK",
147 "vim_uuid": "test_id",
148 "correlation_id": 123,
149 "metric_name": "cpu_utilization",
150 "resource_uuid": "resource_id"}
152 self
.producer
.send('metric_request', key
="read_metric_data_request",
153 value
=json
.dumps(payload
))
155 for message
in self
.req_consumer
:
156 # Check the vim desired by the message
157 if message
.key
== "read_metric_data_request":
158 # Mock empty lists generated by the request message
159 read_data
.return_value
= [], []
160 self
.metric_req
.metric_calls(message
)
162 # A response message is generated and sent by MON's producer
163 resp
.assert_called_with(
164 'read_metric_data_response', m_id
=None,
165 m_name
="cpu_utilization", r_id
="resource_id", cor_id
=123, times
=[],
167 read_resp
.assert_called_with(
168 'read_metric_data_response', resp
.return_value
)
171 self
.fail("No message received in consumer")
173 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
174 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
175 @mock.patch
.object(metrics
.Metrics
, "list_metrics")
176 @mock.patch
.object(prod
, "list_metric_response")
177 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
178 def test_list_metrics_req(self
, resp
, list_resp
, list_metrics
):
179 """Test Gnocchi list metrics request message from producer."""
180 # Set-up message, producer and consumer for tests
181 payload
= {"vim_type": "OpenSTACK",
183 "metrics_list_request":
184 {"correlation_id": 123, }}
186 self
.producer
.send('metric_request', key
="list_metric_request",
187 value
=json
.dumps(payload
))
189 for message
in self
.req_consumer
:
190 # Check the vim desired by the message
191 if message
.key
== "list_metric_request":
192 # Mock an empty list generated by the request
193 list_metrics
.return_value
= []
194 self
.metric_req
.metric_calls(message
)
196 # A response message is generated and sent by MON's producer
197 resp
.assert_called_with(
198 'list_metric_response', m_list
=[], cor_id
=123)
199 list_resp
.assert_called_with(
200 'list_metric_response', resp
.return_value
)
203 self
.fail("No message received in consumer")
205 @mock.patch
.object(Common
, "get_auth_token", mock
.Mock())
206 @mock.patch
.object(Common
, "get_endpoint", mock
.Mock())
207 @mock.patch
.object(metrics
.Metrics
, "get_metric_id")
208 @mock.patch
.object(prod
, "update_metric_response")
209 @mock.patch
.object(response
.OpenStack_Response
, "generate_response")
210 def test_update_metrics_req(self
, resp
, update_resp
, get_id
):
211 """Test Gnocchi update metric request message from KafkaProducer."""
212 # Set-up message, producer and consumer for tests
213 payload
= {"vim_type": "OpenSTACK",
214 "vim_uuid": "test_id",
215 "correlation_id": 123,
217 {"metric_name": "my_metric",
218 "resource_uuid": "resource_id", }}
220 self
.producer
.send('metric_request', key
="update_metric_request",
221 value
=json
.dumps(payload
))
223 for message
in self
.req_consumer
:
224 # Check the vim desired by the message
225 if message
.key
== "update_metric_request":
226 # Gnocchi doesn't support metric updates
227 get_id
.return_value
= "metric_id"
228 self
.metric_req
.metric_calls(message
)
230 # Response message is generated and sent via MON's producer
231 # No metric update has taken place
232 resp
.assert_called_with(
233 'update_metric_response', status
=False, cor_id
=123,
234 r_id
="resource_id", m_id
="metric_id")
235 update_resp
.assert_called_with(
236 'update_metric_response', resp
.return_value
)
239 self
.fail("No message received in consumer")