| 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_discovery |
| 23 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 24 |
|
| 25 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_discovery.' |
| 26 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 27 |
DISC_JOB_RUNNING = "Discovery job '{name}' with ID {id} is running. Please retry after job completion." |
| 28 |
DISC_DEL_JOBS_SUCCESS = "Successfully deleted {n} discovery job(s)." |
| 29 |
MULTI_DISCOVERY = "Multiple discoveries present. Run the job again using a specific ID." |
| 30 |
DISCOVERY_SCHEDULED = "Successfully scheduled the Discovery job." |
| 31 |
DISCOVER_JOB_COMPLETE = "Successfully completed the Discovery job." |
| 32 |
JOB_TRACK_SUCCESS = "Discovery job has {0}." |
| 33 |
JOB_TRACK_FAIL = "No devices discovered, job is in {0} state." |
| 34 |
JOB_TRACK_UNABLE = "Unable to track discovery job status of {0}." |
| 35 |
JOB_TRACK_INCOMPLETE = "Discovery job {0} incomplete after polling {1} times." |
| 36 |
INVALID_DEVICES = "Invalid device types found - {0}." |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def ome_connection_mock_for_discovery(mocker, ome_response_mock): |
| 41 |
connection_class_mock = mocker.patch(MODULE_PATH + 'RestOME') |
| 42 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 43 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 44 |
return ome_connection_mock_obj |
| 45 |
|
| 46 |
|
| 47 |
class TestOmeDiscovery(FakeAnsibleModule): |
| 48 |
module = ome_discovery |
| 49 |
|
| 50 |
@pytest.mark.parametrize("params", [{"mparams": {"state": "absent", "discovery_job_name": "my_discovery1"}, |
| 51 |
"discov_list": [{"DiscoveryConfigGroupId": 12, |
| 52 |
"DiscoveryConfigGroupName": "my_discovery1"}], |
| 53 |
"job_state_dict": {12: 2010}, "res": DISC_DEL_JOBS_SUCCESS.format(n=1), |
| 54 |
"json_data": 1, "success": True}, |
| 55 |
{"mparams": {"state": "absent", "discovery_job_name": "my_discovery1"}, |
| 56 |
"discov_list": [{"DiscoveryConfigGroupId": 12, |
| 57 |
"DiscoveryConfigGroupName": "my_discovery1"}], |
| 58 |
"job_state_dict": {12: 2050}, |
| 59 |
"res": DISC_JOB_RUNNING.format(name='my_discovery1', id=12), "json_data": 1, |
| 60 |
"success": True}]) |
| 61 |
def test_delete_discovery(self, mocker, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 62 |
ome_response_mock.success = params.get("success", True) |
| 63 |
ome_response_mock.json_data = params["json_data"] |
| 64 |
mocker.patch(MODULE_PATH + 'get_discovery_states', return_value=params["job_state_dict"]) |
| 65 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 66 |
error_message = params["res"] |
| 67 |
with pytest.raises(Exception) as err: |
| 68 |
self.module.delete_discovery(f_module, ome_connection_mock_for_discovery, params['discov_list']) |
| 69 |
assert err.value.args[0] == error_message |
| 70 |
|
| 71 |
@pytest.mark.parametrize("params", [{"mparams": {"state": "absent", "discovery_job_name": "my_discovery1"}, |
| 72 |
"res": [{"DiscoveryConfigGroupId": 12, |
| 73 |
"DiscoveryConfigGroupName": "my_discovery1"}], |
| 74 |
"json_data": {"value": [{"DiscoveryConfigGroupId": 12, |
| 75 |
"DiscoveryConfigGroupName": "my_discovery1"}]}, |
| 76 |
"success": True}, |
| 77 |
{"mparams": {"state": "absent", "discovery_id": 12}, "res": [ |
| 78 |
{"DiscoveryConfigGroupId": 12, |
| 79 |
"DiscoveryConfigGroupName": "my_discovery1"}], |
| 80 |
"json_data": {"value": [{"DiscoveryConfigGroupId": 11, |
| 81 |
"DiscoveryConfigGroupName": "my_discovery2"}, |
| 82 |
{"DiscoveryConfigGroupId": 12, |
| 83 |
"DiscoveryConfigGroupName": "my_discovery1"}]}, |
| 84 |
"success": True}]) |
| 85 |
def test_check_existing_discovery(self, mocker, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 86 |
ome_response_mock.success = params.get("success", True) |
| 87 |
ome_response_mock.json_data = params["json_data"] |
| 88 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 89 |
res = self.module.check_existing_discovery(f_module, ome_connection_mock_for_discovery) |
| 90 |
assert res == params["res"] |
| 91 |
|
| 92 |
@pytest.mark.parametrize("params", [ |
| 93 |
{"res": {12: 2020}, "json_data": {"value": [{"DiscoveryConfigGroupId": 12, "JobStatusId": 2020}]}, |
| 94 |
"success": True}]) |
| 95 |
def test_get_discovery_states(self, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 96 |
ome_response_mock.success = params.get("success", True) |
| 97 |
ome_response_mock.json_data = params["json_data"] |
| 98 |
res = self.module.get_discovery_states(ome_connection_mock_for_discovery) |
| 99 |
assert res == params["res"] |
| 100 |
|
| 101 |
@pytest.mark.parametrize("params", [{"mparams": {"schedule": 'RunNow'}, |
| 102 |
'schedule_payload': {"RunNow": True, "RunLater": False, 'Cron': "startnow"}}, |
| 103 |
{"mparams": {"schedule": 'RunLater', 'cron': "1 2 3 4 5 *"}, |
| 104 |
'schedule_payload': {"RunNow": False, "RunLater": True, |
| 105 |
'Cron': "1 2 3 4 5 *"}}, ]) |
| 106 |
def test_get_schedule(self, params): |
| 107 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 108 |
res = self.module.get_schedule(f_module) |
| 109 |
assert res == params['schedule_payload'] |
| 110 |
|
| 111 |
@pytest.mark.parametrize("params", [{"json_data": { |
| 112 |
"value": [{"ProtocolName": "SNMP", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 113 |
{"ProtocolName": "SNMP", "DeviceTypeId": 5000, "DeviceTypeName": "DELL STORAGE"}, |
| 114 |
{"ProtocolName": "SNMP", "DeviceTypeId": 7000, "DeviceTypeName": "NETWORK SWITCH"}, |
| 115 |
{"ProtocolName": "WSMAN", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 116 |
{"ProtocolName": "WSMAN", "DeviceTypeId": 2000, "DeviceTypeName": "CHASSIS"}, |
| 117 |
{"ProtocolName": "REDFISH", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 118 |
{"ProtocolName": "REDFISH", "DeviceTypeId": 2000, "DeviceTypeName": "CHASSIS", }, |
| 119 |
{"ProtocolName": "IPMI", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 120 |
{"ProtocolName": "SSH", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 121 |
{"ProtocolName": "VMWARE", "DeviceTypeId": 1000, "DeviceTypeName": "SERVER"}, |
| 122 |
{"ProtocolName": "STORAGE", "DeviceTypeId": 5000, "DeviceTypeName": "DELL STORAGE"}]}, |
| 123 |
"dev_id_map": {"CHASSIS": 2000, "DELL STORAGE": 5000, "NETWORK SWITCH": 7000, "SERVER": 1000, "STORAGE": 5000}, |
| 124 |
"proto_dev_map": {"CHASSIS": ["WSMAN", "REDFISH"], "DELL STORAGE": ["SNMP", "STORAGE"], |
| 125 |
"NETWORK SWITCH": ["SNMP"], |
| 126 |
"STORAGE": ["SNMP", "STORAGE"], |
| 127 |
"SERVER": ["SNMP", "WSMAN", "REDFISH", "IPMI", "SSH", "VMWARE"]}}]) |
| 128 |
def test_get_protocol_device_map(self, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 129 |
ome_response_mock.success = params.get("success", True) |
| 130 |
ome_response_mock.json_data = params["json_data"] |
| 131 |
prot_dev_map, dev_id_map = self.module.get_protocol_device_map(ome_connection_mock_for_discovery) |
| 132 |
assert prot_dev_map == params['proto_dev_map'] |
| 133 |
assert dev_id_map == params['dev_id_map'] |
| 134 |
|
| 135 |
@pytest.mark.parametrize("params", [{ |
| 136 |
"mparams": {"discovery_job_name": 'd1', 'trap_destination': True, 'community_string': True, |
| 137 |
'email_recipient': '[email protected]', 'description': "d1_desc"}, |
| 138 |
'other_dict': {"DiscoveryConfigGroupName": 'd1', "TrapDestination": True, 'CommunityString': True, |
| 139 |
'DiscoveryStatusEmailRecipient': '[email protected]'}}]) |
| 140 |
def test_get_other_discovery_payload(self, params): |
| 141 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 142 |
res = self.module.get_other_discovery_payload(f_module) |
| 143 |
assert res == params['other_dict'] |
| 144 |
|
| 145 |
@pytest.mark.parametrize("params", [{"json_data": {"value": [{"Id": 1, "StartTime": "2021-04-19 04:54:18.427"}, |
| 146 |
{"Id": 2, "StartTime": "2021-04-19 04:55:18.427"}]}, |
| 147 |
"ips": {"Failed": ["192.168.1.2"], "Completed": ["192.168.1.3"]}, |
| 148 |
"pag_ret_val": { |
| 149 |
"value": [{"Key": "192.168.1.2", "JobStatus": {"Name": "Failed"}}, |
| 150 |
{"Key": "192.168.1.3", "JobStatus": {"Name": "Completed"}}]}}]) |
| 151 |
def test_get_execution_details(self, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 152 |
ome_response_mock.success = params.get("success", True) |
| 153 |
ome_response_mock.json_data = params["json_data"] |
| 154 |
ome_connection_mock_for_discovery.get_all_items_with_pagination.return_value = params['pag_ret_val'] |
| 155 |
ips, job_status = self.module.get_execution_details(ome_connection_mock_for_discovery, 1) |
| 156 |
assert ips == params['ips'] |
| 157 |
|
| 158 |
@pytest.mark.parametrize("params", [{"json_data": {'JobStatusId': 2060}, 'job_wait_sec': 60, 'job_failed': False, |
| 159 |
"msg": JOB_TRACK_SUCCESS.format('completed successfully')}, |
| 160 |
{"json_data": {'JobStatusId': 2070}, 'job_wait_sec': 60, 'job_failed': True, |
| 161 |
"msg": JOB_TRACK_FAIL.format('Failed')}, |
| 162 |
{"json_data": {'JobStatusId': 2050}, 'job_wait_sec': 60, 'job_failed': True, |
| 163 |
"msg": JOB_TRACK_INCOMPLETE.format(1, 2)}, ]) |
| 164 |
def test_discovery_job_tracking(self, params, mocker, ome_connection_mock_for_discovery, ome_response_mock): |
| 165 |
ome_response_mock.success = params.get("success", True) |
| 166 |
ome_response_mock.json_data = params["json_data"] |
| 167 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 168 |
msg = self.module.discovery_job_tracking(ome_connection_mock_for_discovery, 1, |
| 169 |
params['job_wait_sec']) |
| 170 |
assert msg == params['msg'] |
| 171 |
|
| 172 |
@pytest.mark.parametrize("params", [{"discovery_json": {'DiscoveryConfigTaskParam': [{'TaskId': 12}]}, |
| 173 |
'job_id': 12, "json_data": {"value": [{"Id": 1}, {"Id": 2}]}}, |
| 174 |
{"discovery_json": {'DiscoveryConfigGroupId': 123, |
| 175 |
'DiscoveryConfigTaskParam': [{'TaskId': 12}, |
| 176 |
{'TaskId': 23}]}, |
| 177 |
'job_id': 12, "json_data": {"value": [{'DiscoveryConfigGroupId': 234, |
| 178 |
"JobId": 2}, |
| 179 |
{'DiscoveryConfigGroupId': 123, |
| 180 |
"JobId": 12}, ]}}]) |
| 181 |
def test_get_job_data(self, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 182 |
ome_response_mock.success = params.get("success", True) |
| 183 |
ome_response_mock.json_data = params["json_data"] |
| 184 |
job_id = self.module.get_job_data(params['discovery_json'], ome_connection_mock_for_discovery) |
| 185 |
assert job_id == params['job_id'] |
| 186 |
|
| 187 |
@pytest.mark.parametrize("params", [{"disc_config": { |
| 188 |
"ipmi": {"kgkey": None, "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "retries": 3, "timeout": 60, |
| 189 |
"username": "root"}, |
| 190 |
"wsman": {"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "retries": 3, "timeout": 60, "username": "root"}}, |
| 191 |
'conn_profile': {"credentials": [{"authType": "Basic", "credentials": {"kgkey": None, |
| 192 |
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", |
| 193 |
"retries": 3, "timeout": 60, |
| 194 |
"username": "root"}, "modified": False, |
| 195 |
"type": "IPMI"}], "profileDescription": "", "profileId": 0, "profileName": "", |
| 196 |
"type": "DISCOVERY"}}]) |
| 197 |
def test_get_connection_profile(self, params): |
| 198 |
conn_profile = self.module.get_connection_profile(params['disc_config']) |
| 199 |
assert conn_profile['type'] == params['conn_profile']['type'] |
| 200 |
|
| 201 |
@pytest.mark.parametrize("params", [{"disc_cfg_list": [{ |
| 202 |
"ConnectionProfile": "{\"profileDescription\": \"\", \"profileId\": 0, \"type\": \"DISCOVERY\", \"credentials\"" |
| 203 |
": [{\"credentials\": {\"retries\": 3, \"community\": \"public\", \"timeout\": 3, \"port\"" |
| 204 |
": 161}, \"authType\": \"Basic\", \"type\": \"SNMP\", \"modified\": False}], " |
| 205 |
"\"profileName\": \"\"}", "DeviceType": [1000], |
| 206 |
"DiscoveryConfigTargets": [{"NetworkAddressDetail": "196.168.24.17"}]}], |
| 207 |
"get_conn_json": {"profileId": 0, "profileName": "", "profileDescription": "", "type": "DISCOVERY", |
| 208 |
'credentials': [{'authType': 'Basic', |
| 209 |
'credentials': {'community': 'public', 'port': 161, 'retries': 3, |
| 210 |
'timeout': 3}, 'id': 116, 'modified': False, |
| 211 |
'type': 'SNMP'}]}, "DeviceType": [1000], |
| 212 |
"DiscoveryConfigTargets": [{"NetworkAddressDetail": "196.168.24.17"}], 'mparams': {'discovery_config_targets': [ |
| 213 |
{"device_types": ["SERVER"], "network_address_detail": ["196.168.24.17"], |
| 214 |
"snmp": {"community": "public", "port": 161, "retries": 3, "timeout": 3}}]}}]) |
| 215 |
def test_get_discovery_config(self, params, mocker, ome_connection_mock_for_discovery, ): |
| 216 |
dev_id_map = {"CHASSIS": 2000, "DELL STORAGE": 5000, "NETWORK SWITCH": 7000, "SERVER": 1000, "STORAGE": 5000} |
| 217 |
proto_dev_map = {"CHASSIS": ["WSMAN", "REDFISH"], "DELL STORAGE": ["SNMP", "STORAGE"], |
| 218 |
"NETWORK SWITCH": ["SNMP"], "SERVER": ["SNMP", "WSMAN", "REDFISH", "IPMI", "SSH", "VMWARE"]} |
| 219 |
f_module = self.get_module_mock(params=params['mparams']) |
| 220 |
mocker.patch(MODULE_PATH + 'get_protocol_device_map', return_value=(proto_dev_map, dev_id_map)) |
| 221 |
mocker.patch(MODULE_PATH + 'get_connection_profile', return_value=params['get_conn_json']) |
| 222 |
disc_cfg_list = self.module.get_discovery_config(f_module, ome_connection_mock_for_discovery) |
| 223 |
assert disc_cfg_list[0]['DeviceType'] == params['DeviceType'] |
| 224 |
assert disc_cfg_list[0]['DiscoveryConfigTargets'] == params['DiscoveryConfigTargets'] |
| 225 |
|
| 226 |
@pytest.mark.parametrize("params", [{"json_data": {"@odata.type": "#DiscoveryConfigService.DiscoveryJob", |
| 227 |
"@odata.id": "/api/DiscoveryConfigService/Jobs(12617)", |
| 228 |
"JobId": 12617, "JobName": "D1", "JobSchedule": "startnow", |
| 229 |
"DiscoveryConfigExpectedDeviceCount": 713, |
| 230 |
"DiscoveryConfigDiscoveredDeviceCount": 0, |
| 231 |
"DiscoveryConfigEmailRecipient": "[email protected]", }, |
| 232 |
"djob": {"JobId": 12617, "JobName": "D1", "JobSchedule": "startnow", |
| 233 |
"DiscoveryConfigExpectedDeviceCount": 713, |
| 234 |
"DiscoveryConfigDiscoveredDeviceCount": 0, |
| 235 |
"DiscoveryConfigEmailRecipient": "[email protected]", }}]) |
| 236 |
def test_get_discovery_job(self, params, ome_connection_mock_for_discovery, ome_response_mock): |
| 237 |
ome_response_mock.success = params.get("success", True) |
| 238 |
ome_response_mock.json_data = params["json_data"] |
| 239 |
djob = self.module.get_discovery_job(ome_connection_mock_for_discovery, 12) |
| 240 |
assert djob == params['djob'] |
| 241 |
|
| 242 |
@pytest.mark.parametrize("params", [ |
| 243 |
{"json_data": {"DiscoveryConfigGroupName": 'd1'}, 'job_message': DISCOVER_JOB_COMPLETE, |
| 244 |
'mparams': {'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}}, |
| 245 |
{"json_data": {"DiscoveryConfigGroupName": 'd1'}, 'job_message': JOB_TRACK_FAIL, |
| 246 |
'mparams': {'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}}, |
| 247 |
{"json_data": {"DiscoveryConfigGroupName": 'd1'}, 'job_message': DISCOVERY_SCHEDULED, |
| 248 |
'mparams': {'job_wait': False, 'schedule': 'RunLater', 'job_wait_timeout': 1000}}]) |
| 249 |
def test_create_discovery(self, params, mocker, ome_connection_mock_for_discovery, ome_response_mock): |
| 250 |
mocker.patch(MODULE_PATH + 'get_discovery_config', return_value={}) |
| 251 |
mocker.patch(MODULE_PATH + 'get_schedule', return_value={}) |
| 252 |
mocker.patch(MODULE_PATH + 'get_other_discovery_payload', return_value={}) |
| 253 |
mocker.patch(MODULE_PATH + 'get_job_data', return_value=12) |
| 254 |
mocker.patch(MODULE_PATH + 'get_execution_details', return_value=({"Completed": ["XX.XX.XX.XX"], "Failed": []}, |
| 255 |
{"JobStatusId": 2050})) |
| 256 |
mocker.patch(MODULE_PATH + 'get_discovery_job', return_value={"JobStatusId": 2050}) |
| 257 |
mocker.patch(MODULE_PATH + 'discovery_job_tracking', return_value=(params['job_message'])) |
| 258 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 259 |
ome_response_mock.success = params.get("success", True) |
| 260 |
ome_response_mock.json_data = params["json_data"] |
| 261 |
f_module = self.get_module_mock(params=params['mparams']) |
| 262 |
error_message = params["job_message"] |
| 263 |
with pytest.raises(Exception) as err: |
| 264 |
self.module.create_discovery(f_module, ome_connection_mock_for_discovery) |
| 265 |
assert err.value.args[0] == error_message |
| 266 |
|
| 267 |
@pytest.mark.parametrize("exc_type", |
| 268 |
[IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError]) |
| 269 |
def test_ome_discovery_main_exception_failure_case(self, exc_type, mocker, ome_default_args, |
| 270 |
ome_connection_mock_for_discovery, ome_response_mock): |
| 271 |
ome_default_args.update({"state": "absent", "discovery_job_name": "t1"}) |
| 272 |
ome_response_mock.status_code = 400 |
| 273 |
ome_response_mock.success = False |
| 274 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 275 |
if exc_type == URLError: |
| 276 |
mocker.patch(MODULE_PATH + 'check_existing_discovery', side_effect=exc_type("url open error")) |
| 277 |
result = self._run_module(ome_default_args) |
| 278 |
assert result["unreachable"] is True |
| 279 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 280 |
mocker.patch(MODULE_PATH + 'check_existing_discovery', side_effect=exc_type("exception message")) |
| 281 |
result = self._run_module_with_fail_json(ome_default_args) |
| 282 |
assert result['failed'] is True |
| 283 |
else: |
| 284 |
mocker.patch(MODULE_PATH + 'check_existing_discovery', |
| 285 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 286 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 287 |
result = self._run_module_with_fail_json(ome_default_args) |
| 288 |
assert result['failed'] is True |
| 289 |
assert 'msg' in result |
| 290 |
|
| 291 |
@pytest.mark.parametrize( |
| 292 |
"params", [{"json_data": {"DiscoveryConfigGroupName": 'd1'}, |
| 293 |
'job_failed': False, 'job_message': DISCOVER_JOB_COMPLETE, |
| 294 |
'mparams': {'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}}, |
| 295 |
{"json_data": {"DiscoveryConfigGroupName": 'd1'}, 'job_failed': True, |
| 296 |
'job_message': JOB_TRACK_FAIL, |
| 297 |
'mparams': {'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}}, |
| 298 |
{"json_data": {"DiscoveryConfigGroupName": 'd1'}, 'job_failed': True, |
| 299 |
'job_message': DISCOVERY_SCHEDULED, |
| 300 |
'mparams': {'job_wait': False, 'schedule': 'RunLater', 'job_wait_timeout': 1000}}]) |
| 301 |
def test_modify_discovery(self, params, mocker, ome_connection_mock_for_discovery, ome_response_mock): |
| 302 |
discov_list = [{"DiscoveryConfigGroupId": 12, "DiscoveryConfigGroupName": "my_discovery1"}] |
| 303 |
f_module = self.get_module_mock(params=params['mparams']) |
| 304 |
mocker.patch(MODULE_PATH + 'get_other_discovery_payload', return_value={"DiscoveryConfigGroupId": 10}) |
| 305 |
mocker.patch(MODULE_PATH + 'update_modify_payload', return_value=None) |
| 306 |
mocker.patch(MODULE_PATH + 'get_job_data', return_value=12) |
| 307 |
mocker.patch(MODULE_PATH + 'get_execution_details', return_value=({"Completed": ["XX.XX.XX.XX"], "Failed": []}, |
| 308 |
{"JobStatusId": 2050})) |
| 309 |
mocker.patch(MODULE_PATH + 'get_discovery_job', return_value={"JobStatusId": 2050}) |
| 310 |
mocker.patch(MODULE_PATH + 'get_discovery_config', return_value={}) |
| 311 |
mocker.patch(MODULE_PATH + 'get_discovery_states', return_value={12: 15}) |
| 312 |
mocker.patch(MODULE_PATH + 'discovery_job_tracking', return_value=(params['job_message'])) |
| 313 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 314 |
error_message = params["job_message"] |
| 315 |
with pytest.raises(Exception) as err: |
| 316 |
self.module.modify_discovery(f_module, ome_connection_mock_for_discovery, discov_list) |
| 317 |
assert err.value.args[0] == error_message |
| 318 |
|
| 319 |
def test_modify_discovery_failure_case01(self, ome_connection_mock_for_discovery): |
| 320 |
multi_disc_msg = MULTI_DISCOVERY |
| 321 |
f_module = self.get_module_mock(params={'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}) |
| 322 |
with pytest.raises(Exception) as err: |
| 323 |
self.module.modify_discovery(f_module, ome_connection_mock_for_discovery, |
| 324 |
[{"DiscoveryConfigGroupId": 1, "DiscoveryConfigGroupName": "my_discovery1"}, |
| 325 |
{"DiscoveryConfigGroupId": 2, "DiscoveryConfigGroupName": "my_discovery2"}]) |
| 326 |
assert err.value.args[0] == multi_disc_msg |
| 327 |
|
| 328 |
def test_modify_discovery_failure_case2(self, mocker, ome_connection_mock_for_discovery): |
| 329 |
f_module = self.get_module_mock(params={'job_wait': True, 'schedule': 'RunNow', 'job_wait_timeout': 1000}) |
| 330 |
job_run_msg = DISC_JOB_RUNNING.format(name='my_discovery1', id=12) |
| 331 |
mocker.patch(MODULE_PATH + 'get_discovery_states', return_value={12: 2050}) |
| 332 |
with pytest.raises(Exception) as err: |
| 333 |
self.module.modify_discovery(f_module, ome_connection_mock_for_discovery, [ |
| 334 |
{"DiscoveryConfigGroupId": 12, "DiscoveryConfigGroupName": "my_discovery1"}]) |
| 335 |
assert err.value.args[0] == job_run_msg |
| 336 |
|
| 337 |
def test_update_modify_payload(self): |
| 338 |
current_payload = { |
| 339 |
"DiscoveryConfigGroupId": 21, |
| 340 |
"DiscoveryConfigGroupName": "Discoverystorage", |
| 341 |
"DiscoveryStatusEmailRecipient": None, |
| 342 |
"DiscoveryConfigModels": [ |
| 343 |
{ |
| 344 |
"DiscoveryConfigId": 41, |
| 345 |
"DiscoveryConfigStatus": None, |
| 346 |
"DiscoveryConfigTargets": [ |
| 347 |
{ |
| 348 |
"DiscoveryConfigTargetId": 41, |
| 349 |
"NetworkAddressDetail": "mock_network_address", |
| 350 |
"SubnetMask": None, |
| 351 |
"AddressType": 1, |
| 352 |
"Disabled": False, |
| 353 |
"Exclude": False |
| 354 |
} |
| 355 |
], |
| 356 |
"ConnectionProfileId": 21341, |
| 357 |
"ConnectionProfile": "{\n \"profileId\" : 21341,\n \"profileName\" : \"\"," |
| 358 |
"\n \"profileDescription\" : \"\",\n \"type\" : \"DISCOVERY\"," |
| 359 |
"\n \"updatedBy\" : null,\n \"updateTime\" : 1617952521213," |
| 360 |
"\n \"credentials\" : [ {\n \"type\" : \"STORAGE\",\n \"authType\" : " |
| 361 |
"\"Basic\",\n \"modified\" : false,\n \"id\" : 44," |
| 362 |
"\n \"credentials\" : {\n \"username\" : \"root\"," |
| 363 |
"\n \"password\" : null,\n \"domain\" : null,\n \"caCheck\" : " |
| 364 |
"false,\n \"cnCheck\" : false,\n \"certificateData\" : null," |
| 365 |
"\n \"certificateDetail\" : null,\n \"port\" : 443," |
| 366 |
"\n \"retries\" : 3,\n \"timeout\" : 60,\n \"isHttp\" : " |
| 367 |
"false,\n \"keepAlive\" : true,\n \"version\" : null\n }\n } " |
| 368 |
"]\n}", |
| 369 |
"DeviceType": [ |
| 370 |
5000 |
| 371 |
] |
| 372 |
} |
| 373 |
], |
| 374 |
"Schedule": { |
| 375 |
"RunNow": False, |
| 376 |
"RunLater": False, |
| 377 |
"Recurring": None, |
| 378 |
"Cron": "startnow", |
| 379 |
"StartTime": None, |
| 380 |
"EndTime": None |
| 381 |
}, |
| 382 |
"TrapDestination": False, |
| 383 |
"CommunityString": False, |
| 384 |
"UseAllProfiles": False, |
| 385 |
"CreateGroup": True |
| 386 |
} |
| 387 |
discovery_modify_payload = { |
| 388 |
"DiscoveryConfigGroupName": "name1" |
| 389 |
} |
| 390 |
self.module.update_modify_payload(discovery_modify_payload, current_payload, new_name="name2") |
| 391 |
assert discovery_modify_payload["DiscoveryConfigGroupName"] == "name2" |
| 392 |
assert discovery_modify_payload["Schedule"]["RunNow"] is True |
| 393 |
assert discovery_modify_payload["Schedule"]["RunLater"] is False |
| 394 |
assert discovery_modify_payload["Schedule"]["Cron"] == "startnow" |
| 395 |
|
| 396 |
def test_update_modify_payload_case2(self): |
| 397 |
current_payload = { |
| 398 |
"DiscoveryConfigGroupId": 21, |
| 399 |
"DiscoveryConfigGroupName": "Discoverystorage", |
| 400 |
"DiscoveryStatusEmailRecipient": None, |
| 401 |
"DiscoveryConfigModels": [ |
| 402 |
{ |
| 403 |
"DiscoveryConfigId": 41, |
| 404 |
"DiscoveryConfigStatus": None, |
| 405 |
"DiscoveryConfigTargets": [ |
| 406 |
{ |
| 407 |
"DiscoveryConfigTargetId": 41, |
| 408 |
"NetworkAddressDetail": "mock_network_address", |
| 409 |
"SubnetMask": None, |
| 410 |
"AddressType": 1, |
| 411 |
"Disabled": False, |
| 412 |
"Exclude": False |
| 413 |
} |
| 414 |
], |
| 415 |
"ConnectionProfileId": 21341, |
| 416 |
"ConnectionProfile": "{\n \"profileId\" : 21341,\n \"profileName\" : \"\"," |
| 417 |
"\n \"profileDescription\" : \"\",\n \"type\" : \"DISCOVERY\"," |
| 418 |
"\n \"updatedBy\" : null,\n \"updateTime\" : 1617952521213," |
| 419 |
"\n \"credentials\" : [ {\n \"type\" : \"STORAGE\",\n \"authType\" : " |
| 420 |
"\"Basic\",\n \"modified\" : false,\n \"id\" : 44," |
| 421 |
"\n \"credentials\" : {\n \"username\" : \"root\"," |
| 422 |
"\n \"password\" : null,\n \"domain\" : null,\n \"caCheck\" : " |
| 423 |
"false,\n \"cnCheck\" : false,\n \"certificateData\" : null," |
| 424 |
"\n \"certificateDetail\" : null,\n \"port\" : 443," |
| 425 |
"\n \"retries\" : 3,\n \"timeout\" : 60,\n \"isHttp\" : " |
| 426 |
"false,\n \"keepAlive\" : true,\n \"version\" : null\n }\n } " |
| 427 |
"]\n}", |
| 428 |
"DeviceType": [ |
| 429 |
5000 |
| 430 |
] |
| 431 |
} |
| 432 |
], |
| 433 |
"Schedule": { |
| 434 |
"RunNow": False, |
| 435 |
"RunLater": False, |
| 436 |
"Recurring": None, |
| 437 |
"Cron": "startnow", |
| 438 |
"StartTime": None, |
| 439 |
"EndTime": None |
| 440 |
}, |
| 441 |
"TrapDestination": False, |
| 442 |
"CommunityString": False, |
| 443 |
"UseAllProfiles": False, |
| 444 |
"CreateGroup": True |
| 445 |
} |
| 446 |
discovery_modify_payload = { |
| 447 |
"DiscoveryConfigGroupName": "name1", |
| 448 |
"TrapDestination": True, |
| 449 |
"CommunityString": True, |
| 450 |
"Schedule": { |
| 451 |
"Cron": "startlater", |
| 452 |
"RunNow": False, |
| 453 |
|
| 454 |
} |
| 455 |
} |
| 456 |
self.module.update_modify_payload(discovery_modify_payload, current_payload) |
| 457 |
assert discovery_modify_payload["DiscoveryConfigGroupName"] == "name1" |
| 458 |
assert discovery_modify_payload["TrapDestination"] is True |
| 459 |
assert discovery_modify_payload["CommunityString"] is True |
| 460 |
assert discovery_modify_payload["Schedule"]["Cron"] == "startlater" |
| 461 |
assert discovery_modify_payload["Schedule"]["RunNow"] is False |