Skip to content

Commit

Permalink
modify provision_vm_test to fit eco CI
Browse files Browse the repository at this point in the history
  • Loading branch information
shellymiron committed Jun 9, 2024
1 parent 9bf53a7 commit 86f3b93
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 42 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ integration: install-python-packages install-ansible-collections

.PHONY: eco-vcenter-ci
eco-vcenter-ci: install-python-packages install-ansible-collections install-pyvmomy-latest
ansible-test integration --no-temp-workdir info_test
@for dir in $(shell ansible-test integration --list-target --no-temp-workdir | grep 'vmware_'); do \
ansible-test integration --no-temp-workdir $$dir; \
done

.PHONY: ee-clean
ee-clean:
Expand Down
9 changes: 7 additions & 2 deletions tests/integration/targets/init.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env bash
# shellcheck disable=SC2155,SC2086

# Export the collections path
export ANSIBLE_COLLECTIONS_PATH="$ANSIBLE_COLLECTIONS_PATH/ansible_collections"
export DEFAULT_COLLECTIONS_PATH="$ANSIBLE_COLLECTIONS_PATH/ansible_collections"

# Check if the variable is already set (e.g., in CI)
if [ -z "$ANSIBLE_COLLECTIONS_PATH" ]; then
# If not, use base collections path
ANSIBLE_COLLECTIONS_PATH="$DEFAULT_COLLECTIONS_PATH"
fi

echo "ANSIBLE_COLLECTIONS_PATH: $ANSIBLE_COLLECTIONS_PATH"
BASE_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
Expand Down
13 changes: 0 additions & 13 deletions tests/integration/targets/provision_vm_test/run.yml

This file was deleted.

3 changes: 0 additions & 3 deletions tests/integration/targets/provision_vm_test/runme.sh

This file was deleted.

22 changes: 0 additions & 22 deletions tests/integration/targets/provision_vm_test/tasks/main.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

- name: Import info role
ansible.builtin.import_role:
name: info_test
name: vmware_info_test
tags:
- eco-vcenter-ci
- integration-ci
27 changes: 27 additions & 0 deletions tests/integration/targets/vmware_provision_vm_test/run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- hosts: localhost
gather_facts: no
collections:
- community.general

tasks:
- name: Import eco-vcenter credentials
ansible.builtin.include_vars:
file: ../../integration_config.yml
tags: eco-vcenter-ci

- name: Import simulator vars
ansible.builtin.include_vars:
file: vars.yml
tags: integration-ci

- name: Vcsim
ansible.builtin.import_role:
name: prepare_soap
tags: integration-ci

- name: Import provision VM role
ansible.builtin.import_role:
name: vmware_provision_vm_test
tags:
- eco-vcenter-ci
- integration-ci
14 changes: 14 additions & 0 deletions tests/integration/targets/vmware_provision_vm_test/runme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
source ../init.sh

# Extract the ansible_tags from integration_config.yml
ANSIBLE_TAGS=$(awk '/ansible_tags/ {print $2}' ../../integration_config.yml)

# Check if the ANSIBLE_TAGS variable is set
if [[ -n "$ANSIBLE_TAGS" ]]; then
echo "ANSIBLE_TAGS is set to: $ANSIBLE_TAGS"
exec ansible-playbook run.yml --tags "$ANSIBLE_TAGS"
else
echo "ANSIBLE_TAGS is not set for Eco vCenter. Running on simulator."
exec ansible-playbook run.yml --tags integration-ci
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Deprovision VM {{ item.provision_vm_name }}
ansible.builtin.include_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ item.provision_vm_name }}"
provision_vm_state: "absent"
provision_vm_force: true

- name: "Check that the following VM does not exist: {{ item.provision_vm_name }}"
community.vmware.vmware_vm_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ provision_vm_validate_certs }}"
vm_name: "{{ item.provision_vm_name }}"
register: vm_info
ignore_errors: true

- name: "Fail the task if the following VM exists: {{ item.provision_vm_name }}"
ansible.builtin.fail:
msg: "Provisioned VM {{ item.provision_vm_name }} still exists"
when: not vm_info.failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: "Check VM existence: {{ vm_name }}"
community.vmware.vmware_vm_info:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ provision_vm_validate_certs }}"
vm_name: "{{ vm_name }}"
register: vm_info

- name: Fail the task if the VM doesn't exist
ansible.builtin.fail:
msg: "Provisioned VM does not exist"
when: vm_info is not defined or vm_info.failed
36 changes: 36 additions & 0 deletions tests/integration/targets/vmware_provision_vm_test/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Provision VM for simulator
ansible.builtin.import_role:
name: cloud.vmware_ops.provision_vm
when: "'integration-ci' in ansible_run_tags"

- name: Perform VM Lifecycle Operations in vCenter Environment
block:
- name: Provision multiple VMs
loop: "{{ provision_vms }}"
ansible.builtin.include_tasks: provision_multiple_vms.yml

