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 <andreas@florath.net>
This commit is contained in:
Andreas Florath 2016-05-09 21:43:31 +02:00 committed by Ian Wienand
parent ec92ab4981
commit 8849712178
11 changed files with 179 additions and 11 deletions

View File

@ -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 ---------------------"

16
tests/run_tests.sh Executable file
View File

@ -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 ---"

39
tests/tc01.sh Executable file
View File

@ -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}

46
tests/tc02.sh Executable file
View File

@ -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}

3
tests/tc02/td/call_me_1 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
echo "call_me_1 called"

3
tests/tc02/td/call_me_2 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
echo "call_me_2 called"

33
tests/tc03.sh Executable file
View File

@ -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}

View File

@ -0,0 +1,5 @@
#!/bin/bash
export CM1="Some thing"
export CM2="Other thing"

3
tests/tc03/td/call_me_1 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
echo "call_me_1 called [${CM1}]"

3
tests/tc03/td/call_me_2 Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
echo "call_me_2 called [${CM2}]"

View File

@ -5,3 +5,7 @@ skipsdist = True
[testenv:venv]
commands = {posargs}
[testenv:func]
changedir={toxinidir}/tests
envdir = {toxworkdir}/venv
commands = ./run_tests.sh {posargs}