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 | |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 41 | # Enabling/disabling services |
| 42 | |
| 43 | echo "Testing enable_service()" |
| 44 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 45 | function test_enable_service { |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 46 | local start="$1" |
| 47 | local add="$2" |
| 48 | local finish="$3" |
| 49 | |
| 50 | ENABLED_SERVICES="$start" |
| 51 | enable_service $add |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 52 | if [ "$ENABLED_SERVICES" = "$finish" ]; then |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 53 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 54 | else |
| 55 | echo "changing $start to $finish with $add failed: $ENABLED_SERVICES" |
| 56 | fi |
| 57 | } |
| 58 | |
| 59 | test_enable_service '' a 'a' |
| 60 | test_enable_service 'a' b 'a,b' |
| 61 | test_enable_service 'a,b' c 'a,b,c' |
| 62 | test_enable_service 'a,b' c 'a,b,c' |
| 63 | test_enable_service 'a,b,' c 'a,b,c' |
| 64 | test_enable_service 'a,b' c,d 'a,b,c,d' |
| 65 | test_enable_service 'a,b' "c d" 'a,b,c,d' |
| 66 | test_enable_service 'a,b,c' c 'a,b,c' |
| 67 | |
| 68 | test_enable_service 'a,b,-c' c 'a,b' |
| 69 | test_enable_service 'a,b,c' -c 'a,b' |
| 70 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 71 | function test_disable_service { |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 72 | local start="$1" |
| 73 | local del="$2" |
| 74 | local finish="$3" |
| 75 | |
| 76 | ENABLED_SERVICES="$start" |
| 77 | disable_service "$del" |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 78 | if [ "$ENABLED_SERVICES" = "$finish" ]; then |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 79 | echo "OK: $start - $del -> $ENABLED_SERVICES" |
| 80 | else |
| 81 | echo "changing $start to $finish with $del failed: $ENABLED_SERVICES" |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | echo "Testing disable_service()" |
| 86 | test_disable_service 'a,b,c' a 'b,c' |
| 87 | test_disable_service 'a,b,c' b 'a,c' |
| 88 | test_disable_service 'a,b,c' c 'a,b' |
| 89 | |
| 90 | test_disable_service 'a,b,c' a 'b,c' |
| 91 | test_disable_service 'b,c' b 'c' |
| 92 | test_disable_service 'c' c '' |
| 93 | test_disable_service '' d '' |
| 94 | |
| 95 | test_disable_service 'a,b,c,' c 'a,b' |
| 96 | test_disable_service 'a,b' c 'a,b' |
| 97 | |
| 98 | |
| 99 | echo "Testing disable_all_services()" |
| 100 | ENABLED_SERVICES=a,b,c |
| 101 | disable_all_services |
| 102 | |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 103 | if [[ -z "$ENABLED_SERVICES" ]]; then |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 104 | echo "OK" |
| 105 | else |
| 106 | echo "disabling all services FAILED: $ENABLED_SERVICES" |
| 107 | fi |
| 108 | |
| 109 | echo "Testing disable_negated_services()" |
| 110 | |
| 111 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 112 | function test_disable_negated_services { |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 113 | local start="$1" |
| 114 | local finish="$2" |
| 115 | |
| 116 | ENABLED_SERVICES="$start" |
| 117 | disable_negated_services |
Sean Dague | 16dd8b3 | 2014-02-03 09:10:54 +0900 | [diff] [blame] | 118 | if [ "$ENABLED_SERVICES" = "$finish" ]; then |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 119 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 120 | else |
| 121 | echo "changing $start to $finish failed: $ENABLED_SERVICES" |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | test_disable_negated_services '-a' '' |
| 126 | test_disable_negated_services '-a,a' '' |
| 127 | test_disable_negated_services '-a,-a' '' |
| 128 | test_disable_negated_services 'a,-a' '' |
| 129 | test_disable_negated_services 'b,a,-a' 'b' |
| 130 | test_disable_negated_services 'a,b,-a' 'b' |
| 131 | test_disable_negated_services 'a,-a,b' 'b' |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 132 | |
| 133 | |
| 134 | echo "Testing is_package_installed()" |
| 135 | |
| 136 | if [[ -z "$os_PACKAGE" ]]; then |
| 137 | GetOSVersion |
| 138 | fi |
| 139 | |
| 140 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 141 | is_package_installed dpkg |
| 142 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 143 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 144 | is_package_installed rpm |
| 145 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 146 | else |
| 147 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 148 | fi |
| 149 | if [[ "$VAL" -eq 0 ]]; then |
| 150 | echo "OK" |
| 151 | else |
| 152 | echo "is_package_installed() on existing package failed" |
| 153 | fi |
| 154 | |
| 155 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 156 | is_package_installed dpkg bash |
| 157 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 158 | elif [[ "$os_PACKAGE" = "rpm" ]]; then |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 159 | is_package_installed rpm bash |
| 160 | VAL=$? |
Vincent Untz | 00011c0 | 2012-12-06 09:56:32 +0100 | [diff] [blame] | 161 | else |
| 162 | VAL=1 |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 163 | fi |
| 164 | if [[ "$VAL" -eq 0 ]]; then |
| 165 | echo "OK" |
| 166 | else |
| 167 | echo "is_package_installed() on more than one existing package failed" |
| 168 | fi |
| 169 | |
| 170 | is_package_installed zzzZZZzzz |
| 171 | VAL=$? |
| 172 | if [[ "$VAL" -ne 0 ]]; then |
| 173 | echo "OK" |
| 174 | else |
| 175 | echo "is_package_installed() on non-existing package failed" |
| 176 | fi |
Dean Troyer | 04762cd | 2013-08-27 17:06:14 -0500 | [diff] [blame] | 177 | |
| 178 | # test against removed package...was a bug on Ubuntu |
| 179 | if is_ubuntu; then |
| 180 | PKG=cowsay |
| 181 | if ! (dpkg -s $PKG >/dev/null 2>&1); then |
| 182 | # it was never installed...set up the condition |
| 183 | sudo apt-get install -y cowsay >/dev/null 2>&1 |
| 184 | fi |
| 185 | if (dpkg -s $PKG >/dev/null 2>&1); then |
| 186 | # remove it to create the 'un' status |
| 187 | sudo dpkg -P $PKG >/dev/null 2>&1 |
| 188 | fi |
| 189 | |
| 190 | # now test the installed check on a deleted package |
| 191 | is_package_installed $PKG |
| 192 | VAL=$? |
| 193 | if [[ "$VAL" -ne 0 ]]; then |
| 194 | echo "OK" |
| 195 | else |
| 196 | echo "is_package_installed() on deleted package failed" |
| 197 | fi |
| 198 | fi |