| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
 | 3 | # Tests for DevStack INI functions | 
 | 4 |  | 
 | 5 | TOP=$(cd $(dirname "$0")/.. && pwd) | 
 | 6 |  | 
| Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 7 | # Import config functions | 
 | 8 | source $TOP/inc/ini-config | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 9 |  | 
| Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 10 | source $TOP/tests/unittest.sh | 
 | 11 |  | 
 | 12 | set -e | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 13 |  | 
 | 14 | echo "Testing INI functions" | 
 | 15 |  | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 16 | INI_TMP_DIR=$(mktemp -d) | 
 | 17 | INI_TMP_ETC_DIR=$INI_TMP_DIR/etc | 
 | 18 | TEST_INI=${INI_TMP_ETC_DIR}/test.ini | 
 | 19 | mkdir ${INI_TMP_ETC_DIR} | 
 | 20 |  | 
 | 21 | echo "Creating $TEST_INI" | 
 | 22 | cat >${TEST_INI} <<EOF | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 23 | [default] | 
 | 24 | # comment an option | 
 | 25 | #log_file=./log.conf | 
 | 26 | log_file=/etc/log.conf | 
 | 27 | handlers=do not disturb | 
 | 28 |  | 
 | 29 | [aaa] | 
 | 30 | # the commented option should not change | 
 | 31 | #handlers=cc,dd | 
 | 32 | handlers = aa, bb | 
 | 33 |  | 
 | 34 | [bbb] | 
 | 35 | handlers=ee,ff | 
 | 36 |  | 
 | 37 | [ ccc ] | 
 | 38 | spaces  =  yes | 
 | 39 |  | 
 | 40 | [ddd] | 
 | 41 | empty = | 
 | 42 |  | 
 | 43 | [eee] | 
 | 44 | multi = foo1 | 
 | 45 | multi = foo2 | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 46 |  | 
| Nobuhiro MIKI | d266c87 | 2022-08-08 16:45:31 +0900 | [diff] [blame] | 47 | [fff] | 
 | 48 | ampersand = | 
 | 49 |  | 
| Luigi Toscano | c7c6765 | 2018-06-04 10:59:57 +0200 | [diff] [blame] | 50 | [key_with_spaces] | 
 | 51 | rgw special key = something | 
 | 52 |  | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 53 | # inidelete(a) | 
 | 54 | [del_separate_options] | 
 | 55 | a=b | 
 | 56 | b=c | 
 | 57 |  | 
 | 58 | # inidelete(a) | 
 | 59 | [del_same_option] | 
 | 60 | a=b | 
 | 61 | a=c | 
 | 62 |  | 
 | 63 | # inidelete(a) | 
 | 64 | [del_missing_option] | 
 | 65 | b=c | 
 | 66 |  | 
 | 67 | # inidelete(a) | 
 | 68 | [del_missing_option_multi] | 
 | 69 | b=c | 
 | 70 | b=d | 
 | 71 |  | 
 | 72 | # inidelete(a) | 
 | 73 | [del_no_options] | 
 | 74 |  | 
 | 75 | # inidelete(a) | 
 | 76 | # no section - del_no_section | 
 | 77 |  | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 78 | EOF | 
 | 79 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 80 | # set TEST_SUDO to test writing to root-owned files | 
 | 81 | SUDO_ARG="" | 
 | 82 | SUDO="" | 
 | 83 | if [ -n "$TEST_SUDO" ]; then | 
 | 84 |     SUDO="sudo " | 
 | 85 |     SUDO_ARG="-sudo " | 
 | 86 |     sudo chown -R root:root ${INI_TMP_ETC_DIR} | 
 | 87 | fi | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 88 |  | 
| vsaienko | 135bd48 | 2015-12-11 11:03:52 +0200 | [diff] [blame] | 89 | # test iniget_sections | 
 | 90 | VAL=$(iniget_sections "${TEST_INI}") | 
| Nobuhiro MIKI | d266c87 | 2022-08-08 16:45:31 +0900 | [diff] [blame] | 91 | assert_equal "$VAL" "default aaa bbb ccc ddd eee fff key_with_spaces \ | 
| Luigi Toscano | c7c6765 | 2018-06-04 10:59:57 +0200 | [diff] [blame] | 92 | del_separate_options del_same_option del_missing_option \ | 
 | 93 | del_missing_option_multi del_no_options" | 
