| 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 pytest |
| 16 |
import json |
| 17 |
from ansible_collections.dellemc.openmanage.plugins.modules import omevv_baseline_profile |
| 18 |
from ansible_collections.dellemc.openmanage.plugins.modules.omevv_baseline_profile import ModifyBaselineProfile |
| 19 |
from urllib.error import URLError, HTTPError |
| 20 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 21 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 22 |
from io import StringIO |
| 23 |
from unittest.mock import MagicMock |
| 24 |
from ansible.module_utils._text import to_text |
| 25 |
|
| 26 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.omevv_baseline_profile.' |
| 27 |
MODULE_UTILS_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.omevv_utils.omevv_firmware_utils.' |
| 28 |
INFO_UTILS = "ansible_collections.dellemc.openmanage.plugins.module_utils.omevv_utils.omevv_info_utils." |
| 29 |
UTILS_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.utils.' |
| 30 |
SUCCESS_MSG = "Successfully retrieved the baseline profile information." |
| 31 |
NO_PROFILE_MSG = "Unable to complete the operation because the '{profile_name}' is not a valid 'profile_name'." |
| 32 |
FAILED_MSG = "Unable to fetch the baseline profile information." |
| 33 |
INVOKE_REQ_KEY = "RestOMEVV.invoke_request" |
| 34 |
GET_PROFILE_INFO_KEY = "OMEVVBaselineProfile.get_baseline_profile_by_name" |
| 35 |
PERFORM_OPERATION_KEY = "BaselineProfile.execute" |
| 36 |
PERFORM_CREATE_PROFILE = "OMEVVBaselineProfile.create_baseline_profile" |
| 37 |
PERFORM_MODIFY_PROFILE = "OMEVVBaselineProfile.modify_baseline_profile" |
| 38 |
PERFORM_DELETE_PROFILE = "OMEVVBaselineProfile.delete_baseline_profile" |
| 39 |
GET_PROFILE_BY_ID = "OMEVVBaselineProfile.get_baseline_profile_by_id" |
| 40 |
CREATE_DIFF_MODE_CHECK = "CreateBaselineProfile.diff_mode_check" |
| 41 |
MODIFY_DIFF_MODE_CHECK = "ModifyBaselineProfile.diff_mode_check" |
| 42 |
DELETE_DIFF_MODE_CHECK = "DeleteBaselineProfile.diff_mode_check" |
| 43 |
DELETE_EXECUTE = "DeleteBaselineProfile.execute" |
| 44 |
COMMON = "BaselineProfile.validate_common_params" |
| 45 |
CHANGES_FOUND_MSG = "Changes found to be applied." |
| 46 |
GET_REPO_ID = "OMEVVBaselineProfile.get_repo_id_by_name" |
| 47 |
GET_CLUSTER_ID = "OMEVVBaselineProfile.get_cluster_id" |
| 48 |
GET_GROUP_ID = "OMEVVBaselineProfile.get_group_ids_for_clusters" |
| 49 |
GET_JOB_SCHEDULE = "OMEVVBaselineProfile.create_job_schedule" |
| 50 |
GET_CURRENT_JOB_SCHEDULE = "OMEVVBaselineProfile.get_current_job_schedule" |
| 51 |
ADD_REMOVE_GROUP_ID = "OMEVVBaselineProfile.get_add_remove_group_ids" |
| 52 |
CLUSTER_INFO = "OMEVVInfo.get_cluster_info" |
| 53 |
GET_GROUP_ID_CLUSTER = "OMEVVInfo.get_group_id_of_cluster" |
| 54 |
HTTP_ERROR = "http error message" |
| 55 |
HTTP_ERROR_URL = 'https://testhost.com' |
| 56 |
RETURN_TYPE = "application/json" |
| 57 |
PROFILE_NAME = "Dell Default Catalog" |
| 58 |
PROFILE_TEST = "profile-test" |
| 59 |
CLUSTER_NAME = "My Cluster" |
| 60 |
BASELINE_PROFILE = "Baseline profile for testing" |
| 61 |
DESCRIPTION = "Latest Baseline From Dell" |
| 62 |
JOB_SCHEDULE = {"monday": False, "tuesday": False, "wednesday": False, |
| 63 |
"thursday": True, "friday": True, "saturday": True, "time": "05:30", "sunday": True} |
| 64 |
|
| 65 |
|
| 66 |
class TestBaselineProfile(FakeAnsibleModule): |
| 67 |
module = omevv_baseline_profile |
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def omevv_baseline_profile_mock(self): |
| 71 |
omevv_obj = MagicMock() |
| 72 |
return omevv_obj |
| 73 |
|
| 74 |
@pytest.fixture |
| 75 |
def omevv_connection_baseline_profile(self, mocker, omevv_baseline_profile_mock): |
| 76 |
omevv_conn_mock = mocker.patch( |
| 77 |
MODULE_PATH + 'RestOMEVV', return_value=omevv_baseline_profile_mock) |
| 78 |
omevv_conn_mock.return_value.__enter__.return_value = omevv_baseline_profile_mock |
| 79 |
return omevv_conn_mock |
| 80 |
|
| 81 |
def test_execute(self, omevv_default_args, omevv_connection_baseline_profile): |
| 82 |
obj = MagicMock() |
| 83 |
omevv_obj = self.module.BaselineProfile( |
| 84 |
omevv_connection_baseline_profile, obj) |
| 85 |
omevv_obj.execute() |
| 86 |
|
| 87 |
def test_validate_common_params(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 88 |
obj = MagicMock() |
| 89 |
obj.success = True |
| 90 |
mocker.patch( |
| 91 |
MODULE_PATH + 'validate_job_wait', return_value=True) |
| 92 |
mocker.patch( |
| 93 |
MODULE_PATH + 'validate_time', return_value=None) |
| 94 |
mocker.patch(MODULE_UTILS_PATH + |
| 95 |
'OMEVVBaselineProfile.validate_repository_profile', return_value=(True, "")) |
| 96 |
mocker.patch(MODULE_UTILS_PATH + |
| 97 |
'OMEVVBaselineProfile.validate_cluster_names', return_value=(True, "")) |
| 98 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 99 |
obj = self.module.BaselineProfile( |
| 100 |
omevv_connection_baseline_profile, f_module) |
| 101 |
result = obj.validate_common_params() |
| 102 |
assert result is None |
| 103 |
|
| 104 |
|
| 105 |
mocker.patch(MODULE_UTILS_PATH + |
| 106 |
'OMEVVBaselineProfile.validate_repository_profile', return_value=(False, "")) |
| 107 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 108 |
obj = self.module.BaselineProfile( |
| 109 |
omevv_connection_baseline_profile, f_module) |
| 110 |
result = obj.validate_common_params() |
| 111 |
assert result is None |
| 112 |
|
| 113 |
|
| 114 |
mocker.patch(MODULE_UTILS_PATH + |
| 115 |
'OMEVVBaselineProfile.validate_cluster_names', return_value=(False, "")) |
| 116 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 117 |
obj = self.module.BaselineProfile( |
| 118 |
omevv_connection_baseline_profile, f_module) |
| 119 |
result = obj.validate_common_params() |
| 120 |
assert result is None |
| 121 |
|
| 122 |
|
| 123 |
mocker.patch( |
| 124 |
MODULE_PATH + 'validate_job_wait', return_value=False) |
| 125 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 126 |
obj = self.module.BaselineProfile( |
| 127 |
omevv_connection_baseline_profile, f_module) |
| 128 |
result = obj.validate_common_params() |
| 129 |
assert result is None |
| 130 |
|
| 131 |
|
| 132 |
class TestCreateBaselineProfile(FakeAnsibleModule): |
| 133 |
module = omevv_baseline_profile |
| 134 |
|
| 135 |
@pytest.fixture |
| 136 |
def omevv_baseline_profile_mock(self): |
| 137 |
omevv_obj = MagicMock() |
| 138 |
return omevv_obj |
| 139 |
|
| 140 |
@pytest.fixture |
| 141 |
def omevv_connection_baseline_profile(self, mocker, omevv_baseline_profile_mock): |
| 142 |
omevv_conn_mock = mocker.patch(MODULE_PATH + 'RestOMEVV', |
| 143 |
return_value=omevv_baseline_profile_mock) |
| 144 |
omevv_conn_mock.return_value.__enter__.return_value = omevv_baseline_profile_mock |
| 145 |
return omevv_conn_mock |
| 146 |
|
| 147 |
def test_diff_mode_check(self, omevv_connection_baseline_profile, omevv_default_args): |
| 148 |
|
| 149 |
payload = { |
| 150 |
"name": "baseline_profile_test", |
| 151 |
"firmwareRepoId": "repo1234", |
| 152 |
"groupIds": ["group1", "group2"], |
| 153 |
"jobSchedule": { |
| 154 |
"days": "Monday", |
| 155 |
"time": "12:00" |
| 156 |
} |
| 157 |
} |
| 158 |
f_module = self.get_module_mock( |
| 159 |
params=omevv_default_args) |
| 160 |
obj = self.module.CreateBaselineProfile( |
| 161 |
omevv_connection_baseline_profile, f_module) |
| 162 |
result = obj.diff_mode_check(payload) |
| 163 |
assert result |
| 164 |
|
| 165 |
|
| 166 |
payload = { |
| 167 |
"name": "baseline_profile_test", |
| 168 |
"firmwareRepoId": "repo1234", |
| 169 |
"description": "API", |
| 170 |
"groupIds": ["group1", "group2"], |
| 171 |
"jobSchedule": { |
| 172 |
"days": "Monday", |
| 173 |
"time": "12:00" |
| 174 |
} |
| 175 |
} |
| 176 |
f_module = self.get_module_mock( |
| 177 |
params=omevv_default_args) |
| 178 |
obj = self.module.CreateBaselineProfile( |
| 179 |
omevv_connection_baseline_profile, f_module) |
| 180 |
result = obj.diff_mode_check(payload) |
| 181 |
assert result |
| 182 |
|
| 183 |
def test_perform_create_baseline_profile_success(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 184 |
obj = MagicMock() |
| 185 |
|
| 186 |
obj.success = True |
| 187 |
payload = { |
| 188 |
"name": BASELINE_PROFILE, |
| 189 |
"description": "API", |
| 190 |
"firmwareRepoId": 1000, |
| 191 |
"groupIds": [ |
| 192 |
1012 |
| 193 |
], |
| 194 |
"jobSchedule": JOB_SCHEDULE |
| 195 |
} |
| 196 |
|
| 197 |
obj2 = { |
| 198 |
'id': 1124, |
| 199 |
'name': PROFILE_TEST, |
| 200 |
'description': 'TEST', |
| 201 |
'consoleId': '1234-5678', |
| 202 |
'firmwareRepoId': 1000, |
| 203 |
'firmwareRepoName': PROFILE_NAME, |
| 204 |
"lastmodifiedBy": "OMEVV", |
| 205 |
"version": "1.0.0-0", |
| 206 |
"clusterGroups": [{"clusterID": "domain-c1048", "clusterName": CLUSTER_NAME, "omevv_groupID": 1038}], |
| 207 |
"baselineType": "CLUSTER", |
| 208 |
"status": "SUCCESSFUL"} |
| 209 |
|
| 210 |
obj3 = { |
| 211 |
'id': 1124, |
| 212 |
'name': PROFILE_TEST, |
| 213 |
'description': 'TEST', |
| 214 |
'consoleId': '1234-5678', |
| 215 |
'firmwareRepoId': 1000, |
| 216 |
'firmwareRepoName': PROFILE_NAME, |
| 217 |
"lastmodifiedBy": "OMEVV", |
| 218 |
"clusterGroups": [{"clusterID": "domain-c1048", "clusterName": CLUSTER_NAME, "omevv_groupID": 1038}], |
| 219 |
"baselineType": "CLUSTER", |
| 220 |
"status": "CREATING"} |
| 221 |
|
| 222 |
mocker.patch( |
| 223 |
MODULE_PATH + CREATE_DIFF_MODE_CHECK, return_value={}) |
| 224 |
mocker.patch(MODULE_UTILS_PATH + |
| 225 |
PERFORM_CREATE_PROFILE, return_value=(obj, "")) |
| 226 |
mocker.patch(MODULE_UTILS_PATH + |
| 227 |
GET_PROFILE_BY_ID, side_effect=[obj3, obj2]) |
| 228 |
mocker.patch(MODULE_PATH + |
| 229 |
'time.sleep', return_value=None) |
| 230 |
mocker.patch(MODULE_UTILS_PATH + |
| 231 |
GET_PROFILE_INFO_KEY, return_value=obj2) |
| 232 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 233 |
obj = self.module.CreateBaselineProfile( |
| 234 |
omevv_connection_baseline_profile, f_module) |
| 235 |
result = obj.perform_create_baseline_profile(payload) |
| 236 |
assert result is None |
| 237 |
|
| 238 |
def test_perform_create_baseline_profile_failure(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 239 |
obj = MagicMock() |
| 240 |
obj.success = False |
| 241 |
payload = { |
| 242 |
"name": BASELINE_PROFILE, |
| 243 |
"description": "API", |
| 244 |
"firmwareRepoId": 1000, |
| 245 |
"groupIds": [ |
| 246 |
1012 |
| 247 |
], |
| 248 |
"jobSchedule": JOB_SCHEDULE |
| 249 |
} |
| 250 |
mocker.patch( |
| 251 |
MODULE_PATH + CREATE_DIFF_MODE_CHECK, return_value={}) |
| 252 |
mocker.patch(MODULE_UTILS_PATH + |
| 253 |
PERFORM_CREATE_PROFILE, return_value=(obj, "")) |
| 254 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 255 |
obj = self.module.CreateBaselineProfile( |
| 256 |
omevv_connection_baseline_profile, f_module) |
| 257 |
result = obj.perform_create_baseline_profile(payload) |
| 258 |
assert result is None |
| 259 |
|
| 260 |
def test_perform_create_baseline_profile_api_response_failure(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 261 |
|
| 262 |
payload = { |
| 263 |
"name": BASELINE_PROFILE, |
| 264 |
"description": "API", |
| 265 |
"firmwareRepoId": 1000, |
| 266 |
"groupIds": [ |
| 267 |
1012 |
| 268 |
], |
| 269 |
"jobSchedule": JOB_SCHEDULE |
| 270 |
} |
| 271 |
failed_resp = { |
| 272 |
'id': 1124, |
| 273 |
'name': PROFILE_TEST, |
| 274 |
'description': 'TEST', |
| 275 |
'consoleId': '1234-5678', |
| 276 |
'firmwareRepoId': 1000, |
| 277 |
"status": "FAILED"} |
| 278 |
|
| 279 |
mocker.patch( |
| 280 |
MODULE_PATH + CREATE_DIFF_MODE_CHECK, return_value={}) |
| 281 |
mocker.patch(MODULE_UTILS_PATH + |
| 282 |
GET_PROFILE_BY_ID, return_value=failed_resp) |
| 283 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 284 |
obj = self.module.CreateBaselineProfile( |
| 285 |
omevv_connection_baseline_profile, f_module) |
| 286 |
result = obj.perform_create_baseline_profile(payload) |
| 287 |
assert result is None |
| 288 |
|
| 289 |
def test_execute(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 290 |
ob = {'id': 1124, 'status': 'SUCCESSFUL'} |
| 291 |
job_schedule = JOB_SCHEDULE |
| 292 |
mocker.patch( |
| 293 |
MODULE_PATH + 'BaselineProfile.validate_common_params', return_value=None) |
| 294 |
mocker.patch(MODULE_UTILS_PATH + |
| 295 |
GET_REPO_ID, return_value=1234) |
| 296 |
mocker.patch(MODULE_UTILS_PATH + |
| 297 |
GET_CLUSTER_ID, return_value=[1234, 5678]) |
| 298 |
mocker.patch(MODULE_UTILS_PATH + |
| 299 |
GET_JOB_SCHEDULE, return_value=job_schedule) |
| 300 |
mocker.patch( |
| 301 |
MODULE_PATH + CREATE_DIFF_MODE_CHECK, return_value={}) |
| 302 |
mocker.patch( |
| 303 |
MODULE_PATH + 'CreateBaselineProfile.perform_create_baseline_profile', return_value=ob) |
| 304 |
f_module = self.get_module_mock( |
| 305 |
params=omevv_default_args, check_mode=True) |
| 306 |
obj = self.module.CreateBaselineProfile( |
| 307 |
omevv_connection_baseline_profile, f_module) |
| 308 |
result = obj.execute() |
| 309 |
assert result is None |
| 310 |
|
| 311 |
|
| 312 |
mocker.patch(MODULE_UTILS_PATH + |
| 313 |
GET_JOB_SCHEDULE, return_value=None) |
| 314 |
f_module = self.get_module_mock( |
| 315 |
params=omevv_default_args, check_mode=False) |
| 316 |
obj = self.module.CreateBaselineProfile( |
| 317 |
omevv_connection_baseline_profile, f_module) |
| 318 |
result = obj.execute() |
| 319 |
assert result is None |
| 320 |
|
| 321 |
|
| 322 |
class TestModifyBaselineProfile(FakeAnsibleModule): |
| 323 |
module = omevv_baseline_profile |
| 324 |
|
| 325 |
@pytest.fixture |
| 326 |
def omevv_baseline_profile_mock(self): |
| 327 |
omevv_obj = MagicMock() |
| 328 |
return omevv_obj |
| 329 |
|
| 330 |
@pytest.fixture |
| 331 |
def omevv_connection_baseline_profile(self, mocker, omevv_baseline_profile_mock): |
| 332 |
omevv_conn_mock = mocker.patch( |
| 333 |
MODULE_PATH + 'RestOMEVV', return_value=omevv_baseline_profile_mock) |
| 334 |
omevv_conn_mock.return_value.__enter__.return_value = omevv_baseline_profile_mock |
| 335 |
return omevv_conn_mock |
| 336 |
|
| 337 |
def test_diff_mode_check(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 338 |
|
| 339 |
payload = { |
| 340 |
"name": "baseline_profile_test", |
| 341 |
"firmwareRepoId": "repo1234", |
| 342 |
"groupIds": ["group1"], |
| 343 |
"jobSchedule": { |
| 344 |
"days": "Monday", |
| 345 |
"time": "12:00" |
| 346 |
}, |
| 347 |
"cluster_groups": [ |
| 348 |
{ |
| 349 |
"clusterID": 1234, |
| 350 |
"clusterName": "cluster1", |
| 351 |
"omevv_groupID": 1234 |
| 352 |
} |
| 353 |
], |
| 354 |
"description": "Original description" |
| 355 |
} |
| 356 |
existing_profile = { |
| 357 |
"name": "baseline_profile_test", |
| 358 |
"firmwareRepoId": "repo1234", |
| 359 |
"groupIds": ["group1"], |
| 360 |
"jobSchedule": { |
| 361 |
"days": "Monday", |
| 362 |
"time": "12:00" |
| 363 |
}, |
| 364 |
"clusterGroups": [ |
| 365 |
{ |
| 366 |
"clusterID": 1234, |
| 367 |
"clusterName": "cluster1", |
| 368 |
"omevv_groupID": 1234 |
| 369 |
} |
| 370 |
], |
| 371 |
"description": "Original description" |
| 372 |
} |
| 373 |
schedule = JOB_SCHEDULE |
| 374 |
ob = MagicMock() |
| 375 |
ob.json_data = schedule |
| 376 |
|
| 377 |
cluster_info = {"entityId": 1234} |
| 378 |
omevv_default_args['cluster'] = ["Cluster1"] |
| 379 |
|
| 380 |
mocker.patch(INFO_UTILS + CLUSTER_INFO, return_value=cluster_info) |
| 381 |
mocker.patch(INFO_UTILS + GET_GROUP_ID_CLUSTER, return_value=[1234]) |
| 382 |
mocker.patch(MODULE_UTILS_PATH + |
| 383 |
GET_CURRENT_JOB_SCHEDULE, return_value=ob) |
| 384 |
|
| 385 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 386 |
|
| 387 |
obj = self.module.ModifyBaselineProfile( |
| 388 |
f_module, |
| 389 |
omevv_connection_baseline_profile, |
| 390 |
existing_profile |
| 391 |
) |
| 392 |
|
| 393 |
result = obj.diff_mode_check(payload) |
| 394 |
assert result |
| 395 |
|
| 396 |
def test_perform_modify_baseline_profile_success(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 397 |
obj = MagicMock() |
| 398 |
|
| 399 |
obj.success = True |
| 400 |
payload = { |
| 401 |
"addgroupIds": [1038], |
| 402 |
"removeGroupIds": [1032], |
| 403 |
"jobSchedule": JOB_SCHEDULE, |
| 404 |
"description": "SUCCESS TEST", |
| 405 |
"configurationRepoId": 0, |
| 406 |
"firmwareRepoId": 1000, |
| 407 |
"driverRepoId": 0, |
| 408 |
"modifiedBy": "[email protected]" |
| 409 |
} |
| 410 |
|
| 411 |
existing_profile = { |
| 412 |
'id': 1124, |
| 413 |
'name': PROFILE_TEST, |
| 414 |
'description': 'TEST', |
| 415 |
'consoleId': '1234-5678', |
| 416 |
'firmwareRepoId': 1000, |
| 417 |
'firmwareRepoName': PROFILE_NAME, |
| 418 |
"lastmodifiedBy": "OMEVV", |
| 419 |
"clusterGroups": [{"clusterID": "domain-c1020", "clusterName": CLUSTER_NAME, "omevv_groupID": 1032}], |
| 420 |
"baselineType": "CLUSTER", |
| 421 |
"status": "SUCCESSFUL"} |
| 422 |
|
| 423 |
api_response = { |
| 424 |
'id': 1124, |
| 425 |
'name': PROFILE_TEST, |
| 426 |
'description': 'SUCCESS TEST', |
| 427 |
'consoleId': '1234-5678', |
| 428 |
'firmwareRepoId': 1000, |
| 429 |
'firmwareRepoName': PROFILE_NAME, |
| 430 |
"lastmodifiedBy": "OMEVV", |
| 431 |
"lastSuccessfulUpdatedTime": "2024-11-12T15:26:25.541Z", |
| 432 |
"clusterGroups": [{"clusterID": "domain-c1048", "clusterName": CLUSTER_NAME, "omevv_groupID": 1038}], |
| 433 |
"baselineType": "CLUSTER", |
| 434 |
"status": "MODIFYING"} |
| 435 |
|
| 436 |
mocker.patch( |
| 437 |
MODULE_PATH + MODIFY_DIFF_MODE_CHECK, return_value={}) |
| 438 |
mocker.patch(MODULE_UTILS_PATH + |
| 439 |
PERFORM_MODIFY_PROFILE, return_value=(obj, "")) |
| 440 |
mocker.patch(MODULE_UTILS_PATH + |
| 441 |
GET_PROFILE_BY_ID, side_effect=[api_response, existing_profile]) |
| 442 |
mocker.patch(MODULE_PATH + |
| 443 |
'time.sleep', return_value=None) |
| 444 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 445 |
obj = self.module.ModifyBaselineProfile( |
| 446 |
omevv_connection_baseline_profile, f_module, existing_profile) |
| 447 |
result = obj.perform_modify_baseline_profile(payload, existing_profile) |
| 448 |
assert result is None |
| 449 |
|
| 450 |
def test_perform_modify_baseline_profile_failure(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 451 |
|
| 452 |
payload = { |
| 453 |
"addgroupIds": [1038], |
| 454 |
"removeGroupIds": [1032], |
| 455 |
"jobSchedule": JOB_SCHEDULE, |
| 456 |
"description": "SUCCESS TEST", |
| 457 |
"configurationRepoId": 0, |
| 458 |
"firmwareRepoId": 1000, |
| 459 |
"driverRepoId": 0, |
| 460 |
"modifiedBy": "[email protected]" |
| 461 |
} |
| 462 |
|
| 463 |
existing_profile = { |
| 464 |
'id': 1124, |
| 465 |
'name': PROFILE_TEST, |
| 466 |
'description': 'TEST', |
| 467 |
'consoleId': '1234-5678', |
| 468 |
'firmwareRepoId': 1000, |
| 469 |
'firmwareRepoName': PROFILE_NAME, |
| 470 |
"lastmodifiedBy": "OMEVV", |
| 471 |
"clusterGroups": [{"clusterID": "domain-c1020", "clusterName": CLUSTER_NAME, "omevv_groupID": 1032}], |
| 472 |
"baselineType": "CLUSTER", |
| 473 |
"status": "FAILED"} |
| 474 |
|
| 475 |
obj = MagicMock() |
| 476 |
obj.success = False |
| 477 |
mocker.patch( |
| 478 |
MODULE_PATH + MODIFY_DIFF_MODE_CHECK, return_value={}) |
| 479 |
mocker.patch(MODULE_UTILS_PATH + |
| 480 |
PERFORM_MODIFY_PROFILE, return_value=(obj, "")) |
| 481 |
f_module = self.get_module_mock(params=omevv_default_args) |
| 482 |
obj = self.module.ModifyBaselineProfile( |
| 483 |
omevv_connection_baseline_profile, f_module, existing_profile) |
| 484 |
result = obj.perform_modify_baseline_profile(payload, existing_profile) |
| 485 |
assert result is None |
| 486 |
|
| 487 |
def test_execute(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 488 |
|
| 489 |
ob = {'id': 1124, 'status': 'SUCCESSFUL'} |
| 490 |
job_schedule = JOB_SCHEDULE |
| 491 |
|
| 492 |
existing_profile = { |
| 493 |
'id': 1124, |
| 494 |
'name': PROFILE_TEST, |
| 495 |
'description': 'TEST', |
| 496 |
'consoleId': '1234-5678', |
| 497 |
'firmwareRepoId': 1000, |
| 498 |
'firmwareRepoName': 'Dell Default Catalog', |
| 499 |
"lastmodifiedBy": "OMEVV", |
| 500 |
"version": "1.0.0-0", |
| 501 |
"lastSuccessfulUpdatedTime": "2024-11-12T15:26:25.541Z", |
| 502 |
"clusterGroups": [{"clusterID": "domain-c1020", "clusterName": CLUSTER_NAME, "omevv_groupID": 1032}], |
| 503 |
"datacenter_standAloneHostsGroups": [], |
| 504 |
"baselineType": "CLUSTER", |
| 505 |
"status": "SUCCESSFUL"} |
| 506 |
|
| 507 |
mocker.patch( |
| 508 |
MODULE_PATH + 'ModifyBaselineProfile.perform_modify_baseline_profile', return_value=ob) |
| 509 |
omevv_default_args.update( |
| 510 |
{"vcenter_uuid": "1234-5678", "cluster": "abcd"}) |
| 511 |
f_module = self.get_module_mock( |
| 512 |
params=omevv_default_args, check_mode=False) |
| 513 |
obj = ModifyBaselineProfile( |
| 514 |
omevv_connection_baseline_profile, f_module, existing_profile) |
| 515 |
obj.validate_common_params = MagicMock(return_value=None) |
| 516 |
obj.omevv_baseline_obj.get_add_remove_group_ids = MagicMock( |
| 517 |
return_value=(None, None)) |
| 518 |
omevv_default_args.update( |
| 519 |
{"vcenter_uuid": "1234-5678", "cluster": "abcd"}) |
| 520 |
mocker.patch(MODULE_UTILS_PATH + |
| 521 |
GET_JOB_SCHEDULE, return_value=job_schedule) |
| 522 |
obj.omevv_baseline_obj.get_repo_id_by_name = MagicMock( |
| 523 |
return_value=1234) |
| 524 |
obj.omevv_baseline_obj.create_job_schedule = MagicMock( |
| 525 |
return_value=job_schedule) |
| 526 |
obj.diff_mode_check = MagicMock(return_value={ |
| 527 |
"before": {"description": "old_description", "jobSchedule": "old_schedule"}, |
| 528 |
"after": {"description": "new_description", "jobSchedule": "new_schedule"} |
| 529 |
}) |
| 530 |
result = obj.execute() |
| 531 |
assert result is None |
| 532 |
|
| 533 |
|
| 534 |
obj.diff_mode_check = MagicMock(return_value={ |
| 535 |
"before": {}, |
| 536 |
"after": {} |
| 537 |
}) |
| 538 |
result = obj.execute() |
| 539 |
assert result is None |
| 540 |
|
| 541 |
|
| 542 |
class TestDeleteBaselineProfile(FakeAnsibleModule): |
| 543 |
module = omevv_baseline_profile |
| 544 |
|
| 545 |
@pytest.fixture |
| 546 |
def omevv_baseline_profile_mock(self): |
| 547 |
omevv_obj = MagicMock() |
| 548 |
return omevv_obj |
| 549 |
|
| 550 |
@pytest.fixture |
| 551 |
def omevv_connection_baseline_profile(self, mocker, omevv_baseline_profile_mock): |
| 552 |
omevv_conn_mock = mocker.patch(MODULE_PATH + 'RestOMEVV', |
| 553 |
return_value=omevv_baseline_profile_mock) |
| 554 |
omevv_conn_mock.return_value.__enter__.return_value = omevv_baseline_profile_mock |
| 555 |
return omevv_conn_mock |
| 556 |
|
| 557 |
def test_diff_mode_check(self, omevv_connection_baseline_profile, omevv_default_args): |
| 558 |
|
| 559 |
payload = { |
| 560 |
"name": "TestProfile", |
| 561 |
"description": BASELINE_PROFILE, |
| 562 |
"firmwareRepoId": 1001, |
| 563 |
"firmwareRepoName": "TestRepo", |
| 564 |
"clusterGroups": [1, 2] |
| 565 |
} |
| 566 |
f_module = self.get_module_mock( |
| 567 |
params=omevv_default_args) |
| 568 |
obj = self.module.DeleteBaselineProfile( |
| 569 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 570 |
result = obj.diff_mode_check(payload) |
| 571 |
assert result |
| 572 |
|
| 573 |
def test_perform_delete_baseline_profile(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 574 |
profile_resp = { |
| 575 |
"id": 1996, |
| 576 |
"name": "TestProfile", |
| 577 |
"description": BASELINE_PROFILE, |
| 578 |
"firmwareRepoId": 1001, |
| 579 |
"firmwareRepoName": "TestRepo", |
| 580 |
"clusterGroups": [1, 2] |
| 581 |
} |
| 582 |
|
| 583 |
obj = MagicMock() |
| 584 |
obj.success = True |
| 585 |
mocker.patch(MODULE_PATH + DELETE_DIFF_MODE_CHECK, return_value={}) |
| 586 |
mocker.patch(MODULE_UTILS_PATH + |
| 587 |
PERFORM_DELETE_PROFILE, return_value=obj) |
| 588 |
f_module = self.get_module_mock( |
| 589 |
params=omevv_default_args) |
| 590 |
obj = self.module.DeleteBaselineProfile( |
| 591 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 592 |
result = obj.perform_delete_baseline_profile(profile_resp) |
| 593 |
assert result is None |
| 594 |
|
| 595 |
|
| 596 |
obj.success = False |
| 597 |
mocker.patch(MODULE_UTILS_PATH + |
| 598 |
PERFORM_DELETE_PROFILE, return_value=obj) |
| 599 |
f_module = self.get_module_mock( |
| 600 |
params=omevv_default_args) |
| 601 |
obj = self.module.DeleteBaselineProfile( |
| 602 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 603 |
result = obj.perform_delete_baseline_profile(profile_resp) |
| 604 |
assert result is None |
| 605 |
|
| 606 |
def test_execute(self, omevv_connection_baseline_profile, omevv_default_args, mocker): |
| 607 |
|
| 608 |
obj = MagicMock() |
| 609 |
mocker.patch(MODULE_UTILS_PATH + |
| 610 |
GET_PROFILE_INFO_KEY, return_value={}) |
| 611 |
mocker.patch( |
| 612 |
MODULE_PATH + DELETE_DIFF_MODE_CHECK, return_value={}) |
| 613 |
mocker.patch(MODULE_UTILS_PATH + |
| 614 |
PERFORM_DELETE_PROFILE, return_value=obj) |
| 615 |
f_module = self.get_module_mock( |
| 616 |
params=omevv_default_args) |
| 617 |
obj = self.module.DeleteBaselineProfile( |
| 618 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 619 |
result = obj.execute() |
| 620 |
assert result is None |
| 621 |
|
| 622 |
|
| 623 |
f_module = self.get_module_mock( |
| 624 |
params=omevv_default_args, check_mode=True) |
| 625 |
obj = self.module.DeleteBaselineProfile( |
| 626 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 627 |
result = obj.execute() |
| 628 |
assert result is None |
| 629 |
|
| 630 |
|
| 631 |
obj = MagicMock() |
| 632 |
obj.success = True |
| 633 |
res = { |
| 634 |
"id": 1996, |
| 635 |
"name": "TestProfile", |
| 636 |
"description": BASELINE_PROFILE, |
| 637 |
"firmwareRepoId": 1001, |
| 638 |
"firmwareRepoName": "TestRepo", |
| 639 |
"clusterGroups": [1, 2] |
| 640 |
} |
| 641 |
mocker.patch(MODULE_UTILS_PATH + |
| 642 |
GET_PROFILE_INFO_KEY, return_value=res) |
| 643 |
mocker.patch( |
| 644 |
MODULE_PATH + DELETE_DIFF_MODE_CHECK, return_value={}) |
| 645 |
mocker.patch(MODULE_UTILS_PATH + |
| 646 |
PERFORM_DELETE_PROFILE, return_value=obj) |
| 647 |
f_module = self.get_module_mock( |
| 648 |
params=omevv_default_args, check_mode=True) |
| 649 |
obj = self.module.DeleteBaselineProfile( |
| 650 |
omevv_connection_baseline_profile, f_module, profile_name="TestProfile") |
| 651 |
result = obj.execute() |
| 652 |
assert result is None |
| 653 |
|
| 654 |
@pytest.mark.parametrize("exc_type", |
| 655 |
[URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError]) |
| 656 |
def test_omevv_baseline_profile_main_exception_handling_case(self, exc_type, mocker, omevv_default_args, |
| 657 |
omevv_baseline_profile_mock): |
| 658 |
omevv_baseline_profile_mock.status_code = 400 |
| 659 |
omevv_baseline_profile_mock.success = False |
| 660 |
json_str = to_text(json.dumps( |
| 661 |
{"errorCode": "501", "message": "Error"})) |
| 662 |
omevv_default_args.update({'state': 'absent', 'name': 'test', 'validate_certs': False}) |
| 663 |
if exc_type in [HTTPError, SSLValidationError]: |
| 664 |
mocker.patch(MODULE_PATH + DELETE_EXECUTE, |
| 665 |
side_effect=exc_type(HTTP_ERROR_URL, 400, |
| 666 |
HTTP_ERROR, |
| 667 |
{"accept-type": RETURN_TYPE}, |
| 668 |
StringIO(json_str))) |
| 669 |
else: |
| 670 |
mocker.patch(MODULE_PATH + DELETE_EXECUTE, |
| 671 |
side_effect=exc_type('test')) |
| 672 |
result = self._run_module(omevv_default_args) |
| 673 |
if exc_type == URLError: |
| 674 |
assert result['changed'] is False |
| 675 |
else: |
| 676 |
assert result['failed'] is True |
| 677 |
assert 'msg' in result |
| 678 |
|
| 679 |
|
| 680 |
omevv_baseline_profile_mock.status_code = 404 |
| 681 |
omevv_default_args.update({'state': 'absent', 'name': 'test', 'validate_certs': False}) |
| 682 |
error_string = to_text(json.dumps( |
| 683 |
{'errorCode': '404', 'message': "The Requested resource cannot be found."})) |
| 684 |
if exc_type in [HTTPError]: |
| 685 |
mocker.patch(MODULE_PATH + DELETE_EXECUTE, |
| 686 |
side_effect=exc_type(HTTP_ERROR_URL, 404, |
| 687 |
HTTP_ERROR, |
| 688 |
{"accept-type": RETURN_TYPE}, |
| 689 |
StringIO(error_string))) |
| 690 |
res_out = self._run_module(omevv_default_args) |
| 691 |
assert 'msg' in res_out |
| 692 |
|
| 693 |
|
| 694 |
error_string = to_text(json.dumps( |
| 695 |
{'errorCode': '500', 'message': "Error"})) |
| 696 |
if exc_type in [HTTPError]: |
| 697 |
mocker.patch(MODULE_PATH + DELETE_EXECUTE, |
| 698 |
side_effect=exc_type(HTTP_ERROR_URL, 500, |
| 699 |
HTTP_ERROR, |
| 700 |
{"accept-type": RETURN_TYPE}, |
| 701 |
StringIO(error_string))) |
| 702 |
res_out = self._run_module(omevv_default_args) |
| 703 |
assert 'msg' in res_out |
| 704 |
|
| 705 |
|
| 706 |
params = { |
| 707 |
'state': 'present', |
| 708 |
'name': PROFILE_TEST, |
| 709 |
"repository_profile": "repository-profile", |
| 710 |
"cluster": ["cluster-1", "cluster-2"], |
| 711 |
"days": ["sunday", "wednesday"], |
| 712 |
"time": "22:10"} |
| 713 |
omevv_default_args.update(params) |
| 714 |
mocker.patch(MODULE_UTILS_PATH + GET_PROFILE_INFO_KEY, return_value={}) |
| 715 |
res_out = self._run_module(omevv_default_args) |
| 716 |
assert 'msg' in res_out |