Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Tests for DevStack meta-config functions |
| 4 | |
| 5 | TOP=$(cd $(dirname "$0")/.. && pwd) |
| 6 | |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 7 | # Import config functions |
Dean Troyer | bf2ad70 | 2015-03-09 15:16:10 -0500 | [diff] [blame] | 8 | source $TOP/inc/ini-config |
| 9 | source $TOP/inc/meta-config |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 10 | |
Ian Wienand | fa3e841 | 2015-04-17 11:53:40 +1000 | [diff] [blame] | 11 | set -e |
| 12 | |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 13 | # check_result() tests and reports the result values |
| 14 | # check_result "actual" "expected" |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 15 | function check_result { |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 16 | local actual=$1 |
| 17 | local expected=$2 |
| 18 | if [[ "$actual" == "$expected" ]]; then |
| 19 | echo "OK" |
| 20 | else |
| 21 | echo -e "failed: $actual != $expected\n" |
Ian Wienand | fcdca05 | 2015-04-17 13:02:49 +1000 | [diff] [blame] | 22 | exit 1 |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 23 | fi |
| 24 | } |
| 25 | |
Thomas Morin | 85f42f6 | 2015-09-01 10:33:10 +0200 | [diff] [blame] | 26 | # mock function-common:die so that it does not |
Swapnil Kulkarni (coolsvap) | 7f0be4f | 2015-11-20 10:52:59 +0530 | [diff] [blame] | 27 | # interrupt our test script |
Thomas Morin | 85f42f6 | 2015-09-01 10:33:10 +0200 | [diff] [blame] | 28 | function die { |
| 29 | exit -1 |
| 30 | } |
| 31 | |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 32 | TEST_1C_ADD="[eee] |
| 33 | type=new |
| 34 | multi = foo2" |
| 35 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 36 | function create_test1c { |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 37 | cat >test1c.conf <<EOF |
| 38 | [eee] |
| 39 | # original comment |
| 40 | type=original |
| 41 | EOF |
| 42 | } |
| 43 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 44 | function create_test2a { |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 45 | cat >test2a.conf <<EOF |
| 46 | [ddd] |
| 47 | # original comment |
| 48 | type=original |
| 49 | EOF |
| 50 | } |
| 51 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 52 | function setup_test4 { |
| 53 | mkdir -p test-etc |
| 54 | cat >test-etc/test4.conf <<EOF |
| 55 | [fff] |
| 56 | # original comment |
| 57 | type=original |
| 58 | EOF |
| 59 | TEST4_DIR="test-etc" |
| 60 | TEST4_FILE="test4.conf" |
| 61 | } |
| 62 | |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 63 | cat >test.conf <<EOF |
| 64 | [[test1|test1a.conf]] |
| 65 | [default] |
| 66 | # comment an option |
| 67 | #log_file=./log.conf |
| 68 | log_file=/etc/log.conf |
| 69 | handlers=do not disturb |
| 70 | |
| 71 | [aaa] |
| 72 | # the commented option should not change |
| 73 | #handlers=cc,dd |
| 74 | handlers = aa, bb |
| 75 | |
| 76 | [[test1|test1b.conf]] |
| 77 | [bbb] |
| 78 | handlers=ee,ff |
| 79 | |
| 80 | [ ccc ] |
| 81 | spaces = yes |
| 82 | |
| 83 | [[test2|test2a.conf]] |
| 84 | [ddd] |
| 85 | # new comment |
| 86 | type=new |
| 87 | additional=true |
| 88 | |
| 89 | [[test1|test1c.conf]] |
| 90 | $TEST_1C_ADD |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 91 | |
| 92 | [[test3|test-space.conf]] |
| 93 | [DEFAULT] |
| 94 | attribute=value |
| 95 | |
| 96 | # the above line has a single space |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 97 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 98 | [[test4|\$TEST4_DIR/\$TEST4_FILE]] |
| 99 | [fff] |
| 100 | type=new |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 101 | |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 102 | [[test-env|test-env.conf]] |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 103 | [foo] |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 104 | foo=\${FOO_BAR_BAZ} |
Fergal Mc Carthy | cc87c28 | 2014-10-09 16:16:42 -0400 | [diff] [blame] | 105 | |
| 106 | [[test5|test-equals.conf]] |
| 107 | [DEFAULT] |
| 108 | drivers = driver=python.import.path.Driver |
| 109 | |
| 110 | [[test6|test-strip.conf]] |
| 111 | [DEFAULT] |
| 112 | # next line has trailing space |
Kevin Benton | e0d6a46 | 2014-10-14 04:54:42 -0700 | [diff] [blame] | 113 | attr = strip_trailing_space |
| 114 | |
| 115 | [[test7|test-colon.conf]] |
| 116 | [DEFAULT] |
| 117 | servers=10.11.12.13:80 |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 118 | |
Thomas Morin | 85f42f6 | 2015-09-01 10:33:10 +0200 | [diff] [blame] | 119 | [[test8|/permission-denied.conf]] |
| 120 | foo=bar |
| 121 | |
| 122 | [[test9|\$UNDEF]] |
| 123 | foo=bar |
| 124 | |
| 125 | [[test10|does-not-exist-dir/test.conf]] |
| 126 | foo=bar |
| 127 | |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 128 | [[test-multi-sections|test-multi-sections.conf]] |
| 129 | [sec-1] |
| 130 | cfg_item1 = abcd |
| 131 | cfg_item2 = efgh |
| 132 | |
| 133 | [sec-2] |
| 134 | cfg_item1 = abcd |
| 135 | cfg_item3 = /1/2/3/4:5 |
| 136 | cfg_item4 = end |
| 137 | |
| 138 | [sec-3] |
| 139 | cfg_item5 = 5555 |
| 140 | cfg_item6 = 6666 |
| 141 | cfg_item5 = 5555another |
| 142 | |
| 143 | [[test-multiline|test-multiline.conf]] |
| 144 | [multi] |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 145 | cfg_item1 = ab:cd:ef:gh |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 146 | cfg_item1 = abcd |
| 147 | cfg_item2 = efgh |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 148 | cfg_item2 = \${FOO_BAR_BAZ} |
| 149 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 150 | EOF |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 151 | |
| 152 | echo -n "get_meta_section_files: test0 doesn't exist: " |
| 153 | VAL=$(get_meta_section_files test.conf test0) |
| 154 | check_result "$VAL" "" |
| 155 | |
| 156 | echo -n "get_meta_section_files: test1 3 files: " |
| 157 | VAL=$(get_meta_section_files test.conf test1) |
| 158 | EXPECT_VAL="test1a.conf |
| 159 | test1b.conf |
| 160 | test1c.conf" |
| 161 | check_result "$VAL" "$EXPECT_VAL" |
| 162 | |
| 163 | echo -n "get_meta_section_files: test2 1 file: " |
| 164 | VAL=$(get_meta_section_files test.conf test2) |
| 165 | EXPECT_VAL="test2a.conf" |
| 166 | check_result "$VAL" "$EXPECT_VAL" |
| 167 | |
| 168 | |
| 169 | # Get a section from a group that doesn't exist |
| 170 | echo -n "get_meta_section: test0 doesn't exist: " |
| 171 | VAL=$(get_meta_section test.conf test0 test0.conf) |
| 172 | check_result "$VAL" "" |
| 173 | |
| 174 | # Get a single section from a group with multiple files |
| 175 | echo -n "get_meta_section: test1c single section: " |
| 176 | VAL=$(get_meta_section test.conf test1 test1c.conf) |
| 177 | check_result "$VAL" "$TEST_1C_ADD" |
| 178 | |
| 179 | # Get a single section from a group with a single file |
| 180 | echo -n "get_meta_section: test2a single section: " |
| 181 | VAL=$(get_meta_section test.conf test2 test2a.conf) |
| 182 | EXPECT_VAL="[ddd] |
| 183 | # new comment |
| 184 | type=new |
| 185 | additional=true" |
| 186 | check_result "$VAL" "$EXPECT_VAL" |
| 187 | |
| 188 | # Get a single section that doesn't exist from a group |
| 189 | echo -n "get_meta_section: test2z.conf not in test2: " |
| 190 | VAL=$(get_meta_section test.conf test2 test2z.conf) |
| 191 | check_result "$VAL" "" |
| 192 | |
| 193 | # Get a section from a conf file that doesn't exist |
| 194 | echo -n "get_meta_section: nofile doesn't exist: " |
| 195 | VAL=$(get_meta_section nofile.ini test1) |
| 196 | check_result "$VAL" "" |
| 197 | |
| 198 | echo -n "get_meta_section: nofile doesn't exist: " |
| 199 | VAL=$(get_meta_section nofile.ini test0 test0.conf) |
| 200 | check_result "$VAL" "" |
| 201 | |
| 202 | echo -n "merge_config_file test1c exists: " |
| 203 | create_test1c |
| 204 | merge_config_file test.conf test1 test1c.conf |
| 205 | VAL=$(cat test1c.conf) |
| 206 | # iniset adds values immediately under the section header |
| 207 | EXPECT_VAL="[eee] |
| 208 | multi = foo2 |
| 209 | # original comment |
| 210 | type=new" |
| 211 | check_result "$VAL" "$EXPECT_VAL" |
| 212 | |
| 213 | echo -n "merge_config_file test2a exists: " |
| 214 | create_test2a |
| 215 | merge_config_file test.conf test2 test2a.conf |
| 216 | VAL=$(cat test2a.conf) |
| 217 | # iniset adds values immediately under the section header |
| 218 | EXPECT_VAL="[ddd] |
| 219 | additional = true |
| 220 | # original comment |
| 221 | type=new" |
| 222 | check_result "$VAL" "$EXPECT_VAL" |
| 223 | |
| 224 | echo -n "merge_config_file test2a not exist: " |
| 225 | rm test2a.conf |
| 226 | merge_config_file test.conf test2 test2a.conf |
| 227 | VAL=$(cat test2a.conf) |
| 228 | # iniset adds a blank line if it creates the file... |
| 229 | EXPECT_VAL=" |
| 230 | [ddd] |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 231 | type = new |
| 232 | additional = true" |
| 233 | check_result "$VAL" "$EXPECT_VAL" |
| 234 | |
| 235 | echo -n "merge_config_file test-multi-sections: " |
| 236 | rm -f test-multi-sections.conf |
| 237 | merge_config_file test.conf test-multi-sections test-multi-sections.conf |
| 238 | VAL=$(cat test-multi-sections.conf) |
| 239 | EXPECT_VAL=' |
| 240 | [sec-1] |
| 241 | cfg_item1 = abcd |
| 242 | cfg_item2 = efgh |
| 243 | |
| 244 | [sec-2] |
| 245 | cfg_item1 = abcd |
| 246 | cfg_item3 = /1/2/3/4:5 |
| 247 | cfg_item4 = end |
| 248 | |
| 249 | [sec-3] |
| 250 | cfg_item5 = 5555 |
| 251 | cfg_item5 = 5555another |
| 252 | cfg_item6 = 6666' |
| 253 | check_result "$VAL" "$EXPECT_VAL" |
| 254 | |
| 255 | echo -n "merge_config_file test-multiline: " |
| 256 | rm -f test-multiline.conf |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 257 | FOO_BAR_BAZ="foo bar baz" |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 258 | merge_config_file test.conf test-multiline test-multiline.conf |
| 259 | VAL=$(cat test-multiline.conf) |
| 260 | EXPECT_VAL=' |
| 261 | [multi] |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 262 | cfg_item1 = ab:cd:ef:gh |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 263 | cfg_item1 = abcd |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 264 | cfg_item2 = efgh |
| 265 | cfg_item2 = foo bar baz' |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 266 | check_result "$VAL" "$EXPECT_VAL" |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 267 | unset FOO_BAR_BAZ |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 268 | |
| 269 | echo -n "merge_config_group test2: " |
| 270 | rm test2a.conf |
| 271 | merge_config_group test.conf test2 |
| 272 | VAL=$(cat test2a.conf) |
| 273 | # iniset adds a blank line if it creates the file... |
| 274 | EXPECT_VAL=" |
| 275 | [ddd] |
Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 276 | type = new |
| 277 | additional = true" |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 278 | check_result "$VAL" "$EXPECT_VAL" |
| 279 | |
| 280 | echo -n "merge_config_group test2 no conf file: " |
| 281 | rm test2a.conf |
| 282 | merge_config_group x-test.conf test2 |
| 283 | if [[ ! -r test2a.conf ]]; then |
| 284 | echo "OK" |
| 285 | else |
| 286 | echo "failed: $VAL != $EXPECT_VAL" |
| 287 | fi |
| 288 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 289 | echo -n "merge_config_file test-space: " |
| 290 | rm -f test-space.conf |
| 291 | merge_config_file test.conf test3 test-space.conf |
| 292 | VAL=$(cat test-space.conf) |
| 293 | # iniset adds a blank line if it creates the file... |
| 294 | EXPECT_VAL=" |
| 295 | [DEFAULT] |
| 296 | attribute = value" |
| 297 | check_result "$VAL" "$EXPECT_VAL" |
| 298 | |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 299 | echo -n "merge_config_file test-env: " |
| 300 | rm -f test-env.conf |
| 301 | FOO_BAR_BAZ="foo bar baz" |
| 302 | merge_config_file test.conf test-env test-env.conf |
| 303 | VAL=$(cat test-env.conf) |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 304 | EXPECT_VAL=' |
| 305 | [foo] |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 306 | foo = foo bar baz' |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 307 | check_result "$VAL" "$EXPECT_VAL" |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 308 | unset FOO_BAR_BAZ |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 309 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 310 | echo -n "merge_config_group test4 variable filename: " |
| 311 | setup_test4 |
| 312 | merge_config_group test.conf test4 |
| 313 | VAL=$(cat test-etc/test4.conf) |
| 314 | EXPECT_VAL="[fff] |
| 315 | # original comment |
| 316 | type=new" |
| 317 | check_result "$VAL" "$EXPECT_VAL" |
| 318 | |
| 319 | echo -n "merge_config_group test4 variable filename (not exist): " |
| 320 | setup_test4 |
| 321 | rm test-etc/test4.conf |
| 322 | merge_config_group test.conf test4 |
| 323 | VAL=$(cat test-etc/test4.conf) |
| 324 | EXPECT_VAL=" |
| 325 | [fff] |
| 326 | type = new" |
| 327 | check_result "$VAL" "$EXPECT_VAL" |
| 328 | |
Fergal Mc Carthy | cc87c28 | 2014-10-09 16:16:42 -0400 | [diff] [blame] | 329 | echo -n "merge_config_file test5 equals in value: " |
| 330 | rm -f test-equals.conf |
| 331 | merge_config_file test.conf test5 test-equals.conf |
| 332 | VAL=$(cat test-equals.conf) |
| 333 | # iniset adds a blank line if it creates the file... |
| 334 | EXPECT_VAL=" |
| 335 | [DEFAULT] |
| 336 | drivers = driver=python.import.path.Driver" |
| 337 | check_result "$VAL" "$EXPECT_VAL" |
| 338 | |
| 339 | echo -n "merge_config_file test6 value stripped: " |
| 340 | rm -f test-strip.conf |
| 341 | merge_config_file test.conf test6 test-strip.conf |
| 342 | VAL=$(cat test-strip.conf) |
| 343 | # iniset adds a blank line if it creates the file... |
| 344 | EXPECT_VAL=" |
| 345 | [DEFAULT] |
| 346 | attr = strip_trailing_space" |
| 347 | check_result "$VAL" "$EXPECT_VAL" |
| 348 | |
Kevin Benton | e0d6a46 | 2014-10-14 04:54:42 -0700 | [diff] [blame] | 349 | echo -n "merge_config_file test7 colon in value: " |
| 350 | rm -f test-colon.conf |
| 351 | merge_config_file test.conf test7 test-colon.conf |
| 352 | VAL=$(cat test-colon.conf) |
| 353 | EXPECT_VAL=" |
| 354 | [DEFAULT] |
| 355 | servers = 10.11.12.13:80" |
| 356 | check_result "$VAL" "$EXPECT_VAL" |
| 357 | |
Thomas Morin | 85f42f6 | 2015-09-01 10:33:10 +0200 | [diff] [blame] | 358 | echo "merge_config_file test8 non-touchable conf file: " |
| 359 | set +e |
| 360 | # function is expected to fail and exit, running it |
| 361 | # in a subprocess to let this script proceed |
| 362 | (merge_config_file test.conf test8 /permission-denied.conf) |
| 363 | VAL=$? |
| 364 | EXPECT_VAL=255 |
| 365 | check_result "$VAL" "$EXPECT_VAL" |
| 366 | set -e |
| 367 | |
| 368 | echo -n "merge_config_group test9 undefined conf file: " |
| 369 | set +e |
| 370 | # function is expected to fail and exit, running it |
| 371 | # in a subprocess to let this script proceed |
| 372 | (merge_config_group test.conf test9) |
| 373 | VAL=$? |
| 374 | EXPECT_VAL=255 |
| 375 | check_result "$VAL" "$EXPECT_VAL" |
| 376 | set -e |
| 377 | |
| 378 | echo -n "merge_config_group test10 not directory: " |
| 379 | set +e |
| 380 | # function is expected to fail and exit, running it |
| 381 | # in a subprocess to let this script proceed |
| 382 | (merge_config_group test.conf test10) |
| 383 | VAL=$? |
| 384 | EXPECT_VAL=255 |
| 385 | check_result "$VAL" "$EXPECT_VAL" |
| 386 | set -e |
| 387 | |
Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 388 | rm -f test.conf test1c.conf test2a.conf \ |
| 389 | test-space.conf test-equals.conf test-strip.conf \ |
| 390 | test-colon.conf test-env.conf test-multiline.conf \ |
| 391 | test-multi-sections.conf |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 392 | rm -rf test-etc |