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
This commit is contained in:
Ian Wienand 2015-08-25 14:29:08 +10:00
parent c1561f84fb
commit e82bac0498
4 changed files with 26 additions and 7 deletions

View File

@ -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 <True|False> <VAR>
#
# 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

View File

@ -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

View File

@ -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

View File

@ -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