-
-
Save tolland/45a03a95e2db57712c1e30127c34a195 to your computer and use it in GitHub Desktop.
Ansible Role Test Shim Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Ansible role test shim. | |
# | |
# Usage: [OPTIONS] ./tests/test.sh | |
# - distro: a supported Docker distro version (default = "centos7") | |
# - playbook: a playbook in the tests directory (default = "test.yml") | |
# - role_dir: the directory where the role exists (default = $PWD) | |
# - 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) | |
# | |
# If you place a requirements.yml file in tests/requirements.yml, the | |
# requirements listed inside that file will be installed via Ansible Galaxy | |
# prior to running tests. | |
# | |
# License: MIT | |
# Exit on any individual command failure. | |
set -e | |
# Pretty colors. | |
red='\033[0;31m' | |
green='\033[0;32m' | |
neutral='\033[0m' | |
timestamp=$(date +%s) | |
# Allow environment variables to override defaults. | |
distro=${distro:-"centos7"} | |
playbook=${playbook:-"test.yml"} | |
role_dir=${role_dir:-"$PWD"} | |
cleanup=${cleanup:-"true"} | |
container_id=${container_id:-$timestamp} | |
test_idempotence=${test_idempotence:-"true"} | |
ansible_vs=${ansible_vs:-"-vv"} | |
echo "TRAVIS is '${TRAVIS}'" | |
travis=${TRAVIS:-"true"} | |
docker_profile=${docker_profile:-"geerlingguy"} | |
#extravars=${extravars:-""} | |
## Set up vars for Docker setup. | |
# CentOS 7 | |
if [ $distro = 'centos7' ]; then | |
init="/usr/lib/systemd/systemd" | |
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# CentOS 6 | |
elif [ $distro = 'centos6' ]; then | |
init="/sbin/init" | |
opts="--privileged" | |
# Ubuntu 12.04 | |
elif [ $distro = 'ubuntu1204' ]; then | |
init="/sbin/init" | |
opts="--privileged --volume=/var/lib/docker" | |
# Ubuntu 14.04 | |
elif [ $distro = 'ubuntu1404' ]; then | |
init="/sbin/init" | |
opts="--privileged --volume=/var/lib/docker" | |
# Ubuntu 16.04 | |
elif [ $distro = 'ubuntu1604' ]; then | |
init="/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Ubuntu 18.04 | |
elif [ $distro = 'ubuntu1804' ]; then | |
init="/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Ubuntu 19.04 | |
elif [ $distro = 'ubuntu1904' ]; then | |
init="/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Debian 9 | |
elif [ $distro = 'debian9' ]; then | |
init="/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Debian 8 | |
elif [ $distro = 'debian8' ]; then | |
init="/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Fedora 24 | |
elif [ $distro = 'fedora24' ]; then | |
init="/usr/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Fedora 27 | |
elif [ $distro = 'fedora27' ]; then | |
init="/usr/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Fedora 28 | |
elif [ $distro = 'fedora28' ]; then | |
init="/usr/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Fedora 29 | |
elif [ $distro = 'fedora29' ]; then | |
init="/usr/lib/systemd/systemd" | |
# opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
# Fedora 30 | |
elif [ $distro = 'fedora30' ]; then | |
init="/usr/lib/systemd/systemd" | |
opts="--privileged --volume=/var/lib/docker --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" | |
fi | |
# Run the container using the supplied OS. | |
printf ${green}"Starting Docker container: ${docker_profile}/docker-$distro-ansible."${neutral}"\n" | |
docker pull ${docker_profile}/docker-$distro-ansible:latest | |
docker run --detach --volume="$(readlink -f $role_dir)":/etc/ansible/roles/role_under_test:rw --name $container_id $opts ${docker_profile}/docker-$distro-ansible:latest $init | |
printf "\n" | |
echo $container_id | |
# Install requirements if `requirements.yml` is present. | |
if [ -f "$role_dir/tests/requirements.yml" ]; then | |
printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n" | |
echo "here" | |
docker exec --tty $container_id env TERM=xterm sh -c "test -e /usr/bin/git || ( ( test -e /usr/bin/apt && apt -qqy update && apt -qqy update && apt install -qqy git) || ( test -e /usr/bin/dnf && /usr/bin/dnf -qy install git) || ( test -e /usr/bin/yum && /usr/bin/yum -qy install git) )" | |
docker exec --tty $container_id env TERM=xterm ansible-galaxy install ${ansible_vs} -f -r /etc/ansible/roles/role_under_test/tests/requirements.yml | |
fi | |
printf "\n" | |
# Test Ansible syntax. | |
printf ${green}"Checking Ansible playbook syntax."${neutral} | |
docker exec --tty $container_id env TERM=xterm TRAVIS=${travis} ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check | |
printf "\n" | |
# Install requirements if `requirements.yml` is present. | |
if [ -f "$role_dir/tests/extravars-${distro}.sh" ]; then | |
source "$role_dir/tests/extravars-${distro}.sh" | |
fi | |
printf "\n" | |
docker exec --env TERM=xterm --env ANSIBLE_FORCE_COLOR=1 $container_id service apparmor stop || true | |
docker exec --env TERM=xterm --env ANSIBLE_FORCE_COLOR=1 $container_id service apparmor teardown || true | |
# Run Ansible playbook. | |
printf ${green}"Running command: docker exec --env TERM=xterm --env ANSIBLE_FORCE_COLOR=1 $container_id ansible-playbook ${ansible_vs} /etc/ansible/roles/role_under_test/tests/$playbook"${neutral} | |
docker exec --env TERM=xterm --env ANSIBLE_FORCE_COLOR=1 $container_id ansible-playbook ${ansible_vs} /etc/ansible/roles/role_under_test/tests/$playbook ${extravars} | |
if [ "$test_idempotence" = true ]; then | |
# Run Ansible playbook again (idempotence test). | |
printf ${green}"Running playbook again: idempotence test"${neutral} | |
idempotence=$(mktemp) | |
docker exec --env ANSIBLE_FORCE_COLOR=1 $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook ${extravars} | 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 | |
printf "\n" | |
# Run inspec test | |
printf ${green}"Running command: inspec exec test/integration/default/ -t docker://${container_id}"${neutral} | |
inspec exec test/integration/default/ -t docker://${container_id} --reporter documentation | |
# Remove the Docker container (if configured). | |
if [ "$cleanup" = true ]; then | |
printf "Removing Docker container...\n" | |
docker rm -f $container_id | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment