Debian Codegraph @

ansible_collections/dellemc/openmanage/tests/unit/plugins/modules/test_redfish_event_subscription.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 #
4 # Dell OpenManage Ansible Modules
5 # Version 7.0.0
6 # Copyright (C) 2021-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
7
8 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
9 #
10
11 from __future__ import (absolute_import, division, print_function)
12
13 __metaclass__ = type
14
15 import pytest
16 from ansible_collections.dellemc.openmanage.plugins.modules import redfish_event_subscription
17 from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
18
19 MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.'
20 DESTINATION_INVALID = "The Parameter destination must have an HTTPS destination. The HTTP destination is not allowed"
21 SUBSCRIPTION_EXISTS = "No changes found to be applied."
22 SUBSCRIPTION_DELETED = "Successfully deleted the subscription."
23 SUBSCRIPTION_UNABLE_DEL = "Unable to delete the subscription."
24 SUBSCRIPTION_UNABLE_ADD = "Unable to add a subscription."
25 SUBSCRIPTION_ADDED = "Successfully added the subscription."
26 DESTINATION_MISMATCH = "No changes found to be applied."
27 EVENT_TYPE_INVALID = "value of event_type must be one of: Alert, MetricReport, got: Metricreport"
28 PARAM_DESTINATION = "https://XX.XX.XX.XX:8188"
29
30
31 @pytest.fixture
32 def redfish_connection_mock(mocker, redfish_response_mock):
33 connection_class_mock = mocker.patch(MODULE_PATH + 'redfish_event_subscription.Redfish')
34 redfish_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value
35 redfish_connection_mock_obj.invoke_request.return_value = redfish_response_mock
36 return redfish_connection_mock_obj
37
38
39 class TestRedfishSubscription(FakeAnsibleModule):
40 module = redfish_event_subscription
41
42 @pytest.mark.parametrize("val", [{"destination": PARAM_DESTINATION},
43 {"destination": "https://XX.XX.XX.XX:8189"}])
44 def test_function_get_subscription_success(self, mocker, redfish_connection_mock, redfish_response_mock,
45 redfish_default_args, val):
46 redfish_default_args.update({"state": "absent"})
47 redfish_default_args.update({"destination": val["destination"]})
48 redfish_default_args.update({"event_type": "MetricReport"})
49 redfish_default_args.update({"event_format_type": "MetricReport"})
50 json_data1 = {
51 "@odata.context": "/redfish/v1/$metadata#EventDestination.EventDestination",
52 "@odata.id": "/redfish/v1/EventService/Subscriptions/c7e5c3fc-8204-11eb-bd10-2cea7ff7fe80",
53 "@odata.type": "#EventDestination.v1_6_0.EventDestination",
54 "Context": "RedfishEvent",
55 "DeliveryRetryPolicy": "RetryForever",
56 "Description": "Event Subscription Details",
57 "Destination": "https://XX.XX.XX.XX:8189",
58 "EventFormatType": "Event",
59 "EventTypes": [
60 "Alert"
61 ],
62 "[email protected]": 1,
63 "HttpHeaders": [],
64 "[email protected]": 0,
65 "Id": "c7e5c3fc-8204-11eb-bd10-2cea7ff7fe80",
66 "MetricReportDefinitions": [],
67 "[email protected]": 0,
68 "Name": "EventSubscription c7e5c3fc-8204-11eb-bd10-2cea7ff7fe80",
69 "OriginResources": [],
70 "[email protected]": 0,
71 "Protocol": "Redfish",
72 "Status": {
73 "Health": "OK",
74 "HealthRollup": "OK",
75 "State": "Enabled"
76 },
77 "SubscriptionType": "RedfishEvent"
78 }
79 json_data2 = {
80 "@odata.context": "/redfish/v1/$metadata#EventDestination.EventDestination",
81 "@odata.id": "/redfish/v1/EventService/Subscriptions/c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
82 "@odata.type": "#EventDestination.v1_6_0.EventDestination",
83 "Context": "RedfishEvent",
84 "DeliveryRetryPolicy": "RetryForever",
85 "Description": "Event Subscription Details",
86 "Destination": PARAM_DESTINATION,
87 "EventFormatType": "MetricReport",
88 "EventTypes": [
89 "MetricReport"
90 ],
91 "[email protected]": 1,
92 "HttpHeaders": [],
93 "[email protected]": 0,
94 "Id": "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
95 "MetricReportDefinitions": [],
96 "[email protected]": 0,
97 "Name": "EventSubscription c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
98 "OriginResources": [],
99 "[email protected]": 0,
100 "Protocol": "Redfish",
101 "Status": {
102 "Health": "OK",
103 "HealthRollup": "OK",
104 "State": "Enabled"
105 },
106 "SubscriptionType": "RedfishEvent"
107 }
108
109 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription_details',
110 side_effect=[json_data1, json_data2])
111
112 redfish_response_mock.json_data = {
113 "@odata.context": "/redfish/v1/$metadata#EventDestinationCollection.EventDestinationCollection",
114 "@odata.id": "/redfish/v1/EventService/Subscriptions",
115 "@odata.type": "#EventDestinationCollection.EventDestinationCollection",
116 "Description": "List of Event subscriptions",
117 "Members": [
118 {
119 "@odata.id": "/redfish/v1/EventService/Subscriptions/c6ff37fc-8204-11eb-b08f-2cea7ff7fe80"
120 },
121 {
122 "@odata.id": "/redfish/v1/EventService/Subscriptions/c7e5c3fc-8204-11eb-bd10-2cea7ff7fe80"
123 }
124 ],
125 "[email protected]": 2,
126 "Name": "Event Subscriptions Collection"
127 }
128 redfish_response_mock.success = True
129 f_module = self.get_module_mock(params=redfish_default_args)
130 result = self.module.get_subscription(redfish_connection_mock, val["destination"])
131 assert result["Destination"] == val["destination"]
132
133 @pytest.mark.parametrize("val", [
134 {"destination": PARAM_DESTINATION, "event_type": "MetricReport",
135 "event_format_type": "MetricReport"},
136 {"destination": PARAM_DESTINATION, "event_type": "Alert", "event_format_type": "Event"}])
137 def test_function_create_subscription(self, mocker, redfish_connection_mock, redfish_response_mock,
138 redfish_default_args, val):
139 redfish_default_args.update({"state": "absent"})
140 redfish_default_args.update({"destination": val["destination"]})
141 redfish_default_args.update({"event_type": val["event_type"]})
142 redfish_default_args.update({"event_format_type": val["event_format_type"]})
143
144 redfish_response_mock.json_data = {
145 "Id": "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
146 "Destination": val["destination"],
147 "EventFormatType": val["event_format_type"],
148 "Context": "RedfishEvent",
149 "Protocol": "Redfish",
150 "EventTypes": [val["event_type"]],
151 "SubscriptionType": "RedfishEvent"
152 }
153 redfish_response_mock.success = True
154 f_module = self.get_module_mock(params=redfish_default_args)
155 result = self.module.create_subscription(redfish_connection_mock, f_module)
156 assert result.json_data["Destination"] == val["destination"]
157 assert result.json_data["EventFormatType"] == val["event_format_type"]
158 assert result.json_data["EventTypes"] == [val["event_type"]]
159
160 @pytest.mark.parametrize("val", [
161 {"destination": "https://XX.XX.XX.XX:161", "event_type": "MetricReport",
162 "event_format_type": "MetricReport"},
163 {"destination": "https://XX.XX.XX.XX:161", "event_type": "Alert", "event_format_type": "Event"}])
164 def test_function_get_subscription_details(self, mocker, redfish_connection_mock, redfish_response_mock,
165 redfish_default_args, val):
166 redfish_default_args.update({"state": "absent"})
167 redfish_default_args.update({"destination": val["destination"]})
168 redfish_default_args.update({"event_type": val["event_type"]})
169 redfish_default_args.update({"event_format_type": val["event_format_type"]})
170
171 redfish_response_mock.json_data = {
172 "@odata.context": "/redfish/v1/$metadata#EventDestination.EventDestination",
173 "@odata.id": "/redfish/v1/EventService/Subscriptions/087b9026-0afa-11ec-8120-4cd98f5fc5a6",
174 "@odata.type": "#EventDestination.v1_9_0.EventDestination",
175 "Actions": {
176 "#EventDestination.ResumeSubscription": {
177 "target": "/redfish/v1/EventService/Subscriptions/087b9026-0afa-11ec-8120-4cd98f5fc5a6/Actions/EventDestination.ResumeSubscription"
178 }
179 },
180 "Context": "RedfishEvent",
181 "DeliveryRetryPolicy": "RetryForever",
182 "Description": "Event Subscription Details",
183 "Destination": val['destination'],
184 "EventFormatType": val["event_format_type"],
185 "EventTypes": [val["event_type"]],
186 "[email protected]": 1,
187 "HttpHeaders": [],
188 "[email protected]": 0,
189 "Id": "087b9026-0afa-11ec-8120-4cd98f5fc5a6",
190 "Name": "EventSubscription 087b9026-0afa-11ec-8120-4cd98f5fc5a6",
191 "Protocol": "Redfish",
192 "Status": {
193 "Health": "OK",
194 "HealthRollup": "OK",
195 "State": "Enabled"
196 },
197 "SubscriptionType": "RedfishEvent"
198 }
199 redfish_response_mock.success = True
200 result = self.module.get_subscription_details(redfish_connection_mock, "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80")
201 assert result["Destination"] == val["destination"]
202 assert result["EventFormatType"] == val["event_format_type"]
203 assert result["EventTypes"] == [val["event_type"]]
204
205 @pytest.mark.parametrize("val", [
206 {"destination": "https://XX.XX.XX.XX:161", "event_type": "MetricReport",
207 "event_format_type": "MetricReport"},
208 {"destination": "https://XX.XX.XX.XX:161", "event_type": "Alert", "event_format_type": "Event"}])
209 def test_function_get_subscription_details_None(self, mocker, redfish_connection_mock, redfish_response_mock,
210 redfish_default_args, val):
211 redfish_default_args.update({"state": "absent"})
212 redfish_default_args.update({"destination": val["destination"]})
213 redfish_default_args.update({"event_type": val["event_type"]})
214 redfish_default_args.update({"event_format_type": val["event_format_type"]})
215
216 redfish_response_mock.json_data = {
217 "@odata.context": "/redfish/v1/$metadata#EventDestination.EventDestination",
218 "@odata.id": "/redfish/v1/EventService/Subscriptions/087b9026-0afa-11ec-8120-4cd98f5fc5a6",
219 "@odata.type": "#EventDestination.v1_9_0.EventDestination",
220 "Actions": {
221 "#EventDestination.ResumeSubscription": {
222 "target": "/redfish/v1/EventService/Subscriptions/087b9026-0afa-11ec-8120-4cd98f5fc5a6/Actions/EventDestination.ResumeSubscription"
223 }
224 },
225 "Context": "RedfishEvent",
226 "DeliveryRetryPolicy": "RetryForever",
227 "Description": "Event Subscription Details",
228 "Destination": val['destination'],
229 "EventFormatType": val["event_format_type"],
230 "EventTypes": [val["event_type"]],
231 "[email protected]": 1,
232 "HttpHeaders": [],
233 "[email protected]": 0,
234 "Id": "087b9026-0afa-11ec-8120-4cd98f5fc5a6",
235 "Name": "EventSubscription 087b9026-0afa-11ec-8120-4cd98f5fc5a6",
236 "Protocol": "Redfish",
237 "Status": {
238 "Health": "OK",
239 "HealthRollup": "OK",
240 "State": "Enabled"
241 },
242 "SubscriptionType": "RedfishEvent"
243 }
244 redfish_response_mock.success = False
245 result = self.module.get_subscription_details(redfish_connection_mock, "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80")
246 assert result is None
247
248 @pytest.mark.parametrize("val", [
249 {"destination": "https://XX.XX.XX.XX:161"},
250 {"destination": "https://XX.XX.XX.XX:161"}])
251 def test_function_delete_subscription(self, mocker, redfish_connection_mock, redfish_response_mock,
252 redfish_default_args, val):
253 redfish_default_args.update({"state": "absent"})
254 redfish_default_args.update({"destination": val["destination"]})
255
256 redfish_response_mock.json_data = {
257 "@Message.ExtendedInfo": [
258 {
259 "Message": "Successfully Completed Request",
260 "MessageArgs": [],
261 "[email protected]": 0,
262 "MessageId": "Base.1.7.Success",
263 "RelatedProperties": [],
264 "[email protected]": 0,
265 "Resolution": "None",
266 "Severity": "OK"
267 },
268 {
269 "Message": "The operation successfully completed.",
270 "MessageArgs": [],
271 "[email protected]": 0,
272 "MessageId": "IDRAC.2.4.SYS413",
273 "RelatedProperties": [],
274 "[email protected]": 0,
275 "Resolution": "No response action is required.",
276 "Severity": "Informational"
277 }
278 ]
279 }
280 redfish_response_mock.success = True
281 result = self.module.delete_subscription(redfish_connection_mock, "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80")
282 assert result.json_data["@Message.ExtendedInfo"][0]["Message"] == "Successfully Completed Request"
283 assert result.json_data["@Message.ExtendedInfo"][1]["Message"] == "The operation successfully completed."
284
285 def test_module_validation_input_params(self, mocker, redfish_connection_mock, redfish_response_mock,
286 redfish_default_args):
287 redfish_default_args.update({"state": "absent"})
288 http_str = "http"
289 redfish_default_args.update({"destination": http_str + "://XX.XX.XX.XX:8188"})
290 redfish_default_args.update({"event_type": "MetricReport"})
291 redfish_default_args.update({"event_format_type": "MetricReport"})
292 with pytest.raises(Exception) as err:
293 self._run_module(redfish_default_args)
294 assert err.value.args[0]['msg'] == DESTINATION_INVALID
295
296 def test_module_absent_does_not_exist(self, mocker, redfish_connection_mock, redfish_response_mock,
297 redfish_default_args):
298 redfish_default_args.update({"state": "absent"})
299 redfish_default_args.update({"destination": PARAM_DESTINATION})
300 redfish_default_args.update({"event_type": "MetricReport"})
301 redfish_default_args.update({"event_format_type": "MetricReport"})
302
303 redfish_connection_mock.patch(
304 MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=None)
305 redfish_response_mock.success = True
306 result = self._run_module(redfish_default_args)
307 assert result["msg"] == DESTINATION_MISMATCH
308
309 def test_module_absent_does_exist(self, mocker, redfish_connection_mock, redfish_response_mock,
310 redfish_default_args):
311 redfish_default_args.update({"state": "absent"})
312 redfish_default_args.update({"destination": PARAM_DESTINATION})
313 redfish_default_args.update({"event_type": "MetricReport"})
314 redfish_default_args.update({"event_format_type": "MetricReport"})
315
316 json_data = {
317 "Id": "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
318 "Destination": PARAM_DESTINATION,
319 "EventFormatType": "MetricReport",
320 "Context": "RedfishEvent",
321 "Protocol": "Redfish",
322 "EventTypes": ["MetricReport"],
323 "SubscriptionType": "RedfishEvent"
324 }
325 redfish_response_mock.success = True
326 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=json_data)
327 mocker.patch(MODULE_PATH + 'redfish_event_subscription.delete_subscription', return_value=redfish_response_mock)
328 f_module = self.get_module_mock()
329 result = self._run_module(redfish_default_args)
330 print(result)
331 assert result["msg"] == SUBSCRIPTION_DELETED
332
333 def test_module_absent_does_exist_error(self, mocker, redfish_connection_mock, redfish_response_mock,
334 redfish_default_args):
335 redfish_default_args.update({"state": "absent"})
336 redfish_default_args.update({"destination": PARAM_DESTINATION})
337 redfish_default_args.update({"event_type": "MetricReport"})
338 redfish_default_args.update({"event_format_type": "MetricReport"})
339
340 json_data = {
341 "Id": "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
342 "Destination": PARAM_DESTINATION,
343 "EventFormatType": "MetricReport",
344 "Context": "RedfishEvent",
345 "Protocol": "Redfish",
346 "EventTypes": ["MetricReport"],
347 "SubscriptionType": "RedfishEvent"
348 }
349 redfish_response_mock.success = False
350 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=json_data)
351 mocker.patch(MODULE_PATH + 'redfish_event_subscription.delete_subscription', return_value=redfish_response_mock)
352 with pytest.raises(Exception) as err:
353 self._run_module(redfish_default_args)
354 assert err.value.args[0]['msg'] == SUBSCRIPTION_UNABLE_DEL
355
356 def test_module_present_does_not_exist(self, mocker, redfish_connection_mock, redfish_response_mock,
357 redfish_default_args):
358 redfish_default_args.update({"state": "present"})
359 redfish_default_args.update({"destination": PARAM_DESTINATION})
360 redfish_default_args.update({"event_type": "MetricReport"})
361 redfish_default_args.update({"event_format_type": "MetricReport"})
362
363 json_data = {
364 "Destination": PARAM_DESTINATION,
365 "EventFormatType": "MetricReport",
366 "Context": "RedfishEvent",
367 "Protocol": "Redfish",
368 "EventTypes": ["MetricReport"],
369 "SubscriptionType": "RedfishEvent"
370 }
371 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=None)
372 create_subscription_response_mock = redfish_response_mock
373 create_subscription_response_mock.json_data = json_data
374 mocker.patch(MODULE_PATH + 'redfish_event_subscription.create_subscription',
375 return_value=create_subscription_response_mock)
376 f_module = self.get_module_mock()
377 redfish_response_mock.success = True
378 result = self._run_module(redfish_default_args)
379 print(result)
380 assert result["msg"] == SUBSCRIPTION_ADDED
381
382 def test_module_present_does_not_exist_error(self, mocker, redfish_connection_mock, redfish_response_mock,
383 redfish_default_args):
384 redfish_default_args.update({"state": "present"})
385 redfish_default_args.update({"destination": PARAM_DESTINATION})
386 redfish_default_args.update({"event_type": "MetricReport"})
387 redfish_default_args.update({"event_format_type": "MetricReport"})
388
389 json_data = {
390 "Destination": PARAM_DESTINATION,
391 "EventFormatType": "MetricReport",
392 "Context": "RedfishEvent",
393 "Protocol": "Redfish",
394 "EventTypes": ["MetricReport"],
395 "SubscriptionType": "RedfishEvent"
396 }
397 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=None)
398 create_subscription_response_mock = redfish_response_mock
399 create_subscription_response_mock.json_data = json_data
400 mocker.patch(MODULE_PATH + 'redfish_event_subscription.create_subscription',
401 return_value=create_subscription_response_mock)
402 redfish_response_mock.success = False
403 with pytest.raises(Exception) as err:
404 self._run_module(redfish_default_args)
405 assert err.value.args[0]['msg'] == SUBSCRIPTION_UNABLE_ADD
406
407 def test_module_present_does_not_exist_error_wrong_input(self, mocker, redfish_connection_mock,
408 redfish_response_mock,
409 redfish_default_args):
410 redfish_default_args.update({"state": "present"})
411 redfish_default_args.update({"destination": PARAM_DESTINATION})
412 redfish_default_args.update({"event_type": "Metricreport"})
413 redfish_default_args.update({"event_format_type": "MetricReport"})
414
415 json_data = {
416 "Destination": PARAM_DESTINATION,
417 "EventFormatType": "MetricReport",
418 "Context": "RedfishEvent",
419 "Protocol": "Redfish",
420 "EventTypes": ["MetricReport"],
421 "SubscriptionType": "RedfishEvent"
422 }
423 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=None)
424 create_subscription_response_mock = redfish_response_mock
425 create_subscription_response_mock.json_data = json_data
426 mocker.patch(MODULE_PATH + 'redfish_event_subscription.create_subscription',
427 return_value=create_subscription_response_mock)
428 f_module = self.get_module_mock()
429 redfish_response_mock.success = True
430 with pytest.raises(Exception) as err:
431 self._run_module(redfish_default_args)
432 print(err)
433 assert err.value.args[0]['msg'] == EVENT_TYPE_INVALID
434
435 def test_module_present_does_exist(self, mocker, redfish_connection_mock, redfish_response_mock,
436 redfish_default_args):
437 redfish_default_args.update({"state": "present"})
438 redfish_default_args.update({"destination": PARAM_DESTINATION})
439 redfish_default_args.update({"event_type": "MetricReport"})
440 redfish_default_args.update({"event_format_type": "MetricReport"})
441
442 json_data = {
443 "Id": "c6ff37fc-8204-11eb-b08f-2cea7ff7fe80",
444 "Destination": PARAM_DESTINATION,
445 "EventFormatType": "MetricReport",
446 "Context": "RedfishEvent",
447 "Protocol": "Redfish",
448 "EventTypes": ["MetricReport"],
449 "SubscriptionType": "RedfishEvent"
450 }
451 mocker.patch(MODULE_PATH + 'redfish_event_subscription.get_subscription', return_value=json_data)
452 redfish_response_mock.success = True
453 result = self._run_module(redfish_default_args)
454 assert result["msg"] == SUBSCRIPTION_EXISTS