| 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 pytest |
| 17 |
from ssl import SSLError |
| 18 |
from io import StringIO |
| 19 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 20 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 21 |
from ansible.module_utils._text import to_text |
| 22 |
from ansible_collections.dellemc.openmanage.plugins.modules import ome_diagnostics |
| 23 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 24 |
|
| 25 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_diagnostics.' |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def ome_conn_mock_diagnostics(mocker, ome_response_mock): |
| 30 |
connection_class_mock = mocker.patch(MODULE_PATH + 'RestOME') |
| 31 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 32 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 33 |
return ome_connection_mock_obj |
| 34 |
|
| 35 |
|
| 36 |
class TestOMEDiagnostics(FakeAnsibleModule): |
| 37 |
|
| 38 |
module = ome_diagnostics |
| 39 |
|
| 40 |
def test_check_domain_service(self, ome_conn_mock_diagnostics, ome_default_args, mocker): |
| 41 |
f_module = self.get_module_mock() |
| 42 |
result = self.module.check_domain_service(f_module, ome_conn_mock_diagnostics) |
| 43 |
assert result is None |
| 44 |
|
| 45 |
def test_group_validation(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 46 |
f_module = self.get_module_mock(params={"device_group_name": "Servers"}) |
| 47 |
ome_response_mock.json_data = {"value": []} |
| 48 |
with pytest.raises(Exception) as err: |
| 49 |
self.module.group_validation(f_module, ome_conn_mock_diagnostics) |
| 50 |
assert err.value.args[0] == "Unable to complete the operation because the entered target device " \ |
| 51 |
"group name 'Servers' is invalid." |
| 52 |
ome_response_mock.json_data = {"value": [{"Id": 25011, "Type": 1000}]} |
| 53 |
result = self.module.group_validation(f_module, ome_conn_mock_diagnostics) |
| 54 |
assert result == [25011] |
| 55 |
|
| 56 |
def test_group_validation_s1(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 57 |
f_module = self.get_module_mock(params={"device_group_name": "Servers"}) |
| 58 |
ome_response_mock.json_data = {"value": [{"Type": 2000, "Id": 10161}]} |
| 59 |
with pytest.raises(Exception) as err: |
| 60 |
self.module.group_validation(f_module, ome_conn_mock_diagnostics) |
| 61 |
assert err.value.args[0] == "The requested group 'Servers' does not contain devices that support export log." |
| 62 |
|
| 63 |
def test_device_validation(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 64 |
resp = {"report_list": [{"Id": 25014, "DeviceServiceTag": "ZXCVB1", "Type": 1000}]} |
| 65 |
f_module = self.get_module_mock(params={"device_ids": [25011]}) |
| 66 |
ome_conn_mock_diagnostics.get_all_report_details.return_value = resp |
| 67 |
with pytest.raises(Exception) as err: |
| 68 |
self.module.device_validation(f_module, ome_conn_mock_diagnostics) |
| 69 |
assert err.value.args[0] == "Unable to complete the operation because the entered target device " \ |
| 70 |
"id(s) '25011' are invalid." |
| 71 |
resp = {"report_list": [{"Id": 25011, "DeviceServiceTag": "ZXCVB1", "Type": 1000}]} |
| 72 |
ome_conn_mock_diagnostics.get_all_report_details.return_value = resp |
| 73 |
result = self.module.device_validation(f_module, ome_conn_mock_diagnostics) |
| 74 |
assert result == [25011] |
| 75 |
f_module = self.get_module_mock(params={"device_service_tags": ["ZXCVB1"]}) |
| 76 |
result = self.module.device_validation(f_module, ome_conn_mock_diagnostics) |
| 77 |
assert result == [25011] |
| 78 |
resp = {"report_list": [{"Id": 25019, "DeviceServiceTag": "ZXCVB1", "Type": 8000}]} |
| 79 |
ome_conn_mock_diagnostics.get_all_report_details.return_value = resp |
| 80 |
with pytest.raises(Exception) as err: |
| 81 |
self.module.device_validation(f_module, ome_conn_mock_diagnostics) |
| 82 |
assert err.value.args[0] == "The requested device service tag(s) 'ZXCVB1' " \ |
| 83 |
"are not applicable for export log." |
| 84 |
|
| 85 |
def test_extract_log_operation(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 86 |
f_module = self.get_module_mock(params={"log_type": "application", "share_address": "XX.XX.XX.XX", |
| 87 |
"share_type": "NFS", "share_name": "iso", "share_user": "username", |
| 88 |
"share_password": "password", "share_domain": "domain", |
| 89 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], |
| 90 |
"lead_chassis_only": "true"}) |
| 91 |
ome_response_mock.json_data = {"value": [{"Id": 16011, "Type": 2000}]} |
| 92 |
ome_conn_mock_diagnostics.job_submission.return_value = {"Id": 16011} |
| 93 |
ome_conn_mock_diagnostics.get_all_items_with_pagination.return_value = \ |
| 94 |
{"value": [{"DomainRoleTypeValue": "LEAD", "DeviceId": 16011}]} |
| 95 |
result = self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics) |
| 96 |
assert result["Id"] == 16011 |
| 97 |
|
| 98 |
ome_conn_mock_diagnostics.get_all_items_with_pagination.return_value = \ |
| 99 |
{"value": [{"DomainRoleTypeValue": "STANDALONE", "DeviceId": 16011}]} |
| 100 |
result = self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics) |
| 101 |
assert result["Id"] == 16011 |
| 102 |
|
| 103 |
f_module = self.get_module_mock(params={"log_type": "support_assist_collection", "share_address": "XX.XX.XX.XX", |
| 104 |
"share_type": "NFS", "share_name": "iso", "share_user": "username", |
| 105 |
"share_password": "password", "share_domain": "domain", |
| 106 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"]}) |
| 107 |
result = self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics, device_lst=[25012]) |
| 108 |
assert result["Id"] == 16011 |
| 109 |
|
| 110 |
def test_extract_log_operation_member(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 111 |
f_module = self.get_module_mock(params={"log_type": "application", "share_address": "XX.XX.XX.XX", |
| 112 |
"share_type": "NFS", "share_name": "iso", "share_user": "username", |
| 113 |
"share_password": "password", "share_domain": "domain", |
| 114 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], |
| 115 |
"lead_chassis_only": "true"}) |
| 116 |
ome_response_mock.json_data = {"value": [{"Id": 16011, "Type": 2000}]} |
| 117 |
ome_conn_mock_diagnostics.job_submission.return_value = {"Id": 16011} |
| 118 |
ome_conn_mock_diagnostics.get_all_items_with_pagination.return_value = \ |
| 119 |
{"value": [{"DomainRoleTypeValue": "MEMBER", "DeviceId": 16011}]} |
| 120 |
with pytest.raises(Exception) as err: |
| 121 |
self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics) |
| 122 |
assert err.value.args[0] == "There is no device(s) available to export application log." |
| 123 |
|
| 124 |
def test_extract_log_operation_no_lead_chassis(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 125 |
f_module = self.get_module_mock(params={"lead_chassis_only": False, "log_type": "application", |
| 126 |
"share_address": "XX.XX.XX.XX", |
| 127 |
"share_type": "NFS", "share_name": "iso", "share_user": "username", |
| 128 |
"share_password": "password", "share_domain": "domain", |
| 129 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], }) |
| 130 |
ome_response_mock.json_data = {"value": [{"Id": 16011, "Type": 2000}]} |
| 131 |
ome_conn_mock_diagnostics.job_submission.return_value = {"Id": 16011} |
| 132 |
result = self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics) |
| 133 |
assert result["Id"] == 16011 |
| 134 |
|
| 135 |
def test_extract_log_operation_s1(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 136 |
f_module = self.get_module_mock(params={"lead_chassis_only": False, "log_type": "application", |
| 137 |
"share_address": "XX.XX.XX.XX", |
| 138 |
"share_type": "NFS", |
| 139 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], }) |
| 140 |
ome_response_mock.json_data = {"value": [{"Id": 16011, "Type": 2000}]} |
| 141 |
ome_conn_mock_diagnostics.job_submission.return_value = {"Id": 16011} |
| 142 |
result = self.module.extract_log_operation(f_module, ome_conn_mock_diagnostics) |
| 143 |
assert result["Id"] == 16011 |
| 144 |
|
| 145 |
@pytest.mark.parametrize("params", [ |
| 146 |
{"log_type": "support_assist_collection", "share_address": "XX.XX.XX.XX", |
| 147 |
"share_type": "NFS", "share_name": "iso", "share_user": "username", |
| 148 |
"share_password": "password", "share_domain": "domain", |
| 149 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], |
| 150 |
"test_connection": False, "job_wait": True, "device_ids": [25011]}, |
| 151 |
{"log_type": "supportassist_collection", "share_address": "XX.XX.XX.XX", |
| 152 |
"share_type": "CIFS", "share_name": "iso", "share_user": "username", |
| 153 |
"share_password": "password", "share_domain": "domain", |
| 154 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], |
| 155 |
"test_connection": False, "job_wait": True, "device_ids": [25011]}, |
| 156 |
{"log_type": "application", "share_address": "XX.XX.XX.XX", |
| 157 |
"share_type": "NFS", "share_name": "iso", "mask_sensitive_info": "true", |
| 158 |
"test_connection": True, "job_wait": True, "device_ids": [25011]}, |
| 159 |
]) |
| 160 |
def test_main_success_case(self, params, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 161 |
ome_default_args.update(params) |
| 162 |
mocker.patch(MODULE_PATH + "check_domain_service", return_value=None) |
| 163 |
mocker.patch(MODULE_PATH + "device_validation", return_value=[25011]) |
| 164 |
mocker.patch(MODULE_PATH + "find_failed_jobs", return_value=("", False)) |
| 165 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (True, [25011]) |
| 166 |
mocker.patch(MODULE_PATH + "extract_log_operation") |
| 167 |
ome_response_mock.json_data = {"value": {"Id": 25011}} |
| 168 |
ome_conn_mock_diagnostics.job_tracking.return_value = (False, "") |
| 169 |
result = self._run_module(ome_default_args) |
| 170 |
assert result["msg"] == "Export log job completed successfully." |
| 171 |
|
| 172 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (False, [25011]) |
| 173 |
result = self._run_module(ome_default_args) |
| 174 |
assert result["msg"] == "An export log job is already running. Wait for the job to finish." |
| 175 |
|
| 176 |
ome_default_args.update({"test_connection": True, "job_wait": False}) |
| 177 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (True, [25011]) |
| 178 |
ome_conn_mock_diagnostics.job_tracking.return_value = (True, "") |
| 179 |
result = self._run_module(ome_default_args) |
| 180 |
assert result["msg"] == "Unable to access the share. Ensure that the share address, share name, " \ |
| 181 |
"share domain, and share credentials provided are correct." |
| 182 |
|
| 183 |
def test_main_success_case02(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 184 |
ome_default_args.update({"log_type": "supportassist_collection", "share_address": "XX.XX.XX.XX", |
| 185 |
"share_type": "CIFS", "share_name": "iso", "share_user": "username", |
| 186 |
"share_password": "password", "share_domain": "domain", |
| 187 |
"mask_sensitive_info": "true", "log_selectors": ["OS_LOGS"], |
| 188 |
"test_connection": False, "job_wait": True, "device_group_name": "Servers"}) |
| 189 |
mocker.patch(MODULE_PATH + "check_domain_service", return_value=None) |
| 190 |
mocker.patch(MODULE_PATH + "group_validation", return_value=[25011]) |
| 191 |
mocker.patch(MODULE_PATH + "find_failed_jobs", return_value=("", False)) |
| 192 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (True, [25011]) |
| 193 |
mocker.patch(MODULE_PATH + "extract_log_operation") |
| 194 |
ome_response_mock.json_data = {"value": {"Id": 25011}} |
| 195 |
|
| 196 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (False, [25011]) |
| 197 |
result = self._run_module(ome_default_args) |
| 198 |
assert result["msg"] == "An export log job is already running. Wait for the job to finish." |
| 199 |
|
| 200 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (True, [25011]) |
| 201 |
ome_conn_mock_diagnostics.job_tracking.return_value = (False, "") |
| 202 |
result = self._run_module(ome_default_args) |
| 203 |
assert result["msg"] == "Export log job completed successfully." |
| 204 |
|
| 205 |
ome_default_args.update({"test_connection": True, "job_wait": False}) |
| 206 |
ome_conn_mock_diagnostics.check_existing_job_state.return_value = (True, [25011]) |
| 207 |
ome_conn_mock_diagnostics.job_tracking.return_value = (True, "") |
| 208 |
result = self._run_module(ome_default_args) |
| 209 |
assert result["msg"] == "Unable to access the share. Ensure that the share address, share name, " \ |
| 210 |
"share domain, and share credentials provided are correct." |
| 211 |
|
| 212 |
@pytest.mark.parametrize("exc_type", |
| 213 |
[IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError]) |
| 214 |
def test_ome_diagnostics_main_exception_case(self, exc_type, mocker, ome_default_args, |
| 215 |
ome_conn_mock_diagnostics, ome_response_mock): |
| 216 |
ome_default_args.update({"log_type": "application", "share_address": "XX.XX.XX.XX", |
| 217 |
"share_type": "NFS", "mask_sensitive_info": False}) |
| 218 |
ome_response_mock.status_code = 400 |
| 219 |
ome_response_mock.success = False |
| 220 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 221 |
if exc_type == URLError: |
| 222 |
mocker.patch(MODULE_PATH + 'check_domain_service', side_effect=exc_type("url open error")) |
| 223 |
result = self._run_module_with_fail_json(ome_default_args) |
| 224 |
assert result["failed"] is True |
| 225 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 226 |
mocker.patch(MODULE_PATH + 'check_domain_service', side_effect=exc_type("exception message")) |
| 227 |
result = self._run_module_with_fail_json(ome_default_args) |
| 228 |
assert result['failed'] is True |
| 229 |
else: |
| 230 |
mocker.patch(MODULE_PATH + 'check_domain_service', |
| 231 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 232 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 233 |
result = self._run_module_with_fail_json(ome_default_args) |
| 234 |
assert result['failed'] is True |
| 235 |
assert 'msg' in result |
| 236 |
|
| 237 |
def test_find_failed_jobs(self, ome_conn_mock_diagnostics, ome_response_mock, ome_default_args, mocker): |
| 238 |
ome_response_mock.json_data = { |
| 239 |
"Id": 25011, |
| 240 |
"value": [{"Id": 25013, "Value": "Job status for JID_255809594125 is Completed with Errors."}] |
| 241 |
} |
| 242 |
result = self.module.find_failed_jobs({"Id": 25012}, ome_conn_mock_diagnostics) |
| 243 |
assert result[0] == "Export log job completed with errors." |
| 244 |
assert result[1] is False |
| 245 |
|
| 246 |
ome_response_mock.json_data = { |
| 247 |
"Id": 25011, |
| 248 |
"value": [] |
| 249 |
} |
| 250 |
result = self.module.find_failed_jobs({"Id": 25012}, ome_conn_mock_diagnostics) |
| 251 |
assert result[0] == "Export log job completed with errors." |
| 252 |
assert result[1] is False |
| 253 |
|
| 254 |
ome_response_mock.json_data = { |
| 255 |
"Id": 25011, |
| 256 |
"value": [{"Id": 25013, "Value": "Job status for JID_255809594125 is Completed."}] |
| 257 |
} |
| 258 |
result = self.module.find_failed_jobs({"Id": 25012}, ome_conn_mock_diagnostics) |
| 259 |
print(result) |
| 260 |
assert result[0] == "Export log job completed with errors." |
| 261 |
assert result[1] is True |