| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
import json |
| 13 |
from io import StringIO |
| 14 |
from ssl import SSLError |
| 15 |
|
| 16 |
import pytest |
| 17 |
from ansible.module_utils._text import to_text |
| 18 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 19 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 20 |
from ansible_collections.dellemc.openmanage.plugins.modules import ome_profile |
| 21 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 22 |
from unittest.mock import MagicMock |
| 23 |
|
| 24 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.ome_profile.' |
| 25 |
CHANGES_MSG = "Changes found to be applied." |
| 26 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture |
| 30 |
def ome_connection_mock_for_profile(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 |
return ome_connection_mock_obj |
| 35 |
|
| 36 |
|
| 37 |
class TestOmeProfile(FakeAnsibleModule): |
| 38 |
module = ome_profile |
| 39 |
|
| 40 |
@pytest.mark.parametrize("params", |
| 41 |
[{"mparams": {"template_id": 123}, "success": True, |
| 42 |
"json_data": {"value": [{"Name": "vlan_name", "Id": 123, "IdentityPoolId": 23}]}, |
| 43 |
"res": {"Name": "vlan_name", "Id": 123, "IdentityPoolId": 23}}, |
| 44 |
{"mparams": {"template_name": "temp1"}, "success": True, |
| 45 |
"json_data": {"value": [{"Name": "temp1", "Id": 123, "IdentityPoolId": 23}]}, |
| 46 |
"res": {"Name": "temp1", "Id": 123, "IdentityPoolId": 23}}]) |
| 47 |
def test_get_template_details(self, params, ome_connection_mock_for_profile, ome_response_mock): |
| 48 |
ome_response_mock.success = params.get("success", True) |
| 49 |
ome_response_mock.json_data = params["json_data"] |
| 50 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 51 |
result = self.module.get_template_details(f_module, ome_connection_mock_for_profile) |
| 52 |
assert result == params["res"] |
| 53 |
|
| 54 |
@pytest.mark.parametrize("params", |
| 55 |
[{"mparams": {"device_id": 123}, "success": True, |
| 56 |
"json_data": {"value": [{"Name": "vlan_name", "Id": 123, "IdentityPoolId": 23}]}, |
| 57 |
"res": {"Name": "vlan_name", "Id": 123, "IdentityPoolId": 23}}, |
| 58 |
{"mparams": {"device_service_tag": "ABC1234"}, "success": True, |
| 59 |
"json_data": {"value": [{"Identifier": "ABC1234", "Id": 123, "IdentityPoolId": 23}]}, |
| 60 |
"res": {"Identifier": "ABC1234", "Id": 123, "IdentityPoolId": 23}}]) |
| 61 |
def test_get_target_details(self, params, ome_connection_mock_for_profile, ome_response_mock): |
| 62 |
ome_response_mock.success = params.get("success", True) |
| 63 |
ome_response_mock.json_data = params["json_data"] |
| 64 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 65 |
result = self.module.get_target_details(f_module, ome_connection_mock_for_profile) |
| 66 |
assert result == params["res"] |
| 67 |
|
| 68 |
@pytest.mark.parametrize("params", |
| 69 |
[{"mparams": { |
| 70 |
"attributes": { |
| 71 |
"Attributes": [ |
| 72 |
{ |
| 73 |
"Id": 93812, |
| 74 |
"IsIgnored": False, |
| 75 |
"Value": "Aisle Five" |
| 76 |
}, |
| 77 |
{ |
| 78 |
"DisplayName": 'System, Server Topology, ServerTopology 1 Aisle Name', |
| 79 |
"IsIgnored": False, |
| 80 |
"Value": "Aisle 5" |
| 81 |
} |
| 82 |
] |
| 83 |
}}, "success": True, |
| 84 |
"json_data": { |
| 85 |
"Id": 11, |
| 86 |
"Name": "ProfileViewEditAttributes", |
| 87 |
"AttributeGroupNames": [], |
| 88 |
"AttributeGroups": [ |
| 89 |
{ |
| 90 |
"GroupNameId": 5, |
| 91 |
"DisplayName": "System", |
| 92 |
"SubAttributeGroups": [ |
| 93 |
{ |
| 94 |
"GroupNameId": 33016, |
| 95 |
"DisplayName": "Server Operating System", |
| 96 |
"SubAttributeGroups": [], |
| 97 |
"Attributes": [ |
| 98 |
{ |
| 99 |
"AttributeId": 93820, |
| 100 |
"DisplayName": "ServerOS 1 Server Host Name", |
| 101 |
"Description": None, |
| 102 |
"Value": None, |
| 103 |
"IsReadOnly": False, |
| 104 |
"IsIgnored": True, |
| 105 |
} |
| 106 |
] |
| 107 |
}, |
| 108 |
{ |
| 109 |
"GroupNameId": 33019, |
| 110 |
"DisplayName": "Server Topology", |
| 111 |
"SubAttributeGroups": [], |
| 112 |
"Attributes": [ |
| 113 |
{ |
| 114 |
"AttributeId": 93812, |
| 115 |
"CustomId": 0, |
| 116 |
"AttributeEditInfoId": 2248, |
| 117 |
"DisplayName": "ServerTopology 1 Aisle Name", |
| 118 |
"Description": None, |
| 119 |
"Value": "Aisle 5", |
| 120 |
"IsReadOnly": False, |
| 121 |
"IsIgnored": True, |
| 122 |
}, |
| 123 |
{ |
| 124 |
"AttributeId": 93811, |
| 125 |
"DisplayName": "ServerTopology 1 Data Center Name", |
| 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_profile, ome_response_mock): |
| 197 |
ome_response_mock.success = params.get("success", True) |
| 198 |
ome_response_mock.json_data = params["json_data"] |
| 199 |
self.get_module_mock(params=params["mparams"]) |
| 200 |
result = self.module.attributes_check(ome_connection_mock_for_profile, |
| 201 |
params['mparams']['attributes'], 123) |
| 202 |
assert result == params["diff"] |
| 203 |
|
| 204 |
@pytest.mark.parametrize("inp_attr, attr_groups, attr_map, expected_diff", [ |
| 205 |
( |
| 206 |
{"Attributes": [{"Id": 1, "Value": "val1", "IsIgnored": False}]}, |
| 207 |
[{"Attributes": [{"AttributeId": 1, "Value": "val1", "IsIgnored": False}]}], |
| 208 |
{1: {"Value": "val1", "IsIgnored": False}}, |
| 209 |
0 |
| 210 |
), |
| 211 |
( |
| 212 |
{"Attributes": [{"Id": 1, "Value": "val2", "IsIgnored": False}]}, |
| 213 |
[{"Attributes": [{"AttributeId": 1, "Value": "val1", "IsIgnored": False}]}], |
| 214 |
{1: {"Value": "val1", "IsIgnored": False}}, |
| 215 |
1 |
| 216 |
), |
| 217 |
( |
| 218 |
{"Attributes": [{"Id": 2, "Value": "val1", "IsIgnored": False}]}, |
| 219 |
[{"Attributes": [{"AttributeId": 1, "Value": "val1", "IsIgnored": False}]}], |
| 220 |
{}, |
| 221 |
0 |
| 222 |
), |
| 223 |
]) |
| 224 |
def test_attributes_check_diff(self, mocker, inp_attr, attr_groups, attr_map, expected_diff): |
| 225 |
rest_obj = MagicMock() |
| 226 |
rest_obj.invoke_request.return_value.json_data = {"AttributeGroups": attr_groups} |
| 227 |
|
| 228 |
mocker.patch(MODULE_PATH + "normalize_display_names", return_value=inp_attr["Attributes"]) |
| 229 |
mocker.patch(MODULE_PATH + "get_subattr_all", return_value=({}, attr_map)) |
| 230 |
|
| 231 |
diff = self.module.attributes_check(rest_obj, inp_attr, profile_id=123) |
| 232 |
assert diff == expected_diff |
| 233 |
|
| 234 |
@pytest.mark.parametrize("params", [{"mparams": {"command": 'create'}, "func": "create_profile"}, |
| 235 |
{"mparams": {"command": 'modify'}, "func": "modify_profile"}, |
| 236 |
{"mparams": {"command": 'delete'}, "func": "delete_profile"}, |
| 237 |
{"mparams": {"command": 'assign'}, "func": "assign_profile"}, |
| 238 |
{"mparams": {"command": 'unassign'}, "func": "unassign_profile"}, |
| 239 |
{"mparams": {"command": 'migrate'}, "func": "migrate_profile"}]) |
| 240 |
def test_profile_operation(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 241 |
mocker.patch(MODULE_PATH + params.get('func'), return_value={"Id": 12}) |
| 242 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 243 |
self.module.profile_operation(f_module, ome_connection_mock_for_profile) |
| 244 |
|
| 245 |
@pytest.mark.parametrize("params", [{"mparams": {"name": "p1"}, "success": True, "json_data": { |
| 246 |
"value": [{"Id": 123, "ProfileName": "p1"}]}, "res": {"Id": 123, "ProfileName": "p1"}}]) |
| 247 |
def test_get_profile(self, params, ome_connection_mock_for_profile, ome_response_mock): |
| 248 |
ome_response_mock.success = params.get("success", True) |
| 249 |
ome_response_mock.json_data = params["json_data"] |
| 250 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 251 |
result = self.module.get_profile(ome_connection_mock_for_profile, f_module) |
| 252 |
assert result == params["res"] |
| 253 |
|
| 254 |
@pytest.mark.parametrize("params", [{"mparams": { |
| 255 |
"command": "create", "template_name": "t1", "name_prefix": "profile", |
| 256 |
"number_of_profiles": 2, "description": "Created 1", |
| 257 |
"boot_to_network_iso": { |
| 258 |
"boot_to_network": True, |
| 259 |
"share_type": "CIFS", |
| 260 |
"share_ip": "100.200.300", |
| 261 |
"share_user": "shareuser", |
| 262 |
"share_pwd": "sharepwd", |
| 263 |
"workgroup": "workgroup", |
| 264 |
"iso_path": "pathofiso.iso", |
| 265 |
"iso_timeout": 8 |
| 266 |
} |
| 267 |
}, |
| 268 |
"success": True, |
| 269 |
"json_data": [1, 2], |
| 270 |
"res": "Successfully created 2 profile(s)."}, |
| 271 |
{ |
| 272 |
"mparams": |
| 273 |
{ |
| 274 |
"command": "create", |
| 275 |
"template_name": "t1", |
| 276 |
"name_prefix": "profile", |
| 277 |
"number_of_profiles": 1 |
| 278 |
}, |
| 279 |
"success": True, |
| 280 |
"json_data": [1], |
| 281 |
"res": "Successfully created 1 profile(s)."}, |
| 282 |
{ |
| 283 |
"mparams": |
| 284 |
{ |
| 285 |
"command": "create", |
| 286 |
"template_name": "t1", |
| 287 |
"name_prefix": "profile", |
| 288 |
"number_of_profiles": 1 |
| 289 |
}, |
| 290 |
"success": True, "check_mode": True, "json_data": [1], "res": CHANGES_MSG} |
| 291 |
]) |
| 292 |
def test_create_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 293 |
ome_response_mock.success = params.get("success", True) |
| 294 |
ome_response_mock.json_data = params["json_data"] |
| 295 |
mocker.patch(MODULE_PATH + 'get_template_details', return_value={"Id": 12}) |
| 296 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 297 |
error_message = params["res"] |
| 298 |
with pytest.raises(Exception) as err: |
| 299 |
self.module.create_profile(f_module, ome_connection_mock_for_profile) |
| 300 |
assert err.value.args[0] == error_message |
| 301 |
|
| 302 |
@pytest.mark.parametrize("params", [ |
| 303 |
{"mparams": {"command": "modify", "name": "profile"}, |
| 304 |
"success": True, |
| 305 |
"prof": {}, "json_data": 0, |
| 306 |
"res": "Profile with the name 'profile' not found."}, |
| 307 |
{"mparams": {"command": "modify", "name": "profile", "new_name": "modified profile", |
| 308 |
"description": "new description", |
| 309 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 310 |
"iso_path": "path/to/my_iso.iso", |
| 311 |
"iso_timeout": 8}, |
| 312 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", |
| 313 |
"IsIgnored": True}]}}, "success": True, |
| 314 |
"prof": {"Id": 1234, |
| 315 |
"ProfileName": "jrofile 00002", |
| 316 |
"ProfileDescription": "from source template t1", |
| 317 |
"NetworkBootToIso": {"BootToNetwork": True, "ShareType": "NFS", "IsoPath": "abcd.iso", |
| 318 |
"ShareDetail": {"IpAddress": "XX.XX.XX.XX", "ShareName": "XX.XX.XX.XX", }, |
| 319 |
"IsoTimeout": 4}, |
| 320 |
"ProfileState": 0, }, |
| 321 |
"json_data": 0, |
| 322 |
"res": "Successfully modified the profile."}, |
| 323 |
{"mparams": {"command": "modify", "name": "myprofile", "new_name": "myprofile"}, |
| 324 |
"success": True, |
| 325 |
"prof": {"Id": 1234, "ProfileName": "myprofile", "ProfileDescription": "my description"}, |
| 326 |
"json_data": 0, "res": "No changes found to be applied."}, |
| 327 |
{"mparams": {"command": "modify", "name": "profile", "new_name": "modified profile", |
| 328 |
"description": "new description", |
| 329 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 330 |
"iso_path": "path/to/my_iso.iso", "iso_timeout": 8}, |
| 331 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", |
| 332 |
"IsIgnored": True}]}}, "success": True, |
| 333 |
"prof": {"Id": 1234, "ProfileName": "jrofile 00002", |
| 334 |
"ProfileDescription": "from source template t1", |
| 335 |
"NetworkBootToIso": { |
| 336 |
"BootToNetwork": True, "ShareType": "NFS", "IsoPath": "abcd.iso", |
| 337 |
"ShareDetail": {"IpAddress": "XX.XX.XX.XX", "ShareName": "XX.XX.XX.XX"}, "IsoTimeout": 4}, |
| 338 |
"ProfileState": 0, }, |
| 339 |
"json_data": 0, "attributes_check": 2, "check_mode": True, "res": CHANGES_MSG} |
| 340 |
]) |
| 341 |
def test_modify_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 342 |
ome_response_mock.success = params.get("success", True) |
| 343 |
ome_response_mock.json_data = params["json_data"] |
| 344 |
mocker.patch(MODULE_PATH + 'get_profile', return_value=params.get('prof')) |
| 345 |
mocker.patch(MODULE_PATH + 'attributes_check', return_value=params.get('attributes_check', 0)) |
| 346 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 347 |
error_message = params["res"] |
| 348 |
with pytest.raises(Exception) as err: |
| 349 |
self.module.modify_profile(f_module, ome_connection_mock_for_profile) |
| 350 |
assert err.value.args[0] == error_message |
| 351 |
|
| 352 |
@pytest.mark.parametrize("params", [ |
| 353 |
{"mparams": {"command": "delete", "name": "profile"}, "success": True, |
| 354 |
"prof": {"Id": 12, "ProfileState": 4}, "json_data": 0, |
| 355 |
"res": "Profile has to be in an unassigned state for it to be deleted."}, |
| 356 |
{"mparams": {"command": "delete", "name": "profile"}, "success": True, |
| 357 |
"prof": {"Id": 12, "ProfileState": 0}, "json_data": 0, |
| 358 |
"res": "Successfully deleted the profile."}, |
| 359 |
{"mparams": {"command": "delete", "name": "profile"}, "success": True, |
| 360 |
"prof": {"Id": 12, "ProfileState": 0}, "json_data": 0, "check_mode": True, |
| 361 |
"res": CHANGES_MSG}, |
| 362 |
{"mparams": {"command": "delete", "name": "profile"}, "success": True, |
| 363 |
"prof": {}, "json_data": 0, |
| 364 |
"res": "Profile with the name 'profile' not found."}, |
| 365 |
{"mparams": {"command": "delete", "filters": "profile"}, "success": True, |
| 366 |
"prof": {"Id": 12, "ProfileState": 0}, "json_data": 0, |
| 367 |
"res": "Successfully completed the delete operation."}, |
| 368 |
{"mparams": {"command": "delete", "filters": "profile"}, "success": True, |
| 369 |
"prof": {"Id": 12, "ProfileState": 0}, "json_data": 0, "check_mode": True, |
| 370 |
"res": CHANGES_MSG}, |
| 371 |
]) |
| 372 |
def test_delete_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 373 |
ome_response_mock.success = params.get("success", True) |
| 374 |
ome_response_mock.json_data = params["json_data"] |
| 375 |
mocker.patch(MODULE_PATH + 'get_profile', return_value=params.get('prof')) |
| 376 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 377 |
error_message = params["res"] |
| 378 |
with pytest.raises(Exception) as err: |
| 379 |
self.module.delete_profile(f_module, ome_connection_mock_for_profile) |
| 380 |
assert err.value.args[0] == error_message |
| 381 |
|
| 382 |
@pytest.mark.parametrize("params", [ |
| 383 |
{"mparams": {"command": "assign", "name": "profile"}, "success": True, "prof": {}, |
| 384 |
"json_data": 0, "res": "Profile with the name 'profile' not found."}, |
| 385 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234}, "success": True, |
| 386 |
"prof": {"Id": 123, "ProfileState": 0}, "target": {"Id": 234, "Name": "mytarget"}, |
| 387 |
"json_data": [234, 123], |
| 388 |
"res": "The target device is invalid for the given profile."}, |
| 389 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234, |
| 390 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 391 |
"iso_path": "path/to/my_iso.iso", |
| 392 |
"iso_timeout": 8}, |
| 393 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 394 |
"success": True, |
| 395 |
"prof": {"Id": 123, "ProfileState": 0}, "target": {"Id": 234, "Name": "mytarget"}, "json_data": [23, 123], |
| 396 |
"res": "Successfully applied the assign operation. No job was triggered."}, |
| 397 |
{"mparams": {"command": "assign", "name": "profile", "device_service_tag": "ABCDEFG", |
| 398 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 399 |
"iso_path": "path/to/my_iso.iso", |
| 400 |
"iso_timeout": 8}, |
| 401 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 402 |
"success": True, "prof": {"Id": 123, "ProfileState": 0}, "target": {"Id": 234, "Name": "mytarget"}, |
| 403 |
"json_data": [23, 123], "res": "Successfully applied the assign operation. No job was triggered."}, |
| 404 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234, |
| 405 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 406 |
"iso_path": "path/to/my_iso.iso", |
| 407 |
"iso_timeout": 8}, |
| 408 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 409 |
"success": True, |
| 410 |
"prof": {"Id": 123, "ProfileState": 4, "TargetId": 234}, "target": {"Id": 234, "Name": "mytarget"}, |
| 411 |
"json_data": [23, 123], |
| 412 |
"res": "The profile is assigned to the target 234."}, |
| 413 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234, |
| 414 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 415 |
"iso_path": "path/to/my_iso.iso", |
| 416 |
"iso_timeout": 8}, |
| 417 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 418 |
"success": True, |
| 419 |
"prof": {"Id": 123, "ProfileState": 4, "TargetId": 235}, "target": {"Id": 234, "Name": "mytarget"}, |
| 420 |
"json_data": [23, 123], |
| 421 |
"res": "The profile is assigned to a different target. Use the migrate command or unassign the profile and " |
| 422 |
"then proceed with assigning the profile to the target."}, |
| 423 |
{"mparams": {"command": "assign", "name": "profile", "device_service_tag": "STG1234", |
| 424 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 425 |
"iso_path": "path/to/my_iso.iso", |
| 426 |
"iso_timeout": 8}, |
| 427 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 428 |
"success": True, |
| 429 |
"prof": {"Id": 123, "ProfileState": 1, "TargetId": 235, "TargetName": "STG1234"}, "target": "Target invalid.", |
| 430 |
"json_data": [23, 123], |
| 431 |
"res": "The profile is assigned to the target STG1234."}, |
| 432 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 123, |
| 433 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 434 |
"iso_path": "path/to/my_iso.iso", |
| 435 |
"iso_timeout": 8}, |
| 436 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 437 |
"success": True, |
| 438 |
"prof": {"Id": 123, "ProfileState": 1, "TargetId": 235, "TargetName": "STG1234"}, "target": "Target invalid.", |
| 439 |
"json_data": [23, 123], |
| 440 |
"res": "Target invalid."}, |
| 441 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234, |
| 442 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 443 |
"iso_path": "path/to/my_iso.iso", |
| 444 |
"iso_timeout": 8}, |
| 445 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 446 |
"success": True, "check_mode": True, |
| 447 |
"prof": {"Id": 123, "ProfileState": 0}, "target": {"Id": 234, "Name": "mytarget"}, "json_data": [23, 123], |
| 448 |
"res": CHANGES_MSG}, |
| 449 |
{"mparams": {"command": "assign", "name": "profile", "device_id": 234, |
| 450 |
"boot_to_network_iso": {"boot_to_network": True, "share_type": "NFS", "share_ip": "XX.XX.XX.XX", |
| 451 |
"iso_path": "path/to/my_iso.iso", |
| 452 |
"iso_timeout": 8}, |
| 453 |
"attributes": {"Attributes": [{"Id": 4506, "Value": "server attr 1", "IsIgnored": True}]}}, |
| 454 |
"success": True, |
| 455 |
"prof": {"Id": 123, "ProfileState": 0, "DeploymentTaskId": 12}, "target": {"Id": 234, "Name": "mytarget"}, |
| 456 |
"json_data": [23, 123], |
| 457 |
"res": "Successfully triggered the job for the assign operation."}, |
| 458 |
]) |
| 459 |
def test_assign_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 460 |
ome_response_mock.success = params.get("success", True) |
| 461 |
ome_response_mock.json_data = params["json_data"] |
| 462 |
mocker.patch(MODULE_PATH + 'get_profile', return_value=params.get('prof')) |
| 463 |
mocker.patch(MODULE_PATH + 'get_target_details', return_value=params.get('target')) |
| 464 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 465 |
error_message = params["res"] |
| 466 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 467 |
with pytest.raises(Exception) as err: |
| 468 |
self.module.assign_profile(f_module, ome_connection_mock_for_profile) |
| 469 |
assert err.value.args[0] == error_message |
| 470 |
|
| 471 |
@pytest.mark.parametrize("params", [ |
| 472 |
{"mparams": {"command": "unassign", "name": "profile"}, "success": True, |
| 473 |
"prof": {"Id": 12, "ProfileState": 0}, |
| 474 |
"json_data": 0, "res": "Profile is in an unassigned state."}, |
| 475 |
{"mparams": {"command": "unassign", "name": "profile"}, "success": True, |
| 476 |
"prof": {}, "json_data": 0, |
| 477 |
"res": "Profile with the name 'profile' not found."}, |
| 478 |
{"mparams": {"command": "unassign", "filters": "profile"}, "success": True, |
| 479 |
"prof": {"Id": 12, "ProfileState": 4}, |
| 480 |
"json_data": 0, "res": "Successfully applied the unassign operation. No job was triggered."}, |
| 481 |
{"mparams": {"command": "unassign", "filters": "profile"}, "success": True, |
| 482 |
"json_data": 0, "prof": {"Id": 12, "ProfileState": 1}, |
| 483 |
"res": "Successfully applied the unassign operation. No job was triggered."}, |
| 484 |
{"mparams": {"command": "unassign", "name": "profile"}, "success": True, |
| 485 |
"prof": {"Id": 12, "ProfileState": 4, "DeploymentTaskId": 123}, |
| 486 |
"json_data": {"LastRunStatus": {"Name": "Running"}}, |
| 487 |
"res": "Profile deployment task is in progress. Wait for the job to finish."}, |
| 488 |
{"mparams": {"command": "unassign", "name": "profile"}, "success": True, |
| 489 |
"prof": {"Id": 12, "ProfileState": 4, "DeploymentTaskId": 123}, |
| 490 |
"json_data": {"LastRunStatus": {"Name": "Starting"}}, |
| 491 |
"res": "Successfully triggered the job for the unassign operation."}, |
| 492 |
{"mparams": {"command": "unassign", "name": "profile"}, "success": True, |
| 493 |
"prof": {"Id": 12, "ProfileState": 4, "DeploymentTaskId": 123}, |
| 494 |
"json_data": {"LastRunStatus": {"Name": "Starting"}}, "check_mode": True, |
| 495 |
"res": CHANGES_MSG} |
| 496 |
]) |
| 497 |
def test_unassign_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 498 |
ome_response_mock.success = params.get("success", True) |
| 499 |
ome_response_mock.json_data = params["json_data"] |
| 500 |
mocker.patch(MODULE_PATH + 'get_profile', return_value=params.get('prof')) |
| 501 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 502 |
error_message = params["res"] |
| 503 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 504 |
with pytest.raises(Exception) as err: |
| 505 |
self.module.unassign_profile(f_module, ome_connection_mock_for_profile) |
| 506 |
assert err.value.args[0] == error_message |
| 507 |
|
| 508 |
@pytest.mark.parametrize("params", [ |
| 509 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 510 |
"prof": {"Id": 12, "ProfileState": 4, "TargetId": 14, "DeploymentTaskId": 123}, |
| 511 |
"target": {"Id": 12}, |
| 512 |
"json_data": [1, 2, 3], "res": "Successfully triggered the job for the migrate operation."}, |
| 513 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 514 |
"prof": {}, |
| 515 |
"target": {"Id": 12, "TargetId": 14}, "json_data": 0, |
| 516 |
"res": "Profile with the name 'profile' not found."}, |
| 517 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 518 |
"prof": {"Id": 12, "ProfileState": 0, "TargetId": 14}, |
| 519 |
"target": {"Id": 13, "TargetId": 14}, "json_data": [1, 2, 3], |
| 520 |
"res": "Profile needs to be in a deployed state for a migrate operation."}, |
| 521 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 522 |
"prof": {"Id": 12, "ProfileState": 4, "TargetId": 12}, "target": {"Id": 12}, "json_data": 0, |
| 523 |
"res": "No changes found to be applied."}, |
| 524 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 525 |
"prof": {"Id": 12, "ProfileState": 4, "TargetId": 14, "DeploymentTaskId": 123}, |
| 526 |
"target": "Target invalid.", |
| 527 |
"json_data": [1, 2, 3], "res": "Target invalid."}, |
| 528 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 529 |
"prof": {"Id": 12, "ProfileState": 4, "TargetId": 14, "DeploymentTaskId": 123}, |
| 530 |
"target": {"Id": 12}, |
| 531 |
"json_data": [12, 21, 13], "res": "The target device is invalid for the given profile."}, |
| 532 |
{"mparams": {"command": "migrate", "name": "profile"}, "success": True, |
| 533 |
"prof": {"Id": 12, "ProfileState": 4, "TargetId": 14, "DeploymentTaskId": 123}, |
| 534 |
"target": {"Id": 12}, "check_mode": True, |
| 535 |
"json_data": [1, 2, 3], "res": CHANGES_MSG}, |
| 536 |
]) |
| 537 |
def test_migrate_profile(self, mocker, params, ome_connection_mock_for_profile, ome_response_mock): |
| 538 |
ome_response_mock.success = params.get("success", True) |
| 539 |
ome_response_mock.json_data = params["json_data"] |
| 540 |
mocker.patch(MODULE_PATH + 'get_profile', return_value=params.get('prof')) |
| 541 |
mocker.patch(MODULE_PATH + 'get_target_details', return_value=params.get('target')) |
| 542 |
f_module = self.get_module_mock(params=params["mparams"], check_mode=params.get('check_mode', False)) |
| 543 |
error_message = params["res"] |
| 544 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 545 |
with pytest.raises(Exception) as err: |
| 546 |
self.module.migrate_profile(f_module, ome_connection_mock_for_profile) |
| 547 |
assert err.value.args[0] == error_message |
| 548 |
|
| 549 |
@pytest.mark.parametrize("exc_type", |
| 550 |
[IOError, ValueError, SSLError, TypeError, ConnectionError, HTTPError, URLError]) |
| 551 |
def test_ome_profile_main_exception_failure_case(self, exc_type, mocker, ome_default_args, |
| 552 |
ome_connection_mock_for_profile, ome_response_mock): |
| 553 |
ome_default_args.update({"template_name": "t1"}) |
| 554 |
ome_response_mock.status_code = 400 |
| 555 |
ome_response_mock.success = False |
| 556 |
json_str = to_text(json.dumps({"info": "error_details"})) |
| 557 |
if exc_type == URLError: |
| 558 |
mocker.patch(MODULE_PATH + 'profile_operation', side_effect=exc_type("url open error")) |
| 559 |
result = self._run_module(ome_default_args) |
| 560 |
assert result["unreachable"] is True |
| 561 |
elif exc_type not in [HTTPError, SSLValidationError]: |
| 562 |
mocker.patch(MODULE_PATH + 'profile_operation', side_effect=exc_type("exception message")) |
| 563 |
result = self._run_module(ome_default_args) |
| 564 |
assert result['failed'] is True |
| 565 |
else: |
| 566 |
mocker.patch(MODULE_PATH + 'profile_operation', |
| 567 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 568 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 569 |
result = self._run_module(ome_default_args) |
| 570 |
assert result['failed'] is True |
| 571 |
assert 'msg' in result |
| 572 |
|
| 573 |
@pytest.mark.parametrize("params", [{"mparams": {"filters": {"ProfileIds": [123]}}, |
| 574 |
"res": {"Id": 123, "ProfileName": "Test Profile"}}]) |
| 575 |
def test_ome_profile_get_with_profile_id(self, params, ome_connection_mock_for_profile): |
| 576 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 577 |
ome_connection_mock_for_profile.invoke_request.return_value.json_data = params["res"] |
| 578 |
result = self.module.get_profile(ome_connection_mock_for_profile, f_module) |
| 579 |
assert result == params["res"] |
| 580 |
|
| 581 |
@pytest.mark.parametrize("params", [{"mparams": {"filters": {"Filters": "=contains(ProfileName,'Test Profile01')"}}, |
| 582 |
"res": {"value": [{"Id": 123, "ProfileName": "Test Profile01"}]}}]) |
| 583 |
def test_ome_profile_get_with_profile_name_in_filters(self, params, ome_connection_mock_for_profile): |
| 584 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 585 |
ome_connection_mock_for_profile.invoke_request.return_value.json_data = params["res"] |
| 586 |
result = self.module.get_profile(ome_connection_mock_for_profile, f_module) |
| 587 |
assert result is None |
| 588 |
|
| 589 |
def test_handle_post_assignment_http_error(self, mocker, ome_default_args, ome_connection_mock_for_profile, ome_response_mock): |
| 590 |
ome_default_args.update({"template_name": "t1"}) |
| 591 |
ome_response_mock.status_code = 400 |
| 592 |
|
| 593 |
mocker.patch(MODULE_PATH + 'get_profile', side_effect=HTTPError('https://testhost.com', 400, 'http error message', {}, None)) |
| 594 |
mocker.patch(MODULE_PATH + 'time.sleep', return_value=None) |
| 595 |
self.module.handle_post_assignment(ome_connection_mock_for_profile, ome_response_mock, "AssignProfile") |
| 596 |
result = self._run_module(ome_default_args) |
| 597 |
assert result['failed'] is True |
| 598 |
|
| 599 |
@pytest.mark.parametrize("params", |
| 600 |
[{"mparams": {"device_id": 123}, "success": True, |
| 601 |
"json_data": {"data": "some_response"}}]) |
| 602 |
def test_get_target_details_not_found(self, params, ome_connection_mock_for_profile, mocker): |
| 603 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 604 |
ome_connection_mock_for_profile.invoke_request.return_value = params.get("json_data") |
| 605 |
mocker.patch(MODULE_PATH + "match_profile", return_value=None) |
| 606 |
result = self.module.get_target_details(f_module, ome_connection_mock_for_profile) |
| 607 |
assert result == "Target with Id '123' not found." |
| 608 |
|
| 609 |
@pytest.mark.parametrize("params", [{"mparams": { |
| 610 |
"boot_to_network_iso": { |
| 611 |
"boot_to_network": True, |
| 612 |
"share_type": "CIFS", |
| 613 |
"share_ip": "100.200.300", |
| 614 |
"share_user": "shareuser", |
| 615 |
"share_pwd": "sharepwd", |
| 616 |
"workgroup": "workgroup", |
| 617 |
"iso_path": "pathofiso", |
| 618 |
"iso_timeout": 8 |
| 619 |
}}, |
| 620 |
"res": "ISO path does not have extension '.iso'"}]) |
| 621 |
def test_get_network_iso_payload_without_iso(self, params): |
| 622 |
f_module = self.get_module_mock(params=params["mparams"]) |
| 623 |
with pytest.raises(Exception) as err: |
| 624 |
self.module.get_network_iso_payload(f_module) |
| 625 |
assert err.value.args[0] == params["res"] |