mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
VMware: Unit test for vmware_guest (#50140)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
fa0ab82d21
commit
8aa2372073
3 changed files with 158 additions and 0 deletions
0
test/units/modules/cloud/vmware/__init__.py
Normal file
0
test/units/modules/cloud/vmware/__init__.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
[
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local",
|
||||
"password": "Secret@123$%",
|
||||
"validate_certs": "True"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Unknown error while connecting to vCenter or ESXi API"
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Hostname parameter is missing."
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"uuid": "52f7b088-357e-bb81-59ec-9d9389c7d89e"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Hostname parameter is missing."
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Username parameter is missing."
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Password parameter is missing."
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local",
|
||||
"password": "Secret@123$%"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Unknown error while connecting to vCenter or ESXi API"
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local",
|
||||
"password": "Secret@123$%",
|
||||
"validate_certs": "False"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "Unknown error while connecting to vCenter or ESXi API"
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local",
|
||||
"password": "Secret@123$%",
|
||||
"port": "8443"
|
||||
},
|
||||
{
|
||||
"failed": "True",
|
||||
"msg": "8443"
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
"name": "sample_vm_001",
|
||||
"hostname": "esxi01.example.com",
|
||||
"username": "administrator@vsphere.local",
|
||||
"password": "Secret@123$%",
|
||||
"validate_certs": "True"
|
||||
},
|
||||
{
|
||||
"test_ssl_context": "True",
|
||||
"failed": "True",
|
||||
"msg": "pyVim does not support changing verification mode with python < 2.7.9. Either update python or use validate_certs=false."
|
||||
}
|
||||
]
|
||||
]
|
52
test/units/modules/cloud/vmware/test_vmware_guest.py
Normal file
52
test/units/modules/cloud/vmware/test_vmware_guest.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright: (c) 2018, Ansible Project
|
||||
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
import sys
|
||||
import pytest
|
||||
import json
|
||||
|
||||
pyvmomi = pytest.importorskip('pyVmomi')
|
||||
|
||||
if sys.version_info < (2, 7):
|
||||
pytestmark = pytest.mark.skip("vmware_guest Ansible modules require Python >= 2.7")
|
||||
|
||||
|
||||
from ansible.modules.cloud.vmware import vmware_guest
|
||||
|
||||
curr_dir = os.path.dirname(__file__)
|
||||
test_data_file = open(os.path.join(curr_dir, 'test_data', 'test_vmware_guest_with_parameters.json'), 'r')
|
||||
TEST_CASES = json.loads(test_data_file.read())
|
||||
test_data_file.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_vmware_guest_wo_parameters(capfd):
|
||||
with pytest.raises(SystemExit):
|
||||
vmware_guest.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert "one of the following is required: name, uuid" in results['msg']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_vmware_guest_with_parameters(mocker, capfd, testcase):
|
||||
if testcase.get('test_ssl_context', None):
|
||||
class mocked_ssl:
|
||||
pass
|
||||
mocker.patch('ansible.module_utils.vmware.ssl', new=mocked_ssl)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
vmware_guest.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert str(results['failed']) == testcase['failed']
|
||||
assert testcase['msg'] in results['msg']
|
Loading…
Reference in a new issue