Debian Codegraph @

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

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 #
4 # Dell OpenManage Ansible Modules
5 # Version 6.2.0
6 # Copyright (C) 2018-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 import json
17 from ansible_collections.dellemc.openmanage.plugins.modules import idrac_bios
18 from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule
19 from unittest.mock import MagicMock
20 from io import StringIO
21 from ansible.module_utils._text import to_text
22 from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError
23 from ansible.module_utils.urls import ConnectionError, SSLValidationError
24
25 MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.idrac_bios.'
26
27 BIOS_JOB_RUNNING = "BIOS Config job is running. Wait for the job to complete."
28 NO_CHANGES_MSG = "No changes found to be applied."
29 CHANGES_MSG = "Changes found to be applied."
30 SUCCESS_CLEAR = "Successfully cleared the pending BIOS attributes."
31 SUCCESS_COMPLETE = "Successfully applied the BIOS attributes update."
32 SCHEDULED_SUCCESS = "Successfully scheduled the job for the BIOS attributes update."
33 COMMITTED_SUCCESS = "Successfully committed changes. The job is in pending state. The changes will be applied {0}"
34 RESET_TRIGGERRED = "Reset BIOS action triggered successfully."
35 HOST_RESTART_FAILED = "Unable to restart the host. Check the host status and restart the host manually."
36 BIOS_RESET_TRIGGERED = "The BIOS reset action has been triggered successfully. The host reboot is complete."
37 BIOS_RESET_COMPLETE = "BIOS reset to defaults has been completed successfully."
38 BIOS_RESET_PENDING = "Pending attributes to be applied. " \
39 "Clear or apply the pending changes before resetting the BIOS."
40 FORCE_BIOS_DELETE = "The BIOS configuration job is scheduled. Use 'force' to delete the job."
41 INVALID_ATTRIBUTES_MSG = "The values specified for the attributes are invalid."
42 UNSUPPORTED_APPLY_TIME = "Apply time {0} is not supported."
43 MAINTENANCE_OFFSET = "The maintenance time must be post-fixed with local offset to {0}."
44 MAINTENANCE_TIME = "The specified maintenance time window occurs in the past, " \
45 "provide a future time to schedule the maintenance window."
46
47
48 @pytest.fixture
49 def idrac_redfish_mock_for_bios(mocker, ome_response_mock):
50 connection_class_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI')
51 ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value
52 ome_connection_mock_obj.invoke_request.return_value = ome_response_mock
53 return ome_connection_mock_obj
54
55
56 class TestConfigBios(FakeAnsibleModule):
57 module = idrac_bios
58
59 @pytest.fixture
60 def idrac_configure_bios_mock(self):
61 omsdk_mock = MagicMock()
62 idrac_obj = MagicMock()
63 omsdk_mock.config_mgr = idrac_obj
64 return idrac_obj
65
66 @pytest.fixture
67 def idrac_connection_configure_bios_mock(self, mocker, idrac_configure_bios_mock):
68 idrac_conn_class_mock = mocker.patch(MODULE_PATH + 'iDRACConnection',
69 return_value=idrac_configure_bios_mock)
70 idrac_conn_class_mock.return_value.__enter__.return_value = idrac_configure_bios_mock
71 return idrac_configure_bios_mock
72
73 @pytest.mark.parametrize("params", [
74 {"json_data": {"Attributes": {}}, 'message': NO_CHANGES_MSG,
75 "success": True, 'mparams': {'clear_pending': True}},
76 {"json_data": {"Attributes": {}}, 'message': NO_CHANGES_MSG,
77 "success": True, 'mparams': {'clear_pending': True}, "check_mode": True},
78 {"json_data": {"Attributes": {"test": "value"}}, 'message': BIOS_JOB_RUNNING,
79 "success": True, 'mparams': {'clear_pending': True},
80 "check_scheduled_bios_job": ("job1", "Running")},
81 {"json_data": {"Attributes": {"test": "value"}}, 'message': BIOS_JOB_RUNNING,
82 "success": True, 'mparams': {'clear_pending': True},
83 "check_scheduled_bios_job": ("job1", "Starting")},
84 {"json_data": {"Attributes": {"test": "value"}}, 'message': SUCCESS_CLEAR,
85 "success": True, 'mparams': {'clear_pending': True},
86 "check_scheduled_bios_job": ("job1", "Scheduled")},
87 {"json_data": {"Attributes": {"test": "value"}}, 'message': CHANGES_MSG,
88 "success": True, 'mparams': {'clear_pending': True},
89 "check_scheduled_bios_job": ("job1", "Scheduled"), "check_mode": True},
90 {"json_data": {"Attributes": {"test": "value"}}, 'message': CHANGES_MSG,
91 "success": True, 'mparams': {'clear_pending': True},
92 "check_scheduled_bios_job": ("job1", "Scheduler"), "check_mode": True},
93 {"json_data": {"Attributes": {"test": "value"}}, 'message': SUCCESS_CLEAR,
94 "success": True, 'mparams': {'clear_pending': True},
95 "check_scheduled_bios_job": (None, "Scheduled")},
96 {"json_data": {"Attributes": {"test": "value"}}, 'message': CHANGES_MSG,
97 "success": True, 'mparams': {'clear_pending': True},
98 "check_scheduled_bios_job": (None, "Scheduled"), "check_mode": True},
99 {"json_data": {"Attributes": {"test": "value"},
100 "Members": [
101 {"Id": "job_1", "JobType": "RAIDConfiguration", "JobState": "Scheduled"},
102 {"Id": "job_1", "JobType": "BIOSConfiguration", "JobState": "Scheduled"}]},
103 'message': SUCCESS_CLEAR,
104 "success": True, 'mparams': {'clear_pending': True}},
105 {"json_data": {"Attributes": {"test": "value"},
106 "Members": [{"Id": "job_1", "JobType": "BIOSConfiguration", "JobState": "Running"}]},
107 'message': BIOS_JOB_RUNNING,
108 "success": True, 'mparams': {'clear_pending': True}},
109 {"json_data": {"Attributes": {"test": "value"},
110 "Members": [{"Id": "job_1", "JobType": "BIOSConfiguration", "JobState": "Starting"}]},
111 'message': BIOS_JOB_RUNNING,
112 "success": True, 'mparams': {'clear_pending': True}},
113 ])
114 def test_idrac_bios_clear_pending(self, params, idrac_redfish_mock_for_bios, ome_response_mock, idrac_default_args,
115 mocker):
116 ome_response_mock.success = params.get("success", True)
117 ome_response_mock.json_data = params.get('json_data')
118 mocks = ["get_pending_attributes", "check_scheduled_bios_job", "delete_scheduled_bios_job"]
119 for m in mocks:
120 if m in params:
121 mocker.patch(MODULE_PATH + m, return_value=params.get(m, {}))
122 idrac_default_args.update(params['mparams'])
123 result = self._run_module(idrac_default_args, check_mode=params.get('check_mode', False))
124 assert result['status_msg'] == params['message']
125
126 @pytest.mark.parametrize("params", [
127 {"json_data": {"Attributes": {}}, 'message': BIOS_RESET_TRIGGERED,
128 "reset_host": True,
129 "success": True, 'mparams': {'reset_bios': True}},
130 {"json_data": {"Attributes": {"BootMode": "Uefi"}}, 'message': BIOS_RESET_PENDING,
131 "reset_host": True,
132 "success": True, 'mparams': {'reset_bios': True}},
133 {"json_data": {"DateTime": "2022-09-14T05:59:35-05:00",
134 "DateTimeLocalOffset": "-05:00",
135 "Members": [{"Created": "2022-09-14T05:59:20-05:00", "MessageId": "SYS1003"},
136 {"Created": "2022-09-14T05:59:10-05:00", "MessageId": "UEFI0157"},
137 {"Created": "2022-09-14T05:59:30-05:00", "MessageId": "SYS1002"}],
138 "Entries": {
139 "@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/LogServices/Lclog/Entries"
140 },
141 "Attributes": {}},
142 'message': BIOS_RESET_TRIGGERED, "reset_host": True,
143 "success": True, 'mparams': {'reset_bios': True}},
144 {"json_data": {"DateTime": "2022-09-14T05:59:35-05:00",
145 "DateTimeLocalOffset": "-05:00",
146 "Members": [{"Created": "2022-09-14T05:59:20-05:00", "MessageId": "SYS1003"},
147 {"Created": "2022-09-14T05:59:10-05:00", "MessageId": "UEFI0157"},
148 {"Created": "2022-09-14T05:59:40-05:00", "MessageId": "SYS1002"}],
149 "Entries": {
150 "@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/LogServices/Lclog/Entries"
151 },
152 "Attributes": {}},
153 'message': BIOS_RESET_COMPLETE, "reset_host": True,
154 "success": True, 'mparams': {'reset_bios': True}},
155 {"json_data": {"Attributes": {}}, 'message': CHANGES_MSG,
156 "reset_host": True, "check_mode": True,
157 "success": True, 'mparams': {'reset_bios': True}},
158 {"json_data": {"PowerState": "On"}, 'message': BIOS_RESET_TRIGGERED,
159 "success": True, 'mparams': {'reset_bios': True, "reset_type": "force_restart"}},
160 {"json_data": {"PowerState": "Off"}, 'message': "{0} {1}".format(RESET_TRIGGERRED, HOST_RESTART_FAILED),
161 "success": True, 'mparams': {'reset_bios': True}},
162 {"json_data": {"PowerState": "On"}, 'message': HOST_RESTART_FAILED,
163 "get_power_state": "On", "power_act_host": False,
164 "success": True, 'mparams': {'reset_bios': True}},
165 {"json_data": {"PowerState": "On"}, 'message': HOST_RESTART_FAILED,
166 "get_power_state": "Off", "power_act_host": False,
167 "success": True, 'mparams': {'reset_bios': True}},
168 ])
169 def test_idrac_bios_reset_bios(self, params, idrac_redfish_mock_for_bios, ome_response_mock, idrac_default_args,
170 mocker):
171 ome_response_mock.success = params.get("success", True)
172 ome_response_mock.json_data = params.get('json_data')
173 mocks = ["reset_host", "get_power_state", "track_power_state", "power_act_host"]
174 for m in mocks:
175 if m in params:
176 mocker.patch(MODULE_PATH + m, return_value=params.get(m, {}))
177 mocker.patch("ansible_collections.dellemc.openmanage.plugins.module_utils.utils." + 'time.sleep',
178 return_value=None)
179 idrac_default_args.update(params['mparams'])
180 result = self._run_module(idrac_default_args, check_mode=params.get('check_mode', False))
181 assert result['status_msg'] == params['message']
182
183 @pytest.mark.parametrize("params", [
184 {"json_data": {"Attributes": {"NumLock": "On"}}, 'message': NO_CHANGES_MSG,
185 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
186 "success": True, 'mparams': {"attributes": {"NumLock": "On"}}},
187 {"json_data": {"Attributes": {},
188 "RegistryEntries": {
189 "Attributes": [
190 {
191 "AttributeName": "SystemModelName",
192 "ReadOnly": True,
193 "Type": "String"
194 }, {
195 "AttributeName": "MemoryMode",
196 "ReadOnly": False,
197 "Type": "Enumeration",
198 "Value": [
199 {
200 "ValueDisplayName": "Off",
201 "ValueName": "PersistentMemoryOff"
202 },
203 {
204 "ValueDisplayName": "Non-Volatile DIMM",
205 "ValueName": "NVDIMM"
206 }
207 ],
208 }, {
209 "AttributeName": "ValidEnum",
210 "ReadOnly": False,
211 "Type": "Enumeration",
212 "Value": [
213 {
214 "ValueDisplayName": "Enabled",
215 "ValueName": "On"
216 },
217 {
218 "ValueDisplayName": "Disabled",
219 "ValueName": "Off"
220 }
221 ],
222 "WriteOnly": False
223 }, {
224 "AttributeName": "IntSetting",
225 "LowerBound": 0,
226 "ReadOnly": False,
227 "Type": "Integer",
228 "UpperBound": 32,
229 }, {
230 "AttributeName": "IntSetting3",
231 "LowerBound": 0,
232 "ReadOnly": False,
233 "Type": "Integer",
234 "UpperBound": 32,
235 }, {
236 "AttributeName": "IntSetting2",
237 "LowerBound": 0,
238 "ReadOnly": False,
239 "Type": "Integer",
240 "UpperBound": 32,
241 }, ]}},
242 'message': 'Job is in progress.',
243 "reset_host": True,
244 "get_pending_attributes": {},
245 "idrac_redfish_job_tracking": (True, 'Job is in progress.', {}, 10),
246 "success": True,
247 'mparams': {"attributes": {"NumLock": "On", "SystemModelName": "new name", "MemoryMode": "DRAM",
248 "IntSetting": 33, "IntSetting2": 'zero', "IntSetting3": 25,
249 "ValidEnum": "On"}}},
250 {"json_data": {"Attributes": {"NumLock": "On"}}, 'message': CHANGES_MSG,
251 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
252 "check_mode": True,
253 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
254 {"json_data": {
255 "Attributes": {"NumLock": "On"},
256 "@Redfish.Settings": {
257 "SupportedApplyTimes": ["OnReset", "InMaintenanceWindowOnReset"]}},
258 'message': UNSUPPORTED_APPLY_TIME.format('AtMaintenanceWindowStart'),
259 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
260 "success": True, 'mparams': {"attributes": {"NumLock": "Off"},
261 "apply_time": 'AtMaintenanceWindowStart',
262 "maintenance_window": {"start_time": '"2022-09-30T05:15:40-05:00"',
263 "duration": 600}}},
264 {"json_data": {"DateTime": "2022-09-14T05:59:35-05:00",
265 "DateTimeLocalOffset": "-05:00",
266 "Attributes": {"NumLock": "On"},
267 "@Redfish.Settings": {
268 "SupportedApplyTimes": ["OnReset", 'AtMaintenanceWindowStart',
269 "InMaintenanceWindowOnReset"]}},
270 'message': MAINTENANCE_OFFSET.format('-05:00'),
271 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
272 "success": True, 'mparams': {"attributes": {"NumLock": "Off"},
273 "apply_time": 'AtMaintenanceWindowStart',
274 "maintenance_window": {"start_time": '"2022-09-30T05:15:40-00:00"',
275 "duration": 600}}},
276 {"json_data": {"DateTime": '2022-09-30T05:15:41-05:00',
277 "DateTimeLocalOffset": "-05:00",
278 "Attributes": {"NumLock": "On"},
279 "@Redfish.Settings": {
280 "SupportedApplyTimes": ["OnReset", 'AtMaintenanceWindowStart',
281 "InMaintenanceWindowOnReset"]}},
282 'message': MAINTENANCE_TIME,
283 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
284 "success": True, 'mparams': {"attributes": {"NumLock": "Off"},
285 "apply_time": 'AtMaintenanceWindowStart',
286 "maintenance_window": {"start_time": '2022-09-30T05:15:40-05:00',
287 "duration": 600}}},
288 {"json_data": {"DateTime": '2022-09-30T05:15:39-05:00',
289 "DateTimeLocalOffset": "-05:00",
290 "Attributes": {"NumLock": "On"},
291 "@Redfish.Settings": {
292 "SupportedApplyTimes": ["OnReset", 'AtMaintenanceWindowStart',
293 "InMaintenanceWindowOnReset"]}},
294 'message': COMMITTED_SUCCESS.format('AtMaintenanceWindowStart'),
295 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
296 "success": True, 'mparams': {"attributes": {"NumLock": "Off"},
297 "apply_time": 'AtMaintenanceWindowStart',
298 "maintenance_window": {"start_time": '2022-09-30T05:15:40-05:00',
299 "duration": 600}}},
300 {"json_data": {"DateTime": '2022-09-30T05:15:39-05:00',
301 "DateTimeLocalOffset": "-05:00",
302 "Attributes": {"NumLock": "On"},
303 "@Redfish.Settings": {
304 "SupportedApplyTimes": []}},
305 'message': SCHEDULED_SUCCESS,
306 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
307 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}, 'job_wait': False}},
308 {"json_data": {"DateTime": '2022-09-30T05:15:39-05:00',
309 "DateTimeLocalOffset": "-05:00",
310 "Attributes": {"NumLock": "On"},
311 "@Redfish.Settings": {
312 "SupportedApplyTimes": ["OnReset", 'AtMaintenanceWindowStart',
313 "InMaintenanceWindowOnReset"]}},
314 'message': SCHEDULED_SUCCESS,
315 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
316 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}, 'job_wait': False}},
317 {"json_data": {"DateTime": '2022-09-30T05:15:39-05:00',
318 "DateTimeLocalOffset": "-05:00",
319 "Attributes": {"NumLock": "On"},
320 "@Redfish.Settings": {
321 "SupportedApplyTimes": ["OnReset", 'AtMaintenanceWindowStart',
322 "InMaintenanceWindowOnReset"]}},
323 'message': COMMITTED_SUCCESS.format('OnReset'),
324 "reset_host": True, "get_pending_attributes": {}, "validate_vs_registry": {},
325 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}, 'apply_time': 'OnReset'}},
326 {"json_data": {
327 "Attributes": {"NumLock": "On"},
328 "@Redfish.Settings": {
329 "SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
330 'message': BIOS_JOB_RUNNING,
331 "reset_host": True, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
332 "check_scheduled_bios_job": ("job1", "Running"),
333 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
334 {"json_data": {
335 "Attributes": {"NumLock": "On"},
336 "@Redfish.Settings": {
337 "SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
338 'message': "Attributes committed but reboot has failed {0}".format(HOST_RESTART_FAILED),
339 "reset_host": False, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
340 "check_scheduled_bios_job": ("job1", "Scheduled"), "apply_attributes": ("job1", True),
341 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
342 {"json_data": {
343 "Attributes": {"NumLock": "On"},
344 "@Redfish.Settings":
345 {"SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
346 'message': "Job Tracking Failed",
347 "reset_host": True, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
348 "check_scheduled_bios_job": ("job1", "Scheduled"), "apply_attributes": ("job1", True),
349 "idrac_redfish_job_tracking": (True, "Job Tracking Failed", {}, 10),
350 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
351 {"json_data": {
352 "Attributes": {"NumLock": "On"},
353 "@Redfish.Settings": {
354 "SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
355 'message': SUCCESS_COMPLETE,
356 "reset_host": True, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
357 "check_scheduled_bios_job": ("job1", "Scheduled"), "apply_attributes": ("job1", True),
358 "idrac_redfish_job_tracking": (False, "Job Tracking Failed", {}, 10),
359 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
360 {"json_data": {
361 "Attributes": {"NumLock": "On"},
362 "@Redfish.Settings": {
363 "SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
364 'message': SCHEDULED_SUCCESS,
365 "reset_host": True, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
366 "check_scheduled_bios_job": ("job1", "Scheduled"), "apply_attributes": ("job1", True),
367 "idrac_redfish_job_tracking": (False, "Job Tracking Failed", {}, 10),
368 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}, "job_wait": False}},
369 {"json_data": {
370 "Attributes": {"NumLock": "On"},
371 "@Redfish.Settings": {
372 "SupportedApplyTimes": ["OnReset", "AtMaintenanceWindowStart", "InMaintenanceWindowOnReset"]}},
373 'message': COMMITTED_SUCCESS.format("Immediate"),
374 "reset_host": False, "get_pending_attributes": {"AssetTag": 'test'}, "validate_vs_registry": {},
375 "check_scheduled_bios_job": ("job1", "Scheduled"), "apply_attributes": (None, True),
376 "success": True, 'mparams': {"attributes": {"NumLock": "Off"}}},
377 ])
378 def test_idrac_bios_attributes(self, params, idrac_redfish_mock_for_bios, ome_response_mock, idrac_default_args,
379 mocker):
380 ome_response_mock.success = params.get("success", True)
381 ome_response_mock.json_data = params.get('json_data')
382 ome_response_mock.headers = {'Location': 'job1'}
383 mocks = ["get_current_attributes", "get_attributes_registry", "get_pending_attributes",
384 "check_scheduled_bios_job", "apply_attributes", "idrac_redfish_job_tracking",
385 "reset_host", "get_power_state", "track_power_state", "power_act_host"]
386 for m in mocks:
387 if m in params:
388 mocker.patch(MODULE_PATH + m, return_value=params.get(m, {}))
389 mocker.patch("ansible_collections.dellemc.openmanage.plugins.module_utils.utils." + 'time.sleep',
390 return_value=None)
391 idrac_default_args.update(params['mparams'])
392 result = self._run_module(idrac_default_args, check_mode=params.get('check_mode', False))
393 assert result['status_msg'] == params['message']
394
395 @pytest.mark.parametrize("exc_type", [RuntimeError, SSLValidationError, ConnectionError, KeyError,
396 ImportError, ValueError, TypeError, HTTPError])
397 def test_main_idrac_config_bios_exception_handling_case(self, exc_type, mocker,
398 idrac_connection_configure_bios_mock,
399 idrac_default_args):
400 idrac_default_args.update({"share_name": "sharename", "boot_sources": 1})
401 json_str = to_text(json.dumps({"data": "out"}))
402 if exc_type not in [HTTPError, SSLValidationError]:
403 mocker.patch(MODULE_PATH + 'run_server_bios_config',
404 side_effect=exc_type('test'))
405 else:
406 mocker.patch(
407 MODULE_PATH + 'run_server_bios_config',
408 side_effect=exc_type('https://testhost.com', 400, 'http error message',
409 {"accept-type": "application/json"}, StringIO(json_str)))
410 if not exc_type == URLError:
411 result = self._run_module(idrac_default_args)
412 assert result['failed'] is True
413 else:
414 result = self._run_module(idrac_default_args)
415 assert 'msg' in result
416
417 def test_run_idrac_bios_config_success_case01(self, idrac_connection_configure_bios_mock,
418 idrac_default_args, mocker):
419 idrac_default_args.update({"boot_sources": "bootsources"})
420 message = {"changes_applicable": True, "message": "changes are applicable"}
421 mocker.patch(MODULE_PATH +
422 '_validate_params', return_value=(False, "message of validate params"))
423 idrac_connection_configure_bios_mock.config_mgr.is_change_applicabl.return_value = message
424 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
425 f_module = self.get_module_mock(params=idrac_default_args)
426 f_module.check_mode = True
427 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
428 assert msg == {'changes_applicable': True, 'message': 'changes are applicable'}
429
430 def test_run_idrac_bios_config_success_case02(self, idrac_connection_configure_bios_mock, idrac_default_args,
431 mocker):
432 idrac_default_args.update({"boot_sources": "bootsources"})
433 message = {"changes_applicable": True, "Status": "Success", "message": "changes found to commit!"}
434 mocker.patch(MODULE_PATH +
435 '_validate_params', return_value=(False, "message of validate params"))
436 idrac_connection_configure_bios_mock.config_mgr.is_change_applicabl.return_value = message
437 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
438 f_module = self.get_module_mock(params=idrac_default_args)
439 f_module.check_mode = False
440 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
441 assert msg == {'Status': 'Success',
442 'changes_applicable': True,
443 'message': 'changes found to commit!'}
444
445 def test_run_idrac_bios_config_success_case03(self, idrac_connection_configure_bios_mock, idrac_default_args,
446 mocker):
447 idrac_default_args.update({"boot_sources": "bootsources"})
448 message = {"changes_applicable": False, "Status": "Success", "Message": "No changes found to commit!"}
449 mocker.patch(MODULE_PATH +
450 '_validate_params', return_value=(False, "message of validate params"))
451 idrac_connection_configure_bios_mock.config_mgr.is_change_applicabl.return_value = message
452 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
453 f_module = self.get_module_mock(params=idrac_default_args)
454 f_module.check_mode = False
455 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
456 assert msg == {'Message': 'No changes found to commit!',
457 'Status': 'Success',
458 'changes_applicable': False}
459
460 def test_run_idrac_bios_config_success_case04(self, idrac_connection_configure_bios_mock, idrac_default_args,
461 mocker):
462 idrac_default_args.update({"boot_sources": "bootsources"})
463 message = {"changes_applicable": False, "Status": "Success", "Message": "No changes found to apply."}
464 mocker.patch(MODULE_PATH +
465 '_validate_params', return_value=(False, "message of validate params"))
466 idrac_connection_configure_bios_mock.config_mgr.is_change_applicabl.return_value = message
467 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
468 f_module = self.get_module_mock(params=idrac_default_args)
469 f_module.check_mode = False
470 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
471 assert msg == {'Message': 'No changes found to apply.',
472 'Status': 'Success',
473 'changes_applicable': False}
474
475 def test_run_idrac_bios_config_bootmode_failed_case0(self, idrac_connection_configure_bios_mock,
476 idrac_default_args,
477 mocker):
478 idrac_default_args.update({"boot_sources": "bootsources"})
479 message = {"changes_applicable": False, "Status": "failed", "Message": "No changes found to apply."}
480 mocker.patch(MODULE_PATH +
481 '_validate_params', return_value=(False, "message of validate params"))
482 idrac_connection_configure_bios_mock.config_mgr.is_change_applicabl.return_value = message
483 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
484 f_module = self.get_module_mock(params=idrac_default_args)
485 f_module.check_mode = False
486 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
487 assert msg == {'Message': 'No changes found to apply.',
488 'Status': 'failed',
489 'changes_applicable': False}
490
491 def test_run_idrac_bios_config_errorhandle_failed_case0(self, idrac_connection_configure_bios_mock,
492 idrac_default_args,
493 mocker):
494 idrac_default_args.update({"boot_sources": "bootsources"})
495 mocker.patch(MODULE_PATH +
496 '_validate_params', return_value=(True, "Error occurs"))
497 f_module = self.get_module_mock(params=idrac_default_args)
498 f_module.check_mode = False
499 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
500 assert msg == idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources()
501
502 def test_run_idrac_bios_config_status_failed_case01(self, idrac_connection_configure_bios_mock, idrac_default_args,
503 mocker):
504 idrac_default_args.update({"boot_sources": "bootsources"})
505 message = {'Status': 'Failed', 'Message': 'message of validate params'}
506 mocker.patch(MODULE_PATH +
507 '_validate_params', return_value=(True, "Error occurs"))
508 idrac_connection_configure_bios_mock.config_mgr.set_liason_share.return_value = message
509 f_module = self.get_module_mock(params=idrac_default_args)
510 f_module.check_mode = False
511 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
512 assert msg == idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources()
513
514 def test_run_idrac_bios_config_status_success_case01(self, idrac_connection_configure_bios_mock, idrac_default_args,
515 mocker):
516 idrac_default_args.update({"boot_sources": "bootsources",
517 "attributes": {"boot_mode": "BootMode", "nvme_mode": "NvmeMode"}})
518 message = {'Status': 'Successs', 'Message': 'message of validate params'}
519 mocker.patch(MODULE_PATH +
520 '_validate_params', return_value=(False, "Error did not occurs"))
521 idrac_connection_configure_bios_mock.config_mgr.configure_bios.return_value = message
522 idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources.return_value = message
523 f_module = self.get_module_mock(params=idrac_default_args)
524 f_module.check_mode = False
525 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
526 assert msg == {'Message': 'message of validate params', 'Status': 'Successs'}
527
528 def test_run_bios_config_status_boot_sources_failed_case(self, idrac_connection_configure_bios_mock, mocker,
529 idrac_default_args):
530 idrac_default_args.update({"boot_sources": "bootsources"})
531 message = {'Status': 'Failed', "Data": {'Message': 'message of validate params'}}
532 idrac_connection_configure_bios_mock.config_mgr.set_liason_share.return_value = message
533 f_module = self.get_module_mock(params=idrac_default_args)
534 msg = self.module.run_server_bios_config(idrac_connection_configure_bios_mock, f_module)
535 assert msg == idrac_connection_configure_bios_mock.config_mgr.configure_boot_sources()
536
537 def test__validate_params_error_keys_case(self, idrac_connection_configure_bios_mock, idrac_default_args,
538 mocker):
539 idrac_default_args.update({})
540 attr = [{"name": "Name"}, {"index": "Index"}, {"enabled": "Enabled"}]
541 msg = self.module._validate_params(attr)
542 assert msg == "attribute keys must be one of the ['Name', 'Index', 'Enabled']."
543
544 def test__validate_params_check_params_case(self, idrac_connection_configure_bios_mock, mocker,
545 idrac_default_args):
546 mocker.patch(MODULE_PATH +
547 'check_params', return_value=(True, "Error occurs in check params"))
548 attr = [{"name": "name1"}, {"Index": "index1"}]
549 msg = self.module._validate_params(attr)
550 assert msg == "attribute keys must be one of the ['Name', 'Index', 'Enabled']."
551
552 def test__validate_params_empty_params_case(self, idrac_connection_configure_bios_mock, mocker,
553 idrac_default_args):
554 mocker.patch(MODULE_PATH +
555 '_validate_name_index_duplication', return_value=(True, "Error occurs in "
556 "validate name"))
557 msg = self.module._validate_params([])
558 assert msg == (True, 'Error occurs in validate name')
559
560 def test__validate_name_index_duplication_error_true_case(self, idrac_connection_configure_bios_mock,
561 idrac_default_args):
562 result = self.module._validate_name_index_duplication([{"Name": "Name1"}, {"Name": "Name1"}])
563 assert result == 'duplicate name Name1'
564
565 def test__validate_name_index_duplication_error_false_case(self, idrac_connection_configure_bios_mock,
566 idrac_default_args):
567 result = self.module._validate_name_index_duplication([{"Name": "Name1"}, {"Name": "Name2"}])
568 assert result == ''
569
570 def test_check_params_false_case(self, idrac_connection_configure_bios_mock, idrac_default_args):
571 result = self.module.check_params({"required": False}, [{"name": "Name1", "required": False},
572 {"name": "Name2", "required": False}])
573 assert result == ''
574
575 @pytest.mark.parametrize("params", [
576 {"each": {"Name": 1}, 'message': "Name must be of type: <class 'str'>. 1 (<class 'int'>) provided."},
577 {"each": {"Index": "one"}, 'message': "Index must be of type: <class 'int'>. one (<class 'str'>) provided."},
578 {"each": {"Index": -1}, 'message': "Index must be greater than or equal to: 0"},
579 {"each": {"Name": 'test', "Index": 1}, 'message': ""},
580 {"each": {"Enabled": "one"}, 'message': "Enabled must be of type: <class 'bool'>. one (<class 'str'>) provided."},
581 ])
582 def test_check_params_required_true_case(self, idrac_connection_configure_bios_mock, params,
583 idrac_default_args):
584 fields = [
585 {"name": "Name", "type": str, "required": True},
586 {"name": "Index", "type": int, "required": False, "min": 0},
587 {"name": "Enabled", "type": bool, "required": False}
588 ]
589 result = self.module.check_params(params.get('each'), fields)
590 assert result == params.get('message')
591
592 def test_validate_negative_job_time_out(self, idrac_default_args):
593 idrac_default_args.update({"job_wait": True, "job_wait_timeout": -5})
594 f_module = self.get_module_mock(params=idrac_default_args)
595 with pytest.raises(Exception) as ex:
596 self.module.validate_negative_job_time_out(f_module)
597 assert ex.value.args[0] == "The parameter job_wait_timeout value cannot be negative or zero."