Add cleanup functionality

This PS adds cleanup functionality to the example playbooks and to the
tests. This is so a dev doesn't have to manually cleanup artifacts left
by running the playbooks. Not meant to be run in the Zuul gate.

Change-Id: Icb99d2914800243112cd9cf53bcee6657f455fe4
This commit is contained in:
Doug Aaser 2020-01-21 13:53:36 -05:00
parent 3b7fdd8f6b
commit 6e98f3a824
16 changed files with 284 additions and 0 deletions

View File

@ -0,0 +1,34 @@
- hosts: primary
roles:
- role: libvirt-network
vars:
network_action: cleanup
libvirt_network:
name: provision-network
spec:
forward:
mode: nat
nat:
port:
- start: 1024
end: 65535
bridge:
name: "prov-net-br"
stp: 'on'
delay: '0'
ip:
address: "172.22.0.1"
netmask: "255.255.255.0"
- role: libvirt-network
vars:
network_action: cleanup
libvirt_network:
name: oob-net
spec:
bridge:
name: oob-net
stp: 'on'
delay: '0'
ip:
address: "10.23.22.1"
netmask: "255.255.255.0"

View File

@ -0,0 +1,9 @@
- hosts: primary
roles:
- role: libvirt-pool
become: true
vars:
pool_action: cleanup
libvirt_pool:
path: /var/lib/libvirt/airship
name: airship

View File

@ -0,0 +1,33 @@
- hosts: primary
tasks:
- name: Cleanup defined VMs
include_role:
name: libvirt-domain
tasks_from: cleanup
with_items:
- state: running
name: 'vm1'
memory_mb: 512
vcpus: 1
volumes:
- name: 'volume-1'
device: 'disk'
format: 'qcow2'
pool: 'airship'
interfaces:
- network: 'provision-network'
- state: running
name: 'vm2'
memory_mb: 512
vcpus: 1
volumes:
- name: 'volume-2'
device: 'disk'
format: 'qcow2'
pool: 'airship'
interfaces:
- network: 'provision-network'
vars:
libvirt_domain: "{{ vm }}"
loop_control:
loop_var: vm

View File

@ -0,0 +1,17 @@
- hosts: primary
tasks:
- name: Create defined volumes
include_role:
name: libvirt-volume
with_items:
- name: volume-1
pool: airship
action: cleanup
- name: volume-2
pool: airship
action: cleanup
vars:
libvirt_volume: "{{ vol }}"
volume_action: "{{ vol.action }}"
loop_control:
loop_var: vol

View File

@ -0,0 +1,5 @@
- hosts: primary
roles:
- role: redfish-emulator
vars:
redfish_action: cleanup

View File

@ -0,0 +1,17 @@
- name: Get VM status
virt:
name: "{{ libvirt_domain.name }}"
command: status
register: vm_stat
- name: Destroy VM
virt:
name: "{{ libvirt_domain.name }}"
command: destroy
when:
- "vm_stat.status != 'shutdown'"
- name: Undefine VM
virt:
name: "{{ libvirt_domain.name }}"
command: undefine

View File

@ -0,0 +1,9 @@
- name: Include test variables.
include_vars:
file: vars.yml
- name: Delete existing VMs
include_role:
name: libvirt-domain
tasks_from: cleanup

View File

@ -0,0 +1,24 @@
- name: Validate input
assert:
that:
- "libvirt_network.name is defined"
- name: Get network info
virt_net:
command: list_nets
name: "{{ libvirt_network.name }}"
register: net
- name: Delete network
block:
- name: Stop network
virt_net:
command: destroy
name: "{{ libvirt_network.name }}"
- name: Remove network
virt_net:
command: undefine
name: "{{ libvirt_network.name }}"
when:
- "libvirt_network.name in net.list_nets"

View File

@ -0,0 +1,12 @@
- name: Include test variables.
include_vars:
file: vars.yml
- name: Clean networks
include_role:
name: libvirt-network
with_items: "{{ libvirt_networks }}"
loop_control:
loop_var: libvirt_network
vars:
network_action: cleanup

