Skip to content

Commit

Permalink
refactoring of testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
steenzout committed May 4, 2015
1 parent 0710b43 commit 1e43e7c
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 70 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ install:

script:
- tox -e travis

Empty file added requirements.yml
Empty file.
2 changes: 1 addition & 1 deletion tests/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'yaml'

PLAYBOOK = 'vagrant.yml'
CONFIGURATION_FILE = 'group_vars/all.yml'
CONFIGURATION_FILE = 'boxes.yml'

Vagrant.configure('2') do |config|

Expand Down
2 changes: 1 addition & 1 deletion tests/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ host_key_checking = False

private_key_file = ~/.vagrant.d/insecure_private_key

roles_path = ../../
roles_path = ../../:../
File renamed without changes.
1 change: 0 additions & 1 deletion tests/requirements.yml

This file was deleted.

11 changes: 11 additions & 0 deletions tests/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
# file: primogen/tests/tasks
#
# Test tasks to verify role execution.
#

- debug:
msg="You need to define tests here!"
tags:
- debug
- test
54 changes: 54 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# file: primogen/tests/vagrant.yml

- name: set host entries play
hosts: 127.0.0.1
connection: local
gather_facts: no

vars:
hosts_file: /etc/hosts

tasks:
- name: set entry in /etc/hosts
lineinfile:
dest="{{ hosts_file }}"
line="{{ item.value.network.ip }} {{ item.key }}"
state=present
follow=yes
insertafter=EOF
with_dict: vagrant
when: item.value.enabled
sudo: yes
tags:
- vagrant


- name: tests play
hosts: all
gather_facts: yes

roles:
- primogen
- tests


- name: clear host entries play
hosts: 127.0.0.1
connection: local
gather_facts: no

vars:
hosts_file: /etc/hosts

tasks:
- name: clear entry in /etc/hosts
lineinfile:
dest="{{ hosts_file }}"
regexp=".*{{ item.key }}$"
state=absent
follow=yes
with_dict: vagrant
sudo: yes
tags:
- vagrant
75 changes: 65 additions & 10 deletions tests/test_idempotence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
#
# Bash script to run idempotence tests.
#
# version: 1.0
# version: 1.2
#
# mandatory variables:
# usage:
#
# from environment.sh
# - INVENTORY : the inventory to be used to run the test against.
# - PLAYBOOK : the path to the Ansible playbook.
# test_idempotence [options]
#
# from <playbook_name>.sh
# - BOX : the name of the vagrant box
# options:
#
# optional variables:
# --box The name of the Vagrant box or host name
# --inventory The Ansible inventory in the form of a file or string "host,"
# --playbook The path to the Ansible test playbook
#
# example:
#
# # on localhost
# bash test_idempotence.sh
#
# # on a Vagrant box
# bash test_idempotence.sh \
# --box precise64
# --inventory .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
#
# - VIRTUALENV_NAME : the name of the virtualenv being used.
#
# author(s):
# - Pedro Salgado <[email protected]>
Expand All @@ -30,11 +38,58 @@ GREEN='\033[0;32m'
RED='\033[0;31m'
# SGR code to set text color (foreground) to no color.
NC='\033[0m'
# The idempotence pass criteria.
PASS_CRITERIA="changed=0.*failed=0"

# the name of the virtualenv
VIRTUALENV_NAME=$(which python | awk -F / 'NF && NF-2 { print ( $(NF-2) ) }')


while [[ $# > 1 ]]
do
key="$1"

case $key in

--box)
# the name of the Vagrant box or host name
BOX="$2"
shift;;

--inventory)
# the Ansible inventory in the form of a file or string "host,"
INVENTORY="$2"
shift;;

--playbook)
# the path to the Ansible test playbook
PLAYBOOK="$2"
shift;;

*)
# unknown option
;;

esac
shift
done

# the name of the Vagrant box or host name
BOX=${1:-localhost}
# the Ansible inventory in the form of a file or string "host,"
INVENTORY=${2:-'localhost,'}
# the path to the Ansible test playbook
PLAYBOOK=${3:-test.yml}
# the logfile to hold the output of the playbook run
LOGFILE="log/${BOX}_${VIRTUALENV_NAME}.log"

EXTRA_ARGS=''
if [ $BOX == "localhost" ]; then
EXTRA_ARGS="--skip-tags=vagrant --connection=local"
fi

