| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
|
| 13 |
|
| 14 |
from io import StringIO |
| 15 |
import json |
| 16 |
|
| 17 |
|
| 18 |
from urllib.error import HTTPError, URLError |
| 19 |
import pytest |
| 20 |
from unittest.mock import MagicMock |
| 21 |
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_session |
| 22 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 23 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import AnsibleFailJSonException |
| 24 |
from ansible.module_utils.urls import SSLValidationError |
| 25 |
from ansible.module_utils._text import to_text |
| 26 |
|
| 27 |
|
| 28 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.idrac_session.' |
| 29 |
MODULE_UTILS_PATH = 'ansible_collections.dellemc.openmanage.plugins.module_utils.utils.' |
| 30 |
|
| 31 |
REDFISH = "/redfish/v1" |
| 32 |
SESSIONS = "Sessions" |
| 33 |
ODATA = "@odata.id" |
| 34 |
ODATA_REGEX = "(.*?)@odata" |
| 35 |
|
| 36 |
SESSION_URL = "/redfish/v1/SessionService/Sessions" |
| 37 |
GET_SESSION_URL = "Session.get_session_url" |
| 38 |
|
| 39 |
CREATE_SUCCESS_MSG = "The session has been created successfully." |
| 40 |
DELETE_SUCCESS_MSG = "The session has been deleted successfully." |
| 41 |
FAILURE_MSG = "Unable to '{operation}' a session." |
| 42 |
CHANGES_FOUND_MSG = "Changes found to be applied." |
| 43 |
NO_CHANGES_FOUND_MSG = "No changes found to be applied." |
| 44 |
HTTPS_PATH = "https://testhost.com" |
| 45 |
HTTP_ERROR = "http error message" |
| 46 |
APPLICATION_JSON = "application/json" |
| 47 |
|
| 48 |
|
| 49 |
class TestSession(FakeAnsibleModule): |
| 50 |
""" |
| 51 |
Main class for testing the idrac_session module. |
| 52 |
""" |
| 53 |
module = idrac_session |
| 54 |
|
| 55 |
@pytest.fixture |
| 56 |
def idrac_session_mock(self): |
| 57 |
""" |
| 58 |
Creates a mock object for the `idrac_session` fixture. |
| 59 |
|
| 60 |
This function uses the `MagicMock` class from the `unittest.mock` module to create a mock |
| 61 |
object. The mock object is then returned by the function. |
| 62 |
|
| 63 |
Returns: |
| 64 |
MagicMock: A mock object representing the `idrac_session`. |
| 65 |
""" |
| 66 |
idrac_obj = MagicMock() |
| 67 |
return idrac_obj |
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def idrac_connection_session_mock(self, mocker, idrac_session_mock): |
| 71 |
""" |
| 72 |
Returns a mock object for the `SessionAPI` class from the `MODULE_PATH` module. |
| 73 |
The mock object is initialized with the `idrac_session_mock` as the return value. |
| 74 |
The `__enter__` method of the mock object is also mocked to return `idrac_session_mock`. |
| 75 |
|
| 76 |
:param mocker: The pytest fixture for mocking objects. |
| 77 |
:type mocker: pytest_mock.plugin.MockerFixture |
| 78 |
:param idrac_session_mock: The mock object for the `idrac_session_mock`. |
| 79 |
:type idrac_session_mock: Any |
| 80 |
:return: The mock object for the `SessionAPI` class. |
| 81 |
:rtype: MagicMock |
| 82 |
""" |
| 83 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'SessionAPI', |
| 84 |
return_value=idrac_session_mock) |
| 85 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_session_mock |
| 86 |
return idrac_conn_mock |
| 87 |
|
| 88 |
def test_get_session_url(self, idrac_default_args, idrac_connection_session_mock, mocker): |
| 89 |
""" |
| 90 |
Test the `get_session_url` method of the `Session` class. |
| 91 |
|
| 92 |
This test function mocks the `get_dynamic_uri` function to return a dictionary |
| 93 |
containing the session URL. It then creates a `f_module` object with the |
| 94 |
`idrac_default_args` and `check_mode` set to `False`. It initializes a |
| 95 |
`session_obj` with the `idrac_connection_session_mock` and `f_module`. |
| 96 |
Finally, it calls the `get_session_url` method on the `session_obj` and |
| 97 |
asserts that the returned session URL is equal to the `SESSION_URL` constant. |
| 98 |
|
| 99 |
Args: |
| 100 |
self (TestGetSessionUrl): The test case object. |
| 101 |
idrac_default_args (dict): The default arguments for the IDRAC connection. |
| 102 |
idrac_connection_session_mock (MagicMock): The mock object for the IDRAC |
| 103 |
connection session. |
| 104 |
mocker (MagicMock): The mocker object for mocking functions and modules. |
| 105 |
|
| 106 |
Returns: |
| 107 |
None |
| 108 |
""" |
| 109 |
v1_resp = {'Links': {'Sessions': {'@odata.id': SESSION_URL}}} |
| 110 |
mocker.patch(MODULE_PATH + "get_dynamic_uri", |
| 111 |
return_value=v1_resp) |
| 112 |
f_module = self.get_module_mock( |
| 113 |
params=idrac_default_args, check_mode=False) |
| 114 |
session_obj = self.module.Session( |
| 115 |
idrac_connection_session_mock, f_module) |
| 116 |
sessions_url = session_obj.get_session_url() |
| 117 |
assert sessions_url == SESSION_URL |
| 118 |
|
| 119 |
|
| 120 |
class TestCreateSession(FakeAnsibleModule): |
| 121 |
""" |
| 122 |
Main class for testing the create_session module. |
| 123 |
""" |
| 124 |
module = idrac_session |
| 125 |
|
| 126 |
@pytest.fixture |
| 127 |
def create_session_mock(self): |
| 128 |
""" |
| 129 |
Creates a mock object for the `idrac_session` fixture. |
| 130 |
|
| 131 |
This function is a pytest fixture that creates a mock object of type `MagicMock` and |
| 132 |
assigns it to the variable `idrac_obj`. The `idrac_obj` mock object is then returned |
| 133 |
by the fixture. |
| 134 |
|
| 135 |
Returns: |
| 136 |
MagicMock: A mock object representing the `idrac_session` fixture. |
| 137 |
""" |
| 138 |
idrac_obj = MagicMock() |
| 139 |
return idrac_obj |
| 140 |
|
| 141 |
@pytest.fixture |
| 142 |
def idrac_connection_session_mock(self, mocker, create_session_mock): |
| 143 |
""" |
| 144 |
Creates a fixture for mocking the IDRAC connection session. |
| 145 |
|
| 146 |
This fixture uses the `mocker` fixture from the `pytest` library to patch the |
| 147 |
`SessionAPI` class from the `MODULE_PATH` module. It returns a mock object of the |
| 148 |
`SessionAPI` class with the `create_session_mock` object as the return value. |
| 149 |
The `__enter__` method of the mock object is also patched to return the |
| 150 |
`create_session_mock` object. |
| 151 |
|
| 152 |
Parameters: |
| 153 |
- `self` (TestCase): The test case instance. |
| 154 |
- `mocker` (MockerFixture): The `mocker` fixture from the `pytest` library. |
| 155 |
- `create_session_mock` (Mock): The mock object representing the `create_session_mock`. |
| 156 |
|
| 157 |
Returns: |
| 158 |
- `idrac_conn_mock` (MagicMock): The mock object of the `SessionAPI` class. |
| 159 |
""" |
| 160 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'SessionAPI', |
| 161 |
return_value=create_session_mock) |
| 162 |
idrac_conn_mock.return_value.__enter__.return_value = create_session_mock |
| 163 |
return idrac_conn_mock |
| 164 |
|
| 165 |
def test_session_operation(self, idrac_default_args, idrac_connection_session_mock): |
| 166 |
""" |
| 167 |
Test the session operation of the module. |
| 168 |
|
| 169 |
Args: |
| 170 |
idrac_default_args (dict): The default arguments for the IDRAC connection. |
| 171 |
idrac_connection_session_mock (MagicMock): The mock object for the IDRAC |
| 172 |
connection session. |
| 173 |
|
| 174 |
Returns: |
| 175 |
None |
| 176 |
|
| 177 |
This function tests the session operation of the module by creating a session and deleting |
| 178 |
a session. |
| 179 |
It updates the `idrac_default_args` dictionary with the appropriate state parameter and |
| 180 |
creates a `f_module` object with the updated arguments. It then creates a |
| 181 |
`session_operation_obj` object using the `CreateSession` class of the module and asserts |
| 182 |
that it is an instance of `CreateSession`. |
| 183 |
It repeats the same process for deleting a session by updating the `idrac_default_args` |
| 184 |
dictionary with the state parameter set to "absent" and creating a `session_operation_obj` |
| 185 |
object using the |
| 186 |
`DeleteSession` class of the module. It asserts that it is an instance of `DeleteSession`. |
| 187 |
""" |
| 188 |
idrac_default_args.update({"state": "present"}) |
| 189 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 190 |
session_operation_obj = self.module.CreateSession(idrac_connection_session_mock, f_module) |
| 191 |
assert isinstance(session_operation_obj, self.module.CreateSession) |
| 192 |
|
| 193 |
idrac_default_args.update({"state": "absent"}) |
| 194 |
f_module = self.get_module_mock(params=idrac_default_args, check_mode=False) |
| 195 |
session_operation_obj = self.module.DeleteSession(idrac_connection_session_mock, f_module) |
| 196 |
assert isinstance(session_operation_obj, self.module.DeleteSession) |
| 197 |
|
| 198 |
def test_create_session_failure(self, idrac_connection_session_mock, mocker): |
| 199 |
""" |
| 200 |
Test the failure scenario of creating a session. |
| 201 |
|
| 202 |
Args: |
| 203 |
idrac_connection_session_mock (MagicMock): A mock object for the |
| 204 |
idrac_connection_session. |
| 205 |
mocker (MockerFixture): A fixture for mocking objects. |
| 206 |
|
| 207 |
Returns: |
| 208 |
None |
| 209 |
|
| 210 |
This test function creates a session object using the `idrac_connection_session_mock` and |
| 211 |
`f_module` objects. |
| 212 |
It sets the `session_obj.get_session_url` to return a session URL. |
| 213 |
It sets the `f_module.check_mode` to False and `f_module.params` to a dictionary containing |
| 214 |
the username and password. |
| 215 |
It mocks the `idrac_connection_session_mock.invoke_request` method to return a response |
| 216 |
with a status code of 201. |
| 217 |
It calls the `session_obj.execute()` method to create the session. |
| 218 |
It asserts that the `f_module.exit_json` method is called once with the message "Unable to |
| 219 |
'create' a session." and `failed` set to True. |
| 220 |
""" |
| 221 |
f_module = MagicMock() |
| 222 |
session_obj = idrac_session.CreateSession( |
| 223 |
idrac_connection_session_mock, f_module) |
| 224 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 225 |
f_module.check_mode = False |
| 226 |
f_module.params = { |
| 227 |
"username": "admin", |
| 228 |
"password": "password" |
| 229 |
} |
| 230 |
response_mock = MagicMock() |
| 231 |
response_mock.status_code = 201 |
| 232 |
mocker.patch.object(idrac_connection_session_mock.return_value, 'invoke_request', |
| 233 |
return_value=response_mock) |
| 234 |
|
| 235 |
session_obj.execute() |
| 236 |
f_module.exit_json.assert_called_once_with( |
| 237 |
msg="Unable to 'create' a session.", |
| 238 |
failed=True |
| 239 |
) |
| 240 |
|
| 241 |
def test_create_session_check_mode(self, idrac_connection_session_mock): |
| 242 |
""" |
| 243 |
Test the create session functionality in check mode. |
| 244 |
|
| 245 |
Args: |
| 246 |
idrac_connection_session_mock (MagicMock): A mock object for the IDRAC connection |
| 247 |
session. |
| 248 |
|
| 249 |
Returns: |
| 250 |
None |
| 251 |
|
| 252 |
This function tests the create session functionality in check mode. It creates an instance |
| 253 |
of the `CreateSession` class with the provided `idrac_connection_session_mock` and a mock |
| 254 |
`f_module` object. |
| 255 |
It sets the required parameters for the `f_module` object and mocks the `get_session_url` |
| 256 |
method of the `session_obj` to return the session URL. It also mocks the `exit_json` method |
| 257 |
of the `f_module` object. |
| 258 |
|
| 259 |
Finally, it calls the `execute` method of the `session_obj` to execute the create session |
| 260 |
functionality in check mode. |
| 261 |
|
| 262 |
Note: |
| 263 |
This function assumes that the necessary imports and setup for the test are already |
| 264 |
done. |
| 265 |
""" |
| 266 |
f_module = MagicMock() |
| 267 |
session_obj = idrac_session.CreateSession( |
| 268 |
idrac_connection_session_mock, f_module) |
| 269 |
f_module = self.get_module_mock( |
| 270 |
params={"session_id": "1234", "hostname": "X.X.X.X"}, check_mode=True) |
| 271 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 272 |
f_module.exit_json = MagicMock() |
| 273 |
|
| 274 |
session_obj.execute() |
| 275 |
|
| 276 |
def test_create_session_success(self, idrac_connection_session_mock): |
| 277 |
""" |
| 278 |
Test the successful creation of a session. |
| 279 |
|
| 280 |
Args: |
| 281 |
idrac_connection_session_mock (MagicMock): A mock object representing the IDRAC |
| 282 |
connection session. |
| 283 |
|
| 284 |
This test case verifies the successful creation of a session by mocking the necessary |
| 285 |
objects and invoking the `execute()` method of the `CreateSession` class. It sets the |
| 286 |
parameters for the `f_module` object, initializes the `session_obj` with the mocked |
| 287 |
`idrac_connection_session_mock` and `f_module`, and mocks the necessary methods and |
| 288 |
attributes of the `idrac` object. It then asserts that the `exit_json` method of the |
| 289 |
`f_module` object is called with the expected arguments. |
| 290 |
|
| 291 |
Returns: |
| 292 |
None |
| 293 |
""" |
| 294 |
f_module = self.get_module_mock( |
| 295 |
params={"username": "admin", "password": "password"}, check_mode=False) |
| 296 |
session_obj = idrac_session.CreateSession(idrac_connection_session_mock, f_module) |
| 297 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 298 |
session_obj.idrac.invoke_request.return_value.status_code = 201 |
| 299 |
session_obj.idrac.invoke_request.return_value.json_data = {"SessionID": "123456"} |
| 300 |
session_obj.idrac.invoke_request.return_value.headers.get.return_value = "token123" |
| 301 |
f_module.exit_json = MagicMock() |
| 302 |
|
| 303 |
session_obj.execute() |
| 304 |
f_module.exit_json.assert_called_once_with( |
| 305 |
msg=CREATE_SUCCESS_MSG, |
| 306 |
changed=True, |
| 307 |
session_data={"SessionID": "123456"}, |
| 308 |
x_auth_token="token123" |
| 309 |
) |
| 310 |
|
| 311 |
|
| 312 |
class TestDeleteSession(FakeAnsibleModule): |
| 313 |
""" |
| 314 |
Main class for testing the delete session module. |
| 315 |
""" |
| 316 |
module = idrac_session |
| 317 |
|
| 318 |
@pytest.fixture |
| 319 |
def idrac_session_mock(self): |
| 320 |
""" |
| 321 |
Creates a mock object for the `idrac_session` fixture. |
| 322 |
|
| 323 |
This function uses the `MagicMock` class from the `unittest.mock` module to create a mock |
| 324 |
object. |
| 325 |
The mock object is then returned by the function. |
| 326 |
|
| 327 |
Returns: |
| 328 |
MagicMock: A mock object representing the `idrac_session`. |
| 329 |
""" |
| 330 |
idrac_obj = MagicMock() |
| 331 |
return idrac_obj |
| 332 |
|
| 333 |
@pytest.fixture |
| 334 |
def idrac_connection_session_mock(self, mocker, idrac_session_mock): |
| 335 |
""" |
| 336 |
Returns a mocked instance of the SessionAPI class from the specified module path. |
| 337 |
The mocked instance is created using the `mocker.patch` function. The `idrac_session_mock` |
| 338 |
parameter is passed as the return value of the mocked instance. The `__enter__` method |
| 339 |
of the mocked instance is also mocked to return the `idrac_session_mock`. |
| 340 |
:param mocker: The mocker fixture provided by the pytest framework. |
| 341 |
:type mocker: _pytest.monkeypatch.MonkeyPatch |
| 342 |
:param idrac_session_mock: The mocked instance of the idrac session. |
| 343 |
:type idrac_session_mock: Any |
| 344 |
:return: The mocked instance of the SessionAPI class. |
| 345 |
:rtype: MagicMock |
| 346 |
""" |
| 347 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'SessionAPI', |
| 348 |
return_value=idrac_session_mock) |
| 349 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_session_mock |
| 350 |
return idrac_conn_mock |
| 351 |
|
| 352 |
def test_delete_session_success_check_mode_changes(self, idrac_connection_session_mock): |
| 353 |
""" |
| 354 |
Test the `delete_session_success_check_mode_changes` method of the `DeleteSession` class. |
| 355 |
|
| 356 |
This method is responsible for testing the success case when the `delete_session` method |
| 357 |
is called in check mode. |
| 358 |
It verifies that the `exit_json` method of the `f_module` object is called with the |
| 359 |
appropriate arguments when the session is successfully deleted. |
| 360 |
|
| 361 |
Parameters: |
| 362 |
- idrac_connection_session_mock (MagicMock): A mock object representing the |
| 363 |
`idrac_connection_session` object. |
| 364 |
|
| 365 |
Returns: |
| 366 |
None |
| 367 |
""" |
| 368 |
f_module = MagicMock() |
| 369 |
delete_session_obj = idrac_session.DeleteSession(idrac_connection_session_mock, f_module) |
| 370 |
delete_session_obj.idrac.invoke_request.return_value.status_code = 200 |
| 371 |
delete_session_obj.execute() |
| 372 |
f_module.exit_json.assert_called_once_with(msg=CHANGES_FOUND_MSG, changed=True) |
| 373 |
|
| 374 |
def test_delete_session_success_check_mode_no_changes(self, idrac_connection_session_mock): |
| 375 |
""" |
| 376 |
Test the success case of deleting a session in check mode when no changes are expected. |
| 377 |
|
| 378 |
Args: |
| 379 |
idrac_connection_session_mock (MagicMock): A mock object representing the IDRAC |
| 380 |
connection session. |
| 381 |
|
| 382 |
This function tests the scenario where the deletion of a session is successful in check |
| 383 |
mode and no changes are expected. It sets up the necessary mock objects and asserts that |
| 384 |
the `exit_json` method of the `f_module` object is called once with the `msg` parameter |
| 385 |
set to `NO_CHANGES_FOUND_MSG`. |
| 386 |
|
| 387 |
Returns: |
| 388 |
None |
| 389 |
""" |
| 390 |
f_module = MagicMock() |
| 391 |
delete_session_obj = idrac_session.DeleteSession(idrac_connection_session_mock, f_module) |
| 392 |
delete_session_obj.idrac.invoke_request.return_value.status_code = 201 |
| 393 |
delete_session_obj.execute() |
| 394 |
f_module.exit_json.assert_called_once_with(msg=NO_CHANGES_FOUND_MSG) |
| 395 |
|
| 396 |
def test_delete_session_success(self, idrac_connection_session_mock): |
| 397 |
""" |
| 398 |
Test the successful deletion of a session. |
| 399 |
|
| 400 |
This test function verifies the behavior of the `DeleteSession` class when a session is |
| 401 |
successfully deleted. It mocks the `idrac_connection_session_mock` object and sets up the |
| 402 |
necessary parameters for the `f_module` object. It then creates an instance of the |
| 403 |
`DeleteSession` class with the mocked `idrac_connection_session_mock` and the |
| 404 |
`f_module` object. |
| 405 |
|
| 406 |
The `get_session_url` method of the `session_obj` is mocked to return a specific session |
| 407 |
URL. The `invoke_request` method of the `idrac` object of the `session_obj` is also mocked |
| 408 |
to return a response with a status code of 200. The `exit_json` method of the `f_module` |
| 409 |
object is mocked as well. |
| 410 |
|
| 411 |
The `execute` method of the `session_obj` is called to execute the deletion of the session. |
| 412 |
Finally, the `exit_json` method of the `f_module` object is asserted to have been called |
| 413 |
with the expected arguments, including the success message and the changed flag set to |
| 414 |
`True`. |
| 415 |
|
| 416 |
Parameters: |
| 417 |
- idrac_connection_session_mock (MagicMock): A mocked object representing the |
| 418 |
`idrac_connection_session_mock` object. |
| 419 |
|
| 420 |
Returns: |
| 421 |
None |
| 422 |
""" |
| 423 |
f_module = self.get_module_mock( |
| 424 |
params={"session_id": "1234", "hostname": "X.X.X.X"}, check_mode=False) |
| 425 |
session_obj = idrac_session.DeleteSession(idrac_connection_session_mock, f_module) |
| 426 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 427 |
session_obj.idrac.invoke_request.return_value.status_code = 200 |
| 428 |
f_module.exit_json = MagicMock() |
| 429 |
session_obj.execute() |
| 430 |
f_module.exit_json.assert_called_once_with(msg=DELETE_SUCCESS_MSG, changed=True) |
| 431 |
|
| 432 |
def test_delete_session_check_mode_false_no_changes(self, idrac_connection_session_mock): |
| 433 |
""" |
| 434 |
Test the scenario where the delete session is executed in check mode with `check_mode` set |
| 435 |
to False and no changes are expected. |
| 436 |
|
| 437 |
Args: |
| 438 |
idrac_connection_session_mock (MagicMock): A mock object representing the IDRAC |
| 439 |
connection session. |
| 440 |
|
| 441 |
Returns: |
| 442 |
None |
| 443 |
|
| 444 |
This function creates a mock module object with the specified parameters and |
| 445 |
initializes the `DeleteSession` object with the mock IDRAC connection and module. It sets |
| 446 |
the `get_session_url` method of the session object to return a specific session URL. It |
| 447 |
sets the status code of the invoke request to 201. It then asserts that the `exit_json` |
| 448 |
method of the module object is called once with the `msg` parameter set to the |
| 449 |
`NO_CHANGES_FOUND_MSG` constant. |
| 450 |
""" |
| 451 |
f_module = self.get_module_mock( |
| 452 |
params={"session_id": "1234", "hostname": "X.X.X.X"}, check_mode=False) |
| 453 |
session_obj = idrac_session.DeleteSession(idrac_connection_session_mock, f_module) |
| 454 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 455 |
session_obj.idrac.invoke_request.return_value.status_code = 201 |
| 456 |
f_module.exit_json = MagicMock() |
| 457 |
session_obj.execute() |
| 458 |
f_module.exit_json.assert_called_once_with(msg=NO_CHANGES_FOUND_MSG) |
| 459 |
|
| 460 |
def test_delete_session_http_error(self, idrac_connection_session_mock): |
| 461 |
""" |
| 462 |
Test the behavior of the `DeleteSession` class when an HTTP error occurs during the |
| 463 |
deletion of a session. |
| 464 |
|
| 465 |
This test case creates a mock `f_module` object with the necessary parameters and |
| 466 |
initializes a `DeleteSession` object with the mock `idrac_connection_session_mock` and the |
| 467 |
`f_module` object. It then sets up the necessary mock functions and side effects to |
| 468 |
simulate an HTTP error during the deletion of a session. Finally, it executes the |
| 469 |
`execute()` method of the `DeleteSession` object and asserts that an |
| 470 |
`AnsibleFailJSonException` is raised with the expected failure message and error |
| 471 |
information. |
| 472 |
|
| 473 |
Parameters: |
| 474 |
- idrac_connection_session_mock (MagicMock): A mock object representing the |
| 475 |
`idrac_connection_session_mock` parameter. |
| 476 |
|
| 477 |
Raises: |
| 478 |
- AssertionError: If the expected failure message or error information is not present |
| 479 |
in the raised exception. |
| 480 |
|
| 481 |
Returns: |
| 482 |
None |
| 483 |
""" |
| 484 |
f_module = self.get_module_mock( |
| 485 |
params={"session_id": "1234", "hostname": "X.X.X.X"}, check_mode=False) |
| 486 |
session_obj = idrac_session.DeleteSession(idrac_connection_session_mock, f_module) |
| 487 |
session_obj.get_session_url = MagicMock(return_value=SESSION_URL) |
| 488 |
session_obj.get_session_status = MagicMock(return_value=200) |
| 489 |
json_str = to_text(json.dumps({"data": "out"})) |
| 490 |
session_obj.idrac.invoke_request.side_effect = HTTPError(HTTPS_PATH, 200, |
| 491 |
HTTP_ERROR, |
| 492 |
{"accept-type": APPLICATION_JSON}, |
| 493 |
StringIO(json_str)) |
| 494 |
try: |
| 495 |
session_obj.execute() |
| 496 |
except AnsibleFailJSonException as ex: |
| 497 |
assert ex.fail_msg == "Unable to 'delete' a session." |
| 498 |
assert ex.fail_kwargs == {'error_info': {'data': 'out'}, 'failed': True} |
| 499 |
|
| 500 |
|
| 501 |
class TestMain(FakeAnsibleModule): |
| 502 |
""" |
| 503 |
Class for testing the main. |
| 504 |
""" |
| 505 |
module = idrac_session |
| 506 |
|
| 507 |
@pytest.fixture |
| 508 |
def idrac_session_mock(self): |
| 509 |
""" |
| 510 |
Creates a mock object for the `idrac_session` fixture. |
| 511 |
|
| 512 |
This function uses the `MagicMock` class from the `unittest.mock` module to create a mock |
| 513 |
object. |
| 514 |
The mock object is then returned by the function. |
| 515 |
|
| 516 |
Returns: |
| 517 |
MagicMock: A mock object representing the `idrac_session`. |
| 518 |
""" |
| 519 |
idrac_obj = MagicMock() |
| 520 |
return idrac_obj |
| 521 |
|
| 522 |
@pytest.fixture |
| 523 |
def idrac_connection_session_mock(self, mocker, idrac_session_mock): |
| 524 |
""" |
| 525 |
Returns a mock object for the `SessionAPI` class from the `MODULE_PATH` module. |
| 526 |
The mock object is initialized with the `idrac_session_mock` as the return value. |
| 527 |
The `__enter__` method of the mock object is also mocked to return `idrac_session_mock`. |
| 528 |
|
| 529 |
:param mocker: The pytest fixture for mocking objects. |
| 530 |
:type mocker: pytest_mock.plugin.MockerFixture |
| 531 |
:param idrac_session_mock: The mock object for the `idrac_session_mock`. |
| 532 |
:type idrac_session_mock: Any |
| 533 |
:return: The mock object for the `SessionAPI` class. |
| 534 |
:rtype: MagicMock |
| 535 |
""" |
| 536 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'SessionAPI', |
| 537 |
return_value=idrac_session_mock) |
| 538 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_session_mock |
| 539 |
return idrac_conn_mock |
| 540 |
|
| 541 |
@pytest.mark.parametrize("exc_type", |
| 542 |
[URLError, HTTPError, SSLValidationError, ConnectionError, |
| 543 |
TypeError, ValueError]) |
| 544 |
def test_idrac_session_main_exception_handling_case(self, exc_type, ome_default_args, mocker): |
| 545 |
""" |
| 546 |
Test the exception handling of the `idrac_session_main` module. |
| 547 |
|
| 548 |
This function tests the exception handling of the `idrac_session_main` module by mocking |
| 549 |
different exceptions and verifying the expected behavior. |
| 550 |
|
| 551 |
Parameters: |
| 552 |
- exc_type (Exception): The type of exception to be raised. |
| 553 |
- ome_default_args (dict): The default arguments for the module. |
| 554 |
- mocker (MockerFixture): The mocker fixture for mocking functions. |
| 555 |
|
| 556 |
Returns: |
| 557 |
None |
| 558 |
|
| 559 |
Raises: |
| 560 |
AssertionError: If the expected result does not match the actual result. |
| 561 |
|
| 562 |
Notes: |
| 563 |
- The function uses the `pytest.mark.parametrize` decorator to parameterize the test |
| 564 |
cases. |
| 565 |
- The `exc_type` parameter represents the type of exception to be raised. |
| 566 |
- The `ome_default_args` parameter contains the default arguments for the module. |
| 567 |
- The `mocker` parameter is used to mock functions and simulate different exceptions. |
| 568 |
- The function calls the `_run_module` method with the `ome_default_args` to execute |
| 569 |
the module. |
| 570 |
- The function verifies the expected result based on the raised exception type. |
| 571 |
|
| 572 |
""" |
| 573 |
json_str = to_text(json.dumps({"data": "out"})) |
| 574 |
if exc_type in [HTTPError, SSLValidationError]: |
| 575 |
mocker.patch(MODULE_PATH + "CreateSession.get_session_url", |
| 576 |
side_effect=exc_type(HTTPS_PATH, 400, |
| 577 |
HTTP_ERROR, |
| 578 |
{"accept-type": APPLICATION_JSON}, |
| 579 |
StringIO(json_str))) |
| 580 |
else: |
| 581 |
ome_default_args.update({"state": "absent", "session_id": "1234", |
| 582 |
"x_auth_token": "token123"}) |
| 583 |
mocker.patch(MODULE_PATH + "DeleteSession.get_session_url", |
| 584 |
side_effect=exc_type('test')) |
| 585 |
result = self._run_module(ome_default_args) |
| 586 |
if exc_type == URLError: |
| 587 |
assert result['unreachable'] is True |
| 588 |
else: |
| 589 |
assert result['failed'] is True |
| 590 |
assert 'msg' in result |