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