| 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_job_info |
| 18 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 19 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 20 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 21 |
from io import StringIO |
| 22 |
from ansible.module_utils._text import to_text |
| 23 |
|
| 24 |
|
| 25 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 26 |
|
| 27 |
|
| 28 |
class TestOmeJobInfo(FakeAnsibleModule): |
| 29 |
"""Pyest class for ome_job_info module.""" |
| 30 |
module = ome_job_info |
| 31 |
|
| 32 |
@pytest.fixture |
| 33 |
def ome_connection_job_info_mock(self, mocker, ome_response_mock): |
| 34 |
connection_class_mock = mocker.patch(MODULE_PATH + 'ome_job_info.RestOME') |
| 35 |
ome_connection_mock_obj = connection_class_mock.return_value.__enter__.return_value |
| 36 |
ome_connection_mock_obj.invoke_request.return_value = ome_response_mock |
| 37 |
return ome_connection_mock_obj |
| 38 |
|
| 39 |
@pytest.mark.parametrize("module_params,data", [({"system_query_options": {"filter": "abc"}}, "$filter")]) |
| 40 |
def test_get_query_parameters(self, module_params, data): |
| 41 |
res = self.module._get_query_parameters(module_params) |
| 42 |
if data is not None: |
| 43 |
assert data in res |
| 44 |
else: |
| 45 |
assert res is None |
| 46 |
|
| 47 |
def test_job_info_success_case(self, ome_default_args, ome_connection_job_info_mock, |
| 48 |
ome_response_mock): |
| 49 |
ome_response_mock.json_data = {"@odata.context": "/api/$metadata#Collection(JobService.Job)", |
| 50 |
"@odata.count": 1} |
| 51 |
ome_response_mock.success = True |
| 52 |
job_details = {"resp_obj": ome_response_mock, |
| 53 |
"report_list": [{"Name": "job1", "Id": 123}, {"Name": "job2", "Id": 124}]} |
| 54 |
ome_connection_job_info_mock.get_all_report_details.return_value = job_details |
| 55 |
result = self._run_module(ome_default_args) |
| 56 |
assert 'job_info' in result |
| 57 |
assert result['msg'] == "Successfully fetched the job info" |
| 58 |
|
| 59 |
def test_job_info_main_success_case_job_id(self, ome_default_args, ome_connection_job_info_mock, |
| 60 |
ome_response_mock): |
| 61 |
ome_default_args.update({"job_id": 1}) |
| 62 |
ome_response_mock.success = True |
| 63 |
ome_response_mock.json_data = {"value": [{"job_id": 1}]} |
| 64 |
ome_response_mock.status_code = 200 |
| 65 |
result = self._run_module(ome_default_args) |
| 66 |
assert result['changed'] is False |
| 67 |
assert 'job_info' in result |
| 68 |
|
| 69 |
def test_get_execution_history_and_last_execution_detail_of_a_job(self, ome_default_args, |
| 70 |
ome_connection_job_info_mock, |
| 71 |
ome_response_mock): |
| 72 |
ome_default_args.update({"job_id": 1, "fetch_execution_history": True}) |
| 73 |
ome_response_mock.success = True |
| 74 |
ome_response_mock.json_data = {"value": [{"job_id": 1}]} |
| 75 |
ome_response_mock.status_code = 200 |
| 76 |
result = self._run_module(ome_default_args) |
| 77 |
assert result['changed'] is False |
| 78 |
assert 'job_info' in result |
| 79 |
assert 'LastExecutionDetail' in result['job_info'] |
| 80 |
assert 'ExecutionHistories' in result['job_info'] |
| 81 |
|
| 82 |
def test_job_info_success_case03(self, ome_default_args, ome_connection_job_info_mock, |
| 83 |
ome_response_mock): |
| 84 |
ome_default_args.update({"system_query_options": {"filter": "abc"}}) |
| 85 |
ome_response_mock.success = True |
| 86 |
ome_response_mock.json_data = {"value": [{"filter": "abc"}]} |
| 87 |
ome_response_mock.status_code = 200 |
| 88 |
result = self._run_module(ome_default_args) |
| 89 |
assert result['changed'] is False |
| 90 |
assert 'job_info' in result |
| 91 |
|
| 92 |
def test_job_info_failure_case(self, ome_default_args, ome_connection_job_info_mock, |
| 93 |
ome_response_mock): |
| 94 |
ome_response_mock.status_code = 500 |
| 95 |
result = self._run_module(ome_default_args) |
| 96 |
assert result['msg'] == "Failed to fetch the job info" |
| 97 |
|
| 98 |
@pytest.mark.parametrize("exc_type", [URLError, HTTPError, SSLValidationError, ConnectionError, |
| 99 |
TypeError, ValueError]) |
| 100 |
def test_job_info_main_exception_case(self, exc_type, mocker, ome_default_args, ome_connection_job_info_mock, |
| 101 |
ome_response_mock): |
| 102 |
ome_response_mock.status_code = 400 |
| 103 |
ome_response_mock.success = False |
| 104 |
json_str = to_text(json.dumps({"data": "out"})) |
| 105 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 106 |
mocker.patch( |
| 107 |
MODULE_PATH + 'ome_job_info._get_query_parameters', |
| 108 |
side_effect=exc_type('test')) |
| 109 |
else: |
| 110 |
mocker.patch( |
| 111 |
MODULE_PATH + 'ome_job_info._get_query_parameters', |
| 112 |
side_effect=exc_type('https://testhost.com', 400, 'http error message', |
| 113 |
{"accept-type": "application/json"}, StringIO(json_str))) |
| 114 |
if exc_type != URLError: |
| 115 |
result = self._run_module(ome_default_args) |
| 116 |
assert result['failed'] is True |
| 117 |
else: |
| 118 |
result = self._run_module(ome_default_args) |
| 119 |
assert 'msg' in result |