Skip to content

Commit

Permalink
adding molecule e2e tests scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
shellymiron committed Mar 17, 2024
1 parent 3d35896 commit 6a05a40
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 0 deletions.
500 changes: 500 additions & 0 deletions README.md

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions tests/e2e/molecule/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# cloud.vmware_ops Molecule E2E Testing

This repository contains Molecule test scenarios for the `cloud.vmware_ops` Ansible collection. Each scenario represents a feature in the collection, allowing you to test individual features in isolation.
Feel free to contribute, add new scenarios, or enhance existing ones to ensure comprehensive testing of the Ansible collection.

## Prerequisites

Before running Molecule scenarios, ensure that the following tools are installed on your system:

- [Ansible](https://www.ansible.com/)
- [Python 3](https://www.python.org/)
- [Molecule](https://molecule.readthedocs.io/)
- [ansible-junit](https://github.com/hspaans/ansible-junit) (for JUnit XML output)
- [pyvmomi](https://github.com/vmware/pyvmomi) (required for vSphere testing)

Additionally, create a password file for the vSphere credentials (vault password file). The password file should contain the vault password for the `vSphere creds` vault. The vault file should include the following variables:

```yaml
# vault.yml
vault_vsphere_hostname: "your_vsphere_hostname"
vault_vsphere_username: "your_vsphere_username"
vault_vsphere_password: "your_vsphere_password"

## Creating a New Molecule Scenario
To create a new Molecule scenario, follow these steps:

1. Create a new directory under tests/e2e/molecule/provision_vm/scenarios/ for your scenario.
2. Define the scenario configuration in the molecule.yml file within the new scenario directory.
3. Create the necessary playbook(s) and test files for your scenario.
4. Run the scenario using molecule test -s <new_scenario_name>.

## Running Molecule Scenarios
To run a Molecule scenario, use the following command:
molecule test -s <scenario_name>
2 changes: 2 additions & 0 deletions tests/e2e/molecule/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
callback_whitelist = junit
20 changes: 20 additions & 0 deletions tests/e2e/molecule/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: Run all playbooks in the scenarios directory
hosts: localhost
gather_facts: true

tasks:
- name: Include vSphere datacenter creds
ansible.builtin.include_vars:
file: vault_files/vsphere_creds.yml

- name: Run provision_vm scenarios
ansible.builtin.find:
paths: "{{ lookup('env', 'MOLECULE_SCENARIO_NAME') }}/scenarios"
recurse: true
patterns: "*.yml"
register: playbook_files

- name: Include and run each playbook
ansible.builtin.include_tasks:
file: "{{ item.path }}"
with_items: "{{ playbook_files.files }}"
37 changes: 37 additions & 0 deletions tests/e2e/molecule/provision_vm/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
dependency:
name: galaxy

# driver:
# name: docker

platforms:
- name: instance
image: geerlingguy/docker-ubuntu2004-ansible:latest
command: /lib/systemd/systemd
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
ansible_connection: ansible

provisioner:
name: ansible
lint: |
set -e
echo "Run yamllint"
yamllint .
echo "Run ansible-lint"
ansible-lint
env:
ANSIBLE_ROLES_PATH: "../../../../roles"
config_options:
defaults:
vault_password_file: ${HOME}/vsphere_crds_vault_pass.txt
callback_whitelist: junit
playbooks:
converge: ../converge.yml

verifier:
name: ansible
options:
junit-xml: ./test-results.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: Verify Provisioned VM
ansible.builtin.include_vars:
file: ../vars/vars.yml

- name: Check VM existence
community.vmware.vmware_vm_info:
hostname: "{{ provision_vm_hostname }}"
username: "{{ provision_vm_username }}"
password: "{{ provision_vm_password }}"
validate_certs: "{{ provision_vm_validate_certs }}"
vm_name: "{{ provision_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

- name: Validate VM properties
ansible.builtin.assert:
that:
- vm_info.virtual_machines[0].guest_name == provision_vm_name
- vm_info.virtual_machines[0].cluster == provision_vm_cluster
- vm_info.virtual_machines[0].datacenter == provision_vm_datacenter
- vm_info.virtual_machines[0].folder == provision_vm_folder
# - vm_info.virtual_machines[0].power_state == provision_vm_state
- vm_info.virtual_machines[0].resource_pool == provision_vm_resource_pool
- vm_info.virtual_machines[0].datastore_url[0].name == provision_vm_disk[0].datastore
13 changes: 13 additions & 0 deletions tests/e2e/molecule/provision_vm/scenarios/basic_provision_vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- name: Basic Provision VM Scenario
block:
- name: Import vars
ansible.builtin.include_vars:
file: ../vars/vars.yml

- name: Provision a VM
ansible.builtin.import_role:
name: provision_vm

- name: Verify Provisioned VM
ansible.builtin.include_tasks:
file: ../post_validations/verify_vm_post_provisioning.yml
19 changes: 19 additions & 0 deletions tests/e2e/molecule/provision_vm/testinfra/sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import testinfra
import pytest

@pytest.fixture
def host(request):
# Create the Testinfra host using the Ansible inventory
print("the host :: ", testinfra.get_host(f'ansible://{request.config.getoption("--ansible-inventory")}'))
return testinfra.get_host(f'ansible://{request.config.getoption("--ansible-inventory")}')

def test_vm_os(host):
# Use Testinfra to check the properties of the VM
distribution = host.system_info.distribution
release = host.system_info.release
print("release :: ", release)
version = host.system_info.release

# Assert the expected OS type
assert distribution.lower() == 'linux'
assert version.startswith('7.') # Adjust this based on your expected OS version
40 changes: 40 additions & 0 deletions tests/e2e/molecule/provision_vm/vars/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# vars.yaml
provision_vm_validate_certs: false
provision_vm_cluster: "nestedcluster"
provision_vm_folder: "/nesteddatacenter/vm"
provision_vm_datacenter: "nesteddatacenter"
provision_vm_name: "smiron-provision-vm-2"
provision_vm_state: "poweredon"
provision_vm_cdrom:
- controller_number: 0
unit_number: 0
state: present
type: iso
iso_path: "[Datastore-host1] rhel-9.3-x86_64-dvd.iso"
provision_vm_networks:
- name: "Network1"
device_type: "vmxnet3"
mac: "00:50:56:bd:d2:9e"
type: "dhcp"
# ip: "192.168.168.10"
# netmask: "255.255.255.0"
# gateway: "10.185.246.1"
# provision_vm_esxi_hostname: "10.185.246.5"
# provision_vm_state:
# poweredOff: "poweredoff"
# poweredOn: "poweredon"
# suspended: "suspended"
provision_vm_resource_pool: null
# provision_vm_port: "8989"
provision_vm_disk:
- size_gb: 50
type: thin
datastore: "Datastore-host1"
provision_vm_hardware:
memory_mb: 2000
num_cpus: 4
boot_firmware: efi
secure_boot: true
# provision_vm_guest_id: "rhel9_64Guest"
provision_vm_datastore: "Datastore-host1"
provision_vm_template: "provision-vm-template"
14 changes: 14 additions & 0 deletions tests/e2e/molecule/vault_files/vsphere_creds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$ANSIBLE_VAULT;1.1;AES256
62353361643765386533316139656438306166613765336366376563313666666538663838306336
3335623861613631303431623730366564663635643863640a396435306332373639326232643531
63356366366232623830383762653230626139343731366466623262626132623736663732366562
3237326363346263360a623535353534343365663431616537663634353563383639313334343463
35353637373237386133633234383939356537316632376139613366376132323436306464313439
34373939333930353931356432356361376437383338313732663866373663623865613166626632
36306564336464346164656431363865633465613864373231303538333935356434353966383164
65663566613333396435363866623066623530306662646162353665613034663434336261313865
36353738396334633263343766623736343362393765333030386563343563393138363035373135
31613839636538373339346266333863336139363534393339383361376232303939313036633263
66396438613030613433376334663637616566326566643162363430326239316334623534316437
31613962633362383132653166656561366535323236656363333736313536623663386434313234
30646130353664346530383264323433313666393162373261373465336266623032

0 comments on commit 6a05a40

Please sign in to comment.