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