| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 redfish_firmware_rollback |
| 18 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 19 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 20 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 21 |
from io import StringIO |
| 22 |
from unittest.mock import MagicMock |
| 23 |
from ansible.module_utils._text import to_text |
| 24 |
|
| 25 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 26 |
ACCESS_TYPE = "application/json" |
| 27 |
HTTP_ERROR_MSG = 'http error message' |
| 28 |
HTTPS_ADDRESS = 'https://testhost.com' |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def redfish_connection_mock(mocker, redfish_response_mock): |
| 33 |
connection_class_mock = mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.Redfish') |
| 34 |
redfish_connection_obj = connection_class_mock.return_value.__enter__.return_value |
| 35 |
redfish_connection_obj.invoke_request.return_value = redfish_response_mock |
| 36 |
return redfish_connection_obj |
| 37 |
|
| 38 |
|
| 39 |
class TestRedfishFirmware(FakeAnsibleModule): |
| 40 |
|
| 41 |
module = redfish_firmware_rollback |
| 42 |
|
| 43 |
@pytest.mark.parametrize("exc_type", [URLError, HTTPError, TypeError]) |
| 44 |
@pytest.mark.parametrize("generation", [16, 17]) |
| 45 |
def test_wait_for_redfish_idrac_reset_http(self, exc_type, redfish_connection_mock, redfish_response_mock, |
| 46 |
redfish_default_args, mocker, generation): |
| 47 |
redfish_default_args.update({"name": "BIOS", "reboot": True, "reboot_timeout": 900}) |
| 48 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 49 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.time.sleep', return_value=None) |
| 50 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.Redfish', return_value=MagicMock()) |
| 51 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.require_session', return_value=(1, "secret token")) |
| 52 |
json_str = to_text(json.dumps({"data": "out"})) |
| 53 |
if exc_type == HTTPError: |
| 54 |
redfish_connection_mock.invoke_request.side_effect = exc_type( |
| 55 |
HTTPS_ADDRESS, 401, HTTP_ERROR_MSG, {"accept-type": ACCESS_TYPE}, |
| 56 |
StringIO(json_str) |
| 57 |
) |
| 58 |
result = self.module.wait_for_redfish_idrac_reset(f_module, redfish_connection_mock, 5, generation) |
| 59 |
assert result[0] is False |
| 60 |
assert result[1] is True |
| 61 |
redfish_connection_mock.invoke_request.side_effect = exc_type( |
| 62 |
HTTPS_ADDRESS, 400, HTTP_ERROR_MSG, {"accept-type": ACCESS_TYPE}, |
| 63 |
StringIO(json_str) |
| 64 |
) |
| 65 |
result = self.module.wait_for_redfish_idrac_reset(f_module, redfish_connection_mock, 5, generation) |
| 66 |
assert result[0] is True |
| 67 |
assert result[1] is True |
| 68 |
elif exc_type == URLError: |
| 69 |
redfish_connection_mock.invoke_request.side_effect = exc_type("exception message") |
| 70 |
result = self.module.wait_for_redfish_idrac_reset(f_module, redfish_connection_mock, 5, generation) |
| 71 |
assert result[0] is True |
| 72 |
assert result[1] is True |
| 73 |
else: |
| 74 |
redfish_connection_mock.invoke_request.side_effect = exc_type("generic exception message") |
| 75 |
result = self.module.wait_for_redfish_idrac_reset(f_module, redfish_connection_mock, 5, generation) |
| 76 |
assert result[0] is True |
| 77 |
assert result[1] is True |
| 78 |
|
| 79 |
@pytest.mark.parametrize("generation", [16, 17]) |
| 80 |
def test_wait_for_redfish_idrac_reset(self, redfish_connection_mock, redfish_response_mock, |
| 81 |
redfish_default_args, mocker, generation): |
| 82 |
redfish_default_args.update({"name": "BIOS", "reboot": True, "reboot_timeout": 900}) |
| 83 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 84 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.time.sleep', return_value=None) |
| 85 |
result = self.module.wait_for_redfish_idrac_reset(f_module, redfish_connection_mock, 900, generation) |
| 86 |
assert result[0] is False |
| 87 |
assert result[1] is False |
| 88 |
|
| 89 |
def test_rollback_firmware(self, redfish_connection_mock, redfish_response_mock, redfish_default_args, mocker): |
| 90 |
redfish_default_args.update({"name": "BIOS", "reboot": True, "reboot_timeout": 900}) |
| 91 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.simple_update", return_value=["JID_12345678"]) |
| 92 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_reboot_job", |
| 93 |
return_value=({"Id": "JID_123456789"}, True, "")) |
| 94 |
job_resp_mock = MagicMock() |
| 95 |
job_resp_mock.json_data = {"JobState": "RebootFailed"} |
| 96 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_job_complete", |
| 97 |
return_value=(job_resp_mock, "")) |
| 98 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 99 |
preview_uri = ["/redfish/v1/Previous1.1"] |
| 100 |
reboot_uri = ["/redfish/v1/Previous.life_cycle.1.1"] |
| 101 |
network_uri = ["/redfish/v1/Previous.NetworkProtocol1.1"] |
| 102 |
update_uri = "/redfish/v1/SimpleUpdate" |
| 103 |
with pytest.raises(Exception) as ex: |
| 104 |
self.module.rollback_firmware(redfish_connection_mock, f_module, preview_uri, reboot_uri, update_uri, network_uri, []) |
| 105 |
assert ex.value.args[0] == "Failed to reboot the server." |
| 106 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_job_complete", |
| 107 |
return_value=(job_resp_mock, "Failed message.")) |
| 108 |
with pytest.raises(Exception) as ex: |
| 109 |
self.module.rollback_firmware(redfish_connection_mock, f_module, preview_uri, reboot_uri, update_uri, network_uri, []) |
| 110 |
assert ex.value.args[0] == "Task excited after waiting for 900 seconds. " \ |
| 111 |
"Check console for firmware rollback status." |
| 112 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_reboot_job", |
| 113 |
return_value=({}, False, "Reset operation is failed.")) |
| 114 |
with pytest.raises(Exception) as ex: |
| 115 |
self.module.rollback_firmware(redfish_connection_mock, f_module, preview_uri, reboot_uri, update_uri, network_uri, []) |
| 116 |
assert ex.value.args[0] == "Reset operation is failed." |
| 117 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.get_job_status", |
| 118 |
return_value=({"JobState": "Completed"}, False)) |
| 119 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_reboot_job", |
| 120 |
return_value=({"JobState": "Completed", "Id": "JID_123456789"}, True, "")) |
| 121 |
job_resp_mock.json_data = {"JobState": "RebootCompleted"} |
| 122 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_job_complete", |
| 123 |
return_value=(job_resp_mock, "")) |
| 124 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.simple_update", return_value=["JID_12345678"]) |
| 125 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.wait_for_redfish_idrac_reset", |
| 126 |
return_value=(False, True)) |
| 127 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.get_job_status", |
| 128 |
return_value=([{"JobState": "Completed"}], 0)) |
| 129 |
result = self.module.rollback_firmware(redfish_connection_mock, f_module, preview_uri, reboot_uri, update_uri, network_uri, []) |
| 130 |
assert result[0] == [{'JobState': 'Completed'}, {'JobState': 'Completed'}] |
| 131 |
assert result[1] == 0 |
| 132 |
|
| 133 |
redfish_default_args.update({"name": "BIOS", "reboot": False, "reboot_timeout": 900}) |
| 134 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 135 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.get_job_status", |
| 136 |
return_value=([{"JobState": "Scheduled"}], 0)) |
| 137 |
result = self.module.rollback_firmware(redfish_connection_mock, f_module, preview_uri, [], update_uri, [], []) |
| 138 |
assert result[0] == [{"JobState": "Scheduled"}] |
| 139 |
assert result[1] == 0 |
| 140 |
|
| 141 |
def test_main(self, redfish_connection_mock, redfish_response_mock, redfish_default_args, mocker): |
| 142 |
redfish_default_args.update({"reboot": True, "name": "BIOS"}) |
| 143 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.get_rollback_preview_target", |
| 144 |
return_value=(["Previous/URI/1"], [], "/redfish/SimpleUpdate", [], [])) |
| 145 |
job_status = {"ActualRunningStartTime": "2023-08-07T05:09:08", "ActualRunningStopTime": "2023-08-07T05:12:41", |
| 146 |
"CompletionTime": "2023-08-07T05:12:41", "Description": "Job Instance", "EndTime": "TIME_NA", |
| 147 |
"Id": "JID_914026562845", "JobState": "Completed", "JobType": "FirmwareUpdate", |
| 148 |
"Message": "Job completed successfully.", "MessageArgs": [], "MessageId": "PR19", |
| 149 |
"Name": "Firmware Rollback: Network", "PercentComplete": 100, "StartTime": "2023-08-07T05:04:16", |
| 150 |
"TargetSettingsURI": None} |
| 151 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 0, False)) |
| 152 |
result = self._run_module(redfish_default_args) |
| 153 |
assert result["msg"] == "Successfully completed the job for firmware rollback." |
| 154 |
assert result["job_status"]["JobState"] == "Completed" |
| 155 |
job_status.update({"JobState": "Failed"}) |
| 156 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 1, False)) |
| 157 |
result = self._run_module(redfish_default_args) |
| 158 |
assert result["msg"] == "The job for firmware rollback has been completed with error(s)." |
| 159 |
assert result["job_status"]["JobState"] == "Failed" |
| 160 |
redfish_default_args.update({"reboot": False, "name": "BIOS"}) |
| 161 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 1, False)) |
| 162 |
result = self._run_module(redfish_default_args) |
| 163 |
assert result["msg"] == "The job for firmware rollback has been scheduled with error(s)." |
| 164 |
assert result["job_status"]["JobState"] == "Failed" |
| 165 |
job_status.update({"JobState": "Scheduled"}) |
| 166 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 0, False)) |
| 167 |
result = self._run_module(redfish_default_args) |
| 168 |
assert result["msg"] == "Successfully scheduled the job for firmware rollback." |
| 169 |
assert result["job_status"]["JobState"] == "Scheduled" |
| 170 |
job_status = {} |
| 171 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 0, False)) |
| 172 |
result = self._run_module(redfish_default_args) |
| 173 |
assert result["msg"] == "Failed to complete the job for firmware rollback." |
| 174 |
redfish_default_args.update({"reboot": True, "name": "BIOS", "reboot_timeout": -1}) |
| 175 |
result = self._run_module_with_fail_json(redfish_default_args) |
| 176 |
assert result["msg"] == "The parameter reboot_timeout value cannot be negative or zero." |
| 177 |
redfish_default_args.update({"reboot": False, "name": "BIOS", "reboot_timeout": 900}) |
| 178 |
job_status.update({"JobState": "Completed"}) |
| 179 |
mocker.patch(MODULE_PATH + "redfish_firmware_rollback.rollback_firmware", return_value=(job_status, 0, True)) |
| 180 |
result = self._run_module(redfish_default_args) |
| 181 |
assert result["msg"] == "Successfully completed the job for firmware rollback." |
| 182 |
|
| 183 |
def test_get_rollback_preview_target(self, redfish_connection_mock, redfish_response_mock, redfish_default_args): |
| 184 |
redfish_default_args.update({"username": "user", "password": "pwd", "baseuri": "XX.XX.XX.XX", |
| 185 |
"name": "NETWORK", "reboot_timeout": 3600}) |
| 186 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 187 |
redfish_response_mock.json_data = {"Actions": {"#UpdateService.SimpleUpdate": {}}} |
| 188 |
with pytest.raises(Exception) as ex: |
| 189 |
self.module.get_rollback_preview_target(redfish_connection_mock, f_module) |
| 190 |
assert ex.value.args[0] == "The target firmware version does not support the firmware rollback." |
| 191 |
redfish_response_mock.json_data = { |
| 192 |
"Actions": {"#UpdateService.SimpleUpdate": {"target": "/redfish/v1/SimpleUpdate"}}, |
| 193 |
"FirmwareInventory": {"@odata.id": "/redfish/v1/FirmwareInventory"}, |
| 194 |
"Members": [ |
| 195 |
{"@odata.id": "uri/1", "Id": "Previous.1", "Name": "QLogic.1", "Version": "1.2"}, |
| 196 |
{"@odata.id": "uri/2", "Id": "Previous.2", "Name": "QLogic.2", "Version": "1.2"}, |
| 197 |
{"@odata.id": "uri/3", "Id": "Previous.3", "Name": "QLogic.3", "Version": "1.2"}, |
| 198 |
{"@odata.id": "uri/4", "Id": "Previous.4", "Name": "QLogic.4", "Version": "1.2"}] |
| 199 |
} |
| 200 |
with pytest.raises(Exception) as ex: |
| 201 |
self.module.get_rollback_preview_target(redfish_connection_mock, f_module) |
| 202 |
assert ex.value.args[0] == "No changes found to be applied." |
| 203 |
f_module.check_mode = True |
| 204 |
with pytest.raises(Exception) as ex: |
| 205 |
self.module.get_rollback_preview_target(redfish_connection_mock, f_module) |
| 206 |
assert ex.value.args[0] == "No changes found to be applied." |
| 207 |
redfish_response_mock.json_data["Members"] = [ |
| 208 |
{"@odata.id": "uri/1", "Id": "Previous.1", "Name": "QLogic.1", "Version": "1.2"}, |
| 209 |
{"@odata.id": "uri/2", "Id": "Previous.2", "Name": "QLogic.2", "Version": "1.2"}, |
| 210 |
{"@odata.id": "uri/3", "Id": "Previous.3", "Name": "QLogic.3", "Version": "1.2"}, |
| 211 |
{"@odata.id": "uri/4", "Id": "Previous.4", "Name": "NETWORK", "Version": "1.2"} |
| 212 |
] |
| 213 |
with pytest.raises(Exception) as ex: |
| 214 |
self.module.get_rollback_preview_target(redfish_connection_mock, f_module) |
| 215 |
assert ex.value.args[0] == "Changes found to be applied." |
| 216 |
f_module.check_mode = False |
| 217 |
result = self.module.get_rollback_preview_target(redfish_connection_mock, f_module) |
| 218 |
assert result[0] == ["uri/4"] |
| 219 |
assert result[2] == "/redfish/v1/SimpleUpdate" |
| 220 |
|
| 221 |
def test_get_job_status(self, redfish_connection_mock, redfish_response_mock, redfish_default_args, mocker): |
| 222 |
redfish_default_args.update({"username": "user", "password": "pwd", "baseuri": "XX.XX.XX.XX", "Name": "BIOS", |
| 223 |
"reboot_timeout": 900}) |
| 224 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 225 |
redfish_response_mock.json_data = {"JobState": "Completed", "JobType": "FirmwareUpdate", |
| 226 |
"Name": "Firmware Rollback: Network", "PercentComplete": 100} |
| 227 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.wait_for_redfish_job_complete', |
| 228 |
return_value=(redfish_response_mock, "")) |
| 229 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.strip_substr_dict', |
| 230 |
return_value={"JobState": "Completed", "JobType": "FirmwareUpdate", |
| 231 |
"Name": "Firmware Rollback: Network", "PercentComplete": 100}) |
| 232 |
result = self.module.get_job_status(redfish_connection_mock, f_module, ["JID_123456789"], job_wait=True) |
| 233 |
assert result[0] == [{'JobState': 'Completed', 'JobType': 'FirmwareUpdate', |
| 234 |
'Name': 'Firmware Rollback: Network', 'PercentComplete': 100}] |
| 235 |
assert result[1] == 0 |
| 236 |
redfish_response_mock.json_data = {"JobState": "Failed", "JobType": "FirmwareUpdate", |
| 237 |
"Name": "Firmware Rollback: Network", "PercentComplete": 100} |
| 238 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.wait_for_redfish_job_complete', |
| 239 |
return_value=(redfish_response_mock, "")) |
| 240 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.strip_substr_dict', |
| 241 |
return_value={"JobState": "Failed", "JobType": "FirmwareUpdate", |
| 242 |
"Name": "Firmware Rollback: Network", "PercentComplete": 100}) |
| 243 |
result = self.module.get_job_status(redfish_connection_mock, f_module, ["JID_123456789"], job_wait=True) |
| 244 |
assert result[0] == [{'JobState': 'Failed', 'JobType': 'FirmwareUpdate', |
| 245 |
'Name': 'Firmware Rollback: Network', 'PercentComplete': 100}] |
| 246 |
assert result[1] == 1 |
| 247 |
|
| 248 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.wait_for_redfish_job_complete', |
| 249 |
return_value=(redfish_response_mock, "some error message")) |
| 250 |
with pytest.raises(Exception) as ex: |
| 251 |
self.module.get_job_status(redfish_connection_mock, f_module, ["JID_123456789"], job_wait=True) |
| 252 |
assert ex.value.args[0] == "Task excited after waiting for 900 seconds. Check console for " \ |
| 253 |
"firmware rollback status." |
| 254 |
|
| 255 |
def test_simple_update(self, redfish_connection_mock, redfish_response_mock, redfish_default_args, mocker): |
| 256 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.time.sleep', return_value=None) |
| 257 |
preview_uri, update_uri = ["/uri/1"], ["/uri/SimpleUpdate"] |
| 258 |
redfish_response_mock.headers = {"Location": "/job/JID_123456789"} |
| 259 |
result = self.module.simple_update(redfish_connection_mock, preview_uri, update_uri) |
| 260 |
assert result == ["JID_123456789"] |
| 261 |
|
| 262 |
def test_require_session(self, redfish_connection_mock, redfish_response_mock, redfish_default_args): |
| 263 |
redfish_default_args.update({"username": "user", "password": "pwd", "baseuri": "XX.XX.XX.XX", "Name": "BIOS"}) |
| 264 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 265 |
redfish_response_mock.success = True |
| 266 |
redfish_response_mock.json_data = {"Id": 1} |
| 267 |
redfish_response_mock.headers = {"X-Auth-Token": "token_key"} |
| 268 |
result = self.module.require_session(redfish_connection_mock, f_module) |
| 269 |
assert result[0] == 1 |
| 270 |
assert result[1] == "token_key" |
| 271 |
|
| 272 |
@pytest.mark.parametrize("exc_type", [RuntimeError, URLError, SSLValidationError, ConnectionError, KeyError, |
| 273 |
ImportError, ValueError, TypeError, IOError, AssertionError, OSError]) |
| 274 |
def test_main_rollback_exception_handling_case(self, exc_type, mocker, redfish_default_args, |
| 275 |
redfish_connection_mock, redfish_response_mock): |
| 276 |
redfish_default_args.update({"name": "BIOS"}) |
| 277 |
redfish_response_mock.status_code = 400 |
| 278 |
redfish_response_mock.success = False |
| 279 |
json_str = to_text(json.dumps({"data": "out"})) |
| 280 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 281 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.get_rollback_preview_target', |
| 282 |
side_effect=exc_type('test')) |
| 283 |
else: |
| 284 |
mocker.patch(MODULE_PATH + 'redfish_firmware_rollback.get_rollback_preview_target', |
| 285 |
side_effect=exc_type(HTTPS_ADDRESS, 400, HTTP_ERROR_MSG, |
| 286 |
{"accept-type": ACCESS_TYPE}, StringIO(json_str))) |
| 287 |
if exc_type == HTTPError: |
| 288 |
result = self._run_module(redfish_default_args) |
| 289 |
assert result['failed'] is True |
| 290 |
elif exc_type == URLError: |
| 291 |
result = self._run_module(redfish_default_args) |
| 292 |
assert result['unreachable'] is True |
| 293 |
else: |
| 294 |
result = self._run_module_with_fail_json(redfish_default_args) |
| 295 |
assert result['failed'] is True |
| 296 |
if exc_type == HTTPError: |
| 297 |
assert 'error_info' in result |
| 298 |
assert 'msg' in result |