From d190734e84b47568baf418941149a27cc2b6110e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Wed, 3 Feb 2016 14:48:05 -0500 Subject: [PATCH] Add initial docker setup --- .travis.yml | 73 +++++++++++++++++++++-------------- tasks/setup-Debian.yml | 2 +- tests/Dockerfile.centos-6 | 15 +++++++ tests/Dockerfile.centos-7 | 27 +++++++++++++ tests/Dockerfile.ubuntu-12.04 | 11 ++++++ tests/Dockerfile.ubuntu-14.04 | 11 ++++++ tests/test.yml | 5 +-- 7 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 tests/Dockerfile.centos-6 create mode 100644 tests/Dockerfile.centos-7 create mode 100644 tests/Dockerfile.ubuntu-12.04 create mode 100644 tests/Dockerfile.ubuntu-14.04 diff --git a/.travis.yml b/.travis.yml index f794fb4..ba4db13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,55 +1,72 @@ --- -language: python -python: "2.7" +sudo: required env: - - SITE=test.yml + matrix: + - distribution: centos + version: 6 + init: /sbin/init + run_opts: "" + - 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: "" + - distribution: ubuntu + version: 12.04 + init: /sbin/init + run_opts: "" + global: + - testfile: test.yml -before_install: - - sudo apt-get update -qq - - # Remove MySQL. Completely and totally. - - sudo apt-get remove --purge 'mysql*' - - sudo apt-get autoremove - - sudo apt-get autoclean - - sudo rm -rf /var/lib/mysql - - sudo truncate -s 0 /var/log/mysql/error.log - -install: - # Install Ansible. - - pip install ansible +services: + - docker - # Add ansible.cfg to pick up roles path. - - "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg" +before_install: + # Pull container + - 'sudo docker pull ${distribution}:${version}' + # Customize container + - 'sudo docker build --rm=true --file=tests/Dockerfile.${distribution}-${version} --tag=${distribution}-${version}:ansible tests' script: - # Check the role/playbook's syntax. - - "ansible-playbook -i tests/inventory tests/$SITE --syntax-check" + - 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}"' + + # Ansible syntax check. + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/${testsite} --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/${testsite}' - # 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/${testsite} | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) # Some MySQL debugging (show all the logs). - - "sudo ls -lah /var/log" - - "sudo cat /var/log/mysql/error.log" + - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ls -lah /var/log + - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql/error.log || true + - sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql.err || true # Check to make sure we can connect to MySQL via Unix socket. - > - mysql -u root -proot -e 'show databases;' + sudo docker exec "$(cat ${container_id})" mysql -u root -proot -e 'show databases;' | grep -q 'performance_schema' && (echo 'MySQL running normally' && exit 0) || (echo 'MySQL not running' && exit 1) # Check to make sure we can connect to MySQL via TCP. - > - mysql -u root -proot -h 127.0.0.1 -e 'show databases;' + sudo docker exec "$(cat ${container_id})" mysql -u root -proot -h 127.0.0.1 -e 'show databases;' | grep -q 'performance_schema' && (echo 'MySQL running normally' && exit 0) || (echo 'MySQL not running' && exit 1) + + # Clean up + - 'sudo docker stop "$(cat ${container_id})"' diff --git a/tasks/setup-Debian.yml b/tasks/setup-Debian.yml index 1158456..4ead891 100644 --- a/tasks/setup-Debian.yml +++ b/tasks/setup-Debian.yml @@ -18,7 +18,7 @@ # Because Ubuntu starts MySQL as part of the install process, we need to stop # mysql and remove the logfiles in case the user set a custom log file size. - name: Ensure MySQL is stopped after initial install. - service: name=mysql state=stopped + service: "name={{ mysql_daemon }} state=stopped" when: mysql_installed.stat.exists == false - name: Delete innodb log files created by apt package after initial install. diff --git a/tests/Dockerfile.centos-6 b/tests/Dockerfile.centos-6 new file mode 100644 index 0000000..4a4e7b8 --- /dev/null +++ b/tests/Dockerfile.centos-6 @@ -0,0 +1,15 @@ +FROM centos:6 + +# Install Ansible +RUN yum -y update; yum clean all; +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 + +CMD ["/usr/sbin/init"] diff --git a/tests/Dockerfile.centos-7 b/tests/Dockerfile.centos-7 new file mode 100644 index 0000000..8aa0654 --- /dev/null +++ b/tests/Dockerfile.centos-7 @@ -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"] diff --git a/tests/Dockerfile.ubuntu-12.04 b/tests/Dockerfile.ubuntu-12.04 new file mode 100644 index 0000000..d0c130c --- /dev/null +++ b/tests/Dockerfile.ubuntu-12.04 @@ -0,0 +1,11 @@ +FROM ubuntu:12.04 +RUN apt-get update + +# Install Ansible +RUN apt-get install -y software-properties-common python-software-properties 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 diff --git a/tests/Dockerfile.ubuntu-14.04 b/tests/Dockerfile.ubuntu-14.04 new file mode 100644 index 0000000..ca33287 --- /dev/null +++ b/tests/Dockerfile.ubuntu-14.04 @@ -0,0 +1,11 @@ +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 + +# Install Ansible inventory file +RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts diff --git a/tests/test.yml b/tests/test.yml index bfe6c6c..0ed0b43 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -1,5 +1,4 @@ --- -- hosts: localhost - remote_user: root +- hosts: all roles: - - ansible-role-mysql + - role_under_test