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 | |
| 16 | cat >test.ini <<EOF |
| 17 | [default] |
| 18 | # comment an option |
| 19 | #log_file=./log.conf |
| 20 | log_file=/etc/log.conf |
| 21 | handlers=do not disturb |
| 22 | |
| 23 | [aaa] |
| 24 | # the commented option should not change |
| 25 | #handlers=cc,dd |
| 26 | handlers = aa, bb |
| 27 | |
| 28 | [bbb] |
| 29 | handlers=ee,ff |
| 30 | |
| 31 | [ ccc ] |
| 32 | spaces = yes |
| 33 | |
| 34 | [ddd] |
| 35 | empty = |
| 36 | |
| 37 | [eee] |
| 38 | multi = foo1 |
| 39 | multi = foo2 |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 40 | |
| 41 | # inidelete(a) |
| 42 | [del_separate_options] |
| 43 | a=b |
| 44 | b=c |
| 45 | |
| 46 | # inidelete(a) |
| 47 | [del_same_option] |
| 48 | a=b |
| 49 | a=c |
| 50 | |
| 51 | # inidelete(a) |
| 52 | [del_missing_option] |
| 53 | b=c |
| 54 | |
| 55 | # inidelete(a) |
| 56 | [del_missing_option_multi] |
| 57 | b=c |
| 58 | b=d |
| 59 | |
| 60 | # inidelete(a) |
| 61 | [del_no_options] |
| 62 | |
| 63 | # inidelete(a) |
| 64 | # no section - del_no_section |
| 65 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 66 | EOF |
| 67 | |
| 68 | # Test with missing arguments |
| 69 | |
| 70 | BEFORE=$(cat test.ini) |
| 71 | |
| 72 | echo -n "iniset: test missing attribute argument: " |
| 73 | iniset test.ini aaa |
| 74 | NO_ATTRIBUTE=$(cat test.ini) |
| 75 | if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 76 | passed |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 77 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 78 | failed "failed" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 79 | fi |
| 80 | |
| 81 | echo -n "iniset: test missing section argument: " |
| 82 | iniset test.ini |
| 83 | NO_SECTION=$(cat test.ini) |
| 84 | if [[ "$BEFORE" == "$NO_SECTION" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 85 | passed |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 86 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 87 | failed "failed" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 88 | fi |
| 89 | |
| 90 | # Test with spaces |
| 91 | |
| 92 | VAL=$(iniget test.ini aaa handlers) |
| 93 | if [[ "$VAL" == "aa, bb" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 94 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 95 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 96 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 97 | fi |
| 98 | |
| 99 | iniset test.ini aaa handlers "11, 22" |
| 100 | |
| 101 | VAL=$(iniget test.ini aaa handlers) |
| 102 | if [[ "$VAL" == "11, 22" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 103 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 104 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 105 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 106 | fi |
| 107 | |
| 108 | # Test with spaces in section header |
| 109 | |
| 110 | VAL=$(iniget test.ini " ccc " spaces) |
| 111 | if [[ "$VAL" == "yes" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 112 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 113 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 114 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 115 | fi |
| 116 | |
| 117 | iniset test.ini "b b" opt_ion 42 |
| 118 | |
| 119 | VAL=$(iniget test.ini "b b" opt_ion) |
| 120 | if [[ "$VAL" == "42" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 121 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 122 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 123 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 124 | fi |
| 125 | |
| 126 | # Test without spaces, end of file |
| 127 | |
| 128 | VAL=$(iniget test.ini bbb handlers) |
| 129 | if [[ "$VAL" == "ee,ff" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 130 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 131 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 132 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 133 | fi |
| 134 | |
| 135 | iniset test.ini bbb handlers "33,44" |
| 136 | |
| 137 | VAL=$(iniget test.ini bbb handlers) |
| 138 | if [[ "$VAL" == "33,44" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 139 | passed "OK: $VAL" |
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 "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 142 | fi |
| 143 | |
| 144 | # test empty option |
| 145 | if ini_has_option test.ini ddd empty; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 146 | passed "OK: ddd.empty 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: ddd.empty not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 149 | fi |
| 150 | |
| 151 | # test non-empty option |
| 152 | if ini_has_option test.ini bbb handlers; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 153 | passed "OK: bbb.handlers present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 154 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 155 | failed "ini_has_option failed: bbb.handlers not found" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 156 | fi |
| 157 | |
| 158 | # test changing empty option |
| 159 | iniset test.ini ddd empty "42" |
| 160 | |
| 161 | VAL=$(iniget test.ini ddd empty) |
| 162 | if [[ "$VAL" == "42" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 163 | passed "OK: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 164 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 165 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 166 | fi |
| 167 | |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 168 | # test pipe in option |
| 169 | iniset test.ini aaa handlers "a|b" |
| 170 | |
| 171 | VAL=$(iniget test.ini aaa handlers) |
| 172 | if [[ "$VAL" == "a|b" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 173 | passed "OK: $VAL" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 174 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 175 | failed "iniget failed: $VAL" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 176 | fi |
| 177 | |
| 178 | # test space in option |
| 179 | iniset test.ini aaa handlers "a b" |
| 180 | |
| 181 | VAL="$(iniget test.ini aaa handlers)" |
| 182 | if [[ "$VAL" == "a b" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 183 | passed "OK: $VAL" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 184 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 185 | failed "iniget failed: $VAL" |
Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 186 | fi |
| 187 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 188 | # Test section not exist |
| 189 | |
| 190 | VAL=$(iniget test.ini zzz handlers) |
| 191 | if [[ -z "$VAL" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 192 | passed "OK: zzz not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 193 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 194 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 195 | fi |
| 196 | |
| 197 | iniset test.ini zzz handlers "999" |
| 198 | |
| 199 | VAL=$(iniget test.ini zzz handlers) |
| 200 | if [[ -n "$VAL" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 201 | passed "OK: zzz not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 202 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 203 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 204 | fi |
| 205 | |
| 206 | # Test option not exist |
| 207 | |
| 208 | VAL=$(iniget test.ini aaa debug) |
| 209 | if [[ -z "$VAL" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 210 | passed "OK aaa.debug not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 211 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 212 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 213 | fi |
| 214 | |
| 215 | if ! ini_has_option test.ini aaa debug; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 216 | passed "OK aaa.debug not present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 217 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 218 | failed "ini_has_option failed: aaa.debug" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 219 | fi |
| 220 | |
| 221 | iniset test.ini aaa debug "999" |
| 222 | |
| 223 | VAL=$(iniget test.ini aaa debug) |
| 224 | if [[ -n "$VAL" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 225 | passed "OK aaa.debug present" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 226 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 227 | failed "iniget failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 228 | fi |
| 229 | |
| 230 | # Test comments |
| 231 | |
| 232 | inicomment test.ini aaa handlers |
| 233 | |
| 234 | VAL=$(iniget test.ini aaa handlers) |
| 235 | if [[ -z "$VAL" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 236 | passed "OK" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 237 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 238 | failed "inicomment failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 239 | fi |
| 240 | |
| 241 | # Test multiple line iniset/iniget |
| 242 | iniset_multiline test.ini eee multi bar1 bar2 |
| 243 | |
| 244 | VAL=$(iniget_multiline test.ini eee multi) |
| 245 | if [[ "$VAL" == "bar1 bar2" ]]; then |
| 246 | echo "OK: iniset_multiline" |
| 247 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 248 | failed "iniset_multiline failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 249 | fi |
| 250 | |
| 251 | # Test iniadd with exiting values |
| 252 | iniadd test.ini eee multi bar3 |
| 253 | VAL=$(iniget_multiline test.ini eee multi) |
| 254 | if [[ "$VAL" == "bar1 bar2 bar3" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 255 | passed "OK: iniadd" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 256 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 257 | failed "iniadd failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 258 | fi |
| 259 | |
| 260 | # Test iniadd with non-exiting values |
| 261 | iniadd test.ini eee non-multi foobar1 foobar2 |
| 262 | VAL=$(iniget_multiline test.ini eee non-multi) |
| 263 | if [[ "$VAL" == "foobar1 foobar2" ]]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 264 | passed "OK: iniadd with non-exiting value" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 265 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 266 | failed "iniadd with non-exsting failed: $VAL" |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 267 | fi |
| 268 | |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 269 | # Test inidelete |
| 270 | del_cases=" |
| 271 | del_separate_options |
| 272 | del_same_option |
| 273 | del_missing_option |
| 274 | del_missing_option_multi |
| 275 | del_no_options |
| 276 | del_no_section" |
| 277 | |
| 278 | for x in $del_cases; do |
| 279 | inidelete test.ini $x a |
| 280 | VAL=$(iniget_multiline test.ini $x a) |
| 281 | if [ -z "$VAL" ]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 282 | passed "OK: inidelete $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 283 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 284 | failed "inidelete $x failed: $VAL" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 285 | fi |
| 286 | if [ "$x" = "del_separate_options" -o \ |
| 287 | "$x" = "del_missing_option" -o \ |
| 288 | "$x" = "del_missing_option_multi" ]; then |
| 289 | VAL=$(iniget_multiline test.ini $x b) |
| 290 | if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 291 | passed "OK: inidelete other_options $x" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 292 | else |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 293 | failed "inidelete other_option $x failed: $VAL" |
Doug Wiegley | 1f65fd6 | 2014-12-13 11:56:16 -0700 | [diff] [blame] | 294 | fi |
| 295 | fi |
| 296 | done |
| 297 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 298 | rm test.ini |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 299 | |
| 300 | report_results |