From 88497121784d4eecf4a7d1baaeef45780e4d760c Mon Sep 17 00:00:00 2001 From: Andreas Florath Date: Mon, 9 May 2016 21:43:31 +0200 Subject: [PATCH] Use 'output' function instead of 'echo' for statistics log In bash the only sensible way of returning a string from functions or scripts is via stdout. dib-run-parts not only 'returned' the output of the called scripts but also some statistics / performance output. Other parts of the script already used an 'output' function so the statistics output was adapted to also use this kind of function. In addition three simple test cases are implemented to check the basic functions of the script. As a result the log-output of the dib-run-parts is somewhat cleaner. It also makes debugging of the called scripts easier. This change is the precondition for simplification of functions like diskimage-builder's eval_run_d. Change-Id: Ie2f0332359a72ca7ab03f94b7573e073df714a97 Signed-off-by: Andreas Florath --- bin/dib-run-parts | 35 +++++++++++++------- tests/run_tests.sh | 16 ++++++++++ tests/tc01.sh | 39 +++++++++++++++++++++++ tests/tc02.sh | 46 +++++++++++++++++++++++++++ tests/tc02/td/call_me_1 | 3 ++ tests/tc02/td/call_me_2 | 3 ++ tests/tc03.sh | 33 +++++++++++++++++++ tests/tc03/environment.d/set_var.bash | 5 +++ tests/tc03/td/call_me_1 | 3 ++ tests/tc03/td/call_me_2 | 3 ++ tox.ini | 4 +++ 11 files changed, 179 insertions(+), 11 deletions(-) create mode 100755 tests/run_tests.sh create mode 100755 tests/tc01.sh create mode 100755 tests/tc02.sh create mode 100755 tests/tc02/td/call_me_1 create mode 100755 tests/tc02/td/call_me_2 create mode 100755 tests/tc03.sh create mode 100644 tests/tc03/environment.d/set_var.bash create mode 100755 tests/tc03/td/call_me_1 create mode 100755 tests/tc03/td/call_me_2 diff --git a/bin/dib-run-parts b/bin/dib-run-parts index 575e6fd..78580fb 100755 --- a/bin/dib-run-parts +++ b/bin/dib-run-parts @@ -34,8 +34,21 @@ usage() { exit 1 } >&2 + +output_prefix() { + printf "%s %s " "${name}" "$(date)" >&2 +} + output () { - echo $name $(date) $* >&2 + output_prefix + echo $* >&2 +} + +output_printf () { + local FORMAT="$1" + shift + output_prefix + printf "${FORMAT}" $@ >&2 } if [ $# -lt 1 ] ; then @@ -91,22 +104,22 @@ for target in $targets ; do output "$target completed" done -echo "----------------------- PROFILING -----------------------" -echo "" -echo "Target: $(basename $target_dir)" -echo "" -printf "%-40s %9s\n" Script Seconds -printf "%-40s %9s\n" --------------------------------------- ---------- -echo "" +output "----------------------- PROFILING -----------------------" +output "" +output "Target: $(basename $target_dir)" +output "" +output_printf "%-40s %9s\n" Script Seconds +output_printf "%-40s %9s\n" --------------------------------------- ---------- +output "" pushd $PROFILE_DIR > /dev/null for target in $(find . -name 'start_*' -printf '%f\n' | env LC_ALL=C sort -n) ; do stop_file=stop_${target##start_} start_seconds=$(cat $target) stop_seconds=$(cat $stop_file) duration=$(echo - | awk "{ print $stop_seconds - $start_seconds }") - LC_NUMERIC=C LC_ALL=C printf "%-40s %10.3f\n" ${target##start_} $duration + LC_NUMERIC=C LC_ALL=C output_printf "%-40s %10.3f\n" ${target##start_} $duration done popd > /dev/null rm -rf $PROFILE_DIR -echo "" -echo "--------------------- END PROFILING ---------------------" +output "" +output "--------------------- END PROFILING ---------------------" diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..24f9146 --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# +# Runs all test cases in this directory +# + +set -ue +set -o pipefail + +TESTS_BASE_DIR=$(cd $(dirname "$0") && pwd) + +for tc in ${TESTS_BASE_DIR}/tc??.sh; do + echo "--- Running ${tc} ---" + ${tc} +done + +echo "--- TESTS COMPLETE ---" diff --git a/tests/tc01.sh b/tests/tc01.sh new file mode 100755 index 0000000..19948d4 --- /dev/null +++ b/tests/tc01.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# +# Runs dib-run-parts on an empty directory. +# + +set -ue +set -o pipefail +set -x + +TESTS_BASE_DIR=$(cd $(dirname "$0") && pwd) + +DRP_BIN=${TESTS_BASE_DIR}/../bin/dib-run-parts + +TEST_EXEC_DIR=${TESTS_BASE_DIR}/tc01/td + +mkdir -p ${TEST_EXEC_DIR} +rval=0 + +RES=$(${DRP_BIN} --list ${TEST_EXEC_DIR}) +if test $? -ne 0; then + echo "*** FAILED: --list of empty dir failed" + rval=1 +fi +if test -n "${RES}"; then + echo "*** FAILED: --list of empty dir not empty" + rval=1 +fi + +RES=$(${DRP_BIN} ${TEST_EXEC_DIR} 2>/dev/null) +if test $? -ne 0; then + echo "*** FAILED: dib-run-parts on empty dir failed" + rval=1 +fi +if test -n "${RES}"; then + echo "*** FAILED: dib-run-parts on empty dir not empty" + rval=1 +fi + +exit ${rval} diff --git a/tests/tc02.sh b/tests/tc02.sh new file mode 100755 index 0000000..f33f355 --- /dev/null +++ b/tests/tc02.sh @@ -0,0 +1,46 @@ +#!/bin/bash +# +# Runs dib-run-parts on directory containing some scripts. +# + +set -ue +set -o pipefail +set -x + +TESTS_BASE_DIR=$(cd $(dirname "$0") && pwd) + +DRP_BIN=${TESTS_BASE_DIR}/../bin/dib-run-parts + +TEST_EXEC_DIR=${TESTS_BASE_DIR}/tc02/td + +rval=0 + +RES=$(${DRP_BIN} --list ${TEST_EXEC_DIR}) +if test $? -ne 0; then + echo "*** FAILED: --list failed" + rval=1 +fi + +EXPECTED="${TESTS_BASE_DIR}/tc02/td/call_me_1 +${TESTS_BASE_DIR}/tc02/td/call_me_2" + +if test "${EXPECTED}" != "${RES}"; then + echo "*** FAILED: --list returns incorrect result" + rval=1 +fi + +RES=$(${DRP_BIN} ${TEST_EXEC_DIR} 2>/dev/null) +if test $? -ne 0; then + echo "*** FAILED: dib-run-parts on empty dir failed" + rval=1 +fi + +EXPECTED="call_me_1 called +call_me_2 called" + +if test "${EXPECTED}" != "${RES}"; then + echo "*** FAILED: dib-run-parts returns incorrect result" + rval=1 +fi + +exit ${rval} diff --git a/tests/tc02/td/call_me_1 b/tests/tc02/td/call_me_1 new file mode 100755 index 0000000..4e3b6a0 --- /dev/null +++ b/tests/tc02/td/call_me_1 @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "call_me_1 called" diff --git a/tests/tc02/td/call_me_2 b/tests/tc02/td/call_me_2 new file mode 100755 index 0000000..d96423d --- /dev/null +++ b/tests/tc02/td/call_me_2 @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "call_me_2 called" diff --git a/tests/tc03.sh b/tests/tc03.sh new file mode 100755 index 0000000..605f88c --- /dev/null +++ b/tests/tc03.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Runs dib-run-parts on directory containing some scripts +# and using the environment directory. +# + +set -ue +set -o pipefail +set -x + +TESTS_BASE_DIR=$(dirname $0) + +DRP_BIN=${TESTS_BASE_DIR}/../bin/dib-run-parts + +TEST_EXEC_DIR=${TESTS_BASE_DIR}/tc03/td + +rval=0 + +RES=$(${DRP_BIN} ${TEST_EXEC_DIR} 2>/dev/null) +if test $? -ne 0; then + echo "*** FAILED: dib-run-parts failed" + rval=1 +fi + +EXPECTED="call_me_1 called [Some thing] +call_me_2 called [Other thing]" + +if test "${EXPECTED}" != "${RES}"; then + echo "*** FAILED: dib-run-parts returns incorrect result" + rval=1 +fi + +exit ${rval} diff --git a/tests/tc03/environment.d/set_var.bash b/tests/tc03/environment.d/set_var.bash new file mode 100644 index 0000000..d9dc4ac --- /dev/null +++ b/tests/tc03/environment.d/set_var.bash @@ -0,0 +1,5 @@ +#!/bin/bash + +export CM1="Some thing" +export CM2="Other thing" + diff --git a/tests/tc03/td/call_me_1 b/tests/tc03/td/call_me_1 new file mode 100755 index 0000000..11a4850 --- /dev/null +++ b/tests/tc03/td/call_me_1 @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "call_me_1 called [${CM1}]" diff --git a/tests/tc03/td/call_me_2 b/tests/tc03/td/call_me_2 new file mode 100755 index 0000000..db5dec8 --- /dev/null +++ b/tests/tc03/td/call_me_2 @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "call_me_2 called [${CM2}]" diff --git a/tox.ini b/tox.ini index 7d57b47..e8538f7 100644 --- a/tox.ini +++ b/tox.ini @@ -5,3 +5,7 @@ skipsdist = True [testenv:venv] commands = {posargs} +[testenv:func] +changedir={toxinidir}/tests +envdir = {toxworkdir}/venv +commands = ./run_tests.sh {posargs}