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