| 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 |
from io import StringIO |
| 17 |
from ssl import SSLError |
| 18 |
|
| 19 |
import pytest |
| 20 |
from ansible.module_utils._text import to_text |
| 21 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 22 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 23 |
from ansible_collections.dellemc.openmanage.plugins.modules import ome_device_location |
| 24 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 25 |
|
| 26 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_device_location.' |
| 27 |
PARAM_DATA_CENTER = "data center 1" |
| 28 |
PARAM_ROOM = "room 1" |
| 29 |
PARAM_AISLE = "aisle 1" |
| 30 |
PARAM_RACK = "rack 1" |
| 31 |
PARAM_LOCATION = "location 1" |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture |
| 35 |
def ome_conn_mock_location(mocker, ome_response_mock): |
| 36 |
connection_class_mock = mocker.patch(MODULE_PATH + 'RestOME') |
| 37 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 38 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 39 |
return ome_connection_mock_obj |
| 40 |
|
| 41 |
|
| 42 |
class TestOMEMDeviceLocation(FakeAnsibleModule): |
| 43 |
module = ome_device_location |
| 44 |
|
| 45 |
def test_check_domain_service(self, ome_conn_mock_location, ome_default_args, mocker): |
| 46 |
f_module = self.get_module_mock() |
| 47 |
result = self.module.check_domain_service( |
| 48 |
f_module, ome_conn_mock_location) |
| 49 |
assert result is None |
| 50 |
|
| 51 |
def test_standalone_chassis(self, ome_conn_mock_location, ome_default_args, mocker, ome_response_mock): |
| 52 |
mocker.patch(MODULE_PATH + "get_ip_from_host", |
| 53 |
return_value="X.X.X.X") |
| 54 |
ome_response_mock.json_data = {"value": [{"DeviceId": 25011, "DomainRoleTypeValue": "LEAD", |
| 55 |
"PublicAddress": ["XX.XX.XX.XX"]}, |
| 56 |
{"DeviceId": 25012, "DomainRoleTypeValue": "STANDALONE", |
| 57 |
"PublicAddress": ["YY.YY.YY.YY"]}]} |
| 58 |
|
| 59 |
param = {"data_center": PARAM_DATA_CENTER, "rack_slot": 2, "device_id": 25012, "hostname": "XY.XY.XY.XY", |
| 60 |
"room": PARAM_ROOM, "aisle": PARAM_AISLE, "rack": PARAM_RACK, "location": PARAM_LOCATION} |
| 61 |
f_module = self.get_module_mock(params=param) |
| 62 |
with pytest.raises(Exception) as err: |
| 63 |
self.module.standalone_chassis(f_module, ome_conn_mock_location) |
| 64 |
assert err.value.args[0] == "Failed to fetch the device information." |
| 65 |
|
| 66 |
def test_validate_dictionary(self, ome_conn_mock_location, ome_default_args, mocker): |
| 67 |
param = {"data_center": PARAM_DATA_CENTER, "rack_slot": 2, |
| 68 |
"room": PARAM_ROOM, "aisle": PARAM_AISLE, "rack": PARAM_RACK, "location": PARAM_LOCATION} |
| 69 |
f_module = self.get_module_mock(params=param) |
| 70 |
f_module.check_mode = True |
| 71 |
loc_resp = {"DataCenter": PARAM_DATA_CENTER, "RackSlot": 2, "Room": PARAM_ROOM, |
| 72 |
"Aisle": PARAM_AISLE, "RackName": PARAM_RACK, "Location": PARAM_LOCATION} |
| 73 |
with pytest.raises(Exception) as err: |
| 74 |
self.module.validate_dictionary(f_module, loc_resp) |
| 75 |
loc_resp = {"DataCenter": PARAM_DATA_CENTER, "RackSlot": 3, "Room": PARAM_ROOM, |
| 76 |
"Aisle": PARAM_AISLE, "RackName": PARAM_RACK, "Location": PARAM_LOCATION} |
| 77 |
with pytest.raises(Exception) as err: |
| 78 |
self.module.validate_dictionary(f_module, loc_resp) |
| 79 |
assert err.value.args[0] == "Changes found to be applied." |
| 80 |
loc_resp = {"DataCenter": PARAM_DATA_CENTER, "RackSlot": 2, "Room": PARAM_ROOM, |
| 81 |
"Aisle": PARAM_AISLE, "RackName": PARAM_RACK, "Location": PARAM_LOCATION} |
| 82 |
f_module.check_mode = False |
| 83 |
with pytest.raises(Exception) as err: |
| 84 |
self.module.validate_dictionary(f_module, loc_resp) |
| 85 |
assert err.value.args[0] == "No changes found to be applied." |
| 86 |
loc_resp = {"DataCenter": PARAM_DATA_CENTER, "RackSlot": 3, "Room": PARAM_ROOM, |
| 87 |
"Aisle": PARAM_AISLE, "RackName": PARAM_RACK, "Location": PARAM_LOCATION} |
| 88 |
result = self.module.validate_dictionary(f_module, loc_resp) |
| 89 |
assert result == {"DataCenter": PARAM_DATA_CENTER, "RackSlot": 2, |
| 90 |
"Room": PARAM_ROOM, "Aisle": PARAM_AISLE, "RackName": PARAM_RACK, |
| 91 |
"Location": PARAM_LOCATION, "SettingType": "Location"} |
| 92 |
|
| 93 |
def test_device_validation(self, ome_conn_mock_location, ome_default_args, mocker, ome_response_mock): |
| 94 |
mocker.patch(MODULE_PATH + "validate_dictionary", |
| 95 |
return_value={"DataCenter": PARAM_DATA_CENTER, "RackSlot": 2, "Room": PARAM_ROOM, |
| 96 |
"Aisle": PARAM_AISLE, "RackName": PARAM_RACK, "Location": PARAM_LOCATION, |
| 97 |
"SettingType": "Location"}) |
| 98 |
param = {"data_center": PARAM_DATA_CENTER, "rack_slot": 2, "device_id": 25012, |
| 99 |
"room": PARAM_ROOM, "aisle": PARAM_AISLE, "rack": PARAM_RACK, "location": PARAM_LOCATION} |
| 100 |
ome_default_args.update(param) |
| 101 |
f_module = self.get_module_mock(params=param) |
| 102 |
ome_response_mock.status_code = 200 |
| 103 |
ome_response_mock.success = True |
| 104 |
ome_response_mock.json_data = { |
| 105 |
"value": [], "DataCenter": PARAM_DATA_CENTER, |
| 106 |
"RackSlot": 3, "Room": PARAM_ROOM, "Aisle": PARAM_AISLE, "RackName": PARAM_RACK, |
| 107 |
"Location": PARAM_LOCATION, "SettingType": "Location", "result": {"RackSlot": 4}} |
| 108 |
with pytest.raises(Exception) as err: |
| 109 |
self.module.device_validation(f_module, ome_conn_mock_location) |
| 110 |
assert err.value.args[0] == "Unable to complete the operation because the entered target " \ |
| 111 |
"device id '25012' is invalid." |
| 112 |
|
| 113 |
@pytest.mark.parametrize("params", [ |
| 114 |
{"json_data": {"value": [ |
| 115 |
{'Id': 1234, 'PublicAddress': "xxx.xxx.x.x", |
| 116 |
'DeviceId': 1234, "Type": 1000}, |
| 117 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 118 |
'message': "Successfully updated the location settings.", |
| 119 |
'mparams': {"hostname": "xxx.xxx.x.x", |
| 120 |
"device_id": 1234, "data_center": "data center", |
| 121 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 122 |
}, |
| 123 |
{"json_data": {"value": [ |
| 124 |
{'Id': 1234, 'DeviceServiceTag': 'ABCD123', |
| 125 |
'PublicAddress': "xxx.xxx.x.x", 'DeviceId': 1234, "Type": 1000}, |
| 126 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 127 |
'message': "Successfully updated the location settings.", |
| 128 |
'mparams': {"hostname": "xxx.xxx.x.x", |
| 129 |
"device_service_tag": "ABCD123", "data_center": "data center", |
| 130 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 131 |
}, |
| 132 |
{"json_data": {"value": [ |
| 133 |
{'Id': 1234, 'PublicAddress': "xxx.xxx.x.x", |
| 134 |
'DeviceId': 1234, "Type": 1000}, |
| 135 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 136 |
'message': "Successfully updated the location settings.", |
| 137 |
'mparams': {"hostname": "xxx.xxx.x.x", |
| 138 |
"data_center": "data center", |
| 139 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 140 |
}, |
| 141 |
{"json_data": {"value": [ |
| 142 |
{'Id': 1234, 'PublicAddress': "dummyhost_shouldnotexist", |
| 143 |
'DeviceId': 1234, "Type": 1000}, |
| 144 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 145 |
'message': "Successfully updated the location settings.", |
| 146 |
'mparams': {"hostname": "dummyhost_shouldnotexist", |
| 147 |
"data_center": "data center", |
| 148 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 149 |
} |
| 150 |
]) |
| 151 |
def test_ome_devices_location_success(self, params, ome_conn_mock_location, ome_response_mock, |
| 152 |
ome_default_args, module_mock, mocker): |
| 153 |
ome_response_mock.success = params.get("success", True) |
| 154 |
ome_response_mock.json_data = params['json_data'] |
| 155 |
ome_default_args.update(params['mparams']) |
| 156 |
result = self._run_module( |
| 157 |
ome_default_args, check_mode=params.get('check_mode', False)) |
| 158 |
assert result['msg'] == params['message'] |
| 159 |
|
| 160 |
@pytest.mark.parametrize("params", [ |
| 161 |
{"json_data": {"value": [ |
| 162 |
{'Id': 1234, 'PublicAddress': "xxx.xxx.x.x", |
| 163 |
'DeviceId': 1234, "Type": 1000}, |
| 164 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 165 |
'message': "The device location settings operation is supported only on OpenManage Enterprise Modular systems.", |
| 166 |
'http_error_json': { |
| 167 |
"error": { |
| 168 |
"code": "Base.1.0.GeneralError", |
| 169 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 170 |
"@Message.ExtendedInfo": [ |
| 171 |
{ |
| 172 |
"MessageId": "CGEN1006", |
| 173 |
"RelatedProperties": [], |
| 174 |
"Message": "Unable to process the request because an error occurred.", |
| 175 |
"MessageArgs": [], |
| 176 |
"Severity": "Critical", |
| 177 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 178 |
} |
| 179 |
] |
| 180 |
} |
| 181 |
}, |
| 182 |
'mparams': {"hostname": "xxx.xxx.x.x", |
| 183 |
"data_center": "data center", |
| 184 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 185 |
}, |
| 186 |
{"json_data": {"value": [ |
| 187 |
{'Id': 1234, 'PublicAddress': "xxx.xxx.x.x", |
| 188 |
'DeviceId': 1234, "Type": 1000}, |
| 189 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 190 |
'message': "Unable to complete the operation because the location settings are not supported on the specified device.", |
| 191 |
'http_error_json': { |
| 192 |
"error": { |
| 193 |
"code": "Base.1.0.GeneralError", |
| 194 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 195 |
"@Message.ExtendedInfo": [ |
| 196 |
{ |
| 197 |
"MessageId": "CGEN1004", |
| 198 |
"RelatedProperties": [], |
| 199 |
"Message": "Unable to process the request because an error occurred.", |
| 200 |
"MessageArgs": [], |
| 201 |
"Severity": "Critical", |
| 202 |
"Resolution": "Retry the operation. If the issue persists, contact your system administrator." |
| 203 |
} |
| 204 |
] |
| 205 |
} |
| 206 |
}, |
| 207 |
'check_domain_service': 'mocked_check_domain_service', |
| 208 |
'standalone_chassis': ('Id', 1234), |
| 209 |
'mparams': {"hostname": "xxx.xxx.x.x", |
| 210 |
"data_center": "data center", |
| 211 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 212 |
}, |
| 213 |
{"json_data": {"value": [ |
| 214 |
{'Id': 1234, 'PublicAddress': "xxx.xxx.x.x", |
| 215 |
'DeviceId': 1234, "Type": 1000}, |
| 216 |
{'PublicAddress': "X.X.X.X", 'DeviceId': 1235, "Type": 1000}]}, |
| 217 |
'message': "Unable to complete the operation because the entered target device id '123' is invalid.", |
| 218 |
'mparams': {"hostname": "xxx.xxx.x.x.x.x.x.x", "device_id": 123, |
| 219 |
"data_center": "data center", |
| 220 |
"room": "room", "aisle": "aisle", "rack": "rack"} |
| 221 |
}, |
| 222 |
]) |
| 223 |
def test_ome_devices_location_failure(self, params, ome_conn_mock_location, ome_response_mock, |
| 224 |
ome_default_args, module_mock, mocker): |
| 225 |
ome_response_mock.success = params.get("success", True) |
| 226 |
ome_response_mock.json_data = params['json_data'] |
| 227 |
mocks = ["check_domain_service", "standalone_chassis"] |
| 228 |
for m in mocks: |
| 229 |
if m in params: |
| 230 |
mocker.patch(MODULE_PATH + m, return_value=params.get(m, {})) |
| 231 |
if 'http_error_json' in params: |
| 232 |
json_str = to_text(json.dumps(params.get('http_error_json', {}))) |
| 233 |
ome_conn_mock_location.invoke_request.side_effect = HTTPError( |
| 234 |
'https://testhost.com', 401, 'http error message', { |
| 235 |
"accept-type": "application/json"}, |
| 236 |
StringIO(json_str)) |
| 237 |
ome_default_args.update(params['mparams']) |
| 238 |
result = self._run_module(ome_default_args) |
| 239 |
assert result['msg'] == params['message'] |
| 240 |
|
| 241 |
@pytest.mark.parametrize("exc_type", |
| 242 |
[IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError]) |
| 243 |
def test_ome_device_location_main_exception_case(self, exc_type, mocker, ome_default_args, |
| 244 |
ome_conn_mock_location, ome_response_mock): |
| 245 |
ome_default_args.update({"device_id": 25011, "data_center": PARAM_DATA_CENTER, |
| 246 |
"room": PARAM_ROOM, "aisle": PARAM_AISLE, "rack": PARAM_RACK, |
| 247 |
"rack_slot": "2", "location": PARAM_LOCATION}) |
| 248 |
ome_response_mock.status_code = 400 |
| 249 |
ome_response_mock.success = False |
| 250 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 251 |
if exc_type == URLError: |
| 252 |
mocker.patch(MODULE_PATH + 'check_domain_service', |
| 253 |
side_effect=exc_type("url open error")) |
| 254 |
result = self._run_module(ome_default_args) |
| 255 |
assert result["unreachable"] is True |
| 256 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 257 |
mocker.patch(MODULE_PATH + 'check_domain_service', |
| 258 |
side_effect=exc_type("exception message")) |
| 259 |
result = self._run_module(ome_default_args) |
| 260 |
assert result['failed'] is True |
| 261 |
else: |
| 262 |
mocker.patch(MODULE_PATH + 'check_domain_service', |
| 263 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 264 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 265 |
result = self._run_module(ome_default_args) |
| 266 |
assert result['failed'] is True |
| 267 |
assert 'msg' in result |