mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 22:30:22 -07:00
Add testcase for vmware_guest_facts (#26873)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
efbc65bff0
commit
e979663dfe
3 changed files with 108 additions and 18 deletions
|
@ -53,8 +53,22 @@ options:
|
||||||
- This is required if name is not supplied.
|
- This is required if name is not supplied.
|
||||||
folder:
|
folder:
|
||||||
description:
|
description:
|
||||||
- Destination folder, absolute path to find an existing guest.
|
- Destination folder, absolute or relative path to find an existing guest.
|
||||||
- This is required if name is supplied.
|
- This is required if name is supplied.
|
||||||
|
- The folder should include the datacenter. ESX's datacenter is ha-datacenter
|
||||||
|
- 'Examples:'
|
||||||
|
- ' folder: /ha-datacenter/vm'
|
||||||
|
- ' folder: ha-datacenter/vm'
|
||||||
|
- ' folder: /datacenter1/vm'
|
||||||
|
- ' folder: datacenter1/vm'
|
||||||
|
- ' folder: /datacenter1/vm/folder1'
|
||||||
|
- ' folder: datacenter1/vm/folder1'
|
||||||
|
- ' folder: /folder1/datacenter1/vm'
|
||||||
|
- ' folder: folder1/datacenter1/vm'
|
||||||
|
- ' folder: /folder1/datacenter1/vm/folder2'
|
||||||
|
- ' folder: vm/folder2'
|
||||||
|
- ' fodler: folder2'
|
||||||
|
default: /vm
|
||||||
datacenter:
|
datacenter:
|
||||||
description:
|
description:
|
||||||
- Destination datacenter for the deploy operation
|
- Destination datacenter for the deploy operation
|
||||||
|
@ -83,27 +97,23 @@ instance:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
|
|
||||||
# import module snippets
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.pycompat24 import get_exception
|
|
||||||
from ansible.module_utils.six import iteritems
|
|
||||||
from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, gather_vm_facts
|
from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, gather_vm_facts
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
HAS_PYVMOMI = False
|
|
||||||
try:
|
try:
|
||||||
import pyVmomi
|
import pyVmomi
|
||||||
from pyVmomi import vim
|
from pyVmomi import vim
|
||||||
|
|
||||||
HAS_PYVMOMI = True
|
HAS_PYVMOMI = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
HAS_PYVMOMI = False
|
||||||
|
|
||||||
|
|
||||||
class PyVmomiHelper(object):
|
class PyVmomiHelper(object):
|
||||||
|
@ -121,10 +131,7 @@ class PyVmomiHelper(object):
|
||||||
if uuid:
|
if uuid:
|
||||||
vm = find_vm_by_id(self.content, vm_id=uuid, vm_id_type="uuid")
|
vm = find_vm_by_id(self.content, vm_id=uuid, vm_id_type="uuid")
|
||||||
elif folder:
|
elif folder:
|
||||||
# Build the absolute folder path to pass into the search method
|
searchpath = self.params['folder']
|
||||||
if not self.params['folder'].startswith('/'):
|
|
||||||
self.module.fail_json(msg="Folder %(folder)s needs to be an absolute path, starting with '/'." % self.params)
|
|
||||||
searchpath = '%(datacenter)s%(folder)s' % self.params
|
|
||||||
|
|
||||||
# get all objects for this path ...
|
# get all objects for this path ...
|
||||||
f_obj = self.content.searchIndex.FindByInventoryPath(searchpath)
|
f_obj = self.content.searchIndex.FindByInventoryPath(searchpath)
|
||||||
|
@ -167,11 +174,11 @@ def main():
|
||||||
folder=dict(required=False, type='str', default='/vm'),
|
folder=dict(required=False, type='str', default='/vm'),
|
||||||
datacenter=dict(required=True, type='str'),
|
datacenter=dict(required=True, type='str'),
|
||||||
),
|
),
|
||||||
|
required_together=[('name', 'folder')],
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prepend /vm if it was missing from the folder path, also strip trailing slashes
|
# FindByInventoryPath() does not require an absolute path
|
||||||
if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'):
|
# so we should leave the input folder path unmodified
|
||||||
module.params['folder'] = '/vm%(folder)s' % module.params
|
|
||||||
module.params['folder'] = module.params['folder'].rstrip('/')
|
module.params['folder'] = module.params['folder'].rstrip('/')
|
||||||
|
|
||||||
pyv = PyVmomiHelper(module)
|
pyv = PyVmomiHelper(module)
|
||||||
|
@ -184,9 +191,8 @@ def main():
|
||||||
if vm:
|
if vm:
|
||||||
try:
|
try:
|
||||||
module.exit_json(instance=pyv.gather_facts(vm))
|
module.exit_json(instance=pyv.gather_facts(vm))
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
e = get_exception()
|
module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc))
|
||||||
module.fail_json(msg="Fact gather failed with exception %s" % e)
|
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg="Unable to gather facts for non-existing VM %(name)s" % module.params)
|
module.fail_json(msg="Unable to gather facts for non-existing VM %(name)s" % module.params)
|
||||||
|
|
||||||
|
|
2
test/integration/targets/vmware_guest_facts/aliases
Normal file
2
test/integration/targets/vmware_guest_facts/aliases
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
posix/ci/cloud/vcenter
|
||||||
|
cloud/vcenter
|
82
test/integration/targets/vmware_guest_facts/tasks/main.yml
Normal file
82
test/integration/targets/vmware_guest_facts/tasks/main.yml
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
# Test code for the vmware_guest_facts module.
|
||||||
|
# (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
|
||||||
|
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
- name: make sure pyvmomi is installed
|
||||||
|
pip:
|
||||||
|
name: pyvmomi
|
||||||
|
state: latest
|
||||||
|
|
||||||
|
- name: store the vcenter container ip
|
||||||
|
set_fact:
|
||||||
|
vcsim: "{{ lookup('env', 'vcenter_host') }}"
|
||||||
|
|
||||||
|
- debug: var=vcsim
|
||||||
|
|
||||||
|
- name: Wait for Flask controller to come up online
|
||||||
|
wait_for:
|
||||||
|
host: "{{ vcsim }}"
|
||||||
|
port: 5000
|
||||||
|
state: started
|
||||||
|
|
||||||
|
- name: kill vcsim
|
||||||
|
uri:
|
||||||
|
url: "{{ 'http://' + vcsim + ':5000/killall' }}"
|
||||||
|
|
||||||
|
- name: start vcsim
|
||||||
|
uri:
|
||||||
|
url: "{{ 'http://' + vcsim + ':5000/spawn?datacenter=1&cluster=1&folder=0' }}"
|
||||||
|
register: vcsim_instance
|
||||||
|
|
||||||
|
- name: Wait for vcsim server to come up online
|
||||||
|
wait_for:
|
||||||
|
host: "{{ vcsim }}"
|
||||||
|
port: 443
|
||||||
|
state: started
|
||||||
|
|
||||||
|
- name: get a list of Datacenter from vcsim
|
||||||
|
uri:
|
||||||
|
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=DC' }}"
|
||||||
|
register: datacenters
|
||||||
|
|
||||||
|
- set_fact: dc1="{{ datacenters['json'][0] }}"
|
||||||
|
|
||||||
|
- name: get a list of virtual machines from vcsim
|
||||||
|
uri:
|
||||||
|
url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=VM' }}"
|
||||||
|
register: vms
|
||||||
|
|
||||||
|
- set_fact: vm1="{{ vms['json'][0] }}"
|
||||||
|
|
||||||
|
# Testcase 0001: Get details about virtual machines
|
||||||
|
- name: get list of facts about virtual machines
|
||||||
|
vmware_guest_facts:
|
||||||
|
validate_certs: False
|
||||||
|
hostname: "{{ vcsim }}"
|
||||||
|
username: "{{ vcsim_instance['json']['username'] }}"
|
||||||
|
password: "{{ vcsim_instance['json']['password'] }}"
|
||||||
|
datacenter: "{{ dc1 | basename }}"
|
||||||
|
name: "{{ vm1 | basename }}"
|
||||||
|
folder: "{{ vm1 | dirname }}"
|
||||||
|
register: guest_facts_0001
|
||||||
|
|
||||||
|
- debug: msg="{{ guest_facts_0001 }}"
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- "guest_facts_0001['instance']['hw_name'] == vm1 | basename"
|
||||||
|
- "guest_facts_0001['instance']['hw_product_uuid'] is defined"
|
Loading…
Add table
Add a link
Reference in a new issue