Added LICENSE file to root folder
[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.auth import AuthManager
33 from osm_mon.core.database import VimCredentials
34 from osm_mon.core.message_bus.producer import KafkaProducer as Producer
35
36 from kafka import KafkaConsumer
37 from kafka import KafkaProducer
38
39 import mock
40
41 from osm_mon.plugins.OpenStack import response
42
43 from osm_mon.plugins.OpenStack.Gnocchi import metrics
44
45 from osm_mon.plugins.OpenStack.common import Common
46
47 log = logging.getLogger(__name__)
48
49 mock_creds = VimCredentials()
50 mock_creds.config = '{}'
51
52
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):
57 def setUp(self):
58 # Set up common and alarming class instances
59 self.metric_req = metrics.Metrics()
60 self.openstack_auth = Common()
61
62 try:
63 self.producer = KafkaProducer(bootstrap_servers='localhost:9092',
64 key_serializer=str.encode,
65 value_serializer=str.encode
66 )
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'])
73 except KafkaError:
74 self.skipTest('Kafka server not present.')
75
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"}}
86
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 = ''
90
91 self.producer.send('metric_request', key="create_metric_request",
92 value=json.dumps(payload))
93
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')
99
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")
104
105 return
106 self.fail("No message received in consumer")
107
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",
116 "vim_uuid": "1",
117 "correlation_id": 123,
118 "metric_name": "cpu_utilization",
119 "resource_uuid": "resource_id"}
120
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 = ''
124
125 self.producer.send('metric_request', key="delete_metric_request",
126 value=json.dumps(payload))
127
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')
133
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",
138 cor_id=123)
139
140 return
141 self.fail("No message received in consumer")
142
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"}
155
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 = ''
159
160 self.producer.send('metric_request', key="read_metric_data_request",
161 value=json.dumps(payload))
162
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')
169
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=[],
174 metrics=[])
175
176 return
177 self.fail("No message received in consumer")
178
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",
187 "vim_uuid": "1",
188 "metrics_list_request":
189 {"correlation_id": 123, }}
190
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 = ''
194
195 self.producer.send('metric_request', key="list_metric_request",
196 value=json.dumps(payload))
197
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')
204
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)
208
209 return
210 self.fail("No message received in consumer")
211
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", }}
222
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 = ''
226
227 self.producer.send('metric_request', key="update_metric_request",
228 value=json.dumps(payload))
229
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')
236
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")
242
243 return
244 self.fail("No message received in consumer")