Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ galaxy_info:
description: Role to install Oracle Java.
company: ansiblebit.org
license: BSD
min_ansible_version: 1.9.3
min_ansible_version: 1.9.4
platforms:
- name: CentOS
versions:
Expand Down
30 changes: 13 additions & 17 deletions tasks/check_environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
# oracle_java_installed.rc == 0 : installed
# oracle_java_installed.rc == 1 : not installed

- name: echo oracle_java_task_installed
debug:
msg="oracle_java_task_installed={{ oracle_java_task_installed }}"
- debug:
var=oracle_java_task_installed
when: oracle_java_task_installed is defined
tags:
- debug

- name: set fact oracle_java_installed
set_fact:
oracle_java_installed={{ oracle_java_task_installed.rc == 0 }}
when: oracle_java_task_installed is defined and oracle_java_task_installed.rc is defined
changed_when: False

- name: echo oracle_java_installed
debug:
msg="oracle_java_installed={{ oracle_java_installed }}"
- debug:
var=oracle_java_installed
when: oracle_java_installed is defined
tags:
- debug
Expand All @@ -40,33 +39,30 @@

- name: if Java is installed, check version
shell: java -version 2>&1 | head -n 1 | awk '{ print $3 }' | awk -F '"' '{ print $2 }'
when: oracle_java_installed
when: oracle_java_installed is defined
register: oracle_java_task_version
changed_when: False

- name: echo oracle_java_task_version
debug:
msg="oracle_java_task_version={{ oracle_java_task_version }}"
- debug:
var=oracle_java_task_version
when: oracle_java_task_version is defined
tags:
- debug

- name: set fact oracle_java_installed_version
set_fact:
oracle_java_version_installed={{ oracle_java_task_version.stdout }}
when: oracle_java_installed
when: oracle_java_task_version is defined and oracle_java_task_version.stdout is defined
changed_when: False

- name: echo oracle_java_version_installed
debug:
msg="oracle_java_version_installed={{ oracle_java_version_installed }}"
- debug:
var=oracle_java_version_installed
when: oracle_java_version_installed is defined
tags:
- debug

- name: echo oracle_java_version_string
debug:
msg="oracle_java_version_string={{ oracle_java_version_string }}"
- debug:
var=oracle_java_version_string
when: oracle_java_version_string is defined
tags:
- debug
4 changes: 2 additions & 2 deletions tasks/redhat/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
name="{{ item.exe }}"
link="/usr/bin/{{ item.exe }}"
path="{{ item.path }}/{{ item.exe }}"
when: oracle_java_set_as_default
with_items:
- { path: "{{ oracle_java_home }}/jre/bin", exe: 'java' }
- { path: "{{ oracle_java_home }}/jre/bin", exe: 'keytool' }
- { path: "{{ oracle_java_home }}/bin", exe: 'javac' }
- { path: "{{ oracle_java_home }}/bin", exe: 'javadoc' }
sudo: yes
when: oracle_java_task_rpm_download|changed or (oracle_java_installed and oracle_java_version_installed != oracle_java_version_string)
when: (oracle_java_set_as_default and oracle_java_task_rpm_download|changed) or
(oracle_java_set_as_default and oracle_java_installed and oracle_java_version_installed != oracle_java_version_string)
register: oracle_java_task_set_default

- name: in case there were changes, check host environment again
Expand Down
42 changes: 31 additions & 11 deletions tests/plugins/callback/idempotence.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, print_function)

import sys
import os

VAR_IDEMPOTENCE = u'idempotence'
from ansible import constants as C
from ansible.constants import mk_boolean

try:
from ansible.plugins.callback import CallbackBase
parent = CallbackBase
except ImportError:
parent = object


VAR_IDEMPOTENCE = u'IDEMPOTENCE'


class CallbackModule(object):
class CallbackModule(parent):
"""
This callback module performs the idempotence test whenever the 'idempotence' variable is set to True.
This callback module performs the idempotency test whenever the 'idempotency' variable is set to True.
"""
CALLBACK_VERSION = 2.0
CALLBACK_NAME = 'idempotency'

def __init__(self):
pass
self.playbook = None
self.enabled = mk_boolean(os.getenv(VAR_IDEMPOTENCE, 'no'))

def playbook_on_stats(self, stats):
super(CallbackModule, self).__init__()

if (u'%s' % VAR_IDEMPOTENCE) in self.playbook.extra_vars.keys() and self.playbook.extra_vars[VAR_IDEMPOTENCE]:
def playbook_on_stats(self, stats):

if self.enabled:
if len(stats.dark) > 0:
print ('idempotence test failed! unreachable=%s > 0') % stats.dark
self._display.warning('idempotency test failed: unreachable=%s > 0' % stats.dark)
sys.exit(os.EX_SOFTWARE)

if len(stats.changed) > 0:
print ('idempotence test failed! changed=%s > 0') % stats.changed
self._display.warning('idempotency test failed: changed=%s > 0' % stats.changed)
sys.exit(os.EX_SOFTWARE)

if len(stats.failures) > 0:
print ('idempotence test failed! failures=%s > 0') % stats.failures
self._display.warning('idempotency test failed: failures=%s > 0' % stats.failures)
sys.exit(os.EX_SOFTWARE)

def v2_playbook_on_stats(self, stats):
"""Verify that playbook ran without any changes or failures."""
self.playbook_on_stats(stats)
9 changes: 9 additions & 0 deletions tests/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@
fail:
msg="java -version output was {{ result_java_version.stdout }} instead of {{ expected_java_version }}"
when: result_java_version.stdout != expected_java_version

