Skip to content

Commit a4eacfa

Browse files
committed
Merge pull request geerlingguy#84 from oxyc/docker
Issue geerlingguy#77: Add docker based tests
2 parents 693ad37 + 6e9b80c commit a4eacfa

File tree

9 files changed

+159
-34
lines changed

9 files changed

+159
-34
lines changed

.travis.yml

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,73 @@
11
---
2-
language: python
3-
python: "2.7"
2+
sudo: required
43

54
env:
6-
- SITE=test.yml
5+
- distribution: centos
6+
version: 6
7+
init: /sbin/init
8+
run_opts: ""
9+
playbook: test.yml
10+
- distribution: centos
11+
version: 7
12+
init: /usr/lib/systemd/systemd
13+
run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
14+
playbook: centos-7-test.yml
15+
- distribution: ubuntu
16+
version: 14.04
17+
init: /sbin/init
18+
run_opts: ""
19+
playbook: test.yml
20+
# - distribution: ubuntu
21+
# version: 12.04
22+
# init: /sbin/init
23+
# run_opts: ""
24+
# playbook: test.yml
725

8-
before_install:
9-
- sudo apt-get update -qq
10-
11-
# Remove MySQL. Completely and totally.
12-
- sudo apt-get remove --purge 'mysql*'
13-
- sudo apt-get autoremove
14-
- sudo apt-get autoclean
15-
- sudo rm -rf /var/lib/mysql
16-
- sudo truncate -s 0 /var/log/mysql/error.log
17-
18-
install:
19-
# Install Ansible.
20-
- pip install ansible
26+
services:
27+
- docker
2128

22-
# Add ansible.cfg to pick up roles path.
23-
- "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg"
29+
before_install:
30+
# Pull container
31+
- 'sudo docker pull ${distribution}:${version}'
32+
# Customize container
33+
- 'sudo docker build --rm=true --file=tests/Dockerfile.${distribution}-${version} --tag=${distribution}-${version}:ansible tests'
2434

2535
script:
26-
# Check the role/playbook's syntax.
27-
- "ansible-playbook -i tests/inventory tests/$SITE --syntax-check"
36+
- container_id=$(mktemp)
37+
# Run container in detached state
38+
- 'sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distribution}-${version}:ansible "${init}" > "${container_id}"'
39+
40+
# Ansible syntax check.
41+
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/${playbook} --syntax-check'
2842

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

32-
# Run the role/playbook again, checking to make sure it's idempotent.
46+
# Test role idempotence.
3347
- >
34-
ansible-playbook -i tests/inventory tests/$SITE --connection=local --sudo
48+
sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/${playbook}
3549
| grep -q 'changed=0.*failed=0'
3650
&& (echo 'Idempotence test: pass' && exit 0)
3751
|| (echo 'Idempotence test: fail' && exit 1)
3852
3953
# Some MySQL debugging (show all the logs).
40-
- "sudo ls -lah /var/log"
41-
- "sudo cat /var/log/mysql/error.log"
54+
- sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ls -lah /var/log
55+
- sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql/error.log || true
56+
- sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm cat /var/log/mysql.err || true
4257

4358
# Check to make sure we can connect to MySQL via Unix socket.
4459
- >
45-
mysql -u root -proot -e 'show databases;'
46-
| grep -q 'performance_schema'
60+
sudo docker exec "$(cat ${container_id})" mysql -u root -proot -e 'show databases;'
61+
| grep -q 'information_schema'
4762
&& (echo 'MySQL running normally' && exit 0)
4863
|| (echo 'MySQL not running' && exit 1)
4964
5065
# Check to make sure we can connect to MySQL via TCP.
5166
- >
52-
mysql -u root -proot -h 127.0.0.1 -e 'show databases;'
53-
| grep -q 'performance_schema'
67+
sudo docker exec "$(cat ${container_id})" mysql -u root -proot -h 127.0.0.1 -e 'show databases;'
68+
| grep -q 'information_schema'
5469
&& (echo 'MySQL running normally' && exit 0)
5570
|| (echo 'MySQL not running' && exit 1)
71+
72+
# Clean up
73+
- sudo docker stop "$(cat ${container_id})"

tasks/setup-Debian.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Because Ubuntu starts MySQL as part of the install process, we need to stop
1919
# mysql and remove the logfiles in case the user set a custom log file size.
2020
- name: Ensure MySQL is stopped after initial install.
21-
service: name=mysql state=stopped
21+
service: "name={{ mysql_daemon }} state=stopped"
2222
when: mysql_installed.stat.exists == false
2323

