Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorency committed Dec 19, 2024
1 parent 6087019 commit d700808
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
9 changes: 3 additions & 6 deletions plugins/module_utils/clients/_pyvmomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
))

Expand Down
53 changes: 43 additions & 10 deletions tests/unit/plugins/modules/test_utils_pyvmomi_client.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -13,30 +20,56 @@ 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',
'password': 'a',
}
)

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

0 comments on commit d700808

Please sign in to comment.