- name: ensure keytool and javadoc are present
shell: "test -h /usr/bin/{{ item }}"
changed_when: no
with_items:
- keytool
- java
- javadoc

2 changes: 1 addition & 1 deletion tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

vars:
test_java_version: 8
test_java_version_update: 72
test_java_version_update: 74
test_java_version_build: 15

roles:
Expand Down
117 changes: 117 additions & 0 deletions tests/test_checkmode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env bash
# #################
#
# Bash script to run check mode tests.
#
# version: 1.5
#
# usage:
#
# test_checkmode [options]
#
# options:
#
# --box The name of the Vagrant box or host name
# --env The name of the test environment
# --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_checkmode.sh
#
# # on a Vagrant box
# bash test_checkmode.sh \
# --box precise64.vagrant.dev \
# --inventory .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
#
#
# changelog:
#
# v1.5 : 8 Mar 2016
# - pass vagrant_box variable to playbook
# - default inventory changed from localhost to match what Vagrant provisioner generates
# v1.4 : 10 Jul 2015
# - added extra variables to force running idempotence tests on vagrant
# v1.2
# - added env option
#
# author(s):
# - Pedro Salgado <steenzout@ymail.com>
#
# #################


# GREEN : SGR code to set text color (foreground) to green.
GREEN='\033[0;32m'
# RED : SGR code to set text color (foreground) to red.
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.* unreachable=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;;

--env)
# the test environment
ENV="$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=${BOX:-localhost}
# the Ansible inventory
INVENTORY=${INVENTORY:-'.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory'}
# the path to the Ansible test playbook
PLAYBOOK=${PLAYBOOK:-test.yml}
# the logfile to hold the output of the playbook run
LOGFILE="log/${BOX}_checkmode_${VIRTUALENV_NAME}.log"

EXTRA_ARGS=''
if [ $BOX == "localhost" ]; then
INVENTORY='localhost,'
EXTRA_ARGS="--connection=local -e env=${ENV} -e vagrant_box=localhost --skip-tags=test"
else
EXTRA_ARGS="--user vagrant -e env=vagrant -e vagrant_box=${BOX} --skip-tags=test"
fi

echo "[INFO] ${BOX} ${VIRTUALENV_NAME} running checkmode test..."
ansible-playbook -vvvv --check --diff -i ${INVENTORY} --limit ${BOX}, ${EXTRA_ARGS} ${PLAYBOOK} 2>&1 | \
tee ${LOGFILE} | \
grep "${BOX}" | grep -q "${PASS_CRITERIA}" && \
echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} checkmode : ${GREEN}PASS${NC}\n" || ( \
cat ${LOGFILE} &&
echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} checkmode : ${RED}FAILED${NC} ${PASS_CRITERIA}\n" && \
exit 1)
30 changes: 19 additions & 11 deletions tests/test_idempotence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Bash script to run idempotence tests.
#
# version: 1.4
# version: 1.5
#
# usage:
#
Expand All @@ -23,12 +23,15 @@
#
# # on a Vagrant box
# bash test_idempotence.sh \
# --box precise64.vagrant.dev
# --box precise64.vagrant.dev \
# --inventory .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
#
#
# changelog:
#
# v1.5 : 8 Mar 2016
# - pass vagrant_box variable to playbook
# - default inventory changed from localhost to match what Vagrant provisioner generates
# v1.4 : 10 Jul 2015
# - added extra variables to force running idempotence tests on vagrant
# v1.2
Expand All @@ -47,7 +50,7 @@ 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.*unreachable=0.*failed=0"
PASS_CRITERIA="changed=0.* unreachable=0.* failed=0"

# the name of the virtualenv
VIRTUALENV_NAME=$(which python | awk -F / 'NF && NF-2 { print ( $(NF-2) ) }')
Expand Down Expand Up @@ -89,22 +92,27 @@ done

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

EXTRA_ARGS=''
if [ $BOX == "localhost" ]; then
EXTRA_ARGS="--connection=local --extra-vars idempotence=yes --extra-vars env=${ENV}"
INVENTORY='localhost,'
EXTRA_ARGS="--connection=local -e env=${ENV} -e vagrant_box=localhost"
else
EXTRA_ARGS="--u vagrant --extra-vars idempotence=yes --extra-vars env=vagrant"
EXTRA_ARGS="--user vagrant -e env=vagrant -e vagrant_box=${BOX}"
fi

echo "[INFO] ${BOX} ${VIRTUALENV_NAME} running idempotence test..."
ansible-playbook -i ${INVENTORY} --limit ${BOX}, ${EXTRA_ARGS} ${PLAYBOOK} 2>&1 | tee ${LOGFILE} | \
IDEMPOTENCE='yes' \
ansible-playbook -vvvv -i ${INVENTORY} --limit ${BOX}, ${EXTRA_ARGS} ${PLAYBOOK} 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" && cat ${LOGFILE} && exit 1)
echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} idempotence : ${GREEN}PASS${NC}\n" || ( \
cat ${LOGFILE} &&
echo -ne "[TEST] ${BOX} ${VIRTUALENV_NAME} idempotence : ${RED}FAILED${NC} ${PASS_CRITERIA}\n" && \
exit 1)
1 change: 1 addition & 0 deletions tests/vagrant.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ do
fi

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

echo "[INFO] destroy ${VAGRANT_BOX}..."
vagrant destroy -f ${VAGRANT_BOX}
Expand Down
Loading