Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Tests for DevStack functions |
| 4 | |
| 5 | TOP=$(cd $(dirname "$0")/.. && pwd) |
| 6 | |
| 7 | # Import common functions |
| 8 | source $TOP/functions |
| 9 | |
| 10 | # Import configuration |
| 11 | source $TOP/openrc |
| 12 | |
| 13 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 14 | echo "Testing die_if_not_set()" |
| 15 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 16 | bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 17 | if [[ $? != 0 ]]; then |
| 18 | echo "die_if_not_set [X='Y' true] Failed" |
| 19 | else |
| 20 | echo 'OK' |
| 21 | fi |
| 22 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 23 | bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 24 | if [[ $? = 0 ]]; then |
| 25 | echo "die_if_not_set [X='' true] Failed" |
| 26 | fi |
| 27 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 28 | bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 | if [[ $? != 0 ]]; then |
| 30 | echo "die_if_not_set [X='Y' false] Failed" |
| 31 | else |
| 32 | echo 'OK' |
| 33 | fi |
| 34 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 35 | bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 36 | if [[ $? = 0 ]]; then |
| 37 | echo "die_if_not_set [X='' false] Failed" |
| 38 | fi |
| 39 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 40 | |
| 41 | echo "Testing INI functions" |
| 42 | |
| 43 | cat >test.ini <<EOF |
| 44 | [default] |
| 45 | # comment an option |
| 46 | #log_file=./log.conf |
| 47 | log_file=/etc/log.conf |
| 48 | handlers=do not disturb |
| 49 | |
| 50 | [aaa] |
| 51 | # the commented option should not change |
| 52 | #handlers=cc,dd |
| 53 | handlers = aa, bb |
| 54 | |
| 55 | [bbb] |
| 56 | handlers=ee,ff |
Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 57 | |
| 58 | [ ccc ] |
| 59 | spaces = yes |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame^] | 60 | |
| 61 | [ddd] |
| 62 | empty = |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 63 | EOF |
| 64 | |
| 65 | # Test with spaces |
| 66 | |
| 67 | VAL=$(iniget test.ini aaa handlers) |
| 68 | if [[ "$VAL" == "aa, bb" ]]; then |
| 69 | echo "OK: $VAL" |
| 70 | else |
| 71 | echo "iniget failed: $VAL" |
| 72 | fi |
| 73 | |
| 74 | iniset test.ini aaa handlers "11, 22" |
| 75 | |
| 76 | VAL=$(iniget test.ini aaa handlers) |
| 77 | if [[ "$VAL" == "11, 22" ]]; then |
| 78 | echo "OK: $VAL" |
| 79 | else |
| 80 | echo "iniget failed: $VAL" |
| 81 | fi |
| 82 | |
Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 83 | # Test with spaces in section header |
| 84 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame^] | 85 | VAL=$(iniget test.ini " ccc " spaces) |
Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 86 | if [[ "$VAL" == "yes" ]]; then |
| 87 | echo "OK: $VAL" |
| 88 | else |
| 89 | echo "iniget failed: $VAL" |
| 90 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 91 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame^] | 92 | iniset test.ini "b b" opt_ion 42 |
| 93 | |
| 94 | VAL=$(iniget test.ini "b b" opt_ion) |
| 95 | if [[ "$VAL" == "42" ]]; then |
| 96 | echo "OK: $VAL" |
| 97 | else |
| 98 | echo "iniget failed: $VAL" |
| 99 | fi |
| 100 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 101 | # Test without spaces, end of file |
| 102 | |
| 103 | VAL=$(iniget test.ini bbb handlers) |
| 104 | if [[ "$VAL" == "ee,ff" ]]; then |
| 105 | echo "OK: $VAL" |
| 106 | else |
| 107 | echo "iniget failed: $VAL" |
| 108 | fi |
| 109 | |
| 110 | iniset test.ini bbb handlers "33,44" |
| 111 | |
| 112 | VAL=$(iniget test.ini bbb handlers) |
| 113 | if [[ "$VAL" == "33,44" ]]; then |
| 114 | echo "OK: $VAL" |
| 115 | else |
| 116 | echo "iniget failed: $VAL" |
| 117 | fi |
| 118 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame^] | 119 | # test empty option |
| 120 | if ini_has_option test.ini ddd empty; then |
| 121 | echo "OK: ddd.empty present" |
| 122 | else |
| 123 | echo "ini_has_option failed: ddd.empty not found" |
| 124 | fi |
| 125 | |
| 126 | # test non-empty option |
| 127 | if ini_has_option test.ini bbb handlers; then |
| 128 | echo "OK: bbb.handlers present" |
| 129 | else |
| 130 | echo "ini_has_option failed: bbb.handlers not found" |
| 131 | fi |
| 132 | |
| 133 | # test changing empty option |
| 134 | iniset test.ini ddd empty "42" |
| 135 | |
| 136 | VAL=$(iniget test.ini ddd empty) |
| 137 | if [[ "$VAL" == "42" ]]; then |
| 138 | echo "OK: $VAL" |
| 139 | else |
| 140 | echo "iniget failed: $VAL" |
| 141 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 142 | |
| 143 | # Test section not exist |
| 144 | |
| 145 | VAL=$(iniget test.ini zzz handlers) |
| 146 | if [[ -z "$VAL" ]]; then |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 147 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 148 | else |
| 149 | echo "iniget failed: $VAL" |
| 150 | fi |
| 151 | |
| 152 | iniset test.ini zzz handlers "999" |
| 153 | |
| 154 | VAL=$(iniget test.ini zzz handlers) |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 155 | if [[ -n "$VAL" ]]; then |
| 156 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 157 | else |
| 158 | echo "iniget failed: $VAL" |
| 159 | fi |
| 160 | |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 161 | # Test option not exist |
| 162 | |
| 163 | VAL=$(iniget test.ini aaa debug) |
| 164 | if [[ -z "$VAL" ]]; then |
| 165 | echo "OK aaa.debug not present" |
| 166 | else |
| 167 | echo "iniget failed: $VAL" |
| 168 | fi |
| 169 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame^] | 170 | if ! ini_has_option test.ini aaa debug; then |
| 171 | echo "OK aaa.debug not present" |
| 172 | else |
| 173 | echo "ini_has_option failed: aaa.debug" |
| 174 | fi |
| 175 | |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 176 | iniset test.ini aaa debug "999" |
| 177 | |
| 178 | VAL=$(iniget test.ini aaa debug) |
| 179 | if [[ -n "$VAL" ]]; then |
| 180 | echo "OK aaa.debug present" |
| 181 | else |
| 182 | echo "iniget failed: $VAL" |
| 183 | fi |
| 184 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 185 | # Test comments |
| 186 | |
| 187 | inicomment test.ini aaa handlers |
| 188 | |
| 189 | VAL=$(iniget test.ini aaa handlers) |
| 190 | if [[ -z "$VAL" ]]; then |
| 191 | echo "OK" |
| 192 | else |
| 193 | echo "inicomment failed: $VAL" |
| 194 | fi |
Vincent Untz | bf39231 | 2012-06-13 11:26:31 +0200 | [diff] [blame] | 195 | |
| 196 | rm test.ini |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 197 | |
| 198 | # Enabling/disabling services |
| 199 | |
| 200 | echo "Testing enable_service()" |
| 201 | |
| 202 | function test_enable_service() { |
| 203 | local start="$1" |
| 204 | local add="$2" |
| 205 | local finish="$3" |
| 206 | |
| 207 | ENABLED_SERVICES="$start" |
| 208 | enable_service $add |
| 209 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 210 | then |
| 211 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 212 | else |
| 213 | echo "changing $start to $finish with $add failed: $ENABLED_SERVICES" |
| 214 | fi |
| 215 | } |
| 216 | |
| 217 | test_enable_service '' a 'a' |
| 218 | test_enable_service 'a' b 'a,b' |
| 219 | test_enable_service 'a,b' c 'a,b,c' |
| 220 | test_enable_service 'a,b' c 'a,b,c' |
| 221 | test_enable_service 'a,b,' c 'a,b,c' |
| 222 | test_enable_service 'a,b' c,d 'a,b,c,d' |
| 223 | test_enable_service 'a,b' "c d" 'a,b,c,d' |
| 224 | test_enable_service 'a,b,c' c 'a,b,c' |
| 225 | |
| 226 | test_enable_service 'a,b,-c' c 'a,b' |
| 227 | test_enable_service 'a,b,c' -c 'a,b' |
| 228 | |
| 229 | function test_disable_service() { |
| 230 | local start="$1" |
| 231 | local del="$2" |
| 232 | local finish="$3" |
| 233 | |
| 234 | ENABLED_SERVICES="$start" |
| 235 | disable_service "$del" |
| 236 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 237 | then |
| 238 | echo "OK: $start - $del -> $ENABLED_SERVICES" |
| 239 | else |
| 240 | echo "changing $start to $finish with $del failed: $ENABLED_SERVICES" |
| 241 | fi |
| 242 | } |
| 243 | |
| 244 | echo "Testing disable_service()" |
| 245 | test_disable_service 'a,b,c' a 'b,c' |
| 246 | test_disable_service 'a,b,c' b 'a,c' |
| 247 | test_disable_service 'a,b,c' c 'a,b' |
| 248 | |
| 249 | test_disable_service 'a,b,c' a 'b,c' |
| 250 | test_disable_service 'b,c' b 'c' |
| 251 | test_disable_service 'c' c '' |
| 252 | test_disable_service '' d '' |
| 253 | |
| 254 | test_disable_service 'a,b,c,' c 'a,b' |
| 255 | test_disable_service 'a,b' c 'a,b' |
| 256 | |
| 257 | |
| 258 | echo "Testing disable_all_services()" |
| 259 | ENABLED_SERVICES=a,b,c |
| 260 | disable_all_services |
| 261 | |
| 262 | if [[ -z "$ENABLED_SERVICES" ]] |
| 263 | then |
| 264 | echo "OK" |
| 265 | else |
| 266 | echo "disabling all services FAILED: $ENABLED_SERVICES" |
| 267 | fi |
| 268 | |
| 269 | echo "Testing disable_negated_services()" |
| 270 | |
| 271 | |
| 272 | function test_disable_negated_services() { |
| 273 | local start="$1" |
| 274 | local finish="$2" |
| 275 | |
| 276 | ENABLED_SERVICES="$start" |
| 277 | disable_negated_services |
| 278 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 279 | then |
| 280 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 281 | else |
| 282 | echo "changing $start to $finish failed: $ENABLED_SERVICES" |
| 283 | fi |
| 284 | } |
| 285 | |
| 286 | test_disable_negated_services '-a' '' |
| 287 | test_disable_negated_services '-a,a' '' |
| 288 | test_disable_negated_services '-a,-a' '' |
| 289 | test_disable_negated_services 'a,-a' '' |
| 290 | test_disable_negated_services 'b,a,-a' 'b' |
| 291 | test_disable_negated_services 'a,b,-a' 'b' |
| 292 | test_disable_negated_services 'a,-a,b' 'b' |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 293 | |
| 294 | |
| 295 | echo "Testing is_package_installed()" |
| 296 | |
| 297 | if [[ -z "$os_PACKAGE" ]]; then |
| 298 | GetOSVersion |
| 299 | fi |
| 300 | |
| 301 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 302 | is_package_installed dpkg |
| 303 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 304 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 305 | is_package_installed rpm |
| 306 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 307 | else |
| 308 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 309 | fi |
| 310 | if [[ "$VAL" -eq 0 ]]; then |
| 311 | echo "OK" |
| 312 | else |
| 313 | echo "is_package_installed() on existing package failed" |
| 314 | fi |
| 315 | |
| 316 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 317 | is_package_installed dpkg bash |
| 318 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 319 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 320 | is_package_installed rpm bash |
| 321 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 322 | else |
| 323 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 324 | fi |
| 325 | if [[ "$VAL" -eq 0 ]]; then |
| 326 | echo "OK" |
| 327 | else |
| 328 | echo "is_package_installed() on more than one existing package failed" |
| 329 | fi |
| 330 | |
| 331 | is_package_installed zzzZZZzzz |
| 332 | VAL=$? |
| 333 | if [[ "$VAL" -ne 0 ]]; then |
| 334 | echo "OK" |
| 335 | else |
| 336 | echo "is_package_installed() on non-existing package failed" |
| 337 | fi |