Add bashate support

Bashate is a style checker program for bash scripts.  This addition
improves the quality of the current bash scripts and ensures that
any future change will follow the same standards.

Change-Id: Ia346f77632d4ac7beb288fa3aacea221d7969c87
This commit is contained in:
Victor Morales 2017-01-31 16:52:42 -06:00 committed by Ihar Hrachyshka
parent d46636112f
commit a14439549f
13 changed files with 197 additions and 186 deletions

View File

@ -8,8 +8,8 @@ export PYTHONPATH=$NEUTRON_ROOT
cd $NEUTRON_ROOT cd $NEUTRON_ROOT
VERSION=$(sh -c "(cat $NEUTRON_ROOT/neutron/version.py; \ VERSION=$(sh -c "(cat $NEUTRON_ROOT/neutron/version.py; \
echo 'print version_info.release_string()') | \ echo 'print version_info.release_string()') | \
python") python")
cd - cd -
PACKAGE=openstack-neutron-xen-plugins PACKAGE=openstack-neutron-xen-plugins

View File

@ -39,7 +39,7 @@ ${config}
# Tweak gate configuration for our rally scenarios # Tweak gate configuration for our rally scenarios
function load_rc_for_rally { function load_rc_for_rally {
for file in $(ls $RALLY_EXTRA_DIR/*.setup); do for file in $(ls $RALLY_EXTRA_DIR/*.setup); do
local config=$(cat $file) config=$(cat $file)
export DEVSTACK_LOCAL_CONFIG+=" export DEVSTACK_LOCAL_CONFIG+="
# generated from hook '$file' # generated from hook '$file'
${config} ${config}

View File

@ -20,8 +20,7 @@ function generate_testr_results {
fi fi
} }
if [[ "$venv" == dsvm-functional* ]] || [[ "$venv" == dsvm-fullstack* ]] if [[ "$venv" == dsvm-functional* ]] || [[ "$venv" == dsvm-fullstack* ]]; then
then
owner=stack owner=stack
sudo_env= sudo_env=

View File

@ -3,71 +3,71 @@
set -eu set -eu
function usage { function usage {
echo "NOTE: This script is deprecated." echo "NOTE: This script is deprecated."
echo "Usage: $0 [OPTION]..." echo "Usage: $0 [OPTION]..."
echo "Run Neutron's test suite(s)" echo "Run Neutron's test suite(s)"
echo "" echo ""
echo " -V, --virtual-env Always use virtualenv. Install automatically if not present" echo " -V, --virtual-env Always use virtualenv. Install automatically if not present"
echo " -N, --no-virtual-env Don't use virtualenv. Run tests in local environment" echo " -N, --no-virtual-env Don't use virtualenv. Run tests in local environment"
echo " -s, --no-site-packages Isolate the virtualenv from the global Python environment" echo " -s, --no-site-packages Isolate the virtualenv from the global Python environment"
echo " -r, --recreate-db Recreate the test database (deprecated, as this is now the default)." echo " -r, --recreate-db Recreate the test database (deprecated, as this is now the default)."
echo " -n, --no-recreate-db Don't recreate the test database." echo " -n, --no-recreate-db Don't recreate the test database."
echo " -f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added." echo " -f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added."
echo " -u, --update Update the virtual environment with any newer package versions" echo " -u, --update Update the virtual environment with any newer package versions"
echo " -p, --pep8 Just run PEP8 and HACKING compliance check" echo " -p, --pep8 Just run PEP8 and HACKING compliance check"
echo " -8, --pep8-only-changed [<basecommit>]" echo " -8, --pep8-only-changed [<basecommit>]"
echo " Just run PEP8 and HACKING compliance check on files changed since HEAD~1 (or <basecommit>)" echo " Just run PEP8 and HACKING compliance check on files changed since HEAD~1 (or <basecommit>)"
echo " -P, --no-pep8 Don't run static code checks" echo " -P, --no-pep8 Don't run static code checks"
echo " -c, --coverage Generate coverage report" echo " -c, --coverage Generate coverage report"
echo " -d, --debug Run tests with testtools instead of testr. This allows you to use the debugger." echo " -d, --debug Run tests with testtools instead of testr. This allows you to use the debugger."
echo " -h, --help Print this usage message" echo " -h, --help Print this usage message"
echo " --virtual-env-path <path> Location of the virtualenv directory" echo " --virtual-env-path <path> Location of the virtualenv directory"
echo " Default: \$(pwd)" echo " Default: \$(pwd)"
echo " --virtual-env-name <name> Name of the virtualenv directory" echo " --virtual-env-name <name> Name of the virtualenv directory"
echo " Default: .venv" echo " Default: .venv"
echo " --tools-path <dir> Location of the tools directory" echo " --tools-path <dir> Location of the tools directory"
echo " Default: \$(pwd)" echo " Default: \$(pwd)"
echo "" echo ""
echo "Note: with no options specified, the script will try to run the tests in a virtual environment," echo "Note: with no options specified, the script will try to run the tests in a virtual environment,"
echo " If no virtualenv is found, the script will ask if you would like to create one. If you " echo " If no virtualenv is found, the script will ask if you would like to create one. If you "
echo " prefer to run tests NOT in a virtual environment, simply pass the -N option." echo " prefer to run tests NOT in a virtual environment, simply pass the -N option."
exit exit
} }
function process_options { function process_options {
i=1 i=1
while [ $i -le $# ]; do while [ $i -le $# ]; do
case "${!i}" in case "${!i}" in
-h|--help) usage;; -h|--help) usage;;
-V|--virtual-env) always_venv=1; never_venv=0;; -V|--virtual-env) always_venv=1; never_venv=0;;
-N|--no-virtual-env) always_venv=0; never_venv=1;; -N|--no-virtual-env) always_venv=0; never_venv=1;;
-s|--no-site-packages) no_site_packages=1;; -s|--no-site-packages) no_site_packages=1;;
-r|--recreate-db) recreate_db=1;; -r|--recreate-db) recreate_db=1;;
-n|--no-recreate-db) recreate_db=0;; -n|--no-recreate-db) recreate_db=0;;
-f|--force) force=1;; -f|--force) force=1;;
-u|--update) update=1;; -u|--update) update=1;;
-p|--pep8) just_pep8=1;; -p|--pep8) just_pep8=1;;
-8|--pep8-only-changed) just_pep8_changed=1;; -8|--pep8-only-changed) just_pep8_changed=1;;
-P|--no-pep8) no_pep8=1;; -P|--no-pep8) no_pep8=1;;
-c|--coverage) coverage=1;; -c|--coverage) coverage=1;;
-d|--debug) debug=1;; -d|--debug) debug=1;;
--virtual-env-path) --virtual-env-path)
(( i++ )) $(( i++ ))
venv_path=${!i} venv_path=${!i}
;; ;;
--virtual-env-name) --virtual-env-name)
(( i++ )) $(( i++ ))
venv_dir=${!i} venv_dir=${!i}
;; ;;
--tools-path) --tools-path)
(( i++ )) $(( i++ ))
tools_path=${!i} tools_path=${!i}
;; ;;
-*) testopts="$testopts ${!i}";; -*) testopts="$testopts ${!i}";;
*) testargs="$testargs ${!i}" *) testargs="$testargs ${!i}"
esac esac
(( i++ )) $(( i++ ))
done done
} }
tool_path=${tools_path:-$(pwd)} tool_path=${tools_path:-$(pwd)}
@ -107,84 +107,84 @@ export tools_dir
export venv=${venv_path}/${venv_dir} export venv=${venv_path}/${venv_dir}
if [ $no_site_packages -eq 1 ]; then if [ $no_site_packages -eq 1 ]; then
installvenvopts="--no-site-packages" installvenvopts="--no-site-packages"
fi fi
function run_tests { function run_tests {
# Cleanup *pyc # Cleanup *pyc
${wrapper} find . -type f -name "*.pyc" -delete ${wrapper} find . -type f -name "*.pyc" -delete
if [ $debug -eq 1 ]; then if [ $debug -eq 1 ]; then
if [ "$testopts" = "" ] && [ "$testargs" = "" ]; then if [ "$testopts" = "" ] && [ "$testargs" = "" ]; then
# Default to running all tests if specific test is not # Default to running all tests if specific test is not
# provided. # provided.
testargs="discover ./neutron/tests" testargs="discover ./neutron/tests"
fi
${wrapper} python -m testtools.run $testopts $testargs
# Short circuit because all of the testr and coverage stuff
# below does not make sense when running testtools.run for
# debugging purposes.
return $?
fi fi
${wrapper} python -m testtools.run $testopts $testargs
# Short circuit because all of the testr and coverage stuff if [ $coverage -eq 1 ]; then
# below does not make sense when running testtools.run for TESTRTESTS="$TESTRTESTS --coverage"
# debugging purposes. else
return $? TESTRTESTS="$TESTRTESTS --slowest"
fi fi
if [ $coverage -eq 1 ]; then # Just run the test suites in current environment
TESTRTESTS="$TESTRTESTS --coverage" set +e
else testargs=`echo "$testargs" | sed -e's/^\s*\(.*\)\s*$/\1/'`
TESTRTESTS="$TESTRTESTS --slowest" TESTRTESTS="$TESTRTESTS --testr-args='--subunit $testopts $testargs'"
fi OS_TEST_PATH=`echo $testargs|grep -o 'neutron\.tests[^[:space:]:]\+'|tr . /`
if [ -n "$OS_TEST_PATH" ]; then
os_test_dir=$(dirname "$OS_TEST_PATH")
else
os_test_dir=''
fi
if [ -d "$OS_TEST_PATH" ]; then
wrapper="OS_TEST_PATH=$OS_TEST_PATH $wrapper"
elif [ -d "$os_test_dir" ]; then
wrapper="OS_TEST_PATH=$os_test_dir $wrapper"
fi
echo "Running \`${wrapper} $TESTRTESTS\`"
bash -c "${wrapper} $TESTRTESTS | ${wrapper} subunit2pyunit"
RESULT=$?
set -e
# Just run the test suites in current environment copy_subunit_log
set +e
testargs=`echo "$testargs" | sed -e's/^\s*\(.*\)\s*$/\1/'`
TESTRTESTS="$TESTRTESTS --testr-args='--subunit $testopts $testargs'"
OS_TEST_PATH=`echo $testargs|grep -o 'neutron\.tests[^[:space:]:]\+'|tr . /`
if [ -n "$OS_TEST_PATH" ]; then
os_test_dir=$(dirname "$OS_TEST_PATH")
else
os_test_dir=''
fi
if [ -d "$OS_TEST_PATH" ]; then
wrapper="OS_TEST_PATH=$OS_TEST_PATH $wrapper"
elif [ -d "$os_test_dir" ]; then
wrapper="OS_TEST_PATH=$os_test_dir $wrapper"
fi
echo "Running \`${wrapper} $TESTRTESTS\`"
bash -c "${wrapper} $TESTRTESTS | ${wrapper} subunit2pyunit"
RESULT=$?
set -e
copy_subunit_log if [ $coverage -eq 1 ]; then
echo "Generating coverage report in covhtml/"
# Don't compute coverage for common code, which is tested elsewhere
${wrapper} coverage combine
${wrapper} coverage html --include='neutron/*' -d covhtml -i
fi
if [ $coverage -eq 1 ]; then return $RESULT
echo "Generating coverage report in covhtml/"
# Don't compute coverage for common code, which is tested elsewhere
${wrapper} coverage combine
${wrapper} coverage html --include='neutron/*' -d covhtml -i
fi
return $RESULT
} }
function copy_subunit_log { function copy_subunit_log {
LOGNAME=`cat .testrepository/next-stream` LOGNAME=`cat .testrepository/next-stream`
LOGNAME=$(($LOGNAME - 1)) LOGNAME=$(($LOGNAME - 1))
LOGNAME=".testrepository/${LOGNAME}" LOGNAME=".testrepository/${LOGNAME}"
cp $LOGNAME subunit.log cp $LOGNAME subunit.log
} }
function warn_on_flake8_without_venv { function warn_on_flake8_without_venv {
if [ $never_venv -eq 1 ]; then if [ $never_venv -eq 1 ]; then
echo "**WARNING**:" echo "**WARNING**:"
echo "Running flake8 without virtual env may miss OpenStack HACKING detection" echo "Running flake8 without virtual env may miss OpenStack HACKING detection"
fi fi
} }
function run_pep8 { function run_pep8 {
echo "Running flake8 ..." echo "Running flake8 ..."
warn_on_flake8_without_venv warn_on_flake8_without_venv
${wrapper} flake8 ${wrapper} flake8
} }
function run_pep8_changed { function run_pep8_changed {
@ -194,7 +194,7 @@ function run_pep8_changed {
# --diff argument behaves surprisingly as well, because although you feed it a # --diff argument behaves surprisingly as well, because although you feed it a
# diff, it actually checks the file on disk anyway. # diff, it actually checks the file on disk anyway.
local target=${testargs:-HEAD~1} local target=${testargs:-HEAD~1}
local files=$(git diff --name-only $target | tr '\n' ' ') files=$(git diff --name-only $target | tr '\n' ' ')
echo "Running flake8 on ${files}" echo "Running flake8 on ${files}"
warn_on_flake8_without_venv warn_on_flake8_without_venv
diff -u --from-file /dev/null ${files} | ${wrapper} flake8 --diff diff -u --from-file /dev/null ${files} | ${wrapper} flake8 --diff
@ -203,34 +203,33 @@ function run_pep8_changed {
TESTRTESTS="python setup.py testr" TESTRTESTS="python setup.py testr"
if [ $never_venv -eq 0 ] if [ $never_venv -eq 0 ]; then
then # Remove the virtual environment if --force used
# Remove the virtual environment if --force used if [ $force -eq 1 ]; then
if [ $force -eq 1 ]; then echo "Cleaning virtualenv..."
echo "Cleaning virtualenv..." rm -rf ${venv}
rm -rf ${venv} fi
fi if [ $update -eq 1 ]; then
if [ $update -eq 1 ]; then echo "Updating virtualenv..."
echo "Updating virtualenv..." python tools/install_venv.py $installvenvopts
python tools/install_venv.py $installvenvopts fi
fi if [ -e ${venv} ]; then
if [ -e ${venv} ]; then wrapper="${with_venv}"
wrapper="${with_venv}" else
else if [ $always_venv -eq 1 ]; then
if [ $always_venv -eq 1 ]; then # Automatically install the virtualenv
# Automatically install the virtualenv python tools/install_venv.py $installvenvopts
python tools/install_venv.py $installvenvopts wrapper="${with_venv}"
wrapper="${with_venv}" else
else echo -e "No virtual environment found...create one? (Y/n) \c"
echo -e "No virtual environment found...create one? (Y/n) \c" read use_ve
read use_ve if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then
if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then # Install the virtualenv and run the test suite in it
# Install the virtualenv and run the test suite in it python tools/install_venv.py $installvenvopts
python tools/install_venv.py $installvenvopts wrapper=${with_venv}
wrapper=${with_venv} fi
fi fi
fi fi
fi
fi fi
# Delete old coverage data from previous runs # Delete old coverage data from previous runs
@ -259,7 +258,7 @@ run_tests
# distinguish between options (testopts), which begin with a '-', and # distinguish between options (testopts), which begin with a '-', and
# arguments (testargs). # arguments (testargs).
if [ -z "$testargs" ]; then if [ -z "$testargs" ]; then
if [ $no_pep8 -eq 0 ]; then if [ $no_pep8 -eq 0 ]; then
run_pep8 run_pep8
fi fi
fi fi

View File

@ -22,3 +22,4 @@ reno>=1.8.0 # Apache-2.0
# Needed to run DB commands in virtualenvs # Needed to run DB commands in virtualenvs
PyMySQL>=0.7.6 # MIT License PyMySQL>=0.7.6 # MIT License
tempest>=14.0.0 # Apache-2.0 tempest>=14.0.0 # Apache-2.0
bashate>=0.2 # Apache-2.0

View File

@ -43,10 +43,10 @@ function abandon_review {
local msg=$@ local msg=$@
# echo ssh review.openstack.org gerrit review $gitid --abandon --message \"$msg\" # echo ssh review.openstack.org gerrit review $gitid --abandon --message \"$msg\"
if [ $DRY_RUN -eq 1 ]; then if [ $DRY_RUN -eq 1 ]; then
echo "Would abandon $gitid" echo "Would abandon $gitid"
else else
echo "Abandoning $gitid" echo "Abandoning $gitid"
ssh review.openstack.org gerrit review $gitid --abandon --message \"$msg\" ssh review.openstack.org gerrit review $gitid --abandon --message \"$msg\"
fi fi
} }

View File

@ -3,26 +3,26 @@
set -eu set -eu
usage () { usage () {
echo "Usage: $0 [OPTION]..." echo "Usage: $0 [OPTION]..."
echo "Run Neutron's coding check(s)" echo "Run Neutron's coding check(s)"
echo "" echo ""
echo " -Y, --pylint [<basecommit>] Run pylint check on the entire neutron module or just files changed in basecommit (e.g. HEAD~1)" echo " -Y, --pylint [<basecommit>] Run pylint check on the entire neutron module or just files changed in basecommit (e.g. HEAD~1)"
echo " -h, --help Print this usage message" echo " -h, --help Print this usage message"
echo echo
exit 0 exit 0
} }
process_options () { process_options () {
i=1 i=1
while [ $i -le $# ]; do while [ $i -le $# ]; do
eval opt=\$$i eval opt=\$$i
case $opt in case $opt in
-h|--help) usage;; -h|--help) usage;;
-Y|--pylint) pylint=1;; -Y|--pylint) pylint=1;;
*) scriptargs="$scriptargs $opt" *) scriptargs="$scriptargs $opt"
esac esac
i=$((i+1)) i=$((i+1))
done done
} }
run_pylint () { run_pylint () {
@ -31,10 +31,10 @@ run_pylint () {
if [ "$target" = "all" ]; then if [ "$target" = "all" ]; then
files="neutron" files="neutron"
else else
case "$target" in case "$target" in
*HEAD~[0-9]*) files=$(git diff --diff-filter=AM --name-only $target -- "*.py");; *HEAD~[0-9]*) files=$(git diff --diff-filter=AM --name-only $target -- "*.py");;
*) echo "$target is an unrecognized basecommit"; exit 1;; *) echo "$target is an unrecognized basecommit"; exit 1;;
esac esac
fi fi
echo "Running pylint..." echo "Running pylint..."

View File

@ -26,9 +26,9 @@ if [[ "$IS_GATE" != "True" ]] && [[ "$#" -lt 1 ]]; then
>&2 echo "Usage: $0 /path/to/devstack [-i] >&2 echo "Usage: $0 /path/to/devstack [-i]
Configure a host to run Neutron's functional test suite. Configure a host to run Neutron's functional test suite.
-i Install Neutron's package dependencies. By default, it is assumed -i Install Neutron's package dependencies. By default, it is assumed
that devstack has already been used to deploy neutron to the that devstack has already been used to deploy neutron to the
target host and that package dependencies need not be installed. target host and that package dependencies need not be installed.
Warning: This script relies on devstack to perform extensive Warning: This script relies on devstack to perform extensive
modification to the underlying host. It is recommended that it be modification to the underlying host. It is recommended that it be
@ -60,8 +60,8 @@ INSTALL_BASE_DEPENDENCIES=${INSTALL_BASE_DEPENDENCIES:-$IS_GATE}
if [ ! -f "$DEVSTACK_PATH/stack.sh" ]; then if [ ! -f "$DEVSTACK_PATH/stack.sh" ]; then
>&2 echo "Unable to find devstack at '$DEVSTACK_PATH'. Please verify that the specified path points to a valid devstack repo." >&2 echo "Unable to find devstack at '$DEVSTACK_PATH'. Please verify that the specified path points to a valid devstack repo."
exit 1 exit 1
fi fi

View File

@ -15,7 +15,7 @@
set -eu set -eu
if [ "$#" -ne 3 ]; then if [ "$#" -ne 3 ]; then
>&2 echo "Usage: $0 /path/to/neutron /path/to/target/etc /path/to/target/bin >&2 echo "Usage: $0 /path/to/neutron /path/to/target/etc /path/to/target/bin
Deploy Neutron's rootwrap configuration. Deploy Neutron's rootwrap configuration.
Warning: Any existing rootwrap files at the specified etc path will be Warning: Any existing rootwrap files at the specified etc path will be
@ -23,7 +23,7 @@ removed by this script.
Optional: set OS_SUDO_TESTING=1 to deploy the filters required by Optional: set OS_SUDO_TESTING=1 to deploy the filters required by
Neutron's functional testing suite." Neutron's functional testing suite."
exit 1 exit 1
fi fi
OS_SUDO_TESTING=${OS_SUDO_TESTING:-0} OS_SUDO_TESTING=${OS_SUDO_TESTING:-0}

View File

@ -38,8 +38,8 @@ check_pot_files_errors () {
# obsolete entries duplicate normal entries. Prevent obsolete # obsolete entries duplicate normal entries. Prevent obsolete
# entries to slip in # entries to slip in
find neutron -type f -regex '.*\.pot?' \ find neutron -type f -regex '.*\.pot?' \
-print0|xargs -0 -n 1 msgfmt --check-format \ -print0|xargs -0 -n 1 msgfmt --check-format \
-o /dev/null -o /dev/null
if [ "$?" -ne 0 ]; then if [ "$?" -ne 0 ]; then
echo "PO files syntax is not correct!" >>$FAILURES echo "PO files syntax is not correct!" >>$FAILURES
fi fi

View File

@ -2,7 +2,7 @@
# preserve old behavior of using an arg as a regex when '--' is not present # preserve old behavior of using an arg as a regex when '--' is not present
case $@ in case $@ in
(*--*) ostestr $@;; (*--*) ostestr $@;;
('') ostestr;; ('') ostestr;;
(*) ostestr --regex "$@" (*) ostestr --regex "$@"
esac esac

View File

@ -24,8 +24,8 @@
set -e set -e
if [ $# -lt 2 ]; then if [ $# -lt 2 ]; then
echo "Usage $0 <path to file containing list of files to export> <project name>" echo "Usage $0 <path to file containing list of files to export> <project name>"
exit 1 exit 1
fi fi
set -x set -x

12
tox.ini
View File

@ -94,6 +94,7 @@ commands=
neutron-db-manage --config-file neutron/tests/etc/neutron.conf check_migration neutron-db-manage --config-file neutron/tests/etc/neutron.conf check_migration
python ./tools/list_moved_globals.py python ./tools/list_moved_globals.py
{[testenv:genconfig]commands} {[testenv:genconfig]commands}
{[testenv:bashate]commands}
whitelist_externals = whitelist_externals =
sh sh
bash bash
@ -128,6 +129,17 @@ exclude = ./.*,build,dist
import_exceptions = neutron._i18n import_exceptions = neutron._i18n
local-check-factory = neutron.hacking.checks.factory local-check-factory = neutron.hacking.checks.factory
[testenv:bashate]
commands = bash -c "find {toxinidir} \
-not \( -type d -name .tox\* -prune \) \
-not \( -type d -name .venv\* -prune \) \
-type f \
-name \*.sh \
# E005 file does not begin with #! or have a .sh prefix
# E006 check for lines longer than 79 columns
# E042 local declaration hides errors
-print0 | xargs -0 bashate -v -iE006 -eE005,E042"
[testenv:genconfig] [testenv:genconfig]
commands = {toxinidir}/tools/generate_config_file_samples.sh commands = {toxinidir}/tools/generate_config_file_samples.sh