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 | |
| 7 | # Import common functions |
| 8 | source $TOP/functions |
| 9 | |
| 10 | # Import config functions |
| 11 | source $TOP/lib/config |
| 12 | |
| 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" |
| 22 | fi |
| 23 | } |
| 24 | |
| 25 | TEST_1C_ADD="[eee] |
| 26 | type=new |
| 27 | multi = foo2" |
| 28 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 29 | function create_test1c { |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 30 | cat >test1c.conf <<EOF |
| 31 | [eee] |
| 32 | # original comment |
| 33 | type=original |
| 34 | EOF |
| 35 | } |
| 36 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 37 | function create_test2a { |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 38 | cat >test2a.conf <<EOF |
| 39 | [ddd] |
| 40 | # original comment |
| 41 | type=original |
| 42 | EOF |
| 43 | } |
| 44 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 45 | function setup_test4 { |
| 46 | mkdir -p test-etc |
| 47 | cat >test-etc/test4.conf <<EOF |
| 48 | [fff] |
| 49 | # original comment |
| 50 | type=original |
| 51 | EOF |
| 52 | TEST4_DIR="test-etc" |
| 53 | TEST4_FILE="test4.conf" |
| 54 | } |
| 55 | |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 56 | cat >test.conf <<EOF |
| 57 | [[test1|test1a.conf]] |
| 58 | [default] |
| 59 | # comment an option |
| 60 | #log_file=./log.conf |
| 61 | log_file=/etc/log.conf |
| 62 | handlers=do not disturb |
| 63 | |
| 64 | [aaa] |
| 65 | # the commented option should not change |
| 66 | #handlers=cc,dd |
| 67 | handlers = aa, bb |
| 68 | |
| 69 | [[test1|test1b.conf]] |
| 70 | [bbb] |
| 71 | handlers=ee,ff |
| 72 | |
| 73 | [ ccc ] |
| 74 | spaces = yes |
| 75 | |
| 76 | [[test2|test2a.conf]] |
| 77 | [ddd] |
| 78 | # new comment |
| 79 | type=new |
| 80 | additional=true |
| 81 | |
| 82 | [[test1|test1c.conf]] |
| 83 | $TEST_1C_ADD |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 84 | |
| 85 | [[test3|test-space.conf]] |
| 86 | [DEFAULT] |
| 87 | attribute=value |
| 88 | |
| 89 | # the above line has a single space |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 90 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 91 | [[test4|\$TEST4_DIR/\$TEST4_FILE]] |
| 92 | [fff] |
| 93 | type=new |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 94 | |
| 95 | [[test-quote|test-quote.conf]] |
| 96 | [foo] |
| 97 | foo="foo bar" "baz" |
Fergal Mc Carthy | cc87c28 | 2014-10-09 16:16:42 -0400 | [diff] [blame^] | 98 | |
| 99 | [[test5|test-equals.conf]] |
| 100 | [DEFAULT] |
| 101 | drivers = driver=python.import.path.Driver |
| 102 | |
| 103 | [[test6|test-strip.conf]] |
| 104 | [DEFAULT] |
| 105 | # next line has trailing space |
| 106 | attr = strip_trailing_space |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 107 | EOF |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 108 | |
| 109 | echo -n "get_meta_section_files: test0 doesn't exist: " |
| 110 | VAL=$(get_meta_section_files test.conf test0) |
| 111 | check_result "$VAL" "" |
| 112 | |
| 113 | echo -n "get_meta_section_files: test1 3 files: " |
| 114 | VAL=$(get_meta_section_files test.conf test1) |
| 115 | EXPECT_VAL="test1a.conf |
| 116 | test1b.conf |
| 117 | test1c.conf" |
| 118 | check_result "$VAL" "$EXPECT_VAL" |
| 119 | |
| 120 | echo -n "get_meta_section_files: test2 1 file: " |
| 121 | VAL=$(get_meta_section_files test.conf test2) |
| 122 | EXPECT_VAL="test2a.conf" |
| 123 | check_result "$VAL" "$EXPECT_VAL" |
| 124 | |
| 125 | |
| 126 | # Get a section from a group that doesn't exist |
| 127 | echo -n "get_meta_section: test0 doesn't exist: " |
| 128 | VAL=$(get_meta_section test.conf test0 test0.conf) |
| 129 | check_result "$VAL" "" |
| 130 | |
| 131 | # Get a single section from a group with multiple files |
| 132 | echo -n "get_meta_section: test1c single section: " |
| 133 | VAL=$(get_meta_section test.conf test1 test1c.conf) |
| 134 | check_result "$VAL" "$TEST_1C_ADD" |
| 135 | |
| 136 | # Get a single section from a group with a single file |
| 137 | echo -n "get_meta_section: test2a single section: " |
| 138 | VAL=$(get_meta_section test.conf test2 test2a.conf) |
| 139 | EXPECT_VAL="[ddd] |
| 140 | # new comment |
| 141 | type=new |
| 142 | additional=true" |
| 143 | check_result "$VAL" "$EXPECT_VAL" |
| 144 | |
| 145 | # Get a single section that doesn't exist from a group |
| 146 | echo -n "get_meta_section: test2z.conf not in test2: " |
| 147 | VAL=$(get_meta_section test.conf test2 test2z.conf) |
| 148 | check_result "$VAL" "" |
| 149 | |
| 150 | # Get a section from a conf file that doesn't exist |
| 151 | echo -n "get_meta_section: nofile doesn't exist: " |
| 152 | VAL=$(get_meta_section nofile.ini test1) |
| 153 | check_result "$VAL" "" |
| 154 | |
| 155 | echo -n "get_meta_section: nofile doesn't exist: " |
| 156 | VAL=$(get_meta_section nofile.ini test0 test0.conf) |
| 157 | check_result "$VAL" "" |
| 158 | |
| 159 | echo -n "merge_config_file test1c exists: " |
| 160 | create_test1c |
| 161 | merge_config_file test.conf test1 test1c.conf |
| 162 | VAL=$(cat test1c.conf) |
| 163 | # iniset adds values immediately under the section header |
| 164 | EXPECT_VAL="[eee] |
| 165 | multi = foo2 |
| 166 | # original comment |
| 167 | type=new" |
| 168 | check_result "$VAL" "$EXPECT_VAL" |
| 169 | |
| 170 | echo -n "merge_config_file test2a exists: " |
| 171 | create_test2a |
| 172 | merge_config_file test.conf test2 test2a.conf |
| 173 | VAL=$(cat test2a.conf) |
| 174 | # iniset adds values immediately under the section header |
| 175 | EXPECT_VAL="[ddd] |
| 176 | additional = true |
| 177 | # original comment |
| 178 | type=new" |
| 179 | check_result "$VAL" "$EXPECT_VAL" |
| 180 | |
| 181 | echo -n "merge_config_file test2a not exist: " |
| 182 | rm test2a.conf |
| 183 | merge_config_file test.conf test2 test2a.conf |
| 184 | VAL=$(cat test2a.conf) |
| 185 | # iniset adds a blank line if it creates the file... |
| 186 | EXPECT_VAL=" |
| 187 | [ddd] |
| 188 | additional = true |
| 189 | type = new" |
| 190 | check_result "$VAL" "$EXPECT_VAL" |
| 191 | |
| 192 | echo -n "merge_config_group test2: " |
| 193 | rm test2a.conf |
| 194 | merge_config_group test.conf test2 |
| 195 | VAL=$(cat test2a.conf) |
| 196 | # iniset adds a blank line if it creates the file... |
| 197 | EXPECT_VAL=" |
| 198 | [ddd] |
| 199 | additional = true |
| 200 | type = new" |
| 201 | check_result "$VAL" "$EXPECT_VAL" |
| 202 | |
| 203 | echo -n "merge_config_group test2 no conf file: " |
| 204 | rm test2a.conf |
| 205 | merge_config_group x-test.conf test2 |
| 206 | if [[ ! -r test2a.conf ]]; then |
| 207 | echo "OK" |
| 208 | else |
| 209 | echo "failed: $VAL != $EXPECT_VAL" |
| 210 | fi |
| 211 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 212 | echo -n "merge_config_file test-space: " |
| 213 | rm -f test-space.conf |
| 214 | merge_config_file test.conf test3 test-space.conf |
| 215 | VAL=$(cat test-space.conf) |
| 216 | # iniset adds a blank line if it creates the file... |
| 217 | EXPECT_VAL=" |
| 218 | [DEFAULT] |
| 219 | attribute = value" |
| 220 | check_result "$VAL" "$EXPECT_VAL" |
| 221 | |
Ian Wienand | e2c9fee | 2014-09-26 09:42:11 +1000 | [diff] [blame] | 222 | echo -n "merge_config_file test-quote: " |
| 223 | rm -f test-quote.conf |
| 224 | merge_config_file test.conf test-quote test-quote.conf |
| 225 | VAL=$(cat test-quote.conf) |
| 226 | EXPECT_VAL=' |
| 227 | [foo] |
| 228 | foo = "foo bar" "baz"' |
| 229 | check_result "$VAL" "$EXPECT_VAL" |
| 230 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 231 | echo -n "merge_config_group test4 variable filename: " |
| 232 | setup_test4 |
| 233 | merge_config_group test.conf test4 |
| 234 | VAL=$(cat test-etc/test4.conf) |
| 235 | EXPECT_VAL="[fff] |
| 236 | # original comment |
| 237 | type=new" |
| 238 | check_result "$VAL" "$EXPECT_VAL" |
| 239 | |
| 240 | echo -n "merge_config_group test4 variable filename (not exist): " |
| 241 | setup_test4 |
| 242 | rm test-etc/test4.conf |
| 243 | merge_config_group test.conf test4 |
| 244 | VAL=$(cat test-etc/test4.conf) |
| 245 | EXPECT_VAL=" |
| 246 | [fff] |
| 247 | type = new" |
| 248 | check_result "$VAL" "$EXPECT_VAL" |
| 249 | |
Fergal Mc Carthy | cc87c28 | 2014-10-09 16:16:42 -0400 | [diff] [blame^] | 250 | echo -n "merge_config_file test5 equals in value: " |
| 251 | rm -f test-equals.conf |
| 252 | merge_config_file test.conf test5 test-equals.conf |
| 253 | VAL=$(cat test-equals.conf) |
| 254 | # iniset adds a blank line if it creates the file... |
| 255 | EXPECT_VAL=" |
| 256 | [DEFAULT] |
| 257 | drivers = driver=python.import.path.Driver" |
| 258 | check_result "$VAL" "$EXPECT_VAL" |
| 259 | |
| 260 | echo -n "merge_config_file test6 value stripped: " |
| 261 | rm -f test-strip.conf |
| 262 | merge_config_file test.conf test6 test-strip.conf |
| 263 | VAL=$(cat test-strip.conf) |
| 264 | # iniset adds a blank line if it creates the file... |
| 265 | EXPECT_VAL=" |
| 266 | [DEFAULT] |
| 267 | attr = strip_trailing_space" |
| 268 | check_result "$VAL" "$EXPECT_VAL" |
| 269 | |
| 270 | rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf test-equals.conf test-strip.conf |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 271 | rm -rf test-etc |