Skip to content

Commit 08c4162

Browse files
author
Colin Hoglund
authored
Merge pull request #4 from singleplatform-eng/SPROD-3862_make_python_role_idempotent
Sprod 3862 make python role idempotent
2 parents f175015 + 1b0e4cb commit 08c4162

File tree

6 files changed

+106
-29
lines changed

6 files changed

+106
-29
lines changed

.travis.yml

+6-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
---
2-
language: python
3-
python: "2.7"
2+
services: docker
43

5-
# Use the new container infrastructure
6-
sudo: false
7-
8-
# Install ansible
9-
addons:
10-
apt:
11-
packages:
12-
- python-pip
13-
14-
install:
15-
# Install ansible
16-
- pip install ansible
17-
18-
# Check ansible version
19-
- ansible --version
20-
21-
# Create ansible.cfg with correct roles_path
22-
- printf '[defaults]\nroles_path=../' >ansible.cfg
4+
env:
5+
- distro: ubuntu1404
236

247
script:
25-
# Basic role syntax check
26-
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
8+
# Run tests.
9+
- ${PWD}/tests/test.sh
2710

2811
notifications:
29-
webhooks: https://galaxy.ansible.com/api/v1/notifications/
12+
webhooks: https://galaxy.ansible.com/api/v1/notifications/

meta/main.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ galaxy_info:
88
platforms:
99
- name: Ubuntu
1010
versions:
11-
- all
12-
- name: Debian
13-
versions:
14-
- all
11+
- trusty
1512

1613
galaxy_tags:
1714
- python

tasks/install_package.yml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
shell: "apt-cache show {{python_package_name}} | grep 'Version:' | awk '{print $2}' | cut -d. -f1"
1919
check_mode: no
2020
register: python_version_output
21+
changed_when: False #This just polls the system for information and doesn't change system information
2122

2223
- name: Set python_major_version
2324
set_fact:
@@ -27,6 +28,7 @@
2728
command: "which python{{python_major_version}}"
2829
check_mode: no
2930
register: python_location_output
31+
changed_when: False #This just polls the system for information and doesn't change system information
3032

3133
- name: Set python_path
3234
set_fact:

tests/inventory

-1
This file was deleted.

tests/test.sh

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
#
3+
# Ansible role test shim.
4+
#
5+
# Usage: [OPTIONS] ./tests/test.sh
6+
# - distro: a supported Docker distro version (default = "centos7")
7+
# - playbook: a playbook in the tests directory (default = "test.yml")
8+
# - cleanup: whether to remove the Docker container (default = true)
9+
# - container_id: the --name to set for the container (default = timestamp)
10+
11+
# Exit on any individual command failure.
12+
set -e
13+
14+
# Pretty colors.
15+
red='\033[0;31m'
16+
green='\033[0;32m'
17+
neutral='\033[0m'
18+
19+
timestamp=$(date +%s)
20+
21+
# Allow environment variables to override defaults.
22+
distro=${distro:-"centos7"}
23+
playbook=${playbook:-"test.yml"}
24+
cleanup=${cleanup:-"true"}
25+
container_id=${container_id:-$timestamp}
26+
27+
## Set up vars for Docker setup.
28+
# CentOS 7
29+
if [ $distro = 'centos7' ]; then
30+
init="/usr/lib/systemd/systemd"
31+
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
32+
# CentOS 6
33+
elif [ $distro = 'centos6' ]; then
34+
init="/sbin/init"
35+
opts="--privileged"
36+
# Ubuntu 16.04
37+
elif [ $distro = 'ubuntu1604' ]; then
38+
init="/lib/systemd/systemd"
39+
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
40+
# Ubuntu 14.04
41+
elif [ $distro = 'ubuntu1404' ]; then
42+
init="/sbin/init"
43+
opts="--privileged"
44+
# Ubuntu 12.04
45+
elif [ $distro = 'ubuntu1204' ]; then
46+
init="/sbin/init"
47+
opts="--privileged"
48+
# Debian 8
49+
elif [ $distro = 'debian8' ]; then
50+
init="/lib/systemd/systemd"
51+
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
52+
# Fedora 24
53+
elif [ $distro = 'fedora24' ]; then
54+
init="/usr/lib/systemd/systemd"
55+
opts="--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro"
56+
fi
57+
58+
# Run the container using the supplied OS.
59+
printf ${green}"Starting Docker container: geerlingguy/docker-$distro-ansible."${neutral}"\n"
60+
docker pull geerlingguy/docker-$distro-ansible:latest
61+
docker run --detach --volume="$PWD":/etc/ansible/roles/role_under_test:rw --name $container_id $opts geerlingguy/docker-$distro-ansible:latest $init
62+
63+
printf "\n"
64+
65+
# Install requirements if `requirements.yml` is present.
66+
if [ -f "$PWD/tests/requirements.yml" ]; then
67+
printf ${green}"Requirements file detected; installing dependencies."${neutral}"\n"
68+
docker exec --tty $container_id env TERM=xterm ansible-galaxy install -r /etc/ansible/roles/role_under_test/tests/requirements.yml
69+
fi
70+
71+
printf "\n"
72+
73+
# Test Ansible syntax.
74+
printf ${green}"Checking Ansible playbook syntax."${neutral}
75+
docker exec --tty $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook --syntax-check
76+
77+
printf "\n"
78+
79+
# Run Ansible playbook.
80+
printf ${green}"Running command: docker exec $container_id env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook"${neutral}
81+
docker exec $container_id env TERM=xterm env ANSIBLE_FORCE_COLOR=1 ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook
82+
83+
# Run Ansible playbook again (idempotence test).
84+
printf ${green}"Running playbook again: idempotence test"${neutral}
85+
idempotence=$(mktemp)
86+
docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook | tee -a $idempotence
87+
tail $idempotence \
88+
| grep -q 'changed=0.*failed=0' \
89+
&& (printf ${green}'Idempotence test: pass'${neutral}"\n") \
90+
|| (printf ${red}'Idempotence test: fail'${neutral}"\n" && exit 1)
91+
92+
# Remove the Docker container (if configured).
93+
if [ "$cleanup" = true ]; then
94+
printf "Removing Docker container...\n"
95+
docker rm -f $container_id
96+
fi

tests/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
- hosts: localhost
33
remote_user: root
44
roles:
5-
- ansible-role-python
5+
- role_under_test

0 commit comments

Comments
 (0)