Skip to content

Commit

Permalink
Merge pull request #25 from geerlingguy/docker-role-tests
Browse files Browse the repository at this point in the history
Update tests using Docker for cross-platform PHP 7 testing.
  • Loading branch information
geerlingguy authored Jun 14, 2016
2 parents 65d8ed0 + e927f8f commit f96ff60
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 36 deletions.
56 changes: 33 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
---
sudo: required
language: python
python: "2.7"

env:
- SITE=test.yml
- SITE=test-global-require.yml
- distribution: centos
version: 7
init: /usr/lib/systemd/systemd
run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
- distribution: ubuntu
version: 14.04
init: /sbin/init
run_opts: ""

services:
- docker

before_install:
- sudo apt-get update -qq
- sudo apt-get install curl
# - sudo apt-get update
# Pull container
- 'sudo docker pull ${distribution}:${version}'
# Customize container
- 'sudo docker build --rm=true --file=tests/Dockerfile.${distribution}-${version} --tag=${distribution}-${version}:ansible tests'

install:
# Install Ansible.
- pip install ansible

# Add ansible.cfg to pick up roles path.
- "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg"
script:
- container_id=$(mktemp)
# Run container in detached state
- 'sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distribution}-${version}:ansible "${init}" > "${container_id}"'

# Install required dependencies.
- ansible-galaxy install geerlingguy.php
# Install dependencies.
- 'sudo docker exec "$(cat ${container_id})" ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml'

script:
# Check the role/playbook's syntax.
- "ansible-playbook -i tests/inventory tests/$SITE --syntax-check"
# Ansible syntax check.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml --syntax-check'

# Run the role/playbook with ansible-playbook.
- "ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo"
# Test role.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml'

# Run the role/playbook again, checking to make sure it's idempotent.
# Test role idempotence.
- >
ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo
sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml
| grep -q 'changed=0.*failed=0'
&& (echo 'Idempotence test: pass' && exit 0)
|| (echo 'Idempotence test: fail' && exit 1)
# Check if composer is installed and working.
- composer
# Ensure Composer is installed and working.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm composer'

# Clean up
- 'sudo docker stop "$(cat ${container_id})"'

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
27 changes: 27 additions & 0 deletions tests/Dockerfile.centos-7
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM centos:7

# Install systemd -- See https://hub.docker.com/_/centos/
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
RUN yum -y update; yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*; \
rm -f /lib/systemd/system/anaconda.target.wants/*;

# Install Ansible
RUN yum -y install epel-release
RUN yum -y install git ansible sudo
RUN yum clean all

# Disable requiretty
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers

# Install Ansible inventory file
RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts

VOLUME ["/sys/fs/cgroup"]
CMD ["/usr/sbin/init"]
14 changes: 14 additions & 0 deletions tests/Dockerfile.ubuntu-14.04
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM ubuntu:14.04
RUN apt-get update

# Install Ansible
RUN apt-get install -y software-properties-common git
RUN apt-add-repository -y ppa:ansible/ansible
RUN apt-get update
RUN apt-get install -y ansible

COPY initctl_faker .
RUN chmod +x initctl_faker && rm -fr /sbin/initctl && ln -s /initctl_faker /sbin/initctl

# Install Ansible inventory file
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts
23 changes: 23 additions & 0 deletions tests/initctl_faker
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
ALIAS_CMD="$(echo ""$0"" | sed -e 's?/sbin/??')"

case "$ALIAS_CMD" in
start|stop|restart|reload|status)
exec service $1 $ALIAS_CMD
;;
esac

case "$1" in
list )
exec service --status-all
;;
reload-configuration )
exec service $2 restart
;;
start|stop|restart|reload|status)
exec service $2 $1
;;
\?)
exit 0
;;
esac
3 changes: 3 additions & 0 deletions tests/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- src: geerlingguy.repo-remi
- src: geerlingguy.php
12 changes: 0 additions & 12 deletions tests/test-global-require.yml

This file was deleted.

13 changes: 12 additions & 1 deletion tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
remote_user: root

vars:
php_enablerepo: "remi,remi-php70"
php_enable_webserver: false

# Test a global requirement.
composer_global_packages:
- { name: phpunit/phpunit, release: "@stable" }

pre_tasks:
- name: Add repository for PHP 7.0.
apt_repository: repo='ppa:ondrej/php'
when: ansible_os_family == 'Debian'

roles:
- { role: geerlingguy.repo-remi, when: ansible_os_family == 'RedHat' }
- geerlingguy.php
- ansible-role-composer
- role_under_test

0 comments on commit f96ff60

Please sign in to comment.