| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
from io import StringIO |
| 14 |
import json |
| 15 |
import tempfile |
| 16 |
import os |
| 17 |
|
| 18 |
import pytest |
| 19 |
from urllib.error import HTTPError |
| 20 |
from ansible.module_utils._text import to_text |
| 21 |
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_license |
| 22 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 23 |
from unittest.mock import MagicMock |
| 24 |
from ansible_collections.dellemc.openmanage.plugins.modules.idrac_license import main |
| 25 |
|
| 26 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.idrac_license.' |
| 27 |
MODULE_UTILS_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.utils.' |
| 28 |
|
| 29 |
INVALID_LICENSE_MSG = "License with ID '{license_id}' does not exist on the iDRAC." |
| 30 |
SUCCESS_EXPORT_MSG = "Successfully exported the license." |
| 31 |
SUCCESS_DELETE_MSG = "Successfully deleted the license." |
| 32 |
SUCCESS_IMPORT_MSG = "Successfully imported the license." |
| 33 |
FAILURE_MSG = "Unable to '{operation}' the license with id '{license_id}' as it does not exist." |
| 34 |
FAILURE_IMPORT_MSG = "Unable to import the license." |
| 35 |
NO_FILE_MSG = "License file not found." |
| 36 |
UNSUPPORTED_FIRMWARE_MSG = "iDRAC firmware version is not supported." |
| 37 |
NO_OPERATION_SKIP_MSG = "Task is skipped as none of import, export or delete is specified." |
| 38 |
INVALID_FILE_MSG = "File extension is invalid. Supported extensions for local 'share_type' " \ |
| 39 |
"are: .txt and .xml, and for network 'share_type' is: .xml." |
| 40 |
INVALID_DIRECTORY_MSG = "Provided directory path '{path}' is not valid." |
| 41 |
INSUFFICIENT_DIRECTORY_PERMISSION_MSG = "Provided directory path '{path}' is not writable. " \ |
| 42 |
"Please check if the directory has appropriate permissions" |
| 43 |
MISSING_FILE_NAME_PARAMETER_MSG = "Missing required parameter 'file_name'." |
| 44 |
REDFISH = "/redfish/v1" |
| 45 |
|
| 46 |
LIC_GET_LICENSE_URL = "License.get_license_url" |
| 47 |
REDFISH_LICENSE_URL = "/redfish/v1/license" |
| 48 |
REDFISH_BASE_API = '/redfish/v1/api' |
| 49 |
MANAGER_URI_ONE = "/redfish/v1/managers/1" |
| 50 |
API_ONE = "/local/action" |
| 51 |
EXPORT_URL_MOCK = '/redfish/v1/export_license' |
| 52 |
IMPORT_URL_MOCK = '/redfish/v1/import_license' |
| 53 |
API_INVOKE_MOCKER = "iDRACRedfishAPI.invoke_request" |
| 54 |
ODATA = "@odata.id" |
| 55 |
IDRAC_ID = "iDRAC.Embedded.1" |
| 56 |
LIC_FILE_NAME = 'test_lic.txt' |
| 57 |
HTTPS_PATH = "https://testhost.com" |
| 58 |
HTTP_ERROR = "http error message" |
| 59 |
APPLICATION_JSON = "application/json" |
| 60 |
|
| 61 |
|
| 62 |
class TestLicense(FakeAnsibleModule): |
| 63 |
module = idrac_license |
| 64 |
|
| 65 |
@pytest.fixture |
| 66 |
def idrac_license_mock(self): |
| 67 |
idrac_obj = MagicMock() |
| 68 |
return idrac_obj |
| 69 |
|
| 70 |
@pytest.fixture |
| 71 |
def idrac_connection_license_mock(self, mocker, idrac_license_mock): |
| 72 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 73 |
return_value=idrac_license_mock) |
| 74 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_license_mock |
| 75 |
return idrac_conn_mock |
| 76 |
|
| 77 |
def test_check_license_id(self, idrac_default_args, idrac_connection_license_mock, |
| 78 |
idrac_license_mock, mocker): |
| 79 |
mocker.patch(MODULE_PATH + LIC_GET_LICENSE_URL, |
| 80 |
return_value=REDFISH_LICENSE_URL) |
| 81 |
f_module = self.get_module_mock( |
| 82 |
params=idrac_default_args, check_mode=False) |
| 83 |
lic_obj = self.module.License( |
| 84 |
idrac_connection_license_mock, f_module) |
| 85 |
|
| 86 |
idr_obj = MagicMock() |
| 87 |
idr_obj.json_data = {"license_id": "1234"} |
| 88 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 89 |
return_value=idr_obj) |
| 90 |
data = lic_obj.check_license_id(license_id="1234") |
| 91 |
assert data.json_data == {"license_id": "1234"} |
| 92 |
|
| 93 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 94 |
side_effect=HTTPError(HTTPS_PATH, 400, |
| 95 |
HTTP_ERROR, |
| 96 |
{"accept-type": APPLICATION_JSON}, |
| 97 |
StringIO("json_str"))) |
| 98 |
with pytest.raises(Exception) as exc: |
| 99 |
lic_obj.check_license_id(license_id="1234") |
| 100 |
assert exc.value.args[0] == INVALID_LICENSE_MSG.format(license_id="1234") |
| 101 |
|
| 102 |
def test_get_license_url(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 103 |
v1_resp = {"LicenseService": {ODATA: "/redfish/v1/LicenseService"}, |
| 104 |
"Licenses": {ODATA: "/redfish/v1/LicenseService/Licenses"}} |
| 105 |
mocker.patch(MODULE_PATH + "get_dynamic_uri", |
| 106 |
return_value=v1_resp) |
| 107 |
f_module = self.get_module_mock( |
| 108 |
params=idrac_default_args, check_mode=False) |
| 109 |
lic_obj = self.module.License( |
| 110 |
idrac_connection_license_mock, f_module) |
| 111 |
data = lic_obj.get_license_url() |
| 112 |
assert data == "/redfish/v1/LicenseService/Licenses" |
| 113 |
|
| 114 |
def test_get_job_status_success(self, mocker, idrac_license_mock): |
| 115 |
|
| 116 |
module_mock = self.get_module_mock() |
| 117 |
license_job_response_mock = mocker.MagicMock() |
| 118 |
license_job_response_mock.headers.get.return_value = "HTTPS_PATH/job_tracking/12345" |
| 119 |
|
| 120 |
mocker.patch(MODULE_PATH + "remove_key", return_value={"job_details": "mocked_job_details"}) |
| 121 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", return_value=[MANAGER_URI_ONE]) |
| 122 |
|
| 123 |
|
| 124 |
obj_under_test = self.module.License(idrac_license_mock, module_mock) |
| 125 |
|
| 126 |
|
| 127 |
mocker.patch(MODULE_PATH + "idrac_redfish_job_tracking", return_value=(False, "mocked_message", {"job_details": "mocked_job_details"}, 0)) |
| 128 |
|
| 129 |
|
| 130 |
result = obj_under_test.get_job_status(license_job_response_mock) |
| 131 |
|
| 132 |
|
| 133 |
assert result == {"job_details": "mocked_job_details"} |
| 134 |
|
| 135 |
def test_get_job_status_failure(self, mocker, idrac_license_mock): |
| 136 |
|
| 137 |
module_mock = self.get_module_mock() |
| 138 |
license_job_response_mock = mocker.MagicMock() |
| 139 |
license_job_response_mock.headers.get.return_value = "HTTPS_PATH/job_tracking/12345" |
| 140 |
|
| 141 |
mocker.patch(MODULE_PATH + "remove_key", return_value={"Message": "None"}) |
| 142 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", return_value=[MANAGER_URI_ONE]) |
| 143 |
|
| 144 |
|
| 145 |
obj_under_test = self.module.License(idrac_license_mock, module_mock) |
| 146 |
|
| 147 |
|
| 148 |
mocker.patch(MODULE_PATH + "idrac_redfish_job_tracking", return_value=(True, "None", {"Message": "None"}, 0)) |
| 149 |
|
| 150 |
|
| 151 |
exit_json_mock = mocker.patch.object(module_mock, "exit_json") |
| 152 |
|
| 153 |
|
| 154 |
result = obj_under_test.get_job_status(license_job_response_mock) |
| 155 |
|
| 156 |
|
| 157 |
exit_json_mock.assert_called_once_with(msg="None", failed=True, job_details={"Message": "None"}) |
| 158 |
assert result == {"Message": "None"} |
| 159 |
|
| 160 |
def test_get_share_details(self, idrac_connection_license_mock): |
| 161 |
|
| 162 |
module_mock = MagicMock() |
| 163 |
module_mock.params.get.return_value = { |
| 164 |
'ip_address': 'XX.XX.XX.XX', |
| 165 |
'share_name': 'my_share', |
| 166 |
'username': 'my_user', |
| 167 |
'password': 'my_password' |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
lic_obj = self.module.License(idrac_connection_license_mock, module_mock) |
| 172 |
|
| 173 |
|
| 174 |
result = lic_obj.get_share_details() |
| 175 |
|
| 176 |
|
| 177 |
assert result == { |
| 178 |
'IPAddress': 'XX.XX.XX.XX', |
| 179 |
'ShareName': 'my_share', |
| 180 |
'UserName': 'my_user', |
| 181 |
'Password': 'my_password' |
| 182 |
} |
| 183 |
|
| 184 |
def test_get_proxy_details(self, idrac_connection_license_mock): |
| 185 |
|
| 186 |
module_mock = MagicMock() |
| 187 |
module_mock.params.get.return_value = { |
| 188 |
'ip_address': 'XX.XX.XX.XX', |
| 189 |
'share_name': 'my_share', |
| 190 |
'username': 'my_user', |
| 191 |
'password': 'my_password', |
| 192 |
'share_type': 'http', |
| 193 |
'ignore_certificate_warning': 'off', |
| 194 |
'proxy_support': 'parameters_proxy', |
| 195 |
'proxy_type': 'http', |
| 196 |
'proxy_server': 'proxy.example.com', |
| 197 |
'proxy_port': 8080, |
| 198 |
'proxy_username': 'my_username', |
| 199 |
'proxy_password': 'my_password' |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
lic_obj = self.module.License(idrac_connection_license_mock, module_mock) |
| 204 |
|
| 205 |
|
| 206 |
result = lic_obj.get_proxy_details() |
| 207 |
|
| 208 |
|
| 209 |
expected_result = { |
| 210 |
'ShareType': 'HTTP', |
| 211 |
'IgnoreCertWarning': 'Off', |
| 212 |
'ProxySupport': 'ParametersProxy', |
| 213 |
'ProxyType': 'HTTP', |
| 214 |
'ProxyServer': 'proxy.example.com', |
| 215 |
'ProxyPort': '8080', |
| 216 |
'ProxyUname': 'my_username', |
| 217 |
'ProxyPasswd': 'my_password' |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
assert result == expected_result |
| 222 |
|
| 223 |
|
| 224 |
class TestDeleteLicense: |
| 225 |
@pytest.fixture |
| 226 |
def delete_license_mock(self): |
| 227 |
delete_license_obj = MagicMock() |
| 228 |
return delete_license_obj |
| 229 |
|
| 230 |
@pytest.fixture |
| 231 |
def idrac_connection_license_mock(self, mocker, delete_license_mock): |
| 232 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 233 |
return_value=delete_license_mock) |
| 234 |
idrac_conn_mock.return_value.__enter__.return_value = delete_license_mock |
| 235 |
return idrac_conn_mock |
| 236 |
|
| 237 |
def test_execute_delete_license_success(self, mocker, idrac_connection_license_mock): |
| 238 |
mocker.patch(MODULE_PATH + LIC_GET_LICENSE_URL, |
| 239 |
return_value=REDFISH_LICENSE_URL) |
| 240 |
f_module = MagicMock() |
| 241 |
f_module.params = {'license_id': '1234'} |
| 242 |
delete_license_obj = idrac_license.DeleteLicense(idrac_connection_license_mock, f_module) |
| 243 |
delete_license_obj.idrac.invoke_request.return_value.status_code = 204 |
| 244 |
delete_license_obj.execute() |
| 245 |
f_module.exit_json.assert_called_once_with(msg=SUCCESS_DELETE_MSG, changed=True) |
| 246 |
|
| 247 |
def test_execute_delete_license_failure(self, mocker, idrac_connection_license_mock): |
| 248 |
mocker.patch(MODULE_PATH + LIC_GET_LICENSE_URL, |
| 249 |
return_value=REDFISH_LICENSE_URL) |
| 250 |
f_module = MagicMock() |
| 251 |
f_module.params = {'license_id': '5678'} |
| 252 |
delete_license_obj = idrac_license.DeleteLicense(idrac_connection_license_mock, f_module) |
| 253 |
delete_license_obj.idrac.invoke_request.return_value.status_code = 404 |
| 254 |
delete_license_obj.execute() |
| 255 |
f_module.exit_json.assert_called_once_with(msg=FAILURE_MSG.format(operation="delete", license_id="5678"), failed=True) |
| 256 |
|
| 257 |
|
| 258 |
class TestExportLicense(FakeAnsibleModule): |
| 259 |
module = idrac_license |
| 260 |
|
| 261 |
@pytest.fixture |
| 262 |
def idrac_license_mock(self): |
| 263 |
idrac_obj = MagicMock() |
| 264 |
return idrac_obj |
| 265 |
|
| 266 |
@pytest.fixture |
| 267 |
def idrac_connection_license_mock(self, mocker, idrac_license_mock): |
| 268 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 269 |
return_value=idrac_license_mock) |
| 270 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_license_mock |
| 271 |
return idrac_conn_mock |
| 272 |
|
| 273 |
def test_export_license_local(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 274 |
tmp_path = tempfile.gettempdir() |
| 275 |
export_params = { |
| 276 |
'license_id': 'test_license_id', |
| 277 |
'share_parameters': { |
| 278 |
'share_name': str(tmp_path), |
| 279 |
'file_name': 'test_lic.xml' |
| 280 |
} |
| 281 |
} |
| 282 |
idr_obj = MagicMock() |
| 283 |
idr_obj.body = "<lns:LicenseData><lns:EntitlementID>ansible_open_manage</lns:EntitlementID></lns:LicenseData>".encode('utf-8') |
| 284 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 285 |
return_value=idr_obj) |
| 286 |
idrac_default_args.update(export_params) |
| 287 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 288 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 289 |
result = export_license_obj._ExportLicense__export_license_local(EXPORT_URL_MOCK) |
| 290 |
assert result.body == b"<lns:LicenseData><lns:EntitlementID>ansible_open_manage</lns:EntitlementID></lns:LicenseData>" |
| 291 |
assert os.path.exists(f"{tmp_path}/test_lic.xml") |
| 292 |
if os.path.exists(f"{tmp_path}/test_lic.xml"): |
| 293 |
os.remove(f"{tmp_path}/test_lic.xml") |
| 294 |
|
| 295 |
export_params = { |
| 296 |
'license_id': 'test_license_id', |
| 297 |
'share_parameters': { |
| 298 |
'share_name': str(tmp_path), |
| 299 |
} |
| 300 |
} |
| 301 |
idrac_default_args.update(export_params) |
| 302 |
result = export_license_obj._ExportLicense__export_license_local(EXPORT_URL_MOCK) |
| 303 |
assert result.body == b"<lns:LicenseData><lns:EntitlementID>ansible_open_manage</lns:EntitlementID></lns:LicenseData>" |
| 304 |
assert os.path.exists(f"{tmp_path}/test_license_id_iDRAC_license.xml") |
| 305 |
if os.path.exists(f"{tmp_path}/test_license_id_iDRAC_license.xml"): |
| 306 |
os.remove(f"{tmp_path}/test_license_id_iDRAC_license.xml") |
| 307 |
|
| 308 |
def test_export_license_http(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 309 |
export_params = { |
| 310 |
'license_id': 'test_license_id', |
| 311 |
'share_parameters': { |
| 312 |
'file_name': 'test_lic', |
| 313 |
'share_type': 'http', |
| 314 |
'ignore_certificate_warning': 'off' |
| 315 |
} |
| 316 |
} |
| 317 |
idr_obj = MagicMock() |
| 318 |
idr_obj.json_data = {"license_id": "1234", "LicenseFile": "test_license_content"} |
| 319 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 320 |
return_value=idr_obj) |
| 321 |
idrac_default_args.update(export_params) |
| 322 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 323 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 324 |
result = export_license_obj._ExportLicense__export_license_http(EXPORT_URL_MOCK) |
| 325 |
assert result.json_data == {'LicenseFile': 'test_license_content', 'license_id': '1234'} |
| 326 |
|
| 327 |
def test_export_license_cifs(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 328 |
export_params = { |
| 329 |
'license_id': 'test_license_id', |
| 330 |
'share_parameters': { |
| 331 |
'file_name': 'test_lic', |
| 332 |
'share_type': 'cifs', |
| 333 |
'ignore_certificate_warning': 'off', |
| 334 |
'workgroup': "mydomain" |
| 335 |
} |
| 336 |
} |
| 337 |
idr_obj = MagicMock() |
| 338 |
idr_obj.json_data = {"license_id": "1234", "LicenseFile": "test_license_content"} |
| 339 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 340 |
return_value=idr_obj) |
| 341 |
idrac_default_args.update(export_params) |
| 342 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 343 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 344 |
result = export_license_obj._ExportLicense__export_license_cifs(EXPORT_URL_MOCK) |
| 345 |
assert result.json_data == {'LicenseFile': 'test_license_content', 'license_id': '1234'} |
| 346 |
|
| 347 |
def test_export_license_nfs(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 348 |
export_params = { |
| 349 |
'license_id': 'test_license_id', |
| 350 |
'share_parameters': { |
| 351 |
'file_name': 'test_lic', |
| 352 |
'share_type': 'nfs', |
| 353 |
'ignore_certificate_warning': 'off' |
| 354 |
} |
| 355 |
} |
| 356 |
idr_obj = MagicMock() |
| 357 |
idr_obj.json_data = {"license_id": "1234", "LicenseFile": "test_license_content"} |
| 358 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 359 |
return_value=idr_obj) |
| 360 |
idrac_default_args.update(export_params) |
| 361 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 362 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 363 |
result = export_license_obj._ExportLicense__export_license_nfs(EXPORT_URL_MOCK) |
| 364 |
assert result.json_data == {'LicenseFile': 'test_license_content', 'license_id': '1234'} |
| 365 |
|
| 366 |
def test_get_export_license_url_local(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 367 |
export_params = { |
| 368 |
'license_id': 'test_license_id', |
| 369 |
'share_parameters': { |
| 370 |
'file_name': 'test_lic', |
| 371 |
'share_type': 'local', |
| 372 |
'ignore_certificate_warning': 'off' |
| 373 |
} |
| 374 |
} |
| 375 |
mocker.patch(MODULE_PATH + LIC_GET_LICENSE_URL, return_value=REDFISH_LICENSE_URL) |
| 376 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 377 |
idr_obj = MagicMock() |
| 378 |
idr_obj.json_data = {"Id": "test_license_id", |
| 379 |
"DownloadURI": "/redfish/v1/LicenseService/Licenses/test_license_id/DownloadURI"} |
| 380 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, return_value=idr_obj) |
| 381 |
idrac_default_args.update(export_params) |
| 382 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 383 |
result = export_license_obj._ExportLicense__get_export_license_url() |
| 384 |
assert result == "/redfish/v1/LicenseService/Licenses/test_license_id/DownloadURI" |
| 385 |
|
| 386 |
def test_get_export_license_url_share(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 387 |
export_params = { |
| 388 |
'license_id': 'test_license_id', |
| 389 |
'share_parameters': { |
| 390 |
'ip_address': 'XX.XX.XX.XX', |
| 391 |
'share_name': 'myshare', |
| 392 |
'share_type': 'nfs', |
| 393 |
'ignore_certificate_warning': 'off' |
| 394 |
} |
| 395 |
} |
| 396 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", |
| 397 |
return_value=(REDFISH, None)) |
| 398 |
mocker.patch( |
| 399 |
MODULE_PATH + "get_dynamic_uri", |
| 400 |
return_value={ |
| 401 |
"Links": { |
| 402 |
"Oem": { |
| 403 |
"Dell": { |
| 404 |
"DellLicenseManagementService": { |
| 405 |
ODATA: "/LicenseService" |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
}, |
| 410 |
"Actions": { |
| 411 |
"#DellLicenseManagementService.ExportLicenseToNetworkShare": { |
| 412 |
"target": ( |
| 413 |
"/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/" |
| 414 |
"DellLicenseManagementService/Actions/" |
| 415 |
"DellLicenseManagementService.ExportLicenseToNetworkShare" |
| 416 |
) |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
) |
| 421 |
idrac_default_args.update(export_params) |
| 422 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 423 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 424 |
result = export_license_obj._ExportLicense__get_export_license_url() |
| 425 |
assert result == "/redfish/v1/Managers/iDRAC.Embedded.1/Oem/Dell/" + \ |
| 426 |
"DellLicenseManagementService/Actions/DellLicenseManagementService.ExportLicenseToNetworkShare" |
| 427 |
|
| 428 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", |
| 429 |
return_value=(REDFISH, "error")) |
| 430 |
with pytest.raises(Exception) as exc: |
| 431 |
export_license_obj._ExportLicense__get_export_license_url() |
| 432 |
assert exc.value.args[0] == "error" |
| 433 |
|
| 434 |
def test_execute(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 435 |
share_type = 'local' |
| 436 |
export_params = { |
| 437 |
'license_id': 'test_license_id', |
| 438 |
'share_parameters': { |
| 439 |
'file_name': 'test_lic', |
| 440 |
'share_type': share_type |
| 441 |
} |
| 442 |
} |
| 443 |
idrac_default_args.update(export_params) |
| 444 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 445 |
mocker.patch(MODULE_PATH + "License.check_license_id") |
| 446 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__get_export_license_url", |
| 447 |
return_value="/License/url") |
| 448 |
mocker.patch(MODULE_PATH + "ExportLicense.get_job_status", |
| 449 |
return_value={"JobId": "JID1234"}) |
| 450 |
idr_obj = MagicMock() |
| 451 |
idr_obj.status_code = 200 |
| 452 |
|
| 453 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__export_license_local", |
| 454 |
return_value=idr_obj) |
| 455 |
export_license_obj = self.module.ExportLicense(idrac_connection_license_mock, f_module) |
| 456 |
with pytest.raises(Exception) as exc: |
| 457 |
export_license_obj.execute() |
| 458 |
assert exc.value.args[0] == SUCCESS_EXPORT_MSG |
| 459 |
|
| 460 |
export_params.get('share_parameters')["share_type"] = "http" |
| 461 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__export_license_http", |
| 462 |
return_value=idr_obj) |
| 463 |
with pytest.raises(Exception) as exc: |
| 464 |
export_license_obj.execute() |
| 465 |
assert exc.value.args[0] == SUCCESS_EXPORT_MSG |
| 466 |
|
| 467 |
export_params.get('share_parameters')["share_type"] = "cifs" |
| 468 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__export_license_cifs", |
| 469 |
return_value=idr_obj) |
| 470 |
with pytest.raises(Exception) as exc: |
| 471 |
export_license_obj.execute() |
| 472 |
assert exc.value.args[0] == SUCCESS_EXPORT_MSG |
| 473 |
|
| 474 |
export_params.get('share_parameters')["share_type"] = "nfs" |
| 475 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__export_license_nfs", |
| 476 |
return_value=idr_obj) |
| 477 |
with pytest.raises(Exception) as exc: |
| 478 |
export_license_obj.execute() |
| 479 |
assert exc.value.args[0] == SUCCESS_EXPORT_MSG |
| 480 |
|
| 481 |
export_params.get('share_parameters')["share_type"] = "https" |
| 482 |
idr_obj.status_code = 400 |
| 483 |
mocker.patch(MODULE_PATH + "ExportLicense._ExportLicense__export_license_http", |
| 484 |
return_value=idr_obj) |
| 485 |
with pytest.raises(Exception) as exc: |
| 486 |
export_license_obj.execute() |
| 487 |
assert exc.value.args[0] == FAILURE_MSG.format(operation="export", license_id="test_license_id") |
| 488 |
|
| 489 |
|
| 490 |
class TestImportLicense(FakeAnsibleModule): |
| 491 |
module = idrac_license |
| 492 |
|
| 493 |
@pytest.fixture |
| 494 |
def idrac_license_mock(self): |
| 495 |
idrac_obj = MagicMock() |
| 496 |
return idrac_obj |
| 497 |
|
| 498 |
@pytest.fixture |
| 499 |
def idrac_connection_license_mock(self, mocker, idrac_license_mock): |
| 500 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 501 |
return_value=idrac_license_mock) |
| 502 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_license_mock |
| 503 |
return idrac_conn_mock |
| 504 |
|
| 505 |
def test_execute(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 506 |
share_type = 'local' |
| 507 |
import_params = { |
| 508 |
'license_id': 'test_license_id', |
| 509 |
'share_parameters': { |
| 510 |
'file_name': 'test_lic.xml', |
| 511 |
'share_type': share_type |
| 512 |
} |
| 513 |
} |
| 514 |
idrac_default_args.update(import_params) |
| 515 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 516 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__get_import_license_url", |
| 517 |
return_value="/License/url") |
| 518 |
mocker.patch(MODULE_PATH + "get_manager_res_id", |
| 519 |
return_value=IDRAC_ID) |
| 520 |
mocker.patch(MODULE_PATH + "ImportLicense.get_job_status", |
| 521 |
return_value={"JobId": "JID1234"}) |
| 522 |
idr_obj = MagicMock() |
| 523 |
idr_obj.status_code = 200 |
| 524 |
|
| 525 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__import_license_local", |
| 526 |
return_value=idr_obj) |
| 527 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 528 |
with pytest.raises(Exception) as exc: |
| 529 |
import_license_obj.execute() |
| 530 |
assert exc.value.args[0] == SUCCESS_IMPORT_MSG |
| 531 |
|
| 532 |
import_params.get('share_parameters')["share_type"] = "http" |
| 533 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__import_license_http", |
| 534 |
return_value=idr_obj) |
| 535 |
with pytest.raises(Exception) as exc: |
| 536 |
import_license_obj.execute() |
| 537 |
assert exc.value.args[0] == SUCCESS_IMPORT_MSG |
| 538 |
|
| 539 |
import_params.get('share_parameters')["share_type"] = "cifs" |
| 540 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__import_license_cifs", |
| 541 |
return_value=idr_obj) |
| 542 |
with pytest.raises(Exception) as exc: |
| 543 |
import_license_obj.execute() |
| 544 |
assert exc.value.args[0] == SUCCESS_IMPORT_MSG |
| 545 |
|
| 546 |
import_params.get('share_parameters')["share_type"] = "nfs" |
| 547 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__import_license_nfs", |
| 548 |
return_value=idr_obj) |
| 549 |
with pytest.raises(Exception) as exc: |
| 550 |
import_license_obj.execute() |
| 551 |
assert exc.value.args[0] == SUCCESS_IMPORT_MSG |
| 552 |
|
| 553 |
import_params.get('share_parameters')["share_type"] = "https" |
| 554 |
idr_obj.status_code = 400 |
| 555 |
mocker.patch(MODULE_PATH + "ImportLicense._ImportLicense__import_license_http", |
| 556 |
return_value=idr_obj) |
| 557 |
with pytest.raises(Exception) as exc: |
| 558 |
import_license_obj.execute() |
| 559 |
assert exc.value.args[0] == FAILURE_IMPORT_MSG |
| 560 |
|
| 561 |
def test_import_license_local(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 562 |
tmp_path = tempfile.gettempdir() |
| 563 |
import_params = { |
| 564 |
'license_id': 'test_license_id', |
| 565 |
'share_parameters': { |
| 566 |
'share_name': 'doesnotexistpath', |
| 567 |
'file_name': LIC_FILE_NAME |
| 568 |
} |
| 569 |
} |
| 570 |
idrac_default_args.update(import_params) |
| 571 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 572 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 573 |
with pytest.raises(Exception) as exc: |
| 574 |
import_license_obj._ImportLicense__import_license_local(EXPORT_URL_MOCK, IDRAC_ID) |
| 575 |
assert exc.value.args[0] == INVALID_DIRECTORY_MSG.format(path='doesnotexistpath') |
| 576 |
|
| 577 |
import_params = { |
| 578 |
'license_id': 'test_license_id', |
| 579 |
'share_parameters': { |
| 580 |
'share_name': str(tmp_path), |
| 581 |
'file_name': LIC_FILE_NAME |
| 582 |
} |
| 583 |
} |
| 584 |
file_name = os.path.join(tmp_path, LIC_FILE_NAME) |
| 585 |
with open(file_name, "w") as fp: |
| 586 |
fp.writelines("license_file") |
| 587 |
idr_obj = MagicMock() |
| 588 |
idr_obj.json_data = {"license_id": "1234", "LicenseFile": "test_license_content"} |
| 589 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 590 |
return_value=idr_obj) |
| 591 |
idrac_default_args.update(import_params) |
| 592 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 593 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 594 |
result = import_license_obj._ImportLicense__import_license_local(EXPORT_URL_MOCK, IDRAC_ID) |
| 595 |
assert result.json_data == {'LicenseFile': 'test_license_content', 'license_id': '1234'} |
| 596 |
assert os.path.exists(file_name) |
| 597 |
|
| 598 |
json_str = to_text(json.dumps({"error": {'@Message.ExtendedInfo': [ |
| 599 |
{ |
| 600 |
'MessageId': "LIC018", |
| 601 |
"Message": "Already imported" |
| 602 |
} |
| 603 |
]}})) |
| 604 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 605 |
side_effect=HTTPError(HTTPS_PATH, 400, HTTP_ERROR, |
| 606 |
{"accept-type": APPLICATION_JSON}, StringIO(json_str))) |
| 607 |
with pytest.raises(Exception) as exc: |
| 608 |
import_license_obj._ImportLicense__import_license_local(EXPORT_URL_MOCK, IDRAC_ID) |
| 609 |
assert exc.value.args[0] == "Already imported" |
| 610 |
|
| 611 |
if os.path.exists(file_name): |
| 612 |
os.remove(file_name) |
| 613 |
|
| 614 |
def test_import_license_http(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 615 |
import_params = { |
| 616 |
'license_id': 'test_license_id', |
| 617 |
'share_parameters': { |
| 618 |
'ip_address': 'XX.XX.XX.XX', |
| 619 |
'share_name': '/ansible', |
| 620 |
'file_name': 'test_lic', |
| 621 |
'share_type': 'http', |
| 622 |
'username': 'my_user', |
| 623 |
'password': 'my_password', |
| 624 |
'ignore_certificate_warning': 'off' |
| 625 |
} |
| 626 |
} |
| 627 |
expected_result = { |
| 628 |
"@Message.ExtendedInfo": [ |
| 629 |
{ |
| 630 |
"Message": "The request completed successfully.", |
| 631 |
"Severity": "OK" |
| 632 |
}, |
| 633 |
{ |
| 634 |
"Message": "The operation successfully completed.", |
| 635 |
"Severity": "Informational" |
| 636 |
} |
| 637 |
] |
| 638 |
} |
| 639 |
idr_obj = MagicMock() |
| 640 |
idr_obj.json_data = expected_result |
| 641 |
|
| 642 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 643 |
return_value=idr_obj) |
| 644 |
idrac_default_args.update(import_params) |
| 645 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 646 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 647 |
result = import_license_obj._ImportLicense__import_license_http(IMPORT_URL_MOCK) |
| 648 |
assert result.json_data == expected_result |
| 649 |
|
| 650 |
def test_import_license_cifs(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 651 |
import_params = { |
| 652 |
'license_id': 'test_license_id', |
| 653 |
'share_parameters': { |
| 654 |
'ip_address': 'XX.XX.XX.XX', |
| 655 |
'share_name': '/ansible', |
| 656 |
'file_name': 'test_lic', |
| 657 |
'share_type': 'cifs', |
| 658 |
'username': 'my_user', |
| 659 |
'password': 'my_password', |
| 660 |
'ignore_certificate_warning': 'off', |
| 661 |
'workgroup': 'mydomain' |
| 662 |
} |
| 663 |
} |
| 664 |
expected_result = { |
| 665 |
"@Message.ExtendedInfo": [ |
| 666 |
{ |
| 667 |
"Message": "The request completed successfully.", |
| 668 |
"Severity": "OK" |
| 669 |
}, |
| 670 |
{ |
| 671 |
"Message": "The operation successfully completed.", |
| 672 |
"Severity": "Informational" |
| 673 |
} |
| 674 |
] |
| 675 |
} |
| 676 |
idr_obj = MagicMock() |
| 677 |
idr_obj.json_data = expected_result |
| 678 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 679 |
return_value=idr_obj) |
| 680 |
idrac_default_args.update(import_params) |
| 681 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 682 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 683 |
result = import_license_obj._ImportLicense__import_license_cifs(IMPORT_URL_MOCK) |
| 684 |
assert result.json_data == expected_result |
| 685 |
|
| 686 |
def test_import_license_nfs(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 687 |
import_params = { |
| 688 |
'license_id': 'test_license_id', |
| 689 |
'share_parameters': { |
| 690 |
'ip_address': 'XX.XX.XX.XX', |
| 691 |
'share_name': '/ansible', |
| 692 |
'file_name': 'test_lic', |
| 693 |
'share_type': 'nfs', |
| 694 |
'ignore_certificate_warning': 'off', |
| 695 |
} |
| 696 |
} |
| 697 |
expected_result = { |
| 698 |
"@Message.ExtendedInfo": [ |
| 699 |
{ |
| 700 |
"Message": "The request completed successfully.", |
| 701 |
"Severity": "OK" |
| 702 |
}, |
| 703 |
{ |
| 704 |
"Message": "The operation successfully completed.", |
| 705 |
"Severity": "Informational" |
| 706 |
} |
| 707 |
] |
| 708 |
} |
| 709 |
idr_obj = MagicMock() |
| 710 |
idr_obj.json_data = expected_result |
| 711 |
mocker.patch(MODULE_PATH + API_INVOKE_MOCKER, |
| 712 |
return_value=idr_obj) |
| 713 |
idrac_default_args.update(import_params) |
| 714 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 715 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 716 |
result = import_license_obj._ImportLicense__import_license_nfs(IMPORT_URL_MOCK) |
| 717 |
assert result.json_data == expected_result |
| 718 |
|
| 719 |
def test_get_import_license_url_local(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 720 |
import_params = { |
| 721 |
'license_id': 'test_license_id', |
| 722 |
'share_parameters': { |
| 723 |
'file_name': 'test_lic', |
| 724 |
'share_type': 'local', |
| 725 |
'ignore_certificate_warning': 'off' |
| 726 |
} |
| 727 |
} |
| 728 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", |
| 729 |
return_value=(REDFISH, None)) |
| 730 |
mocker.patch(MODULE_PATH + "get_dynamic_uri", |
| 731 |
return_value={"Links": {"Oem": {"Dell": {"DellLicenseManagementService": {ODATA: "/LicenseService"}}}}, |
| 732 |
"Actions": {"#DellLicenseManagementService.ImportLicense": {"target": API_ONE}}}) |
| 733 |
idrac_default_args.update(import_params) |
| 734 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 735 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 736 |
result = import_license_obj._ImportLicense__get_import_license_url() |
| 737 |
assert result == API_ONE |
| 738 |
|
| 739 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", |
| 740 |
return_value=(REDFISH, "error")) |
| 741 |
with pytest.raises(Exception) as exc: |
| 742 |
import_license_obj._ImportLicense__get_import_license_url() |
| 743 |
assert exc.value.args[0] == "error" |
| 744 |
|
| 745 |
def test_get_import_license_url_share(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 746 |
import_params = { |
| 747 |
'license_id': 'test_license_id', |
| 748 |
'share_parameters': { |
| 749 |
'ip_address': 'XX.XX.XX.XX', |
| 750 |
'share_name': '/ansible_share', |
| 751 |
'file_name': 'test_lic', |
| 752 |
'share_type': 'nfs', |
| 753 |
'ignore_certificate_warning': 'off' |
| 754 |
} |
| 755 |
} |
| 756 |
mocker.patch(MODULE_PATH + "get_dynamic_uri", |
| 757 |
return_value={"LicenseService": {"@odata.id": "/redfish/v1/LicenseService"}, |
| 758 |
"Actions": {"#LicenseService.Install": {"target": "/redfish/v1/LicenseService/Actions/LicenseService.Install"}}}) |
| 759 |
idrac_default_args.update(import_params) |
| 760 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 761 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 762 |
result = import_license_obj._ImportLicense__get_import_license_url() |
| 763 |
assert result == "/redfish/v1/LicenseService/Actions/LicenseService.Install" |
| 764 |
|
| 765 |
def test_get_job_status(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 766 |
mocker.patch(MODULE_PATH + "validate_and_get_first_resource_id_uri", return_value=[MANAGER_URI_ONE]) |
| 767 |
lic_job_resp_obj = MagicMock() |
| 768 |
lic_job_resp_obj.headers = {"Location": "idrac_internal"} |
| 769 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 770 |
import_license_obj = self.module.ImportLicense(idrac_connection_license_mock, f_module) |
| 771 |
|
| 772 |
mocker.patch(MODULE_PATH + "idrac_redfish_job_tracking", return_value=(False, "None", {"JobId": "JID1234"}, 0)) |
| 773 |
result = import_license_obj.get_job_status(lic_job_resp_obj) |
| 774 |
assert result == {"JobId": "JID1234"} |
| 775 |
|
| 776 |
mocker.patch(MODULE_PATH + "idrac_redfish_job_tracking", return_value=(True, "None", {"Message": "Got LIC018", |
| 777 |
"MessageId": "LIC018"}, 0)) |
| 778 |
with pytest.raises(Exception) as exc: |
| 779 |
import_license_obj.get_job_status(lic_job_resp_obj) |
| 780 |
assert exc.value.args[0] == "Got LIC018" |
| 781 |
|
| 782 |
mocker.patch(MODULE_PATH + "idrac_redfish_job_tracking", return_value=(True, "None", {"Message": "Got LIC019", |
| 783 |
"MessageId": "LIC019"}, 0)) |
| 784 |
with pytest.raises(Exception) as exc: |
| 785 |
import_license_obj.get_job_status(lic_job_resp_obj) |
| 786 |
assert exc.value.args[0] == "Got LIC019" |
| 787 |
|
| 788 |
|
| 789 |
class TestLicenseType(FakeAnsibleModule): |
| 790 |
module = idrac_license |
| 791 |
|
| 792 |
@pytest.fixture |
| 793 |
def idrac_license_mock(self): |
| 794 |
idrac_obj = MagicMock() |
| 795 |
return idrac_obj |
| 796 |
|
| 797 |
@pytest.fixture |
| 798 |
def idrac_connection_license_mock(self, mocker, idrac_license_mock): |
| 799 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', |
| 800 |
return_value=idrac_license_mock) |
| 801 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_license_mock |
| 802 |
return idrac_conn_mock |
| 803 |
|
| 804 |
def test_license_operation(self, idrac_default_args, idrac_connection_license_mock, mocker): |
| 805 |
idrac_default_args.update({"import": False, "export": False, "delete": True}) |
| 806 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 807 |
lic_class = self.module.LicenseType.license_operation(idrac_connection_license_mock, f_module) |
| 808 |
assert isinstance(lic_class, self.module.DeleteLicense) |
| 809 |
|
| 810 |
idrac_default_args.update({"import": False, "export": True, "delete": False}) |
| 811 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 812 |
lic_class = self.module.LicenseType.license_operation(idrac_connection_license_mock, f_module) |
| 813 |
assert isinstance(lic_class, self.module.ExportLicense) |
| 814 |
|
| 815 |
idrac_default_args.update({"import": True, "export": False, "delete": False}) |
| 816 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 817 |
lic_class = self.module.LicenseType.license_operation(idrac_connection_license_mock, f_module) |
| 818 |
assert isinstance(lic_class, self.module.ImportLicense) |
| 819 |
|
| 820 |
def test_main(self, mocker): |
| 821 |
module_mock = mocker.MagicMock() |
| 822 |
idrac_mock = mocker.MagicMock() |
| 823 |
license_mock = mocker.MagicMock() |
| 824 |
|
| 825 |
|
| 826 |
mocker.patch(MODULE_PATH + 'get_argument_spec', return_value={}) |
| 827 |
mocker.patch(MODULE_PATH + 'IdracAnsibleModule', return_value=module_mock) |
| 828 |
mocker.patch(MODULE_PATH + 'iDRACRedfishAPI', return_value=idrac_mock) |
| 829 |
mocker.patch(MODULE_PATH + 'LicenseType.license_operation', return_value=license_mock) |
| 830 |
main() |