| 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 |
|
| 17 |
import pytest |
| 18 |
from urllib.error import URLError, HTTPError |
| 19 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 20 |
from io import StringIO |
| 21 |
from ansible.module_utils._text import to_text |
| 22 |
from ssl import SSLError |
| 23 |
from ansible_collections.dellemc.openmanage.plugins.modules import ome_application_certificate |
| 24 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 25 |
|
| 26 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 27 |
MODULE_UTILS_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.utils.' |
| 28 |
EMAIL_ADDRESS = "[email protected]" |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def ome_connection_mock_for_application_certificate(mocker, ome_response_mock): |
| 33 |
connection_class_mock = mocker.patch( |
| 34 |
MODULE_PATH + 'ome_application_certificate.RestOME') |
| 35 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 36 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 37 |
return ome_connection_mock_obj |
| 38 |
|
| 39 |
|
| 40 |
class TestOmeAppCSR(FakeAnsibleModule): |
| 41 |
module = ome_application_certificate |
| 42 |
|
| 43 |
@pytest.mark.parametrize("exc_type", |
| 44 |
[ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError]) |
| 45 |
def test_ome_application_certificate_main_error_cases(self, exc_type, mocker, ome_default_args, |
| 46 |
ome_connection_mock_for_application_certificate, |
| 47 |
ome_response_mock): |
| 48 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 49 |
args = {"command": "generate_csr", "distinguished_name": "hostname.com", |
| 50 |
"department_name": "Remote Access Group", "business_name": "Dell Inc.", |
| 51 |
"locality": "Round Rock", "country_state": "Texas", "country": "US", |
| 52 |
"email": EMAIL_ADDRESS, "subject_alternative_names": "XX.XX.XX.XX"} |
| 53 |
ome_default_args.update(args) |
| 54 |
if exc_type == URLError: |
| 55 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 56 |
side_effect=exc_type("TEST")) |
| 57 |
result = self._run_module(ome_default_args) |
| 58 |
assert result["unreachable"] is True |
| 59 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 60 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 61 |
side_effect=exc_type("exception message")) |
| 62 |
result = self._run_module(ome_default_args) |
| 63 |
assert result['failed'] is True |
| 64 |
else: |
| 65 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 66 |
side_effect=exc_type('https://testhost.com', 400, |
| 67 |
'http error message', |
| 68 |
{"accept-type": "application/json"}, |
| 69 |
StringIO(json_str))) |
| 70 |
result = self._run_module(ome_default_args) |
| 71 |
assert result['failed'] is True |
| 72 |
assert 'csr_status' not in result |
| 73 |
assert 'msg' in result |
| 74 |
|
| 75 |
def test_get_resource_parameters_generate(self, mocker, ome_default_args, |
| 76 |
ome_connection_mock_for_application_certificate, |
| 77 |
ome_response_mock): |
| 78 |
args = {"command": "generate_csr", "distinguished_name": "hostname.com", |
| 79 |
"department_name": "Remote Access Group", "business_name": "Dell Inc.", |
| 80 |
"locality": "Round Rock", "country_state": "Texas", "country": "US", |
| 81 |
"email": EMAIL_ADDRESS, "subject_alternative_names": "XX.XX.XX.XX"} |
| 82 |
f_module = self.get_module_mock(params=args) |
| 83 |
result = self.module.get_resource_parameters(f_module) |
| 84 |
assert result[0] == "POST" |
| 85 |
assert result[1] == "ApplicationService/Actions/ApplicationService.GenerateCSR" |
| 86 |
assert result[2] == {'DistinguishedName': 'hostname.com', 'Locality': 'Round Rock', |
| 87 |
'DepartmentName': 'Remote Access Group', 'BusinessName': 'Dell Inc.', |
| 88 |
'State': 'Texas', 'Country': 'US', 'Email': '[email protected]', |
| 89 |
'San': 'XX.XX.XX.XX'} |
| 90 |
|
| 91 |
def test_empty_san(self, mocker, ome_default_args, |
| 92 |
ome_connection_mock_for_application_certificate, |
| 93 |
ome_response_mock): |
| 94 |
args = {"command": "generate_csr", "distinguished_name": "hostname.com", |
| 95 |
"department_name": "Remote Access Group", "business_name": "Dell Inc.", |
| 96 |
"locality": "Round Rock", "country_state": "Texas", "country": "US", |
| 97 |
"email": EMAIL_ADDRESS, "subject_alternative_names": ""} |
| 98 |
f_module = self.get_module_mock(params=args) |
| 99 |
result = self.module.get_resource_parameters(f_module) |
| 100 |
assert result[0] == "POST" |
| 101 |
assert result[1] == "ApplicationService/Actions/ApplicationService.GenerateCSR" |
| 102 |
assert result[2] == {'DistinguishedName': 'hostname.com', 'Locality': 'Round Rock', |
| 103 |
'DepartmentName': 'Remote Access Group', 'BusinessName': 'Dell Inc.', |
| 104 |
'State': 'Texas', 'Country': 'US', 'Email': '[email protected]', |
| 105 |
'San': ''} |
| 106 |
|
| 107 |
def test_upload_csr_fail01(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 108 |
ome_response_mock): |
| 109 |
args = {"command": "upload", "upload_file": "/path/certificate.cer"} |
| 110 |
f_module = self.get_module_mock(params=args) |
| 111 |
with pytest.raises(Exception) as exc: |
| 112 |
self.module.get_resource_parameters(f_module) |
| 113 |
assert exc.value.args[0] == "No such file or directory." |
| 114 |
|
| 115 |
def test_invalid_command(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 116 |
ome_response_mock): |
| 117 |
args = {"command": "invalid", "upload_file": "/path/certificate.cer"} |
| 118 |
f_module = self.get_module_mock(params=args) |
| 119 |
with pytest.raises(Exception) as exc: |
| 120 |
self.module.get_resource_parameters(f_module) |
| 121 |
assert exc.value.args[0] == "Unknown command: invalid" |
| 122 |
|
| 123 |
def test_upload_csr_success(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 124 |
ome_response_mock): |
| 125 |
payload = "--BEGIN-REQUEST--" |
| 126 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 127 |
return_value=("POST", "ApplicationService/Actions/ApplicationService.UploadCertificate", payload)) |
| 128 |
ome_default_args.update({"command": "upload", "upload_file": "/path/certificate.cer"}) |
| 129 |
ome_response_mock.success = True |
| 130 |
result = self._run_module(ome_default_args) |
| 131 |
assert result['msg'] == "Successfully uploaded application certificate." |
| 132 |
|
| 133 |
def test_upload_csr_failed(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 134 |
ome_response_mock): |
| 135 |
payload = "--BEGIN-REQUEST--" |
| 136 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 137 |
return_value=("POST", "ApplicationService/Actions/ApplicationService.UploadCertificate", payload)) |
| 138 |
ome_default_args.update({"command": "upload", "upload_file": "/path/certificate.cer"}) |
| 139 |
ome_response_mock.success = False |
| 140 |
result = self._run_module(ome_default_args) |
| 141 |
assert result['msg'] == "Request failed" |
| 142 |
|
| 143 |
def test_upload_cert_chain_fail(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 144 |
ome_response_mock): |
| 145 |
args = {"command": "upload_cert_chain", "upload_file": "/path/nonexistent_certificate_chain.p7b"} |
| 146 |
f_module = self.get_module_mock(params=args) |
| 147 |
with pytest.raises(Exception) as exc: |
| 148 |
self.module.get_resource_parameters(f_module) |
| 149 |
assert exc.value.args[0] == "No such file or directory." |
| 150 |
|
| 151 |
def test_upload_cert_chain_success(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 152 |
ome_response_mock): |
| 153 |
payload = "--BEGIN-CERT-CHAIN--" |
| 154 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 155 |
return_value=("POST", "ApplicationService/Actions/ApplicationService.UploadCertChain", payload)) |
| 156 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_ome_version', return_value="4.1.0") |
| 157 |
ome_default_args.update({"command": "upload_cert_chain", "upload_file": "/path/certificate_chain.cer"}) |
| 158 |
result = self._run_module(ome_default_args) |
| 159 |
assert result['msg'] == "Successfully uploaded application certificate." |
| 160 |
|
| 161 |
def test_generate_csr(self, mocker, ome_default_args, ome_connection_mock_for_application_certificate, |
| 162 |
ome_response_mock): |
| 163 |
csr_data = "-----BEGIN CERTIFICATE REQUEST-----MIIFMDCCAxgCAQAwgbAxCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhWaXJnaW5pYTES-----END CERTIFICATE REQUEST-----" |
| 164 |
csr_json = {"CertificateData": csr_data} |
| 165 |
payload = {"DistinguishedName": "hostname.com", "DepartmentName": "Remote Access Group", |
| 166 |
"BusinessName": "Dell Inc.", "Locality": "Round Rock", "State": "Texas", |
| 167 |
"Country": "US", "Email": EMAIL_ADDRESS, "subject_alternative_names": "XX.XX.XX.XX"} |
| 168 |
mocker.patch(MODULE_PATH + 'ome_application_certificate.get_resource_parameters', |
| 169 |
return_value=("POST", "ApplicationService/Actions/ApplicationService.GenerateCSR", payload)) |
| 170 |
ome_default_args.update({"command": "generate_csr", "distinguished_name": "hostname.com", |
| 171 |
"department_name": "Remote Access Group", "business_name": "Dell Inc.", |
| 172 |
"locality": "Round Rock", "country_state": "Texas", "country": "US", |
| 173 |
"email": EMAIL_ADDRESS, "subject_alternative_names": "XX.XX.XX.XX, YY.YY.YY.YY"}) |
| 174 |
ome_response_mock.success = True |
| 175 |
ome_response_mock.json_data = csr_json |
| 176 |
result = self._run_module(ome_default_args) |
| 177 |
assert result['msg'] == "Successfully generated certificate signing request." |
| 178 |
data = '''-----BEGIN CERTIFICATE REQUEST-----\nMIIFMDCCAxgCAQAwgbAxCzAJBgNVBAYTAlVTMREwDwYDVQQIDAhWaXJnaW5pYTES\n-----END CERTIFICATE REQUEST-----''' |
| 179 |
assert result['csr_status']['CertificateData'] == data |