| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
from __future__ import (absolute_import, division, print_function) |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
import pytest |
| 15 |
import json |
| 16 |
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_user |
| 17 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 18 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 19 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 20 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import AnsibleFailJSonException |
| 21 |
from unittest.mock import MagicMock |
| 22 |
from ansible.module_utils._text import to_text |
| 23 |
from io import StringIO |
| 24 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.utils \ |
| 25 |
import AnsibleFailJson |
| 26 |
|
| 27 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 28 |
VERSION = "3.60.60.60" |
| 29 |
VERSION13G = "2.70.70.70" |
| 30 |
HARDWARE_13G = 13 |
| 31 |
HARDWARE_14G = 14 |
| 32 |
GET_SERVER_VERSION_KEY = "_get_server_version" |
| 33 |
SLOT_API = "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/{0}/" |
| 34 |
CHANGES_FOUND = "Changes found to commit!" |
| 35 |
SLEEP_PATH = 'idrac_user.time.sleep' |
| 36 |
USERNAME2 = "Users.2#UserName" |
| 37 |
GET_PAYLOAD = "idrac_user.get_payload" |
| 38 |
PAYLOAD_XML = "idrac_user.convert_payload_xml" |
| 39 |
XML_DATA = "<xml-data>" |
| 40 |
USERNAME1 = "Users.1#UserName" |
| 41 |
IMPORT_SCP = "idrac_user.iDRACRedfishAPI.import_scp" |
| 42 |
USER2 = "User.2#UserName" |
| 43 |
SUCCESS_CREATED = "Successfully created a request." |
| 44 |
SUCCESS_MSG = "Successfully created user account." |
| 45 |
SUCCESS_UPDATED = "Successfully updated user account." |
| 46 |
INVOKE_REQUEST = "idrac_user.iDRACRedfishAPI.invoke_request" |
| 47 |
CM_ACCOUNT = "idrac_user.create_or_modify_account" |
| 48 |
USER_PRIVILAGE = "Users.1#Privilege" |
| 49 |
MAX_USERS_MSG = "Maximum number of users reached. Delete a user \ |
| 50 |
account and retry the operation." |
| 51 |
CREATE_USER_DICT = {"state": "present", |
| 52 |
"new_user_name": "new_user_name", |
| 53 |
"user_name": "test", |
| 54 |
"user_password": "password", |
| 55 |
"privilege": "Administrator", |
| 56 |
"ipmi_lan_privilege": "Administrator", |
| 57 |
"ipmi_serial_privilege": "Administrator", |
| 58 |
"enable": True, |
| 59 |
"sol_enable": True, |
| 60 |
"protocol_enable": True, |
| 61 |
"authentication_protocol": "MD5", |
| 62 |
"privacy_protocol": "AES"} |
| 63 |
ATTRIBUTE_URI = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1/" |
| 64 |
ATTRIBUTE_URI_10 = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DellAttributes/iDRAC.Embedded.1/" |
| 65 |
INVALID_USERNAME_FORMAT = "{username} is not a valid username." |
| 66 |
|
| 67 |
|
| 68 |
class TestIDRACUser(FakeAnsibleModule): |
| 69 |
module = idrac_user |
| 70 |
|
| 71 |
@pytest.fixture |
| 72 |
def idrac_user_mock(self): |
| 73 |
idrac_obj = MagicMock() |
| 74 |
return idrac_obj |
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def idrac_connection_user_mock(self, mocker, idrac_user_mock): |
| 78 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'idrac_user.iDRACRedfishAPI', |
| 79 |
return_value=idrac_user_mock) |
| 80 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_user_mock |
| 81 |
return idrac_conn_mock |
| 82 |
|
| 83 |
def test_get_payload(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 84 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 85 |
"user_name": "test", "user_password": "password", |
| 86 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 87 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 88 |
"sol_enable": True, "protocol_enable": True, |
| 89 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 90 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 91 |
idrac = idrac_connection_user_mock |
| 92 |
|
| 93 |
|
| 94 |
resp = self.module.get_payload(f_module, idrac, 1, HARDWARE_14G, action="update") |
| 95 |
assert resp["Users.1.UserName"] == idrac_default_args["new_user_name"] |
| 96 |
|
| 97 |
|
| 98 |
resp = self.module.get_payload(f_module, idrac, 1, 17, action="update") |
| 99 |
assert resp["Users.1.UserName"] == idrac_default_args["new_user_name"] |
| 100 |
|
| 101 |
|
| 102 |
idrac_default_args['privilege'] = None |
| 103 |
resp = self.module.get_payload(f_module, idrac, 1, 17, action="update") |
| 104 |
assert resp["Users.1.UserName"] == idrac_default_args["new_user_name"] |
| 105 |
|
| 106 |
|
| 107 |
idrac_default_args['privilege'] = "None" |
| 108 |
|
| 109 |
with pytest.raises(AnsibleFailJSonException) as err: |
| 110 |
self.module.get_payload(f_module, idrac, 1, 17, action="update") |
| 111 |
assert err.value.args[0] == \ |
| 112 |
"None is not an applicable value for privilege in iDRAC 17G and later." |
| 113 |
|
| 114 |
def test_get_payload_2(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 115 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 116 |
"user_name": "test", "user_password": "password", |
| 117 |
"privilege": "Administrator", "custom_privilege": 17, "ipmi_lan_privilege": "Administrator", |
| 118 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 119 |
"sol_enable": True, "protocol_enable": True, |
| 120 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 121 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 122 |
idrac = idrac_connection_user_mock |
| 123 |
resp = self.module.get_payload(f_module, idrac, 1, 16) |
| 124 |
assert resp["Users.1.Privilege"] == idrac_default_args["custom_privilege"] |
| 125 |
|
| 126 |
def test_convert_payload_xml(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 127 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 128 |
"user_name": "test", "user_password": "password", |
| 129 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 130 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 131 |
"sol_enable": True, "protocol_enable": True, |
| 132 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 133 |
payload = {"Users.1.UserName": idrac_default_args["user_name"], |
| 134 |
"Users.1.Password": idrac_default_args["user_password"], |
| 135 |
"Users.1.Enable": idrac_default_args["enable"], |
| 136 |
"Users.1.Privilege": idrac_default_args["privilege"], |
| 137 |
"Users.1.IpmiLanPrivilege": idrac_default_args["ipmi_lan_privilege"], |
| 138 |
"Users.1.IpmiSerialPrivilege": idrac_default_args["ipmi_serial_privilege"], |
| 139 |
"Users.1.SolEnable": idrac_default_args["sol_enable"], |
| 140 |
"Users.1.ProtocolEnable": idrac_default_args["protocol_enable"], |
| 141 |
"Users.1.AuthenticationProtocol": idrac_default_args["authentication_protocol"], |
| 142 |
"Users.1.PrivacyProtocol": idrac_default_args["privacy_protocol"]} |
| 143 |
_xml, json_payload = self.module.convert_payload_xml(payload) |
| 144 |
assert json_payload["Users.1#SolEnable"] is True |
| 145 |
|
| 146 |
def test_remove_user_account_check_mode_1(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 147 |
idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None, |
| 148 |
"user_password": None, "privilege": None, "ipmi_lan_privilege": None, |
| 149 |
"ipmi_serial_privilege": None, "enable": False, "sol_enable": False, |
| 150 |
"protocol_enable": False, "authentication_protocol": "SHA", |
| 151 |
"privacy_protocol": "AES"}) |
| 152 |
f_module = self.get_module_mock( |
| 153 |
params=idrac_default_args, check_mode=True) |
| 154 |
slot_id = 1 |
| 155 |
slot_uri = SLOT_API.format(slot_id) |
| 156 |
with pytest.raises(Exception) as exc: |
| 157 |
self.module.remove_user_account( |
| 158 |
f_module, idrac_connection_user_mock, slot_uri, slot_id, HARDWARE_14G) |
| 159 |
assert exc.value.args[0] == CHANGES_FOUND |
| 160 |
|
| 161 |
def test_remove_user_account_check_mode_2(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 162 |
idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None, |
| 163 |
"user_password": None, "privilege": None, "ipmi_lan_privilege": None, |
| 164 |
"ipmi_serial_privilege": None, "enable": False, "sol_enable": False, |
| 165 |
"protocol_enable": False, "authentication_protocol": "SHA", |
| 166 |
"privacy_protocol": "AES"}) |
| 167 |
f_module = self.get_module_mock( |
| 168 |
params=idrac_default_args, check_mode=True) |
| 169 |
with pytest.raises(Exception) as exc: |
| 170 |
self.module.remove_user_account( |
| 171 |
f_module, idrac_connection_user_mock, None, None, HARDWARE_14G) |
| 172 |
assert exc.value.args[0] == "No changes found to commit!" |
| 173 |
|
| 174 |
def test_remove_user_account_check_mode_3(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 175 |
idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None, |
| 176 |
"user_password": None, "privilege": None, "ipmi_lan_privilege": None, |
| 177 |
"ipmi_serial_privilege": None, "enable": False, "sol_enable": False, |
| 178 |
"protocol_enable": False, "authentication_protocol": "SHA", |
| 179 |
"privacy_protocol": "AES"}) |
| 180 |
idrac_connection_user_mock.remove_user_account.return_value = { |
| 181 |
"success": True} |
| 182 |
f_module = self.get_module_mock( |
| 183 |
params=idrac_default_args, check_mode=False) |
| 184 |
slot_id = 1 |
| 185 |
slot_uri = SLOT_API.format(slot_id) |
| 186 |
mocker.patch(MODULE_PATH + SLEEP_PATH, return_value=None) |
| 187 |
self.module.remove_user_account( |
| 188 |
f_module, idrac_connection_user_mock, slot_uri, slot_id, HARDWARE_14G) |
| 189 |
|
| 190 |
def test_remove_user_account_check_mode_4(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 191 |
idrac_default_args.update({"state": "absent", "user_name": "user_name", "new_user_name": None, |
| 192 |
"user_password": None, "privilege": None, "ipmi_lan_privilege": None, |
| 193 |
"ipmi_serial_privilege": None, "enable": False, "sol_enable": False, |
| 194 |
"protocol_enable": False, "authentication_protocol": "SHA", |
| 195 |
"privacy_protocol": "AES"}) |
| 196 |
idrac_connection_user_mock.remove_user_account.return_value = { |
| 197 |
"success": True} |
| 198 |
f_module = self.get_module_mock( |
| 199 |
params=idrac_default_args, check_mode=False) |
| 200 |
with pytest.raises(Exception) as exc: |
| 201 |
self.module.remove_user_account( |
| 202 |
f_module, idrac_connection_user_mock, None, None, HARDWARE_14G) |
| 203 |
assert exc.value.args[0] == 'The user account is absent.' |
| 204 |
|
| 205 |
def test_get_user_account_1(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 206 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 207 |
"user_name": "test", "user_password": "password", |
| 208 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 209 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 210 |
"sol_enable": True, "protocol_enable": True, |
| 211 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 212 |
mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.export_scp", |
| 213 |
return_value=MagicMock()) |
| 214 |
mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.get_idrac_local_account_attr", |
| 215 |
return_value={USERNAME2: "test_user", "Users.3#UserName": ""}) |
| 216 |
f_module = self.get_module_mock( |
| 217 |
params=idrac_default_args, check_mode=False) |
| 218 |
response = self.module.get_user_account( |
| 219 |
f_module, idrac_connection_user_mock) |
| 220 |
assert response[0][USERNAME2] == "test_user" |
| 221 |
assert response[3] == 3 |
| 222 |
assert response[4] == "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/3" |
| 223 |
|
| 224 |
def test_get_user_account_2(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 225 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 226 |
"user_name": "test", "user_password": "password", |
| 227 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 228 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 229 |
"sol_enable": True, "protocol_enable": True, |
| 230 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 231 |
mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.export_scp", |
| 232 |
return_value=MagicMock()) |
| 233 |
mocker.patch(MODULE_PATH + "idrac_user.iDRACRedfishAPI.get_idrac_local_account_attr", |
| 234 |
return_value={USERNAME2: "test_user", "Users.3#UserName": "test"}) |
| 235 |
f_module = self.get_module_mock( |
| 236 |
params=idrac_default_args, check_mode=False) |
| 237 |
response = self.module.get_user_account( |
| 238 |
f_module, idrac_connection_user_mock) |
| 239 |
assert response[2] == 3 |
| 240 |
assert response[1] == "/redfish/v1/Managers/iDRAC.Embedded.1/Accounts/3" |
| 241 |
|
| 242 |
def test_get_user_account_invalid_name(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 243 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 244 |
"user_name": "", "user_password": "password", |
| 245 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 246 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 247 |
"sol_enable": True, "protocol_enable": True, |
| 248 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 249 |
f_module = self.get_module_mock( |
| 250 |
params=idrac_default_args, check_mode=False) |
| 251 |
with pytest.raises(Exception) as err: |
| 252 |
self.module.get_user_account(f_module, idrac_connection_user_mock) |
| 253 |
assert err.value.args[0] == "User name is not valid." |
| 254 |
|
| 255 |
@pytest.mark.parametrize("params", [ |
| 256 |
{"ret_val": SUCCESS_MSG, "empty_slot_id": 2, |
| 257 |
"empty_slot_uri": SLOT_API.format(2)}, |
| 258 |
{"ret_val": SUCCESS_UPDATED, "slot_id": 2, |
| 259 |
"slot_uri": SLOT_API.format(2)}, |
| 260 |
{"firm_ver": (14, VERSION, HARDWARE_14G), "ret_val": SUCCESS_MSG, |
| 261 |
"empty_slot_id": 2, "empty_slot_uri": SLOT_API.format(2)}, |
| 262 |
{"firm_ver": (14, VERSION, HARDWARE_14G), "ret_val": SUCCESS_UPDATED, |
| 263 |
"slot_id": 2, "slot_uri": SLOT_API.format(2)}, |
| 264 |
{"firm_ver": (14, VERSION, HARDWARE_14G), "ret_val": SUCCESS_UPDATED, "slot_id": 2, "slot_uri": SLOT_API.format(2), |
| 265 |
"empty_slot_id": 2, "empty_slot_uri": SLOT_API.format(2)}, |
| 266 |
]) |
| 267 |
def test_create_or_modify_account(self, idrac_connection_user_mock, idrac_default_args, mocker, params): |
| 268 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 269 |
"user_name": "test", "user_password": "password", |
| 270 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 271 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 272 |
"sol_enable": True, "protocol_enable": True, |
| 273 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 274 |
f_module = self.get_module_mock( |
| 275 |
params=idrac_default_args, check_mode=False) |
| 276 |
idrac_connection_user_mock.get_server_generation = params.get( |
| 277 |
"firm_ver", (13, VERSION13G, HARDWARE_13G)) |
| 278 |
mocker.patch(MODULE_PATH + GET_PAYLOAD, |
| 279 |
return_value={USERNAME2: "test_user"}) |
| 280 |
mocker.patch(MODULE_PATH + PAYLOAD_XML, |
| 281 |
return_value=(XML_DATA, {USERNAME2: "test_user"})) |
| 282 |
mocker.patch(MODULE_PATH + IMPORT_SCP, |
| 283 |
return_value={"Message": SUCCESS_CREATED}) |
| 284 |
mocker.patch(MODULE_PATH + SLEEP_PATH, return_value=None) |
| 285 |
mocker.patch(MODULE_PATH + INVOKE_REQUEST, |
| 286 |
return_value={"Message": SUCCESS_CREATED}) |
| 287 |
|
| 288 |
empty_slot_id = params.get("empty_slot_id", None) |
| 289 |
empty_slot_uri = params.get("empty_slot_uri", None) |
| 290 |
slot_id = params.get("slot_id", None) |
| 291 |
slot_uri = params.get("slot_uri", None) |
| 292 |
user_attr = {USER2: "test_user"} |
| 293 |
gen = 13 |
| 294 |
|
| 295 |
response = self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id, |
| 296 |
empty_slot_id, empty_slot_uri, user_attr, gen) |
| 297 |
assert response[1] == params.get("ret_val") |
| 298 |
|
| 299 |
@pytest.mark.parametrize("params", [ |
| 300 |
{"ret_val": "Requested changes are already present in the user slot."}, |
| 301 |
{"firm_ver": (14, VERSION, HARDWARE_14G), "slot_id": None, "slot_uri": None, |
| 302 |
"ret_val": MAX_USERS_MSG}, |
| 303 |
{"check_mode": True, "ret_val": "No changes found to commit!"}, |
| 304 |
{"check_mode": True, "user_attr": { |
| 305 |
USERNAME1: "test_user"}, "ret_val": CHANGES_FOUND}, |
| 306 |
{"check_mode": True, "user_attr": {USERNAME1: "test_user"}, "ret_val": |
| 307 |
CHANGES_FOUND, "empty_slot_id": 2, "empty_slot_uri": SLOT_API.format(2)}, |
| 308 |
]) |
| 309 |
def test_create_or_modify_account_exception(self, idrac_connection_user_mock, idrac_default_args, mocker, params): |
| 310 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 311 |
"user_name": "test", "user_password": "password", |
| 312 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 313 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 314 |
"sol_enable": True, "protocol_enable": True, |
| 315 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 316 |
f_module = self.get_module_mock( |
| 317 |
params=idrac_default_args, check_mode=params.get("check_mode", False)) |
| 318 |
idrac_connection_user_mock.get_server_generation = params.get( |
| 319 |
"firm_ver", (13, VERSION13G, HARDWARE_13G)) |
| 320 |
mocker.patch(MODULE_PATH + GET_PAYLOAD, |
| 321 |
return_value={USERNAME2: "test_user"}) |
| 322 |
mocker.patch(MODULE_PATH + PAYLOAD_XML, |
| 323 |
return_value=(XML_DATA, {USERNAME2: "test_user"})) |
| 324 |
mocker.patch(MODULE_PATH + IMPORT_SCP, |
| 325 |
return_value={"Message": SUCCESS_CREATED}) |
| 326 |
mocker.patch(MODULE_PATH + INVOKE_REQUEST, |
| 327 |
return_value={"Message": SUCCESS_CREATED}) |
| 328 |
slot_id = params.get("slot_id", 2) |
| 329 |
slot_uri = params.get("slot_uri", SLOT_API.format(2)) |
| 330 |
empty_slot_id = params.get("empty_slot_id", None) |
| 331 |
empty_slot_uri = params.get("empty_slot_uri", None) |
| 332 |
user_attr = params.get("user_attr", {USERNAME2: "test_user"}) |
| 333 |
gener = 13 |
| 334 |
with pytest.raises(Exception) as exc: |
| 335 |
self.module.create_or_modify_account(f_module, idrac_connection_user_mock, slot_uri, slot_id, |
| 336 |
empty_slot_id, empty_slot_uri, user_attr, gener) |
| 337 |
assert exc.value.args[0] == params.get("ret_val") |
| 338 |
|
| 339 |
@pytest.mark.parametrize("exc_type", [SSLValidationError, URLError, ValueError, TypeError, |
| 340 |
ConnectionError, HTTPError, ImportError, RuntimeError]) |
| 341 |
def test_main_execptions(self, exc_type, idrac_connection_user_mock, idrac_default_args, mocker): |
| 342 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 343 |
"user_name": "test", "user_password": "password", |
| 344 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 345 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 346 |
"sol_enable": True, "protocol_enable": True, |
| 347 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 348 |
json_str = to_text(json.dumps({"data": "out"})) |
| 349 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 350 |
mocker.patch(MODULE_PATH + CM_ACCOUNT, |
| 351 |
side_effect=exc_type('test')) |
| 352 |
else: |
| 353 |
mocker.patch(MODULE_PATH + CM_ACCOUNT, |
| 354 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 355 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 356 |
if exc_type != URLError: |
| 357 |
result = self._run_module(idrac_default_args) |
| 358 |
assert result['failed'] is True |
| 359 |
else: |
| 360 |
result = self._run_module(idrac_default_args) |
| 361 |
assert 'msg' in result |
| 362 |
|
| 363 |
def test_main_error(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 364 |
idrac_default_args.update({"state": "absent", "new_user_name": "new_user_name", |
| 365 |
"user_name": "test", "user_password": "password", |
| 366 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 367 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 368 |
"sol_enable": True, "protocol_enable": True, |
| 369 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 370 |
obj = MagicMock() |
| 371 |
mocker.patch( |
| 372 |
MODULE_PATH + "idrac_user.set_attribute_uri", |
| 373 |
return_value=None |
| 374 |
) |
| 375 |
obj.json_data = {"error": {"message": "Some Error Occured"}} |
| 376 |
mocker.patch(MODULE_PATH + "idrac_user.remove_user_account", |
| 377 |
return_value=(obj, "error")) |
| 378 |
result = self._run_module(idrac_default_args) |
| 379 |
assert result['failed'] is True |
| 380 |
assert result['msg'] == "Some Error Occured" |
| 381 |
|
| 382 |
def test_main_error_oem(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 383 |
idrac_default_args.update({"state": "absent", "new_user_name": "new_user_name", |
| 384 |
"user_name": "test", "user_password": "password", |
| 385 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 386 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 387 |
"sol_enable": True, "protocol_enable": True, |
| 388 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 389 |
obj = MagicMock() |
| 390 |
mocker.patch( |
| 391 |
MODULE_PATH + "idrac_user.set_attribute_uri", |
| 392 |
return_value=None |
| 393 |
) |
| 394 |
mocker.patch(MODULE_PATH + "idrac_user.create_or_modify_account", |
| 395 |
return_value=(obj, "error")) |
| 396 |
obj.json_data = {"Oem": {"Dell": { |
| 397 |
"Message": "Unable to complete application of configuration profile values."}}} |
| 398 |
mocker.patch(MODULE_PATH + "idrac_user.remove_user_account", |
| 399 |
return_value=(obj, "error")) |
| 400 |
result = self._run_module(idrac_default_args) |
| 401 |
assert result['failed'] is True |
| 402 |
assert result['msg'] == "Unable to complete application of configuration profile values." |
| 403 |
|
| 404 |
def test_main_create_oem(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 405 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 406 |
"user_name": "test", "user_password": "password", |
| 407 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 408 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 409 |
"sol_enable": True, "protocol_enable": True, |
| 410 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 411 |
obj = MagicMock() |
| 412 |
mocker.patch(MODULE_PATH + "idrac_user.validate_input", |
| 413 |
return_value=None) |
| 414 |
mocker.patch(MODULE_PATH + "idrac_user.set_attribute_uri", |
| 415 |
return_value=(obj, "")) |
| 416 |
obj.json_data = { |
| 417 |
"Oem": {"Dell": {"Message": "This Message Does Not Exists"}}} |
| 418 |
mocker.patch(MODULE_PATH + CM_ACCOUNT, return_value=(obj, "created")) |
| 419 |
|
| 420 |
result = self._run_module(idrac_default_args) |
| 421 |
assert result['changed'] is True |
| 422 |
assert result['msg'] == "created" |
| 423 |
|
| 424 |
def test_main_invalid_authentication(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 425 |
idrac_default_args.update(CREATE_USER_DICT) |
| 426 |
mocker.patch(MODULE_PATH + "idrac_user." + GET_SERVER_VERSION_KEY, return_value=16) |
| 427 |
mocker.patch(MODULE_PATH + "idrac_user.validate_choices_for_protocol", |
| 428 |
return_value=(["AES"], ["SHA"])) |
| 429 |
result = self._run_module(idrac_default_args) |
| 430 |
assert result['failed'] is True |
| 431 |
assert result['msg'] == "Authentication protocol MD5 \ |
| 432 |
is not supported. The supported authentication protocols are ['SHA']." |
| 433 |
|
| 434 |
def test_main_invalid_privacy(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 435 |
idrac_default_args.update(CREATE_USER_DICT) |
| 436 |
mocker.patch(MODULE_PATH + "idrac_user." + GET_SERVER_VERSION_KEY, return_value=16) |
| 437 |
mocker.patch(MODULE_PATH + "idrac_user.validate_choices_for_protocol", |
| 438 |
return_value=(["AES-256"], ["MD5"])) |
| 439 |
result = self._run_module(idrac_default_args) |
| 440 |
assert result['failed'] is True |
| 441 |
assert result['msg'] == "Privacy protocol AES is not \ |
| 442 |
supported. The supported privacy protocols are ['AES-256']." |
| 443 |
|
| 444 |
def test_main_state_some(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 445 |
idrac_default_args.update({"state": "some", "new_user_name": "new_user_name", |
| 446 |
"user_name": "test", "user_password": "password", |
| 447 |
"privilege": "Administrator", "ipmi_lan_privilege": "Administrator", |
| 448 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 449 |
"sol_enable": True, "protocol_enable": True, |
| 450 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 451 |
try: |
| 452 |
self._run_module(idrac_default_args) |
| 453 |
except AnsibleFailJson as err: |
| 454 |
assert err.args[0]["msg"] == "value of state must be one of: present, absent, got: some" |
| 455 |
assert err.args[0].get('failed') is True |
| 456 |
|
| 457 |
def test_validate_input(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 458 |
idrac_default_args.update({"state": "present", "new_user_name": "new_user_name", |
| 459 |
"user_name": "test", "user_password": "password", |
| 460 |
"custom_privilege": 512, "ipmi_lan_privilege": "Administrator", |
| 461 |
"ipmi_serial_privilege": "Administrator", "enable": True, |
| 462 |
"sol_enable": True, "protocol_enable": True, |
| 463 |
"authentication_protocol": "SHA", "privacy_protocol": "AES"}) |
| 464 |
f_module = self.get_module_mock( |
| 465 |
params=idrac_default_args, check_mode=False) |
| 466 |
with pytest.raises(Exception) as err: |
| 467 |
self.module.validate_input(f_module, idrac_default_args, 15) |
| 468 |
assert err.value.args[0] == "custom_privilege value must range from 0 to 511." |
| 469 |
|
| 470 |
with pytest.raises(Exception) as err: |
| 471 |
self.module.validate_input(f_module, idrac_default_args, 17) |
| 472 |
assert err.value.args[0] == "custom_privilege value must range from 1 to 511." |
| 473 |
|
| 474 |
idrac_default_args.update({"state": "absent"}) |
| 475 |
ret = self.module.validate_input(f_module, idrac_default_args, 14) |
| 476 |
assert ret is None |
| 477 |
|
| 478 |
def test_compare_payload(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 479 |
json_payload = {"Users.1#Password": "MyDummyPassword"} |
| 480 |
is_change_required = self.module.compare_payload(json_payload, None) |
| 481 |
assert is_change_required is True |
| 482 |
|
| 483 |
json_payload = {USER_PRIVILAGE: "123"} |
| 484 |
idrac_attr = {USER_PRIVILAGE: "123"} |
| 485 |
is_change_required = self.module.compare_payload( |
| 486 |
json_payload, idrac_attr) |
| 487 |
assert is_change_required is False |
| 488 |
|
| 489 |
json_payload = {USER_PRIVILAGE: "123"} |
| 490 |
idrac_attr = {USER_PRIVILAGE: "124"} |
| 491 |
is_change_required = self.module.compare_payload( |
| 492 |
json_payload, idrac_attr) |
| 493 |
assert is_change_required is True |
| 494 |
|
| 495 |
def test_validate_choices_for_protocol(self, idrac_connection_user_mock, idrac_default_args, mocker): |
| 496 |
idrac_connection_user_mock.invoke_request.return_value = [] |
| 497 |
privacy_protocols, auth_protocols = self.module.validate_choices_for_protocol( |
| 498 |
idrac_connection_user_mock) |
| 499 |
assert auth_protocols == [] |
| 500 |
assert privacy_protocols == [] |
| 501 |
|
| 502 |
def test_set_attribute_uri_generation_17(self): |
| 503 |
self.module.set_attribute_uri(17) |
| 504 |
assert ATTRIBUTE_URI == ATTRIBUTE_URI_10 |
| 505 |
|
| 506 |
def test_get_role(self): |
| 507 |
user_roles = { |
| 508 |
0: "None", |
| 509 |
1: "ReadOnly", |
| 510 |
499: "Operator", |
| 511 |
511: "Administrator", |
| 512 |
} |
| 513 |
for key, value in user_roles.items(): |
| 514 |
ret = self.module.get_role(key) |
| 515 |
assert ret == value |
| 516 |
|
| 517 |
ret = self.module.get_role(400) |
| 518 |
assert ret == "Operator" |
| 519 |
ret = self.module.get_role(505) |
| 520 |
assert ret == "Administrator" |
| 521 |
|
| 522 |
def test_validate_choices_for_protocol_success(self): |
| 523 |
idrac = MagicMock() |
| 524 |
idrac.invoke_request.return_value.json_data = { |
| 525 |
"RegistryEntries": { |
| 526 |
"Attributes": [ |
| 527 |
{ |
| 528 |
"AttributeName": "Users.1.AuthenticationProtocol", |
| 529 |
"Value": [{"ValueDisplayName": "LDAP"}] |
| 530 |
}, |
| 531 |
{ |
| 532 |
"AttributeName": "Users.1.PrivacyProtocol", |
| 533 |
"Value": [{"ValueDisplayName": "HTTPS"}] |
| 534 |
} |
| 535 |
] |
| 536 |
} |
| 537 |
} |
| 538 |
expected_output = (["HTTPS"], ["LDAP"]) |
| 539 |
assert self.module.validate_choices_for_protocol(idrac) == expected_output |
| 540 |
|
| 541 |
def test_handle_create_generation_14(self): |
| 542 |
module = MagicMock() |
| 543 |
module.check_mode = False |
| 544 |
idrac = MagicMock() |
| 545 |
idrac.invoke_request.return_value = "response" |
| 546 |
generation = 14 |
| 547 |
payload = {} |
| 548 |
response = self.module.handle_create(module, idrac, generation, payload) |
| 549 |
assert response == "response" |
| 550 |
|
| 551 |
def test_handle_create_check_mode(self): |
| 552 |
module = MagicMock() |
| 553 |
module.check_mode = True |
| 554 |
idrac = MagicMock() |
| 555 |
generation = 14 |
| 556 |
payload = {} |
| 557 |
idrac.invoke_request.return_value = "response" |
| 558 |
res = self.module.handle_create(module, idrac, generation, payload) |
| 559 |
assert res == "response" |
| 560 |
|
| 561 |
def test_handle_update_generation_14(self): |
| 562 |
module = MagicMock() |
| 563 |
module.check_mode = False |
| 564 |
idrac = MagicMock() |
| 565 |
idrac.invoke_request.return_value = "response1" |
| 566 |
generation = 14 |
| 567 |
payload = {} |
| 568 |
response = self.module.handle_update(module, idrac, generation, payload, "Users.1#UserName", "test_user") |
| 569 |
assert response == "response1" |
| 570 |
|
| 571 |
def test_rolename_exists(self, idrac_connection_user_mock, mocker): |
| 572 |
idrac_connection_user_mock.invoke_request.return_value.json_data = { |
| 573 |
"Members": [ |
| 574 |
{"Id": "Administrator"}, |
| 575 |
{"@odata.id": "/redfish/v1/AccountService/Roles/CustomRole1"} |
| 576 |
] |
| 577 |
} |
| 578 |
|
| 579 |
assert idrac_user.rolename_exists( |
| 580 |
idrac_connection_user_mock, "CustomRole1" |
| 581 |
) is True |
| 582 |
|
| 583 |
def test_rolename_exists_not_found(self, idrac_connection_user_mock, mocker): |
| 584 |
idrac_connection_user_mock.invoke_request.return_value.json_data = { |
| 585 |
"Members": [ |
| 586 |
{"Id": "Administrator"} |
| 587 |
] |
| 588 |
} |
| 589 |
|
| 590 |
assert idrac_user.rolename_exists( |
| 591 |
idrac_connection_user_mock, "MissingRole" |
| 592 |
) is False |
| 593 |
|
| 594 |
def test_create_custom_role_success(self): |
| 595 |
module = MagicMock() |
| 596 |
idrac = MagicMock() |
| 597 |
response = MagicMock() |
| 598 |
response.status_code = 201 |
| 599 |
idrac.invoke_request.return_value = response |
| 600 |
|
| 601 |
role_name = "CustomRole1" |
| 602 |
custom_priv = 17 |
| 603 |
|
| 604 |
result = idrac_user.create_custom_role( |
| 605 |
module, |
| 606 |
idrac, |
| 607 |
role_name, |
| 608 |
custom_priv |
| 609 |
) |
| 610 |
|
| 611 |
assert result == role_name |
| 612 |
idrac.invoke_request.assert_called_once() |
| 613 |
|
| 614 |
@pytest.mark.parametrize("username", [ |
| 615 |
"testuserlengthmorethan16", |
| 616 |
"test@123dell" |
| 617 |
]) |
| 618 |
def test_validate_username(self, username, idrac_default_args): |
| 619 |
module = MagicMock() |
| 620 |
module.check_mode = False |
| 621 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 622 |
with pytest.raises(Exception) as err: |
| 623 |
self.module.validate_username(f_module, username) |
| 624 |
assert err.value.args[0] == \ |
| 625 |
INVALID_USERNAME_FORMAT.format(username=username) |