|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Ansible role test shim. |
| 4 | +# |
| 5 | +# Usage: [OPTIONS] ./tests/test.sh |
| 6 | +# - distro: a supported Docker distro version (default = "centos7") |
| 7 | +# - playbook: a playbook in the tests directory (default = "test.yml") |
| 8 | +# - cleanup: whether to remove the Docker container (default = true) |
| 9 | +# - container_id: the --name to set for the container (default = timestamp) |
| 10 | + |
| 11 | +# Exit on any individual command failure. |
| 12 | +set -e |
| 13 | + |
| 14 | +# Pretty colors. |
| 15 | +red='\033[0;31m' |
| 16 | +green='\033[0;32m' |
| 17 | +neutral='\033[0m' |
| 18 | + |
| 19 | +timestamp=$(date +%s) |
| 20 | + |
| 21 | +# Allow environment variables to override defaults. |
| 22 | +distro=${distro:-"centos7"} |
| 23 | +playbook=${playbook:-"test.yml"} |
| 24 | +cleanup=${cleanup:-"true"} |
| 25 | +container_id=${container_id:-$timestamp} |
| 26 | + |
| 27 | +## Set up vars for Docker setup. |
| 28 | +# CentOS 7 |
| 29 | +if [ $distro = 'centos7' ]; then |
| 30 | + init="/usr/lib/systemd/systemd" |
| 31 | + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" |
| 32 | +# CentOS 6 |
| 33 | +elif [ $distro = 'centos6' ]; then |
| 34 | + init="/sbin/init" |
| 35 | + opts="--privileged" |
| 36 | +# Ubuntu 16.04 |
| 37 | +elif [ $distro = 'ubuntu1604' ]; then |
| 38 | + init="/lib/systemd/systemd" |
| 39 | + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" |
| 40 | +# Ubuntu 14.04 |
| 41 | +elif [ $distro = 'ubuntu1404' ]; then |
| 42 | + init="/sbin/init" |
| 43 | + opts="--privileged" |
| 44 | +# Ubuntu 12.04 |
| 45 | +elif [ $distro = 'ubuntu1204' ]; then |
| 46 | + init="/sbin/init" |
| 47 | + opts="--privileged" |
| 48 | +# Debian 8 |
| 49 | +elif [ $distro = 'debian8' ]; then |
| 50 | + init="/lib/systemd/systemd" |
| 51 | + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" |
| 52 | +# Fedora 24 |
| 53 | +elif [ $distro = 'fedora24' ]; then |
| 54 | + init="/usr/lib/systemd/systemd" |
| 55 | + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" |
| 56 | +fi |
| 57 | + |
| 58 | +# Run the container using the supplied OS. |
| 59 | +printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n" |
| 60 | +docker pull geerlingguy/docker-$distro-ansible:latest |
| 61 | +docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts geerlingguy/docker-$distro-ansible:latest $init |
| 62 | + |
| 63 | +printf "\n" |
| 64 | + |
| 65 | +# Install requirements if `requirements.yml` is present. |
| 66 | +if [ -f "$PWD/tests/requirements.yml" ]; then |
| 67 | + printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n" |
| 68 | + docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml |
| 69 | +fi |
| 70 | + |
| 71 | +printf "\n" |
| 72 | + |
| 73 | +# Test Ansible syntax. |
| 74 | +printf ${green}"Checking Ansible playbook syntax."${neutral} |
| 75 | +docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check |
| 76 | + |
| 77 | +printf "\n" |
| 78 | + |
| 79 | +# Run Ansible playbook. |
| 80 | +printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral} |
| 81 | +docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook |
| 82 | + |
| 83 | +# Run Ansible playbook again (idempotence test). |
| 84 | +printf ${green}"Running playbook again: idempotence test"${neutral} |
| 85 | +idempotence=$(mktemp) |
| 86 | +docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence |
| 87 | +tail $idempotence \ |
| 88 | + | grep -q 'changed=0.*failed=0' \ |
| 89 | + && (printf ${green}'Idempotence test: pass'${neutral}"\n") \ |
| 90 | + || (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1) |
| 91 | + |
| 92 | +# Remove the Docker container (if configured). |
| 93 | +if [ "$cleanup" = true ]; then |
| 94 | + printf "Removing Docker container...\n" |
| 95 | + docker rm -f $container_id |
| 96 | +fi |
0 commit comments