summaryrefslogtreecommitdiffstats
path: root/tests/functions-common
diff options
context:
space:
mode:
authorCarlos Goncalves <carlos.goncalves@neclab.eu>2016-11-16 08:57:09 +0000
committerCarlos Goncalves <carlos.goncalves@neclab.eu>2016-11-16 09:58:05 +0000
commitdffe8ff7c269af0d8d11593d8aed95f5a9039114 (patch)
treef837a9e961a8aee6846856b8ff32d1d2410eeb52 /tests/functions-common
parent69264fa32e7885be44667efa34c0c58cc110f5b6 (diff)
Introducing common functions
These functions will ease human readability, writing and debugging of our test codes. Common functions imported from devstack project. This is series of patches to refactor the test baseline code in what it will hopefully become more modular, readable and maintainable. JIRA: DOCTOR-71 Change-Id: Icbd7a1c2b3979081db8e5de3c46d9827ab54d7ca Signed-off-by: Carlos Goncalves <carlos.goncalves@neclab.eu>
Diffstat (limited to 'tests/functions-common')
-rw-r--r--tests/functions-common72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/functions-common b/tests/functions-common
new file mode 100644
index 00000000..db2565a3
--- /dev/null
+++ b/tests/functions-common
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+# Test if the named environment variable is set and not zero length
+# is_set env-var
+function is_set {
+ local var=\$"$1"
+ eval "[ -n \"$var\" ]"
+}
+
+# Prints backtrace info
+# filename:lineno:function
+# backtrace level
+function backtrace {
+ local level=$1
+ local deep
+ deep=$((${#BASH_SOURCE[@]} - 1))
+ echo "[Call Trace]"
+ while [ $level -le $deep ]; do
+ echo "${BASH_SOURCE[$deep]}:${BASH_LINENO[$deep-1]}:${FUNCNAME[$deep-1]}"
+ deep=$((deep - 1))
+ done
+}
+
+# Prints line number and "message" in error format
+# err $LINENO "message"
+function err {
+ local exitcode=$?
+ local xtrace
+ xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local msg="[ERROR] ${BASH_SOURCE[2]}:$1 $2"
+ echo $msg 1>&2;
+ if [[ -n ${LOGDIR} ]]; then
+ echo $msg >> "${LOGDIR}/error.log"
+ fi
+ $xtrace
+ return $exitcode
+}
+
+# Prints line number and "message" then exits
+# die $LINENO "message"
+function die {
+ local exitcode=$?
+ set +o xtrace
+ local line=$1; shift
+ if [ $exitcode == 0 ]; then
+ exitcode=1
+ fi
+ backtrace 2
+ err $line "$*"
+ # Give buffers a second to flush
+ sleep 1
+ exit $exitcode
+}
+
+# Checks an environment variable is not set or has length 0 OR if the
+# exit code is non-zero and prints "message" and exits
+# NOTE: env-var is the variable name without a '$'
+# die_if_not_set $LINENO env-var "message"
+function die_if_not_set {
+ local exitcode=$?
+ local xtrace
+ xtrace=$(set +o | grep xtrace)
+ set +o xtrace
+ local line=$1; shift
+ local evar=$1; shift
+ if ! is_set $evar || [ $exitcode != 0 ]; then
+ die $line "$*"
+ fi
+ $xtrace
+}
+