Skip to content

Commit

Permalink
tests: use vmware.vmware_rest to clean up the resources (ansible-coll…
Browse files Browse the repository at this point in the history
…ections#1125)

tests: use vmware.vmware_rest to clean up the resources

Depends-On: ansible/ansible-zuul-jobs#1215
vmware.vmware_rest allow uses to simplify the clean up of the resources
and avoid any leaked VM.
Also:

test VMs use lighter disk size.
do not umount the datastore during the teardown.

Reviewed-by: None <None>
  • Loading branch information
goneri authored Nov 15, 2021
1 parent 5f96b4b commit 836ba31
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 145 deletions.
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pytest-xdist
# We should avoid these two modules with py3
pytest-mock
mock
aiohttp ; python_version >= '3.6'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
state: poweredoff
guest_id: debian8_64Guest
disk:
- size_gb: 1
- size_mb: 10
type: thin
datastore: '{{ rw_datastore }}'
hardware:
Expand Down
42 changes: 23 additions & 19 deletions tests/integration/targets/prepare_vmware_tests/tasks/teardown.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
---
- name: Delete a datastore cluster to datacenter
vmware_datastore_cluster:
datacenter_name: "{{ dc1 }}"
datastore_cluster_name: '{{ item }}'
state: absent
with_items:
- DSC1
- DSC2
ignore_errors: true

- name: Remove the datacenter
vmware_datacenter:
datacenter_name: '{{ item }}'
state: absent
when: vcsim is not defined
with_items:
- '{{ dc1 }}'
- datacenter_0001

- when: not (vcsim is defined)
block:
- name: Get a list of all the datacenters
vmware.vmware_rest.vcenter_datacenter_info:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
register: existing_datacenters
- name: Force delete the existing DC
vmware.vmware_rest.vcenter_datacenter:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
state: absent
datacenter: '{{ item.datacenter }}'
force: true
with_items: "{{ existing_datacenters.value }}"
until: _result is succeeded
retries: 10
delay: 1
register: _result
- name: kill vcsim
uri:
url: "http://{{ vcsim }}:5000/killall"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,111 +1,94 @@
---
- name: Clean up the firewall rules
vmware_host_firewall_manager:
cluster_name: '{{ ccr1 }}'
rules:
- name: vvold
enabled: false
- name: CIMHttpServer
enabled: true
allowed_hosts:
all_ip: true
- name: NFC
enabled: true
allowed_hosts:
all_ip: true
- name: Collect the list of the existing VM
vmware.vmware_rest.vcenter_vm_info:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
register: existing_vms
until: existing_vms is not failed

- name: Turn off the VM
vmware.vmware_rest.vcenter_vm_power:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
state: stop
vm: '{{ item.vm }}'
with_items: "{{ existing_vms.value }}"
ignore_errors: true

- name: Remove the VM prepared by prepare_vmware_tests
vmware_guest:
name: "{{ item.name }}"
force: true
- name: Delete the VMs
vmware.vmware_rest.vcenter_vm:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
state: absent
with_items: '{{ virtual_machines + virtual_machines_in_cluster }}'
vm: '{{ item.vm }}'
with_items: "{{ existing_vms.value }}"
when:
- not item.name.startswith("vCLS")

- name: Remove the test_vm* VMs
vmware_guest:
name: "{{ item }}"
force: true
state: absent
with_items:
- test_vm1
- test_vm2
- test_vm3

- name: Remove the DVS portgroups
vmware_dvs_portgroup:
switch_name: "{{ dvswitch1 }}"
portgroup_name: '{{ item }}'
vlan_id: 0
num_ports: 32
portgroup_type: earlyBinding
state: absent
loop:
- DC0_DVPG0
- DVPG/1
ignore_errors: true
- name: Build a list of local libraries
vmware.vmware_rest.content_locallibrary_info:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
register: result
retries: 100
delay: 3
until: result is not failed

- name: Remove the DVSwitch
vmware_dvswitch:
datacenter_name: '{{ dc1 }}'
- name: Delete all the local libraries
vmware.vmware_rest.content_locallibrary:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
library_id: "{{ item.id }}"
state: absent
switch_name: '{{ item }}'
loop:
- '{{ dvswitch1 }}'
- dvswitch_0001
- dvswitch_0002
ignore_errors: true
with_items: "{{ result.value }}"

- name: Clean up the VM Network portgroup
vmware_portgroup:
esxi_hostname: '{{ esxi_hosts }}'
switch: isolated_vSwitch
portgroup: VM Network
state: absent
ignore_errors: true
with_items:
- isolated_vSwitch
- vSwitch0
- name: Build a list of subscribed libraries
vmware.vmware_rest.content_subscribedlibrary_info:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
register: result

- name: Remove the vSwitches
vmware_vswitch:
hostname: '{{ item }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
switch_name: "{{ switch1 }}"
- name: Delete all the subscribed libraries
vmware.vmware_rest.content_subscribedlibrary:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
library_id: "{{ item.id }}"
state: absent
with_items: "{{ esxi_hosts }}"
ignore_errors: true

- include_tasks: move_host_out_of_cluster.yml

- name: Umount NFS datastores from ESXi (1/2)
vmware_host_datastore:
hostname: '{{ item }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
datastore_name: '{{ ro_datastore }}'
state: absent
with_items: "{{ esxi_hosts }}"
ignore_errors: true
with_items: "{{ result.value }}"

- name: Umount NFS datastores from ESXi (2/2)
vmware_host_datastore:
hostname: '{{ item }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
datastore_name: '{{ rw_datastore }}'
state: absent
with_items: "{{ esxi_hosts }}"
ignore_errors: true

- name: Remove ESXi Hosts from vCenter
vmware_host:
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
esxi_hostname: '{{ item }}'
esxi_username: '{{ esxi_user }}'
esxi_password: '{{ esxi_password }}'
- name: Get a list of all the datacenters
vmware.vmware_rest.vcenter_datacenter_info:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
register: existing_datacenters
- name: Force delete the existing DC
vmware.vmware_rest.vcenter_datacenter:
vcenter_hostname: '{{ vcenter_hostname }}'
vcenter_username: '{{ vcenter_username }}'
vcenter_password: '{{ vcenter_password }}'
vcenter_validate_certs: false
state: absent
with_items: "{{ esxi_hosts }}"
ignore_errors: true
datacenter: '{{ item.datacenter }}'
force: true
with_items: "{{ existing_datacenters.value }}"
until: _result is succeeded
retries: 10
delay: 1
register: _result
25 changes: 22 additions & 3 deletions tests/integration/targets/vmware_guest/tasks/check_mode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
# Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Create VMs
vmware_guest:
datacenter: "{{ dc1 }}"
folder: '{{ f0 }}'
name: DC0_H0_VM0
state: poweredoff
guest_id: debian8_64Guest
disk:
- size_mb: 10
type: thin
datastore: '{{ rw_datastore }}'
hardware:
memory_mb: 128
num_cpus: 1
scsi: paravirtual
version: 11
cdrom:
type: iso
iso_path: "[{{ ro_datastore }}] fedora.iso"
networks:
- name: VM Network

- name: Perform all operation in check mode
vmware_guest:
validate_certs: false
Expand All @@ -27,9 +49,6 @@
register: check_mode_state
check_mode: true

- debug:
var: check_mode_state

- name: assert that changes were made
assert:
that:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@
# Copyright: (c) 2019, Pavan Bidkar <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Create VMs
vmware_guest:
datacenter: "{{ dc1 }}"
folder: '{{ f0 }}'
name: DC0_H0_VM0
state: poweredoff
guest_id: debian8_64Guest
disk:
- size_mb: 10
type: thin
datastore: '{{ rw_datastore }}'
hardware:
memory_mb: 128
num_cpus: 1
scsi: paravirtual
version: 11
cdrom:
type: iso
iso_path: "[{{ ro_datastore }}] fedora.iso"
networks:
- name: VM Network

- name: clone vm from template and customize GOS
vmware_guest:
validate_certs: false
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
template: "{{ virtual_machines[0].name }}"
template: DC0_H0_VM0
datacenter: "{{ dc1 }}"
state: poweredoff
folder: "{{ virtual_machines[0].folder }}"
folder: "{{ f0 }}"
convert: thin
register: clone_customize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@
# Copyright: (c) 2018, Christophe FERREIRA <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Create VMs
vmware_guest:
datacenter: "{{ dc1 }}"
folder: '{{ f0 }}'
name: DC0_H0_VM0
state: poweredoff
guest_id: debian8_64Guest
disk:
- size_mb: 10
type: thin
datastore: '{{ rw_datastore }}'
hardware:
memory_mb: 128
num_cpus: 1
scsi: paravirtual
version: 11
cdrom:
type: iso
iso_path: "[{{ ro_datastore }}] fedora.iso"
networks:
- name: VM Network

- name: clone vm from template and convert to thin
vmware_guest:
validate_certs: false
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: test_vm1
template: "{{ virtual_machines[0].name }}"
template: DC0_H0_VM0
datacenter: "{{ dc1 }}"
state: poweredoff
folder: "{{ virtual_machines[0].folder }}"
folder: "{{ f0 }}"
convert: thin
register: clone_thin

Expand Down
1 change: 0 additions & 1 deletion tests/integration/targets/vmware_guest/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
setup_datastore: true
setup_dvswitch: true
setup_resource_pool: true
setup_virtualmachines: true
setup_dvs_portgroup: true

- include_tasks: run_test_playbook.yml
Expand Down
Loading

0 comments on commit 836ba31

Please sign in to comment.