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