- name: Turn one of the provisioned VMs into template
loop: "{{ provision_vms_template }}"
community.vmware.vmware_guest:
hostname: "{{ provision_vm_hostname }}"
username: "{{ provision_vm_username }}"
password: "{{ provision_vm_password }}"
validate_certs: "{{ provision_vm_validate_certs }}"
name: "{{ item.provision_vm_name }}"
is_template: true
folder: "{{ provision_vm_folder }}"
datacenter: "{{ provision_vm_datacenter }}"

- name: Create a VM from the template
loop: "{{ provision_vms_from_template }}"
ansible.builtin.include_tasks: provision_multiple_vms.yml

- name: Update one the provisioned VMs
ansible.builtin.include_tasks: update_vm.yml

always:
- name: Cleanup VMs and template from the vcenter env
loop: "{{ provision_vms + provision_vms_template + provision_vms_from_template }}"
ansible.builtin.include_tasks: cleanup_vms.yml
when: "'eco-vcenter-ci' in ansible_run_tags"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Run Provision VM Role
ansible.builtin.include_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ item.provision_vm_name }}"
provision_vm_state: "{{ item.provision_vm_state | default(omit) }}"
provision_vm_cdrom: "{{ item.provision_vm_cdrom | default(omit) }}"
provision_vm_networks: "{{ item.provision_vm_networks | default(omit) }}"
provision_vm_esxi_hostname: "{{ item.provision_vm_esxi_hostname | default(omit) }}"
provision_vm_resource_pool: "{{ item.provision_vm_resource_pool | default(omit) }}"
provision_vm_port: "{{ item.provision_vm_port | default(omit) }}"
provision_vm_disk: "{{ item.provision_vm_disk | default(omit) }}"
provision_vm_hardware: "{{ item.provision_vm_hardware | default(omit) }}"
provision_vm_guest_id: "{{ item.provision_vm_guest_id | default(omit) }}"
provision_vm_datastore: "{{ item.provision_vm_datastore | default(omit) }}"
provision_vm_template: "{{ item.provision_vm_template | default(omit) }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
- name: Get info about the VM to be updated
ansible.builtin.include_tasks:
file: get_vm_info.yml
vars:
vm_name: "{{ vm_update_name }}"

- name: Set vm_uuid variable
ansible.builtin.set_fact:
vm_uuid: "{{ vm_info.virtual_machines[0].uuid }}"

- name: Set vm_moid variable
ansible.builtin.set_fact:
vm_moid: "{{ vm_info.virtual_machines[0].moid }}"

- name: Update the name of an existing VM with UUID {{ vm_uuid }}
loop: "{{ vm_names_to_update }}"
ansible.builtin.include_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_uuid: "{{ vm_uuid }}"
provision_vm_name: "{{ item }}"

- name: "Update the state of an existing VM to: {{ vm_update_name }}"
loop: "{{ vm_states }}"
ansible.builtin.include_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_state: "{{ item }}"
provision_vm_force: true

- name: Add port group with network label {{ portgroup_name }}
community.vmware.vmware_portgroup:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ provision_vm_validate_certs }}"
cluster_name: "{{ provision_vm_cluster }}"
switch: "{{ vswitch_name }}"
portgroup: "{{ portgroup_name }}"

- name: Update networks of existing VM {{ vm_update_name }}
ansible.builtin.import_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_networks: "{{ vm_updated_networks }}"

- name: Update VM hardware properties
ansible.builtin.import_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_hardware: "{{ vm_updated_hardware }}"

- name: Enlarge existing disk of existing VM {{ vm_update_name }}
ansible.builtin.import_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_disk: "{{ vm_enlarge_disk }}"

- name: Update guest_id of existing VM {{ vm_update_name }}
ansible.builtin.import_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_guest_id: "{{ vm_updated_guest_id }}"
provision_vm_cdrom: "{{ vm_updated_cdrom }}"

- name: Power on the updated VM {{ vm_update_name }}
loop:
- "poweredon"
ansible.builtin.include_role:
name: cloud.vmware_ops.provision_vm
vars:
provision_vm_name: "{{ vm_update_name }}"
provision_vm_state: "{{ item }}"
provision_vm_force: true
17 changes: 17 additions & 0 deletions tests/integration/targets/vmware_provision_vm_test/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
provision_vm_hostname: "127.0.0.1"
provision_vm_username: "test"
provision_vm_password: "test"
provision_vm_validate_certs: false
provision_vm_cluster: "DC0_C0"
provision_vm_folder: "/DC0/vm"
provision_vm_datacenter: "DC0"
provision_vm_name: "vm-test"
provision_vm_port: "8989"
provision_vm_disk:
- size_gb: 10
type: thin
datastore: "LocalDS_0"
provision_vm_hardware:
memory_mb: 512
num_cpus: 4
provision_vm_guest_id: "centos64Guest"
Loading

0 comments on commit 86f3b93

Please sign in to comment.