From db0c317436e093085093575804cced931b47ad46 Mon Sep 17 00:00:00 2001 From: Pawel Wodkowski Date: Tue, 30 Jan 2018 15:18:17 +0100 Subject: [PATCH] test/vhost: avoid setting nullglob's where possible If any command fail while nullglob is set error handlers find bizare shell state and sometimes fail with odd errors. To avoid this introduce function that will never leave nullglob set after return Change-Id: Iff8ae410072c4e5f7f9e72e28171fc24e6186f5f Signed-off-by: Pawel Wodkowski Reviewed-on: https://review.gerrithub.io/397092 Tested-by: SPDK Automated Test System Reviewed-by: Karol Latecki Reviewed-by: Daniel Verkamp Reviewed-by: Jim Harris --- test/vhost/common/common.sh | 48 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/test/vhost/common/common.sh b/test/vhost/common/common.sh index fccd025fd..9a0835eb3 100644 --- a/test/vhost/common/common.sh +++ b/test/vhost/common/common.sh @@ -362,15 +362,24 @@ function vm_kill() fi } +# List all VM numbers in VM_BASE_DIR +# +function vm_list_all() +{ + local vms="$(shopt -s nullglob; echo $VM_BASE_DIR/[0-9])" + if [[ ! -z "$vms" ]]; then + basename --multiple $vms + fi +} + # Kills all VM in $VM_BASE_DIR # function vm_kill_all() { - shopt -s nullglob - for vm in $VM_BASE_DIR/[0-9]*; do - vm_kill $(basename $vm) + local vm + for vm in $(vm_list_all); do + vm_kill $vm done - shopt -u nullglob } # Shutdown all VM in $VM_BASE_DIR @@ -380,8 +389,10 @@ function vm_shutdown_all() local shell_restore_x="$( [[ "$-" =~ x ]] && echo 'set -x' )" set +x - shopt -s nullglob - for vm in $VM_BASE_DIR/[0-9]*; do + local vms=$(vm_list_all) + local vm + + for vm in $vms; do vm_shutdown $(basename $vm) done @@ -389,7 +400,7 @@ function vm_shutdown_all() timeo=15 while [[ $timeo -gt 0 ]]; do all_vms_down=1 - for vm in $VM_BASE_DIR/[0-9]*; do + for vm in $vms; do if [[ -r $vm/qemu.pid ]] && pkill -0 -F "$vm/qemu.pid"; then all_vms_down=0 break @@ -398,7 +409,6 @@ function vm_shutdown_all() if [[ $all_vms_down == 1 ]]; then notice "All VMs successfully shut down" - shopt -u nullglob $shell_restore_x return 0 fi @@ -406,8 +416,9 @@ function vm_shutdown_all() ((timeo-=1)) sleep 1 done - shopt -u nullglob + $shell_restore_x + error "Timout waiting for some VMs to shutdown" return 1 } @@ -696,8 +707,10 @@ function vm_setup() function vm_run() { - local OPTIND optchar a + local OPTIND optchar vm local run_all=false + local vms_to_run="" + while getopts 'a-:' optchar; do case "$optchar" in a) run_all=true ;; @@ -708,11 +721,8 @@ function vm_run() esac done - local vms_to_run="" - if $run_all; then - shopt -s nullglob - vms_to_run=$VM_BASE_DIR/[0-9]* + vms_to_run="$(vm_list_all)" else shift $((OPTIND-1)) for vm in $@; do @@ -721,18 +731,18 @@ function vm_run() error "VM$vm not defined - setup it first" return 1 fi - vms_to_run+=" $VM_BASE_DIR/$vm" + vms_to_run+=" $vm" done fi for vm in $vms_to_run; do - if vm_is_running $(basename $vm); then - warning "VM$(basename $vm) ($vm) already running" + if vm_is_running $vm; then + warning "VM$vm ($VM_BASE_DIR/$vm) already running" continue fi - notice "running $vm/run.sh" - if ! $vm/run.sh; then + notice "running $VM_BASE_DIR$vm/run.sh" + if ! $VM_BASE_DIR/$vm/run.sh; then error "FAILED to run vm $vm" return 1 fi