| 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_template |
| 24 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule, Constants |
| 25 |
|
| 26 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_template.' |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture |
| 30 |
def ome_connection_mock_for_template(mocker, ome_response_mock): |
| 31 |
connection_class_mock = mocker.patch(MODULE_PATH + 'RestOME') |
| 32 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 33 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 34 |
ome_connection_mock_obj.get_all_report_details.return_value = { |
| 35 |
"report_list": []} |
| 36 |
return ome_connection_mock_obj |
| 37 |
|
| 38 |
|
| 39 |
TEMPLATE_RESOURCE = {"TEMPLATE_RESOURCE": "TemplateService/Templates"} |
| 40 |
|
| 41 |
|
| 42 |
class TestOmeTemplate(FakeAnsibleModule): |
| 43 |
module = ome_template |
| 44 |
|
| 45 |
@pytest.fixture |
| 46 |
def get_template_resource_mock(self, mocker): |
| 47 |
response_class_mock = mocker.patch( |
| 48 |
MODULE_PATH + '_get_resource_parameters') |
| 49 |
return response_class_mock |
| 50 |
|
| 51 |
def test_get_service_tags_success_case(self, ome_connection_mock_for_template, ome_response_mock): |
| 52 |
ome_connection_mock_for_template.get_all_report_details.return_value = { |
| 53 |
"report_list": [{"Id": Constants.device_id1, |
| 54 |
"DeviceServiceTag": Constants.service_tag1}]} |
| 55 |
f_module = self.get_module_mock( |
| 56 |
{'device_id': [], 'device_service_tag': [Constants.service_tag1]}) |
| 57 |
data = self.module.get_device_ids( |
| 58 |
f_module, ome_connection_mock_for_template) |
| 59 |
assert data == [Constants.device_id1] |
| 60 |
|
| 61 |
def test_get_device_ids_failure_case01(self, ome_connection_mock_for_template, ome_response_mock, ome_default_args): |
| 62 |
ome_response_mock.json_data = {'value': []} |
| 63 |
ome_response_mock.success = False |
| 64 |
f_module = self.get_module_mock(params={'device_id': ["#@!1"]}) |
| 65 |
with pytest.raises(Exception) as exc: |
| 66 |
self.module.get_device_ids( |
| 67 |
f_module, ome_connection_mock_for_template) |
| 68 |
assert exc.value.args[0] == "Unable to complete the operation because the entered target device id(s) " \ |
| 69 |
"'{0}' are invalid.".format("#@!1") |
| 70 |
|
| 71 |
@pytest.mark.parametrize("params", |
| 72 |
[{"mparams": { |
| 73 |
"attributes": { |
| 74 |
"Attributes": [ |
| 75 |
{ |
| 76 |
"Id": 93812, |
| 77 |
"IsIgnored": False, |
| 78 |
"Value": "Aisle Five" |
| 79 |
}, |
| 80 |
{ |
| 81 |
"DisplayName": 'System, Server Topology, ServerTopology 1 Aisle Name', |
| 82 |
"IsIgnored": False, |
| 83 |
"Value": "Aisle 5" |
| 84 |
} |
| 85 |
] |
| 86 |
}}, "success": True, |
| 87 |
"json_data": { |
| 88 |
"Id": 11, |
| 89 |
"Name": "ProfileViewEditAttributes", |
| 90 |
"AttributeGroupNames": [], |
| 91 |
"AttributeGroups": [ |
| 92 |
{ |
| 93 |
"GroupNameId": 5, |
| 94 |
"DisplayName": "System", |
| 95 |
"SubAttributeGroups": [ |
| 96 |
{ |
| 97 |
"GroupNameId": 33016, |
| 98 |
"DisplayName": "Server Operating System", |
| 99 |
"SubAttributeGroups": [], |
| 100 |
"Attributes": [ |
| 101 |
{ |
| 102 |
"AttributeId": 93820, |
| 103 |
"DisplayName": "ServerOS 1 Server Host Name", |
| 104 |
"Value": None, |
| 105 |
"IsReadOnly": False, |
| 106 |
"IsIgnored": True, |
| 107 |
} |
| 108 |
] |
| 109 |
}, |
| 110 |
{ |
| 111 |
"GroupNameId": 33019, |
| 112 |
"DisplayName": "Server Topology", |
| 113 |
"SubAttributeGroups": [], |
| 114 |
"Attributes": [ |
| 115 |
{ |
| 116 |
"AttributeId": 93812, |
| 117 |
"DisplayName": "ServerTopology 1 Aisle Name", |
| 118 |
"Value": "Aisle 5", |
| 119 |
"IsReadOnly": False, |
| 120 |
"IsIgnored": True, |
| 121 |
}, |
| 122 |
{ |
| 123 |
"AttributeId": 93811, |
| 124 |
"DisplayName": "ServerTopology 1 Data Center Name", |
| 125 |
"Description": None, |
| 126 |
"Value": "BLG 2nd Floor DS 1", |
| 127 |
"IsReadOnly": False, |
| 128 |
"IsIgnored": True, |
| 129 |
}, |
| 130 |
{ |
| 131 |
"AttributeId": 93813, |
| 132 |
"DisplayName": "ServerTopology 1 Rack Name", |
| 133 |
"Description": None, |
| 134 |
"Value": None, |
| 135 |
"IsReadOnly": False, |
| 136 |
"IsIgnored": True, |
| 137 |
}, |
| 138 |
{ |
| 139 |
"AttributeId": 93814, |
| 140 |
"DisplayName": "ServerTopology 1 Rack Slot", |
| 141 |
"Description": None, |
| 142 |
"Value": None, |
| 143 |
"IsReadOnly": False, |
| 144 |
"IsIgnored": True, |
| 145 |
} |
| 146 |
] |
| 147 |
} |
| 148 |
], |
| 149 |
"Attributes": [] |
| 150 |
}, |
| 151 |
{ |
| 152 |
"GroupNameId": 9, |
| 153 |
"DisplayName": "iDRAC", |
| 154 |
"SubAttributeGroups": [ |
| 155 |
{ |
| 156 |
"GroupNameId": 32688, |
| 157 |
"DisplayName": "Active Directory", |
| 158 |
"SubAttributeGroups": [], |
| 159 |
"Attributes": [ |
| 160 |
{ |
| 161 |
"AttributeId": 93523, |
| 162 |
"DisplayName": "ActiveDirectory 1 Active Directory RAC Name", |
| 163 |
"Description": None, |
| 164 |
"Value": None, |
| 165 |
"IsReadOnly": False, |
| 166 |
"IsIgnored": True, |
| 167 |
} |
| 168 |
] |
| 169 |
}, |
| 170 |
{ |
| 171 |
"GroupNameId": 32930, |
| 172 |
"DisplayName": "NIC Information", |
| 173 |
"SubAttributeGroups": [], |
| 174 |
"Attributes": [ |
| 175 |
{ |
| 176 |
"AttributeId": 93035, |
| 177 |
"DisplayName": "NIC 1 DNS RAC Name", |
| 178 |
"Description": None, |
| 179 |
"Value": None, |
| 180 |
"IsReadOnly": False, |
| 181 |
"IsIgnored": True, |
| 182 |
}, |
| 183 |
{ |
| 184 |
"AttributeId": 92510, |
| 185 |
"DisplayName": "NIC 1 Enable VLAN", |
| 186 |
"Description": None, |
| 187 |
"Value": "Disabled", |
| 188 |
"IsReadOnly": False, |
| 189 |
"IsIgnored": False, |
| 190 |
} |
| 191 |
] |
| 192 |
} |
| 193 |
], |
| 194 |
"Attributes": []}]}, |
| 195 |
"diff": 2}]) |
| 196 |
def test_attributes_check(self, params, ome_connection_mock_for_template, ome_response_mock): |
| 197 |
ome_response_mock.success = params.get("success", True) |
| 198 |
ome_response_mock.json_data = params["json_data"] |
| 199 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 200 |
result = self.module.attributes_check(f_module, ome_connection_mock_for_template, |
| 201 |
params['mparams']['attributes'], 123) |
| 202 |
assert result == params["diff"] |
| 203 |
|
| 204 |
def test_get_device_ids_failure_case_02(self, ome_connection_mock_for_template, ome_response_mock, |
| 205 |
ome_default_args): |
| 206 |
ome_connection_mock_for_template.get_all_report_details.return_value = { |
| 207 |
"report_list": [{"Id": Constants.device_id1, |
| 208 |
"DeviceServiceTag": Constants.service_tag1}, |
| 209 |
{"Id": Constants.device_id2, |
| 210 |
"DeviceServiceTag": "tag2"} |
| 211 |
]} |
| 212 |
f_module = self.get_module_mock( |
| 213 |
params={'device_id': [Constants.device_id2], 'device_service_tag': ["abcd"]}) |
| 214 |
with pytest.raises(Exception) as exc: |
| 215 |
self.module.get_device_ids( |
| 216 |
f_module, ome_connection_mock_for_template) |
| 217 |
assert exc.value.args[0] == "Unable to complete the operation because the entered target service tag(s) " \ |
| 218 |
"'{0}' are invalid.".format('abcd') |
| 219 |
|
| 220 |
def test_get_device_ids_for_no_device_failue_case_03(self, ome_connection_mock_for_template, ome_response_mock, |
| 221 |
ome_default_args): |
| 222 |
ome_connection_mock_for_template.get_all_report_details.return_value = { |
| 223 |
"report_list": [{"Id": Constants.device_id1, |
| 224 |
"DeviceServiceTag": Constants.service_tag1} |
| 225 |
], "resp_obj": ome_response_mock} |
| 226 |
f_module = self.get_module_mock( |
| 227 |
params={'device_service_tag': [Constants.service_tag1], 'device_id': []}) |
| 228 |
with pytest.raises(Exception) as exc: |
| 229 |
device_ids = self.module.get_device_ids( |
| 230 |
f_module, ome_connection_mock_for_template) |
| 231 |
assert exc.value.args[0] == "Failed to fetch the device ids." |
| 232 |
|
| 233 |
def test_get_view_id_success_case(self, ome_connection_mock_for_template, ome_response_mock): |
| 234 |
ome_response_mock.json_data = {'value': [{"Description": "", 'Id': 2}]} |
| 235 |
ome_response_mock.status_code = 200 |
| 236 |
ome_response_mock.success = True |
| 237 |
result = self.module.get_view_id(ome_response_mock, "Deployment") |
| 238 |
assert result == 2 |
| 239 |
|
| 240 |
create_payload = {"Fqdds": "All", |
| 241 |
"ViewTypeId": 4, "attributes": {"Name": "create template name"}, "SourceDeviceId": 2224} |
| 242 |
|
| 243 |
@pytest.mark.parametrize("param", [{"Fqdds": "All", |
| 244 |
"ViewTypeId": 4, "attributes": {"Name": "create template name"}, |
| 245 |
"SourceDeviceId": 2224}]) |
| 246 |
def test_get_create_payload(self, param, ome_response_mock, ome_connection_mock_for_template): |
| 247 |
f_module = self.get_module_mock(params=param) |
| 248 |
data = self.module.get_create_payload( |
| 249 |
f_module, ome_connection_mock_for_template, 2224, 4) |
| 250 |
assert data['Fqdds'] == "All" |
| 251 |
|
| 252 |
def test_get_template_by_id_success_case(self, ome_response_mock): |
| 253 |
ome_response_mock.json_data = {'value': []} |
| 254 |
ome_response_mock.status_code = 200 |
| 255 |
ome_response_mock.success = True |
| 256 |
f_module = self.get_module_mock() |
| 257 |
data = self.module.get_template_by_id(f_module, ome_response_mock, 17) |
| 258 |
assert data |
| 259 |
|
| 260 |
def test_get_template_by_name_success_case(self, ome_response_mock, ome_connection_mock_for_template): |
| 261 |
ome_response_mock.json_data = { |
| 262 |
'value': [{"Name": "test Sample Template import1", "Id": 24}]} |
| 263 |
ome_response_mock.status_code = 200 |
| 264 |
ome_response_mock.success = True |
| 265 |
f_module = self.get_module_mock() |
| 266 |
data = self.module.get_template_by_name("test Sample Template import1", f_module, |
| 267 |
ome_connection_mock_for_template) |
| 268 |
assert data["Name"] == "test Sample Template import1" |
| 269 |
assert data["Id"] == 24 |
| 270 |
|
| 271 |
def test_get_group_devices_all(self, ome_response_mock, ome_connection_mock_for_template): |
| 272 |
ome_response_mock.json_data = { |
| 273 |
'value': [{"Name": "Device1", "Id": 24}]} |
| 274 |
ome_response_mock.status_code = 200 |
| 275 |
ome_response_mock.success = True |
| 276 |
f_module = self.get_module_mock() |
| 277 |
data = self.module.get_group_devices_all( |
| 278 |
ome_connection_mock_for_template, "uri") |
| 279 |
assert data == [{"Name": "Device1", "Id": 24}] |
| 280 |
|
| 281 |
def _test_get_template_by_name_fail_case(self, ome_response_mock): |
| 282 |
ome_response_mock.json_data = { |
| 283 |
'value': [{"Name": "template by name for template name", "Id": 12}]} |
| 284 |
ome_response_mock.status_code = 500 |
| 285 |
ome_response_mock.success = False |
| 286 |
f_module = self.get_module_mock() |
| 287 |
with pytest.raises(Exception) as exc: |
| 288 |
self.module.get_template_by_name( |
| 289 |
"template by name for template name", f_module, ome_response_mock) |
| 290 |
assert exc.value.args[0] == "Unable to complete the operation because the" \ |
| 291 |
" requested template with name {0} is not present." \ |
| 292 |
.format("template by name for template name") |
| 293 |
|
| 294 |
create_payload = {"command": "create", "device_id": [25007], |
| 295 |
"ViewTypeId": 4, "attributes": {"Name": "texplate999", "Fqdds": "All"}, "template_view_type": 4} |
| 296 |
inter_payload = { |
| 297 |
"Name": "texplate999", |
| 298 |
"SourceDeviceId": 25007, |
| 299 |
"Fqdds": "All", |
| 300 |
"TypeId": 2, |
| 301 |
"ViewTypeId": 2 |
| 302 |
} |
| 303 |
payload_out = ('TemplateService/Templates', |
| 304 |
{ |
| 305 |
"Name": "texplate999", |
| 306 |
"SourceDeviceId": 25007, |
| 307 |
"Fqdds": "All", |
| 308 |
"TypeId": 2, |
| 309 |
"ViewTypeId": 2 |
| 310 |
}, "POST") |
| 311 |
|
| 312 |
@pytest.mark.parametrize("params", [{"inp": create_payload, "mid": inter_payload, "out": payload_out}]) |
| 313 |
def test__get_resource_parameters_create_success_case(self, mocker, ome_response_mock, |
| 314 |
ome_connection_mock_for_template, params): |
| 315 |
f_module = self.get_module_mock(params=params["inp"]) |
| 316 |
mocker.patch(MODULE_PATH + 'get_device_ids', |
| 317 |
return_value=[25007]) |
| 318 |
mocker.patch(MODULE_PATH + 'get_view_id', |
| 319 |
return_value=["Deployment"]) |
| 320 |
mocker.patch(MODULE_PATH + 'get_create_payload', |
| 321 |
return_value=params["mid"]) |
| 322 |
data = self.module._get_resource_parameters( |
| 323 |
f_module, ome_connection_mock_for_template) |
| 324 |
assert data == params["out"] |
| 325 |
|
| 326 |
modify_payload = {"command": "modify", "device_id": [25007], "template_id": 1234, |
| 327 |
"ViewTypeId": 4, "attributes": {"Name": "texplate999", "Fqdds": "All"}, "template_view_type": 4} |
| 328 |
inter_payload = { |
| 329 |
"Name": "texplate999", |
| 330 |
"SourceDeviceId": 25007, |
| 331 |
"Fqdds": "All", |
| 332 |
"TypeId": 2, |
| 333 |
"ViewTypeId": 2 |
| 334 |
} |
| 335 |
payload_out = ('TemplateService/Templates(1234)', |
| 336 |
{ |
| 337 |
"Name": "texplate999", |
| 338 |
"SourceDeviceId": 25007, |
| 339 |
"Fqdds": "All", |
| 340 |
"TypeId": 2, |
| 341 |
"ViewTypeId": 2 |
| 342 |
}, "PUT") |
| 343 |
|
| 344 |
@pytest.mark.parametrize("params", [{"inp": modify_payload, "mid": inter_payload, "out": payload_out}]) |
| 345 |
def test__get_resource_parameters_modify_success_case(self, mocker, ome_response_mock, |
| 346 |
ome_connection_mock_for_template, params): |
| 347 |
f_module = self.get_module_mock(params=params["inp"]) |
| 348 |
mocker.patch(MODULE_PATH + 'get_template_by_id', |
| 349 |
return_value={}) |
| 350 |
mocker.patch(MODULE_PATH + 'get_modify_payload', |
| 351 |
return_value={}) |
| 352 |
mocker.patch(MODULE_PATH + 'get_template_details', |
| 353 |
return_value={"Id": 1234, "Name": "templ1"}) |
| 354 |
data = self.module._get_resource_parameters( |
| 355 |
f_module, ome_connection_mock_for_template) |
| 356 |
assert data == ('TemplateService/Templates(1234)', {}, 'PUT') |
| 357 |
|
| 358 |
def test__get_resource_parameters_delete_success_case(self, mocker, ome_response_mock, |
| 359 |
ome_connection_mock_for_template): |
| 360 |
f_module = self.get_module_mock( |
| 361 |
{"command": "delete", "template_id": 1234}) |
| 362 |
mocker.patch(MODULE_PATH + 'get_template_details', |
| 363 |
return_value={"Id": 1234, "Name": "templ1"}) |
| 364 |
data = self.module._get_resource_parameters( |
| 365 |
f_module, ome_connection_mock_for_template) |
| 366 |
assert data == ('TemplateService/Templates(1234)', {}, 'DELETE') |
| 367 |
|
| 368 |
def test__get_resource_parameters_export_success_case(self, mocker, ome_response_mock, |
| 369 |
ome_connection_mock_for_template): |
| 370 |
f_module = self.get_module_mock( |
| 371 |
{"command": "export", "template_id": 1234}) |
| 372 |
mocker.patch(MODULE_PATH + 'get_template_details', |
| 373 |
return_value={"Id": 1234, "Name": "templ1"}) |
| 374 |
data = self.module._get_resource_parameters( |
| 375 |
f_module, ome_connection_mock_for_template) |
| 376 |
assert data == ( |
| 377 |
'TemplateService/Actions/TemplateService.Export', {'TemplateId': 1234}, 'POST') |
| 378 |
|
| 379 |
def test__get_resource_parameters_deploy_success_case(self, mocker, ome_response_mock, |
| 380 |
ome_connection_mock_for_template): |
| 381 |
f_module = self.get_module_mock( |
| 382 |
{"command": "deploy", "template_id": 1234}) |
| 383 |
mocker.patch(MODULE_PATH + 'get_device_ids', |
| 384 |
return_value=[Constants.device_id1]) |
| 385 |
mocker.patch(MODULE_PATH + 'get_deploy_payload', |
| 386 |
return_value={"deploy_payload": "value"}) |
| 387 |
mocker.patch(MODULE_PATH + 'get_template_details', |
| 388 |
return_value={"Id": 1234, "Name": "templ1"}) |
| 389 |
data = self.module._get_resource_parameters( |
| 390 |
f_module, ome_connection_mock_for_template) |
| 391 |
assert data == ('TemplateService/Actions/TemplateService.Deploy', |
| 392 |
{"deploy_payload": "value"}, 'POST') |
| 393 |
|
| 394 |
def test__get_resource_parameters_clone_success_case(self, mocker, ome_response_mock, |
| 395 |
ome_connection_mock_for_template): |
| 396 |
f_module = self.get_module_mock( |
| 397 |
{"command": "clone", "template_id": 1234, "template_view_type": 2}) |
| 398 |
mocker.patch(MODULE_PATH + 'get_view_id', |
| 399 |
return_value=2) |
| 400 |
mocker.patch(MODULE_PATH + 'get_clone_payload', |
| 401 |
return_value={"clone_payload": "value"}) |
| 402 |
mocker.patch(MODULE_PATH + 'get_template_details', |
| 403 |
return_value={"Id": 1234, "Name": "templ1"}) |
| 404 |
data = self.module._get_resource_parameters( |
| 405 |
f_module, ome_connection_mock_for_template) |
| 406 |
assert data == ('TemplateService/Actions/TemplateService.Clone', |
| 407 |
{"clone_payload": "value"}, 'POST') |
| 408 |
|
| 409 |
def test__get_resource_parameters_import_success_case(self, mocker, ome_response_mock, |
| 410 |
ome_connection_mock_for_template): |
| 411 |
f_module = self.get_module_mock( |
| 412 |
{"command": "import", "template_id": 1234, "template_view_type": 2}) |
| 413 |
mocker.patch(MODULE_PATH + 'get_view_id', |
| 414 |
return_value=2) |
| 415 |
mocker.patch(MODULE_PATH + 'get_import_payload', |
| 416 |
return_value={"import_payload": "value"}) |
| 417 |
data = self.module._get_resource_parameters( |
| 418 |
f_module, ome_connection_mock_for_template) |
| 419 |
assert data == ('TemplateService/Actions/TemplateService.Import', |
| 420 |
{"import_payload": "value"}, 'POST') |
| 421 |
|
| 422 |
@pytest.mark.parametrize("params", [{"inp": {"command": "modify"}, "mid": inter_payload, "out": payload_out}]) |
| 423 |
def test__get_resource_parameters_modify_template_none_failure_case(self, mocker, ome_response_mock, |
| 424 |
ome_connection_mock_for_template, params): |
| 425 |
f_module = self.get_module_mock(params=params["inp"]) |
| 426 |
with pytest.raises(Exception) as exc: |
| 427 |
data = self.module._get_resource_parameters( |
| 428 |
f_module, ome_connection_mock_for_template) |
| 429 |
assert exc.value.args[0] == "Enter a valid template_name or template_id" |
| 430 |
|
| 431 |
@pytest.mark.parametrize("params", |
| 432 |
[{"success": True, "json_data": {"value": [{"Name": "template_name", "Id": 123}]}, |
| 433 |
"id": 123, "gtype": True}, |
| 434 |
{"success": True, "json_data": {}, |
| 435 |
"id": 0, "gtype": False}, |
| 436 |
{"success": False, "json_data": {"value": [{"Name": "template_name", "Id": 123}]}, |
| 437 |
"id": 0, "gtype": False}, |
| 438 |
{"success": True, "json_data": {"value": [{"Name": "template_name1", "Id": 123}]}, |
| 439 |
"id": 12, "gtype": False}]) |
| 440 |
def test_get_type_id_valid(self, params, ome_connection_mock_for_template, |
| 441 |
ome_response_mock): |
| 442 |
ome_response_mock.success = params["success"] |
| 443 |
ome_response_mock.json_data = params["json_data"] |
| 444 |
id = self.module.get_type_id_valid( |
| 445 |
ome_connection_mock_for_template, params["id"]) |
| 446 |
assert id == params["gtype"] |
| 447 |
|
| 448 |
@pytest.mark.parametrize("params", |
| 449 |
[{"success": True, "json_data": {"value": [{"Description": "Deployment", "Id": 2}]}, |
| 450 |
"view": "Deployment", "gtype": 2}, |
| 451 |
{"success": True, "json_data": {}, |
| 452 |
"view": "Compliance", "gtype": 1}, |
| 453 |
{"success": False, "json_data": {"value": [{"Description": "template_name", "Id": 1}]}, |
| 454 |
"view": "Deployment", "gtype": 2}, |
| 455 |
{"success": True, "json_data": {"value": [{"Description": "template_name1", "Id": 2}]}, |
| 456 |
"view": "Deployment", "gtype": 2}]) |
| 457 |
def test_get_view_id(self, params, ome_connection_mock_for_template, |
| 458 |
ome_response_mock): |
| 459 |
ome_response_mock.success = params["success"] |
| 460 |
ome_response_mock.json_data = params["json_data"] |
| 461 |
id = self.module.get_view_id( |
| 462 |
ome_connection_mock_for_template, params["view"]) |
| 463 |
assert id == params["gtype"] |
| 464 |
|
| 465 |
@pytest.mark.parametrize("param", |
| 466 |
[{"pin": {"NetworkBootIsoModel": {"ShareDetail": {"Password": "share_password"}}}}, |
| 467 |
{"pin": {"NetworkBootIsoModel": { |
| 468 |
"ShareDetail": {"Password1": "share_password"}}}}, |
| 469 |
{"pin": {"NetworkBootIsoModel": {"ShareDetail": [{"Password1": "share_password"}]}}}]) |
| 470 |
def test_password_no_log(self, param): |
| 471 |
attributes = param["pin"] |
| 472 |
self.module.password_no_log(attributes) |
| 473 |
|
| 474 |
def test__get_resource_parameters_create_failure_case_02(self, mocker, ome_response_mock, |
| 475 |
ome_connection_mock_for_template): |
| 476 |
f_module = self.get_module_mock( |
| 477 |
{"command": "create", "template_name": "name"}) |
| 478 |
mocker.patch(MODULE_PATH + 'get_device_ids', |
| 479 |
return_value=[Constants.device_id1, Constants.device_id2]) |
| 480 |
mocker.patch(MODULE_PATH + 'get_template_by_name', |
| 481 |
return_value=("template", 1234)) |
| 482 |
with pytest.raises(Exception) as exc: |
| 483 |
data = self.module._get_resource_parameters( |
| 484 |
f_module, ome_connection_mock_for_template) |
| 485 |
assert exc.value.args[0] == "Create template requires only one reference device" |
| 486 |
|
| 487 |
def test_main_template_success_case2(self, ome_default_args, mocker, module_mock, ome_connection_mock_for_template, |
| 488 |
get_template_resource_mock, ome_response_mock): |
| 489 |
ome_connection_mock_for_template.__enter__.return_value = ome_connection_mock_for_template |
| 490 |
ome_connection_mock_for_template.invoke_request.return_value = ome_response_mock |
| 491 |
ome_response_mock.json_data = { |
| 492 |
"value": [{"device_id": "1111", "command": "create", "attributes": {"Name": "new 1template name"}}]} |
| 493 |
ome_response_mock.status_code = 200 |
| 494 |
ome_default_args.update( |
| 495 |
{"device_id": "1111", "command": "create", "attributes": {"Name": "new 1template name"}}) |
| 496 |
ome_response_mock.success = True |
| 497 |
mocker.patch(MODULE_PATH + '_get_resource_parameters', |
| 498 |
return_value=(TEMPLATE_RESOURCE, "template_payload", "POST")) |
| 499 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 500 |
result = self._run_module(ome_default_args) |
| 501 |
assert result['changed'] is True |
| 502 |
assert result['msg'] == "Successfully created a template with ID {0}".format( |
| 503 |
ome_response_mock.json_data) |
| 504 |
|
| 505 |
def test_get_import_payload_success_case_01(self, ome_connection_mock_for_template): |
| 506 |
f_module = self.get_module_mock( |
| 507 |
params={"attributes": {"Name": "template1", "Content": "Content"}}) |
| 508 |
self.module.get_import_payload( |
| 509 |
f_module, ome_connection_mock_for_template, 2) |
| 510 |
|
| 511 |
def test_get_deploy_payload_success_case_01(self): |
| 512 |
module_params = {"attributes": {"Name": "template1"}} |
| 513 |
self.module.get_deploy_payload( |
| 514 |
module_params, [Constants.device_id1], 1234) |
| 515 |
|
| 516 |
@pytest.mark.parametrize("param", |
| 517 |
[{"mparams": {"attributes": {"Name": "template1"}}, "name": "template0", |
| 518 |
"template_id": 123, |
| 519 |
"clone_payload": {"SourceTemplateId": 123, "NewTemplateName": "template1", |
| 520 |
"ViewTypeId": 2}}]) |
| 521 |
def test_get_clone_payload_success_case_01(self, param, ome_connection_mock_for_template): |
| 522 |
f_module = self.get_module_mock(param["mparams"]) |
| 523 |
module_params = param["mparams"] |
| 524 |
payload = self.module.get_clone_payload( |
| 525 |
f_module, ome_connection_mock_for_template, param['template_id'], 2) |
| 526 |
assert payload == param['clone_payload'] |
| 527 |
|
| 528 |
@pytest.mark.parametrize("param", |
| 529 |
[{"inp": {"command": "create", "template_name": "name", "device_id": [None], |
| 530 |
"device_service_tag": [None]}, |
| 531 |
"msg": "Argument device_id or device_service_tag has null values"}, |
| 532 |
{"inp": {"command": "deploy", "template_name": "name", "device_id": [None], |
| 533 |
"device_service_tag": [None]}, |
| 534 |
"msg": "Argument device_id or device_service_tag has null values"}, |
| 535 |
{"inp": {"command": "import", "template_name": "name", "device_id": [], |
| 536 |
"device_service_tag": []}, |
| 537 |
"msg": "Argument 'Name' required in attributes for import operation"}, |
| 538 |
{"inp": {"command": "import", "attributes": {"Name": "name"}, "device_id": [], |
| 539 |
"device_service_tag": []}, |
| 540 |
"msg": "Argument 'Content' required in attributes for import operation"}, |
| 541 |
{"inp": {"command": "clone", "template_name": "name", "device_id": [], |
| 542 |
"device_service_tag": []}, |
| 543 |
"msg": "Argument 'Name' required in attributes for clone operation"} |
| 544 |
]) |
| 545 |
def test_validate_inputs(self, param, mocker): |
| 546 |
f_module = self.get_module_mock(param["inp"]) |
| 547 |
mocker.patch(MODULE_PATH + 'password_no_log') |
| 548 |
with pytest.raises(Exception) as exc: |
| 549 |
self.module._validate_inputs(f_module) |
| 550 |
assert exc.value.args[0] == param["msg"] |
| 551 |
|
| 552 |
@pytest.mark.parametrize("param", [ |
| 553 |
{"inp": {"command": "deploy", "template_name": "name", |
| 554 |
"device_group_names": ["mygroup"]}, |
| 555 |
"group": {'Id': 23, "Name": "mygroup"}, |
| 556 |
"dev_list": [1, 2, 3]}]) |
| 557 |
def test_get_group_details(self, param, ome_connection_mock_for_template, mocker, |
| 558 |
ome_response_mock): |
| 559 |
f_module = self.get_module_mock(param["inp"]) |
| 560 |
ome_response_mock.json_data = { |
| 561 |
"value": [{'Id': 1, "Name": "mygroup3"}, {'Id': 2, "Name": "mygroup2"}, {'Id': 3, "Name": "mygroup"}]} |
| 562 |
ome_response_mock.status_code = 200 |
| 563 |
mocker.patch(MODULE_PATH + 'get_group_devices_all', |
| 564 |
return_value=[{'Id': 1}, {'Id': 2}, {'Id': 3}]) |
| 565 |
dev_list = self.module.get_group_details( |
| 566 |
ome_connection_mock_for_template, f_module) |
| 567 |
assert dev_list == param["dev_list"] |
| 568 |
|
| 569 |
@pytest.mark.parametrize("param", [ |
| 570 |
{"inp": {"command": "deploy", "template_name": "name", |
| 571 |
"device_group_names": ["mygroup"]}, |
| 572 |
"group": {'Id': 23, "Name": "mygroup"}, |
| 573 |
"dev_list": [1, 2, 3]}]) |
| 574 |
def test_modify_payload(self, param, ome_connection_mock_for_template, mocker, |
| 575 |
ome_response_mock): |
| 576 |
f_module = self.get_module_mock(param["inp"]) |
| 577 |
ome_response_mock.json_data = { |
| 578 |
"value": [{'Id': 1, "Name": "mygroup3"}, {'Id': 2, "Name": "mygroup2"}, {'Id': 3, "Name": "mygroup"}]} |
| 579 |
ome_response_mock.status_code = 200 |
| 580 |
mocker.patch(MODULE_PATH + 'get_group_devices_all', |
| 581 |
return_value=[{'Id': 1}, {'Id': 2}, {'Id': 3}]) |
| 582 |
dev_list = self.module.get_group_details( |
| 583 |
ome_connection_mock_for_template, f_module) |
| 584 |
assert dev_list == param["dev_list"] |
| 585 |
|
| 586 |
@pytest.mark.parametrize("params", [ |
| 587 |
{"mparams": {"command": "modify", "name": "profile", "attributes": { |
| 588 |
"Attributes": [ |
| 589 |
{ |
| 590 |
"Id": 93812, |
| 591 |
"IsIgnored": False, |
| 592 |
"Value": "Aisle Five" |
| 593 |
}, |
| 594 |
{ |
| 595 |
"DisplayName": 'System, Server Topology, ServerTopology 1 Aisle Name', |
| 596 |
"IsIgnored": False, |
| 597 |
"Value": "Aisle 5" |
| 598 |
}]}}, |
| 599 |
"success": True, "template": {"Name": "template_name", "Id": 123, "Description": "temp described"}, |
| 600 |
"json_data": 0, "get_template_by_name": {"Name": "template1", "Id": 122, "Description": "temp described"}, |
| 601 |
"res": "No changes found to be applied."}, |
| 602 |
{"mparams": {"command": "modify", "name": "profile", "attributes": { |
| 603 |
"Name": "new_name", |
| 604 |
"Attributes": [ |
| 605 |
{ |
| 606 |
"Id": 93812, |
| 607 |
"IsIgnored": False, |
| 608 |
"Value": "Aisle Five" |
| 609 |
}, |
| 610 |
{ |
| 611 |
"DisplayName": 'System, Server Topology, ServerTopology 1 Aisle Name', |
| 612 |
"IsIgnored": False, |
| 613 |
"Value": "Aisle 5" |
| 614 |
}]}}, "success": True, |
| 615 |
"template": {"Name": "template_name", "Id": 123, "Description": "temp described"}, "json_data": 0, |
| 616 |
"get_template_by_name": {"Name": "template1", "Id": 122, "Description": "temp described"}, |
| 617 |
"res": "Template with name 'new_name' already exists."} |
| 618 |
]) |
| 619 |
def test_modify_payload(self, params, ome_connection_mock_for_template, mocker, |
| 620 |
ome_response_mock): |
| 621 |
ome_response_mock.success = params.get("success", True) |
| 622 |
ome_response_mock.json_data = params["json_data"] |
| 623 |
mocker.patch(MODULE_PATH + 'get_template_by_name', |
| 624 |
return_value=params.get('get_template_by_name')) |
| 625 |
mocker.patch(MODULE_PATH + 'attributes_check', |
| 626 |
return_value=params.get('attributes_check', 0)) |
| 627 |
f_module = self.get_module_mock( |
| 628 |
params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 629 |
error_message = params["res"] |
| 630 |
with pytest.raises(Exception) as err: |
| 631 |
self.module.get_modify_payload( |
| 632 |
f_module, ome_connection_mock_for_template, params.get('template')) |
| 633 |
assert err.value.args[0] == error_message |
| 634 |
|
| 635 |
@pytest.mark.parametrize("params", [ |
| 636 |
{"json_data": {"value": [ |
| 637 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 638 |
'DeviceId': 1234, "Type": 1000}, |
| 639 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'DeviceId': 1235, "Type": 1000}], |
| 640 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 641 |
'DeviceId': 1234, "Type": 1000}]}, |
| 642 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Running"}}, True), |
| 643 |
'message': "Template operation is in progress. Task excited after 'job_wait_timeout'.", |
| 644 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234} |
| 645 |
}, |
| 646 |
{"json_data": {"value": [ |
| 647 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 648 |
'DeviceId': 1234, "Type": 1000}, |
| 649 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'DeviceId': 1235, "Type": 1000}], |
| 650 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 651 |
'DeviceId': 1234, "Type": 1000}]}, |
| 652 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Running"}}, True), |
| 653 |
'message': "Changes found to be applied.", |
| 654 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234}, |
| 655 |
"check_mode": True |
| 656 |
}, |
| 657 |
{"json_data": {"value": [ |
| 658 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 659 |
'TemplateId': 1, 'DeviceId': 1234, "Type": 1000}, |
| 660 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'TemplateId': 12, 'DeviceId': 1235, "Type": 1000}], |
| 661 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 662 |
'DeviceId': 1234, "Type": 1000}]}, |
| 663 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Running"}}, True), |
| 664 |
"get_device_ids": [123, 1234], |
| 665 |
'message': "The device(s) '123' have been assigned the template(s) '1' respectively. Please unassign the profiles from the devices.", |
| 666 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234} |
| 667 |
}, |
| 668 |
{"json_data": {"value": [ |
| 669 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 670 |
'TemplateId': 123, 'DeviceId': 1234, "Type": 1000}, |
| 671 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'TemplateId': 12, 'DeviceId': 1235, "Type": 1000}], |
| 672 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 673 |
'DeviceId': 1234, "Type": 1000}]}, |
| 674 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Running"}}, True), |
| 675 |
"get_device_ids": [123], |
| 676 |
'message': "No changes found to be applied.", |
| 677 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234} |
| 678 |
}, |
| 679 |
{"json_data": {"value": [ |
| 680 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 681 |
'TemplateId': 123, 'DeviceId': 1234, "Type": 1000}, |
| 682 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'TemplateId': 12, 'DeviceId': 1235, "Type": 1000}], |
| 683 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 684 |
'DeviceId': 1234, "Type": 1000}]}, |
| 685 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Running"}}, True), |
| 686 |
"get_device_ids": [123], |
| 687 |
'message': "No changes found to be applied.", |
| 688 |
'mparams': {"command": "delete", "template_id": 12, "device_id": 1234} |
| 689 |
} |
| 690 |
]) |
| 691 |
def test_ome_template_success(self, params, ome_connection_mock_for_template, ome_response_mock, |
| 692 |
ome_default_args, module_mock, mocker): |
| 693 |
ome_response_mock.success = params.get("success", True) |
| 694 |
ome_response_mock.json_data = params['json_data'] |
| 695 |
ome_connection_mock_for_template.get_all_report_details.return_value = params[ |
| 696 |
'json_data'] |
| 697 |
ome_default_args.update(params['mparams']) |
| 698 |
mocks = ["job_tracking", "get_device_ids"] |
| 699 |
for m in mocks: |
| 700 |
if m in params: |
| 701 |
mocker.patch(MODULE_PATH + m, return_value=params.get(m, {})) |
| 702 |
result = self._run_module( |
| 703 |
ome_default_args, check_mode=params.get('check_mode', False)) |
| 704 |
assert result['msg'] == params['message'] |
| 705 |
|
| 706 |
@pytest.mark.parametrize("params", [ |
| 707 |
{"json_data": {"value": [ |
| 708 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 709 |
'DeviceId': 1234, "Type": 1000}, |
| 710 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'DeviceId': 1235, "Type": 1000}], |
| 711 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 712 |
'DeviceId': 1234, "Type": 1000}]}, |
| 713 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Complete"}}, True), |
| 714 |
'message': "Failed to deploy template.", |
| 715 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234} |
| 716 |
}, |
| 717 |
{"json_data": {"value": [ |
| 718 |
{'Id': 123, 'TargetId': 123, 'ProfileState': 1, |
| 719 |
'DeviceId': 1234, "Type": 1000}, |
| 720 |
{'Id': 234, 'TargetId': 235, 'ProfileState': 1, 'DeviceId': 1235, "Type": 1000}], |
| 721 |
"report_list": [{'Id': 1234, 'PublicAddress': "XX.XX.XX.XX", |
| 722 |
'DeviceId': 1234, "Type": 1000}]}, |
| 723 |
"job_tracking": (True, "msg", {'LastRunStatus': {"Name": "Complete"}}, True), |
| 724 |
"get_device_ids": [], |
| 725 |
'message': "There are no devices provided for deploy operation", |
| 726 |
'mparams': {"command": "deploy", "template_id": 123, "device_id": 1234} |
| 727 |
} |
| 728 |
]) |
| 729 |
def test_ome_template_fail_json(self, params, ome_connection_mock_for_template, ome_response_mock, |
| 730 |
ome_default_args, module_mock, mocker): |
| 731 |
ome_response_mock.success = params.get("success", True) |
| 732 |
ome_response_mock.json_data = params['json_data'] |
| 733 |
ome_connection_mock_for_template.get_all_report_details.return_value = params[ |
| 734 |
'json_data'] |
| 735 |
ome_default_args.update(params['mparams']) |
| 736 |
mocks = ["job_tracking", "get_device_ids"] |
| 737 |
for m in mocks: |
| 738 |
if m in params: |
| 739 |
mocker.patch(MODULE_PATH + m, return_value=params.get(m, {})) |
| 740 |
result = self._run_module(ome_default_args) |
| 741 |
assert result['changed'] is False |
| 742 |
|
| 743 |
@pytest.mark.parametrize("exc_type", |
| 744 |
[IOError, ValueError, TypeError, ConnectionError, |
| 745 |
HTTPError, URLError, SSLError]) |
| 746 |
def test_main_template_exception_case(self, exc_type, mocker, ome_default_args, |
| 747 |
ome_connection_mock_for_template, ome_response_mock): |
| 748 |
ome_default_args.update( |
| 749 |
{"command": "export", "template_name": "t1", 'attributes': {'Attributes': "myattr1"}}) |
| 750 |
ome_response_mock.status_code = 400 |
| 751 |
ome_response_mock.success = False |
| 752 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 753 |
if exc_type == URLError: |
| 754 |
mocker.patch(MODULE_PATH + 'password_no_log') |
| 755 |
mocker.patch(MODULE_PATH + '_get_resource_parameters', |
| 756 |
side_effect=exc_type("url open error")) |
| 757 |
result = self._run_module(ome_default_args) |
| 758 |
assert result["unreachable"] is True |
| 759 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 760 |
mocker.patch(MODULE_PATH + '_get_resource_parameters', |
| 761 |
side_effect=exc_type("exception message")) |
| 762 |
result = self._run_module(ome_default_args) |
| 763 |
assert result['failed'] is True |
| 764 |
else: |
| 765 |
mocker.patch(MODULE_PATH + '_get_resource_parameters', |
| 766 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 767 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 768 |
result = self._run_module(ome_default_args) |
| 769 |
assert result['failed'] is True |
| 770 |
assert 'msg' in result |