From 90d76c59f80658c5dc458ad44c5485e30d22b210 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Tue, 4 Oct 2016 14:44:49 +0100 Subject: [PATCH] Implement centralised Ansible test scripts This patch implements test scripts intended for use by all OpenStack-Ansible role tests. The intent is to simplify the role tox.ini configuration and ensure that as many changes to role testing configuration can be managed from the centralised tests repo instead of individually in each repository. This patch implements the scripts to centralise the ansible-lint, ansible-syntax, and functional Ansible tests. Functionality included: - For a simple functional test, the defaults will be allow the execution of the test without any parameters set. - For a scenario test the scripts allow the inventory, extra vars and any other CLI parameters for Ansible to be set via environment variables. - Both check mode and an idempotence test. In addition to this functionality, the bash scripts are all set to fail on error to ensure that tox shows a failure. Change-Id: I23c24146485da340d4f046f80e4814652e6e3876 --- test-ansible-env-prep.sh | 11 ++++ test-ansible-functional.sh | 108 +++++++++++++++++++++++++++++++++++++ test-ansible-lint.sh | 40 ++++++++++++++ test-ansible-syntax.sh | 43 +++++++++++++++ test-bashate.sh | 8 +++ test-log-collect.sh | 6 ++- test-pep8.sh | 8 +++ tox.ini | 33 +++--------- 8 files changed, 231 insertions(+), 26 deletions(-) create mode 100755 test-ansible-functional.sh create mode 100755 test-ansible-lint.sh create mode 100755 test-ansible-syntax.sh diff --git a/test-ansible-env-prep.sh b/test-ansible-env-prep.sh index 1f705933..48708952 100755 --- a/test-ansible-env-prep.sh +++ b/test-ansible-env-prep.sh @@ -23,10 +23,18 @@ # This script prepares the host with all the required Ansible # roles and plugins to execute the test playbook. +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + export TESTING_HOME=${TESTING_HOME:-$HOME} export WORKING_DIR=${WORKING_DIR:-$(pwd)} export ROLE_NAME=${ROLE_NAME:-''} +export ANSIBLE_INVENTORY=${ANSIBLE_INVENTORY:-$WORKING_DIR/tests/inventory} +export ANSIBLE_NOCOLOR=1 export ANSIBLE_ROLE_DIR="${TESTING_HOME}/.ansible/roles" export ANSIBLE_PLUGIN_DIR="${TESTING_HOME}/.ansible/plugins" export ANSIBLE_CFG_PATH="${TESTING_HOME}/.ansible.cfg" @@ -36,6 +44,7 @@ export COMMON_TESTS_PATH="${WORKING_DIR}/tests/common" echo "TESTING_HOME: ${TESTING_HOME}" echo "WORKING_DIR: ${WORKING_DIR}" echo "ROLE_NAME: ${ROLE_NAME}" +echo "ANSIBLE_INVENTORY: ${ANSIBLE_INVENTORY}" # Toggle the reset of all data cloned from other repositories. export TEST_RESET=${TEST_RESET:-false} @@ -44,6 +53,8 @@ export TEST_RESET=${TEST_RESET:-false} # console output is immediate. export PYTHONUNBUFFERED=1 +## Main ---------------------------------------------------------------------- + # If the test reset toggle is set, destroy the existing cloned data. if [ "${TEST_RESET}" == "true" ]; then echo "Resetting all cloned data." diff --git a/test-ansible-functional.sh b/test-ansible-functional.sh new file mode 100755 index 00000000..1d816854 --- /dev/null +++ b/test-ansible-functional.sh @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# WARNING: +# This file is use by all OpenStack-Ansible roles for testing purposes. +# Any changes here will affect all OpenStack-Ansible role repositories +# with immediate effect. + +# PURPOSE: +# This script executes a test Ansible playbook for the purpose of +# functionally testing the role. It supports a convergence test, +# check mode and an idempotence test. + +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + +export WORKING_DIR=${WORKING_DIR:-$(pwd)} +export ROLE_NAME=${ROLE_NAME:-''} + +export ANSIBLE_OVERRIDES=${ANSIBLE_OVERRIDES:-$WORKING_DIR/tests/$ROLE_NAME-overrides.yml} +export ANSIBLE_PARAMETERS=${ANSIBLE_PARAMETERS:-"-vvv"} +export TEST_PLAYBOOK=${TEST_PLAYBOOK:-$WORKING_DIR/tests/test.yml} +export TEST_CHECK_MODE=${TEST_CHECK_MODE:-false} +export TEST_IDEMPOTENCE=${TEST_IDEMPOTENCE:-false} + +export COMMON_TESTS_PATH="${WORKING_DIR}/tests/common" + +echo "ANSIBLE_OVERRIDES: ${ANSIBLE_OVERRIDES}" +echo "ANSIBLE_PARAMETERS: ${ANSIBLE_PARAMETERS}" +echo "TEST_PLAYBOOK: ${TEST_PLAYBOOK}" +echo "TEST_CHECK_MODE: ${TEST_CHECK_MODE}" +echo "TEST_IDEMPOTENCE: ${TEST_IDEMPOTENCE}" + +## Functions ----------------------------------------------------------------- + +function set_ansible_parameters { + if [ -f "${ANSIBLE_OVERRIDES}" ]; then + ANSIBLE_CLI_PARAMETERS="${ANSIBLE_PARAMETERS} -e @${ANSIBLE_OVERRIDES}" + else + ANSIBLE_CLI_PARAMETERS="${ANSIBLE_PARAMETERS}" + fi + echo "ANSIBLE_CLI_PARAMETERS: ${ANSIBLE_CLI_PARAMETERS}" +} + +function gate_job_exit_tasks { + source "${COMMON_TESTS_PATH}/test-log-collect.sh" +} + +## Main ---------------------------------------------------------------------- + +# Ensure that the Ansible environment is properly prepared +source "${COMMON_TESTS_PATH}/test-ansible-env-prep.sh" + +# Set gate job exit traps, this is run regardless of exit state when the job finishes. +trap gate_job_exit_tasks EXIT + +# Prepare the extra CLI parameters used in each execution +set_ansible_parameters + +# If the test for check mode is enabled, then execute it +if [ "${TEST_CHECK_MODE}" == "true" ]; then + ansible-playbook --check \ + ${ANSIBLE_CLI_PARAMETERS} \ + ${TEST_PLAYBOOK} +fi + +# Execute the test playbook +ansible-playbook ${ANSIBLE_CLI_PARAMETERS} \ + ${TEST_PLAYBOOK} + +# If the idempotence test is enabled, then execute the +# playbook again and verify that nothing changed/failed +# in the output log. + +if [ "${TEST_IDEMPOTENCE}" == "true" ]; then + + # Set the path for the output log + ANSIBLE_LOG_PATH="/tmp/ansible.log" + + # Execute the test playbook + ansible-playbook ${ANSIBLE_CLI_PARAMETERS} \ + ${TEST_PLAYBOOK} + + # Check the output log for changed/failed tasks + if grep -q "changed=0.*failed=0" /tmp/idempotence_test_output.txt; then + echo "Idempotence test: pass" + else + echo "Idempotence test: fail" + exit 1 + fi + +fi diff --git a/test-ansible-lint.sh b/test-ansible-lint.sh new file mode 100755 index 00000000..8cf265f9 --- /dev/null +++ b/test-ansible-lint.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# WARNING: +# This file is use by all OpenStack-Ansible roles for testing purposes. +# Any changes here will affect all OpenStack-Ansible role repositories +# with immediate effect. + +# PURPOSE: +# This script executes ansible-lint against the role directory. + +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + +export WORKING_DIR=${WORKING_DIR:-$(pwd)} +export COMMON_TESTS_PATH="${WORKING_DIR}/tests/common" + +## Main ---------------------------------------------------------------------- + +# Ensure that the Ansible environment is properly prepared +source "${COMMON_TESTS_PATH}/test-ansible-env-prep.sh" + +# Execute ansible-lint +ansible-lint ${WORKING_DIR} diff --git a/test-ansible-syntax.sh b/test-ansible-syntax.sh new file mode 100755 index 00000000..d5ed980c --- /dev/null +++ b/test-ansible-syntax.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +# Copyright 2016, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# WARNING: +# This file is use by all OpenStack-Ansible roles for testing purposes. +# Any changes here will affect all OpenStack-Ansible role repositories +# with immediate effect. + +# PURPOSE: +# This script executes ansible-syntax against the role test playbook. + +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + +export WORKING_DIR=${WORKING_DIR:-$(pwd)} +export COMMON_TESTS_PATH="${WORKING_DIR}/tests/common" +export ANSIBLE_INVENTORY=${ANSIBLE_INVENTORY:-$WORKING_DIR/tests/inventory} + +## Main ---------------------------------------------------------------------- + +# Ensure that the Ansible environment is properly prepared +source "${COMMON_TESTS_PATH}/test-ansible-env-prep.sh" + +# Execute the Ansible syntax check +ansible-playbook --syntax-check \ + --list-tasks \ + ${WORKING_DIR}/tests/test.yml diff --git a/test-bashate.sh b/test-bashate.sh index 78c0a35b..9814a0a8 100755 --- a/test-bashate.sh +++ b/test-bashate.sh @@ -35,8 +35,16 @@ # use jinja templating, this will often fail and the syntax # error will be discovered in execution anyway) +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + export WORKING_DIR=${WORKING_DIR:-$(pwd)} +## Main ---------------------------------------------------------------------- + grep --recursive --binary-files=without-match \ --files-with-match '^.!.*\(ba\)\?sh$' \ --exclude-dir .tox \ diff --git a/test-log-collect.sh b/test-log-collect.sh index 0bb71b88..bb68da4f 100755 --- a/test-log-collect.sh +++ b/test-log-collect.sh @@ -23,9 +23,13 @@ # This script collects, renames and compresses the logs produced in # a role test if the host is in OpenStack-CI. +## Vars ---------------------------------------------------------------------- + export WORKING_DIR=${WORKING_DIR:-$(pwd)} -if [[ -d "/etc/nodepool" ]];then +## Main ---------------------------------------------------------------------- + +if [[ -d "/etc/nodepool" ]]; then mkdir -p "${WORKING_DIR}/logs/host" "${WORKING_DIR}/logs/openstack" rsync --archive --verbose --safe-links --ignore-errors /var/log/ "${WORKING_DIR}/logs/host" || true rsync --archive --verbose --safe-links --ignore-errors /openstack/log/ "${WORKING_DIR}/logs/openstack" || true diff --git a/test-pep8.sh b/test-pep8.sh index f71e99f8..33a43893 100755 --- a/test-pep8.sh +++ b/test-pep8.sh @@ -24,8 +24,16 @@ # the search pattern. The search pattern is meant to find any python # scripts present in the role. +## Shell Opts ---------------------------------------------------------------- + +set -e + +## Vars ---------------------------------------------------------------------- + export WORKING_DIR=${WORKING_DIR:-$(pwd)} +## Main ---------------------------------------------------------------------- + grep --recursive --binary-files=without-match \ --files-with-match '^.!.*python$' \ --exclude-dir .eggs \ diff --git a/tox.ini b/tox.ini index b125a3a2..9d5a18d3 100644 --- a/tox.ini +++ b/tox.ini @@ -60,10 +60,11 @@ commands = # https://git.openstack.org/openstack/openstack-ansible-tests # or for a stable branch: # -b stable/mitaka https://git.openstack.org/openstack/openstack-ansible-tests +# [testenv:tests_clone] commands = bash -c "if [ ! -d "{toxinidir}/tests/common" ]; then \ - git clone {toxinidir} {toxinidir}/tests/common; \ + ln -s {toxinidir} {toxinidir}/tests/common; \ fi" @@ -100,28 +101,22 @@ commands = deps = {[testenv]deps} -r{toxinidir}/test-ansible-deps.txt -commands = - {[testenv:tests_clone]commands} - bash -c "{toxinidir}/tests/common/test-ansible-env-prep.sh" [testenv:ansible-syntax] deps = {[testenv:ansible]deps} commands = - {[testenv:ansible]commands} - ansible-playbook -i {toxinidir}/tests/inventory \ - --syntax-check \ - --list-tasks \ - {toxinidir}/tests/test.yml + {[testenv:tests_clone]commands} + bash -c "{toxinidir}/tests/common/test-ansible-syntax.sh" [testenv:ansible-lint] deps = {[testenv:ansible]deps} commands = - {[testenv:ansible]commands} - ansible-lint {toxinidir} + {[testenv:tests_clone]commands} + bash -c "{toxinidir}/tests/common/test-ansible-lint.sh" [testenv:func_base] @@ -132,17 +127,7 @@ install_command = pip install -U --force-reinstall {opts} {packages} -[testenv:func_logs] -commands = - bash -c "{toxinidir}/tests/common/test-log-collect.sh" - - [testenv:functional] -# Ignore_errors is set to true so that the logs are collected at the -# end of the run. This will not produce a false positive. Any -# exception will be mark the run as failed and exit 1 after all of -# the commands have been iterated through. -ignore_errors = True # NOTE(odyssey4me): this target does not use constraints because # it doesn't work in OpenStack-CI yet. Once that's fixed, we can # drop the install_command. @@ -151,10 +136,8 @@ install_command = deps = {[testenv:ansible]deps} commands = - {[testenv:ansible]commands} - ansible-playbook -i {toxinidir}/tests/inventory \ - {toxinidir}/tests/test.yml -vvvv - {[testenv:func_logs]commands} + {[testenv:tests_clone]commands} + bash -c "{toxinidir}/tests/common/test-ansible-functional.sh" [testenv:linters]