Skip to content

Commit e82bac0

Browse files
committed
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
1 parent c1561f8 commit e82bac0

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

functions-common

+15-4
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,27 @@ function write_clouds_yaml {
106106
--os-project-name admin
107107
}
108108

109-
# Normalize config values to True or False
110-
# Accepts as False: 0 no No NO false False FALSE
111-
# Accepts as True: 1 yes Yes YES true True TRUE
112-
# VAR=$(trueorfalse default-value test-value)
109+
# trueorfalse <True|False> <VAR>
110+
#
111+
# Normalize config-value provided in variable VAR to either "True" or
112+
# "False". If VAR is unset (i.e. $VAR evaluates as empty), the value
113+
# of the second argument will be used as the default value.
114+
#
115+
# Accepts as False: 0 no No NO false False FALSE
116+
# Accepts as True: 1 yes Yes YES true True TRUE
117+
#
118+
# usage:
119+
# VAL=$(trueorfalse False VAL)
113120
function trueorfalse {
114121
local xtrace
115122
xtrace=$(set +o | grep xtrace)
116123
set +o xtrace
117124

118125
local default=$1
126+
127+
if [ -z $2 ]; then
128+
die $LINENO "variable to normalize required"
129+
fi
119130
local testval=${!2:-}
120131

121132
case "$testval" in

lib/heat

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ HEAT_BIN_DIR=$(get_python_exec_prefix)
5959
# other default options
6060
if [[ "$HEAT_STANDALONE" = "True" ]]; then
6161
# for standalone, use defaults which require no service user
62-
HEAT_STACK_DOMAIN=`trueorfalse False $HEAT_STACK_DOMAIN`
62+
HEAT_STACK_DOMAIN=$(trueorfalse False HEAT_STACK_DOMAIN)
6363
HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-password}
6464
else
65-
HEAT_STACK_DOMAIN=`trueorfalse True $HEAT_STACK_DOMAIN`
65+
HEAT_STACK_DOMAIN=$(trueorfalse True HEAT_STACK_DOMAIN)
6666
HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-trusts}
6767
fi
6868

tests/test_truefalse.sh

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ TOP=$(cd $(dirname "$0")/.. && pwd)
88
source $TOP/functions
99
source $TOP/tests/unittest.sh
1010

11+
# common mistake is to use $FOO instead of "FOO"; in that case we
12+
# should die
13+
bash -c "source $TOP/functions-common; VAR=\$(trueorfalse False \$FOO)" &> /dev/null
14+
assert_equal 1 $? "missing test-value"
15+
16+
VAL=$(trueorfalse False MISSING_VARIABLE)
17+
assert_equal "False" $VAL "blank test-value"
18+
1119
function test_trueorfalse {
1220
local one=1
1321
local captrue=True

tools/fixup_stuff.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ if is_fedora; then
108108
sudo setenforce 0
109109
fi
110110

111-
FORCE_FIREWALLD=$(trueorfalse False $FORCE_FIREWALLD)
111+
FORCE_FIREWALLD=$(trueorfalse False FORCE_FIREWALLD)
112112
if [[ $FORCE_FIREWALLD == "False" ]]; then
113113
# On Fedora 20 firewalld interacts badly with libvirt and
114114
# slows things down significantly (this issue was fixed in

0 commit comments

Comments
 (0)