Browse code

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

Ian Wienand authored on 2015/08/25 13:29:08
Showing 4 changed files
... ...
@@ -106,16 +106,27 @@ function write_clouds_yaml {
106 106
         --os-project-name admin
107 107
 }
108 108
 
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)
113 120
 function trueorfalse {
114 121
     local xtrace
115 122
     xtrace=$(set +o | grep xtrace)
116 123
     set +o xtrace
117 124
 
118 125
     local default=$1
126
+
127
+    if [ -z $2 ]; then
128
+        die $LINENO "variable to normalize required"
129
+    fi
119 130
     local testval=${!2:-}
120 131
 
121 132
     case "$testval" in
... ...
@@ -59,10 +59,10 @@ HEAT_BIN_DIR=$(get_python_exec_prefix)
59 59
 # other default options
60 60
 if [[ "$HEAT_STANDALONE" = "True" ]]; then
61 61
     # 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)
63 63
     HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-password}
64 64
 else
65
-    HEAT_STACK_DOMAIN=`trueorfalse True $HEAT_STACK_DOMAIN`
65
+    HEAT_STACK_DOMAIN=$(trueorfalse True HEAT_STACK_DOMAIN)
66 66
     HEAT_DEFERRED_AUTH=${HEAT_DEFERRED_AUTH:-trusts}
67 67
 fi
68 68
 
... ...
@@ -8,6 +8,14 @@ TOP=$(cd $(dirname "$0")/.. && pwd)
8 8
 source $TOP/functions
9 9
 source $TOP/tests/unittest.sh
10 10
 
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
+
11 19
 function test_trueorfalse {
12 20
     local one=1
13 21
     local captrue=True
... ...
@@ -108,7 +108,7 @@ if is_fedora; then
108 108
         sudo setenforce 0
109 109
     fi
110 110
 
111
-    FORCE_FIREWALLD=$(trueorfalse False $FORCE_FIREWALLD)
111
+    FORCE_FIREWALLD=$(trueorfalse False FORCE_FIREWALLD)
112 112
     if [[ $FORCE_FIREWALLD == "False" ]]; then
113 113
         # On Fedora 20 firewalld interacts badly with libvirt and
114 114
         # slows things down significantly (this issue was fixed in