Add Test Suite for vmware_vswitch (#26076)

Fix adds test suite for vwmare_switch

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-06-30 19:48:20 +05:30 committed by jctanner
parent 69e55b2cfe
commit 5c374cd1ae
5 changed files with 68 additions and 12 deletions

View file

@ -91,10 +91,12 @@ try:
HAS_PYVMOMI = True HAS_PYVMOMI = True
except ImportError: except ImportError:
HAS_PYVMOMI = False HAS_PYVMOMI = False
from ansible.module_utils.vmware import vmware_argument_spec, get_all_objs, connect_to_api
from ansible.module_utils.basic import AnsibleModule
def find_vswitch_by_name(host, vswitch_name): def find_vswitch_by_name(host, vswitch_name):
for vss in host.config.network.vswitch: for vss in host.configManager.networkSystem.networkInfo.vswitch:
if vss.name == vswitch_name: if vss.name == vswitch_name:
return vss return vss
return None return None
@ -137,7 +139,6 @@ class VMwareHostVirtualSwitch(object):
except Exception as e: except Exception as e:
self.module.fail_json(msg=str(e)) self.module.fail_json(msg=str(e))
# Source from # Source from
# https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py # https://github.com/rreubenur/pyvmomi-community-samples/blob/patch-1/samples/create_vswitch.py
@ -180,7 +181,7 @@ class VMwareHostVirtualSwitch(object):
if not host: if not host:
self.module.fail_json(msg="Unable to find host") self.module.fail_json(msg="Unable to find host")
self.host_system = host.keys()[0] self.host_system = list(host.keys())[0]
self.vss = find_vswitch_by_name(self.host_system, self.switch_name) self.vss = find_vswitch_by_name(self.host_system, self.switch_name)
if self.vss is None: if self.vss is None:
@ -192,10 +193,10 @@ class VMwareHostVirtualSwitch(object):
def main(): def main():
argument_spec = vmware_argument_spec() argument_spec = vmware_argument_spec()
argument_spec.update(dict(switch_name=dict(required=True, type='str'), argument_spec.update(dict(switch_name=dict(required=True, type='str'),
nic_name=dict(required=False, type='str'), nic_name=dict(required=False, type='str'),
number_of_ports=dict(required=False, type='int', default=128), number_of_ports=dict(required=False, type='int', default=128),
mtu=dict(required=False, type='int', default=1500), mtu=dict(required=False, type='int', default=1500),
state=dict(default='present', choices=['present', 'absent'], type='str'))) state=dict(default='present', choices=['present', 'absent'], type='str')))
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
@ -205,8 +206,5 @@ def main():
host_virtual_switch = VMwareHostVirtualSwitch(module) host_virtual_switch = VMwareHostVirtualSwitch(module)
host_virtual_switch.process_state() host_virtual_switch.process_state()
from ansible.module_utils.vmware import *
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -1,7 +1,7 @@
- name: make sure pyvmomi is installed - name: make sure pyvmomi is installed
pip: pip:
name: pyvmomi name: pyvmomi
state: latest state: latest
- name: store the vcenter container ip - name: store the vcenter container ip
set_fact: set_fact:

View file

@ -0,0 +1,2 @@
posix/ci/cloud/vcenter
cloud/vcenter

View file

@ -0,0 +1,57 @@
# Test code for the vmware_vswitch
# (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: kill vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/killall' }}"
- name: start vcsim
uri:
url: "{{ 'http://' + vcsim + ':5000/spawn?cluster=2' }}"
register: vcsim_instance
- debug: var=vcsim_instance
- name: find folders for each vm
vmware_vswitch:
validate_certs: False
hostname: "{{ vcsim }}"
username: "{{ vcsim_instance['json']['username'] }}"
password: "{{ vcsim_instance['json']['password'] }}"
switch_name: vmswitch_0001
nic_name: vnic_1
register: vswitch
- debug: var=vswitch
# Currently, we don't return anything about vswitch, will
# extend this TC once we have something to test more than
# the state of task.
- assert:
that:
- "{{ vswitch.changed == true }}"

View file

@ -203,7 +203,6 @@ lib/ansible/modules/cloud/vmware/vmware_migrate_vmk.py
lib/ansible/modules/cloud/vmware/vmware_target_canonical_facts.py lib/ansible/modules/cloud/vmware/vmware_target_canonical_facts.py
lib/ansible/modules/cloud/vmware/vmware_vm_shell.py lib/ansible/modules/cloud/vmware/vmware_vm_shell.py
lib/ansible/modules/cloud/vmware/vmware_vmotion.py lib/ansible/modules/cloud/vmware/vmware_vmotion.py
lib/ansible/modules/cloud/vmware/vmware_vswitch.py
lib/ansible/modules/cloud/vmware/vsphere_copy.py lib/ansible/modules/cloud/vmware/vsphere_copy.py
lib/ansible/modules/cloud/vmware/vsphere_guest.py lib/ansible/modules/cloud/vmware/vsphere_guest.py
lib/ansible/modules/cloud/webfaction/webfaction_app.py lib/ansible/modules/cloud/webfaction/webfaction_app.py