| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2018, Ansible Project |
| 3 |
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import sys |
| 10 |
import pytest |
| 11 |
|
| 12 |
from unittest import mock |
| 13 |
|
| 14 |
pyvmomi = pytest.importorskip('pyVmomi') |
| 15 |
|
| 16 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import option_diff |
| 17 |
|
| 18 |
import ansible_collections.community.vmware.plugins.module_utils.vmware as vmware_module_utils |
| 19 |
|
| 20 |
|
| 21 |
test_data = [ |
| 22 |
( |
| 23 |
dict( |
| 24 |
username='[email protected]', |
| 25 |
password='Esxi@123$%', |
| 26 |
hostname=False, |
| 27 |
port=443, |
| 28 |
validate_certs=False, |
| 29 |
proxy_host=None, |
| 30 |
proxy_port=None, |
| 31 |
), |
| 32 |
"Hostname parameter is missing. Please specify this parameter in task or" |
| 33 |
" export environment variable like 'export VMWARE_HOST=ESXI_HOSTNAME'" |
| 34 |
), |
| 35 |
( |
| 36 |
dict( |
| 37 |
username=False, |
| 38 |
password='Esxi@123$%', |
| 39 |
hostname='esxi1', |
| 40 |
port=443, |
| 41 |
validate_certs=False, |
| 42 |
), |
| 43 |
"Username parameter is missing. Please specify this parameter in task or" |
| 44 |
" export environment variable like 'export VMWARE_USER=ESXI_USERNAME'" |
| 45 |
), |
| 46 |
( |
| 47 |
dict( |
| 48 |
username='[email protected]', |
| 49 |
password=False, |
| 50 |
hostname='esxi1', |
| 51 |
port=443, |
| 52 |
validate_certs=False, |
| 53 |
), |
| 54 |
"Password parameter is missing. Please specify this parameter in task or" |
| 55 |
" export environment variable like 'export VMWARE_PASSWORD=ESXI_PASSWORD'" |
| 56 |
), |
| 57 |
( |
| 58 |
dict( |
| 59 |
username='[email protected]', |
| 60 |
password='Esxi@123$%', |
| 61 |
hostname='esxi1', |
| 62 |
port=443, |
| 63 |
proxy_host=None, |
| 64 |
proxy_port=None, |
| 65 |
validate_certs=True, |
| 66 |
), |
| 67 |
"certificate verify failed" |
| 68 |
), |
| 69 |
( |
| 70 |
dict( |
| 71 |
username='[email protected]', |
| 72 |
password='Esxi@123$%', |
| 73 |
hostname='esxi1', |
| 74 |
port=443, |
| 75 |
proxy_host='myproxyserver.com', |
| 76 |
proxy_port=80, |
| 77 |
validate_certs=False, |
| 78 |
), |
| 79 |
" [proxy: myproxyserver.com:80]" |
| 80 |
), |
| 81 |
] |
| 82 |
|
| 83 |
test_ids = [ |
| 84 |
'hostname', |
| 85 |
'username', |
| 86 |
'password', |
| 87 |
'validate_certs', |
| 88 |
'valid_http_proxy', |
| 89 |
] |
| 90 |
|
| 91 |
|
| 92 |
class FailJsonException(BaseException): |
| 93 |
pass |
| 94 |
|
| 95 |
|
| 96 |
@pytest.fixture |
| 97 |
def fake_ansible_module(): |
| 98 |
ret = mock.Mock() |
| 99 |
ret.params = test_data[3][0] |
| 100 |
ret.tmpdir = None |
| 101 |
ret.fail_json.side_effect = FailJsonException() |
| 102 |
return ret |
| 103 |
|
| 104 |
|
| 105 |
def fake_connect_to_api(module, return_si=None): |
| 106 |
return None, mock.Mock() |
| 107 |
|
| 108 |
|
| 109 |
testdata = [ |
| 110 |
('HAS_PYVMOMI', 'PyVmomi'), |
| 111 |
('HAS_REQUESTS', 'requests'), |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize("key,libname", testdata) |
| 116 |
def test_lib_loading_failure(monkeypatch, fake_ansible_module, key, libname): |
| 117 |
""" Test if Pyvmomi is present or not""" |
| 118 |
monkeypatch.setattr(vmware_module_utils, key, False) |
| 119 |
with pytest.raises(FailJsonException): |
| 120 |
vmware_module_utils.PyVmomi(fake_ansible_module) |
| 121 |
error_str = 'Failed to import the required Python library (%s)' % libname |
| 122 |
fake_ansible_module.fail_json.assert_called_once() |
| 123 |
assert error_str in fake_ansible_module.fail_json.call_args[1]['msg'] |
| 124 |
|
| 125 |
|
| 126 |
@pytest.mark.skipif(sys.version_info < (2, 7), reason="requires python2.7 and greater") |
| 127 |
@pytest.mark.parametrize("params, msg", test_data, ids=test_ids) |
| 128 |
def test_required_params(request, params, msg, fake_ansible_module): |
| 129 |
""" Test if required params are correct or not""" |
| 130 |
fake_ansible_module.params = params |
| 131 |
with pytest.raises(FailJsonException): |
| 132 |
vmware_module_utils.connect_to_api(fake_ansible_module) |
| 133 |
fake_ansible_module.fail_json.assert_called_once() |
| 134 |
# TODO: assert msg in fake_ansible_module.fail_json.call_args[1]['msg'] |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.parametrize("test_options, test_current_options, test_truthy_strings_as_bool", [ |
| 138 |
({"data": True}, [], True), |
| 139 |
({"data": 1}, [], True), |
| 140 |
({"data": 1.2}, [], True), |
| 141 |
({"data": 'string'}, [], True), |
| 142 |
({"data": True}, [], False), |
| 143 |
({"data": 1}, [], False), |
| 144 |
({"data": 1.2}, [], False), |
| 145 |
({"data": 'string'}, [], False), |
| 146 |
]) |
| 147 |
def test_option_diff(test_options, test_current_options, test_truthy_strings_as_bool): |
| 148 |
assert option_diff(test_options, test_current_options, test_truthy_strings_as_bool)[0].value == test_options["data"] |