View File

@ -1,9 +1,11 @@
- name: Include test variables.
include_vars:
file: vars.yml
- name: install libvirt
include_role:
name: libvirt-install
- name: create networks
include_role:
name: libvirt-network
@ -12,12 +14,14 @@
loop_var: libvirt_network
vars:
network_action: "{{ libvirt_network.network_action }}"
- name: install required packages
apt:
name:
- bridge-utils
state: present
become: true
- name: gather network info
virt_net:
command: info

View File

@ -0,0 +1,23 @@
- name: Get pool information
virt_pool:
command: info
register: storage_pools
- name: Delete libvirt storage pool
block:
- name: Ensure libvirt storage pools are destroyed
virt_pool:
name: "{{ libvirt_pool.name }}"
command: destroy
- name: Ensure libvirt storage pools are deleted
virt_pool:
name: "{{ libvirt_pool.name }}"
command: delete
- name: Ensure libvirt storage pools are undefined
virt_pool:
name: "{{ libvirt_pool.name }}"
command: undefine
when:
- "libvirt_pool.name in storage_pools.pools"

View File

@ -0,0 +1,31 @@
- name: Include test variables.
include_vars:
file: vars.yml
- name: Delete defined volumes
include_role:
name: libvirt-volume
with_items: "{{ libvirt_volumes }}"
vars:
libvirt_volume: "{{ vol }}"
volume_action: cleanup
loop_control:
loop_var: vol
when:
- "libvirt_volumes is defined"
- name: Delete defined pools
include_role:
name: libvirt-pool
vars:
pool_action: cleanup
- name: Get pool information
virt_pool:
command: info
register: storage_pools
- name: Ensure pool has been deleted
assert:
that:
- "storage_pools.pools.{{ libvirt_pool.name }} is not defined"

View File

@ -1,4 +1,22 @@
- name: Clean up cache directory
become: true
file:
path: "{{ libvirt_image_cache_path }}"
state: absent
- name: Remove volumes from pool
environment:
LIBVIRT_DEFAULT_URI: qemu:///system
command: >-
virsh vol-delete \
--pool "{{ libvirt_volume.pool }}" \
"{{ libvirt_volume.name }}"
register: libvirt_delete_volume
failed_when:
- "libvirt_delete_volume.rc != 0"
- "'not found' not in libvirt_delete_volume.stderr"
- "'not found' not in libvirt_delete_volume.stdout"
changed_when:
- "libvirt_delete_volume.rc != 1"
- "'not found' not in libvirt_delete_volume.stderr"
- "'not found' not in libvirt_delete_volume.stdout"

View File

@ -0,0 +1,26 @@
- name: Include test variables.
include_vars:
file: vars.yml
- name: Delete defined volumes
include_role:
name: libvirt-volume
with_items: "{{ libvirt_volumes }}"
vars:
libvirt_volume: "{{ vol }}"
volume_action: cleanup
loop_control:
loop_var: vol
- name: Get pool information
virt_pool:
command: info
register: libvirt_pool_list
- name: Verify volumes don't exist
assert:
that:
- "vol.name not in libvirt_pool_list"
with_items: "{{ libvirt_volumes }}"
loop_control:
loop_var: vol

View File

@ -0,0 +1,6 @@
- name: Stop sushy-emulator
become: yes
service:
name: sushy-tools
state: stopped
enabled: false

16
tests/ansible/cleanup.yml Normal file
View File

@ -0,0 +1,16 @@
---
- hosts: primary
tasks:
- name: set default roles
set_fact:
test_subject_roles_default:
- libvirt-network
- libvirt-pool
- libvirt-volume
- libvirt-domain
- redfish-emulator
- name: run tests against defined roles
include_tasks: "../../roles/{{ role_name }}/tests/cleanup.yml"
with_items: "{{ test_subject_roles | default(test_subject_roles_default) }}"
loop_control:
loop_var: role_name