From 2b72889d9075594da07cbbaef65761fda779ded4 Mon Sep 17 00:00:00 2001 From: Alessandro Fael Garcia Date: Fri, 13 Apr 2018 15:24:09 -0700 Subject: [PATCH] Test role across multiple distributions Implement support to test the role in multiple Linux distributions by deploying Docker containers within Travis --- .travis.yml | 52 +++++++++++---------- tests/dockerfiles/Dockerfile.centos-6 | 12 +++++ tests/dockerfiles/Dockerfile.centos-7 | 24 ++++++++++ tests/dockerfiles/Dockerfile.debian-jessie | 17 +++++++ tests/dockerfiles/Dockerfile.debian-stretch | 18 +++++++ tests/dockerfiles/Dockerfile.ubuntu-trusty | 12 +++++ tests/dockerfiles/Dockerfile.ubuntu-xenial | 12 +++++ 7 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 tests/dockerfiles/Dockerfile.centos-6 create mode 100644 tests/dockerfiles/Dockerfile.centos-7 create mode 100644 tests/dockerfiles/Dockerfile.debian-jessie create mode 100644 tests/dockerfiles/Dockerfile.debian-stretch create mode 100644 tests/dockerfiles/Dockerfile.ubuntu-trusty create mode 100644 tests/dockerfiles/Dockerfile.ubuntu-xenial diff --git a/.travis.yml b/.travis.yml index 2afc404..0f32d06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,30 +1,34 @@ --- -language: python -python: "2.7" -# Use the new container infrastructure sudo: required -# Install ansible -addons: - apt: - packages: - - python-pip -install: - # Install ansible - - pip install ansible - # Check ansible version - - ansible --version - # Create ansible.cfg with correct roles_path - - printf '[defaults]\nroles_path=../' >ansible.cfg +services: + - docker +env: + - distribution: centos + version: 6 + - distribution: centos + version: 7 + - distribution: ubuntu + version: trusty + - distribution: ubuntu + version: xenial + - distribution: debian + version: jessie + - distribution: debian + version: stretch +before_install: + - 'sudo docker pull ${distribution}:${version}' + - 'sudo docker build --no-cache --rm --file=tests/dockerfiles/Dockerfile.${distribution}-${version} --tag=${distribution}-${version}:ansible tests' script: - # Basic role syntax check - - ansible-playbook tests/test.yml -i tests/inventory --syntax-check - # Run the role with ansible-playbook. - - ansible-playbook tests/test.yml -i tests/inventory --connection=local --become - # Run the role again, checking to make sure it's idempotent. + - container_id=$(mktemp) + - 'sudo docker run --detach --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro --volume="${PWD}":/etc/ansible/roles/ansible-role-nginx:ro ${distribution}-${version}:ansible > "${container_id}"' + - 'sudo docker exec "$(cat ${container_id})" env ANSIBLE_FORCE_COLOR=1 ansible-playbook -v /etc/ansible/roles/ansible-role-nginx/tests/test.yml --syntax-check' + - 'sudo docker exec "$(cat ${container_id})" env ANSIBLE_FORCE_COLOR=1 ansible-playbook -v /etc/ansible/roles/ansible-role-nginx/tests/test.yml' - > - ansible-playbook tests/test.yml -i tests/inventory --connection=local --become | grep -q 'changed=0.*failed=0' - && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) - # Request a page via the web server, to make sure NGINX is running and responds. - - curl http://localhost/ + sudo docker exec "$(cat ${container_id})" env ANSIBLE_FORCE_COLOR=1 ansible-playbook -v /etc/ansible/roles/ansible-role-nginx/tests/test.yml + | grep -q 'changed=0.*failed=0' + && (echo 'Idempotence test: pass' && exit 0) + || (echo 'Idempotence test: fail' && exit 1) + - 'sudo docker exec "$(cat ${container_id})" curl http://localhost/' + - 'sudo docker rm -f "$(cat ${container_id})"' notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/tests/dockerfiles/Dockerfile.centos-6 b/tests/dockerfiles/Dockerfile.centos-6 new file mode 100644 index 0000000..164d3cd --- /dev/null +++ b/tests/dockerfiles/Dockerfile.centos-6 @@ -0,0 +1,12 @@ +FROM centos:6 + +RUN yum -y install epel-release +RUN yum -y install git ansible sudo + +RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers + +RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts + +VOLUME ["/sys/fs/cgroup"] + +CMD ["/usr/sbin/init"] diff --git a/tests/dockerfiles/Dockerfile.centos-7 b/tests/dockerfiles/Dockerfile.centos-7 new file mode 100644 index 0000000..b7c1e90 --- /dev/null +++ b/tests/dockerfiles/Dockerfile.centos-7 @@ -0,0 +1,24 @@ +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; \ +(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/*; + +RUN yum -y install epel-release +RUN yum -y install git ansible sudo + +RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers + +RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts + +VOLUME ["/sys/fs/cgroup"] + +CMD ["/usr/sbin/init"] diff --git a/tests/dockerfiles/Dockerfile.debian-jessie b/tests/dockerfiles/Dockerfile.debian-jessie new file mode 100644 index 0000000..8d38f0c --- /dev/null +++ b/tests/dockerfiles/Dockerfile.debian-jessie @@ -0,0 +1,17 @@ +FROM debian:jessie + +RUN apt-get update -y && apt-get install -y --no-install-recommends \ + software-properties-common \ + build-essential \ + libffi-dev \ + libssl-dev \ + python-dev \ + python-pip \ + git \ + && rm -rf /var/lib/apt/lists/* + +RUN pip install --upgrade setuptools && pip install ansible + +RUN mkdir -p /etc/ansible && echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts + +ENTRYPOINT ["/sbin/init"] diff --git a/tests/dockerfiles/Dockerfile.debian-stretch b/tests/dockerfiles/Dockerfile.debian-stretch new file mode 100644 index 0000000..ee2fbc7 --- /dev/null +++ b/tests/dockerfiles/Dockerfile.debian-stretch @@ -0,0 +1,18 @@ +FROM debian:stretch + +RUN apt-get update -y && apt-get install -y --no-install-recommends \ + software-properties-common \ + build-essential \ + libffi-dev \ + libssl-dev \ + python-dev \ + python-pip \ + git \ + systemd \ + && rm -rf /var/lib/apt/lists/* + +RUN pip install --upgrade setuptools && pip install ansible + +RUN mkdir -p /etc/ansible && echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts + +ENTRYPOINT ["/bin/systemd"] diff --git a/tests/dockerfiles/Dockerfile.ubuntu-trusty b/tests/dockerfiles/Dockerfile.ubuntu-trusty new file mode 100644 index 0000000..c9687d2 --- /dev/null +++ b/tests/dockerfiles/Dockerfile.ubuntu-trusty @@ -0,0 +1,12 @@ +FROM ubuntu:trusty + +RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y software-properties-common && rm -rf /var/lib/apt/lists/* + +RUN apt-add-repository -y ppa:ansible/ansible && apt-get update && apt-get install -y \ + git \ + ansible \ + && rm -rf /var/lib/apt/lists/* + +RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts + +ENTRYPOINT ["/sbin/init"] diff --git a/tests/dockerfiles/Dockerfile.ubuntu-xenial b/tests/dockerfiles/Dockerfile.ubuntu-xenial new file mode 100644 index 0000000..2faaba1 --- /dev/null +++ b/tests/dockerfiles/Dockerfile.ubuntu-xenial @@ -0,0 +1,12 @@ +FROM ubuntu:xenial + +RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y software-properties-common && rm -rf /var/lib/apt/lists/* + +RUN apt-add-repository -y ppa:ansible/ansible && apt-get update && apt-get install -y \ + git \ + ansible \ + && rm -rf /var/lib/apt/lists/* + +RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts + +ENTRYPOINT ["/sbin/init"]