From 43c25c03028095cd8bec4656b6e9c633ef0e3320 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 11:17:40 +0100 Subject: [PATCH 01/11] Run multi-platform test on Travis-CI --- .travis.yml | 55 ++++++++++++++++++++++------------------- tests/Dockerfile.centos | 26 +++++++++++++++++++ tests/Dockerfile.ubuntu | 12 +++++++++ tests/inventory | 1 - tests/requirements.yml | 1 + tests/test.yml | 5 ++-- 6 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 tests/Dockerfile.centos create mode 100644 tests/Dockerfile.ubuntu delete mode 100644 tests/inventory create mode 100644 tests/requirements.yml diff --git a/.travis.yml b/.travis.yml index e7bf93b..acd0162 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,34 +1,37 @@ ---- -language: python -python: "2.7" - +sudo: required env: - - SITE=test.yml + - CONTAINER_ID=$(mktemp) + +services: + - docker before_install: - - sudo apt-get update -qq - - sudo apt-get install -y curl - -install: - # Install Ansible. - - pip install ansible - - # Add ansible.cfg to pick up roles path. - - "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg" + - sudo apt-get update + # Pull containers + - sudo docker pull centos:7 + - sudo docker pull ubuntu:14.04 + # Customize containers + - sudo docker build --rm=true --file=Dockerfile.centos --tag=centos:ansible tests + - sudo docker build --rm=true --file=Dockerfile.ubuntu --tag=ubuntu:ansible tests script: - # Check the role/playbook's syntax. - - "ansible-playbook -i tests/inventory tests/$SITE --syntax-check" + # + # Run test playbook on Ubuntu container + # + - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml --syntax-check + - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml - # Run the role/playbook with ansible-playbook. - - "ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo" + # + # Run test playbook on CentOS container + # - # Run the role/playbook again, checking to make sure it's idempotent. - - > - ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo - | grep -q 'changed=0.*failed=0' - && (echo 'Idempotence test: pass' && exit 0) - || (echo 'Idempotence test: fail' && exit 1) + # Run container in detached state + - sudo docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro centos:ansible /usr/lib/systemd/systemd > "${CONTAINER_ID}" - # Request a page via Apache, to make sure Apache is running and responds. - - "curl http://localhost/" + - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml --syntax-check + - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml + # Clean up + - sudo docker stop "$(cat ${CONTAINER_ID})" + +notifications: + email: false diff --git a/tests/Dockerfile.centos b/tests/Dockerfile.centos new file mode 100644 index 0000000..dc27b87 --- /dev/null +++ b/tests/Dockerfile.centos @@ -0,0 +1,26 @@ +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 +COPY requirements.yml /etc/ansible/requirements.yml +COPY test.yml /etc/ansible/test.yml +RUN ansible-galaxy install -r /etc/ansible/requirements.yml +VOLUME [ "/sys/fs/cgroup" ] +CMD ["/usr/sbin/init"] + diff --git a/tests/Dockerfile.ubuntu b/tests/Dockerfile.ubuntu new file mode 100644 index 0000000..bb37040 --- /dev/null +++ b/tests/Dockerfile.ubuntu @@ -0,0 +1,12 @@ +FROM ubuntu:14.04 +# 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 +# Install Ansible inventory file +RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts +COPY requirements.yml /etc/ansible/requirements.yml +COPY test.yml /etc/ansible/test.yml +RUN ansible-galaxy install -r /etc/ansible/requirements.yml + diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 2fbb50c..0000000 --- a/tests/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost diff --git a/tests/requirements.yml b/tests/requirements.yml new file mode 100644 index 0000000..9302d4d --- /dev/null +++ b/tests/requirements.yml @@ -0,0 +1 @@ +- src: geerlingguy.apache diff --git a/tests/test.yml b/tests/test.yml index 854cb52..09d3cf6 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -1,5 +1,4 @@ --- -- hosts: localhost - remote_user: root +- hosts: all roles: - - ansible-role-apache + - geerlingguy.apache From 5d73dbbb9424ff7af9a378ed11a0758bf37c9e8c Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 11:20:32 +0100 Subject: [PATCH 02/11] Fix path to Dockerfile --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index acd0162..a795b1b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,8 @@ before_install: - sudo docker pull centos:7 - sudo docker pull ubuntu:14.04 # Customize containers - - sudo docker build --rm=true --file=Dockerfile.centos --tag=centos:ansible tests - - sudo docker build --rm=true --file=Dockerfile.ubuntu --tag=ubuntu:ansible tests + - sudo docker build --rm=true --file=tests/Dockerfile.centos --tag=centos:ansible tests + - sudo docker build --rm=true --file=tests/Dockerfile.ubuntu --tag=ubuntu:ansible tests script: # From 0060d6d1c8b60cf1fa96ec0527ce6eb4518c7029 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 11:29:05 +0100 Subject: [PATCH 03/11] Add idempotence test --- .travis.yml | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a795b1b..1582316 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,19 +18,37 @@ script: # # Run test playbook on Ubuntu container # + + # Syntax check - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml --syntax-check + # Test role - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml + # Idempotence test + - > + sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml + | grep -q 'changed=0.*failed=0' + && (echo 'Idempotence test: pass' && exit 0) + || (echo 'Idempotence test: fail' && exit 1) # # Run test playbook on CentOS container # - # Run container in detached state + # Run container in detached state - sudo docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro centos:ansible /usr/lib/systemd/systemd > "${CONTAINER_ID}" + # Syntax check - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml --syntax-check + # Test role - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml - # Clean up + # Idempotence test + - > + sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml + | grep -q 'changed=0.*failed=0' + && (echo 'Idempotence test: pass' && exit 0) + || (echo 'Idempotence test: fail' && exit 1) + + # Clean up - sudo docker stop "$(cat ${CONTAINER_ID})" notifications: From 5260215d2892bcd85f938d34f938cf0cf654c1ec Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 11:36:33 +0100 Subject: [PATCH 04/11] Change order in build process --- tests/Dockerfile.centos | 2 +- tests/Dockerfile.ubuntu | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Dockerfile.centos b/tests/Dockerfile.centos index dc27b87..fcab1d2 100644 --- a/tests/Dockerfile.centos +++ b/tests/Dockerfile.centos @@ -19,8 +19,8 @@ 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 COPY requirements.yml /etc/ansible/requirements.yml -COPY test.yml /etc/ansible/test.yml RUN ansible-galaxy install -r /etc/ansible/requirements.yml +COPY test.yml /etc/ansible/test.yml VOLUME [ "/sys/fs/cgroup" ] CMD ["/usr/sbin/init"] diff --git a/tests/Dockerfile.ubuntu b/tests/Dockerfile.ubuntu index bb37040..f48ad29 100644 --- a/tests/Dockerfile.ubuntu +++ b/tests/Dockerfile.ubuntu @@ -7,6 +7,6 @@ RUN apt-get install -y ansible # Install Ansible inventory file RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts COPY requirements.yml /etc/ansible/requirements.yml -COPY test.yml /etc/ansible/test.yml RUN ansible-galaxy install -r /etc/ansible/requirements.yml +COPY test.yml /etc/ansible/test.yml From 609812fb484b1fd86a6b6e0ee289c4936287999b Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 11:36:56 +0100 Subject: [PATCH 05/11] Set some role variables in the test playbook --- tests/test.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test.yml b/tests/test.yml index 09d3cf6..0bf4014 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -1,4 +1,11 @@ --- - hosts: all + vars: + apache_listen_port_ssl: 443 + apache_create_vhosts: true + apache_vhosts_filename: "vhosts.conf" + apache_vhosts: + - servername: "example.com" + documentroot: "/var/www/vhosts/example_com" roles: - geerlingguy.apache From 33c4876d51f19a5381ca1dbdde5c77f311a5b5fb Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 13:36:52 +0100 Subject: [PATCH 06/11] Fix docker run on Ubuntu --- .travis.yml | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1582316..c571912 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,17 +19,24 @@ script: # Run test playbook on Ubuntu container # + # Run container in detached state + - sudo docker run --detach ubuntu:ansible /sbin/init > "${CONTAINER_ID}" + + # Syntax check - - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml --syntax-check + - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml --syntax-check # Test role - - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml + - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml # Idempotence test - > - sudo docker run ubuntu:ansible ansible-playbook /etc/ansible/test.yml + sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) + # Clean up + - sudo docker stop "$(cat ${CONTAINER_ID})" + # # Run test playbook on CentOS container # From 940968d86f97fb058f0fbcb1fe01a8b03e64c4a3 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 13:42:26 +0100 Subject: [PATCH 07/11] Remove duplicate code, use build matrix --- .travis.yml | 47 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/.travis.yml b/.travis.yml index c571912..9eed15f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,62 +1,39 @@ sudo: required env: - - CONTAINER_ID=$(mktemp) + - container_id=$(mktemp) distribution=centos version=7 + - container_id=$(mktemp) distribution=ubuntu version=14.04 services: - docker before_install: - sudo apt-get update - # Pull containers - - sudo docker pull centos:7 - - sudo docker pull ubuntu:14.04 - # Customize containers - - sudo docker build --rm=true --file=tests/Dockerfile.centos --tag=centos:ansible tests - - sudo docker build --rm=true --file=tests/Dockerfile.ubuntu --tag=ubuntu:ansible tests + # Pull container + - sudo docker pull ${distribution}:${version} + # Customize container + - sudo docker build --rm=true --file=tests/Dockerfile.${distribution} --tag=${distribution}:ansible tests script: # - # Run test playbook on Ubuntu container + # Run test playbook # # Run container in detached state - - sudo docker run --detach ubuntu:ansible /sbin/init > "${CONTAINER_ID}" - + - sudo docker run --detach ${distribution}:ansible /sbin/init > "${container_id}" # Syntax check - - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml --syntax-check + - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml --syntax-check # Test role - - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml + - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml # Idempotence test - > - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml + sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) # Clean up - - sudo docker stop "$(cat ${CONTAINER_ID})" - - # - # Run test playbook on CentOS container - # - - # Run container in detached state - - sudo docker run --detach --privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro centos:ansible /usr/lib/systemd/systemd > "${CONTAINER_ID}" - - # Syntax check - - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml --syntax-check - # Test role - - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml - # Idempotence test - - > - sudo docker exec "$(cat ${CONTAINER_ID})" ansible-playbook /etc/ansible/test.yml - | grep -q 'changed=0.*failed=0' - && (echo 'Idempotence test: pass' && exit 0) - || (echo 'Idempotence test: fail' && exit 1) - - # Clean up - - sudo docker stop "$(cat ${CONTAINER_ID})" + - sudo docker stop "$(cat ${container_id})" notifications: email: false From 1ac286c4ddfa12523ac4a5b0ae8eb7a6a5005287 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 13:48:44 +0100 Subject: [PATCH 08/11] Fix differences in init systems --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9eed15f..8d87722 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: required env: - - container_id=$(mktemp) distribution=centos version=7 - - container_id=$(mktemp) distribution=ubuntu version=14.04 + - container_id=$(mktemp) distribution=centos version=7 init=/usr/lib/systemd/systemd + - container_id=$(mktemp) distribution=ubuntu version=14.04 init=/sbin/init services: - docker @@ -19,7 +19,7 @@ script: # # Run container in detached state - - sudo docker run --detach ${distribution}:ansible /sbin/init > "${container_id}" + - sudo docker run --detach ${distribution}:ansible "${init}" > "${container_id}" # Syntax check - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml --syntax-check From b9d629ae98d241bbc72260d6823dfc16ae1fe03c Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sat, 12 Dec 2015 13:54:56 +0100 Subject: [PATCH 09/11] CentOS build should mount cgroups and run privileged --- .travis.yml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8d87722..7db524c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,17 @@ sudo: required env: - - container_id=$(mktemp) distribution=centos version=7 init=/usr/lib/systemd/systemd - - container_id=$(mktemp) distribution=ubuntu version=14.04 init=/sbin/init + - > + container_id=$(mktemp) + distribution=centos + version=7 + init=/usr/lib/systemd/systemd + run_opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" + - > + container_id=$(mktemp) + distribution=ubuntu + version=14.04 + init=/sbin/init + run_opts="" services: - docker @@ -19,7 +29,7 @@ script: # # Run container in detached state - - sudo docker run --detach ${distribution}:ansible "${init}" > "${container_id}" + - sudo docker run --detach ${run_opts} ${distribution}:ansible "${init}" > "${container_id}" # Syntax check - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml --syntax-check From 840423b1b9187fb1a43e29f04da50ac8f26d1807 Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sun, 13 Dec 2015 10:14:50 +0100 Subject: [PATCH 10/11] Test the current code, not the last release on Galaxy; colored output --- .travis.yml | 8 ++++---- tests/Dockerfile.centos | 3 --- tests/test.yml | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7db524c..0253d7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,15 +29,15 @@ script: # # Run container in detached state - - sudo docker run --detach ${run_opts} ${distribution}:ansible "${init}" > "${container_id}" + - sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distribution}:ansible "${init}" > "${container_id}" # Syntax check - - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml --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 # Test role - - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml + - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml # Idempotence test - > - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/test.yml + 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) diff --git a/tests/Dockerfile.centos b/tests/Dockerfile.centos index fcab1d2..888d502 100644 --- a/tests/Dockerfile.centos +++ b/tests/Dockerfile.centos @@ -18,9 +18,6 @@ RUN yum clean all 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 -COPY requirements.yml /etc/ansible/requirements.yml -RUN ansible-galaxy install -r /etc/ansible/requirements.yml -COPY test.yml /etc/ansible/test.yml VOLUME [ "/sys/fs/cgroup" ] CMD ["/usr/sbin/init"] diff --git a/tests/test.yml b/tests/test.yml index 0bf4014..1edb57a 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -8,4 +8,4 @@ - servername: "example.com" documentroot: "/var/www/vhosts/example_com" roles: - - geerlingguy.apache + - role_under_test From d8d1770482affdbe2cf3b04b872d62a939ae46ef Mon Sep 17 00:00:00 2001 From: Bert Van Vreckem Date: Sun, 13 Dec 2015 19:27:21 +0100 Subject: [PATCH 11/11] =?UTF-8?q?Don=E2=80=99t=20install=20test=20code=20o?= =?UTF-8?q?n=20Ubuntu=20Docker=20container,=20remove=20requirements.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Dockerfile.ubuntu | 3 --- tests/requirements.yml | 1 - 2 files changed, 4 deletions(-) delete mode 100644 tests/requirements.yml diff --git a/tests/Dockerfile.ubuntu b/tests/Dockerfile.ubuntu index f48ad29..3dd5383 100644 --- a/tests/Dockerfile.ubuntu +++ b/tests/Dockerfile.ubuntu @@ -6,7 +6,4 @@ RUN apt-get update RUN apt-get install -y ansible # Install Ansible inventory file RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts -COPY requirements.yml /etc/ansible/requirements.yml -RUN ansible-galaxy install -r /etc/ansible/requirements.yml -COPY test.yml /etc/ansible/test.yml diff --git a/tests/requirements.yml b/tests/requirements.yml deleted file mode 100644 index 9302d4d..0000000 --- a/tests/requirements.yml +++ /dev/null @@ -1 +0,0 @@ -- src: geerlingguy.apache