echo "[INFO] ${BOX} ${VIRTUALENV_NAME} running idempotence test..."
ansible-playbook -i ${INVENTORY} --limit ${BOX} ${PLAYBOOK} 2>&1 | tee ${LOGFILE} | \
ansible-playbook -i ${INVENTORY} --limit ${BOX} ${PLAYBOOK} ${EXTRA_ARGS} 2>&1 | tee ${LOGFILE} | \
grep "${BOX}" | grep -q "${PASS_CRITERIA}" && \
echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} idempotence : ${GREEN}PASS${NC}\n" || \
(echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} idempotence : ${RED}FAILED${NC} ${PASS_CRITERIA}\n" && exit 1)
77 changes: 24 additions & 53 deletions tests/vagrant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#
# Bash script to run the test suite against the Vagrant environment.
#
# version: 1.0
# version: 1.2
#
# usage:
#
# $ bash vagrant.sh --python 27 --ansible 1901
# # run tests against all enabled Vagrant boxes
# $ bash vagrant.sh
#
# # run tests against a single Vagrant box
# $ bash vagrant.sh --box precise64.vagrant.dev
#
# $ bash vagrant.sh --virtualenv ../.tox/py27-ansible1901 --virtualenv-name py27-ansible1901
#
# author(s):
# - Pedro Salgado <[email protected]>
Expand All @@ -20,13 +23,10 @@ DIR="$(dirname "$0")"

cd $DIR

source environment.sh

# The filename of the Ansible playbook to be used on the test.
# NOTE: PLAYBOOK must be the same value as defined in the Vagrantfile.
PLAYBOOK="vagrant.yml"
# force Ansible to ask for sudo password when running tests against Vagrant
ANSIBLE_ASK_SUDO_PASS=True

# Inventory file to run tests againt (points to Vagrant generated inventory)
# the path to the Ansible inventory generated by Vagrant
INVENTORY=${DIR}/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory


Expand All @@ -36,26 +36,9 @@ key="$1"

case $key in

--python)
# The Python version to be used on the test.
# NOTE: PYTHON_VERSION must be set to a Python version defined in tox.
PYTHON_VERSION="$2"
shift;;

--ansible)
# The Ansible version to be used on the test.
# ANSIBLE_VERSION must be set to a Ansible version defined in tox.
ANSIBLE_VERSION="$2"
shift;;

--virtualenv-name)
# The tox virtualenv name
VIRTUALENV_NAME="$2"
shift;;

--virtualenv)
# The virtualenv directory to be used on the test.
VIRTUALENV="$2"
--box)
# the name of the Vagrant box to run tests against
BOX="$2"
shift;;

*)
Expand All @@ -66,32 +49,20 @@ key="$1"
shift
done

if [[ ! -z ${PYTHON_VERSION} ]] && [[ ! -z ${ANSIBLE_VERSION} ]]; then
echo '[INFO] loading Python / Ansible virtualenv...'
VIRTUALENV="../.tox/py${PYTHON_VERSION}-ansible${ANSIBLE_VERSION}"
VIRTUALENV_NAME="py${PYTHON_VERSION}-ansible${ANSIBLE_VERSION}"
fi

source ${VIRTUALENV}/bin/activate


. install_role_dependencies.sh

# force Ansible to ask for sudo password when running tests against Vagrant
export ANSIBLE_ASK_SUDO_PASS=True

for BOX in `grep vagrant.dev group_vars/all.yml | sed 's/://g'`
for VAGRANT_BOX in `grep vagrant.dev boxes.yml | sed 's/://g'`
do
if [ -n ${BOX} ] && [ "${BOX}" == "${VAGRANT_BOX}" ]; then

echo "[INFO] preparing ${BOX}..."
vagrant up ${BOX} 2> /dev/null
if [ $? -ne 0 ]; then
# box not enabled
continue
fi
echo "[INFO] preparing ${VAGRANT_BOX}..."
vagrant up ${VAGRANT_BOX} 2> /dev/null
if [ $? -ne 0 ]; then
# box not enabled
continue
fi

. test_idempotence.sh
. ${DIR}/test_idempotence.sh --box ${VAGRANT_BOX} --inventory $INVENTORY

echo "[INFO] destroy ${BOX}..."
vagrant destroy -f ${BOX}
echo "[INFO] destroy ${VAGRANT_BOX}..."
vagrant destroy -f ${VAGRANT_BOX}
fi
done
13 changes: 10 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ skipsdist = True
[testenv]
changedir = tests
deps =
travis: ansible
travis: ansible==1.9.0.1
ansible1901: ansible==1.9.0.1
ansible184: ansible==1.8.4
ansible172: ansible==1.7.2
Expand All @@ -19,15 +19,22 @@ deps =
# ansible123: ansible==1.2.3

commands =
bash vagrant.sh --virtualenv {envdir} --virtualenv-name {envname}
bash -c "test -s ../requirements.yml && ansible-galaxy install --force -r ../requirements.yml || true"
bash vagrant.sh

whitelist_externals =
bash


[testenv:travis]
commands =
bash localhost.sh
ansible-playbook -i localhost, --skip-tags=vagrant --connection=local test.yml
bash test_idempotence.sh

whitelist_externals =
ansible-galaxy
ansible-playbook
bash


[testenv:docs]
Expand Down

0 comments on commit 1e43e7c

Please sign in to comment.