| 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" | 
 | 15 | function check_result() { | 
 | 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 |  | 
 | 29 | function create_test1c() { | 
 | 30 |     cat >test1c.conf <<EOF | 
 | 31 | [eee] | 
 | 32 | # original comment | 
 | 33 | type=original | 
 | 34 | EOF | 
 | 35 | } | 
 | 36 |  | 
 | 37 | function create_test2a() { | 
 | 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 | 
 | 73 | EOF | 
 | 74 |  | 
 | 75 |  | 
 | 76 | echo -n "get_meta_section_files: test0 doesn't exist: " | 
 | 77 | VAL=$(get_meta_section_files test.conf test0) | 
 | 78 | check_result "$VAL" "" | 
 | 79 |  | 
 | 80 | echo -n "get_meta_section_files: test1 3 files: " | 
 | 81 | VAL=$(get_meta_section_files test.conf test1) | 
 | 82 | EXPECT_VAL="test1a.conf | 
 | 83 | test1b.conf | 
 | 84 | test1c.conf" | 
 | 85 | check_result "$VAL" "$EXPECT_VAL" | 
 | 86 |  | 
 | 87 | echo -n "get_meta_section_files: test2 1 file: " | 
 | 88 | VAL=$(get_meta_section_files test.conf test2) | 
 | 89 | EXPECT_VAL="test2a.conf" | 
 | 90 | check_result "$VAL" "$EXPECT_VAL" | 
 | 91 |  | 
 | 92 |  | 
 | 93 | # Get a section from a group that doesn't exist | 
 | 94 | echo -n "get_meta_section: test0 doesn't exist: " | 
 | 95 | VAL=$(get_meta_section test.conf test0 test0.conf) | 
 | 96 | check_result "$VAL" "" | 
 | 97 |  | 
 | 98 | # Get a single section from a group with multiple files | 
 | 99 | echo -n "get_meta_section: test1c single section: " | 
 | 100 | VAL=$(get_meta_section test.conf test1 test1c.conf) | 
 | 101 | check_result "$VAL" "$TEST_1C_ADD" | 
 | 102 |  | 
 | 103 | # Get a single section from a group with a single file | 
 | 104 | echo -n "get_meta_section: test2a single section: " | 
 | 105 | VAL=$(get_meta_section test.conf test2 test2a.conf) | 
 | 106 | EXPECT_VAL="[ddd] | 
 | 107 | # new comment | 
 | 108 | type=new | 
 | 109 | additional=true" | 
 | 110 | check_result "$VAL" "$EXPECT_VAL" | 
 | 111 |  | 
 | 112 | # Get a single section that doesn't exist from a group | 
 | 113 | echo -n "get_meta_section: test2z.conf not in test2: " | 
 | 114 | VAL=$(get_meta_section test.conf test2 test2z.conf) | 
 | 115 | check_result "$VAL" "" | 
 | 116 |  | 
 | 117 | # Get a section from a conf file that doesn't exist | 
 | 118 | echo -n "get_meta_section: nofile doesn't exist: " | 
 | 119 | VAL=$(get_meta_section nofile.ini test1) | 
 | 120 | check_result "$VAL" "" | 
 | 121 |  | 
 | 122 | echo -n "get_meta_section: nofile doesn't exist: " | 
 | 123 | VAL=$(get_meta_section nofile.ini test0 test0.conf) | 
 | 124 | check_result "$VAL" "" | 
 | 125 |  | 
 | 126 | echo -n "merge_config_file test1c exists: " | 
 | 127 | create_test1c | 
 | 128 | merge_config_file test.conf test1 test1c.conf | 
 | 129 | VAL=$(cat test1c.conf) | 
 | 130 | # iniset adds values immediately under the section header | 
 | 131 | EXPECT_VAL="[eee] | 
 | 132 | multi = foo2 | 
 | 133 | # original comment | 
 | 134 | type=new" | 
 | 135 | check_result "$VAL" "$EXPECT_VAL" | 
 | 136 |  | 
 | 137 | echo -n "merge_config_file test2a exists: " | 
 | 138 | create_test2a | 
 | 139 | merge_config_file test.conf test2 test2a.conf | 
 | 140 | VAL=$(cat test2a.conf) | 
 | 141 | # iniset adds values immediately under the section header | 
 | 142 | EXPECT_VAL="[ddd] | 
 | 143 | additional = true | 
 | 144 | # original comment | 
 | 145 | type=new" | 
 | 146 | check_result "$VAL" "$EXPECT_VAL" | 
 | 147 |  | 
 | 148 | echo -n "merge_config_file test2a not exist: " | 
 | 149 | rm test2a.conf | 
 | 150 | merge_config_file test.conf test2 test2a.conf | 
 | 151 | VAL=$(cat test2a.conf) | 
 | 152 | # iniset adds a blank line if it creates the file... | 
 | 153 | EXPECT_VAL=" | 
 | 154 | [ddd] | 
 | 155 | additional = true | 
 | 156 | type = new" | 
 | 157 | check_result "$VAL" "$EXPECT_VAL" | 
 | 158 |  | 
 | 159 | echo -n "merge_config_group test2: " | 
 | 160 | rm test2a.conf | 
 | 161 | merge_config_group test.conf test2 | 
 | 162 | VAL=$(cat test2a.conf) | 
 | 163 | # iniset adds a blank line if it creates the file... | 
 | 164 | EXPECT_VAL=" | 
 | 165 | [ddd] | 
 | 166 | additional = true | 
 | 167 | type = new" | 
 | 168 | check_result "$VAL" "$EXPECT_VAL" | 
 | 169 |  | 
 | 170 | echo -n "merge_config_group test2 no conf file: " | 
 | 171 | rm test2a.conf | 
 | 172 | merge_config_group x-test.conf test2 | 
 | 173 | if [[ ! -r test2a.conf ]]; then | 
 | 174 |     echo "OK" | 
 | 175 | else | 
 | 176 |     echo "failed: $VAL != $EXPECT_VAL" | 
 | 177 | fi | 
 | 178 |  | 
 | 179 | rm -f test.conf test1c.conf test2a.conf |