From 4f485a2b4342cac6b93073c7878c0e3134da5a5f Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 22 Feb 2016 11:54:37 -0700 Subject: [PATCH 1/6] updated galaxy tag minimum version to 1.9.4 --- meta/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta/main.yml b/meta/main.yml index 13e4098..37c3bed 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -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: From 8dda13ff33805d5377e49885601aba1804248ab0 Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 21 Mar 2016 13:41:48 -0600 Subject: [PATCH 2/6] use 1.8.0_74 for testing --- tests/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.yml b/tests/test.yml index bd7b541..0597b58 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -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: From f8b130e514ab9072a8dd805c9cee85c832b66160 Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 21 Mar 2016 14:00:01 -0600 Subject: [PATCH 3/6] improve test scripts --- tests/plugins/callback/idempotence.py | 42 ++++++--- tests/test_checkmode.sh | 117 ++++++++++++++++++++++++++ tests/test_idempotence.sh | 30 ++++--- tests/vagrant.sh | 1 + 4 files changed, 168 insertions(+), 22 deletions(-) create mode 100644 tests/test_checkmode.sh diff --git a/tests/plugins/callback/idempotence.py b/tests/plugins/callback/idempotence.py index 2d82adf..f583e1b 100644 --- a/tests/plugins/callback/idempotence.py +++ b/tests/plugins/callback/idempotence.py @@ -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) diff --git a/tests/test_checkmode.sh b/tests/test_checkmode.sh new file mode 100644 index 0000000..5985891 --- /dev/null +++ b/tests/test_checkmode.sh @@ -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 +# +# ################# + + +# 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) diff --git a/tests/test_idempotence.sh b/tests/test_idempotence.sh index ee566aa..6d2acc2 100644 --- a/tests/test_idempotence.sh +++ b/tests/test_idempotence.sh @@ -3,7 +3,7 @@ # # Bash script to run idempotence tests. # -# version: 1.4 +# version: 1.5 # # usage: # @@ -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 @@ -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) ) }') @@ -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) diff --git a/tests/vagrant.sh b/tests/vagrant.sh index 9a3d568..425cbd5 100644 --- a/tests/vagrant.sh +++ b/tests/vagrant.sh @@ -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} From e82a4e37473cbcc355d863d7c051fdd95635148e Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 21 Mar 2016 14:00:11 -0600 Subject: [PATCH 4/6] test symlinks --- tests/tasks/main.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/tasks/main.yml b/tests/tasks/main.yml index c162e9a..51c0236 100644 --- a/tests/tasks/main.yml +++ b/tests/tasks/main.yml @@ -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 + From 89c9af35f10731639b98113027c326c3866c1e1b Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 21 Mar 2016 14:11:54 -0600 Subject: [PATCH 5/6] added tests for /usr/bin symlinks and fixed double when parameters on task that sets default Java installation in RedHat based distributions --- tasks/redhat/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/redhat/main.yml b/tasks/redhat/main.yml index ab4c089..18c5f95 100644 --- a/tasks/redhat/main.yml +++ b/tasks/redhat/main.yml @@ -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 From ac5991f0e95afdc28b3f04023538970836905378 Mon Sep 17 00:00:00 2001 From: Pedro Salgado Date: Mon, 21 Mar 2016 14:17:04 -0600 Subject: [PATCH 6/6] test using ansible 2.0.1.0; updated environment variables; added check mode tests; removed docs section --- tasks/check_environment.yml | 30 +++++++++++++----------------- tox.ini | 20 ++++++-------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/tasks/check_environment.yml b/tasks/check_environment.yml index 93232d8..4c5bafb 100644 --- a/tasks/check_environment.yml +++ b/tasks/check_environment.yml @@ -16,9 +16,8 @@ # 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 @@ -26,11 +25,11 @@ - 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 @@ -40,13 +39,12 @@ - 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 @@ -54,19 +52,17 @@ - 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 diff --git a/tox.ini b/tox.ini index df305e2..7950821 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] envlist = - {py27}-{ansible2002,ansible194} + {py27}-{ansible2010,ansible194} skipsdist = True @@ -8,11 +8,11 @@ skipsdist = True [testenv] changedir = tests deps = - travis: ansible==1.9.4 - ansible2002: ansible==2.0.0.2 + travis: ansible + ansible2010: ansible==2.0.1.0 ansible194: ansible==1.9.4 -passenv = ANSIBLE_ASK_SUDO_PASS HOME +passenv = ANSIBLE_ASK_SUDO_PASS HOME LANG LC_ALL commands = bash -c "test -s ../requirements.yml && ansible-galaxy install --force -r ../requirements.yml || true" @@ -27,19 +27,11 @@ passenv = ANSIBLE_ASK_SUDO_PASS HOME TRAVIS commands = bash -c "test -s ../requirements.yml && ansible-galaxy install --force -r ../requirements.yml || true" - ansible-playbook -i localhost, --connection=local test.yml {posargs} --skip-tags=test + ansible-playbook -i localhost, --connection=local test.yml -e vagrant_box=localhost {posargs} --skip-tags=test + bash test_checkmode.sh --env travis bash test_idempotence.sh --env travis whitelist_externals = ansible-playbook bash - -[testenv:docs] -# sphinx documentation checks -changedir = docs -deps = - Sphinx - -commands = - sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html