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