Debian Codegraph @

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

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 #
4 # Dell OpenManage Ansible Modules
5 # Version 9.1.0
6 # Copyright (C) 2022-2024 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 json
16 from io import StringIO
17 from ssl import SSLError
18
19 import pytest
20 from ansible.module_utils._text import to_text
21 from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
22 from ansible.module_utils.urls import ConnectionError, SSLValidationError
23 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import CHANGES_MSG, NO_CHANGES_MSG
24 from ansible_collections.dellemc.openmanage.plugins.modules import ome_devices
25 from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
26
27 DELETE_SUCCESS = "The devices(s) are removed successfully."
28 INVALID_DEV_ST = "Unable to complete the operation because the entered target device(s) '{0}' are invalid."
29 JOB_DESC = "The {0} task initiated from OpenManage Ansible Modules for devices with the ids '{1}'."
30 APPLY_TRIGGERED = "Successfully initiated the device action job."
31 JOB_SCHEDULED = "The job is scheduled successfully."
32 SUCCESS_MSG = "The device operation is performed successfully."
33
34 MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_devices.'
35
36
37 @pytest.fixture
38 def ome_connection_mock_for_devices(mocker, ome_response_mock):
39 connection_class_mock = mocker.patch('ansible_collections.dellemc.openmanage.plugins.modules.ome_devices.RestOME')
40 ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value
41 ome_connection_mock_obj.invoke_request.return_value = ome_response_mock
42 return ome_connection_mock_obj
43
44
45 class TestOmeDevices(FakeAnsibleModule):
46 module = ome_devices
47
48 @pytest.mark.parametrize("params", [
49 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000}]},
50 'message': DELETE_SUCCESS, "success": True, 'mparams': {
51 "device_service_tags": ["ABCTAG1", "BCDTAG2"], 'state': 'absent'}},
52 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000}]},
53 'message': CHANGES_MSG, "success": True,
54 'check_mode': True,
55 'mparams': {"device_service_tags": ["ABCTAG1", "BCDTAG2"], 'state': 'absent'}},
56 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000}]},
57 'message': NO_CHANGES_MSG, "success": True,
58 'mparams': {"device_service_tags": ["ABCTAG2", "BCDTAG2"], 'state': 'absent'}},
59 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG2", "Type": 1001}]},
60 'message': INVALID_DEV_ST.format(",".join(map(str, ["ABCTAG2"]))), "success": True,
61 'mparams': {"device_service_tags": ["ABCTAG2"], 'state': 'present'}},
62 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG2", "Type": 1001}]},
63 'message': INVALID_DEV_ST.format(",".join(map(str, [24, 25]))), "success": True,
64 'mparams': {"device_ids": [24, 25], 'state': 'present'}},
65 {"json_data": {"value": []},
66 'message': INVALID_DEV_ST.format(",".join(map(str, [24]))), "success": True,
67 'mparams': {"device_ids": [24], 'state': 'present'}}
68 ])
69 def test_ome_devices_delete(self, params, ome_connection_mock_for_devices, ome_response_mock, ome_default_args,
70 module_mock):
71 ome_response_mock.success = params.get("success", True)
72 ome_response_mock.json_data = params['json_data']
73 ome_connection_mock_for_devices.get_all_items_with_pagination.return_value = params['json_data']
74 ome_default_args.update(params['mparams'])
75 result = self._run_module(ome_default_args, check_mode=params.get('check_mode', False))
76 assert result['msg'] == params['message']
77
78 @pytest.mark.parametrize("params", [
79 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000},
80 {'Id': 25, 'Identifier': "BCDTAG2", "Type": 1000}]},
81 'message': APPLY_TRIGGERED, "success": True, 'mparams': {
82 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False,
83 "job_name": "my test job", "job_description": "My job description"
84 }, "check_similar_job": {}},
85 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000},
86 {'Id': 25, 'Identifier': "BCDTAG2", "Type": 1000}]},
87 'message': APPLY_TRIGGERED, "success": True, 'mparams': {
88 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False
89 }, "check_similar_job": {}},
90 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000},
91 {'Id': 25, 'Identifier': "BCDTAG2", "Type": 1000}]},
92 'message': JOB_SCHEDULED, "success": True, 'mparams': {
93 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False,
94 "job_schedule": "my cron task"
95 }, "check_similar_job": {}},
96 {"json_data": {"value": [{'Id': 24, 'Identifier': "ABCTAG1", "Type": 1000},
97 {'Id': 25, 'Identifier': "BCDTAG2", "Type": 1000}]},
98 'message': CHANGES_MSG, "success": True, 'mparams': {
99 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False},
100 "check_similar_job": {}, "check_mode": True
101 },
102 {"json_data": {
103 "value": [
104 {
105 "Id": 14874,
106 "JobName": "Refresh inventory",
107 "JobDescription": JOB_DESC.format("Refresh inventory", "13216"),
108 "Schedule": "startnow",
109 "State": "Enabled",
110 "Targets": [
111 {
112 "JobId": 14874,
113 "Id": 13123,
114 "Data": "",
115 "TargetType": {
116 "Id": 1000,
117 "Name": "DEVICE"
118 }
119 }
120 ],
121 "Params": [
122 {
123 "JobId": 14874,
124 "Key": "action",
125 "Value": "CONFIG_INVENTORY"
126 },
127 {
128 "JobId": 14874,
129 "Key": "isCollectDriverInventory",
130 "Value": "true"
131 }
132 ],
133 "LastRunStatus": {
134 "@odata.type": "#JobService.JobStatus",
135 "Id": 2060,
136 "Name": "Completed"
137 },
138 "JobType": {
139 "@odata.type": "#JobService.JobType",
140 "Id": 8,
141 "Name": "Inventory_Task",
142 },
143 },
144 {
145 "Id": 14874,
146 "JobName": "Refresh inventory",
147 "JobDescription": JOB_DESC.format("Refresh inventory", "13216"),
148 "Schedule": "startnow",
149 "State": "Enabled",
150 "Targets": [
151 {
152 "JobId": 14874,
153 "Id": 13216,
154 "Data": "",
155 "TargetType": {
156 "Id": 1000,
157 "Name": "DEVICE"
158 }
159 }
160 ],
161 "Params": [
162 {
163 "JobId": 14874,
164 "Key": "action",
165 "Value": "CONFIG_INVENTORY"
166 },
167 {
168 "JobId": 14874,
169 "Key": "isCollectDriverInventory",
170 "Value": "false"
171 }
172 ],
173 "LastRunStatus": {
174 "@odata.type": "#JobService.JobStatus",
175 "Id": 2060,
176 "Name": "Completed"
177 },
178 "JobType": {
179 "@odata.type": "#JobService.JobType",
180 "Id": 8,
181 "Name": "Inventory_Task",
182 },
183 },
184 {
185 "Id": 14874,
186 "JobName": "Refresh inventory",
187 "JobDescription": JOB_DESC.format("Refresh inventory", "13216"),
188 "Schedule": "startnow",
189 "State": "Enabled",
190 "Targets": [
191 {
192 "JobId": 14874,
193 "Id": 13216,
194 "Data": "",
195 "TargetType": {
196 "Id": 1000,
197 "Name": "DEVICE"
198 }
199 }
200 ],
201 "Params": [
202 {
203 "JobId": 14874,
204 "Key": "action",
205 "Value": "CONFIG_INVENTORY"
206 },
207 {
208 "JobId": 14874,
209 "Key": "isCollectDriverInventory",
210 "Value": "true"
211 }
212 ],
213 "LastRunStatus": {
214 "@odata.type": "#JobService.JobStatus",
215 "Id": 2060,
216 "Name": "Completed"
217 },
218 "JobType": {
219 "@odata.type": "#JobService.JobType",
220 "Id": 8,
221 "Name": "Inventory_Task",
222 },
223 }
224 ]
225 },
226 'message': APPLY_TRIGGERED, "success": True, 'mparams': {
227 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False
228 }, "get_dev_ids": ([13216], {})},
229
230 {"json_data": {
231 "value": [
232 {
233 "Id": 14874,
234 "JobName": "Refresh inventory",
235 "JobDescription": JOB_DESC.format("Refresh inventory", "13216"),
236 "Schedule": "startnow",
237 "State": "Enabled",
238 "Targets": [
239 {
240 "JobId": 14874,
241 "Id": 13216,
242 "Data": "",
243 "TargetType": {
244 "Id": 1000,
245 "Name": "DEVICE"
246 }
247 }
248 ],
249 "Params": [
250 {
251 "JobId": 14874,
252 "Key": "action",
253 "Value": "CONFIG_INVENTORY"
254 },
255 {
256 "JobId": 14874,
257 "Key": "isCollectDriverInventory",
258 "Value": "true"
259 }
260 ],
261 "LastRunStatus": {
262 "@odata.type": "#JobService.JobStatus",
263 "Id": 2060,
264 "Name": "Completed"
265 },
266 "JobType": {
267 "@odata.type": "#JobService.JobType",
268 "Id": 8,
269 "Name": "Inventory_Task",
270 },
271 }
272 ]
273 },
274 'message': CHANGES_MSG, "success": True, 'mparams': {
275 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False
276 }, "get_dev_ids": ([13216], {}), "check_mode": True},
277 {"json_data": {
278 "value": [
279 {
280 "Id": 14874,
281 "JobName": "Refresh inventory",
282 "JobDescription": JOB_DESC.format("Refresh inventory", "13216"),
283 "Schedule": "startnow",
284 "State": "Enabled",
285 "Targets": [
286 {
287 "JobId": 14874,
288 "Id": 13216,
289 "Data": "",
290 "TargetType": {
291 "Id": 1000,
292 "Name": "DEVICE"
293 }
294 }
295 ],
296 "Params": [
297 {
298 "JobId": 14874,
299 "Key": "action",
300 "Value": "CONFIG_INVENTORY"
301 },
302 {
303 "JobId": 14874,
304 "Key": "isCollectDriverInventory",
305 "Value": "true"
306 }
307 ],
308 "LastRunStatus": {
309 "@odata.type": "#JobService.JobStatus",
310 "Id": 2050,
311 "Name": "Completed"
312 },
313 "JobType": {
314 "@odata.type": "#JobService.JobType",
315 "Id": 8,
316 "Name": "Inventory_Task",
317 },
318 }
319 ]},
320 'message': NO_CHANGES_MSG, "success": True, 'mparams': {
321 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": False
322 }, "get_dev_ids": ([13216], {})},
323 {"json_data": {
324 "value": [
325 {
326 "Id": 14874,
327 "JobName": "Reset iDRAC",
328 "JobDescription": JOB_DESC.format("Reset iDRAC", "13216"),
329 "Schedule": "startnow",
330 "State": "Enabled",
331 "Targets": [
332 {
333 "JobId": 14874,
334 "Id": 13216,
335 "Data": "",
336 "TargetType": {
337 "Id": 1000,
338 "Name": "DEVICE"
339 }
340 }
341 ],
342 "Params": [
343 {
344 "JobId": 14874,
345 "Key": "operationName",
346 "Value": "RESET_IDRAC"
347 }
348 ],
349 "LastRunStatus": {
350 "@odata.type": "#JobService.JobStatus",
351 "Id": 2050,
352 "Name": "Completed"
353 },
354 "JobType": {
355 "@odata.type": "#JobService.JobType",
356 "Id": 3,
357 "Name": "DeviceAction_Task",
358 },
359 }
360 ]},
361 'message': NO_CHANGES_MSG, "success": True, 'mparams': {
362 "device_service_tags": ["ABCTAG1", "BCDTAG2"],
363 "job_wait": False, "device_action": 'reset_idrac',
364 }, "get_dev_ids": ([13216], {})},
365 {"json_data": {
366 "value": [
367 {
368 "Id": 14874,
369 "JobName": "Clear iDRAC job queue",
370 "JobDescription": JOB_DESC.format("Clear iDRAC job queue", "13216"),
371 "Schedule": "startnow",
372 "State": "Enabled",
373 "Targets": [
374 {
375 "JobId": 14874,
376 "Id": 13216,
377 "Data": "",
378 "TargetType": {
379 "Id": 1000,
380 "Name": "DEVICE"
381 }
382 }
383 ],
384 "Params": [
385 {
386 "JobId": 14874,
387 "Key": "deviceTypes",
388 "Value": "1000"
389 },
390 {
391 "JobId": 14874,
392 "Key": "operationName",
393 "Value": "REMOTE_RACADM_EXEC"
394 },
395 {
396 "JobId": 14874,
397 "Key": "Command",
398 "Value": "jobqueue delete -i JID_CLEARALL_FORCE"
399 },
400 {
401 "JobId": 14874,
402 "Key": "CommandTimeout",
403 "Value": "60"
404 }
405 ],
406 "LastRunStatus": {
407 "@odata.type": "#JobService.JobStatus",
408 "Id": 2050,
409 "Name": "Completed"
410 },
411 "JobType": {
412 "@odata.type": "#JobService.JobType",
413 "Id": 3,
414 "Name": "DeviceAction_Task",
415 },
416 }
417 ]},
418 'message': NO_CHANGES_MSG, "success": True, 'mparams': {
419 "device_service_tags": ["ABCTAG1", "BCDTAG2"],
420 "job_wait": False, "device_action": 'clear_idrac_job_queue',
421 }, "get_dev_ids": ([13216], {})},
422 {"json_data": {"Id": 14874, "LastRunStatus": {"Id": 2060, "Name": "Completed"}},
423 'message': SUCCESS_MSG, "success": True, 'mparams': {
424 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": True
425 }, "check_similar_job": {}, "get_dev_ids": ([13216], {})},
426 {"json_data": {"Id": 14874, "LastRunStatus": {"Id": 2070, "Name": "Completed"},
427 "Value": "Job Tracking has failed"},
428 'message': "Job Tracking has failed", "success": True, 'mparams': {
429 "device_service_tags": ["ABCTAG1", "BCDTAG2"], "job_wait": True
430 }, "check_similar_job": {}, "get_dev_ids": ([13216], {})}
431 ])
432 def test_ome_devices_main_state_present(self, params, ome_connection_mock_for_devices, ome_response_mock,
433 ome_default_args, module_mock, mocker):
434 ome_response_mock.success = params.get("success", True)
435 ome_response_mock.json_data = params['json_data']
436 mocks = ["check_similar_job", "get_dev_ids"]
437 for m in mocks:
438 if m in params:
439 mocker.patch(MODULE_PATH + m, return_value=params.get(m, {}))
440 mocker.patch("ansible_collections.dellemc.openmanage.plugins.module_utils.utils." + 'time.sleep', return_value=None)
441 ome_default_args.update(params['mparams'])
442 result = self._run_module(ome_default_args, check_mode=params.get('check_mode', False))
443 assert result['msg'] == params['message']
444
445 @pytest.mark.parametrize("exc_type",
446 [IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError])
447 def test_ome_devices_main_exception_failure_case(self, exc_type, mocker, ome_default_args,
448 ome_connection_mock_for_devices, ome_response_mock):
449 ome_default_args.update({"state": "absent", "device_service_tags": "t1"})
450 ome_response_mock.status_code = 400
451 ome_response_mock.success = False
452 json_str = to_text(json.dumps({"info": "error_details"}))
453 if exc_type == URLError:
454 mocker.patch(MODULE_PATH + 'get_dev_ids', side_effect=exc_type("url open error"))
455 result = self._run_module(ome_default_args)
456 assert result["unreachable"] is True
457 elif exc_type not in [HTTPError, SSLValidationError]:
458 mocker.patch(MODULE_PATH + 'get_dev_ids', side_effect=exc_type("exception message"))
459 result = self._run_module(ome_default_args)
460 assert result['failed'] is True
461 else:
462 mocker.patch(MODULE_PATH + 'get_dev_ids',
463 side_effect=exc_type('https://testhost.com', 400, 'http error message',
464 {"accept-type": "application/json"}, StringIO(json_str)))
465 result = self._run_module(ome_default_args)
466 assert result['failed'] is True
467 assert 'msg' in result