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