From d7008081b4763d763835e4659c2eb67345cd5624 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 19 Dec 2024 10:37:24 -0500 Subject: [PATCH] more unit tests --- plugins/module_utils/clients/_pyvmomi.py | 9 ++-- .../modules/test_utils_pyvmomi_client.py | 53 +++++++++++++++---- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/plugins/module_utils/clients/_pyvmomi.py b/plugins/module_utils/clients/_pyvmomi.py index 5f2e5e31c..59359c3d9 100644 --- a/plugins/module_utils/clients/_pyvmomi.py +++ b/plugins/module_utils/clients/_pyvmomi.py @@ -76,9 +76,6 @@ def connect_to_api(self, connection_params, disconnect_atexit=True, return_si=Fa if ssl_context: connection_args.update(sslContext=ssl_context) - if http_proxy_host: - self.__add_proxy_to_connection(self, connection_args) - service_instance = self.__create_service_instance( connection_args, username, password, http_proxy_host, http_proxy_port) @@ -145,13 +142,13 @@ def __create_service_instance(self, connection_args, username, password, http_pr error_msg_suffix = '' try: if http_proxy_host: - error_msg_suffix = " [proxy: %s:%d]" % (http_proxy_host, http_proxy_port) + error_msg_suffix = " [proxy: %s:%s]" % (http_proxy_host, http_proxy_port) connection_args.update(httpProxyHost=http_proxy_host, httpProxyPort=http_proxy_port) smart_stub = connect.SmartStubAdapter(**connection_args) session_stub = connect.VimSessionOrientedStub( smart_stub, connect.VimSessionOrientedStub.makeUserLoginMethod(username, password) ) - service_instance = vim.ServiceInstance('ServiceInstance', session_stub) + service_instance = vim.ServiceInstance('ServiceInstance', session_stub ) else: connection_args.update(user=username, pwd=password) service_instance = connect.SmartConnect(**connection_args) @@ -180,7 +177,7 @@ def __create_service_instance(self, connection_args, username, password, http_pr except Exception as e: raise ApiAccessError(( "Unknown error while connecting to the vCenter or ESXi API at %s:%s : %s" % - (connection_args['host'], connection_args['port'], e.msg) + + (connection_args['host'], connection_args['port'], str(e)) + error_msg_suffix )) diff --git a/tests/unit/plugins/modules/test_utils_pyvmomi_client.py b/tests/unit/plugins/modules/test_utils_pyvmomi_client.py index 13922e57c..f7e9e13e6 100644 --- a/tests/unit/plugins/modules/test_utils_pyvmomi_client.py +++ b/tests/unit/plugins/modules/test_utils_pyvmomi_client.py @@ -1,8 +1,15 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import pytest + from ansible_collections.vmware.vmware.plugins.module_utils.clients._pyvmomi import PyvmomiClient +from ansible_collections.vmware.vmware.plugins.module_utils.clients._errors import ( + ApiAccessError +) + from pyVim import connect +from pyVmomi import vim class MockContainerView(): @@ -13,14 +20,16 @@ def __init__(self): class TestPyvmomiClient(): def __prepare(self, mocker): - mocked_smart_connect = mocker.patch.object(connect, 'SmartConnect') - service_instance = mocker.Mock() - mocked_smart_connect.return_value = service_instance - - content = mocker.Mock() - content_mock = mocker.patch.object(service_instance, 'RetrieveContent') - content_mock.return_value = content - self.client = PyvmomiClient( + # non-proxy init mocks + mocker.patch.object(connect, 'SmartConnect') + + # proxy init mocks + mocker.patch.object(connect, 'SmartStubAdapter') + mocker.patch.object(connect, 'VimSessionOrientedStub') + mocker.patch.object(vim, 'ServiceInstance') + + def __prepare_client(self): + return PyvmomiClient( { 'hostname': 'a', 'username': 'a', @@ -28,15 +37,39 @@ def __prepare(self, mocker): } ) + def test_class_init(self, mocker): + self.__prepare(mocker) + init_args = { + 'hostname': 'a', + 'username': 'a', + 'password': 'a', + 'port': 443, + 'validate_certs': True, + 'http_proxy_host': 'a', + 'http_proxy_port': 443 + } + + PyvmomiClient(init_args) + + with pytest.raises(ApiAccessError): + PyvmomiClient({**init_args, **{'hostname': ''}}) + + with pytest.raises(ApiAccessError): + PyvmomiClient({**init_args, **{'username': ''}}) + + with pytest.raises(ApiAccessError): + PyvmomiClient({**init_args, **{'password': ''}}) + def test_get_all_objs_by_type(self, mocker): self.__prepare(mocker) - mocked_container_view = mocker.patch.object(self.client.content.viewManager, 'CreateContainerView') + client = self.__prepare_client() + mocked_container_view = mocker.patch.object(client.content.viewManager, 'CreateContainerView') mocked_container_view.return_value = MockContainerView() mocked_container_view.return_value.view = [ object(), object() ] - objs = self.client.get_all_objs_by_type(vimtype='blah', folder=object()) + objs = client.get_all_objs_by_type(vimtype='blah', folder=object()) assert len(objs) == 2