autotest/common: factor out xtrace disable/restore

Factor out `set -x` and +x to separate functions. Changing
xtraces is not so trivial and we'll be improving it later
on. We can't factor out the code as is due to local variables,
so we already simplify it a bit in this patch.

Change-Id: Iecbf5cedf821b7b1b71da933ceb158761881a843
Signed-off-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/453246
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Karol Latecki <karol.latecki@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Darek Stojaczyk 2019-05-06 11:36:15 +02:00 committed by Jim Harris
parent 63d5459656
commit 3b660ea857

View File

@ -4,6 +4,17 @@ if $SPDK_AUTOTEST_X; then
set -x set -x
fi fi
function xtrace_disable() {
PREV_BASH_OPTS="$-"
set +x
}
function xtrace_restore() {
if [[ "$PREV_BASH_OPTS" == *"x"* ]]; then
set -x
fi
}
set -e set -e
# Export flag to skip the known bug that exists in librados # Export flag to skip the known bug that exists in librados
@ -209,17 +220,15 @@ function timing() {
} }
function timing_enter() { function timing_enter() {
local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" xtrace_disable
set +x
timing "enter" "$1" timing "enter" "$1"
$shell_restore_x xtrace_restore
} }
function timing_exit() { function timing_exit() {
local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" xtrace_disable
set +x
timing "exit" "$1" timing "exit" "$1"
$shell_restore_x xtrace_restore
} }
function timing_finish() { function timing_finish() {
@ -309,8 +318,7 @@ function waitforlisten() {
echo "Waiting for process to start up and listen on UNIX domain socket $rpc_addr..." echo "Waiting for process to start up and listen on UNIX domain socket $rpc_addr..."
# turn off trace for this loop # turn off trace for this loop
local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" xtrace_disable
set +x
local ret=0 local ret=0
local i local i
for (( i = 40; i != 0; i-- )); do for (( i = 40; i != 0; i-- )); do
@ -350,7 +358,7 @@ function waitforlisten() {
sleep 0.5 sleep 0.5
done done
$shell_restore_x xtrace_restore
if (( i == 0 )); then if (( i == 0 )); then
echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$rpc_addr'" echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$rpc_addr'"
ret=1 ret=1
@ -484,28 +492,26 @@ function kill_stub() {
} }
function run_test() { function run_test() {
local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" xtrace_disable
set +x
local test_type="$(echo $1 | tr 'a-z' 'A-Z')" local test_type="$(echo $1 | tr 'a-z' 'A-Z')"
shift shift
echo "************************************" echo "************************************"
echo "START TEST $test_type $@" echo "START TEST $test_type $@"
echo "************************************" echo "************************************"
$shell_restore_x xtrace_restore
time "$@" time "$@"
set +x xtrace_disable
echo "************************************" echo "************************************"
echo "END TEST $test_type $@" echo "END TEST $test_type $@"
echo "************************************" echo "************************************"
$shell_restore_x xtrace_restore
} }
function print_backtrace() { function print_backtrace() {
# if errexit is not enabled, don't print a backtrace # if errexit is not enabled, don't print a backtrace
[[ "$-" =~ e ]] || return 0 [[ "$-" =~ e ]] || return 0
local shell_options="$-" xtrace_disable
set +x
echo "========== Backtrace start: ==========" echo "========== Backtrace start: =========="
echo "" echo ""
for i in $(seq 1 $((${#FUNCNAME[@]} - 1))); do for i in $(seq 1 $((${#FUNCNAME[@]} - 1))); do
@ -520,7 +526,7 @@ function print_backtrace() {
done done
echo "" echo ""
echo "========== Backtrace end ==========" echo "========== Backtrace end =========="
[[ "$shell_options" =~ x ]] && set -x xtrace_restore
return 0 return 0
} }