| 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 json |
| 16 |
import os |
| 17 |
import tempfile |
| 18 |
from io import StringIO |
| 19 |
|
| 20 |
import pytest |
| 21 |
from ansible.module_utils._text import to_text |
| 22 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 23 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 24 |
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_certificates |
| 25 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 26 |
from unittest.mock import MagicMock |
| 27 |
|
| 28 |
IMPORT_SSL_CERTIFICATE = "#DelliDRACCardService.ImportSSLCertificate" |
| 29 |
EXPORT_SSL_CERTIFICATE = "#DelliDRACCardService.ExportSSLCertificate" |
| 30 |
IDRAC_CARD_SERVICE_ACTION_URI = "/redfish/v1/Managers/{res_id}/Oem/Dell/DelliDRACCardService/Actions" |
| 31 |
IDRAC_CARD_SERVICE_ACTION_URI_RES_ID = "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DelliDRACCardService/Actions" |
| 32 |
|
| 33 |
NOT_SUPPORTED_ACTION = "Certificate '{operation}' not supported for the specified certificate type '{cert_type}'." |
| 34 |
SUCCESS_MSG = "Successfully performed the '{command}' certificate operation." |
| 35 |
SUCCESS_MSG_SSL = "Successfully performed the SSL key upload and '{command}' certificate operation." |
| 36 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 37 |
CHANGES_MSG = "Changes found to be applied." |
| 38 |
WAIT_NEGATIVE_OR_ZERO_MSG = "The value for the `wait` parameter cannot be negative or zero." |
| 39 |
SSL_KEY_MSG = "Unable to locate the SSL key file at {ssl_key}." |
| 40 |
SSK_KEY_NOT_SUPPORTED = "Upload of SSL key not supported" |
| 41 |
NO_RESET = "Reset iDRAC to apply the new certificate. Until the iDRAC is reset, the old certificate will remain active." |
| 42 |
RESET_UNTRACK = " iDRAC reset is in progress. Until the iDRAC is reset, the changes would not apply." |
| 43 |
RESET_SUCCESS = "iDRAC has been reset successfully." |
| 44 |
RESET_FAIL = " Unable to reset the iDRAC. For changes to reflect, manually reset the iDRAC." |
| 45 |
SYSTEM_ID = "System.Embedded.1" |
| 46 |
MANAGER_ID = "iDRAC.Embedded.1" |
| 47 |
SYSTEMS_URI = "/redfish/v1/Systems" |
| 48 |
MANAGERS_URI = "/redfish/v1/Managers" |
| 49 |
IDRAC_SERVICE = "/redfish/v1/Managers/{res_id}/Oem/Dell/DelliDRACCardService" |
| 50 |
CSR_SSL = "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR" |
| 51 |
IMPORT_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ImportSSLCertificate" |
| 52 |
UPLOAD_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.UploadSSLKey" |
| 53 |
EXPORT_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ExportSSLCertificate" |
| 54 |
RESET_SSL = f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.SSLResetCfg" |
| 55 |
IDRAC_RESET = "/redfish/v1/Managers/{res_id}/Actions/Manager.Reset" |
| 56 |
idrac_service_actions = { |
| 57 |
"#DelliDRACCardService.DeleteCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.DeleteCertificate", |
| 58 |
"#DelliDRACCardService.ExportCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ExportCertificate", |
| 59 |
EXPORT_SSL_CERTIFICATE: EXPORT_SSL, |
| 60 |
"#DelliDRACCardService.FactoryIdentityCertificateGenerateCSR": |
| 61 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityCertificateGenerateCSR", |
| 62 |
"#DelliDRACCardService.FactoryIdentityExportCertificate": |
| 63 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityExportCertificate", |
| 64 |
"#DelliDRACCardService.FactoryIdentityImportCertificate": |
| 65 |
f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.FactoryIdentityImportCertificate", |
| 66 |
"#DelliDRACCardService.GenerateSEKMCSR": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.GenerateSEKMCSR", |
| 67 |
"#DelliDRACCardService.ImportCertificate": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.ImportCertificate", |
| 68 |
IMPORT_SSL_CERTIFICATE: IMPORT_SSL, |
| 69 |
"#DelliDRACCardService.UploadSSLKey": UPLOAD_SSL, |
| 70 |
"#DelliDRACCardService.SSLResetCfg": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.SSLResetCfg", |
| 71 |
"#DelliDRACCardService.iDRACReset": f"{IDRAC_CARD_SERVICE_ACTION_URI}/DelliDRACCardService.iDRACReset" |
| 72 |
} |
| 73 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.idrac_certificates.' |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def idrac_redfish_mock_for_certs(mocker, ome_response_mock): |
| 78 |
connection_class_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI') |
| 79 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 80 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 81 |
return ome_connection_mock_obj |
| 82 |
|
| 83 |
|
| 84 |
class TestIdracCertificates(FakeAnsibleModule): |
| 85 |
module = idrac_certificates |
| 86 |
|
| 87 |
@pytest.fixture |
| 88 |
def idrac_certificates_mock(self): |
| 89 |
idrac_obj = MagicMock() |
| 90 |
return idrac_obj |
| 91 |
|
| 92 |
@pytest.fixture |
| 93 |
def idrac_connection_certificates_mock( |
| 94 |
self, mocker, idrac_certificates_mock): |
| 95 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 96 |
return_value=idrac_certificates_mock) |
| 97 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_certificates_mock |
| 98 |
return idrac_conn_mock |
| 99 |
|
| 100 |
@pytest.mark.parametrize("params", [ |
| 101 |
{"json_data": {"CertificateFile": b'Hello world!', "@Message.ExtendedInfo": [ |
| 102 |
{ |
| 103 |
"Message": "Successfully exported SSL Certificate.", |
| 104 |
"MessageId": "IDRAC.2.5.LC067", |
| 105 |
"Resolution": "No response action is required.", |
| 106 |
"Severity": "Informational" |
| 107 |
}]}, 'message': SUCCESS_MSG.format(command="export"), "success": True, |
| 108 |
"reset_idrac": (True, False, RESET_SUCCESS), |
| 109 |
'mparams': {'command': 'export', 'certificate_type': "HTTPS", 'certificate_path': tempfile.gettempdir(), |
| 110 |
'reset': False}}, |
| 111 |
{"json_data": {"CertificateFile": b'Hello world!'}, 'message': CHANGES_MSG, "success": True, |
| 112 |
"reset_idrac": (True, False, RESET_SUCCESS), 'check_mode': True, |
| 113 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', 'reset': False}}, |
| 114 |
{"json_data": {"CertificateFile": b'Hello world!', "ssl_key": b'Hello world!'}, 'message': CHANGES_MSG, "success": True, |
| 115 |
"reset_idrac": (True, False, RESET_SUCCESS), 'check_mode': True, |
| 116 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', "ssl_key": '.pem', 'reset': False}}, |
| 117 |
{"json_data": {}, 'message': "{0}{1}".format(SUCCESS_MSG.format(command="import"), NO_RESET), "success": True, |
| 118 |
"reset_idrac": (True, False, RESET_SUCCESS), |
| 119 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', 'reset': False}}, |
| 120 |
{"json_data": {}, 'message': "{0} {1}".format(SUCCESS_MSG_SSL.format(command="import"), NO_RESET), "success": True, |
| 121 |
"reset_idrac": (True, False, RESET_SUCCESS), |
| 122 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', "ssl_key": '.pem', 'reset': False}}, |
| 123 |
{"json_data": {}, 'message': SUCCESS_MSG.format(command="generate_csr"), |
| 124 |
"success": True, |
| 125 |
"get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 126 |
'mparams': {'command': 'generate_csr', 'certificate_type': "HTTPS", 'certificate_path': tempfile.gettempdir(), |
| 127 |
'cert_params': { |
| 128 |
"common_name": "dell", |
| 129 |
"country_code": "IN", |
| 130 |
"email_address": "[email protected]", |
| 131 |
"locality_name": "Bangalore", |
| 132 |
"organization_name": "Dell", |
| 133 |
"organization_unit": "ansible", |
| 134 |
"state_name": "Karnataka", |
| 135 |
"subject_alt_name": [ |
| 136 |
"emc" |
| 137 |
]}}}, |
| 138 |
{"json_data": {}, 'message': NOT_SUPPORTED_ACTION.format(operation="generate_csr", cert_type="CA"), |
| 139 |
"success": True, |
| 140 |
"get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 141 |
'mparams': {'command': 'generate_csr', 'certificate_type': "CA", 'certificate_path': tempfile.gettempdir(), |
| 142 |
'cert_params': { |
| 143 |
"common_name": "dell", |
| 144 |
"country_code": "IN", |
| 145 |
"email_address": "[email protected]", |
| 146 |
"locality_name": "Bangalore", |
| 147 |
"organization_name": "Dell", |
| 148 |
"organization_unit": "ansible", |
| 149 |
"state_name": "Karnataka", |
| 150 |
"subject_alt_name": [ |
| 151 |
"emc" |
| 152 |
]}}}, |
| 153 |
{"json_data": {}, 'message': "{0}{1}".format(SUCCESS_MSG.format(command="import"), RESET_SUCCESS), |
| 154 |
"success": True, |
| 155 |
"get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 156 |
'mparams': {'command': 'import', 'certificate_type': "CA", 'passphrase': 'myphrase', |
| 157 |
'certificate_path': '.p12'}}, |
| 158 |
{"json_data": {}, 'message': "{0}{1}".format(SUCCESS_MSG.format(command="import"), RESET_SUCCESS), |
| 159 |
"success": True, |
| 160 |
"get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 161 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem'}}, |
| 162 |
{"json_data": {}, 'message': "{0} {1}".format(SUCCESS_MSG_SSL.format(command="import"), RESET_SUCCESS), |
| 163 |
"success": True, |
| 164 |
"get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 165 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', 'ssl_key': '.pem'}}, |
| 166 |
{"json_data": {}, 'message': "{0}{1}".format(SUCCESS_MSG.format(command="import"), RESET_SUCCESS), |
| 167 |
"success": True, |
| 168 |
"reset_idrac": (True, False, RESET_SUCCESS), |
| 169 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem'}}, |
| 170 |
{"json_data": {}, 'message': "{0} {1}".format(SUCCESS_MSG_SSL.format(command="import"), RESET_SUCCESS), |
| 171 |
"success": True, |
| 172 |
"reset_idrac": (True, False, RESET_SUCCESS), |
| 173 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', "ssl_key": '.pem'}}, |
| 174 |
{"json_data": {}, 'message': SUCCESS_MSG.format(command="export"), "success": True, "get_cert_url": "url", |
| 175 |
'mparams': {'command': 'export', 'certificate_type': "HTTPS", 'certificate_path': tempfile.gettempdir()}}, |
| 176 |
{"json_data": {}, 'message': "{0}{1}".format(SUCCESS_MSG.format(command="reset"), RESET_SUCCESS), |
| 177 |
"success": True, "get_cert_url": "url", "reset_idrac": (True, False, RESET_SUCCESS), |
| 178 |
'mparams': {'command': 'reset', 'certificate_type': "HTTPS"} |
| 179 |
}, |
| 180 |
{"json_data": {}, 'message': WAIT_NEGATIVE_OR_ZERO_MSG, "success": True, |
| 181 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', 'wait': -1}}, |
| 182 |
{"json_data": {}, 'message': WAIT_NEGATIVE_OR_ZERO_MSG, "success": True, |
| 183 |
'mparams': {'command': 'reset', 'certificate_type': "HTTPS", 'wait': 0}}, |
| 184 |
{"json_data": {}, 'message': f"{SSL_KEY_MSG.format(ssl_key='/invalid/path')}", "success": True, |
| 185 |
'mparams': {'command': 'import', 'certificate_type': "HTTPS", 'certificate_path': '.pem', 'ssl_key': '/invalid/path'}} |
| 186 |
]) |
| 187 |
def test_idrac_certificates( |
| 188 |
self, params, idrac_connection_certificates_mock, idrac_default_args, mocker): |
| 189 |
idrac_connection_certificates_mock.success = params.get( |
| 190 |
"success", True) |
| 191 |
idrac_connection_certificates_mock.json_data = params.get('json_data') |
| 192 |
if params.get('mparams').get('certificate_path') and params.get( |
| 193 |
'mparams').get('command') == 'import': |
| 194 |
sfx = params.get('mparams').get('certificate_path') |
| 195 |
temp = tempfile.NamedTemporaryFile(suffix=sfx, delete=False) |
| 196 |
temp.write(b'Hello') |
| 197 |
temp.close() |
| 198 |
params.get('mparams')['certificate_path'] = temp.name |
| 199 |
if params.get('mparams').get('ssl_key') == '.pem': |
| 200 |
temp = tempfile.NamedTemporaryFile(suffix=sfx, delete=False) |
| 201 |
temp.write(b'Hello') |
| 202 |
temp.close() |
| 203 |
params.get('mparams')['ssl_key'] = temp.name |
| 204 |
mocker.patch(MODULE_PATH + 'get_res_id', return_value=MANAGER_ID) |
| 205 |
mocker.patch( |
| 206 |
MODULE_PATH + 'get_idrac_service', |
| 207 |
return_value=IDRAC_SERVICE.format( |
| 208 |
res_id=MANAGER_ID)) |
| 209 |
mocker.patch( |
| 210 |
MODULE_PATH + 'get_actions_map', |
| 211 |
return_value=idrac_service_actions) |
| 212 |
|
| 213 |
|
| 214 |
mocker.patch( |
| 215 |
MODULE_PATH + 'reset_idrac', |
| 216 |
return_value=params.get('reset_idrac')) |
| 217 |
idrac_default_args.update(params.get('mparams')) |
| 218 |
result = self._run_module( |
| 219 |
idrac_default_args, |
| 220 |
check_mode=params.get( |
| 221 |
'check_mode', |
| 222 |
False)) |
| 223 |
if params.get('mparams').get('command') == 'import' and params.get('mparams').get( |
| 224 |
'certificate_path') and os.path.exists(temp.name): |
| 225 |
os.remove(temp.name) |
| 226 |
assert result['msg'] == params['message'] |
| 227 |
|
| 228 |
@pytest.mark.parametrize("params", [{"json_data": {"Members": [{"@odata.id": '/redfish/v1/Mangers/iDRAC.1'}]}, |
| 229 |
"cert_type": 'Server', "res_id": "iDRAC.1"}, |
| 230 |
{"json_data": {"Members": []}, |
| 231 |
"cert_type": 'Server', "res_id": MANAGER_ID} |
| 232 |
]) |
| 233 |
def test_res_id( |
| 234 |
self, params, idrac_redfish_mock_for_certs, ome_response_mock): |
| 235 |
ome_response_mock.success = params.get("success", True) |
| 236 |
ome_response_mock.json_data = params["json_data"] |
| 237 |
res_id = self.module.get_res_id( |
| 238 |
idrac_redfish_mock_for_certs, |
| 239 |
params.get('cert_type')) |
| 240 |
assert res_id == params['res_id'] |
| 241 |
|
| 242 |
@pytest.mark.parametrize("params", [{"json_data": { |
| 243 |
"Links": { |
| 244 |
"Oem": { |
| 245 |
"Dell": { |
| 246 |
"DelliDRACCardService": { |
| 247 |
"@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DelliDRACCardService" |
| 248 |
}}}}, |
| 249 |
"VirtualMedia": { |
| 250 |
"@odata.id": "/redfish/v1/Managers/iDRAC.Embedded.1/VirtualMedia"} |
| 251 |
}, |
| 252 |
"idrac_srv": '/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DelliDRACCardService', "res_id": "iDRAC.1"} |
| 253 |
]) |
| 254 |
def test_get_idrac_service( |
| 255 |
self, params, idrac_redfish_mock_for_certs, ome_response_mock): |
| 256 |
ome_response_mock.success = params.get("success", True) |
| 257 |
ome_response_mock.json_data = params["json_data"] |
| 258 |
idrac_srv = self.module.get_idrac_service( |
| 259 |
idrac_redfish_mock_for_certs, params.get('res_id')) |
| 260 |
assert idrac_srv == params['idrac_srv'] |
| 261 |
|
| 262 |
def test_write_to_file(self, idrac_default_args, mocker): |
| 263 |
temp_dir = 'XX/YY/' |
| 264 |
inv_dir = "invalid_temp_dir" |
| 265 |
idrac_default_args.update({"certificate_path": inv_dir}) |
| 266 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 267 |
with pytest.raises(Exception) as ex: |
| 268 |
self.module.write_to_file(f_module, {}, "dkey") |
| 269 |
assert ex.value.args[0] == f"Provided directory path '{inv_dir}' is not valid." |
| 270 |
mocker.patch('os.path.exists', return_value=True) |
| 271 |
mocker.patch('os.access', return_value=False) |
| 272 |
idrac_default_args.update({"certificate_path": temp_dir}) |
| 273 |
with pytest.raises(Exception) as ex: |
| 274 |
self.module.write_to_file(f_module, {}, "dkey") |
| 275 |
assert ex.value.args[0] == f"Provided directory path '{temp_dir}' is not writable. Please check if you have appropriate permissions." |
| 276 |
|
| 277 |
def test_upload_ssl_key(self, idrac_default_args, mocker): |
| 278 |
temp_ssl = tempfile.NamedTemporaryFile(delete=False) |
| 279 |
temp_ssl.write(b'ssl_key') |
| 280 |
temp_ssl.close() |
| 281 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 282 |
with pytest.raises(Exception) as ex: |
| 283 |
self.module.upload_ssl_key(f_module, {}, {}, temp_ssl.name, "res_id") |
| 284 |
assert ex.value.args[0] == "Upload of SSL key not supported" |
| 285 |
mocker.patch('builtins.open', side_effect=OSError(0, "Permission denied")) |
| 286 |
with pytest.raises(Exception) as ex: |
| 287 |
self.module.upload_ssl_key(f_module, {}, {}, temp_ssl.name, "res_id") |
| 288 |
assert "Permission denied" in ex.value.args[0] |
| 289 |
os.remove(temp_ssl.name) |
| 290 |
|
| 291 |
def test_build_generate_csr_payload(self, idrac_default_args): |
| 292 |
cert_params_data = { |
| 293 |
"cert_params": { |
| 294 |
"subject_alt_name": ['192.198.2.1,192.198.2.2', 'X.X.X.X'] |
| 295 |
} |
| 296 |
} |
| 297 |
idrac_default_args.update(cert_params_data) |
| 298 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 299 |
payload = self.module._build_generate_csr_payload(f_module, None) |
| 300 |
assert payload["AlternativeNames"] == ['192.198.2.1,192.198.2.2,X.X.X.X'] |
| 301 |
|
| 302 |
def test_perform_operation_and_download_csr(self, idrac_default_args): |
| 303 |
idrac, value = MagicMock(), MagicMock() |
| 304 |
value.json_data = {"CSRString": "value"} |
| 305 |
idrac.invoke_request = MagicMock(return_value=value) |
| 306 |
idrac_default_args.update({"timeout": 120}) |
| 307 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 308 |
payload = self.module.perform_operation_and_download_csr(idrac, None, None, None, f_module) |
| 309 |
assert "CSRString" in payload.json_data |
| 310 |
|
| 311 |
def test_check_csr_generated(self, mocker): |
| 312 |
idrac, value = MagicMock(), MagicMock() |
| 313 |
value.json_data = {"Attributes": {"Security.1.ConfigCertStatus": 2}} |
| 314 |
idrac.invoke_request = MagicMock(return_value=value) |
| 315 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 316 |
payload = self.module.check_csr_generated(idrac) |
| 317 |
assert payload is True |
| 318 |
|
| 319 |
def test_perform_operation_and_download_csr_with_HTTPError(self, idrac_default_args, mocker): |
| 320 |
idrac, value = MagicMock(), MagicMock() |
| 321 |
value.json_data = {"CSRString": "value"} |
| 322 |
err_info = '{"error":{"@Message.ExtendedInfo":[{"MessageId": "IDRAC.2.9.SYS537"}]}}' |
| 323 |
|
| 324 |
def mock_invoke_request(*args, **kwargs): |
| 325 |
if args[1] == 'POST': |
| 326 |
return value |
| 327 |
else: |
| 328 |
raise HTTPError('https://testhost.com', 503, "http error message", |
| 329 |
{"accept-type": "application/json"}, StringIO(err_info)) |
| 330 |
idrac.invoke_request = MagicMock(side_effect=mock_invoke_request) |
| 331 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 332 |
mocker.patch(MODULE_PATH + 'check_csr_generated', return_value=True) |
| 333 |
idrac_default_args.update({"timeout": 120}) |
| 334 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 335 |
payload = self.module.perform_operation_and_download_csr(idrac, None, "GET", None, f_module) |
| 336 |
assert payload.json_data["CSRString"] == "value" |
| 337 |
|
| 338 |
@pytest.mark.parametrize("params", [{"json_data": { |
| 339 |
"Actions": { |
| 340 |
EXPORT_SSL_CERTIFICATE: { |
| 341 |
"[email protected]": ["CA", "CSC", "CustomCertificate", "ClientTrustCertificate", "Server"], |
| 342 |
"target": |
| 343 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.ExportSSLCertificate" |
| 344 |
}, |
| 345 |
IMPORT_SSL_CERTIFICATE: { |
| 346 |
"[email protected]": ["CA", "CSC", "CustomCertificate", "ClientTrustCertificate", "Server"], |
| 347 |
"target": |
| 348 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.ImportSSLCertificate" |
| 349 |
}, |
| 350 |
"#DelliDRACCardService.SSLResetCfg": { |
| 351 |
"target": f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.SSLResetCfg" |
| 352 |
}, |
| 353 |
"#DelliDRACCardService.UploadSSLKey": { |
| 354 |
"target": f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.UploadSSLKey"} |
| 355 |
}, |
| 356 |
}, |
| 357 |
"idrac_service_uri": '/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DelliDRACCardService', |
| 358 |
"actions": { |
| 359 |
EXPORT_SSL_CERTIFICATE: |
| 360 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.ExportSSLCertificate", |
| 361 |
IMPORT_SSL_CERTIFICATE: |
| 362 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.ImportSSLCertificate", |
| 363 |
'#DelliDRACCardService.SSLResetCfg': |
| 364 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.SSLResetCfg", |
| 365 |
'#DelliDRACCardService.UploadSSLKey': |
| 366 |
f"{IDRAC_CARD_SERVICE_ACTION_URI_RES_ID}/DelliDRACCardService.UploadSSLKey"}}, |
| 367 |
{"json_data": {"Members": []}, |
| 368 |
"idrac_service_uri": '/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/DelliDRACCardService', |
| 369 |
"actions": idrac_service_actions} |
| 370 |
]) |
| 371 |
def test_get_actions_map( |
| 372 |
self, params, idrac_redfish_mock_for_certs, ome_response_mock): |
| 373 |
ome_response_mock.success = params.get("success", True) |
| 374 |
ome_response_mock.json_data = params["json_data"] |
| 375 |
actions = self.module.get_actions_map( |
| 376 |
idrac_redfish_mock_for_certs, |
| 377 |
params.get('idrac_service_uri')) |
| 378 |
assert actions == params['actions'] |
| 379 |
|
| 380 |
@pytest.mark.parametrize("params", [{"actions": {}, "operation": "generate_csr", |
| 381 |
"cert_type": 'Server', "res_id": "iDRAC.1", |
| 382 |
"dynurl": "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR"}, |
| 383 |
{"actions": {}, "operation": "import", |
| 384 |
"cert_type": 'Server', "res_id": "iDRAC.1", |
| 385 |
"dynurl": "/redfish/v1/Managers/iDRAC.1/Oem/Dell/DelliDRACCardService/Actions/" |
| 386 |
"DelliDRACCardService.ImportSSLCertificate"} |
| 387 |
]) |
| 388 |
def test_get_cert_url(self, params): |
| 389 |
dynurl = self.module.get_cert_url(params.get('actions'), params.get('operation'), params.get('cert_type'), |
| 390 |
params.get('res_id')) |
| 391 |
assert dynurl == params['dynurl'] |
| 392 |
|
| 393 |
@pytest.mark.parametrize("params", [ |
| 394 |
{"cert_data": {"CertificateFile": 'Hello world!', |
| 395 |
"@Message.ExtendedInfo": [{ |
| 396 |
"Message": "Successfully exported SSL Certificate.", |
| 397 |
"MessageId": "IDRAC.2.5.LC067", |
| 398 |
"Resolution": "No response action is required.", |
| 399 |
"Severity": "Informational"} |
| 400 |
]}, |
| 401 |
"result": {'@Message.ExtendedInfo': [ |
| 402 |
{'Message': 'Successfully exported SSL Certificate.', |
| 403 |
'MessageId': 'IDRAC.2.5.LC067', |
| 404 |
'Resolution': 'No response action is required.', |
| 405 |
'Severity': 'Informational'}]}, |
| 406 |
"mparams": {'command': 'export', 'certificate_type': "HTTPS", |
| 407 |
'certificate_path': tempfile.gettempdir(), 'reset': False} |
| 408 |
}, |
| 409 |
{"cert_data": {"CertificateFile": 'Hello world!', |
| 410 |
"@Message.ExtendedInfo": [{ |
| 411 |
"Message": "Successfully exported SSL Certificate.", |
| 412 |
"MessageId": "IDRAC.2.5.LC067", |
| 413 |
"Resolution": "No response action is required.", |
| 414 |
"Severity": "Informational"} |
| 415 |
]}, |
| 416 |
"result": {'@Message.ExtendedInfo': [ |
| 417 |
{'Message': 'Successfully exported SSL Certificate.', |
| 418 |
'MessageId': 'IDRAC.2.5.LC067', |
| 419 |
'Resolution': 'No response action is required.', |
| 420 |
'Severity': 'Informational'}]}, |
| 421 |
"mparams": {'command': 'generate_csr', 'certificate_type': "HTTPS", |
| 422 |
'certificate_path': tempfile.gettempdir(), 'reset': False}}]) |
| 423 |
def test_format_output(self, params, idrac_default_args): |
| 424 |
idrac_default_args.update(params.get('mparams')) |
| 425 |
f_module = self.get_module_mock(params=idrac_default_args) |
| 426 |
result = self.module.format_output(f_module, params.get('cert_data')) |
| 427 |
if os.path.exists(result.get('certificate_path')): |
| 428 |
os.remove(result.get('certificate_path')) |
| 429 |
assert 'result' not in result |
| 430 |
|
| 431 |
@pytest.mark.parametrize("exc_type", [SSLValidationError, URLError, ValueError, TypeError, |
| 432 |
ConnectionError, HTTPError, ImportError, RuntimeError]) |
| 433 |
def test_main_exceptions( |
| 434 |
self, exc_type, idrac_connection_certificates_mock, idrac_default_args, mocker): |
| 435 |
idrac_default_args.update( |
| 436 |
{"command": "export", "certificate_path": "mypath"}) |
| 437 |
json_str = to_text(json.dumps({"data": "out"})) |
| 438 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 439 |
mocker.patch(MODULE_PATH + "get_res_id", |
| 440 |
side_effect=exc_type('test')) |
| 441 |
else: |
| 442 |
mocker.patch(MODULE_PATH + "get_res_id", |
| 443 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 444 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 445 |
if not exc_type == URLError: |
| 446 |
result = self._run_module(idrac_default_args) |
| 447 |
assert result['failed'] is True |
| 448 |
else: |
| 449 |
result = self._run_module(idrac_default_args) |
| 450 |
assert 'msg' in result |