mirror of
https://github.com/davestephens/ansible-nas
synced 2025-01-26 02:05:05 +00:00
Add start of molecule role testing
This commit is contained in:
parent
4dead04145
commit
1784f5663b
6 changed files with 72 additions and 75 deletions
|
@ -8,3 +8,7 @@ exclude_paths:
|
|||
- roles/bertvv.samba
|
||||
- roles/geerlingguy.docker
|
||||
- roles/geerlingguy.nfs
|
||||
|
||||
mock_modules:
|
||||
- docker_container
|
||||
- docker_container_info
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
pre-commit
|
||||
pre-commit
|
||||
molecule==4.0.1
|
||||
molecule[docker]==4.0.1
|
42
tests/molecule/base.yml
Normal file
42
tests/molecule/base.yml
Normal file
|
@ -0,0 +1,42 @@
|
|||
---
|
||||
dependency:
|
||||
name: galaxy
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: instance
|
||||
image: geerlingguy/docker-ubuntu2204-ansible:latest
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:ro
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
privileged: true
|
||||
provisioner:
|
||||
name: ansible
|
||||
config_options:
|
||||
defaults:
|
||||
stdout_callback: dense
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
docker_home: /ansible-nas
|
||||
ansible_nas_timezone: Europe/London
|
||||
ansible_nas_domain: ansible-nas.io
|
||||
playbooks:
|
||||
prepare: ../../../../tests/molecule/resources/playbooks/prepare.yml
|
||||
converge: ../../../../tests/molecule/resources/playbooks/converge.yml
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
test_sequence:
|
||||
- cleanup
|
||||
- destroy
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- converge
|
||||
- idempotence
|
||||
- verify
|
||||
- side_effect
|
||||
- verify verify_stopped.yml
|
||||
- cleanup
|
||||
- destroy
|
8
tests/molecule/resources/playbooks/converge.yml
Normal file
8
tests/molecule/resources/playbooks/converge.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
- name: Converge
|
||||
hosts: all
|
||||
become: true
|
||||
tasks:
|
||||
- name: "Include {{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }} role"
|
||||
include_role:
|
||||
name: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
|
7
tests/molecule/resources/playbooks/prepare.yml
Normal file
7
tests/molecule/resources/playbooks/prepare.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
tasks:
|
||||
- name: Install docker python module
|
||||
pip:
|
||||
name: docker
|
|
@ -1,16 +1,7 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Ansible playbook tester.
|
||||
# Based on geerlingguy's Ansible role tester.
|
||||
# Ansible-NAS role tester
|
||||
#
|
||||
# Usage: [OPTIONS] .tests/test.sh
|
||||
# - distro: a supported Docker distro version (default = "ubuntu1604")
|
||||
# - playbook: a playbook in the tests directory (default = "test.yml")
|
||||
# - cleanup: whether to remove the Docker container (default = true)
|
||||
# - container_id: the --name to set for the container (default = timestamp)
|
||||
# - test_idempotence: whether to test playbook's idempotence (default = true)
|
||||
#
|
||||
# License: MIT
|
||||
|
||||
# Exit on any individual command failure.
|
||||
set -e
|
||||
|
@ -22,68 +13,11 @@ neutral='\033[0m'
|
|||
|
||||
timestamp=$(date +%s)
|
||||
|
||||
# Allow environment variables to override defaults.
|
||||
distro=${distro:-"ubuntu2004"}
|
||||
docker_owner=${docker_owner:-"geerlingguy"}
|
||||
playbook=${playbook:-"nas.yml"}
|
||||
cleanup=${cleanup:-"true"}
|
||||
container_id=${container_id:-$timestamp}
|
||||
test_syntax=${test_syntax:-"true"}
|
||||
test_playbook=${test_playbook:-"false"}
|
||||
test_idempotence=${test_idempotence:-"false"}
|
||||
init="/lib/systemd/systemd"
|
||||
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
|
||||
|
||||
# Run the container using the supplied OS.
|
||||
printf ${green}"Starting Docker container: $docker_owner/docker-$distro-ansible."${neutral}"\n"
|
||||
docker pull $docker_owner/docker-$distro-ansible:latest
|
||||
docker run --detach --volume="$PWD":/etc/ansible/playbooks/playbook_under_test:rw --name $container_id $opts $docker_owner/docker-$distro-ansible:latest $init
|
||||
|
||||
printf "\n"
|
||||
|
||||
# Install requirements if `requirements.yml` is present.
|
||||
if [ -f "$PWD/requirements.yml" ]; then
|
||||
printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
|
||||
docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/playbooks/playbook_under_test/requirements.yml
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
|
||||
# Output Ansible version
|
||||
printf ${green}"Checking Ansible version."${neutral}"\n"
|
||||
docker exec --tty $container_id env TERM=xterm ansible-playbook --version
|
||||
|
||||
printf "\n"
|
||||
|
||||
if [ "$test_syntax" = true ]; then
|
||||
# Test Ansible syntax.
|
||||
printf ${green}"Checking Ansible playbook syntax."${neutral}"\n"
|
||||
docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/playbooks/playbook_under_test/$playbook --syntax-check
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
|
||||
if [ "$test_playbook" = true ]; then
|
||||
# Run Ansible playbook.
|
||||
printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/playbooks/playbook_under_test/$playbook"${neutral}"\n"
|
||||
docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/playbooks/playbook_under_test/$playbook
|
||||
fi
|
||||
|
||||
printf "\n"
|
||||
|
||||
if [ "$test_idempotence" = true ]; then
|
||||
# Run Ansible playbook again (idempotence test).
|
||||
printf ${green}"Running playbook again: idempotence test"${neutral}
|
||||
idempotence=$(mktemp)
|
||||
docker exec $container_id ansible-playbook /etc/ansible/playbooks/playbook_under_test/$playbook | tee -a $idempotence
|
||||
tail $idempotence \
|
||||
| grep -q 'changed=0.*failed=0' \
|
||||
&& (printf ${green}'Idempotence test: pass'${neutral}"\n") \
|
||||
|| (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
|
||||
fi
|
||||
|
||||
# Remove the Docker container (if configured).
|
||||
if [ "$cleanup" = true ]; then
|
||||
printf "Removing Docker container...\n"
|
||||
docker rm -f $container_id
|
||||
fi
|
||||
for f in ./roles/*; do
|
||||
if [[ -d "$f/molecule" ]]; then
|
||||
pushd "$f"
|
||||
molecule -c ../../tests/molecule/base.yml test
|
||||
popd
|
||||
fi
|
||||
done
|
||||
|
|
Loading…
Reference in a new issue