| 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 ome_template_info |
| 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.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 21 |
from io import StringIO |
| 22 |
from ansible.module_utils._text import to_text |
| 23 |
|
| 24 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture |
| 28 |
def ome_connection_template_info_mock(mocker, ome_response_mock): |
| 29 |
connection_class_mock = mocker.patch(MODULE_PATH + 'ome_template_info.RestOME') |
| 30 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 31 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 32 |
return ome_connection_mock_obj |
| 33 |
|
| 34 |
|
| 35 |
class TestOmeTemplateInfo(FakeAnsibleModule): |
| 36 |
module = ome_template_info |
| 37 |
|
| 38 |
@pytest.mark.parametrize("module_params,data", [({"system_query_options": {"filter": "abc"}}, "$filter")]) |
| 39 |
def test_get_query_parameters(self, module_params, data): |
| 40 |
res = self.module._get_query_parameters(module_params) |
| 41 |
if data is not None: |
| 42 |
assert data in res |
| 43 |
else: |
| 44 |
assert res is None |
| 45 |
|
| 46 |
def test_get_template_info_success_case01(self, ome_default_args, ome_connection_template_info_mock, |
| 47 |
ome_response_mock): |
| 48 |
ome_response_mock.json_data = {"value": [""]} |
| 49 |
ome_response_mock.status_code = 200 |
| 50 |
result = self._run_module(ome_default_args) |
| 51 |
assert 'template_info' in result |
| 52 |
|
| 53 |
def test_get_template_info_success_case02(self, mocker, ome_default_args, ome_connection_template_info_mock, |
| 54 |
ome_response_mock): |
| 55 |
ome_default_args.update({"template_id": "24"}) |
| 56 |
ome_response_mock.success = True |
| 57 |
ome_response_mock.json_data = {"value": [{"template_id": "24"}]} |
| 58 |
ome_response_mock.status_code = 200 |
| 59 |
result = self._run_module(ome_default_args) |
| 60 |
assert result['changed'] is False |
| 61 |
assert 'template_info' in result |
| 62 |
|
| 63 |
def test_get_template_info_success_case03(self, mocker, ome_default_args, ome_connection_template_info_mock, |
| 64 |
ome_response_mock): |
| 65 |
mocker.patch(MODULE_PATH + 'ome_template_info._get_query_parameters', |
| 66 |
return_value={"filter": "abc"}) |
| 67 |
ome_default_args.update({"system_query_options": {"filter": "abc"}}) |
| 68 |
ome_response_mock.success = True |
| 69 |
ome_response_mock.json_data = {"value": [{"filter": "abc"}]} |
| 70 |
ome_response_mock.status_code = 200 |
| 71 |
result = self._run_module(ome_default_args) |
| 72 |
assert result['changed'] is False |
| 73 |
assert 'template_info' in result |
| 74 |
|
| 75 |
def test_get_template_info_failure_case(self, ome_default_args, ome_connection_template_info_mock, |
| 76 |
ome_response_mock): |
| 77 |
ome_response_mock.status_code = 500 |
| 78 |
result = self._run_module(ome_default_args) |
| 79 |
assert result['msg'] == 'Failed to fetch the template facts' |
| 80 |
|
| 81 |
@pytest.mark.parametrize("exc_type", |
| 82 |
[URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError]) |
| 83 |
def test_ome_template_info_main_exception_handling_case(self, exc_type, mocker, ome_default_args, |
| 84 |
ome_connection_template_info_mock, ome_response_mock): |
| 85 |
ome_response_mock.status_code = 400 |
| 86 |
ome_response_mock.success = False |
| 87 |
json_str = to_text(json.dumps({"data": "out"})) |
| 88 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 89 |
ome_connection_template_info_mock.invoke_request.side_effect = exc_type('test') |
| 90 |
else: |
| 91 |
ome_connection_template_info_mock.invoke_request.side_effect = exc_type('https://testhost.com', 400, |
| 92 |
'http error message', |
| 93 |
{"accept-type": "application/json"}, |
| 94 |
StringIO(json_str)) |
| 95 |
result = self._run_module(ome_default_args) |
| 96 |
assert 'template_info' not in result |
| 97 |
assert 'msg' in result |
| 98 |
assert result['failed'] is True |