| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
 | 3 | # Tests for DevStack INI functions | 
 | 4 |  | 
 | 5 | TOP=$(cd $(dirname "$0")/.. && pwd) | 
 | 6 |  | 
 | 7 | # Import common functions | 
 | 8 | source $TOP/functions | 
 | 9 |  | 
 | 10 |  | 
 | 11 | echo "Testing INI functions" | 
 | 12 |  | 
 | 13 | cat >test.ini <<EOF | 
 | 14 | [default] | 
 | 15 | # comment an option | 
 | 16 | #log_file=./log.conf | 
 | 17 | log_file=/etc/log.conf | 
 | 18 | handlers=do not disturb | 
 | 19 |  | 
 | 20 | [aaa] | 
 | 21 | # the commented option should not change | 
 | 22 | #handlers=cc,dd | 
 | 23 | handlers = aa, bb | 
 | 24 |  | 
 | 25 | [bbb] | 
 | 26 | handlers=ee,ff | 
 | 27 |  | 
 | 28 | [ ccc ] | 
 | 29 | spaces  =  yes | 
 | 30 |  | 
 | 31 | [ddd] | 
 | 32 | empty = | 
 | 33 |  | 
 | 34 | [eee] | 
 | 35 | multi = foo1 | 
 | 36 | multi = foo2 | 
 | 37 | EOF | 
 | 38 |  | 
 | 39 | # Test with missing arguments | 
 | 40 |  | 
 | 41 | BEFORE=$(cat test.ini) | 
 | 42 |  | 
 | 43 | echo -n "iniset: test missing attribute argument: " | 
 | 44 | iniset test.ini aaa | 
 | 45 | NO_ATTRIBUTE=$(cat test.ini) | 
 | 46 | if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then | 
 | 47 |     echo "OK" | 
 | 48 | else | 
 | 49 |     echo "failed" | 
 | 50 | fi | 
 | 51 |  | 
 | 52 | echo -n "iniset: test missing section argument: " | 
 | 53 | iniset test.ini | 
 | 54 | NO_SECTION=$(cat test.ini) | 
 | 55 | if [[ "$BEFORE" == "$NO_SECTION" ]]; then | 
 | 56 |     echo "OK" | 
 | 57 | else | 
 | 58 |     echo "failed" | 
 | 59 | fi | 
 | 60 |  | 
 | 61 | # Test with spaces | 
 | 62 |  | 
 | 63 | VAL=$(iniget test.ini aaa handlers) | 
 | 64 | if [[ "$VAL" == "aa, bb" ]]; then | 
 | 65 |     echo "OK: $VAL" | 
 | 66 | else | 
 | 67 |     echo "iniget failed: $VAL" | 
 | 68 | fi | 
 | 69 |  | 
 | 70 | iniset test.ini aaa handlers "11, 22" | 
 | 71 |  | 
 | 72 | VAL=$(iniget test.ini aaa handlers) | 
 | 73 | if [[ "$VAL" == "11, 22" ]]; then | 
 | 74 |     echo "OK: $VAL" | 
 | 75 | else | 
 | 76 |     echo "iniget failed: $VAL" | 
 | 77 | fi | 
 | 78 |  | 
 | 79 | # Test with spaces in section header | 
 | 80 |  | 
 | 81 | VAL=$(iniget test.ini " ccc " spaces) | 
 | 82 | if [[ "$VAL" == "yes" ]]; then | 
 | 83 |     echo "OK: $VAL" | 
 | 84 | else | 
 | 85 |     echo "iniget failed: $VAL" | 
 | 86 | fi | 
 | 87 |  | 
 | 88 | iniset test.ini "b b" opt_ion 42 | 
 | 89 |  | 
 | 90 | VAL=$(iniget test.ini "b b" opt_ion) | 
 | 91 | if [[ "$VAL" == "42" ]]; then | 
 | 92 |     echo "OK: $VAL" | 
 | 93 | else | 
 | 94 |     echo "iniget failed: $VAL" | 
 | 95 | fi | 
 | 96 |  | 
 | 97 | # Test without spaces, end of file | 
 | 98 |  | 
 | 99 | VAL=$(iniget test.ini bbb handlers) | 
 | 100 | if [[ "$VAL" == "ee,ff" ]]; then | 
 | 101 |     echo "OK: $VAL" | 
 | 102 | else | 
 | 103 |     echo "iniget failed: $VAL" | 
 | 104 | fi | 
 | 105 |  | 
 | 106 | iniset test.ini bbb handlers "33,44" | 
 | 107 |  | 
 | 108 | VAL=$(iniget test.ini bbb handlers) | 
 | 109 | if [[ "$VAL" == "33,44" ]]; then | 
 | 110 |     echo "OK: $VAL" | 
 | 111 | else | 
 | 112 |     echo "iniget failed: $VAL" | 
 | 113 | fi | 
 | 114 |  | 
 | 115 | # test empty option | 
 | 116 | if ini_has_option test.ini ddd empty; then | 
 | 117 |     echo "OK: ddd.empty present" | 
 | 118 | else | 
 | 119 |     echo "ini_has_option failed: ddd.empty not found" | 
 | 120 | fi | 
 | 121 |  | 
 | 122 | # test non-empty option | 
 | 123 | if ini_has_option test.ini bbb handlers; then | 
 | 124 |     echo "OK: bbb.handlers present" | 
 | 125 | else | 
 | 126 |     echo "ini_has_option failed: bbb.handlers not found" | 
 | 127 | fi | 
 | 128 |  | 
 | 129 | # test changing empty option | 
 | 130 | iniset test.ini ddd empty "42" | 
 | 131 |  | 
 | 132 | VAL=$(iniget test.ini ddd empty) | 
 | 133 | if [[ "$VAL" == "42" ]]; then | 
 | 134 |     echo "OK: $VAL" | 
 | 135 | else | 
 | 136 |     echo "iniget failed: $VAL" | 
 | 137 | fi | 
 | 138 |  | 