| vsaienko | 135bd48 | 2015-12-11 11:03:52 +0200 | [diff] [blame] | 94 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 95 | # Test with missing arguments | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 96 | BEFORE=$(cat ${TEST_INI}) | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 97 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 98 | iniset ${SUDO_ARG} ${TEST_INI} aaa | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 99 | NO_ATTRIBUTE=$(cat ${TEST_INI}) | 
 | 100 | assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 101 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 102 | iniset ${SUDO_ARG} ${TEST_INI} | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 103 | NO_SECTION=$(cat ${TEST_INI}) | 
 | 104 | assert_equal "$BEFORE" "$NO_SECTION" "missing section argument" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 105 |  | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 106 | # Test with spaces in values | 
 | 107 | VAL=$(iniget ${TEST_INI} aaa handlers) | 
 | 108 | assert_equal "$VAL" "aa, bb" "iniget spaces in option" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 109 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 110 | iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "11, 22" | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 111 | VAL=$(iniget ${TEST_INI} aaa handlers) | 
 | 112 | assert_equal "$VAL" "11, 22" "iniset spaces in option" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 113 |  | 
 | 114 | # Test with spaces in section header | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 115 | VAL=$(iniget ${TEST_INI} " ccc " spaces) | 
 | 116 | assert_equal "$VAL" "yes" "iniget with section header space" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 117 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 118 | iniset ${SUDO_ARG} ${TEST_INI} "b b" opt_ion 42 | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 119 | VAL=$(iniget ${TEST_INI} "b b" opt_ion) | 
 | 120 | assert_equal "$VAL" "42" "iniset with section header space" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 121 |  | 
 | 122 | # Test without spaces, end of file | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 123 | VAL=$(iniget ${TEST_INI} bbb handlers) | 
 | 124 | assert_equal "$VAL" "ee,ff" "iniget at EOF" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 125 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 126 | iniset ${SUDO_ARG} ${TEST_INI} bbb handlers "33,44" | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 127 | VAL=$(iniget ${TEST_INI} bbb handlers) | 
 | 128 | assert_equal "$VAL" "33,44" "inset at EOF" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 129 |  | 
| Nobuhiro MIKI | d266c87 | 2022-08-08 16:45:31 +0900 | [diff] [blame] | 130 | # Test with ampersand in values | 
 | 131 | for i in `seq 3`; do | 
 | 132 |     iniset ${TEST_INI} fff ampersand '&y' | 
 | 133 | done | 
 | 134 | VAL=$(iniget ${TEST_INI} fff ampersand) | 
 | 135 | assert_equal "$VAL" "&y" "iniset ampersands in option" | 
 | 136 |  | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 137 | # test empty option | 
| Yi Wang | 698796f | 2018-12-14 10:35:26 +0800 | [diff] [blame] | 138 | if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 139 |     passed "ini_has_option: ddd.empty present" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 140 | else | 
| Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 141 |     failed "ini_has_option failed: ddd.empty not found" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 142 | fi | 
 | 143 |  | 
 | 144 | # test non-empty option | 
| Yi Wang | 698796f | 2018-12-14 10:35:26 +0800 | [diff] [blame] | 145 | if ini_has_option ${SUDO_ARG} ${TEST_INI} bbb handlers; then | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 146 |     passed "ini_has_option: bbb.handlers present" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 147 | else | 
| Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 148 |     failed "ini_has_option failed: bbb.handlers not found" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 149 | fi | 
 | 150 |  | 
 | 151 | # test changing empty option | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 152 | iniset ${SUDO_ARG} ${TEST_INI} ddd empty "42" | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 153 | VAL=$(iniget ${TEST_INI} ddd empty) | 
 | 154 | assert_equal "$VAL" "42" "change empty option" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 155 |  | 
| Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 156 | # test pipe in option | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 157 | iniset ${SUDO_ARG} ${TEST_INI} aaa handlers "a|b" | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 158 | VAL=$(iniget ${TEST_INI} aaa handlers) | 
 | 159 | assert_equal "$VAL" "a|b" "pipe in option" | 
| Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 160 |  | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 161 | # Test section not exist | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 162 | VAL=$(iniget ${TEST_INI} zzz handlers) | 
 | 163 | assert_empty VAL "section does not exist" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 164 |  | 
 | 165 | # Test option not exist | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 166 | VAL=$(iniget ${TEST_INI} aaa debug) | 
 | 167 | assert_empty VAL "option does not exist" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 168 |  | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 169 | if ! ini_has_option ${TEST_INI} aaa debug; then | 
 | 170 |     passed "ini_has_option: aaa.debug not present" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 171 | else | 
| Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 172 |     failed "ini_has_option failed: aaa.debug" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 173 | fi | 
 | 174 |  | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 175 | # Test comments | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 176 | inicomment ${SUDO_ARG} ${TEST_INI} aaa handlers | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 177 | VAL=$(iniget ${TEST_INI} aaa handlers) | 
 | 178 | assert_empty VAL "test inicomment" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 179 |  | 
 | 180 | # Test multiple line iniset/iniget | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 181 | iniset_multiline ${SUDO_ARG} ${TEST_INI} eee multi bar1 bar2 | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 182 |  | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 183 | VAL=$(iniget_multiline ${TEST_INI} eee multi) | 
 | 184 | assert_equal "$VAL" "bar1 bar2" "iniget_multiline" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 185 |  | 
 | 186 | # Test iniadd with exiting values | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 187 | iniadd ${SUDO_ARG} ${TEST_INI} eee multi bar3 | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 188 | VAL=$(iniget_multiline ${TEST_INI} eee multi) | 
 | 189 | assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 190 |  | 
 | 191 | # Test iniadd with non-exiting values | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 192 | iniadd ${SUDO_ARG} ${TEST_INI} eee non-multi foobar1 foobar2 | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 193 | VAL=$(iniget_multiline ${TEST_INI} eee non-multi) | 
 | 194 | assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values" | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 195 |  | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 196 | # Test inidelete | 
 | 197 | del_cases=" | 
 | 198 |     del_separate_options | 
 | 199 |     del_same_option | 
 | 200 |     del_missing_option | 
 | 201 |     del_missing_option_multi | 
 | 202 |     del_no_options | 
 | 203 |     del_no_section" | 
 | 204 |  | 
 | 205 | for x in $del_cases; do | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 206 |     inidelete ${SUDO_ARG} ${TEST_INI} $x a | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 207 |     VAL=$(iniget_multiline ${TEST_INI} $x a) | 
 | 208 |     assert_empty VAL "inidelete $x" | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 209 |     if [ "$x" = "del_separate_options" -o \ | 
 | 210 |         "$x" = "del_missing_option" -o \ | 
 | 211 |         "$x" = "del_missing_option_multi" ]; then | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 212 |         VAL=$(iniget_multiline ${TEST_INI} $x b) | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 213 |         if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 214 |             passed "inidelete other_options $x" | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 215 |         else | 
| Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 216 |             failed "inidelete other_option $x: $VAL" | 
| Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 217 |         fi | 
 | 218 |     fi | 
 | 219 | done | 
 | 220 |  | 
| Ian Wienand | cede787 | 2015-07-22 13:36:12 +1000 | [diff] [blame] | 221 | # test file-creation | 
 | 222 | iniset $SUDO_ARG ${INI_TMP_ETC_DIR}/test.new.ini test foo bar | 
 | 223 | VAL=$(iniget ${INI_TMP_ETC_DIR}/test.new.ini test foo) | 
 | 224 | assert_equal "$VAL" "bar" "iniset created file" | 
 | 225 |  | 
| Luigi Toscano | c7c6765 | 2018-06-04 10:59:57 +0200 | [diff] [blame] | 226 | # test creation of keys with spaces | 
 | 227 | iniset ${SUDO_ARG} ${TEST_INI} key_with_spaces "rgw another key" somethingelse | 
 | 228 | VAL=$(iniget ${TEST_INI} key_with_spaces "rgw another key") | 
 | 229 | assert_equal "$VAL" "somethingelse" "iniset created a key with spaces" | 
 | 230 |  | 
 | 231 | # test update of keys with spaces | 
 | 232 | iniset ${SUDO_ARG} ${TEST_INI} key_with_spaces "rgw special key" newvalue | 
 | 233 | VAL=$(iniget ${TEST_INI} key_with_spaces "rgw special key") | 
 | 234 | assert_equal "$VAL" "newvalue" "iniset updated a key with spaces" | 
 | 235 |  | 
 | 236 | inidelete ${SUDO_ARG} ${TEST_INI} key_with_spaces "rgw another key" | 
 | 237 | VAL=$(iniget ${TEST_INI} key_with_spaces "rgw another key") | 
 | 238 | assert_empty VAL "inidelete removed a key with spaces" | 
 | 239 |  | 
| Ian Wienand | f44a024 | 2015-07-22 10:34:47 +1000 | [diff] [blame] | 240 | $SUDO rm -rf ${INI_TMP_DIR} | 
| Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 241 |  | 
 | 242 | report_results |