From e82bac04981c8e41a919907d16621c9c952d9224 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 25 Aug 2015 14:29:08 +1000 Subject: [PATCH] Detect blank variable in trueorfalse As a follow-on to I8cefb58f49dcd2cb2def8a5071d0892af520e7f7, put in some detection around missing variable-to-test arguments in trueorfalse. Correct a couple of places where we were passing in blank strings, resulting in the default always being applied. Add test-cases and enhance the documentation a little. Depends-On: I8cefb58f49dcd2cb2def8a5071d0892af520e7f7 Change-Id: Icc0eb3808a2b6583828d8c47f0af4181e7e2c75a --- functions-common | 19 +++++++++++++++---- lib/heat | 4 ++-- tests/test_truefalse.sh | 8 ++++++++ tools/fixup_stuff.sh | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/functions-common b/functions-common index f9e0b5adaa..d506b846bd 100644 --- a/functions-common +++ b/functions-common @@ -106,16 +106,27 @@ function write_clouds_yaml { --os-project-name admin } -# Normalize config values to True or False -# Accepts as False: 0 no No NO false False FALSE -# Accepts as True: 1 yes Yes YES true True TRUE -# VAR=$(trueorfalse default-value test-value) +# trueorfalse +# +# Normalize config-value provided in variable VAR to either "True" or +# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value +# of the second argument will be used as the default value. +# +# Accepts as False: 0 no No NO false False FALSE +# Accepts as True: 1 yes Yes YES true True TRUE +# +# usage: +# VAL=$(trueorfalse False VAL) function trueorfalse { local xtrace xtrace=$(set +o | grep xtrace) set +o xtrace local default=$1 + + if [ -z $2 ]; then + die $LINENO "variable to normalize required" + fi local testval=${!2:-} case "$testval" in diff --git a/lib/heat b/lib/heat index 3e6975ae26..c22369f56c 100644 --- a/lib/heat +++ b/lib/heat @@ -59,10 +59,10 @@ HEAT_BIN_DIR=$(get_python_exec_prefix) # other default options if [[ "$HEAT_STANDALONE" = "True" ]]; then # for standalone, use defaults which require no service user - HEAT_STACK_DOMAIN=`trueorfalse False $HEAT_STACK_DOMAIN` + HEAT_STACK_DOMAIN=$(trueorfalse False HEAT_STACK_DOMAIN) HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-password} else - HEAT_STACK_DOMAIN=`trueorfalse True $HEAT_STACK_DOMAIN` + HEAT_STACK_DOMAIN=$(trueorfalse True HEAT_STACK_DOMAIN) HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-trusts} fi diff --git a/tests/test_truefalse.sh b/tests/test_truefalse.sh index 2689589dc9..03996ceab4 100755 --- a/tests/test_truefalse.sh +++ b/tests/test_truefalse.sh @@ -8,6 +8,14 @@ TOP=$(cd $(dirname "$0")/.. && pwd) source $TOP/functions source $TOP/tests/unittest.sh +# common mistake is to use $FOO instead of "FOO"; in that case we +# should die +bash -c "source $TOP/functions-common; VAR=\$(trueorfalse False \$FOO)" &> /dev/null +assert_equal 1 $? "missing test-value" + +VAL=$(trueorfalse False MISSING_VARIABLE) +assert_equal "False" $VAL "blank test-value" + function test_trueorfalse { local one=1 local captrue=True diff --git a/tools/fixup_stuff.sh b/tools/fixup_stuff.sh index a601cf2f67..6ef32c8894 100755 --- a/tools/fixup_stuff.sh +++ b/tools/fixup_stuff.sh @@ -108,7 +108,7 @@ if is_fedora; then sudo setenforce 0 fi - FORCE_FIREWALLD=$(trueorfalse False $FORCE_FIREWALLD) + FORCE_FIREWALLD=$(trueorfalse False FORCE_FIREWALLD) if [[ $FORCE_FIREWALLD == "False" ]]; then # On Fedora 20 firewalld interacts badly with libvirt and # slows things down significantly (this issue was fixed in