| Andrea Frittoli | cd7d956 | 2013-12-05 08:09:12 +0000 | [diff] [blame] | 139 | # test pipe in option | 
 | 140 | iniset test.ini aaa handlers "a|b" | 
 | 141 |  | 
 | 142 | VAL=$(iniget test.ini aaa handlers) | 
 | 143 | if [[ "$VAL" == "a|b" ]]; then | 
 | 144 |     echo "OK: $VAL" | 
 | 145 | else | 
 | 146 |     echo "iniget failed: $VAL" | 
 | 147 | fi | 
 | 148 |  | 
 | 149 | # test space in option | 
 | 150 | iniset test.ini aaa handlers "a b" | 
 | 151 |  | 
 | 152 | VAL="$(iniget test.ini aaa handlers)" | 
 | 153 | if [[ "$VAL" == "a b" ]]; then | 
 | 154 |     echo "OK: $VAL" | 
 | 155 | else | 
 | 156 |     echo "iniget failed: $VAL" | 
 | 157 | fi | 
 | 158 |  | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 159 | # Test section not exist | 
 | 160 |  | 
 | 161 | VAL=$(iniget test.ini zzz handlers) | 
 | 162 | if [[ -z "$VAL" ]]; then | 
 | 163 |     echo "OK: zzz not present" | 
 | 164 | else | 
 | 165 |     echo "iniget failed: $VAL" | 
 | 166 | fi | 
 | 167 |  | 
 | 168 | iniset test.ini zzz handlers "999" | 
 | 169 |  | 
 | 170 | VAL=$(iniget test.ini zzz handlers) | 
 | 171 | if [[ -n "$VAL" ]]; then | 
 | 172 |     echo "OK: zzz not present" | 
 | 173 | else | 
 | 174 |     echo "iniget failed: $VAL" | 
 | 175 | fi | 
 | 176 |  | 
 | 177 | # Test option not exist | 
 | 178 |  | 
 | 179 | VAL=$(iniget test.ini aaa debug) | 
 | 180 | if [[ -z "$VAL" ]]; then | 
 | 181 |     echo "OK aaa.debug not present" | 
 | 182 | else | 
 | 183 |     echo "iniget failed: $VAL" | 
 | 184 | fi | 
 | 185 |  | 
 | 186 | if ! ini_has_option test.ini aaa debug; then | 
 | 187 |     echo "OK aaa.debug not present" | 
 | 188 | else | 
 | 189 |     echo "ini_has_option failed: aaa.debug" | 
 | 190 | fi | 
 | 191 |  | 
 | 192 | iniset test.ini aaa debug "999" | 
 | 193 |  | 
 | 194 | VAL=$(iniget test.ini aaa debug) | 
 | 195 | if [[ -n "$VAL" ]]; then | 
 | 196 |     echo "OK aaa.debug present" | 
 | 197 | else | 
 | 198 |     echo "iniget failed: $VAL" | 
 | 199 | fi | 
 | 200 |  | 
 | 201 | # Test comments | 
 | 202 |  | 
 | 203 | inicomment test.ini aaa handlers | 
 | 204 |  | 
 | 205 | VAL=$(iniget test.ini aaa handlers) | 
 | 206 | if [[ -z "$VAL" ]]; then | 
 | 207 |     echo "OK" | 
 | 208 | else | 
 | 209 |     echo "inicomment failed: $VAL" | 
 | 210 | fi | 
 | 211 |  | 
 | 212 | # Test multiple line iniset/iniget | 
 | 213 | iniset_multiline test.ini eee multi bar1 bar2 | 
 | 214 |  | 
 | 215 | VAL=$(iniget_multiline test.ini eee multi) | 
 | 216 | if [[ "$VAL" == "bar1 bar2" ]]; then | 
 | 217 |     echo "OK: iniset_multiline" | 
 | 218 | else | 
 | 219 |     echo "iniset_multiline failed: $VAL" | 
 | 220 | fi | 
 | 221 |  | 
 | 222 | # Test iniadd with exiting values | 
 | 223 | iniadd test.ini eee multi bar3 | 
 | 224 | VAL=$(iniget_multiline test.ini eee multi) | 
 | 225 | if [[ "$VAL" == "bar1 bar2 bar3" ]]; then | 
 | 226 |     echo "OK: iniadd" | 
 | 227 | else | 
 | 228 |     echo "iniadd failed: $VAL" | 
 | 229 | fi | 
 | 230 |  | 
 | 231 | # Test iniadd with non-exiting values | 
 | 232 | iniadd test.ini eee non-multi foobar1 foobar2 | 
 | 233 | VAL=$(iniget_multiline test.ini eee non-multi) | 
 | 234 | if [[ "$VAL" == "foobar1 foobar2" ]]; then | 
 | 235 |     echo "OK: iniadd with non-exiting value" | 
 | 236 | else | 
 | 237 |     echo "iniadd with non-exsting failed: $VAL" | 
 | 238 | fi | 
 | 239 |  | 
 | 240 | rm test.ini |