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.
|
||||
folder:
|
||||
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.
|
||||
- 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:
|
||||
description:
|
||||
- Destination datacenter for the deploy operation
|
||||
|
@ -83,27 +97,23 @@ instance:
|
|||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
# import module snippets
|
||||
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._text import to_text
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
|
||||
HAS_PYVMOMI = False
|
||||
|
||||
try:
|
||||
import pyVmomi
|
||||
from pyVmomi import vim
|
||||
|
||||
HAS_PYVMOMI = True
|
||||
except ImportError:
|
||||
pass
|
||||
HAS_PYVMOMI = False
|
||||
|
||||
|
||||
class PyVmomiHelper(object):
|
||||
|
@ -121,10 +131,7 @@ class PyVmomiHelper(object):
|
|||
if uuid:
|
||||
vm = find_vm_by_id(self.content, vm_id=uuid, vm_id_type="uuid")
|
||||
elif folder:
|
||||
# Build the absolute folder path to pass into the search method
|
||||
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
|
||||
searchpath = self.params['folder']
|
||||
|
||||
# get all objects for this path ...
|
||||
f_obj = self.content.searchIndex.FindByInventoryPath(searchpath)
|
||||
|
@ -167,11 +174,11 @@ def main():
|
|||
folder=dict(required=False, type='str', default='/vm'),
|
||||
datacenter=dict(required=True, type='str'),
|
||||
),
|
||||
required_together=[('name', 'folder')],
|
||||
)
|
||||
|
||||
# Prepend /vm if it was missing from the folder path, also strip trailing slashes
|
||||
if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'):
|
||||
module.params['folder'] = '/vm%(folder)s' % module.params
|
||||
# FindByInventoryPath() does not require an absolute path
|
||||
# so we should leave the input folder path unmodified
|
||||
module.params['folder'] = module.params['folder'].rstrip('/')
|
||||
|
||||
pyv = PyVmomiHelper(module)
|
||||
|
@ -184,9 +191,8 @@ def main():
|
|||
if vm:
|
||||
try:
|
||||
module.exit_json(instance=pyv.gather_facts(vm))
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="Fact gather failed with exception %s" % e)
|
||||
except Exception as exc:
|
||||
module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc))
|
||||
else:
|
||||
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…
Reference in a new issue