From 0f3e44d726623c9ce9144c9fd5215dec57a5959b Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Mon, 5 Sep 2016 11:34:27 +1000 Subject: [PATCH] Fix bashate "local hides exit status" warnings Promote "local hides exit status" (E042) to error and fix issues found (this is inline with what we do in devstack). Change-Id: Idd784af066db80bb67792da3dd0aa0ae4c8475db --- devstack-vm-gate.sh | 6 +++-- functions.sh | 55 ++++++++++++++++++++++++++++++--------------- test-features.sh | 22 ++++++++++++------ test-functions.sh | 19 +++++++++++----- tox.ini | 3 ++- 5 files changed, 72 insertions(+), 33 deletions(-) diff --git a/devstack-vm-gate.sh b/devstack-vm-gate.sh index 38868770..ace86ac6 100755 --- a/devstack-vm-gate.sh +++ b/devstack-vm-gate.sh @@ -314,7 +314,8 @@ function setup_localrc { TEST_MATRIX='roles/test-matrix/library/test_matrix.py -n' MY_ENABLED_SERVICES=$(cd $BASE/new/devstack-gate && $PYTHON_PATH $TEST_MATRIX -b $branch_for_matrix -f $DEVSTACK_GATE_FEATURE_MATRIX -r $test_matrix_role) - local original_enabled_services=$(cd $BASE/new/devstack-gate && $PYTHON_PATH $TEST_MATRIX -b $branch_for_matrix -f $DEVSTACK_GATE_FEATURE_MATRIX -r primary) + local original_enabled_services + original_enabled_services=$(cd $BASE/new/devstack-gate && $PYTHON_PATH $TEST_MATRIX -b $branch_for_matrix -f $DEVSTACK_GATE_FEATURE_MATRIX -r primary) echo "MY_ENABLED_SERVICES: ${MY_ENABLED_SERVICES}" echo "original_enabled_services: ${original_enabled_services}" @@ -568,7 +569,8 @@ function setup_localrc { localrc_set "$localrc_file" "NOVA_ALLOW_MOVE_TO_SAME_HOST" "False" localrc_set "$localrc_file" "LIVE_MIGRATION_AVAILABLE" "True" localrc_set "$localrc_file" "USE_BLOCK_MIGRATION_FOR_LIVE_MIGRATION" "True" - local primary_node=`cat /etc/nodepool/primary_node_private` + local primary_node + primary_node=`cat /etc/nodepool/primary_node_private` localrc_set "$localrc_file" "SERVICE_HOST" "$primary_node" if [[ "$role" = sub ]]; then diff --git a/functions.sh b/functions.sh index fde5e3e4..7a33d245 100644 --- a/functions.sh +++ b/functions.sh @@ -105,6 +105,7 @@ function tsfilter { # create the start timer for when the job began function start_timer { + local default_ntp_server # first make sure the time is right, so we don't go into crazy land # later if the system decides to apply an ntp date and we jump forward # 4 hrs (which has happened) @@ -116,8 +117,8 @@ function start_timer { echo "Unsupported platform, can't determine ntp service" exit 1 fi - local default_ntp_server=$( - grep ^server /etc/ntp.conf | head -1 | awk '{print $2}') + default_ntp_server=$(grep ^server /etc/ntp.conf | \ + head -1 | awk '{print $2}') local ntp_server=${NTP_SERVER:-$default_ntp_server} sudo service $ntp_service stop sudo /usr/sbin/ntpdate $ntp_server @@ -127,8 +128,10 @@ function start_timer { } function remaining_time { - local now=`date +%s` - local elapsed=$(((now - START_TIME) / 60)) + local now + local elapsed + now=`date +%s` + elapsed=$(((now - START_TIME) / 60)) export REMAINING_TIME=$((DEVSTACK_GATE_TIMEOUT - elapsed - 5)) echo "Job timeout set to: $REMAINING_TIME minutes" if [ ${REMAINING_TIME} -le 0 ]; then @@ -139,7 +142,8 @@ function remaining_time { # Create a script to reproduce this build function reproduce { - local xtrace=$(set +o | grep xtrace) + local xtrace + xtrace=$(set +o | grep xtrace) set +o xtrace JOB_PROJECTS=$1 @@ -295,6 +299,8 @@ function git_remote_update { function git_timed { local max_attempts=3 local count=0 + local sleep_time + until timeout -k 1m 5m git "$@"; do count=$(($count + 1)) echo "git $@ failed." @@ -302,7 +308,7 @@ function git_timed { echo "Max attempts reached for git $@; giving up." exit 1 fi - local sleep_time=$((30 + $RANDOM % 60)) + sleep_time=$((30 + $RANDOM % 60)) echo "sleep $sleep_time before retrying." sleep $sleep_time done @@ -358,18 +364,22 @@ function git_clone_and_cd { function setup_project { local project=$1 local branch=$2 - local short_project=`basename $project` + local short_project + short_project=$(basename $project) local git_base=${GIT_BASE:-https://git.openstack.org} # allow for possible project branch override - local uc_project=`echo $short_project | tr [:lower:] [:upper:] | tr '-' '_' | sed 's/[^A-Z_]//'` + local uc_project + uc_project=$(echo $short_project | tr [:lower:] [:upper:] | tr '-' '_' | sed 's/[^A-Z_]//') local project_branch_var="\$OVERRIDE_${uc_project}_PROJECT_BRANCH" - local project_branch=`eval echo ${project_branch_var}` + local project_branch + project_branch=$(eval echo ${project_branch_var}) if [[ "$project_branch" != "" ]]; then branch=$project_branch fi # allow for possible git_base override local project_git_base_var="\$OVERRIDE_${uc_project}_GIT_BASE" - local project_git_base=`eval echo ${project_git_base_var}` + local project_git_base + project_git_base=$(eval echo ${project_git_base_var}) if [[ "$project_git_base" != "" ]]; then git_base=$project_git_base fi @@ -420,12 +430,14 @@ function setup_project { function setup_workspace { local base_branch=$1 local DEST=$2 - local xtrace=$(set +o | grep xtrace) # Note on infra images, this is setup by # project-config:nodepool/elements/cache-devstack # during image builds. local cache_dir=/opt/cache/files/ + local xtrace + xtrace=$(set +o | grep xtrace) + # Enabled detailed logging, since output of this function is redirected set -o xtrace @@ -618,11 +630,13 @@ function save_dir { function cleanup_host { # TODO: clean this up to be errexit clean - local errexit=$(set +o | grep errexit) + local errexit + errexit=$(set +o | grep errexit) set +o errexit # Enabled detailed logging, since output of this function is redirected - local xtrace=$(set +o | grep xtrace) + local xtrace + xtrace=$(set +o | grep xtrace) set -o xtrace cd $WORKSPACE @@ -952,22 +966,27 @@ function remote_copy_file { # enable_netconsole function enable_netconsole { + local remote_ip + local remote_port + local default_gw + local gw_mac + local gw_dev # do nothing if not set if [[ $DEVSTACK_GATE_NETCONSOLE = "" ]]; then return fi - local remote_ip=$(echo $DEVSTACK_GATE_NETCONSOLE | awk -F: -e '{print $1}') - local remote_port=$(echo $DEVSTACK_GATE_NETCONSOLE | awk -F: -e '{print $2}') + remote_ip=$(echo $DEVSTACK_GATE_NETCONSOLE | awk -F: -e '{print $1}') + remote_port=$(echo $DEVSTACK_GATE_NETCONSOLE | awk -F: -e '{print $2}') # netconsole requires the device to send and the destitation MAC, # which is obviously on the same subnet. The way to get packets # out to the world is specify the default gw as the remote # destination. - local default_gw=$(ip route | grep default | awk '{print $3}') - local gw_mac=$(arp -n $default_gw | grep $default_gw | awk '{print $3}') - local gw_dev=$(ip route | grep default | awk '{print $5}') + default_gw=$(ip route | grep default | awk '{print $3}') + gw_mac=$(arp $default_gw | grep $default_gw | awk '{print $3}') + gw_dev=$(ip route | grep default | awk '{print $5}') # turn up message output sudo dmesg -n 8 diff --git a/test-features.sh b/test-features.sh index 71e2cd98..1456f794 100755 --- a/test-features.sh +++ b/test-features.sh @@ -26,8 +26,11 @@ GRENADE_NEW_MASTER="n-api,n-api-meta,n-obj,n-cpu,n-sch,n-cond,n-novnc,n-cauth,g- # Utility function for tests function assert_list_equal { - local source=$(echo $1 | awk 'BEGIN{RS=",";} {print $1}' | sort -V | xargs echo) - local target=$(echo $2 | awk 'BEGIN{RS=",";} {print $1}' | sort -V | xargs echo) + local source + local target + + source=$(echo $1 | awk 'BEGIN{RS=",";} {print $1}' | sort -V | xargs echo) + target=$(echo $2 | awk 'BEGIN{RS=",";} {print $1}' | sort -V | xargs echo) if [[ "$target" != "$source" ]]; then echo -n `caller 0 | awk '{print $2}'` echo -e " - ERROR\n $target \n != $source" @@ -40,27 +43,32 @@ function assert_list_equal { } function test_full_master { - local results=$(DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) + local results + results=$(DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) assert_list_equal $TEMPEST_FULL_MASTER $results } function test_full_feature_ec { - local results=$(DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n -b feature/ec) + local results + results=$(DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n -b feature/ec) assert_list_equal $TEMPEST_FULL_MASTER $results } function test_neutron_master { - local results=$(DEVSTACK_GATE_NEUTRON=1 DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) + local results + results=$(DEVSTACK_GATE_NEUTRON=1 DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) assert_list_equal $TEMPEST_NEUTRON_MASTER $results } function test_heat_slow_master { - local results=$(DEVSTACK_GATE_TEMPEST_HEAT_SLOW=1 DEVSTACK_GATE_NEUTRON=1 DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) + local results + results=$(DEVSTACK_GATE_TEMPEST_HEAT_SLOW=1 DEVSTACK_GATE_NEUTRON=1 DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) assert_list_equal $TEMPEST_HEAT_SLOW_MASTER $results } function test_grenade_new_master { - local results=$(DEVSTACK_GATE_TEMPEST_HEAT_SLOW=1 DEVSTACK_GATE_GRENADE=pullup DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) + local results + results=$(DEVSTACK_GATE_TEMPEST_HEAT_SLOW=1 DEVSTACK_GATE_GRENADE=pullup DEVSTACK_GATE_TEMPEST=1 ./roles/test-matrix/library/test_matrix.py -n) assert_list_equal $GRENADE_NEW_MASTER $results } diff --git a/test-functions.sh b/test-functions.sh index 9d5d7f69..3a36d740 100755 --- a/test-functions.sh +++ b/test-functions.sh @@ -98,8 +98,12 @@ function git_clone_and_cd { # Utility function for tests function assert_equal { - local lineno=`caller 0 | awk '{print $1}'` - local function=`caller 0 | awk '{print $2}'` + local lineno + local function + + lineno=$(caller 0 | awk '{print $1}') + function=$(caller 0 | awk '{print $2}') + if [[ "$1" != "$2" ]]; then echo "ERROR: $1 != $2 in $function:L$lineno!" ERROR=1 @@ -109,8 +113,12 @@ function assert_equal { } function assert_raises { - local lineno=`caller 0 | awk '{print $1}'` - local function=`caller 0 | awk '{print $2}'` + local lineno + local function + + lineno=$(caller 0 | awk '{print $1}') + function=$(caller 0 | awk '{print $2}') + eval "$@" &>/dev/null if [[ $? -eq 0 ]]; then ERROR=1 @@ -465,8 +473,9 @@ function test_workspace_branch_arg { function test_call_hook_if_defined { local filename=test_call_hook_if_defined.txt - local save_dir=`pwd`/tmp + local save_dir + save_dir=$(pwd)/tmp mkdir -p $save_dir function demo_script { diff --git a/tox.ini b/tox.ini index d935904b..c8d8071c 100644 --- a/tox.ini +++ b/tox.ini @@ -18,5 +18,6 @@ whitelist_externals= # bashate options: # -i E006 : ignore long lines # -e E005 : error if not starting with #! +# E042 : error if "local" hides exit status commands = - bash -c "ls *.sh | xargs bashate -v {posargs} -iE006 -eE005" + bash -c "ls *.sh | xargs bashate -v {posargs} -iE006 -eE005,E042"