| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import tarfile |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
import uuid |
| 13 |
from unittest.mock import patch |
| 14 |
|
| 15 |
from ansible.module_utils import basic |
| 16 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 17 |
AnsibleExitJson, |
| 18 |
AnsibleFailJson, |
| 19 |
exit_json, |
| 20 |
fail_json, |
| 21 |
set_module_args, |
| 22 |
) |
| 23 |
|
| 24 |
import ansible_collections.community.general.plugins.modules.wdc_redfish_command as module |
| 25 |
|
| 26 |
MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE = {"ret": True, "data": {}} |
| 27 |
|
| 28 |
MOCK_GET_ENCLOSURE_RESPONSE_SINGLE_TENANT = {"ret": True, "data": {"SerialNumber": "12345"}} |
| 29 |
|
| 30 |
MOCK_GET_ENCLOSURE_RESPONSE_MULTI_TENANT = {"ret": True, "data": {"SerialNumber": "12345-A"}} |
| 31 |
|
| 32 |
MOCK_URL_ERROR = {"ret": False, "msg": "This is a mock URL error", "status": 500} |
| 33 |
|
| 34 |
MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE = { |
| 35 |
"ret": True, |
| 36 |
"data": {"UpdateService": {"@odata.id": "/UpdateService"}, "Chassis": {"@odata.id": "/Chassis"}}, |
| 37 |
} |
| 38 |
|
| 39 |
MOCK_SUCCESSFUL_RESPONSE_CHASSIS = {"ret": True, "data": {"Members": [{"@odata.id": "/redfish/v1/Chassis/Enclosure"}]}} |
| 40 |
|
| 41 |
MOCK_SUCCESSFUL_RESPONSE_CHASSIS_ENCLOSURE = { |
| 42 |
"ret": True, |
| 43 |
"data": { |
| 44 |
"Id": "Enclosure", |
| 45 |
"IndicatorLED": "Off", |
| 46 |
"Actions": { |
| 47 |
"Oem": { |
| 48 |
"WDC": { |
| 49 |
"#Chassis.Locate": {"target": "/Chassis.Locate"}, |
| 50 |
"#Chassis.PowerMode": { |
| 51 |
"target": "/redfish/v1/Chassis/Enclosure/Actions/Chassis.PowerMode", |
| 52 |
}, |
| 53 |
} |
| 54 |
} |
| 55 |
}, |
| 56 |
"Oem": {"WDC": {"PowerMode": "Normal"}}, |
| 57 |
}, |
| 58 |
} |
| 59 |
|
| 60 |
MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_AND_FW_ACTIVATE = { |
| 61 |
"ret": True, |
| 62 |
"data": { |
| 63 |
"Actions": { |
| 64 |
"#UpdateService.SimpleUpdate": {"target": "mocked value"}, |
| 65 |
"Oem": { |
| 66 |
"WDC": { |
| 67 |
"#UpdateService.FWActivate": { |
| 68 |
"title": "Activate the downloaded firmware.", |
| 69 |
"target": "/redfish/v1/UpdateService/Actions/UpdateService.FWActivate", |
| 70 |
} |
| 71 |
} |
| 72 |
}, |
| 73 |
} |
| 74 |
}, |
| 75 |
} |
| 76 |
|
| 77 |
MOCK_SUCCESSFUL_RESPONSE_WITH_ACTIONS = {"ret": True, "data": {"Actions": {}}} |
| 78 |
|
| 79 |
MOCK_GET_IOM_A_MULTI_TENANT = {"ret": True, "data": {"Id": "IOModuleAFRU"}} |
| 80 |
|
| 81 |
MOCK_GET_IOM_B_MULTI_TENANAT = {"ret": True, "data": {"error": {"message": "IOM Module B cannot be read"}}} |
| 82 |
|
| 83 |
|
| 84 |
MOCK_READY_FOR_FW_UPDATE = {"ret": True, "entries": {"Description": "Ready for FW update", "StatusCode": 0}} |
| 85 |
|
| 86 |
MOCK_FW_UPDATE_IN_PROGRESS = {"ret": True, "entries": {"Description": "FW update in progress", "StatusCode": 1}} |
| 87 |
|
| 88 |
MOCK_WAITING_FOR_ACTIVATION = { |
| 89 |
"ret": True, |
| 90 |
"entries": {"Description": "FW update completed. Waiting for activation.", "StatusCode": 2}, |
| 91 |
} |
| 92 |
|
| 93 |
MOCK_SIMPLE_UPDATE_STATUS_LIST = [MOCK_READY_FOR_FW_UPDATE, MOCK_FW_UPDATE_IN_PROGRESS, MOCK_WAITING_FOR_ACTIVATION] |
| 94 |
|
| 95 |
|
| 96 |
def get_bin_path(self, arg, required=False): |
| 97 |
"""Mock AnsibleModule.get_bin_path""" |
| 98 |
return arg |
| 99 |
|
| 100 |
|
| 101 |
def get_exception_message(ansible_exit_json): |
| 102 |
"""From an AnsibleExitJson exception, get the message string.""" |
| 103 |
return ansible_exit_json.exception.args[0]["msg"] |
| 104 |
|
| 105 |
|
| 106 |
def is_changed(ansible_exit_json): |
| 107 |
"""From an AnsibleExitJson exception, return the value of the changed flag""" |
| 108 |
return ansible_exit_json.exception.args[0]["changed"] |
| 109 |
|
| 110 |
|
| 111 |
def mock_simple_update(*args, **kwargs): |
| 112 |
return {"ret": True} |
| 113 |
|
| 114 |
|
| 115 |
def mocked_url_response(*args, **kwargs): |
| 116 |
"""Mock to just return a generic string.""" |
| 117 |
return "/mockedUrl" |
| 118 |
|
| 119 |
|
| 120 |
def mock_update_url(*args, **kwargs): |
| 121 |
"""Mock of the update url""" |
| 122 |
return "/UpdateService" |
| 123 |
|
| 124 |
|
| 125 |
def mock_fw_activate_url(*args, **kwargs): |
| 126 |
"""Mock of the FW Activate URL""" |
| 127 |
return "/UpdateService.FWActivate" |
| 128 |
|
| 129 |
|
| 130 |
def empty_return(*args, **kwargs): |
| 131 |
"""Mock to just return an empty successful return.""" |
| 132 |
return {"ret": True} |
| 133 |
|
| 134 |
|
| 135 |
def mock_get_simple_update_status_ready_for_fw_update(*args, **kwargs): |
| 136 |
"""Mock to return simple update status Ready for FW update""" |
| 137 |
return MOCK_READY_FOR_FW_UPDATE |
| 138 |
|
| 139 |
|
| 140 |
def mock_get_request_enclosure_single_tenant(*args, **kwargs): |
| 141 |
"""Mock for get_request for single-tenant enclosure.""" |
| 142 |
if args[1].endswith("/redfish/v1") or args[1].endswith("/redfish/v1/"): |
| 143 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE |
| 144 |
elif args[1].endswith("/mockedUrl"): |
| 145 |
return MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE |
| 146 |
elif args[1].endswith("Chassis/Enclosure"): |
| 147 |
return MOCK_GET_ENCLOSURE_RESPONSE_SINGLE_TENANT |
| 148 |
elif args[1].endswith("/UpdateService"): |
| 149 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_AND_FW_ACTIVATE |
| 150 |
else: |
| 151 |
raise RuntimeError(f"Illegal call to get_request in test: {args[1]}") |
| 152 |
|
| 153 |
|
| 154 |
def mock_get_request_enclosure_multi_tenant(*args, **kwargs): |
| 155 |
"""Mock for get_request with multi-tenant enclosure.""" |
| 156 |
if args[1].endswith("/redfish/v1") or args[1].endswith("/redfish/v1/"): |
| 157 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE |
| 158 |
elif args[1].endswith("/mockedUrl"): |
| 159 |
return MOCK_SUCCESSFUL_HTTP_EMPTY_RESPONSE |
| 160 |
elif args[1].endswith("Chassis/Enclosure"): |
| 161 |
return MOCK_GET_ENCLOSURE_RESPONSE_MULTI_TENANT |
| 162 |
elif args[1].endswith("/UpdateService"): |
| 163 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_SIMPLE_UPDATE_AND_FW_ACTIVATE |
| 164 |
elif args[1].endswith("/IOModuleAFRU"): |
| 165 |
return MOCK_GET_IOM_A_MULTI_TENANT |
| 166 |
elif args[1].endswith("/IOModuleBFRU"): |
| 167 |
return MOCK_GET_IOM_B_MULTI_TENANAT |
| 168 |
else: |
| 169 |
raise RuntimeError(f"Illegal call to get_request in test: {args[1]}") |
| 170 |
|
| 171 |
|
| 172 |
def mock_get_request(*args, **kwargs): |
| 173 |
"""Mock for get_request for simple resource tests.""" |
| 174 |
if args[1].endswith("/redfish/v1") or args[1].endswith("/redfish/v1/"): |
| 175 |
return MOCK_SUCCESSFUL_RESPONSE_WITH_UPDATE_SERVICE_RESOURCE |
| 176 |
elif args[1].endswith("/Chassis"): |
| 177 |
return MOCK_SUCCESSFUL_RESPONSE_CHASSIS |
| 178 |
elif args[1].endswith("Chassis/Enclosure"): |
| 179 |
return MOCK_SUCCESSFUL_RESPONSE_CHASSIS_ENCLOSURE |
| 180 |
else: |
| 181 |
raise RuntimeError(f"Illegal call to get_request in test: {args[1]}") |
| 182 |
|
| 183 |
|
| 184 |
def mock_post_request(*args, **kwargs): |
| 185 |
"""Mock post_request with successful response.""" |
| 186 |
valid_endpoints = [ |
| 187 |
"/UpdateService.FWActivate", |
| 188 |
"/Chassis.Locate", |
| 189 |
"/Chassis.PowerMode", |
| 190 |
] |
| 191 |
for endpoint in valid_endpoints: |
| 192 |
if args[1].endswith(endpoint): |
| 193 |
return {"ret": True, "data": ACTION_WAS_SUCCESSFUL_MESSAGE} |
| 194 |
raise RuntimeError(f"Illegal POST call to: {args[1]}") |
| 195 |
|
| 196 |
|
| 197 |
def mock_get_firmware_inventory_version_1_2_3(*args, **kwargs): |
| 198 |
return { |
| 199 |
"ret": True, |
| 200 |
"entries": [{"Id": "IOModuleA_OOBM", "Version": "1.2.3"}, {"Id": "IOModuleB_OOBM", "Version": "1.2.3"}], |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
ERROR_MESSAGE_UNABLE_TO_EXTRACT_BUNDLE_VERSION = ( |
| 205 |
"Unable to extract bundle version or multi-tenant status or generation from update image file" |
| 206 |
) |
| 207 |
ACTION_WAS_SUCCESSFUL_MESSAGE = "Action was successful" |
| 208 |
|
| 209 |
|
| 210 |
class TestWdcRedfishCommand(unittest.TestCase): |
| 211 |
def setUp(self): |
| 212 |
self.mock_module_helper = patch.multiple( |
| 213 |
basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json, get_bin_path=get_bin_path |
| 214 |
) |
| 215 |
self.mock_module_helper.start() |
| 216 |
self.addCleanup(self.mock_module_helper.stop) |
| 217 |
self.tempdir = tempfile.mkdtemp() |
| 218 |
|
| 219 |
def tearDown(self): |
| 220 |
shutil.rmtree(self.tempdir) |
| 221 |
|
| 222 |
def test_module_fail_when_required_args_missing(self): |
| 223 |
with self.assertRaises(AnsibleFailJson): |
| 224 |
with set_module_args({}): |
| 225 |
module.main() |
| 226 |
|
| 227 |
def test_module_fail_when_unknown_category(self): |
| 228 |
with self.assertRaises(AnsibleFailJson): |
| 229 |
with set_module_args( |
| 230 |
{ |
| 231 |
"category": "unknown", |
| 232 |
"command": "FWActivate", |
| 233 |
"username": "USERID", |
| 234 |
"password": "PASSW0RD=21", |
| 235 |
"ioms": [], |
| 236 |
} |
| 237 |
): |
| 238 |
module.main() |
| 239 |
|
| 240 |
def test_module_fail_when_unknown_command(self): |
| 241 |
with self.assertRaises(AnsibleFailJson): |
| 242 |
with set_module_args( |
| 243 |
{ |
| 244 |
"category": "Update", |
| 245 |
"command": "unknown", |
| 246 |
"username": "USERID", |
| 247 |
"password": "PASSW0RD=21", |
| 248 |
"ioms": [], |
| 249 |
} |
| 250 |
): |
| 251 |
module.main() |
| 252 |
|
| 253 |
def test_module_chassis_power_mode_low(self): |
| 254 |
"""Test setting chassis power mode to low (happy path).""" |
| 255 |
module_args = { |
| 256 |
"category": "Chassis", |
| 257 |
"command": "PowerModeLow", |
| 258 |
"username": "USERID", |
| 259 |
"password": "PASSW0RD=21", |
| 260 |
"resource_id": "Enclosure", |
| 261 |
"baseuri": "example.com", |
| 262 |
} |
| 263 |
with set_module_args(module_args): |
| 264 |
with patch.multiple( |
| 265 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 266 |
get_request=mock_get_request, |
| 267 |
post_request=mock_post_request, |
| 268 |
): |
| 269 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 270 |
module.main() |
| 271 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 272 |
self.assertTrue(is_changed(ansible_exit_json)) |
| 273 |
|
| 274 |
def test_module_chassis_power_mode_normal_when_already_normal(self): |
| 275 |
"""Test setting chassis power mode to normal when it already is. Verify we get changed=False.""" |
| 276 |
module_args = { |
| 277 |
"category": "Chassis", |
| 278 |
"command": "PowerModeNormal", |
| 279 |
"username": "USERID", |
| 280 |
"password": "PASSW0RD=21", |
| 281 |
"resource_id": "Enclosure", |
| 282 |
"baseuri": "example.com", |
| 283 |
} |
| 284 |
with set_module_args(module_args): |
| 285 |
with patch.multiple( |
| 286 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 287 |
get_request=mock_get_request, |
| 288 |
): |
| 289 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 290 |
module.main() |
| 291 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 292 |
self.assertFalse(is_changed(ansible_exit_json)) |
| 293 |
|
| 294 |
def test_module_chassis_power_mode_invalid_command(self): |
| 295 |
"""Test that we get an error when issuing an invalid PowerMode command.""" |
| 296 |
module_args = { |
| 297 |
"category": "Chassis", |
| 298 |
"command": "PowerModeExtraHigh", |
| 299 |
"username": "USERID", |
| 300 |
"password": "PASSW0RD=21", |
| 301 |
"resource_id": "Enclosure", |
| 302 |
"baseuri": "example.com", |
| 303 |
} |
| 304 |
with set_module_args(module_args): |
| 305 |
with patch.multiple( |
| 306 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 307 |
get_request=mock_get_request, |
| 308 |
): |
| 309 |
with self.assertRaises(AnsibleFailJson) as ansible_fail_json: |
| 310 |
module.main() |
| 311 |
expected_error_message = "Invalid Command 'PowerModeExtraHigh'" |
| 312 |
self.assertIn(expected_error_message, get_exception_message(ansible_fail_json)) |
| 313 |
|
| 314 |
def test_module_enclosure_led_indicator_on(self): |
| 315 |
"""Test turning on a valid LED indicator (in this case we use the Enclosure resource).""" |
| 316 |
module_args = { |
| 317 |
"category": "Chassis", |
| 318 |
"command": "IndicatorLedOn", |
| 319 |
"username": "USERID", |
| 320 |
"password": "PASSW0RD=21", |
| 321 |
"resource_id": "Enclosure", |
| 322 |
"baseuri": "example.com", |
| 323 |
} |
| 324 |
with set_module_args(module_args): |
| 325 |
with patch.multiple( |
| 326 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 327 |
get_request=mock_get_request, |
| 328 |
post_request=mock_post_request, |
| 329 |
): |
| 330 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 331 |
module.main() |
| 332 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 333 |
self.assertTrue(is_changed(ansible_exit_json)) |
| 334 |
|
| 335 |
def test_module_invalid_resource_led_indicator_on(self): |
| 336 |
"""Test turning LED on for an invalid resource id.""" |
| 337 |
module_args = { |
| 338 |
"category": "Chassis", |
| 339 |
"command": "IndicatorLedOn", |
| 340 |
"username": "USERID", |
| 341 |
"password": "PASSW0RD=21", |
| 342 |
"resource_id": "Disk99", |
| 343 |
"baseuri": "example.com", |
| 344 |
} |
| 345 |
with set_module_args(module_args): |
| 346 |
with patch.multiple( |
| 347 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 348 |
get_request=mock_get_request, |
| 349 |
post_request=mock_post_request, |
| 350 |
): |
| 351 |
with self.assertRaises(AnsibleFailJson) as ansible_fail_json: |
| 352 |
module.main() |
| 353 |
expected_error_message = "Chassis resource Disk99 not found" |
| 354 |
self.assertEqual(expected_error_message, get_exception_message(ansible_fail_json)) |
| 355 |
|
| 356 |
def test_module_enclosure_led_off_already_off(self): |
| 357 |
"""Test turning LED indicator off when it's already off. Confirm changed is False and no POST occurs.""" |
| 358 |
module_args = { |
| 359 |
"category": "Chassis", |
| 360 |
"command": "IndicatorLedOff", |
| 361 |
"username": "USERID", |
| 362 |
"password": "PASSW0RD=21", |
| 363 |
"resource_id": "Enclosure", |
| 364 |
"baseuri": "example.com", |
| 365 |
} |
| 366 |
with set_module_args(module_args): |
| 367 |
with patch.multiple( |
| 368 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 369 |
get_request=mock_get_request, |
| 370 |
): |
| 371 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 372 |
module.main() |
| 373 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 374 |
self.assertFalse(is_changed(ansible_exit_json)) |
| 375 |
|
| 376 |
def test_module_fw_activate_first_iom_unavailable(self): |
| 377 |
"""Test that if the first IOM is not available, the 2nd one is used.""" |
| 378 |
ioms = ["bad.example.com", "good.example.com"] |
| 379 |
module_args = { |
| 380 |
"category": "Update", |
| 381 |
"command": "FWActivate", |
| 382 |
"username": "USERID", |
| 383 |
"password": "PASSW0RD=21", |
| 384 |
"ioms": ioms, |
| 385 |
} |
| 386 |
with set_module_args(module_args): |
| 387 |
|
| 388 |
def mock_get_request(*args, **kwargs): |
| 389 |
"""Mock for get_request that will fail on the 'bad' IOM.""" |
| 390 |
if "bad.example.com" in args[1]: |
| 391 |
return MOCK_URL_ERROR |
| 392 |
else: |
| 393 |
return mock_get_request_enclosure_single_tenant(*args, **kwargs) |
| 394 |
|
| 395 |
with patch.multiple( |
| 396 |
module.WdcRedfishUtils, |
| 397 |
_firmware_activate_uri=mock_fw_activate_url, |
| 398 |
_update_uri=mock_update_url, |
| 399 |
_find_updateservice_resource=empty_return, |
| 400 |
_find_updateservice_additional_uris=empty_return, |
| 401 |
get_request=mock_get_request, |
| 402 |
post_request=mock_post_request, |
| 403 |
): |
| 404 |
with self.assertRaises(AnsibleExitJson) as cm: |
| 405 |
module.main() |
| 406 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(cm)) |
| 407 |
|
| 408 |
def test_module_fw_activate_pass(self): |
| 409 |
"""Test the FW Activate command in a passing scenario.""" |
| 410 |
# Run the same test twice -- once specifying ioms, and once specifying baseuri. |
| 411 |
# Both should work the same way. |
| 412 |
uri_specifiers = [{"ioms": ["example1.example.com"]}, {"baseuri": "example1.example.com"}] |
| 413 |
for uri_specifier in uri_specifiers: |
| 414 |
module_args = { |
| 415 |
"category": "Update", |
| 416 |
"command": "FWActivate", |
| 417 |
"username": "USERID", |
| 418 |
"password": "PASSW0RD=21", |
| 419 |
} |
| 420 |
module_args.update(uri_specifier) |
| 421 |
with set_module_args(module_args): |
| 422 |
with patch.multiple( |
| 423 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 424 |
_firmware_activate_uri=mock_fw_activate_url, |
| 425 |
_update_uri=mock_update_url, |
| 426 |
_find_updateservice_resource=empty_return, |
| 427 |
_find_updateservice_additional_uris=empty_return, |
| 428 |
get_request=mock_get_request_enclosure_single_tenant, |
| 429 |
post_request=mock_post_request, |
| 430 |
): |
| 431 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 432 |
module.main() |
| 433 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 434 |
self.assertTrue(is_changed(ansible_exit_json)) |
| 435 |
|
| 436 |
def test_module_fw_activate_service_does_not_support_fw_activate(self): |
| 437 |
"""Test FW Activate when it is not supported.""" |
| 438 |
expected_error_message = "Service does not support FWActivate" |
| 439 |
with set_module_args( |
| 440 |
{ |
| 441 |
"category": "Update", |
| 442 |
"command": "FWActivate", |
| 443 |
"username": "USERID", |
| 444 |
"password": "PASSW0RD=21", |
| 445 |
"ioms": ["example1.example.com"], |
| 446 |
} |
| 447 |
): |
| 448 |
|
| 449 |
def mock_update_uri_response(*args, **kwargs): |
| 450 |
return { |
| 451 |
"ret": True, |
| 452 |
"data": {}, # No Actions |
| 453 |
} |
| 454 |
|
| 455 |
with patch.multiple( |
| 456 |
module.WdcRedfishUtils, |
| 457 |
_firmware_activate_uri=mocked_url_response, |
| 458 |
_update_uri=mock_update_url, |
| 459 |
_find_updateservice_resource=empty_return, |
| 460 |
_find_updateservice_additional_uris=empty_return, |
| 461 |
get_request=mock_update_uri_response, |
| 462 |
): |
| 463 |
with self.assertRaises(AnsibleFailJson) as cm: |
| 464 |
module.main() |
| 465 |
self.assertEqual(expected_error_message, get_exception_message(cm)) |
| 466 |
|
| 467 |
def test_module_update_and_activate_image_uri_not_http(self): |
| 468 |
"""Test Update and Activate when URI is not http(s)""" |
| 469 |
expected_error_message = "Bundle URI must be HTTP or HTTPS" |
| 470 |
with set_module_args( |
| 471 |
{ |
| 472 |
"category": "Update", |
| 473 |
"command": "UpdateAndActivate", |
| 474 |
"username": "USERID", |
| 475 |
"password": "PASSW0RD=21", |
| 476 |
"ioms": ["example1.example.com"], |
| 477 |
"update_image_uri": "ftp://example.com/image", |
| 478 |
} |
| 479 |
): |
| 480 |
with patch.multiple( |
| 481 |
module.WdcRedfishUtils, |
| 482 |
_firmware_activate_uri=mocked_url_response, |
| 483 |
_update_uri=mock_update_url, |
| 484 |
_find_updateservice_resource=empty_return, |
| 485 |
_find_updateservice_additional_uris=empty_return, |
| 486 |
): |
| 487 |
with self.assertRaises(AnsibleFailJson) as cm: |
| 488 |
module.main() |
| 489 |
self.assertEqual(expected_error_message, get_exception_message(cm)) |
| 490 |
|
| 491 |
def test_module_update_and_activate_target_not_ready_for_fw_update(self): |
| 492 |
"""Test Update and Activate when target is not in the correct state.""" |
| 493 |
mock_status_code = 999 |
| 494 |
mock_status_description = "mock status description" |
| 495 |
expected_error_message = ( |
| 496 |
f"Target is not ready for FW update. Current status: {mock_status_code} ({mock_status_description})" |
| 497 |
) |
| 498 |
with set_module_args( |
| 499 |
{ |
| 500 |
"category": "Update", |
| 501 |
"command": "UpdateAndActivate", |
| 502 |
"username": "USERID", |
| 503 |
"password": "PASSW0RD=21", |
| 504 |
"ioms": ["example1.example.com"], |
| 505 |
"update_image_uri": "http://example.com/image", |
| 506 |
} |
| 507 |
): |
| 508 |
with patch.object(module.WdcRedfishUtils, "get_simple_update_status") as mock_get_simple_update_status: |
| 509 |
mock_get_simple_update_status.return_value = { |
| 510 |
"ret": True, |
| 511 |
"entries": {"StatusCode": mock_status_code, "Description": mock_status_description}, |
| 512 |
} |
| 513 |
|
| 514 |
with patch.multiple( |
| 515 |
module.WdcRedfishUtils, |
| 516 |
_firmware_activate_uri=mocked_url_response, |
| 517 |
_update_uri=mock_update_url, |
| 518 |
_find_updateservice_resource=empty_return, |
| 519 |
_find_updateservice_additional_uris=empty_return, |
| 520 |
): |
| 521 |
with self.assertRaises(AnsibleFailJson) as cm: |
| 522 |
module.main() |
| 523 |
self.assertEqual(expected_error_message, get_exception_message(cm)) |
| 524 |
|
| 525 |
def test_module_update_and_activate_bundle_not_a_tarfile(self): |
| 526 |
"""Test Update and Activate when bundle is not a tarfile""" |
| 527 |
mock_filename = os.path.abspath(__file__) |
| 528 |
expected_error_message = ERROR_MESSAGE_UNABLE_TO_EXTRACT_BUNDLE_VERSION |
| 529 |
with set_module_args( |
| 530 |
{ |
| 531 |
"category": "Update", |
| 532 |
"command": "UpdateAndActivate", |
| 533 |
"username": "USERID", |
| 534 |
"password": "PASSW0RD=21", |
| 535 |
"ioms": ["example1.example.com"], |
| 536 |
"update_image_uri": "http://example.com/image", |
| 537 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 538 |
} |
| 539 |
): |
| 540 |
with patch( |
| 541 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 542 |
) as mock_fetch_file: |
| 543 |
mock_fetch_file.return_value = mock_filename |
| 544 |
with patch.multiple( |
| 545 |
module.WdcRedfishUtils, |
| 546 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 547 |
_firmware_activate_uri=mocked_url_response, |
| 548 |
_update_uri=mock_update_url, |
| 549 |
_find_updateservice_resource=empty_return, |
| 550 |
_find_updateservice_additional_uris=empty_return, |
| 551 |
): |
| 552 |
with self.assertRaises(AnsibleFailJson) as cm: |
| 553 |
module.main() |
| 554 |
self.assertEqual(expected_error_message, get_exception_message(cm)) |
| 555 |
|
| 556 |
def test_module_update_and_activate_bundle_contains_no_firmware_version(self): |
| 557 |
"""Test Update and Activate when bundle contains no firmware version""" |
| 558 |
expected_error_message = ERROR_MESSAGE_UNABLE_TO_EXTRACT_BUNDLE_VERSION |
| 559 |
with set_module_args( |
| 560 |
{ |
| 561 |
"category": "Update", |
| 562 |
"command": "UpdateAndActivate", |
| 563 |
"username": "USERID", |
| 564 |
"password": "PASSW0RD=21", |
| 565 |
"ioms": ["example1.example.com"], |
| 566 |
"update_image_uri": "http://example.com/image", |
| 567 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 568 |
} |
| 569 |
): |
| 570 |
tar_name = f"empty_tarfile{uuid.uuid4()}.tar" |
| 571 |
empty_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w") |
| 572 |
empty_tarfile.close() |
| 573 |
with patch( |
| 574 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 575 |
) as mock_fetch_file: |
| 576 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 577 |
with patch.multiple( |
| 578 |
module.WdcRedfishUtils, |
| 579 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 580 |
_firmware_activate_uri=mocked_url_response, |
| 581 |
_update_uri=mock_update_url, |
| 582 |
_find_updateservice_resource=empty_return, |
| 583 |
_find_updateservice_additional_uris=empty_return, |
| 584 |
): |
| 585 |
with self.assertRaises(AnsibleFailJson) as cm: |
| 586 |
module.main() |
| 587 |
self.assertEqual(expected_error_message, get_exception_message(cm)) |
| 588 |
|
| 589 |
def test_module_update_and_activate_version_already_installed(self): |
| 590 |
"""Test Update and Activate when the bundle version is already installed""" |
| 591 |
mock_firmware_version = "1.2.3" |
| 592 |
expected_error_message = ACTION_WAS_SUCCESSFUL_MESSAGE |
| 593 |
with set_module_args( |
| 594 |
{ |
| 595 |
"category": "Update", |
| 596 |
"command": "UpdateAndActivate", |
| 597 |
"username": "USERID", |
| 598 |
"password": "PASSW0RD=21", |
| 599 |
"ioms": ["example1.example.com"], |
| 600 |
"update_image_uri": "http://example.com/image", |
| 601 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 602 |
} |
| 603 |
): |
| 604 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=False) |
| 605 |
with patch( |
| 606 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 607 |
) as mock_fetch_file: |
| 608 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 609 |
with patch.multiple( |
| 610 |
module.WdcRedfishUtils, |
| 611 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3, |
| 612 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 613 |
_firmware_activate_uri=mocked_url_response, |
| 614 |
_update_uri=mock_update_url, |
| 615 |
_find_updateservice_resource=empty_return, |
| 616 |
_find_updateservice_additional_uris=empty_return, |
| 617 |
get_request=mock_get_request_enclosure_single_tenant, |
| 618 |
): |
| 619 |
with self.assertRaises(AnsibleExitJson) as result: |
| 620 |
module.main() |
| 621 |
self.assertEqual(expected_error_message, get_exception_message(result)) |
| 622 |
self.assertFalse(is_changed(result)) |
| 623 |
|
| 624 |
def test_module_update_and_activate_version_already_installed_multi_tenant(self): |
| 625 |
"""Test Update and Activate on multi-tenant when version is already installed""" |
| 626 |
mock_firmware_version = "1.2.3" |
| 627 |
expected_error_message = ACTION_WAS_SUCCESSFUL_MESSAGE |
| 628 |
with set_module_args( |
| 629 |
{ |
| 630 |
"category": "Update", |
| 631 |
"command": "UpdateAndActivate", |
| 632 |
"username": "USERID", |
| 633 |
"password": "PASSW0RD=21", |
| 634 |
"ioms": ["example1.example.com"], |
| 635 |
"update_image_uri": "http://example.com/image", |
| 636 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 637 |
} |
| 638 |
): |
| 639 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=True) |
| 640 |
with patch( |
| 641 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 642 |
) as mock_fetch_file: |
| 643 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 644 |
with patch.multiple( |
| 645 |
module.WdcRedfishUtils, |
| 646 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3, |
| 647 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 648 |
_firmware_activate_uri=mocked_url_response, |
| 649 |
_update_uri=mock_update_url, |
| 650 |
_find_updateservice_resource=empty_return, |
| 651 |
_find_updateservice_additional_uris=empty_return, |
| 652 |
get_request=mock_get_request_enclosure_multi_tenant, |
| 653 |
): |
| 654 |
with self.assertRaises(AnsibleExitJson) as result: |
| 655 |
module.main() |
| 656 |
self.assertEqual(expected_error_message, get_exception_message(result)) |
| 657 |
self.assertFalse(is_changed(result)) |
| 658 |
|
| 659 |
def test_module_update_and_activate_pass(self): |
| 660 |
"""Test Update and Activate (happy path)""" |
| 661 |
mock_firmware_version = "1.2.2" |
| 662 |
with set_module_args( |
| 663 |
{ |
| 664 |
"category": "Update", |
| 665 |
"command": "UpdateAndActivate", |
| 666 |
"username": "USERID", |
| 667 |
"password": "PASSW0RD=21", |
| 668 |
"ioms": ["example1.example.com"], |
| 669 |
"update_image_uri": "http://example.com/image", |
| 670 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 671 |
} |
| 672 |
): |
| 673 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=False) |
| 674 |
|
| 675 |
with patch( |
| 676 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 677 |
) as mock_fetch_file: |
| 678 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 679 |
with patch.multiple( |
| 680 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils", |
| 681 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3, |
| 682 |
simple_update=mock_simple_update, |
| 683 |
_simple_update_status_uri=mocked_url_response, |
| 684 |
# _find_updateservice_resource=empty_return, |
| 685 |
# _find_updateservice_additional_uris=empty_return, |
| 686 |
get_request=mock_get_request_enclosure_single_tenant, |
| 687 |
post_request=mock_post_request, |
| 688 |
): |
| 689 |
with patch( |
| 690 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status" |
| 691 |
) as mock_get_simple_update_status: |
| 692 |
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST |
| 693 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 694 |
module.main() |
| 695 |
self.assertTrue(is_changed(ansible_exit_json)) |
| 696 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 697 |
|
| 698 |
def test_module_update_and_activate_pass_multi_tenant(self): |
| 699 |
"""Test Update and Activate with multi-tenant (happy path)""" |
| 700 |
mock_firmware_version = "1.2.2" |
| 701 |
with set_module_args( |
| 702 |
{ |
| 703 |
"category": "Update", |
| 704 |
"command": "UpdateAndActivate", |
| 705 |
"username": "USERID", |
| 706 |
"password": "PASSW0RD=21", |
| 707 |
"ioms": ["example1.example.com"], |
| 708 |
"update_image_uri": "http://example.com/image", |
| 709 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 710 |
} |
| 711 |
): |
| 712 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=True) |
| 713 |
|
| 714 |
with patch( |
| 715 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 716 |
) as mock_fetch_file: |
| 717 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 718 |
with patch.multiple( |
| 719 |
module.WdcRedfishUtils, |
| 720 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3, |
| 721 |
simple_update=mock_simple_update, |
| 722 |
_simple_update_status_uri=mocked_url_response, |
| 723 |
# _find_updateservice_resource=empty_return, |
| 724 |
# _find_updateservice_additional_uris=empty_return, |
| 725 |
get_request=mock_get_request_enclosure_multi_tenant, |
| 726 |
post_request=mock_post_request, |
| 727 |
): |
| 728 |
with patch( |
| 729 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.WdcRedfishUtils.get_simple_update_status" |
| 730 |
) as mock_get_simple_update_status: |
| 731 |
mock_get_simple_update_status.side_effect = MOCK_SIMPLE_UPDATE_STATUS_LIST |
| 732 |
with self.assertRaises(AnsibleExitJson) as ansible_exit_json: |
| 733 |
module.main() |
| 734 |
self.assertTrue(is_changed(ansible_exit_json)) |
| 735 |
self.assertEqual(ACTION_WAS_SUCCESSFUL_MESSAGE, get_exception_message(ansible_exit_json)) |
| 736 |
|
| 737 |
def test_module_fw_update_multi_tenant_firmware_single_tenant_enclosure(self): |
| 738 |
"""Test Update and Activate using multi-tenant bundle on single-tenant enclosure""" |
| 739 |
mock_firmware_version = "1.1.1" |
| 740 |
expected_error_message = "Enclosure multi-tenant is False but bundle multi-tenant is True" |
| 741 |
with set_module_args( |
| 742 |
{ |
| 743 |
"category": "Update", |
| 744 |
"command": "UpdateAndActivate", |
| 745 |
"username": "USERID", |
| 746 |
"password": "PASSW0RD=21", |
| 747 |
"ioms": ["example1.example.com"], |
| 748 |
"update_image_uri": "http://example.com/image", |
| 749 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 750 |
} |
| 751 |
): |
| 752 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=True) |
| 753 |
with patch( |
| 754 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 755 |
) as mock_fetch_file: |
| 756 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 757 |
with patch.multiple( |
| 758 |
module.WdcRedfishUtils, |
| 759 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(), |
| 760 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 761 |
_firmware_activate_uri=mocked_url_response, |
| 762 |
_update_uri=mock_update_url, |
| 763 |
_find_updateservice_resource=empty_return, |
| 764 |
_find_updateservice_additional_uris=empty_return, |
| 765 |
get_request=mock_get_request_enclosure_single_tenant, |
| 766 |
): |
| 767 |
with self.assertRaises(AnsibleFailJson) as result: |
| 768 |
module.main() |
| 769 |
self.assertEqual(expected_error_message, get_exception_message(result)) |
| 770 |
|
| 771 |
def test_module_fw_update_single_tentant_firmware_multi_tenant_enclosure(self): |
| 772 |
"""Test Update and Activate using singe-tenant bundle on multi-tenant enclosure""" |
| 773 |
mock_firmware_version = "1.1.1" |
| 774 |
expected_error_message = "Enclosure multi-tenant is True but bundle multi-tenant is False" |
| 775 |
with set_module_args( |
| 776 |
{ |
| 777 |
"category": "Update", |
| 778 |
"command": "UpdateAndActivate", |
| 779 |
"username": "USERID", |
| 780 |
"password": "PASSW0RD=21", |
| 781 |
"ioms": ["example1.example.com"], |
| 782 |
"update_image_uri": "http://example.com/image", |
| 783 |
"update_creds": {"username": "image_user", "password": "image_password"}, |
| 784 |
} |
| 785 |
): |
| 786 |
tar_name = self.generate_temp_bundlefile(mock_firmware_version=mock_firmware_version, is_multi_tenant=False) |
| 787 |
with patch( |
| 788 |
"ansible_collections.community.general.plugins.module_utils._wdc_redfish_utils.fetch_file" |
| 789 |
) as mock_fetch_file: |
| 790 |
mock_fetch_file.return_value = os.path.join(self.tempdir, tar_name) |
| 791 |
with patch.multiple( |
| 792 |
module.WdcRedfishUtils, |
| 793 |
get_firmware_inventory=mock_get_firmware_inventory_version_1_2_3(), |
| 794 |
get_simple_update_status=mock_get_simple_update_status_ready_for_fw_update, |
| 795 |
_firmware_activate_uri=mocked_url_response, |
| 796 |
_update_uri=mock_update_url, |
| 797 |
_find_updateservice_resource=empty_return, |
| 798 |
_find_updateservice_additional_uris=empty_return, |
| 799 |
get_request=mock_get_request_enclosure_multi_tenant, |
| 800 |
): |
| 801 |
with self.assertRaises(AnsibleFailJson) as result: |
| 802 |
module.main() |
| 803 |
self.assertEqual(expected_error_message, get_exception_message(result)) |
| 804 |
|
| 805 |
def generate_temp_bundlefile(self, mock_firmware_version, is_multi_tenant): |
| 806 |
"""Generate a temporary fake bundle file. |
| 807 |
|
| 808 |
:param str mock_firmware_version: The simulated firmware version for the bundle. |
| 809 |
:param bool is_multi_tenant: Is the simulated bundle multi-tenant? |
| 810 |
|
| 811 |
This can be used for a mock FW update. |
| 812 |
""" |
| 813 |
tar_name = f"tarfile{uuid.uuid4()}.tar" |
| 814 |
|
| 815 |
bundle_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w") |
| 816 |
package_filename = f"oobm-{mock_firmware_version}.pkg" |
| 817 |
package_filename_path = os.path.join(self.tempdir, package_filename) |
| 818 |
with open(package_filename_path, "w"): |
| 819 |
pass |
| 820 |
bundle_tarfile.add(os.path.join(self.tempdir, package_filename), arcname=package_filename) |
| 821 |
bin_filename = "firmware.bin" |
| 822 |
bin_filename_path = os.path.join(self.tempdir, bin_filename) |
| 823 |
with open(bin_filename_path, "wb") as bin_file: |
| 824 |
byte_to_write = b"\x80" if is_multi_tenant else b"\xff" |
| 825 |
bin_file.write(byte_to_write * 12) |
| 826 |
for filename in [package_filename, bin_filename]: |
| 827 |
bundle_tarfile.add(os.path.join(self.tempdir, filename), arcname=filename) |
| 828 |
bundle_tarfile.close() |
| 829 |
return tar_name |