| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
import pytest |
| 13 |
import json |
| 14 |
from ansible_collections.dellemc.openmanage.plugins.modules import omevv_vcenter_info |
| 15 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 16 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 17 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 18 |
from io import StringIO |
| 19 |
from unittest.mock import MagicMock |
| 20 |
from ansible.module_utils._text import to_text |
| 21 |
|
| 22 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 23 |
SUCCESS_MSG = "Successfully retrieved the vCenter information." |
| 24 |
GET_ALL_VCENTER_INFO_KEY = "omevv_vcenter_info.OMEVVInfo.get_vcenter_info" |
| 25 |
PERFORM_OPERATION_KEY = "omevv_vcenter_info.OMEVVVCenterInfo.perform_module_operation" |
| 26 |
|
| 27 |
|
| 28 |
class TestOMEVVVCENTERINFO(FakeAnsibleModule): |
| 29 |
module = omevv_vcenter_info |
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def omevv_vcenter_info_mock(self): |
| 33 |
omevv_obj = MagicMock() |
| 34 |
return omevv_obj |
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def omevv_connection_vcenter_info(self, mocker, omevv_vcenter_info_mock): |
| 38 |
omevv_conn_mock = mocker.patch(MODULE_PATH + 'omevv_vcenter_info.RestOMEVV', |
| 39 |
return_value=omevv_vcenter_info_mock) |
| 40 |
omevv_conn_mock.return_value.__enter__.return_value = omevv_vcenter_info_mock |
| 41 |
return omevv_conn_mock |
| 42 |
|
| 43 |
def test_perform_operation(self, ome_default_args, omevv_connection_vcenter_info, |
| 44 |
omevv_vcenter_info_mock, mocker): |
| 45 |
sample_resp = [ |
| 46 |
{ |
| 47 |
"uuid": "77373c7e-d2b0-453b-9567-102484519bd1", |
| 48 |
"consoleAddress": "hostname1", |
| 49 |
"description": "vCenter 8.0", |
| 50 |
"registeredExtensions": [ |
| 51 |
"PHM", |
| 52 |
"WEBCLIENT", |
| 53 |
"PHA", |
| 54 |
"VLCM" |
| 55 |
] |
| 56 |
}, |
| 57 |
{ |
| 58 |
"uuid": "77373c7e-d2b0-453b-9567-102484519bd2", |
| 59 |
"consoleAddress": "hostname2", |
| 60 |
"description": "vCenter 8.1", |
| 61 |
"registeredExtensions": [ |
| 62 |
"PHM", |
| 63 |
"WEBCLIENT", |
| 64 |
"PHA", |
| 65 |
"VLCM" |
| 66 |
] |
| 67 |
} |
| 68 |
] |
| 69 |
|
| 70 |
obj = MagicMock() |
| 71 |
obj.success = 'OK' |
| 72 |
obj.json_data = sample_resp |
| 73 |
mocker.patch(MODULE_PATH + GET_ALL_VCENTER_INFO_KEY, return_value=sample_resp) |
| 74 |
resp = self._run_module(ome_default_args) |
| 75 |
assert resp['msg'] == SUCCESS_MSG |
| 76 |
assert resp['changed'] is False |
| 77 |
|
| 78 |
|
| 79 |
ome_default_args.update({'vcenter_hostname': 'hostname1'}) |
| 80 |
mocker.patch(MODULE_PATH + GET_ALL_VCENTER_INFO_KEY, return_value=sample_resp[0]) |
| 81 |
resp = self._run_module(ome_default_args) |
| 82 |
assert resp['msg'] == SUCCESS_MSG |
| 83 |
assert resp['changed'] is False |
| 84 |
|
| 85 |
|
| 86 |
obj.json_data = [sample_resp[1]] |
| 87 |
ome_default_args.update({'vcenter_hostname': 'hostname1'}) |
| 88 |
mocker.patch(MODULE_PATH + GET_ALL_VCENTER_INFO_KEY, return_value={}) |
| 89 |
resp = self._run_module(ome_default_args) |
| 90 |
assert resp['msg'] == "'hostname1' vCenter is not registered in OME." |
| 91 |
assert resp['changed'] is False |
| 92 |
|
| 93 |
@pytest.mark.parametrize("exc_type", |
| 94 |
[URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError]) |
| 95 |
def test_omevv_vcenter_info_main_exception_handling_case(self, exc_type, mocker, ome_default_args, |
| 96 |
omevv_connection_vcenter_info, omevv_vcenter_info_mock): |
| 97 |
omevv_vcenter_info_mock.status_code = 400 |
| 98 |
omevv_vcenter_info_mock.success = False |
| 99 |
json_str = to_text(json.dumps({"data": "out"})) |
| 100 |
if exc_type in [HTTPError, SSLValidationError]: |
| 101 |
mocker.patch(MODULE_PATH + PERFORM_OPERATION_KEY, |
| 102 |
side_effect=exc_type('https://testhost.com', 400, |
| 103 |
'http error message', |
| 104 |
{"accept-type": "application/json"}, |
| 105 |
StringIO(json_str))) |
| 106 |
else: |
| 107 |
mocker.patch(MODULE_PATH + PERFORM_OPERATION_KEY, |
| 108 |
side_effect=exc_type('test')) |
| 109 |
result = self._run_module(ome_default_args) |
| 110 |
if exc_type == URLError: |
| 111 |
assert result['unreachable'] is True |
| 112 |
else: |
| 113 |
assert result['failed'] is True |
| 114 |
assert 'msg' in result |