Included OpenStack integration tests.
[osm/MON.git] / osm_mon / test / integration / test_alarm_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 alarm requests."""
24
25 import json
26
27 import logging
28
29 from osm_mon.core.message_bus.producer import KafkaProducer as prod
30
31 from kafka import KafkaConsumer
32 from kafka import KafkaProducer
33
34 import mock
35
36 from osm_mon.plugins.OpenStack import response
37
38 from osm_mon.plugins.OpenStack.Aodh import alarming
39 from osm_mon.plugins.OpenStack.common import Common
40
41 log = logging.getLogger(__name__)
42
43 # Set up common and alarming class instances
44 alarms = alarming.Alarming()
45 openstack_auth = Common()
46
47 # Initialise the alarm request consumer and a producer for testing
48 producer = KafkaProducer(bootstrap_servers='localhost:9092')
49 req_consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
50 group_id='osm_mon')
51 req_consumer.subscribe("alarm_request")
52
53
54 @mock.patch.object(prod, "update_alarm_response")
55 @mock.patch.object(alarming.Alarming, "update_alarm")
56 @mock.patch.object(response.OpenStack_Response, "generate_response")
57 def test_update_alarm_req(resp, update_alarm, update_resp):
58 """Test Aodh update alarm request message from KafkaProducer."""
59 # Set-up message, producer and consumer for tests
60 payload = {"vim_type": "OpenSTACK",
61 "alarm_update_request":
62 {"correlation_id": 123,
63 "alarm_uuid": "alarm_id",
64 "metric_uuid": "metric_id"}}
65
66 producer.send('alarm_request', key="update_alarm_request",
67 value=json.dumps(payload))
68
69 for message in req_consumer:
70 # Check the vim desired by the message
71 vim_type = json.loads(message.value)["vim_type"].lower()
72 if vim_type == "openstack":
73 # Mock a valid alarm update
74 update_alarm.return_value = "alarm_id", True
75 alarms.alarming(message, openstack_auth, None)
76
77 # A response message is generated and sent via MON's producer
78 resp.assert_called_with(
79 'update_alarm_response', alarm_id="alarm_id", cor_id=123,
80 status=True)
81 update_resp.assert_called_with(
82 'update_alarm_response', resp.return_value, 'alarm_response')
83
84 return
85
86
87 @mock.patch.object(prod, "create_alarm_response")
88 @mock.patch.object(alarming.Alarming, "configure_alarm")
89 @mock.patch.object(response.OpenStack_Response, "generate_response")
90 def test_create_alarm_req(resp, config_alarm, create_resp):
91 """Test Aodh create alarm request message from KafkaProducer."""
92 # Set-up message, producer and consumer for tests
93 payload = {"vim_type": "OpenSTACK",
94 "alarm_create_request":
95 {"correlation_id": 123,
96 "alarm_name": "my_alarm",
97 "metric_name": "my_metric",
98 "resource_uuid": "my_resource",
99 "severity": "WARNING"}}
100
101 producer.send('alarm_request', key="create_alarm_request",
102 value=json.dumps(payload))
103
104 for message in req_consumer:
105 # Check the vim desired by the message
106 vim_type = json.loads(message.value)["vim_type"].lower()
107 if vim_type == "openstack":
108 # Mock a valid alarm creation
109 config_alarm.return_value = "alarm_id", True
110 alarms.alarming(message, openstack_auth, None)
111
112 # A response message is generated and sent via MON's produce
113 resp.assert_called_with(
114 'create_alarm_response', status=True, alarm_id="alarm_id",
115 cor_id=123)
116 create_resp.assert_called_with(
117 'create_alarm_response', resp.return_value, 'alarm_response')
118
119 return
120
121
122 @mock.patch.object(prod, "list_alarm_response")
123 @mock.patch.object(alarming.Alarming, "list_alarms")
124 @mock.patch.object(response.OpenStack_Response, "generate_response")
125 def test_list_alarm_req(resp, list_alarm, list_resp):
126 """Test Aodh list alarm request message from KafkaProducer."""
127 # Set-up message, producer and consumer for tests
128 payload = {"vim_type": "OpenSTACK",
129 "alarm_list_request":
130 {"correlation_id": 123,
131 "resource_uuid": "resource_id", }}
132
133 producer.send('alarm_request', key="list_alarm_request",
134 value=json.dumps(payload))
135
136 for message in req_consumer:
137 # Check the vim desired by the message
138 vim_type = json.loads(message.value)["vim_type"].lower()
139 if vim_type == "openstack":
140 # Mock an empty list generated by the request
141 list_alarm.return_value = []
142 alarms.alarming(message, openstack_auth, None)
143
144 # Resoonse message is generated
145 resp.assert_called_with(
146 'list_alarm_response', alarm_list=[],
147 cor_id=123)
148 # Producer attempts to send the response message back to the SO
149 list_resp.assert_called_with(
150 'list_alarm_response', resp.return_value, 'alarm_response')
151
152 return
153
154
155 @mock.patch.object(alarming.Alarming, "delete_alarm")
156 @mock.patch.object(prod, "delete_alarm_response")
157 @mock.patch.object(response.OpenStack_Response, "generate_response")
158 def test_delete_alarm_req(resp, del_resp, del_alarm):
159 """Test Aodh delete alarm request message from KafkaProducer."""
160 # Set-up message, producer and consumer for tests
161 payload = {"vim_type": "OpenSTACK",
162 "alarm_delete_request":
163 {"correlation_id": 123,
164 "alarm_uuid": "alarm_id", }}
165
166 producer.send('alarm_request', key="delete_alarm_request",
167 value=json.dumps(payload))
168
169 for message in req_consumer:
170 # Check the vim desired by the message
171 vim_type = json.loads(message.value)["vim_type"].lower()
172 if vim_type == "openstack":
173 alarms.alarming(message, openstack_auth, None)
174
175 # Response message is generated and sent by MON's producer
176 resp.assert_called_with(
177 'delete_alarm_response', alarm_id="alarm_id",
178 status=del_alarm.return_value, cor_id=123)
179 del_resp.assert_called_with(
180 'delete_alarm_response', resp.return_value, 'alarm_response')
181
182 return
183
184
185 @mock.patch.object(alarming.Alarming, "update_alarm_state")
186 def test_ack_alarm_req(ack_alarm):
187 """Test Aodh acknowledge alarm request message from KafkaProducer."""
188 # Set-up message, producer and consumer for tests
189 payload = {"vim_type": "OpenSTACK",
190 "ack_details":
191 {"alarm_uuid": "alarm_id", }}
192
193 producer.send('alarm_request', key="acknowledge_alarm",
194 value=json.dumps(payload))
195
196 for message in req_consumer:
197 # Check the vim desired by the message
198 vim_type = json.loads(message.value)["vim_type"].lower()
199 if vim_type == "openstack":
200 alarms.alarming(message, openstack_auth, None)
201 # No response message is sent for and ack request
202 # Alarm state is updated from alarm -> ok
203 ack_alarm.assert_called_with(None, None, "alarm_id")
204 return