From 61cf403dd97e43c16e05dc0dd03694df1543574f Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 1 Apr 2017 15:05:50 -0500 Subject: Add test shim and update Travisfile. --- .travis.yml | 30 ++------------------ tests/test.sh | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 28 deletions(-) create mode 100755 tests/test.sh diff --git a/.travis.yml b/.travis.yml index 494f809..5ea940f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,44 +3,18 @@ services: docker env: - distro: centos7 - init: /usr/lib/systemd/systemd - run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" - distro: centos6 - init: /sbin/init - run_opts: "" - distro: ubuntu1604 - init: /lib/systemd/systemd - run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" - distro: ubuntu1404 - init: /sbin/init - run_opts: "" - distro: ubuntu1204 - init: /sbin/init - run_opts: "" before_install: # Pull container. - 'docker pull geerlingguy/docker-${distro}-ansible:latest' script: - - container_id=$(mktemp) - # Run container in detached state. - - 'docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} geerlingguy/docker-${distro}-ansible:latest "${init}" > "${container_id}"' - - # Ansible syntax check. - - 'docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml --syntax-check' - - # Test role. - - 'docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml' - - # Test role idempotence. - - idempotence=$(mktemp) - - docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml | tee -a ${idempotence} - - > - tail ${idempotence} - | grep -q 'changed=0.*failed=0' - && (echo 'Idempotence test: pass' && exit 0) - || (echo 'Idempotence test: fail' && exit 1) + # Run tests. + - ${PWD}/tests/test.sh ${distro} notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..5bc169b --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# +# Ansible role test shim. +# +# Usage: test.sh [os] [playbook] +# - [os] = One of the supported distros. +# = [playbook] = The test playbook to run. (Defaults to test.yml if unset). + +# Exit on any individual command failure. +set -e + +# Pretty colors. +red='\033[0;31m' +green='\033[0;32m' +neutral='\033[0m' + +# TODO: Check to make sure OS is provided. +distro=$1 +playbook=${2:-"test.yml"} + +## 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="" +# Ubuntu 16.04 +elif [ $distro = 'ubuntu1604' ]; then + init="/lib/systemd/systemd" + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" +# Ubuntu 14.04 +elif [ $distro = 'ubuntu1404' ]; then + init="/sbin/init" + opts="" +# Ubuntu 12.04 +elif [ $distro = 'ubuntu1204' ]; then + init="/sbin/init" + opts="" +# Debian 8 +elif [ $distro = 'debian8' ]; then + init="/lib/systemd/systemd" + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" +# Fedora 24 +elif [ $distro = 'fedora24' ]; then + init="/usr/lib/systemd/systemd" + opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" +fi + +# Create a container ID. +container_id=$(mktemp) + +# Run the container using the supplied OS. +printf ${green}"Pulling Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n" +docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $run_opts geerlingguy/docker-$distro-ansible:latest "$init" > "$container_id" + +container_id=$(cat $container_id) + +printf "\n" + +# Test Ansible syntax. +printf ${green}"Checking Ansible playbook syntax."${neutral} +docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check + +printf "\n" + +# Install requirements if `requirements.yml` is present. +# TODO + +printf "\n" + +# Run Ansible playbook. +printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral} +docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook + +# Run Ansible playbook again (idempotence test). +printf ${green}"Running playbook again: idempotence test"${neutral} +idempotence=$(mktemp) +docker exec --tty $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$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) + +# Kill the Docker container? +printf "Removing Docker container...\n" +docker rm -f $container_id -- cgit v1.2.1 From 27b71feaa79024126033f4410205804b3c657399 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 1 Apr 2017 15:53:03 -0500 Subject: Optimize installation, make things work better. --- .travis.yml | 5 +---- tasks/main.yml | 13 ++----------- tests/test.sh | 5 +++-- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5ea940f..1f2c376 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,7 @@ env: - distro: ubuntu1604 - distro: ubuntu1404 - distro: ubuntu1204 - -before_install: - # Pull container. - - 'docker pull geerlingguy/docker-${distro}-ansible:latest' + - distro: debian8 script: # Run tests. diff --git a/tasks/main.yml b/tasks/main.yml index b0379a2..c5a4d7f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -17,17 +17,8 @@ force: yes when: ansible_os_family == 'Debian' -- name: Install NTP (RedHat). - yum: name=ntp state=installed - when: ansible_os_family == 'RedHat' - -- name: Install NTP (Debian). - apt: name=ntp state=installed - when: ansible_os_family == 'Debian' - -- name: Install NTP (FreeBSD). - pkgng: name=ntp state=present - when: ansible_os_family == 'FreeBSD' +- name: Install NTP. + package: name=ntp state=present - name: Ensure NTP is running and enabled as configured. service: diff --git a/tests/test.sh b/tests/test.sh index 5bc169b..e625951 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -53,8 +53,9 @@ fi container_id=$(mktemp) # Run the container using the supplied OS. -printf ${green}"Pulling Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n" -docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $run_opts geerlingguy/docker-$distro-ansible:latest "$init" > "$container_id" +printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n" +docker pull geerlingguy/docker-$distro-ansible:latest +docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $opts geerlingguy/docker-$distro-ansible:latest $init > "$container_id" container_id=$(cat $container_id) -- cgit v1.2.1 From bbe9066d500b3c7c37bb5280e4280d46f055b525 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 1 Apr 2017 16:02:12 -0500 Subject: Final adjustments. --- .travis.yml | 2 +- tests/test.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1f2c376..a829b5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ env: - distro: ubuntu1604 - distro: ubuntu1404 - distro: ubuntu1204 - - distro: debian8 + # - distro: debian8 script: # Run tests. diff --git a/tests/test.sh b/tests/test.sh index e625951..107d843 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -79,7 +79,7 @@ docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/rol # Run Ansible playbook again (idempotence test). printf ${green}"Running playbook again: idempotence test"${neutral} idempotence=$(mktemp) -docker exec --tty $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence +docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence tail $idempotence \ | grep -q 'changed=0.*failed=0' \ && (printf ${green}'Idempotence test: pass'${neutral}"\n") \ -- cgit v1.2.1 From 7a748c8d71428ec6161d4b055accd2b6853ac3ee Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 1 Apr 2017 16:07:28 -0500 Subject: Add gitignore and use shipping test.sh. --- .gitignore | 2 ++ .travis.yml | 4 +++ tests/README.md | 8 +++++ tests/test.sh | 90 --------------------------------------------------------- 4 files changed, 14 insertions(+), 90 deletions(-) create mode 100644 .gitignore create mode 100644 tests/README.md delete mode 100755 tests/test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9b2377 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.retry +tests/test.sh diff --git a/.travis.yml b/.travis.yml index a829b5f..5e152aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,10 @@ env: # - distro: debian8 script: + # Download test shim. + - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/51ef4c5aea346b3f8dd2c2518233a70dac6407d9/ansible-role-test.sh + - chmod +x ${PWD}/tests/test.sh + # Run tests. - ${PWD}/tests/test.sh ${distro} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..34dd4f5 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,8 @@ +# Ansible Role tests + +To run the test playbook(s) in this directory: + + 1. Install and start Docker. + 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`. + 1. Make the test shim executable: `chmod +x tests/test.sh`. + 1. Run (from the role root directory) `./tests/test.sh [distro] [optional - playbook]` diff --git a/tests/test.sh b/tests/test.sh deleted file mode 100755 index 107d843..0000000 --- a/tests/test.sh +++ /dev/null @@ -1,90 +0,0 @@ -#!/bin/bash -# -# Ansible role test shim. -# -# Usage: test.sh [os] [playbook] -# - [os] = One of the supported distros. -# = [playbook] = The test playbook to run. (Defaults to test.yml if unset). - -# Exit on any individual command failure. -set -e - -# Pretty colors. -red='\033[0;31m' -green='\033[0;32m' -neutral='\033[0m' - -# TODO: Check to make sure OS is provided. -distro=$1 -playbook=${2:-"test.yml"} - -## 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="" -# Ubuntu 16.04 -elif [ $distro = 'ubuntu1604' ]; then - init="/lib/systemd/systemd" - opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" -# Ubuntu 14.04 -elif [ $distro = 'ubuntu1404' ]; then - init="/sbin/init" - opts="" -# Ubuntu 12.04 -elif [ $distro = 'ubuntu1204' ]; then - init="/sbin/init" - opts="" -# Debian 8 -elif [ $distro = 'debian8' ]; then - init="/lib/systemd/systemd" - opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" -# Fedora 24 -elif [ $distro = 'fedora24' ]; then - init="/usr/lib/systemd/systemd" - opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" -fi - -# Create a container ID. -container_id=$(mktemp) - -# Run the container using the supplied OS. -printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n" -docker pull geerlingguy/docker-$distro-ansible:latest -docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw $opts geerlingguy/docker-$distro-ansible:latest $init > "$container_id" - -container_id=$(cat $container_id) - -printf "\n" - -# Test Ansible syntax. -printf ${green}"Checking Ansible playbook syntax."${neutral} -docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check - -printf "\n" - -# Install requirements if `requirements.yml` is present. -# TODO - -printf "\n" - -# Run Ansible playbook. -printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral} -docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook - -# Run Ansible playbook again (idempotence test). -printf ${green}"Running playbook again: idempotence test"${neutral} -idempotence=$(mktemp) -docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$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) - -# Kill the Docker container? -printf "Removing Docker container...\n" -docker rm -f $container_id -- cgit v1.2.1 From fd5694f2f65b36c394c4933a6fc4ccbc24d12830 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Sat, 1 Apr 2017 16:10:01 -0500 Subject: Add README to tests shim. --- tests/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/README.md b/tests/README.md index 34dd4f5..323a451 100644 --- a/tests/README.md +++ b/tests/README.md @@ -3,6 +3,7 @@ To run the test playbook(s) in this directory: 1. Install and start Docker. - 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`. + 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`: + - `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/51ef4c5aea346b3f8dd2c2518233a70dac6407d9/ansible-role-test.sh` 1. Make the test shim executable: `chmod +x tests/test.sh`. 1. Run (from the role root directory) `./tests/test.sh [distro] [optional - playbook]` -- cgit v1.2.1