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 = |
Lianhao Lu | 239f324 | 2013-03-01 15:54:02 +0800 | [diff] [blame] | 63 | |
| 64 | [eee] |
| 65 | multi = foo1 |
| 66 | multi = foo2 |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 67 | EOF |
| 68 | |
| 69 | # Test with spaces |
| 70 | |
| 71 | VAL=$(iniget test.ini aaa handlers) |
| 72 | if [[ "$VAL" == "aa, bb" ]]; then |
| 73 | echo "OK: $VAL" |
| 74 | else |
| 75 | echo "iniget failed: $VAL" |
| 76 | fi |
| 77 | |
| 78 | iniset test.ini aaa handlers "11, 22" |
| 79 | |
| 80 | VAL=$(iniget test.ini aaa handlers) |
| 81 | if [[ "$VAL" == "11, 22" ]]; then |
| 82 | echo "OK: $VAL" |
| 83 | else |
| 84 | echo "iniget failed: $VAL" |
| 85 | fi |
| 86 | |
Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 87 | # Test with spaces in section header |
| 88 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 89 | VAL=$(iniget test.ini " ccc " spaces) |
Dean Troyer | e833562 | 2012-11-27 17:00:11 -0600 | [diff] [blame] | 90 | if [[ "$VAL" == "yes" ]]; then |
| 91 | echo "OK: $VAL" |
| 92 | else |
| 93 | echo "iniget failed: $VAL" |
| 94 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 95 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 96 | iniset test.ini "b b" opt_ion 42 |
| 97 | |
| 98 | VAL=$(iniget test.ini "b b" opt_ion) |
| 99 | if [[ "$VAL" == "42" ]]; then |
| 100 | echo "OK: $VAL" |
| 101 | else |
| 102 | echo "iniget failed: $VAL" |
| 103 | fi |
| 104 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 105 | # Test without spaces, end of file |
| 106 | |
| 107 | VAL=$(iniget test.ini bbb handlers) |
| 108 | if [[ "$VAL" == "ee,ff" ]]; then |
| 109 | echo "OK: $VAL" |
| 110 | else |
| 111 | echo "iniget failed: $VAL" |
| 112 | fi |
| 113 | |
| 114 | iniset test.ini bbb handlers "33,44" |
| 115 | |
| 116 | VAL=$(iniget test.ini bbb handlers) |
| 117 | if [[ "$VAL" == "33,44" ]]; then |
| 118 | echo "OK: $VAL" |
| 119 | else |
| 120 | echo "iniget failed: $VAL" |
| 121 | fi |
| 122 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 123 | # test empty option |
| 124 | if ini_has_option test.ini ddd empty; then |
| 125 | echo "OK: ddd.empty present" |
| 126 | else |
| 127 | echo "ini_has_option failed: ddd.empty not found" |
| 128 | fi |
| 129 | |
| 130 | # test non-empty option |
| 131 | if ini_has_option test.ini bbb handlers; then |
| 132 | echo "OK: bbb.handlers present" |
| 133 | else |
| 134 | echo "ini_has_option failed: bbb.handlers not found" |
| 135 | fi |
| 136 | |
| 137 | # test changing empty option |
| 138 | iniset test.ini ddd empty "42" |
| 139 | |
| 140 | VAL=$(iniget test.ini ddd empty) |
| 141 | if [[ "$VAL" == "42" ]]; then |
| 142 | echo "OK: $VAL" |
| 143 | else |
| 144 | echo "iniget failed: $VAL" |
| 145 | fi |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 146 | |
| 147 | # Test section not exist |
| 148 | |
| 149 | VAL=$(iniget test.ini zzz handlers) |
| 150 | if [[ -z "$VAL" ]]; then |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 151 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 152 | else |
| 153 | echo "iniget failed: $VAL" |
| 154 | fi |
| 155 | |
| 156 | iniset test.ini zzz handlers "999" |
| 157 | |
| 158 | VAL=$(iniget test.ini zzz handlers) |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 159 | if [[ -n "$VAL" ]]; then |
| 160 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 161 | else |
| 162 | echo "iniget failed: $VAL" |
| 163 | fi |
| 164 | |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 165 | # Test option not exist |
| 166 | |
| 167 | VAL=$(iniget test.ini aaa debug) |
| 168 | if [[ -z "$VAL" ]]; then |
| 169 | echo "OK aaa.debug not present" |
| 170 | else |
| 171 | echo "iniget failed: $VAL" |
| 172 | fi |
| 173 | |
Attila Fazekas | 588eb41 | 2012-12-20 10:57:16 +0100 | [diff] [blame] | 174 | if ! ini_has_option test.ini aaa debug; then |
| 175 | echo "OK aaa.debug not present" |
| 176 | else |
| 177 | echo "ini_has_option failed: aaa.debug" |
| 178 | fi |
| 179 | |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 180 | iniset test.ini aaa debug "999" |
| 181 | |
| 182 | VAL=$(iniget test.ini aaa debug) |
| 183 | if [[ -n "$VAL" ]]; then |
| 184 | echo "OK aaa.debug present" |
| 185 | else |
| 186 | echo "iniget failed: $VAL" |
| 187 | fi |
| 188 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 189 | # Test comments |
| 190 | |
| 191 | inicomment test.ini aaa handlers |
| 192 | |
| 193 | VAL=$(iniget test.ini aaa handlers) |
| 194 | if [[ -z "$VAL" ]]; then |
| 195 | echo "OK" |
| 196 | else |
| 197 | echo "inicomment failed: $VAL" |
| 198 | fi |
Vincent Untz | bf39231 | 2012-06-13 11:26:31 +0200 | [diff] [blame] | 199 | |
Lianhao Lu | 239f324 | 2013-03-01 15:54:02 +0800 | [diff] [blame] | 200 | # Test multiple line iniset/iniget |
| 201 | iniset_multiline test.ini eee multi bar1 bar2 |
| 202 | |
| 203 | VAL=$(iniget_multiline test.ini eee multi) |
| 204 | if [[ "$VAL" == "bar1 bar2" ]]; then |
| 205 | echo "OK: iniset_multiline" |
| 206 | else |
| 207 | echo "iniset_multiline failed: $VAL" |
| 208 | fi |
| 209 | |
| 210 | # Test iniadd with exiting values |
| 211 | iniadd test.ini eee multi bar3 |
| 212 | VAL=$(iniget_multiline test.ini eee multi) |
| 213 | if [[ "$VAL" == "bar1 bar2 bar3" ]]; then |
| 214 | echo "OK: iniadd" |
| 215 | else |
| 216 | echo "iniadd failed: $VAL" |
| 217 | fi |
| 218 | |
| 219 | # Test iniadd with non-exiting values |
| 220 | iniadd test.ini eee non-multi foobar1 foobar2 |
| 221 | VAL=$(iniget_multiline test.ini eee non-multi) |
| 222 | if [[ "$VAL" == "foobar1 foobar2" ]]; then |
| 223 | echo "OK: iniadd with non-exiting value" |
| 224 | else |
| 225 | echo "iniadd with non-exsting failed: $VAL" |
| 226 | fi |
| 227 | |
Vincent Untz | bf39231 | 2012-06-13 11:26:31 +0200 | [diff] [blame] | 228 | rm test.ini |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 229 | |
| 230 | # Enabling/disabling services |
| 231 | |
| 232 | echo "Testing enable_service()" |
| 233 | |
| 234 | function test_enable_service() { |
| 235 | local start="$1" |
| 236 | local add="$2" |
| 237 | local finish="$3" |
| 238 | |
| 239 | ENABLED_SERVICES="$start" |
| 240 | enable_service $add |
| 241 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 242 | then |
| 243 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 244 | else |
| 245 | echo "changing $start to $finish with $add failed: $ENABLED_SERVICES" |
| 246 | fi |
| 247 | } |
| 248 | |
| 249 | test_enable_service '' a 'a' |
| 250 | test_enable_service 'a' b 'a,b' |
| 251 | test_enable_service 'a,b' c 'a,b,c' |
| 252 | test_enable_service 'a,b' c 'a,b,c' |
| 253 | test_enable_service 'a,b,' c 'a,b,c' |
| 254 | test_enable_service 'a,b' c,d 'a,b,c,d' |
| 255 | test_enable_service 'a,b' "c d" 'a,b,c,d' |
| 256 | test_enable_service 'a,b,c' c 'a,b,c' |
| 257 | |
| 258 | test_enable_service 'a,b,-c' c 'a,b' |
| 259 | test_enable_service 'a,b,c' -c 'a,b' |
| 260 | |
| 261 | function test_disable_service() { |
| 262 | local start="$1" |
| 263 | local del="$2" |
| 264 | local finish="$3" |
| 265 | |
| 266 | ENABLED_SERVICES="$start" |
| 267 | disable_service "$del" |
| 268 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 269 | then |
| 270 | echo "OK: $start - $del -> $ENABLED_SERVICES" |
| 271 | else |
| 272 | echo "changing $start to $finish with $del failed: $ENABLED_SERVICES" |
| 273 | fi |
| 274 | } |
| 275 | |
| 276 | echo "Testing disable_service()" |
| 277 | test_disable_service 'a,b,c' a 'b,c' |
| 278 | test_disable_service 'a,b,c' b 'a,c' |
| 279 | test_disable_service 'a,b,c' c 'a,b' |
| 280 | |
| 281 | test_disable_service 'a,b,c' a 'b,c' |
| 282 | test_disable_service 'b,c' b 'c' |
| 283 | test_disable_service 'c' c '' |
| 284 | test_disable_service '' d '' |
| 285 | |
| 286 | test_disable_service 'a,b,c,' c 'a,b' |
| 287 | test_disable_service 'a,b' c 'a,b' |
| 288 | |
| 289 | |
| 290 | echo "Testing disable_all_services()" |
| 291 | ENABLED_SERVICES=a,b,c |
| 292 | disable_all_services |
| 293 | |
| 294 | if [[ -z "$ENABLED_SERVICES" ]] |
| 295 | then |
| 296 | echo "OK" |
| 297 | else |
| 298 | echo "disabling all services FAILED: $ENABLED_SERVICES" |
| 299 | fi |
| 300 | |
| 301 | echo "Testing disable_negated_services()" |
| 302 | |
| 303 | |
| 304 | function test_disable_negated_services() { |
| 305 | local start="$1" |
| 306 | local finish="$2" |
| 307 | |
| 308 | ENABLED_SERVICES="$start" |
| 309 | disable_negated_services |
| 310 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 311 | then |
| 312 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 313 | else |
| 314 | echo "changing $start to $finish failed: $ENABLED_SERVICES" |
| 315 | fi |
| 316 | } |
| 317 | |
| 318 | test_disable_negated_services '-a' '' |
| 319 | test_disable_negated_services '-a,a' '' |
| 320 | test_disable_negated_services '-a,-a' '' |
| 321 | test_disable_negated_services 'a,-a' '' |
| 322 | test_disable_negated_services 'b,a,-a' 'b' |
| 323 | test_disable_negated_services 'a,b,-a' 'b' |
| 324 | test_disable_negated_services 'a,-a,b' 'b' |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 325 | |
| 326 | |
| 327 | echo "Testing is_package_installed()" |
| 328 | |
| 329 | if [[ -z "$os_PACKAGE" ]]; then |
| 330 | GetOSVersion |
| 331 | fi |
| 332 | |
| 333 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 334 | is_package_installed dpkg |
| 335 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 336 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 337 | is_package_installed rpm |
| 338 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 339 | else |
| 340 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 341 | fi |
| 342 | if [[ "$VAL" -eq 0 ]]; then |
| 343 | echo "OK" |
| 344 | else |
| 345 | echo "is_package_installed() on existing package failed" |
| 346 | fi |
| 347 | |
| 348 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 349 | is_package_installed dpkg bash |
| 350 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 351 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 352 | is_package_installed rpm bash |
| 353 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 354 | else |
| 355 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 356 | fi |
| 357 | if [[ "$VAL" -eq 0 ]]; then |
| 358 | echo "OK" |
| 359 | else |
| 360 | echo "is_package_installed() on more than one existing package failed" |
| 361 | fi |
| 362 | |
| 363 | is_package_installed zzzZZZzzz |
| 364 | VAL=$? |
| 365 | if [[ "$VAL" -ne 0 ]]; then |
| 366 | echo "OK" |
| 367 | else |
| 368 | echo "is_package_installed() on non-existing package failed" |
| 369 | fi |
Dean Troyer | 04762cd | 2013-08-27 17:06:14 -0500 | [diff] [blame^] | 370 | |
| 371 | # test against removed package...was a bug on Ubuntu |
| 372 | if is_ubuntu; then |
| 373 | PKG=cowsay |
| 374 | if ! (dpkg -s $PKG >/dev/null 2>&1); then |
| 375 | # it was never installed...set up the condition |
| 376 | sudo apt-get install -y cowsay >/dev/null 2>&1 |
| 377 | fi |
| 378 | if (dpkg -s $PKG >/dev/null 2>&1); then |
| 379 | # remove it to create the 'un' status |
| 380 | sudo dpkg -P $PKG >/dev/null 2>&1 |
| 381 | fi |
| 382 | |
| 383 | # now test the installed check on a deleted package |
| 384 | is_package_installed $PKG |
| 385 | VAL=$? |
| 386 | if [[ "$VAL" -ne 0 ]]; then |
| 387 | echo "OK" |
| 388 | else |
| 389 | echo "is_package_installed() on deleted package failed" |
| 390 | fi |
| 391 | fi |