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 | |
| 45 | cat >test.conf <<EOF |
| 46 | [[test1|test1a.conf]] |
| 47 | [default] |
| 48 | # comment an option |
| 49 | #log_file=./log.conf |
| 50 | log_file=/etc/log.conf |
| 51 | handlers=do not disturb |
| 52 | |
| 53 | [aaa] |
| 54 | # the commented option should not change |
| 55 | #handlers=cc,dd |
| 56 | handlers = aa, bb |
| 57 | |
| 58 | [[test1|test1b.conf]] |
| 59 | [bbb] |
| 60 | handlers=ee,ff |
| 61 | |
| 62 | [ ccc ] |
| 63 | spaces = yes |
| 64 | |
| 65 | [[test2|test2a.conf]] |
| 66 | [ddd] |
| 67 | # new comment |
| 68 | type=new |
| 69 | additional=true |
| 70 | |
| 71 | [[test1|test1c.conf]] |
| 72 | $TEST_1C_ADD |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 73 | |
| 74 | [[test3|test-space.conf]] |
| 75 | [DEFAULT] |
| 76 | attribute=value |
| 77 | |
| 78 | # the above line has a single space |
Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 79 | EOF |
| 80 | |
| 81 | |
| 82 | echo -n "get_meta_section_files: test0 doesn't exist: " |
| 83 | VAL=$(get_meta_section_files test.conf test0) |
| 84 | check_result "$VAL" "" |
| 85 | |
| 86 | echo -n "get_meta_section_files: test1 3 files: " |
| 87 | VAL=$(get_meta_section_files test.conf test1) |
| 88 | EXPECT_VAL="test1a.conf |
| 89 | test1b.conf |
| 90 | test1c.conf" |
| 91 | check_result "$VAL" "$EXPECT_VAL" |
| 92 | |
| 93 | echo -n "get_meta_section_files: test2 1 file: " |
| 94 | VAL=$(get_meta_section_files test.conf test2) |
| 95 | EXPECT_VAL="test2a.conf" |
| 96 | check_result "$VAL" "$EXPECT_VAL" |
| 97 | |
| 98 | |
| 99 | # Get a section from a group that doesn't exist |
| 100 | echo -n "get_meta_section: test0 doesn't exist: " |
| 101 | VAL=$(get_meta_section test.conf test0 test0.conf) |
| 102 | check_result "$VAL" "" |
| 103 | |
| 104 | # Get a single section from a group with multiple files |
| 105 | echo -n "get_meta_section: test1c single section: " |
| 106 | VAL=$(get_meta_section test.conf test1 test1c.conf) |
| 107 | check_result "$VAL" "$TEST_1C_ADD" |
| 108 | |
| 109 | # Get a single section from a group with a single file |
| 110 | echo -n "get_meta_section: test2a single section: " |
| 111 | VAL=$(get_meta_section test.conf test2 test2a.conf) |
| 112 | EXPECT_VAL="[ddd] |
| 113 | # new comment |
| 114 | type=new |
| 115 | additional=true" |
| 116 | check_result "$VAL" "$EXPECT_VAL" |
| 117 | |
| 118 | # Get a single section that doesn't exist from a group |
| 119 | echo -n "get_meta_section: test2z.conf not in test2: " |
| 120 | VAL=$(get_meta_section test.conf test2 test2z.conf) |
| 121 | check_result "$VAL" "" |
| 122 | |
| 123 | # Get a section from a conf file that doesn't exist |
| 124 | echo -n "get_meta_section: nofile doesn't exist: " |
| 125 | VAL=$(get_meta_section nofile.ini test1) |
| 126 | check_result "$VAL" "" |
| 127 | |
| 128 | echo -n "get_meta_section: nofile doesn't exist: " |
| 129 | VAL=$(get_meta_section nofile.ini test0 test0.conf) |
| 130 | check_result "$VAL" "" |
| 131 | |
| 132 | echo -n "merge_config_file test1c exists: " |
| 133 | create_test1c |
| 134 | merge_config_file test.conf test1 test1c.conf |
| 135 | VAL=$(cat test1c.conf) |
| 136 | # iniset adds values immediately under the section header |
| 137 | EXPECT_VAL="[eee] |
| 138 | multi = foo2 |
| 139 | # original comment |
| 140 | type=new" |
| 141 | check_result "$VAL" "$EXPECT_VAL" |
| 142 | |
| 143 | echo -n "merge_config_file test2a exists: " |
| 144 | create_test2a |
| 145 | merge_config_file test.conf test2 test2a.conf |
| 146 | VAL=$(cat test2a.conf) |
| 147 | # iniset adds values immediately under the section header |
| 148 | EXPECT_VAL="[ddd] |
| 149 | additional = true |
| 150 | # original comment |
| 151 | type=new" |
| 152 | check_result "$VAL" "$EXPECT_VAL" |
| 153 | |
| 154 | echo -n "merge_config_file test2a not exist: " |
| 155 | rm test2a.conf |
| 156 | merge_config_file test.conf test2 test2a.conf |
| 157 | VAL=$(cat test2a.conf) |
| 158 | # iniset adds a blank line if it creates the file... |
| 159 | EXPECT_VAL=" |
| 160 | [ddd] |
| 161 | additional = true |
| 162 | type = new" |
| 163 | check_result "$VAL" "$EXPECT_VAL" |
| 164 | |
| 165 | echo -n "merge_config_group test2: " |
| 166 | rm test2a.conf |
| 167 | merge_config_group test.conf test2 |
| 168 | VAL=$(cat test2a.conf) |
| 169 | # iniset adds a blank line if it creates the file... |
| 170 | EXPECT_VAL=" |
| 171 | [ddd] |
| 172 | additional = true |
| 173 | type = new" |
| 174 | check_result "$VAL" "$EXPECT_VAL" |
| 175 | |
| 176 | echo -n "merge_config_group test2 no conf file: " |
| 177 | rm test2a.conf |
| 178 | merge_config_group x-test.conf test2 |
| 179 | if [[ ! -r test2a.conf ]]; then |
| 180 | echo "OK" |
| 181 | else |
| 182 | echo "failed: $VAL != $EXPECT_VAL" |
| 183 | fi |
| 184 | |
Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 185 | echo -n "merge_config_file test-space: " |
| 186 | rm -f test-space.conf |
| 187 | merge_config_file test.conf test3 test-space.conf |
| 188 | VAL=$(cat test-space.conf) |
| 189 | # iniset adds a blank line if it creates the file... |
| 190 | EXPECT_VAL=" |
| 191 | [DEFAULT] |
| 192 | attribute = value" |
| 193 | check_result "$VAL" "$EXPECT_VAL" |
| 194 | |
| 195 | rm -f test.conf test1c.conf test2a.conf test-space.conf |