2424
- name: Delete innodb log files created by apt package after initial install.

tests/Dockerfile.centos-6

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM centos:6
2+
3+
# Install Ansible
4+
RUN yum -y update; yum clean all;
5+
RUN yum -y install epel-release
6+
RUN yum -y install git ansible sudo
7+
RUN yum clean all
8+
9+
# Disable requiretty
10+
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers
11+
12+
# Install Ansible inventory file
13+
RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts
14+
15+
CMD ["/usr/sbin/init"]

tests/Dockerfile.centos-7

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM centos:7
2+
3+
# Install systemd -- See https://hub.docker.com/_/centos/
4+
RUN yum -y swap -- remove fakesystemd -- install systemd systemd-libs
5+
RUN yum -y update; yum clean all; \
6+
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
7+
rm -f /lib/systemd/system/multi-user.target.wants/*; \
8+
rm -f /etc/systemd/system/*.wants/*; \
9+
rm -f /lib/systemd/system/local-fs.target.wants/*; \
10+
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
11+
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
12+
rm -f /lib/systemd/system/basic.target.wants/*; \
13+
rm -f /lib/systemd/system/anaconda.target.wants/*;
14+
15+
# Install Ansible
16+
RUN yum -y install epel-release
17+
RUN yum -y install git ansible sudo
18+
RUN yum clean all
19+
20+
# Disable requiretty
21+
RUN sed -i -e 's/^\(Defaults\s*requiretty\)/#--- \1/' /etc/sudoers
22+
23+
# Install Ansible inventory file
24+
RUN echo -e '[local]\nlocalhost ansible_connection=local' > /etc/ansible/hosts
25+
26+
VOLUME ["/sys/fs/cgroup"]
27+
CMD ["/usr/sbin/init"]

tests/Dockerfile.ubuntu-12.04

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM ubuntu:12.04
2+
RUN apt-get update
3+
4+
# Install Ansible
5+
RUN apt-get install -y software-properties-common python-software-properties git
6+
RUN apt-add-repository -y ppa:ansible/ansible
7+
RUN apt-get update
8+
RUN apt-get install -y ansible
9+
10+
COPY initctl_faker .
11+
RUN chmod +x initctl_faker && rm -fr /sbin/initctl && ln -s /initctl_faker /sbin/initctl
12+
13+
# Install Ansible inventory file
14+
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts

tests/Dockerfile.ubuntu-14.04

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM ubuntu:14.04
2+
RUN apt-get update
3+
4+
# Install Ansible
5+
RUN apt-get install -y software-properties-common git
6+
RUN apt-add-repository -y ppa:ansible/ansible
7+
RUN apt-get update
8+
RUN apt-get install -y ansible
9+
10+
COPY initctl_faker .
11+
RUN chmod +x initctl_faker && rm -fr /sbin/initctl && ln -s /initctl_faker /sbin/initctl
12+
13+
# Install Ansible inventory file
14+
RUN echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts

tests/centos-7-test.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
- hosts: all
3+
vars:
4+
mysql_packages:
5+
- mariadb
6+
- mariadb-server
7+
- mariadb-libs
8+
- MySQL-python
9+
- perl-DBD-MySQL
10+
mysql_daemon: mariadb
11+
mysql_log_error: /var/log/mariadb/mariadb.log
12+
mysql_syslog_tag: mariadb
13+
mysql_pid_file: /var/run/mariadb/mariadb.pid
14+
roles:
15+
- role_under_test

tests/initctl_faker

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
ALIAS_CMD="$(echo ""$0"" | sed -e 's?/sbin/??')"
3+
4+
case "$ALIAS_CMD" in
5+
start|stop|restart|reload|status)
6+
exec service $1 $ALIAS_CMD
7+
;;
8+
esac
9+
10+
case "$1" in
11+
list )
12+
exec service --status-all
13+
;;
14+
reload-configuration )
15+
exec service $2 restart
16+
;;
17+
start|stop|restart|reload|status)
18+
exec service $2 $1
19+
;;
20+
\?)
21+
exit 0
22+
;;
23+
esac

tests/test.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
- hosts: localhost
3-
remote_user: root
2+
- hosts: all
43
roles:
5-
- ansible-role-mysql
4+
- role_under_test

0 commit comments

Comments
 (0)