| 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 unittest |
| 8 |
from unittest.mock import patch |
| 9 |
|
| 10 |
from ansible.module_utils import basic |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 12 |
AnsibleExitJson, |
| 13 |
AnsibleFailJson, |
| 14 |
exit_json, |
| 15 |
fail_json, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
import ansible_collections.community.general.plugins.modules.xcc_redfish_command as module |
| 20 |
|
| 21 |
|
| 22 |
def get_bin_path(self, arg, required=False): |
| 23 |
"""Mock AnsibleModule.get_bin_path""" |
| 24 |
return arg |
| 25 |
|
| 26 |
|
| 27 |
class TestXCCRedfishCommand(unittest.TestCase): |
| 28 |
def setUp(self): |
| 29 |
self.mock_module_helper = patch.multiple( |
| 30 |
basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json, get_bin_path=get_bin_path |
| 31 |
) |
| 32 |
self.mock_module_helper.start() |
| 33 |
self.addCleanup(self.mock_module_helper.stop) |
| 34 |
|
| 35 |
def test_module_fail_when_required_args_missing(self): |
| 36 |
with self.assertRaises(AnsibleFailJson): |
| 37 |
with set_module_args({}): |
| 38 |
module.main() |
| 39 |
|
| 40 |
def test_module_fail_when_unknown_category(self): |
| 41 |
with self.assertRaises(AnsibleFailJson): |
| 42 |
with set_module_args( |
| 43 |
{ |
| 44 |
"category": "unknown", |
| 45 |
"command": "VirtualMediaEject", |
| 46 |
"baseuri": "10.245.39.251", |
| 47 |
"username": "USERID", |
| 48 |
"password": "PASSW0RD=21", |
| 49 |
} |
| 50 |
): |
| 51 |
module.main() |
| 52 |
|
| 53 |
def test_module_fail_when_unknown_command(self): |
| 54 |
with self.assertRaises(AnsibleFailJson): |
| 55 |
with set_module_args( |
| 56 |
{ |
| 57 |
"category": "Manager", |
| 58 |
"command": "unknown", |
| 59 |
"baseuri": "10.245.39.251", |
| 60 |
"username": "USERID", |
| 61 |
"password": "PASSW0RD=21", |
| 62 |
} |
| 63 |
): |
| 64 |
module.main() |
| 65 |
|
| 66 |
def test_module_command_VirtualMediaInsert_pass(self): |
| 67 |
with set_module_args( |
| 68 |
{ |
| 69 |
"category": "Manager", |
| 70 |
"command": "VirtualMediaInsert", |
| 71 |
"baseuri": "10.245.39.251", |
| 72 |
"username": "USERID", |
| 73 |
"password": "PASSW0RD=21", |
| 74 |
"timeout": 30, |
| 75 |
"virtual_media": { |
| 76 |
"image_url": "nfs://10.245.52.18:/home/nfs/bootable-sr635-20210111-autorun.iso", |
| 77 |
"media_types": ["CD"], |
| 78 |
"inserted": True, |
| 79 |
"write_protected": True, |
| 80 |
"transfer_protocol_type": "NFS", |
| 81 |
}, |
| 82 |
} |
| 83 |
): |
| 84 |
with patch.object(module.XCCRedfishUtils, "_find_systems_resource") as mock__find_systems_resource: |
| 85 |
mock__find_systems_resource.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 86 |
with patch.object(module.XCCRedfishUtils, "_find_managers_resource") as mock__find_managers_resource: |
| 87 |
mock__find_managers_resource.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 88 |
|
| 89 |
with patch.object(module.XCCRedfishUtils, "virtual_media_insert") as mock_virtual_media_insert: |
| 90 |
mock_virtual_media_insert.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 91 |
|
| 92 |
with self.assertRaises(AnsibleExitJson): |
| 93 |
module.main() |
| 94 |
|
| 95 |
def test_module_command_VirtualMediaEject_pass(self): |
| 96 |
with set_module_args( |
| 97 |
{ |
| 98 |
"category": "Manager", |
| 99 |
"command": "VirtualMediaEject", |
| 100 |
"baseuri": "10.245.39.251", |
| 101 |
"username": "USERID", |
| 102 |
"password": "PASSW0RD=21", |
| 103 |
"timeout": 30, |
| 104 |
"virtual_media": { |
| 105 |
"image_url": "nfs://10.245.52.18:/home/nfs/bootable-sr635-20210111-autorun.iso", |
| 106 |
}, |
| 107 |
} |
| 108 |
): |
| 109 |
with patch.object(module.XCCRedfishUtils, "_find_systems_resource") as mock__find_systems_resource: |
| 110 |
mock__find_systems_resource.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 111 |
with patch.object(module.XCCRedfishUtils, "_find_managers_resource") as mock__find_managers_resource: |
| 112 |
mock__find_managers_resource.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 113 |
|
| 114 |
with patch.object(module.XCCRedfishUtils, "virtual_media_eject") as mock_virtual_media_eject: |
| 115 |
mock_virtual_media_eject.return_value = {"ret": True, "changed": True, "msg": "success"} |
| 116 |
|
| 117 |
with self.assertRaises(AnsibleExitJson): |
| 118 |
module.main() |
| 119 |
|
| 120 |
def test_module_command_VirtualMediaEject_fail_when_required_args_missing(self): |
| 121 |
with self.assertRaises(AnsibleFailJson): |
| 122 |
with set_module_args( |
| 123 |
{ |
| 124 |
"category": "Manager", |
| 125 |
"command": "VirtualMediaEject", |
| 126 |
"baseuri": "10.245.39.251", |
| 127 |
"username": "USERID", |
| 128 |
"password": "PASSW0RD=21", |
| 129 |
} |
| 130 |
): |
| 131 |
module.main() |
| 132 |
|
| 133 |
def test_module_command_GetResource_fail_when_required_args_missing(self): |
| 134 |
with set_module_args( |
| 135 |
{ |
| 136 |
"category": "Raw", |
| 137 |
"command": "GetResource", |
| 138 |
"baseuri": "10.245.39.251", |
| 139 |
"username": "USERID", |
| 140 |
"password": "PASSW0RD=21", |
| 141 |
} |
| 142 |
): |
| 143 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 144 |
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 145 |
|
| 146 |
with self.assertRaises(AnsibleFailJson): |
| 147 |
module.main() |
| 148 |
|
| 149 |
def test_module_command_GetResource_fail_when_get_return_false(self): |
| 150 |
with set_module_args( |
| 151 |
{ |
| 152 |
"category": "Raw", |
| 153 |
"command": "GetResource", |
| 154 |
"baseuri": "10.245.39.251", |
| 155 |
"username": "USERID", |
| 156 |
"password": "PASSW0RD=21", |
| 157 |
"resource_uri": "/redfish/v1/testuri", |
| 158 |
} |
| 159 |
): |
| 160 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 161 |
mock_get_request.return_value = {"ret": False, "msg": "404 error"} |
| 162 |
|
| 163 |
with self.assertRaises(AnsibleFailJson): |
| 164 |
module.main() |
| 165 |
|
| 166 |
def test_module_command_GetResource_pass(self): |
| 167 |
with set_module_args( |
| 168 |
{ |
| 169 |
"category": "Raw", |
| 170 |
"command": "GetResource", |
| 171 |
"baseuri": "10.245.39.251", |
| 172 |
"username": "USERID", |
| 173 |
"password": "PASSW0RD=21", |
| 174 |
"resource_uri": "/redfish/v1/testuri", |
| 175 |
} |
| 176 |
): |
| 177 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 178 |
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 179 |
|
| 180 |
with self.assertRaises(AnsibleExitJson): |
| 181 |
module.main() |
| 182 |
|
| 183 |
def test_module_command_GetCollectionResource_fail_when_required_args_missing(self): |
| 184 |
with set_module_args( |
| 185 |
{ |
| 186 |
"category": "Raw", |
| 187 |
"command": "GetCollectionResource", |
| 188 |
"baseuri": "10.245.39.251", |
| 189 |
"username": "USERID", |
| 190 |
"password": "PASSW0RD=21", |
| 191 |
} |
| 192 |
): |
| 193 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 194 |
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 195 |
|
| 196 |
with self.assertRaises(AnsibleFailJson): |
| 197 |
module.main() |
| 198 |
|
| 199 |
def test_module_command_GetCollectionResource_fail_when_get_return_false(self): |
| 200 |
with set_module_args( |
| 201 |
{ |
| 202 |
"category": "Raw", |
| 203 |
"command": "GetCollectionResource", |
| 204 |
"baseuri": "10.245.39.251", |
| 205 |
"username": "USERID", |
| 206 |
"password": "PASSW0RD=21", |
| 207 |
"resource_uri": "/redfish/v1/testuri", |
| 208 |
} |
| 209 |
): |
| 210 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 211 |
mock_get_request.return_value = {"ret": False, "msg": "404 error"} |
| 212 |
|
| 213 |
with self.assertRaises(AnsibleFailJson): |
| 214 |
module.main() |
| 215 |
|
| 216 |
def test_module_command_GetCollectionResource_fail_when_get_not_colection(self): |
| 217 |
with set_module_args( |
| 218 |
{ |
| 219 |
"category": "Raw", |
| 220 |
"command": "GetCollectionResource", |
| 221 |
"baseuri": "10.245.39.251", |
| 222 |
"username": "USERID", |
| 223 |
"password": "PASSW0RD=21", |
| 224 |
"resource_uri": "/redfish/v1/testuri", |
| 225 |
} |
| 226 |
): |
| 227 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 228 |
mock_get_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 229 |
|
| 230 |
with self.assertRaises(AnsibleFailJson): |
| 231 |
module.main() |
| 232 |
|
| 233 |
def test_module_command_GetCollectionResource_pass_when_get_empty_collection(self): |
| 234 |
with set_module_args( |
| 235 |
{ |
| 236 |
"category": "Raw", |
| 237 |
"command": "GetCollectionResource", |
| 238 |
"baseuri": "10.245.39.251", |
| 239 |
"username": "USERID", |
| 240 |
"password": "PASSW0RD=21", |
| 241 |
"resource_uri": "/redfish/v1/testuri", |
| 242 |
} |
| 243 |
): |
| 244 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 245 |
mock_get_request.return_value = {"ret": True, "data": {"Members": [], "[email protected]": 0}} |
| 246 |
|
| 247 |
with self.assertRaises(AnsibleExitJson): |
| 248 |
module.main() |
| 249 |
|
| 250 |
def test_module_command_GetCollectionResource_pass_when_get_collection(self): |
| 251 |
with set_module_args( |
| 252 |
{ |
| 253 |
"category": "Raw", |
| 254 |
"command": "GetCollectionResource", |
| 255 |
"baseuri": "10.245.39.251", |
| 256 |
"username": "USERID", |
| 257 |
"password": "PASSW0RD=21", |
| 258 |
"resource_uri": "/redfish/v1/testuri", |
| 259 |
} |
| 260 |
): |
| 261 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 262 |
mock_get_request.return_value = { |
| 263 |
"ret": True, |
| 264 |
"data": {"Members": [{"@odata.id": "/redfish/v1/testuri/1"}], "[email protected]": 1}, |
| 265 |
} |
| 266 |
|
| 267 |
with self.assertRaises(AnsibleExitJson): |
| 268 |
module.main() |
| 269 |
|
| 270 |
def test_module_command_PatchResource_fail_when_required_args_missing(self): |
| 271 |
with set_module_args( |
| 272 |
{ |
| 273 |
"category": "Raw", |
| 274 |
"command": "PatchResource", |
| 275 |
"baseuri": "10.245.39.251", |
| 276 |
"username": "USERID", |
| 277 |
"password": "PASSW0RD=21", |
| 278 |
} |
| 279 |
): |
| 280 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 281 |
mock_get_request.return_value = { |
| 282 |
"ret": True, |
| 283 |
"data": {"teststr": "xxxx", "@odata.etag": "27f6eb13fa1c28a2711"}, |
| 284 |
} |
| 285 |
|
| 286 |
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request: |
| 287 |
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 288 |
|
| 289 |
with self.assertRaises(AnsibleFailJson): |
| 290 |
module.main() |
| 291 |
|
| 292 |
def test_module_command_PatchResource_fail_when_required_args_missing_no_requestbody(self): |
| 293 |
with set_module_args( |
| 294 |
{ |
| 295 |
"category": "Raw", |
| 296 |
"command": "PatchResource", |
| 297 |
"baseuri": "10.245.39.251", |
| 298 |
"username": "USERID", |
| 299 |
"password": "PASSW0RD=21", |
| 300 |
"resource_uri": "/redfish/v1/testuri", |
| 301 |
} |
| 302 |
): |
| 303 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 304 |
mock_get_request.return_value = { |
| 305 |
"ret": True, |
| 306 |
"data": {"teststr": "xxxx", "@odata.etag": "27f6eb13fa1c28a2711"}, |
| 307 |
} |
| 308 |
|
| 309 |
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request: |
| 310 |
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 311 |
|
| 312 |
with self.assertRaises(AnsibleFailJson): |
| 313 |
module.main() |
| 314 |
|
| 315 |
def test_module_command_PatchResource_fail_when_noexisting_property_in_requestbody(self): |
| 316 |
with set_module_args( |
| 317 |
{ |
| 318 |
"category": "Raw", |
| 319 |
"command": "PatchResource", |
| 320 |
"baseuri": "10.245.39.251", |
| 321 |
"username": "USERID", |
| 322 |
"password": "PASSW0RD=21", |
| 323 |
"resource_uri": "/redfish/v1/testuri", |
| 324 |
"request_body": {"teststr": "yyyy", "otherkey": "unknownkey"}, |
| 325 |
} |
| 326 |
): |
| 327 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 328 |
mock_get_request.return_value = { |
| 329 |
"ret": True, |
| 330 |
"data": {"teststr": "xxxx", "@odata.etag": "27f6eb13fa1c28a2711"}, |
| 331 |
} |
| 332 |
|
| 333 |
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request: |
| 334 |
mock_patch_request.return_value = {"ret": True, "data": {"teststr": "xxxx"}} |
| 335 |
|
| 336 |
with self.assertRaises(AnsibleFailJson): |
| 337 |
module.main() |
| 338 |
|
| 339 |
def test_module_command_PatchResource_fail_when_get_return_false(self): |
| 340 |
with set_module_args( |
| 341 |
{ |
| 342 |
"category": "Raw", |
| 343 |
"command": "PatchResource", |
| 344 |
"baseuri": "10.245.39.251", |
| 345 |
"username": "USERID", |
| 346 |
"password": "PASSW0RD=21", |
| 347 |
"resource_uri": "/redfish/v1/testuri", |
| 348 |
"request_body": {"teststr": "yyyy"}, |
| 349 |
} |
| 350 |
): |
| 351 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 352 |
mock_get_request.return_value = { |
| 353 |
"ret": True, |
| 354 |
"data": {"teststr": "xxxx", "@odata.etag": "27f6eb13fa1c28a2711"}, |
| 355 |
} |
| 356 |
|
| 357 |
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request: |
| 358 |
mock_patch_request.return_value = {"ret": False, "msg": "500 internal error"} |
| 359 |
|
| 360 |
with self.assertRaises(AnsibleFailJson): |
| 361 |
module.main() |
| 362 |
|
| 363 |
def test_module_command_PatchResource_pass(self): |
| 364 |
with set_module_args( |
| 365 |
{ |
| 366 |
"category": "Raw", |
| 367 |
"command": "PatchResource", |
| 368 |
"baseuri": "10.245.39.251", |
| 369 |
"username": "USERID", |
| 370 |
"password": "PASSW0RD=21", |
| 371 |
"resource_uri": "/redfish/v1/testuri", |
| 372 |
"request_body": {"teststr": "yyyy"}, |
| 373 |
} |
| 374 |
): |
| 375 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 376 |
mock_get_request.return_value = { |
| 377 |
"ret": True, |
| 378 |
"data": {"teststr": "xxxx", "@odata.etag": "27f6eb13fa1c28a2711"}, |
| 379 |
} |
| 380 |
|
| 381 |
with patch.object(module.XCCRedfishUtils, "patch_request") as mock_patch_request: |
| 382 |
mock_patch_request.return_value = { |
| 383 |
"ret": True, |
| 384 |
"data": {"teststr": "yyyy", "@odata.etag": "322e0d45d9572723c98"}, |
| 385 |
} |
| 386 |
|
| 387 |
with self.assertRaises(AnsibleExitJson): |
| 388 |
module.main() |
| 389 |
|
| 390 |
def test_module_command_PostResource_fail_when_required_args_missing(self): |
| 391 |
with set_module_args( |
| 392 |
{ |
| 393 |
"category": "Raw", |
| 394 |
"command": "PostResource", |
| 395 |
"baseuri": "10.245.39.251", |
| 396 |
"username": "USERID", |
| 397 |
"password": "PASSW0RD=21", |
| 398 |
} |
| 399 |
): |
| 400 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 401 |
mock_get_request.return_value = { |
| 402 |
"ret": True, |
| 403 |
"data": { |
| 404 |
"Actions": { |
| 405 |
"#Bios.ChangePassword": { |
| 406 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 407 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 408 |
"title": "ChangePassword", |
| 409 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 410 |
}, |
| 411 |
"#Bios.ResetBios": { |
| 412 |
"title": "ResetBios", |
| 413 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 414 |
}, |
| 415 |
}, |
| 416 |
}, |
| 417 |
} |
| 418 |
|
| 419 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 420 |
mock_post_request.return_value = {"ret": True} |
| 421 |
|
| 422 |
with self.assertRaises(AnsibleFailJson): |
| 423 |
module.main() |
| 424 |
|
| 425 |
def test_module_command_PostResource_fail_when_invalid_resourceuri(self): |
| 426 |
with set_module_args( |
| 427 |
{ |
| 428 |
"category": "Raw", |
| 429 |
"command": "PostResource", |
| 430 |
"baseuri": "10.245.39.251", |
| 431 |
"username": "USERID", |
| 432 |
"password": "PASSW0RD=21", |
| 433 |
"resource_uri": "/redfish/v1/testuri", |
| 434 |
} |
| 435 |
): |
| 436 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 437 |
mock_get_request.return_value = { |
| 438 |
"ret": True, |
| 439 |
"data": { |
| 440 |
"Actions": { |
| 441 |
"#Bios.ChangePassword": { |
| 442 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 443 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 444 |
"title": "ChangePassword", |
| 445 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 446 |
}, |
| 447 |
"#Bios.ResetBios": { |
| 448 |
"title": "ResetBios", |
| 449 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 450 |
}, |
| 451 |
}, |
| 452 |
}, |
| 453 |
} |
| 454 |
|
| 455 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 456 |
mock_post_request.return_value = {"ret": True} |
| 457 |
|
| 458 |
with self.assertRaises(AnsibleFailJson): |
| 459 |
module.main() |
| 460 |
|
| 461 |
def test_module_command_PostResource_fail_when_no_requestbody(self): |
| 462 |
with set_module_args( |
| 463 |
{ |
| 464 |
"category": "Raw", |
| 465 |
"command": "PostResource", |
| 466 |
"baseuri": "10.245.39.251", |
| 467 |
"username": "USERID", |
| 468 |
"password": "PASSW0RD=21", |
| 469 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 470 |
} |
| 471 |
): |
| 472 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 473 |
mock_get_request.return_value = { |
| 474 |
"ret": True, |
| 475 |
"data": { |
| 476 |
"Actions": { |
| 477 |
"#Bios.ChangePassword": { |
| 478 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 479 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 480 |
"title": "ChangePassword", |
| 481 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 482 |
}, |
| 483 |
"#Bios.ResetBios": { |
| 484 |
"title": "ResetBios", |
| 485 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 486 |
}, |
| 487 |
}, |
| 488 |
}, |
| 489 |
} |
| 490 |
|
| 491 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 492 |
mock_post_request.return_value = {"ret": True} |
| 493 |
|
| 494 |
with self.assertRaises(AnsibleFailJson): |
| 495 |
module.main() |
| 496 |
|
| 497 |
def test_module_command_PostResource_fail_when_no_requestbody_2(self): |
| 498 |
with set_module_args( |
| 499 |
{ |
| 500 |
"category": "Raw", |
| 501 |
"command": "PostResource", |
| 502 |
"baseuri": "10.245.39.251", |
| 503 |
"username": "USERID", |
| 504 |
"password": "PASSW0RD=21", |
| 505 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 506 |
} |
| 507 |
): |
| 508 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 509 |
mock_get_request.return_value = { |
| 510 |
"ret": True, |
| 511 |
"data": { |
| 512 |
"Actions": { |
| 513 |
"#Bios.ChangePassword": { |
| 514 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 515 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 516 |
"title": "ChangePassword", |
| 517 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 518 |
}, |
| 519 |
"#Bios.ResetBios": { |
| 520 |
"title": "ResetBios", |
| 521 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 522 |
}, |
| 523 |
}, |
| 524 |
}, |
| 525 |
} |
| 526 |
|
| 527 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 528 |
mock_post_request.return_value = {"ret": True} |
| 529 |
|
| 530 |
with self.assertRaises(AnsibleFailJson): |
| 531 |
module.main() |
| 532 |
|
| 533 |
def test_module_command_PostResource_fail_when_requestbody_mismatch_with_data_from_actioninfo_uri(self): |
| 534 |
with set_module_args( |
| 535 |
{ |
| 536 |
"category": "Raw", |
| 537 |
"command": "PostResource", |
| 538 |
"baseuri": "10.245.39.251", |
| 539 |
"username": "USERID", |
| 540 |
"password": "PASSW0RD=21", |
| 541 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 542 |
"request_body": {"PasswordName": "UefiAdminPassword", "NewPassword": "PASSW0RD=="}, |
| 543 |
} |
| 544 |
): |
| 545 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 546 |
mock_get_request.return_value = { |
| 547 |
"ret": True, |
| 548 |
"data": { |
| 549 |
"Parameters": [], |
| 550 |
"Actions": { |
| 551 |
"#Bios.ChangePassword": { |
| 552 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 553 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 554 |
"title": "ChangePassword", |
| 555 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 556 |
}, |
| 557 |
"#Bios.ResetBios": { |
| 558 |
"title": "ResetBios", |
| 559 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 560 |
}, |
| 561 |
}, |
| 562 |
}, |
| 563 |
} |
| 564 |
|
| 565 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 566 |
mock_post_request.return_value = {"ret": True} |
| 567 |
|
| 568 |
with self.assertRaises(AnsibleFailJson): |
| 569 |
module.main() |
| 570 |
|
| 571 |
def test_module_command_PostResource_fail_when_get_return_false(self): |
| 572 |
with set_module_args( |
| 573 |
{ |
| 574 |
"category": "Raw", |
| 575 |
"command": "PostResource", |
| 576 |
"baseuri": "10.245.39.251", |
| 577 |
"username": "USERID", |
| 578 |
"password": "PASSW0RD=21", |
| 579 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 580 |
"request_body": {"PasswordName": "UefiAdminPassword", "NewPassword": "PASSW0RD=="}, |
| 581 |
} |
| 582 |
): |
| 583 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 584 |
mock_get_request.return_value = {"ret": False, "msg": "404 error"} |
| 585 |
|
| 586 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 587 |
mock_post_request.return_value = {"ret": True} |
| 588 |
|
| 589 |
with self.assertRaises(AnsibleFailJson): |
| 590 |
module.main() |
| 591 |
|
| 592 |
def test_module_command_PostResource_fail_when_post_return_false(self): |
| 593 |
with set_module_args( |
| 594 |
{ |
| 595 |
"category": "Raw", |
| 596 |
"command": "PostResource", |
| 597 |
"baseuri": "10.245.39.251", |
| 598 |
"username": "USERID", |
| 599 |
"password": "PASSW0RD=21", |
| 600 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 601 |
"request_body": {}, |
| 602 |
} |
| 603 |
): |
| 604 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 605 |
mock_get_request.return_value = { |
| 606 |
"ret": True, |
| 607 |
"data": { |
| 608 |
"Actions": { |
| 609 |
"#Bios.ChangePassword": { |
| 610 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 611 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 612 |
"title": "ChangePassword", |
| 613 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 614 |
}, |
| 615 |
"#Bios.ResetBios": { |
| 616 |
"title": "ResetBios", |
| 617 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 618 |
}, |
| 619 |
}, |
| 620 |
}, |
| 621 |
} |
| 622 |
|
| 623 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 624 |
mock_post_request.return_value = {"ret": False, "msg": "500 internal error"} |
| 625 |
|
| 626 |
with self.assertRaises(AnsibleFailJson): |
| 627 |
module.main() |
| 628 |
|
| 629 |
def test_module_command_PostResource_pass(self): |
| 630 |
with set_module_args( |
| 631 |
{ |
| 632 |
"category": "Raw", |
| 633 |
"command": "PostResource", |
| 634 |
"baseuri": "10.245.39.251", |
| 635 |
"username": "USERID", |
| 636 |
"password": "PASSW0RD=21", |
| 637 |
"resource_uri": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 638 |
"request_body": {}, |
| 639 |
} |
| 640 |
): |
| 641 |
with patch.object(module.XCCRedfishUtils, "get_request") as mock_get_request: |
| 642 |
mock_get_request.return_value = { |
| 643 |
"ret": True, |
| 644 |
"data": { |
| 645 |
"Actions": { |
| 646 |
"#Bios.ChangePassword": { |
| 647 |
"@Redfish.ActionInfo": "/redfish/v1/Systems/1/Bios/ChangePasswordActionInfo", |
| 648 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ChangePassword", |
| 649 |
"title": "ChangePassword", |
| 650 |
"[email protected]": ["UefiAdminPassword", "UefiPowerOnPassword"], |
| 651 |
}, |
| 652 |
"#Bios.ResetBios": { |
| 653 |
"title": "ResetBios", |
| 654 |
"target": "/redfish/v1/Systems/1/Bios/Actions/Bios.ResetBios", |
| 655 |
}, |
| 656 |
}, |
| 657 |
}, |
| 658 |
} |
| 659 |
|
| 660 |
with patch.object(module.XCCRedfishUtils, "post_request") as mock_post_request: |
| 661 |
mock_post_request.return_value = {"ret": True, "msg": "post success"} |
| 662 |
|
| 663 |
with self.assertRaises(AnsibleExitJson): |
| 664 |
module.main() |