Skip to content

Commit

Permalink
Displays command line on failure (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendufresne committed Jul 11, 2016
1 parent 0cede41 commit 0e59a65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
19 changes: 17 additions & 2 deletions src/_print.sh
@@ -1,13 +1,28 @@
#!/usr/bin/env bash

display_command() {
local script_name="$1"
local ansible_role="$2"
local vagrant_box="$3"
local pre_script="$4"
local enable_vbguest=$5

warning "This test has failed. You can rerun it with the following command"

>&2 printf '%s --verbose --ansible-role "%s" --vagrant-box "%s"' "${script_name}" "${ansible_role}" "${vagrant_box}"
[ -n "${pre_script}" ] && >&2 printf ' --pre-script "%s"' "${pre_script}"
${enable_vbguest} && >&2 printf ' --enable-vbguest'
>&2 printf "\n"
}

error() {
local message="$1"

>&2 printf "\e[1;91m%s\e[0m\n\n" "${message}"
>&2 printf "\e[1;91m%s\e[0m\n" "${message}"
}

warning() {
local message="$1"

>&2 printf "\e[1;93m%s\e[0m\n\n" "${message}"
>&2 printf "\e[1;93m%s\e[0m\n" "${message}"
}
18 changes: 15 additions & 3 deletions src/_run.sh
Expand Up @@ -6,7 +6,10 @@ boot_vagrant_box() {

printf "* booting vagrant box: "
vagrant up --no-provision &>"${output_file}"

handle_result $? "${output_file}" ${is_verbose} false

return $?
}

clean_previous_vagrant_box() {
Expand Down Expand Up @@ -45,7 +48,10 @@ test_clean_install() {
printf "* Testing clean install: "
vagrant provision &>"${output_file}"
grep -q 'unreachable=0.*failed=0' "${output_file}"

handle_result $? "${output_file}" ${is_verbose} true

return $?
}

test_idempotent() {
Expand All @@ -55,7 +61,10 @@ test_idempotent() {
printf "* Idempotent test: "
vagrant provision &>"${output_file}"
grep -q 'changed=0.*unreachable=0.*failed=0' "${output_file}"

handle_result $? "${output_file}" ${is_verbose} true

return $?
}

run() {
Expand All @@ -66,6 +75,7 @@ run() {
local is_verbose=$5
local is_vbguest_enabled="$6"
local testing_directory="$(mktemp -d)"
local status=0

printf "# Testing ansible role \033[1;34m%s\033[0m in vagrant box \033[1;34m%s\033[0m\n" "${ansible_role}" "${vagrant_box}"

Expand All @@ -92,14 +102,16 @@ run() {
create_vagrantfile "${vagrant_box}" "${is_vbguest_enabled}" "${pre_script}"
local report_file=$(ensure_report_file_exists "${repository_directory}" "${ansible_role}")

boot_vagrant_box ${is_verbose}
boot_vagrant_box ${is_verbose} || status=1
start_new_report
test_clean_install ${is_verbose}
test_idempotent ${is_verbose}
test_clean_install ${is_verbose} || status=1
test_idempotent ${is_verbose} || status=1

save_report "${report_file}"

vagrant destroy -f >/dev/null
cd
rm -rf "${testing_directory}"

return ${status}
}
20 changes: 9 additions & 11 deletions test.sh
Expand Up @@ -62,18 +62,17 @@ do
shift
done

if [ -z "${CONFIG_FILE}" ]
then
CONFIG_FILE="${DEFAULT_CONFIG_FILE}"
fi
[ -z "${CONFIG_FILE}" ] && CONFIG_FILE="${DEFAULT_CONFIG_FILE}"

if [ -n "${ANSIBLE_ROLES}" ] && [ -n "${VAGRANT_BOXES}" ]
then
for ANSIBLE_ROLE in ${ANSIBLE_ROLES}
do
for VAGRANT_BOX in ${VAGRANT_BOXES}
do
run "${ANSIBLE_ROLE}" "${VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
run "${ANSIBLE_ROLE}" "${VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST} \
|| display_command "$0" "${ANSIBLE_ROLE}" "${VAGRANT_BOX}" "${PRE_SCRIPT}" ${ENABLE_VBGUEST}
echo
done
done
exit 0
Expand All @@ -87,11 +86,8 @@ do
CURRENT_ANSIBLE_ROLE="$(echo "${line}" | cut -d '|' -f 1 | sed 's/^\s*//' | sed 's/\s*$//')"
CURRENT_VAGRANT_BOX="$(echo "${line}" | cut -d '|' -f 2 | sed 's/^\s*//' | sed 's/\s*$//')"
PRE_SCRIPT="$(echo "${line}" | cut -d '|' -f 4 | sed 's/^\s*//' | sed 's/\s*$//')"
ENABLE_VBGUEST="false"
if [ "yes" == "$(echo "${line}" | cut -d '|' -f 3 | sed 's/^\s*//' | sed 's/\s*$//')" ]
then
ENABLE_VBGUEST="true"
fi

[ "yes" == "$(echo "${line}" | cut -d '|' -f 3 | sed 's/^\s*//' | sed 's/\s*$//')" ] && ENABLE_VBGUEST="true" || ENABLE_VBGUEST="false"

if [ -n "${ANSIBLE_ROLES}" ]
then
Expand Down Expand Up @@ -121,5 +117,7 @@ do

${FOUND} || continue

run "${CURRENT_ANSIBLE_ROLE}" "${CURRENT_VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST}
run "${CURRENT_ANSIBLE_ROLE}" "${CURRENT_VAGRANT_BOX}" "${REPOSITORY_DIRECTORY}" "${PRE_SCRIPT}" ${IS_VERBOSE} ${ENABLE_VBGUEST} \
|| display_command "$0" "${CURRENT_ANSIBLE_ROLE}" "${CURRENT_VAGRANT_BOX}" "${PRE_SCRIPT}" ${ENABLE_VBGUEST}
echo
done

0 comments on commit 0e59a65

Please sign in to comment.