Adds support for vdu_name, ns_id and vnf_member_index
[osm/MON.git] / osm_mon / test / OpenStack / integration / test_metric_integration.py
1 # Copyright 2017 Intel 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 # __author__ = "Helena McGough"
23 """Test an end to end Openstack metric requests."""
24
25 import json
26
27 import logging
28 import unittest
29
30 from kafka.errors import KafkaError
31
32 from osm_mon.core.message_bus.producer import KafkaProducer as prod
33
34 from kafka import KafkaConsumer
35 from kafka import KafkaProducer
36
37 import mock
38
39 from osm_mon.plugins.OpenStack import response
40
41 from osm_mon.plugins.OpenStack.Gnocchi import metrics
42
43 from osm_mon.plugins.OpenStack.common import Common
44
45 log = logging.getLogger(__name__)
46
47
48 class MetricIntegrationTest(unittest.TestCase):
49 def setUp(self):
50 # Set up common and alarming class instances
51 self.metric_req = metrics.Metrics()
52 self.openstack_auth = Common()
53
54 try:
55 self.producer = KafkaProducer(bootstrap_servers='localhost:9092',
56 key_serializer=str.encode,
57 value_serializer=str.encode
58 )
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'])
65 except KafkaError:
66 self.skipTest('Kafka server not present.')
67
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 = {"metric_create_request": {"correlation_id": 123,
77 "metric_name": "cpu_utilization",
78 "resource_uuid": "resource_id"}}
79
80 self.producer.send('metric_request', key="create_metric_request",
81 value=json.dumps(payload))
82
83 for message in self.req_consumer:
84 if message.key == "create_metric_request":
85 # A valid metric is created
86 config_metric.return_value = "metric_id", "resource_id", True
87 self.metric_req.metric_calls(message, 'test_id')
88
89 # A response message is generated and sent by MON's producer
90 resp.assert_called_with(
91 'create_metric_response', status=True, cor_id=123,
92 metric_id="metric_id", r_id="resource_id")
93 create_resp.assert_called_with(
94 'create_metric_response', resp.return_value)
95
96 return
97 self.fail("No message received in consumer")
98
99 @mock.patch.object(Common, "get_auth_token", mock.Mock())
100 @mock.patch.object(Common, "get_endpoint", mock.Mock())
101 @mock.patch.object(metrics.Metrics, "delete_metric")
102 @mock.patch.object(prod, "delete_metric_response")
103 @mock.patch.object(response.OpenStack_Response, "generate_response")
104 def test_delete_metric_req(self, resp, del_resp, del_metric):
105 """Test Gnocchi delete metric request message from producer."""
106 # Set-up message, producer and consumer for tests
107 payload = {"vim_type": "OpenSTACK",
108 "vim_uuid": "1",
109 "correlation_id": 123,
110 "metric_name": "cpu_utilization",
111 "resource_uuid": "resource_id"}
112
113 self.producer.send('metric_request', key="delete_metric_request",
114 value=json.dumps(payload))
115
116 for message in self.req_consumer:
117 if message.key == "delete_metric_request":
118 # Metric has been deleted
119 del_metric.return_value = True
120 self.metric_req.metric_calls(message, 'test_id')
121
122 # A response message is generated and sent by MON's producer
123 resp.assert_called_with(
124 'delete_metric_response', m_id=None,
125 m_name="cpu_utilization", status=True, r_id="resource_id",
126 cor_id=123)
127 del_resp.assert_called_with(
128 'delete_metric_response', resp.return_value)
129
130 return
131 self.fail("No message received in consumer")
132
133 @mock.patch.object(Common, "get_auth_token", mock.Mock())
134 @mock.patch.object(Common, "get_endpoint", mock.Mock())
135 @mock.patch.object(metrics.Metrics, "read_metric_data")
136 @mock.patch.object(prod, "read_metric_data_response")
137 @mock.patch.object(response.OpenStack_Response, "generate_response")
138 def test_read_metric_data_req(self, resp, read_resp, read_data):
139 """Test Gnocchi read metric data request message from producer."""
140 # Set-up message, producer and consumer for tests
141 payload = {"vim_type": "OpenSTACK",
142 "vim_uuid": "test_id",
143 "correlation_id": 123,
144 "metric_name": "cpu_utilization",
145 "resource_uuid": "resource_id"}
146
147 self.producer.send('metric_request', key="read_metric_data_request",
148 value=json.dumps(payload))
149
150 for message in self.req_consumer:
151 # Check the vim desired by the message
152 if message.key == "read_metric_data_request":
153 # Mock empty lists generated by the request message
154 read_data.return_value = [], []
155 self.metric_req.metric_calls(message, 'test_id')
156
157 # A response message is generated and sent by MON's producer
158 resp.assert_called_with(
159 'read_metric_data_response', m_id=None,
160 m_name="cpu_utilization", r_id="resource_id", cor_id=123, times=[],
161 metrics=[])
162 read_resp.assert_called_with(
163 'read_metric_data_response', resp.return_value)
164
165 return
166 self.fail("No message received in consumer")
167
168 @mock.patch.object(Common, "get_auth_token", mock.Mock())
169 @mock.patch.object(Common, "get_endpoint", mock.Mock())
170 @mock.patch.object(metrics.Metrics, "list_metrics")
171 @mock.patch.object(prod, "list_metric_response")
172 @mock.patch.object(response.OpenStack_Response, "generate_response")
173 def test_list_metrics_req(self, resp, list_resp, list_metrics):
174 """Test Gnocchi list metrics request message from producer."""
175 # Set-up message, producer and consumer for tests
176 payload = {"vim_type": "OpenSTACK",
177 "vim_uuid": "1",
178 "metrics_list_request":
179 {"correlation_id": 123, }}
180
181 self.producer.send('metric_request', key="list_metric_request",
182 value=json.dumps(payload))
183
184 for message in self.req_consumer:
185 # Check the vim desired by the message
186 if message.key == "list_metric_request":
187 # Mock an empty list generated by the request
188 list_metrics.return_value = []
189 self.metric_req.metric_calls(message, 'test_id')
190
191 # A response message is generated and sent by MON's producer
192 resp.assert_called_with(
193 'list_metric_response', m_list=[], cor_id=123)
194 list_resp.assert_called_with(
195 'list_metric_response', resp.return_value)
196
197 return
198 self.fail("No message received in consumer")
199
200 @mock.patch.object(Common, "get_auth_token", mock.Mock())
201 @mock.patch.object(Common, "get_endpoint", mock.Mock())
202 @mock.patch.object(metrics.Metrics, "get_metric_id")
203 @mock.patch.object(prod, "update_metric_response")
204 @mock.patch.object(response.OpenStack_Response, "generate_response")
205 def test_update_metrics_req(self, resp, update_resp, get_id):
206 """Test Gnocchi update metric request message from KafkaProducer."""
207 # Set-up message, producer and consumer for tests
208 payload = {"metric_create_request": {"metric_name": "my_metric",
209 "correlation_id": 123,
210 "resource_uuid": "resource_id", }}
211
212 self.producer.send('metric_request', key="update_metric_request",
213 value=json.dumps(payload))
214
215 for message in self.req_consumer:
216 # Check the vim desired by the message
217 if message.key == "update_metric_request":
218 # Gnocchi doesn't support metric updates
219 get_id.return_value = "metric_id"
220 self.metric_req.metric_calls(message, 'test_id')
221
222 # Response message is generated and sent via MON's producer
223 # No metric update has taken place
224 resp.assert_called_with(
225 'update_metric_response', status=False, cor_id=123,
226 r_id="resource_id", m_id="metric_id")
227 update_resp.assert_called_with(
228 'update_metric_response', resp.return_value)
229
230 return
231 self.fail("No message received in consumer")