5f3cd37d12
The current idempotency check is broken because Ansible does not produce a log output without the environment variable being exported. This patch ensures that each playbook run produces an individual log, and ensures that the idempotency check uses the correct log output. The following are also included: - The script outputs the full CLI used to execute the test playbook. This is useful for debugging purposes. - The parameters are re-ordered in alphabetical order to make them easier to find. - The log collection now also collects the Ansible logs. - Instead of repeating the rsync command in the log collection a variable is used, reducing the line length and making it easier to change/re-use. Change-Id: Ie379de765c6ebba958ce8e7f9dc27b7a3af74ff8
123 lines
3.7 KiB
Bash
Executable File
123 lines
3.7 KiB
Bash
Executable File
#!/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
|
|
|
|
}
|
|
|
|
function execute_ansible_playbook {
|
|
|
|
CMD_TO_EXECUTE="ansible-playbook ${ANSIBLE_CLI_PARAMETERS} ${TEST_PLAYBOOK} $@"
|
|
echo "Executing: ${CMD_TO_EXECUTE}"
|
|
${CMD_TO_EXECUTE}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
# Set the path for the output log
|
|
export ANSIBLE_LOG_PATH="${ANSIBLE_LOG_DIR}/ansible-check.log"
|
|
|
|
# Execute the test playbook in check mode
|
|
execute_ansible_playbook --check
|
|
|
|
fi
|
|
|
|
# Set the path for the output log
|
|
export ANSIBLE_LOG_PATH="${ANSIBLE_LOG_DIR}/ansible-execute.log"
|
|
|
|
# Execute the test playbook
|
|
execute_ansible_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
|
|
export ANSIBLE_LOG_PATH="${ANSIBLE_LOG_DIR}/ansible-idempotence.log"
|
|
|
|
# Execute the test playbook
|
|
execute_ansible_playbook
|
|
|
|
# Check the output log for changed/failed tasks
|
|
if grep -q "changed=0.*failed=0" ${ANSIBLE_LOG_PATH}; then
|
|
echo "Idempotence test: pass"
|
|
else
|
|
echo "Idempotence test: fail"
|
|
exit 1
|
|
fi
|
|
|
|
fi
|