| 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_powerstate |
| 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 |
tarrget_error_msg = "The target device does not support the system reset" \ |
| 26 |
" feature using Redfish API." |
| 27 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 28 |
HTTPS_ADDRESS = 'https://testhost.com' |
| 29 |
INVALID_RESET_TYPE = "The target device does not support a {0} operation.The acceptable values for device reset types are {1}." |
| 30 |
INVALID_RESET_TYPE_OEM = "'{option}' is not supported. The supported values" \ |
| 31 |
" are {supported_oem_reset_type_values}. Enter the valid values and retry" \ |
| 32 |
" the operation." |
| 33 |
MODULE_PATH_COMP = 'ansible_collections.dellemc.openmanage.plugins.modules.redfish_powerstate.' |
| 34 |
VENDOR_NOT_SPECIFIED = "The vendor is not specified. Enter the valid vendor and retry the operation." |
| 35 |
VALIDATE_FINAL_PWR_STATE = 'redfish_powerstate.is_valid_final_pwr_state' |
| 36 |
VALIDATE_VENDOR = 'redfish_powerstate.is_valid_vendor' |
| 37 |
CHECK_FIRMWARE_VERSION = 'redfish_powerstate.check_firmware_version' |
| 38 |
VALIDATE_RESET_TYPE = 'redfish_powerstate.is_valid_reset_type' |
| 39 |
FETCH_PWR_URI = 'redfish_powerstate.fetch_power_uri_resource' |
| 40 |
TARGET_URI = "/redfish/v1/Chassis/System.Embedded.1/Actions/Oem/DellOemChassis.ExtendedReset" |
| 41 |
RESET_URI = '/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem.Reset' |
| 42 |
ROOT_URI = "/redfish/v1/" |
| 43 |
RESET_ALLOWED_KEY = "[email protected]" |
| 44 |
ODATA_KEY = "@odata.id" |
| 45 |
RESOURCE_URI = "/redfish/v1/Systems" |
| 46 |
SPECIFIC_RESOURCE_URI_ONE = "/redfish/v1/Systems/System.Embedded.1" |
| 47 |
SPECIFIC_RESOURCE_URI_TWO = "/redfish/v1/Systems/System.Embedded.2" |
| 48 |
|
| 49 |
|
| 50 |
@pytest.fixture |
| 51 |
def redfish_connection_mock_for_powerstate(mocker, redfish_response_mock): |
| 52 |
connection_class_mock = mocker.patch(MODULE_PATH + 'redfish_powerstate.Redfish') |
| 53 |
redfish_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 54 |
redfish_connection_mock_obj.invoke_request.return_value = redfish_response_mock |
| 55 |
return redfish_connection_mock_obj |
| 56 |
|
| 57 |
|
| 58 |
class TestRedfishPowerstate(FakeAnsibleModule): |
| 59 |
module = redfish_powerstate |
| 60 |
|
| 61 |
arg_list1 = [{"resource_id": "System.Embedded.1", "reset_type": "ForceOff"}] |
| 62 |
resource_uri_output = { |
| 63 |
"Systems": { |
| 64 |
ODATA_KEY: RESOURCE_URI |
| 65 |
}, |
| 66 |
"Members": [ |
| 67 |
{ |
| 68 |
ODATA_KEY: SPECIFIC_RESOURCE_URI_ONE |
| 69 |
}, |
| 70 |
{ |
| 71 |
ODATA_KEY: SPECIFIC_RESOURCE_URI_TWO |
| 72 |
} |
| 73 |
], |
| 74 |
"Actions": { |
| 75 |
"#ComputerSystem.Reset": { |
| 76 |
"target": RESET_URI, |
| 77 |
RESET_ALLOWED_KEY: [ |
| 78 |
"On", |
| 79 |
"ForceOff", |
| 80 |
"ForceRestart", |
| 81 |
"GracefulShutdown", |
| 82 |
"PushPowerButton", |
| 83 |
"Nmi", |
| 84 |
"PowerCycle" |
| 85 |
] |
| 86 |
} |
| 87 |
}, |
| 88 |
"PowerState": "On" |
| 89 |
} |
| 90 |
|
| 91 |
def test_fetch_powerstate_resource_success_case_01(self, mocker, redfish_connection_mock_for_powerstate, redfish_default_args, |
| 92 |
redfish_response_mock): |
| 93 |
"""dynamically fetch the computer system id if one member exists in system""" |
| 94 |
f_module = self.get_module_mock(params={"reset_type": "ForceOff"}) |
| 95 |
|
| 96 |
redfish_response_mock.json_data = self.resource_uri_output |
| 97 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 98 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate, "Systems") |
| 99 |
|
| 100 |
assert self.module.powerstate_map["allowable_enums"] == [ |
| 101 |
"On", |
| 102 |
"ForceOff", |
| 103 |
"ForceRestart", |
| 104 |
"GracefulShutdown", |
| 105 |
"PushPowerButton", |
| 106 |
"Nmi", |
| 107 |
"PowerCycle" |
| 108 |
] |
| 109 |
assert self.module.powerstate_map['power_uri'] == RESET_URI |
| 110 |
assert self.module.powerstate_map['current_state'] == 'On' |
| 111 |
|
| 112 |
def test_fetch_powerstate_resource_success_case_02(self, mocker, redfish_connection_mock_for_powerstate, redfish_default_args, |
| 113 |
redfish_response_mock): |
| 114 |
"""dynamically fetch the computer system id if one member exists in system""" |
| 115 |
f_module = self.get_module_mock(params={"oem_reset_type": {"dell": {"reset_type": "PowerCycle"}}}) |
| 116 |
|
| 117 |
redfish_response_mock.json_data = { |
| 118 |
"Chassis": { |
| 119 |
ODATA_KEY: "/redfish/v1/Chassis" |
| 120 |
}, |
| 121 |
"Members": [ |
| 122 |
{ |
| 123 |
ODATA_KEY: "/redfish/v1/Chassis/System.Embedded.1" |
| 124 |
} |
| 125 |
], |
| 126 |
"Actions": { |
| 127 |
"Oem": { |
| 128 |
"#DellOemChassis.ExtendedReset": { |
| 129 |
"[email protected]": [ |
| 130 |
"On", |
| 131 |
"Off" |
| 132 |
], |
| 133 |
RESET_ALLOWED_KEY: [ |
| 134 |
"PowerCycle" |
| 135 |
], |
| 136 |
"target": TARGET_URI |
| 137 |
} |
| 138 |
} |
| 139 |
}, |
| 140 |
"PowerState": "Off" |
| 141 |
} |
| 142 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 143 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate, "Chassis") |
| 144 |
|
| 145 |
assert self.module.powerstate_map["allowable_enums"] == [ |
| 146 |
"PowerCycle" |
| 147 |
] |
| 148 |
assert self.module.powerstate_map['power_uri'] == '/redfish/v1/Chassis/System.Embedded.1/Actions/Oem/DellOemChassis.ExtendedReset' |
| 149 |
assert self.module.powerstate_map['current_state'] == 'Off' |
| 150 |
|
| 151 |
def test_fetch_powerstate_resource_resource_id_given_success_case(self, |
| 152 |
redfish_connection_mock_for_powerstate, |
| 153 |
redfish_response_mock): |
| 154 |
"""case when system id is explicitly provided""" |
| 155 |
f_module = self.get_module_mock(params={"resource_id": "System.Embedded.1", "reset_type": "ForceOff"}) |
| 156 |
redfish_response_mock.json_data = self.resource_uri_output |
| 157 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 158 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate, "Systems") |
| 159 |
assert self.module.powerstate_map["allowable_enums"] == [ |
| 160 |
"On", |
| 161 |
"ForceOff", |
| 162 |
"ForceRestart", |
| 163 |
"GracefulShutdown", |
| 164 |
"PushPowerButton", |
| 165 |
"Nmi", |
| 166 |
"PowerCycle" |
| 167 |
] |
| 168 |
assert self.module.powerstate_map['power_uri'] == RESET_URI |
| 169 |
assert self.module.powerstate_map['current_state'] == 'On' |
| 170 |
|
| 171 |
def test_fetch_powerstate_resource_resource_id_not_given_success_case(self, |
| 172 |
redfish_connection_mock_for_powerstate, |
| 173 |
redfish_response_mock): |
| 174 |
"""case when system id not provided but multipble resource exists""" |
| 175 |
f_module = self.get_module_mock(params={"reset_type": "ForceOff"}) |
| 176 |
redfish_response_mock.json_data = self.resource_uri_output |
| 177 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 178 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate, "Systems") |
| 179 |
assert self.module.powerstate_map["allowable_enums"] == [ |
| 180 |
"On", |
| 181 |
"ForceOff", |
| 182 |
"ForceRestart", |
| 183 |
"GracefulShutdown", |
| 184 |
"PushPowerButton", |
| 185 |
"Nmi", |
| 186 |
"PowerCycle" |
| 187 |
] |
| 188 |
assert self.module.powerstate_map['power_uri'] == RESET_URI |
| 189 |
assert self.module.powerstate_map['current_state'] == 'On' |
| 190 |
|
| 191 |
def test_fetch_powerstate_resource_resource_id_invalid_failure_case(self, |
| 192 |
redfish_connection_mock_for_powerstate, |
| 193 |
redfish_response_mock): |
| 194 |
"""failure case when system id is explicitly provided but which is not valid""" |
| 195 |
f_module = self.get_module_mock(params={"resource_id": "System.Embedded.3", "reset_type": "ForceOff"}) |
| 196 |
redfish_response_mock.json_data = { |
| 197 |
"Systems": { |
| 198 |
ODATA_KEY: RESOURCE_URI |
| 199 |
}, |
| 200 |
"Members": |
| 201 |
[ |
| 202 |
{ |
| 203 |
ODATA_KEY: SPECIFIC_RESOURCE_URI_ONE |
| 204 |
}, |
| 205 |
{ |
| 206 |
ODATA_KEY: SPECIFIC_RESOURCE_URI_TWO |
| 207 |
} |
| 208 |
], |
| 209 |
"Actions": { |
| 210 |
"#ComputerSystem.Reset": { |
| 211 |
"target": "/redfish/v1/Systems/System.Embedded.2/Actions/ComputerSystem.Reset", |
| 212 |
RESET_ALLOWED_KEY: [ |
| 213 |
"On", |
| 214 |
"ForceOff", |
| 215 |
"ForceRestart", |
| 216 |
"GracefulShutdown", |
| 217 |
"PushPowerButton", |
| 218 |
"Nmi", |
| 219 |
"PowerCycle" |
| 220 |
] |
| 221 |
} |
| 222 |
}, |
| 223 |
"PowerState": "On" |
| 224 |
} |
| 225 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 226 |
with pytest.raises(Exception) as exc: |
| 227 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate, "Systems") |
| 228 |
assert exc.value.args[0] == "Invalid device Id 'System.Embedded.3' is provided" |
| 229 |
|
| 230 |
def test_fetch_powerstate_resource_error_case_01(self, redfish_connection_mock_for_powerstate, |
| 231 |
redfish_response_mock): |
| 232 |
"""failure case when system does not supports redfish computer system in schema""" |
| 233 |
f_module = self.get_module_mock() |
| 234 |
redfish_response_mock.json_data = { |
| 235 |
ODATA_KEY: RESOURCE_URI, |
| 236 |
"Members": [ |
| 237 |
], |
| 238 |
} |
| 239 |
|
| 240 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 241 |
with pytest.raises(Exception) as exc: |
| 242 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 243 |
assert exc.value.args[0] == tarrget_error_msg |
| 244 |
|
| 245 |
def test_fetch_powerstate_resource_error_case_02(self, redfish_connection_mock_for_powerstate, |
| 246 |
redfish_response_mock): |
| 247 |
"""failuere case when system does not supports redfish computer system action in schema""" |
| 248 |
f_module = self.get_module_mock() |
| 249 |
redfish_response_mock.json_data = { |
| 250 |
"Systems": { |
| 251 |
ODATA_KEY: RESOURCE_URI |
| 252 |
}, |
| 253 |
"Members": [ |
| 254 |
{ |
| 255 |
ODATA_KEY: SPECIFIC_RESOURCE_URI_ONE |
| 256 |
} |
| 257 |
], |
| 258 |
"Actions": { |
| 259 |
|
| 260 |
}} |
| 261 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 262 |
with pytest.raises(Exception) as exc: |
| 263 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 264 |
assert exc.value.args[0] == tarrget_error_msg |
| 265 |
|
| 266 |
def test_fetch_powerstate_resource_error_case_03(self, redfish_connection_mock_for_powerstate, |
| 267 |
redfish_response_mock): |
| 268 |
"""failuere case when system does not supports and throws http error not found""" |
| 269 |
f_module = self.get_module_mock() |
| 270 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 271 |
redfish_connection_mock_for_powerstate.invoke_request.side_effect = HTTPError(HTTPS_ADDRESS, 404, |
| 272 |
json.dumps(tarrget_error_msg), {}, |
| 273 |
None) |
| 274 |
with pytest.raises(Exception) as exc: |
| 275 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 276 |
|
| 277 |
def test_fetch_powerstate_resource_error_case_04(self, redfish_connection_mock_for_powerstate, |
| 278 |
redfish_response_mock): |
| 279 |
"""failure case when system does not supports and throws http error 400 bad request""" |
| 280 |
f_module = self.get_module_mock() |
| 281 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 282 |
redfish_connection_mock_for_powerstate.invoke_request.side_effect = HTTPError(HTTPS_ADDRESS, 400, |
| 283 |
tarrget_error_msg, |
| 284 |
{}, None) |
| 285 |
with pytest.raises(Exception, match=tarrget_error_msg) as exc: |
| 286 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 287 |
|
| 288 |
def test_fetch_powerstate_resource_error_case_05(self, redfish_connection_mock_for_powerstate, |
| 289 |
redfish_response_mock): |
| 290 |
f_module = self.get_module_mock() |
| 291 |
msg = "connection error" |
| 292 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 293 |
redfish_connection_mock_for_powerstate.invoke_request.side_effect = URLError(msg) |
| 294 |
with pytest.raises(Exception, match=msg) as exc: |
| 295 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 296 |
|
| 297 |
def test_fetch_powerstate_resource_error_case_06(self, redfish_connection_mock_for_powerstate, |
| 298 |
redfish_response_mock): |
| 299 |
"""when both system id and mebers of id not provided""" |
| 300 |
f_module = self.get_module_mock() |
| 301 |
redfish_response_mock.json_data = { |
| 302 |
"Systems": { |
| 303 |
ODATA_KEY: RESOURCE_URI |
| 304 |
}, |
| 305 |
"Members": [ |
| 306 |
], |
| 307 |
"Actions": { |
| 308 |
|
| 309 |
}} |
| 310 |
redfish_connection_mock_for_powerstate.root_uri = ROOT_URI |
| 311 |
with pytest.raises(Exception) as exc: |
| 312 |
self.module.fetch_power_uri_resource(f_module, redfish_connection_mock_for_powerstate) |
| 313 |
assert exc.value.args[0] == tarrget_error_msg |
| 314 |
|
| 315 |
power_vals = [{"apply": "On", "current": "On", "result": False}, |
| 316 |
{"apply": "On", "current": "PoweringOn", "result": False}, |
| 317 |
{"apply": "On", "current": "Off", "result": True}, |
| 318 |
{"apply": "On", "current": "PoweringOff", "result": True}, |
| 319 |
{"apply": "ForceOn", "current": "On", "result": False}, |
| 320 |
{"apply": "ForceOn", "current": "PoweringOn", "result": False}, |
| 321 |
{"apply": "ForceOn", "current": "Off", "result": True}, |
| 322 |
{"apply": "ForceOn", "current": "PoweringOff", "result": True}, |
| 323 |
{"apply": "PushPowerButton", "current": "On", "result": True}, |
| 324 |
{"apply": "PushPowerButton", "current": "PoweringOn", "result": True}, |
| 325 |
{"apply": "PushPowerButton", "current": "Off", "result": True}, |
| 326 |
{"apply": "PushPowerButton", "current": "PoweringOff", "result": True}, |
| 327 |
{"apply": "ForceOff", "current": "On", "result": True}, |
| 328 |
{"apply": "ForceOff", "current": "PoweringOn", "result": True}, |
| 329 |
{"apply": "ForceOff", "current": "Off", "result": False}, |
| 330 |
{"apply": "ForceOff", "current": "PoweringOff", "result": False}, |
| 331 |
{"apply": "ForceRestart", "current": "On", "result": True}, |
| 332 |
{"apply": "ForceRestart", "current": "PoweringOn", "result": True}, |
| 333 |
{"apply": "ForceRestart", "current": "Off", "result": False}, |
| 334 |
{"apply": "ForceRestart", "current": "PoweringOff", "result": False}, |
| 335 |
{"apply": "GracefulRestart", "current": "On", "result": True}, |
| 336 |
{"apply": "GracefulRestart", "current": "PoweringOn", "result": True}, |
| 337 |
{"apply": "GracefulRestart", "current": "Off", "result": False}, |
| 338 |
{"apply": "GracefulRestart", "current": "PoweringOff", "result": False}, |
| 339 |
{"apply": "GracefulShutdown", "current": "On", "result": True}, |
| 340 |
{"apply": "GracefulShutdown", "current": "PoweringOn", "result": True}, |
| 341 |
{"apply": "GracefulShutdown", "current": "Off", "result": False}, |
| 342 |
{"apply": "GracefulShutdown", "current": "PoweringOff", "result": False}, |
| 343 |
{"apply": "Nmi", "current": "On", "result": True}, |
| 344 |
{"apply": "Nmi", "current": "PoweringOn", "result": True}, |
| 345 |
{"apply": "Nmi", "current": "Off", "result": False}, |
| 346 |
{"apply": "Nmi", "current": "PoweringOff", "result": False}, |
| 347 |
{"apply": "PowerCycle", "current": "On", "result": True}, |
| 348 |
{"apply": "PowerCycle", "current": "PoweringOn", "result": True}, |
| 349 |
{"apply": "PowerCycle", "current": "Off", "result": False}, |
| 350 |
{"apply": "PowerCycle", "current": "PoweringOff", "result": False}, |
| 351 |
|
| 352 |
] |
| 353 |
|
| 354 |
@pytest.mark.parametrize("power_map", power_vals) |
| 355 |
def test_is_change_applicable_for_power_state(self, power_map): |
| 356 |
apply_state = power_map["apply"] |
| 357 |
current_state = power_map["current"] |
| 358 |
result = power_map["result"] |
| 359 |
res = self.module.is_change_applicable_for_power_state(current_state, apply_state) |
| 360 |
assert res is result |
| 361 |
|
| 362 |
def test_is_change_applicable_for_power_state_case_02(self): |
| 363 |
apply_state = "xyz" |
| 364 |
current_state = "On" |
| 365 |
result = False |
| 366 |
res = self.module.is_change_applicable_for_power_state(current_state, apply_state) |
| 367 |
assert res is result |
| 368 |
|
| 369 |
def test_is_valid_reset_type(self): |
| 370 |
|
| 371 |
f_module = self.get_module_mock({"reset_type": "ForceOn"}) |
| 372 |
reset_type = "GracefulRestart" |
| 373 |
allowable_enum = [ |
| 374 |
"On", |
| 375 |
"ForceOff", |
| 376 |
"ForceRestart", |
| 377 |
"GracefulShutdown", |
| 378 |
"PushPowerButton", |
| 379 |
"Nmi", |
| 380 |
"PowerCycle" |
| 381 |
] |
| 382 |
error_msg = INVALID_RESET_TYPE.format("graceful restart", ", ".join(allowable_enum)) |
| 383 |
with pytest.raises(Exception) as exc: |
| 384 |
self.module.is_valid_reset_type(reset_type, allowable_enum, f_module) |
| 385 |
assert exc.value.args[0] == error_msg |
| 386 |
|
| 387 |
|
| 388 |
f_module = self.get_module_mock({"oem_reset_type": {"dell": {"reset_type": "GracefulRestart"}}}) |
| 389 |
allowable_enum = [ |
| 390 |
"PowerCycle" |
| 391 |
] |
| 392 |
error_msg = INVALID_RESET_TYPE_OEM.format(option=reset_type, |
| 393 |
supported_oem_reset_type_values=", ".join(allowable_enum)) |
| 394 |
with pytest.raises(Exception) as exc: |
| 395 |
self.module.is_valid_reset_type(reset_type, allowable_enum, f_module) |
| 396 |
assert exc.value.args[0] == error_msg |
| 397 |
|
| 398 |
def test_is_valid_reset_type_case2(self): |
| 399 |
f_module = self.get_module_mock({"reset_type": "On"}) |
| 400 |
reset_type = "ForceOff" |
| 401 |
allowable_enum = [ |
| 402 |
"On", |
| 403 |
"ForceOff", |
| 404 |
"ForceRestart", |
| 405 |
"GracefulShutdown", |
| 406 |
"PushPowerButton", |
| 407 |
"Nmi", |
| 408 |
"PowerCycle" |
| 409 |
] |
| 410 |
self.module.is_valid_reset_type(reset_type, allowable_enum, f_module) |
| 411 |
|
| 412 |
@pytest.mark.parametrize("val", [{"change_applicable": True, "check_mode_msg": "Changes found to be applied."}, |
| 413 |
{"change_applicable": False, "check_mode_msg": "No Changes found to be applied."}]) |
| 414 |
def test_run_change_power_state_case_with_checkmode(self, mocker, val): |
| 415 |
change_applicable = val["change_applicable"] |
| 416 |
message = val["check_mode_msg"] |
| 417 |
f_module = self.get_module_mock(params={"reset_type": "On"}, check_mode=True) |
| 418 |
self.module.powerstate_map.update({"allowable_enums": [ |
| 419 |
"On", |
| 420 |
"ForceOff", |
| 421 |
"ForceRestart", |
| 422 |
"GracefulShutdown", |
| 423 |
"PushPowerButton", |
| 424 |
"Nmi", |
| 425 |
"PowerCycle" |
| 426 |
]}) |
| 427 |
self.module.powerstate_map.update({'power_uri': '/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem' |
| 428 |
'.Reset'}) |
| 429 |
self.module.powerstate_map.update({'current_state': 'On'}) |
| 430 |
|
| 431 |
mocker.patch(MODULE_PATH + FETCH_PWR_URI, |
| 432 |
return_value=None) |
| 433 |
mocker.patch(MODULE_PATH + VALIDATE_RESET_TYPE, |
| 434 |
return_value=None) |
| 435 |
mocker.patch(MODULE_PATH + 'redfish_powerstate.is_change_applicable_for_power_state', |
| 436 |
return_value=change_applicable) |
| 437 |
|
| 438 |
with pytest.raises(Exception, match=message): |
| 439 |
self.module.run_change_power_state(redfish_connection_mock_for_powerstate, f_module) |
| 440 |
|
| 441 |
@pytest.mark.parametrize("val", [{"change_applicable": True, "status_code": 204}, |
| 442 |
{"change_applicable": False, "status_code": 200}, |
| 443 |
{"change_applicable": True, "status_code": 200}]) |
| 444 |
def test_run_change_power_state_case_without_checkmode(self, mocker, val, redfish_connection_mock_for_powerstate, |
| 445 |
redfish_response_mock): |
| 446 |
redfish_response_mock.status_code = val["status_code"] |
| 447 |
change_applicable = val["change_applicable"] |
| 448 |
f_module = self.get_module_mock(params={"reset_type": "On"}) |
| 449 |
self.module.powerstate_map.update({"allowable_enums": [ |
| 450 |
"On", |
| 451 |
"ForceOff", |
| 452 |
"ForceRestart", |
| 453 |
"GracefulShutdown", |
| 454 |
"PushPowerButton", |
| 455 |
"Nmi", |
| 456 |
"PowerCycle" |
| 457 |
]}) |
| 458 |
self.module.powerstate_map.update({'power_uri': '/redfish/v1/Systems/System.Embedded.1/Actions/ComputerSystem' |
| 459 |
'.Reset'}) |
| 460 |
self.module.powerstate_map.update({'current_state': 'On'}) |
| 461 |
if change_applicable is True: |
| 462 |
if val["status_code"] == 204: |
| 463 |
redfish_response_mock.success = True |
| 464 |
message = "Successfully performed the reset type operation 'On'." |
| 465 |
else: |
| 466 |
redfish_response_mock.success = False |
| 467 |
message = "Unable to perform the reset type operation 'On'." |
| 468 |
else: |
| 469 |
message = "The device is already powered on." |
| 470 |
mocker.patch(MODULE_PATH + FETCH_PWR_URI, |
| 471 |
return_value=None) |
| 472 |
mocker.patch(MODULE_PATH + VALIDATE_RESET_TYPE, |
| 473 |
return_value=None) |
| 474 |
mocker.patch(MODULE_PATH + 'redfish_powerstate.is_change_applicable_for_power_state', |
| 475 |
return_value=change_applicable) |
| 476 |
|
| 477 |
with pytest.raises(Exception, match=message): |
| 478 |
self.module.run_change_power_state(redfish_connection_mock_for_powerstate, f_module) |
| 479 |
|
| 480 |
@pytest.mark.parametrize("exc_type", |
| 481 |
[URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError, |
| 482 |
Exception]) |
| 483 |
def test_main_redfish_powerstate_exception_handling_case(self, exc_type, redfish_default_args, |
| 484 |
redfish_connection_mock_for_powerstate, |
| 485 |
redfish_response_mock, mocker): |
| 486 |
redfish_default_args.update({"reset_type": "On"}) |
| 487 |
redfish_response_mock.status_code = 400 |
| 488 |
redfish_response_mock.success = False |
| 489 |
json_str = to_text(json.dumps({"data": "out"})) |
| 490 |
if exc_type == URLError: |
| 491 |
mocker.patch(MODULE_PATH + 'redfish_powerstate.run_change_power_state', |
| 492 |
side_effect=exc_type("url open error")) |
| 493 |
result = self._run_module(redfish_default_args) |
| 494 |
assert result["unreachable"] is True |
| 495 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 496 |
mocker.patch(MODULE_PATH + 'redfish_powerstate.run_change_power_state', |
| 497 |
side_effect=exc_type("exception message")) |
| 498 |
result = self._run_module(redfish_default_args) |
| 499 |
assert result['failed'] is True |
| 500 |
else: |
| 501 |
mocker.patch(MODULE_PATH + 'redfish_powerstate.run_change_power_state', |
| 502 |
side_effect=exc_type(HTTPS_ADDRESS, 400, 'http error message', |
| 503 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 504 |
result = self._run_module(redfish_default_args) |
| 505 |
assert result['failed'] is True |
| 506 |
assert 'msg' in result |
| 507 |
|
| 508 |
def test_run_change_ac_power_cycle(self, redfish_default_args, redfish_connection_mock_for_powerstate, mocker): |
| 509 |
|
| 510 |
obj1 = MagicMock() |
| 511 |
obj1.json_data = {"FirmwareVersion": "6.10.00"} |
| 512 |
redfish_default_args.update({"oem_reset_type": {"dell": {"reset_type": "PowerCycle"}}}) |
| 513 |
redfish_connection_mock_for_powerstate.invoke_request.return_value = obj1 |
| 514 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 515 |
with pytest.raises(Exception) as exc: |
| 516 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 517 |
assert exc.value.args[0] == "Unable to perform the Virtual AC power-cycle operation because the firmware version is not supported." \ |
| 518 |
" The minimum supported firmware version is '7.00.60'." |
| 519 |
|
| 520 |
|
| 521 |
obj1.json_data = {"Vendor": "Dell"} |
| 522 |
redfish_default_args.update({}) |
| 523 |
redfish_default_args.update({"oem_reset_type": {"Invalid": {}}}) |
| 524 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 525 |
redfish_connection_mock_for_powerstate.invoke_request.return_value = obj1 |
| 526 |
mocker.patch(MODULE_PATH + CHECK_FIRMWARE_VERSION, return_value=None) |
| 527 |
with pytest.raises(Exception) as exc: |
| 528 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 529 |
assert exc.value.args[0] == "The vendor is not supported. The supported vendors" \ |
| 530 |
" are 'Dell'. Enter the valid vendor and retry the operation." |
| 531 |
|
| 532 |
|
| 533 |
redfish_default_args.update({}) |
| 534 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"final_power_state": "Off"}}}) |
| 535 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 536 |
mocker.patch(MODULE_PATH + CHECK_FIRMWARE_VERSION, return_value=None) |
| 537 |
mocker.patch(MODULE_PATH + VALIDATE_VENDOR, return_value=None) |
| 538 |
with pytest.raises(Exception) as exc: |
| 539 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 540 |
assert exc.value.args[0] == "No reset type is specified for the target device. Enter the valid value and retry the operation." |
| 541 |
|
| 542 |
|
| 543 |
redfish_default_args.update({}) |
| 544 |
self.module.powerstate_map["allowable_enums"] = ["PowerCycle"] |
| 545 |
self.module.powerstate_map["power_uri"] = TARGET_URI |
| 546 |
self.module.powerstate_map["current_state"] = "Off" |
| 547 |
self.module.powerstate_map["allowable_power_state"] = ["On", "Off"] |
| 548 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"final_power_state": "Invalid", "reset_type": "PowerCycle"}}}) |
| 549 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 550 |
mocker.patch(MODULE_PATH + CHECK_FIRMWARE_VERSION, return_value=None) |
| 551 |
mocker.patch(MODULE_PATH + VALIDATE_VENDOR, return_value=None) |
| 552 |
mocker.patch(MODULE_PATH + FETCH_PWR_URI, return_value=None) |
| 553 |
with pytest.raises(Exception) as exc: |
| 554 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 555 |
assert exc.value.args[0] == "The target device does not support 'Invalid' operation. The valid values of final power states for device are 'On, Off'." |
| 556 |
|
| 557 |
def setup_mocker(self, mocker): |
| 558 |
mocker.patch(MODULE_PATH + CHECK_FIRMWARE_VERSION, return_value=None) |
| 559 |
mocker.patch(MODULE_PATH + VALIDATE_VENDOR, return_value=None) |
| 560 |
mocker.patch(MODULE_PATH + VALIDATE_RESET_TYPE, return_value=None) |
| 561 |
mocker.patch(MODULE_PATH + VALIDATE_FINAL_PWR_STATE, return_value=None) |
| 562 |
mocker.patch(MODULE_PATH + FETCH_PWR_URI, return_value=None) |
| 563 |
|
| 564 |
def test_run_change_ac_power_cycle_case02(self, redfish_default_args, redfish_connection_mock_for_powerstate, mocker): |
| 565 |
|
| 566 |
redfish_default_args.update({}) |
| 567 |
self.module.powerstate_map["allowable_enums"] = ["PowerCycle"] |
| 568 |
self.module.powerstate_map["power_uri"] = TARGET_URI |
| 569 |
self.module.powerstate_map["current_state"] = "Off" |
| 570 |
self.module.powerstate_map["allowable_power_state"] = ["On", "Off"] |
| 571 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"final_power_state": "On", "reset_type": "PowerCycle"}}}) |
| 572 |
f_module = self.get_module_mock(params=redfish_default_args, check_mode=True) |
| 573 |
self.setup_mocker(mocker) |
| 574 |
with pytest.raises(Exception) as exc: |
| 575 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 576 |
assert exc.value.args[0] == "Changes found to be applied." |
| 577 |
|
| 578 |
|
| 579 |
self.module.powerstate_map["current_state"] = "On" |
| 580 |
f_module = self.get_module_mock(params=redfish_default_args, check_mode=True) |
| 581 |
self.setup_mocker(mocker) |
| 582 |
with pytest.raises(Exception) as exc: |
| 583 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 584 |
assert exc.value.args[0] == "No changes found to be applied because system is in power ON state." |
| 585 |
|
| 586 |
|
| 587 |
redfish_default_args.update({}) |
| 588 |
obj = MagicMock() |
| 589 |
obj.status_code = 204 |
| 590 |
self.module.powerstate_map["current_state"] = "Off" |
| 591 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"final_power_state": "On", "reset_type": "PowerCycle"}}}) |
| 592 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 593 |
redfish_connection_mock_for_powerstate.invoke_request.return_value = obj |
| 594 |
self.setup_mocker(mocker) |
| 595 |
with pytest.raises(Exception) as exc: |
| 596 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 597 |
assert exc.value.args[0] == "Successfully performed the full virtual server AC power-cycle operation."\ |
| 598 |
" Please wait a few minutes, the server will automatically power on." |
| 599 |
|
| 600 |
|
| 601 |
redfish_default_args.update({}) |
| 602 |
obj = MagicMock() |
| 603 |
obj.status_code = 204 |
| 604 |
self.module.powerstate_map["current_state"] = "Off" |
| 605 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"reset_type": "PowerCycle"}}}) |
| 606 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 607 |
redfish_connection_mock_for_powerstate.invoke_request.return_value = obj |
| 608 |
self.setup_mocker(mocker) |
| 609 |
with pytest.raises(Exception) as exc: |
| 610 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 611 |
assert exc.value.args[0] == "Successfully performed the full virtual server AC power-cycle operation." |
| 612 |
|
| 613 |
|
| 614 |
redfish_default_args.update({}) |
| 615 |
obj = MagicMock() |
| 616 |
obj.status_code = 409 |
| 617 |
obj_str = { |
| 618 |
"error": |
| 619 |
{ |
| 620 |
"@Message.ExtendedInfo": [ |
| 621 |
{ |
| 622 |
"Message": "Unable to perform the Virtual AC power-cycle operation because the server is powered on.", |
| 623 |
"MessageId": "IDRAC.2.9.PSU507", |
| 624 |
} |
| 625 |
], |
| 626 |
} |
| 627 |
} |
| 628 |
json_str = to_text(json.dumps(obj_str)) |
| 629 |
self.module.powerstate_map["current_state"] = "Off" |
| 630 |
redfish_default_args.update({"oem_reset_type": {"Dell": {"reset_type": "PowerCycle"}}}) |
| 631 |
f_module = self.get_module_mock(params=redfish_default_args) |
| 632 |
redfish_connection_mock_for_powerstate.invoke_request.side_effect = HTTPError("https://test.com", |
| 633 |
409, "obj", {"accept-type": "application/json"}, |
| 634 |
StringIO(json_str)) |
| 635 |
self.setup_mocker(mocker) |
| 636 |
with pytest.raises(Exception) as exc: |
| 637 |
self.module.run_change_ac_power_cycle(redfish_connection_mock_for_powerstate, f_module) |
| 638 |
assert exc.value.args[0] == "Unable to perform the Virtual AC power-cycle operation because the server is powered on." |