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 |
| 94 | EOF |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 95 | |
| 96 | echo -n "get_meta_section_files: test0 doesn't exist: " |
| 97 | VAL=$(get_meta_section_files test.conf test0) |
| 98 | check_result "$VAL" "" |
| 99 | |
| 100 | echo -n "get_meta_section_files: test1 3 files: " |
| 101 | VAL=$(get_meta_section_files test.conf test1) |
| 102 | EXPECT_VAL="test1a.conf |
| 103 | test1b.conf |
| 104 | test1c.conf" |
| 105 | check_result "$VAL" "$EXPECT_VAL" |
| 106 | |
| 107 | echo -n "get_meta_section_files: test2 1 file: " |
| 108 | VAL=$(get_meta_section_files test.conf test2) |
| 109 | EXPECT_VAL="test2a.conf" |
| 110 | check_result "$VAL" "$EXPECT_VAL" |
| 111 | |
| 112 | |
| 113 | # Get a section from a group that doesn't exist |
| 114 | echo -n "get_meta_section: test0 doesn't exist: " |
| 115 | VAL=$(get_meta_section test.conf test0 test0.conf) |
| 116 | check_result "$VAL" "" |
| 117 | |
| 118 | # Get a single section from a group with multiple files |
| 119 | echo -n "get_meta_section: test1c single section: " |
| 120 | VAL=$(get_meta_section test.conf test1 test1c.conf) |
| 121 | check_result "$VAL" "$TEST_1C_ADD" |
| 122 | |
| 123 | # Get a single section from a group with a single file |
| 124 | echo -n "get_meta_section: test2a single section: " |
| 125 | VAL=$(get_meta_section test.conf test2 test2a.conf) |
| 126 | EXPECT_VAL="[ddd] |
| 127 | # new comment |
| 128 | type=new |
| 129 | additional=true" |
| 130 | check_result "$VAL" "$EXPECT_VAL" |
| 131 | |
| 132 | # Get a single section that doesn't exist from a group |
| 133 | echo -n "get_meta_section: test2z.conf not in test2: " |
| 134 | VAL=$(get_meta_section test.conf test2 test2z.conf) |
| 135 | check_result "$VAL" "" |
| 136 | |
| 137 | # Get a section from a conf file that doesn't exist |
| 138 | echo -n "get_meta_section: nofile doesn't exist: " |
| 139 | VAL=$(get_meta_section nofile.ini test1) |
| 140 | check_result "$VAL" "" |
| 141 | |
| 142 | echo -n "get_meta_section: nofile doesn't exist: " |
| 143 | VAL=$(get_meta_section nofile.ini test0 test0.conf) |
| 144 | check_result "$VAL" "" |
| 145 | |
| 146 | echo -n "merge_config_file test1c exists: " |
| 147 | create_test1c |
| 148 | merge_config_file test.conf test1 test1c.conf |
| 149 | VAL=$(cat test1c.conf) |
| 150 | # iniset adds values immediately under the section header |
| 151 | EXPECT_VAL="[eee] |
| 152 | multi = foo2 |
| 153 | # original comment |
| 154 | type=new" |
| 155 | check_result "$VAL" "$EXPECT_VAL" |
| 156 | |
| 157 | echo -n "merge_config_file test2a exists: " |
| 158 | create_test2a |
| 159 | merge_config_file test.conf test2 test2a.conf |
| 160 | VAL=$(cat test2a.conf) |
| 161 | # iniset adds values immediately under the section header |
| 162 | EXPECT_VAL="[ddd] |
| 163 | additional = true |
| 164 | # original comment |
| 165 | type=new" |
| 166 | check_result "$VAL" "$EXPECT_VAL" |
| 167 | |
| 168 | echo -n "merge_config_file test2a not exist: " |
| 169 | rm test2a.conf |
| 170 | merge_config_file test.conf test2 test2a.conf |
| 171 | VAL=$(cat test2a.conf) |
| 172 | # iniset adds a blank line if it creates the file... |
| 173 | EXPECT_VAL=" |
| 174 | [ddd] |
| 175 | additional = true |
| 176 | type = new" |
| 177 | check_result "$VAL" "$EXPECT_VAL" |
| 178 | |
| 179 | echo -n "merge_config_group test2: " |
| 180 | rm test2a.conf |
| 181 | merge_config_group test.conf test2 |
| 182 | VAL=$(cat test2a.conf) |
| 183 | # iniset adds a blank line if it creates the file... |
| 184 | EXPECT_VAL=" |
| 185 | [ddd] |
| 186 | additional = true |
| 187 | type = new" |
| 188 | check_result "$VAL" "$EXPECT_VAL" |
| 189 | |
| 190 | echo -n "merge_config_group test2 no conf file: " |
| 191 | rm test2a.conf |
| 192 | merge_config_group x-test.conf test2 |
| 193 | if [[ ! -r test2a.conf ]]; then |
| 194 | echo "OK" |
| 195 | else |
| 196 | echo "failed: $VAL != $EXPECT_VAL" |
| 197 | fi |
| 198 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 199 | echo -n "merge_config_file test-space: " |
| 200 | rm -f test-space.conf |
| 201 | merge_config_file test.conf test3 test-space.conf |
| 202 | VAL=$(cat test-space.conf) |
| 203 | # iniset adds a blank line if it creates the file... |
| 204 | EXPECT_VAL=" |
| 205 | [DEFAULT] |
| 206 | attribute = value" |
| 207 | check_result "$VAL" "$EXPECT_VAL" |
| 208 | |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 209 | echo -n "merge_config_group test4 variable filename: " |
| 210 | setup_test4 |
| 211 | merge_config_group test.conf test4 |
| 212 | VAL=$(cat test-etc/test4.conf) |
| 213 | EXPECT_VAL="[fff] |
| 214 | # original comment |
| 215 | type=new" |
| 216 | check_result "$VAL" "$EXPECT_VAL" |
| 217 | |
| 218 | echo -n "merge_config_group test4 variable filename (not exist): " |
| 219 | setup_test4 |
| 220 | rm test-etc/test4.conf |
| 221 | merge_config_group test.conf test4 |
| 222 | VAL=$(cat test-etc/test4.conf) |
| 223 | EXPECT_VAL=" |
| 224 | [fff] |
| 225 | type = new" |
| 226 | check_result "$VAL" "$EXPECT_VAL" |
| 227 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 228 | rm -f test.conf test1c.conf test2a.conf test-space.conf |
Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 229 | rm -rf test-etc |