From 1ad5ae84a9101f6fda756b13a4128a86092067fb Mon Sep 17 00:00:00 2001 From: anna-savina Date: Tue, 18 Jun 2024 15:55:35 +0300 Subject: [PATCH] Modified vcenter_host_connection_test --- .../fragments/66_vcenter-host-connection.yml | 3 + .../vcenter_host_connection_test/run.yml | 17 --- .../vcenter_host_connection_test/runme.sh | 3 - .../tasks/main.yml | 12 --- .../run.yml | 32 ++++++ .../tasks/main.yml | 100 ++++++++++++++++++ .../tasks/manage_resource_pool.yml | 11 ++ .../tasks/provision_esxi.yml | 48 +++++++++ .../vars.yml} | 2 +- .../vars/main.yml | 53 ++++++++++ 10 files changed, 248 insertions(+), 33 deletions(-) create mode 100644 changelogs/fragments/66_vcenter-host-connection.yml delete mode 100644 tests/integration/targets/vcenter_host_connection_test/run.yml delete mode 100755 tests/integration/targets/vcenter_host_connection_test/runme.sh delete mode 100644 tests/integration/targets/vcenter_host_connection_test/tasks/main.yml create mode 100644 tests/integration/targets/vmware_ops_vcenter_host_connection_test/run.yml create mode 100644 tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/main.yml create mode 100644 tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/manage_resource_pool.yml create mode 100644 tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/provision_esxi.yml rename tests/integration/targets/{vcenter_host_connection_test/vars/main.yml => vmware_ops_vcenter_host_connection_test/vars.yml} (89%) create mode 100644 tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars/main.yml diff --git a/changelogs/fragments/66_vcenter-host-connection.yml b/changelogs/fragments/66_vcenter-host-connection.yml new file mode 100644 index 00000000..8d7cf7a8 --- /dev/null +++ b/changelogs/fragments/66_vcenter-host-connection.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - vcenter_host_connection_test - modified vcenter_host_connection_test to run on a real vcenter environment \ No newline at end of file diff --git a/tests/integration/targets/vcenter_host_connection_test/run.yml b/tests/integration/targets/vcenter_host_connection_test/run.yml deleted file mode 100644 index 3926e500..00000000 --- a/tests/integration/targets/vcenter_host_connection_test/run.yml +++ /dev/null @@ -1,17 +0,0 @@ -- hosts: localhost - gather_facts: no - collections: - - community.general - - tasks: - - name: Vcsim - ansible.builtin.import_role: - name: prepare_soap - - - name: ESXi - ansible.builtin.import_role: - name: prepare_esx - - - name: Import vcenter_host_connection_test - ansible.builtin.import_role: - name: vcenter_host_connection_test diff --git a/tests/integration/targets/vcenter_host_connection_test/runme.sh b/tests/integration/targets/vcenter_host_connection_test/runme.sh deleted file mode 100755 index a4c36631..00000000 --- a/tests/integration/targets/vcenter_host_connection_test/runme.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -source ../init.sh -exec ansible-playbook run.yml diff --git a/tests/integration/targets/vcenter_host_connection_test/tasks/main.yml b/tests/integration/targets/vcenter_host_connection_test/tasks/main.yml deleted file mode 100644 index 7a549a63..00000000 --- a/tests/integration/targets/vcenter_host_connection_test/tasks/main.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Add ESXI Host - ansible.builtin.import_role: - name: cloud.vmware_ops.vcenter_host_connection - vars: - vcenter_host_connection_state: present - -- name: Remove ESXI Host - ansible.builtin.import_role: - name: cloud.vmware_ops.vcenter_host_connection - vars: - vcenter_host_connection_state: absent diff --git a/tests/integration/targets/vmware_ops_vcenter_host_connection_test/run.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/run.yml new file mode 100644 index 00000000..da6e18b3 --- /dev/null +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/run.yml @@ -0,0 +1,32 @@ +- 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: ESXi + ansible.builtin.import_role: + name: prepare_esx + tags: integration-ci + + - name: Import vcenter_host_connection_test + ansible.builtin.include_role: + name: vmware_ops_vcenter_host_connection_test + tags: + - eco-vcenter-ci + - integration-ci diff --git a/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/main.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/main.yml new file mode 100644 index 00000000..c81a6b54 --- /dev/null +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/main.yml @@ -0,0 +1,100 @@ +--- +- name: Test vCenter host connection role on vCenter env + tags: eco-vcenter-ci + block: + - name: Add Resource Pool + ansible.builtin.include_tasks: manage_resource_pool.yml + vars: + required_state: present + + - name: Provision ESXI VM and set ESXI hostname + ansible.builtin.include_tasks: provision_esxi.yml + + - name: Add ESXI Host to vCenter Cluster + ansible.builtin.import_role: + name: cloud.vmware_ops.vcenter_host_connection + vars: + vcenter_host_connection_state: present + + - name: Gather ESXI Host facts + community.vmware.vmware_host_facts: + hostname: "{{ vcenter_host_connection_hostname }}" + username: "{{ vcenter_host_connection_username }}" + password: "{{ vcenter_host_connection_password }}" + validate_certs: false + esxi_hostname: "{{ vcenter_host_connection_esxi_hostname }}" + register: _host_facts + + - name: Verify ESXI Host connected to vCenter Cluster + ansible.builtin.assert: + that: + - _host_facts.ansible_facts.cluster == cluster_name + - _host_facts.ansible_facts.ansible_host_connection_state == 'connected' + + - name: Remove ESXI Host from vCenter Cluster + ansible.builtin.import_role: + name: cloud.vmware_ops.vcenter_host_connection + vars: + vcenter_host_connection_state: absent + + - name: Ensure ESXI Host removed from vCenter Cluster + block: + - name: Gather ESXI Host facts + community.vmware.vmware_host_facts: + hostname: "{{ vcenter_host_connection_hostname }}" + username: "{{ vcenter_host_connection_username }}" + password: "{{ vcenter_host_connection_password }}" + validate_certs: false + esxi_hostname: "{{ vcenter_host_connection_esxi_hostname }}" + register: _no_host_facts + + - name: Force failure if ESXI Host was found + ansible.builtin.fail: + msg: "'{{ vcenter_host_connection_esxi_hostname }}' host exists, collected info is '{{ _no_host_facts }}'" + + rescue: + - name: ESXI Host doesn't exist as expected + when: _no_host_facts.msg is contains 'not found' + ansible.builtin.debug: + msg: ESXI Host doesn't exist as expected + + always: + - name: Remove ESXI Host from vCenter Cluster to cleanup env + community.vmware.vmware_host: + hostname: "{{ vcenter_host_connection_hostname }}" + username: "{{ vcenter_host_connection_username }}" + password: "{{ vcenter_host_connection_password }}" + datacenter: "{{ vcenter_host_connection_datacenter }}" + cluster: "{{ vcenter_host_connection_cluster }}" + validate_certs: false + esxi_hostname: "{{ vcenter_host_connection_esxi_hostname }}" + esxi_username: "{{ vcenter_host_connection_esxi_username }}" + esxi_password: "{{ vcenter_host_connection_esxi_password }}" + state: absent + + - name: Remove ESXI VM to cleanup env + ansible.builtin.include_role: + name: cloud.vmware_ops.provision_vm + vars: + provision_vm_state: absent + provision_vm_force: true + + - name: Remove Resource Pool to cleanup env + ansible.builtin.include_tasks: manage_resource_pool.yml + vars: + required_state: absent + +- name: Test vCenter host connection role on simulator + tags: integration-ci + block: + - name: Add ESXI Host to vCenter Cluster + ansible.builtin.import_role: + name: cloud.vmware_ops.vcenter_host_connection + vars: + vcenter_host_connection_state: present + + - name: Remove ESXI Host from vCenter Cluster + ansible.builtin.import_role: + name: cloud.vmware_ops.vcenter_host_connection + vars: + vcenter_host_connection_state: absent diff --git a/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/manage_resource_pool.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/manage_resource_pool.yml new file mode 100644 index 00000000..dad64dda --- /dev/null +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/manage_resource_pool.yml @@ -0,0 +1,11 @@ +--- +- name: Manage resource pool + community.vmware.vmware_resource_pool: + hostname: "{{ provision_virtual_esxi_hostname }}" + username: "{{ provision_virtual_esxi_username }}" + password: "{{ provision_virtual_esxi_password }}" + datacenter: "{{ provision_virtual_esxi_datacenter }}" + validate_certs: false + cluster: "{{ provision_virtual_esxi_cluster }}" + resource_pool: "{{ provision_virtual_esxi_resource_pool }}" + state: "{{ required_state }}" diff --git a/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/provision_esxi.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/provision_esxi.yml new file mode 100644 index 00000000..194dac72 --- /dev/null +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/tasks/provision_esxi.yml @@ -0,0 +1,48 @@ +--- +- name: Provision Virtual ESXI + ansible.builtin.import_role: + name: cloud.vmware_ops.provision_virtual_esxi + +- name: Wait until ESXI VM is powered off after OS installation + community.vmware.vmware_guest_info: + hostname: "{{ provision_virtual_esxi_hostname }}" + username: "{{ provision_virtual_esxi_username }}" + password: "{{ provision_virtual_esxi_password }}" + datacenter: "{{ provision_virtual_esxi_datacenter }}" + validate_certs: false + name: "{{ provision_virtual_esxi_vms[0].name }}" + register: _esxi_power_status + until: _esxi_power_status.instance.hw_power_status == 'poweredOff' + retries: 30 + delay: 20 + +- name: Power on ESXI VM after provisioning + ansible.builtin.include_role: + name: cloud.vmware_ops.provision_vm + vars: + provision_vm_state: poweredon + +- name: Wait until ESXI VM has an IP address + community.vmware.vmware_guest_info: + hostname: "{{ provision_virtual_esxi_hostname }}" + username: "{{ provision_virtual_esxi_username }}" + password: "{{ provision_virtual_esxi_password }}" + datacenter: "{{ provision_virtual_esxi_datacenter }}" + validate_certs: false + name: "{{ provision_virtual_esxi_vms[0].name }}" + register: esxi_host_info + until: esxi_host_info.instance.ipv4 is defined and esxi_host_info.instance.ipv4 is not none + retries: 30 + delay: 10 + +- name: Check if ESXI is up + wait_for: + host: "{{ esxi_host_info.instance.ipv4 }}" + port: 22 + delay: 10 + timeout: 300 + state: started + +- name: Set ESXI hostname + ansible.builtin.set_fact: + vcenter_host_connection_esxi_hostname: "{{ esxi_host_info.instance.ipv4 }}" diff --git a/tests/integration/targets/vcenter_host_connection_test/vars/main.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars.yml similarity index 89% rename from tests/integration/targets/vcenter_host_connection_test/vars/main.yml rename to tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars.yml index 783b2b57..20c4996a 100644 --- a/tests/integration/targets/vcenter_host_connection_test/vars/main.yml +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars.yml @@ -9,4 +9,4 @@ vcenter_host_connection_datacenter: "DC0" vcenter_host_connection_esxi_hostname: "127.0.0.1" vcenter_host_connection_esxi_username: test -vcenter_host_connection_esxi_password: test +vcenter_host_connection_esxi_password: test \ No newline at end of file diff --git a/tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars/main.yml b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars/main.yml new file mode 100644 index 00000000..e30d46a0 --- /dev/null +++ b/tests/integration/targets/vmware_ops_vcenter_host_connection_test/vars/main.yml @@ -0,0 +1,53 @@ +--- +# General +cluster_name: "Eco-Cluster" +vcenter_datacenter: "Eco-Datacenter" +resource_pool_name: "host-connection-test-pool" +esxi_username: "root" +esxi_password: "!#%135qEt" +network_name: "TestNetwork" + +# Vars for cloud.vmware_ops.provision_virtual_esxi role +provision_virtual_esxi_hostname: "{{ vcenter_hostname }}" +provision_virtual_esxi_username: "{{ vcenter_username }}" +provision_virtual_esxi_password: "{{ vcenter_password }}" +provision_virtual_esxi_validate_certs: false +provision_virtual_esxi_port: 443 +provision_virtual_esxi_cluster: "{{ cluster_name }}" +provision_virtual_esxi_datacenter: "{{ vcenter_datacenter }}" +provision_virtual_esxi_folder: "" +provision_virtual_esxi_resource_pool: "{{ resource_pool_name }}" +provision_virtual_esxi_datastore_iso_path: "[eco-nfs-datastore-iso] custom_esxi_8.iso" +provision_virtual_esxi_vms: + - name: "vm-host-connection-test" +provision_virtual_esxi_networks: + - name: "{{ network_name }}" + device_type: "vmxnet3" + type: "dhcp" + mac: "00:50:56:8b:ea:f0" +provision_virtual_esxi_disks: + - size_gb: 100 + type: thin + datastore: datastore1 + +# Vars for cloud.vmware_ops.vcenter_host_connection role +vcenter_host_connection_hostname: "{{ vcenter_hostname }}" +vcenter_host_connection_username: "{{ vcenter_username }}" +vcenter_host_connection_password: "{{ vcenter_password }}" +vcenter_host_connection_validate_certs: false +vcenter_host_connection_port: 443 +vcenter_host_connection_cluster: "{{ cluster_name }}" +vcenter_host_connection_datacenter: "{{ vcenter_datacenter }}" +vcenter_host_connection_esxi_hostname: "" +vcenter_host_connection_esxi_username: "{{ esxi_username }}" +vcenter_host_connection_esxi_password: "{{ esxi_password }}" + +# Var for cloud.vmware_ops.provision_vm role +provision_vm_hostname: "{{ provision_virtual_esxi_hostname }}" +provision_vm_username: "{{ provision_virtual_esxi_username }}" +provision_vm_password: "{{ provision_virtual_esxi_password }}" +provision_vm_validate_certs: false +provision_vm_port: "{{ provision_virtual_esxi_port }}" +provision_vm_cluster: "{{ provision_virtual_esxi_cluster }}" +provision_vm_datacenter: "{{ provision_virtual_esxi_datacenter }}" +provision_vm_name: "{{ provision_virtual_esxi_vms[0].name }}"