| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash | 
 | 2 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 3 | # Tests for DevStack functions | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 4 |  | 
 | 5 | TOP=$(cd $(dirname "$0")/.. && pwd) | 
 | 6 |  | 
 | 7 | # Import common functions | 
 | 8 | source $TOP/functions | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 9 |  | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 10 | source $TOP/tests/unittest.sh | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 11 |  | 
 | 12 | echo "Testing die_if_not_set()" | 
 | 13 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 14 | bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set $LINENO X 'not OK'" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 15 | if [[ $? != 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 16 |     failed "die_if_not_set [X='Y' true] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 17 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 18 |     passed 'OK' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 19 | fi | 
 | 20 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 21 | bash -c "source $TOP/functions; X=`true`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1 | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 22 | if [[ $? = 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 23 |     failed "die_if_not_set [X='' true] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 24 | fi | 
 | 25 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 26 | bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set $LINENO X 'not OK'" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 27 | if [[ $? != 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 28 |     failed "die_if_not_set [X='Y' false] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 29 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 30 |     passed 'OK' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 31 | fi | 
 | 32 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 33 | bash -c "source $TOP/functions; X=`false`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1 | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 34 | if [[ $? = 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 35 |     failed "die_if_not_set [X='' false] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 36 | fi | 
 | 37 |  | 
 | 38 |  | 
 | 39 | # Enabling/disabling services | 
 | 40 |  | 
 | 41 | echo "Testing enable_service()" | 
 | 42 |  | 
 | 43 | function test_enable_service { | 
 | 44 |     local start="$1" | 
 | 45 |     local add="$2" | 
 | 46 |     local finish="$3" | 
 | 47 |  | 
 | 48 |     ENABLED_SERVICES="$start" | 
 | 49 |     enable_service $add | 
 | 50 |     if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 51 |         passed "OK: $start + $add -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 52 |     else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 53 |         failed "changing $start to $finish with $add failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 54 |     fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 55 | } | 
 | 56 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 57 | test_enable_service '' a 'a' | 
 | 58 | test_enable_service 'a' b 'a,b' | 
 | 59 | test_enable_service 'a,b' c 'a,b,c' | 
 | 60 | test_enable_service 'a,b' c 'a,b,c' | 
 | 61 | test_enable_service 'a,b,' c 'a,b,c' | 
 | 62 | test_enable_service 'a,b' c,d 'a,b,c,d' | 
 | 63 | test_enable_service 'a,b' "c d" 'a,b,c,d' | 
 | 64 | test_enable_service 'a,b,c' c 'a,b,c' | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 65 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 66 | test_enable_service 'a,b,-c' c 'a,b' | 
 | 67 | test_enable_service 'a,b,c' -c 'a,b' | 
 | 68 |  | 
 | 69 | function test_disable_service { | 
 | 70 |     local start="$1" | 
 | 71 |     local del="$2" | 
 | 72 |     local finish="$3" | 
 | 73 |  | 
 | 74 |     ENABLED_SERVICES="$start" | 
 | 75 |     disable_service "$del" | 
 | 76 |     if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 77 |         passed "OK: $start - $del -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 78 |     else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 79 |         failed "changing $start to $finish with $del failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 80 |     fi | 
 | 81 | } | 
 | 82 |  | 
 | 83 | echo "Testing disable_service()" | 
 | 84 | test_disable_service 'a,b,c' a 'b,c' | 
 | 85 | test_disable_service 'a,b,c' b 'a,c' | 
 | 86 | test_disable_service 'a,b,c' c 'a,b' | 
 | 87 |  | 
 | 88 | test_disable_service 'a,b,c' a 'b,c' | 
 | 89 | test_disable_service 'b,c' b 'c' | 
 | 90 | test_disable_service 'c' c '' | 
 | 91 | test_disable_service '' d '' | 
 | 92 |  | 
 | 93 | test_disable_service 'a,b,c,' c 'a,b' | 
 | 94 | test_disable_service 'a,b' c 'a,b' | 
 | 95 |  | 
 | 96 |  | 
 | 97 | echo "Testing disable_all_services()" | 
 | 98 | ENABLED_SERVICES=a,b,c | 
 | 99 | disable_all_services | 
 | 100 |  | 
 | 101 | if [[ -z "$ENABLED_SERVICES" ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 102 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 103 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 104 |     failed "disabling all services FAILED: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 105 | fi | 
 | 106 |  | 
 | 107 | echo "Testing disable_negated_services()" | 
 | 108 |  | 
 | 109 |  | 
 | 110 | function test_disable_negated_services { | 
 | 111 |     local start="$1" | 
 | 112 |     local finish="$2" | 
 | 113 |  | 
 | 114 |     ENABLED_SERVICES="$start" | 
 | 115 |     disable_negated_services | 
 | 116 |     if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 117 |         passed "OK: $start + $add -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 118 |     else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 119 |         failed "changing $start to $finish failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 120 |     fi | 
 | 121 | } | 
 | 122 |  | 
 | 123 | test_disable_negated_services '-a' '' | 
 | 124 | test_disable_negated_services '-a,a' '' | 
 | 125 | test_disable_negated_services '-a,-a' '' | 
 | 126 | test_disable_negated_services 'a,-a' '' | 
 | 127 | test_disable_negated_services 'b,a,-a' 'b' | 
 | 128 | test_disable_negated_services 'a,b,-a' 'b' | 
 | 129 | test_disable_negated_services 'a,-a,b' 'b' | 
 | 130 |  | 
 | 131 |  | 
 | 132 | echo "Testing is_package_installed()" | 
 | 133 |  | 
 | 134 | if [[ -z "$os_PACKAGE" ]]; then | 
 | 135 |     GetOSVersion | 
 | 136 | fi | 
 | 137 |  | 
 | 138 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 139 |     is_package_installed dpkg | 
 | 140 |     VAL=$? | 
 | 141 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
 | 142 |     is_package_installed rpm | 
 | 143 |     VAL=$? | 
 | 144 | else | 
 | 145 |     VAL=1 | 
 | 146 | fi | 
 | 147 | if [[ "$VAL" -eq 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 148 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 149 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 150 |     failed "is_package_installed() on existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 151 | fi | 
 | 152 |  | 
 | 153 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 154 |     is_package_installed dpkg bash | 
 | 155 |     VAL=$? | 
 | 156 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
 | 157 |     is_package_installed rpm bash | 
 | 158 |     VAL=$? | 
 | 159 | else | 
 | 160 |     VAL=1 | 
 | 161 | fi | 
 | 162 | if [[ "$VAL" -eq 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 163 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 164 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 165 |     failed "is_package_installed() on more than one existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 166 | fi | 
 | 167 |  | 
 | 168 | is_package_installed zzzZZZzzz | 
 | 169 | VAL=$? | 
 | 170 | if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 171 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 172 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 173 |     failed "is_package_installed() on non-existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 174 | fi | 
 | 175 |  | 
 | 176 | # test against removed package...was a bug on Ubuntu | 
 | 177 | if is_ubuntu; then | 
 | 178 |     PKG=cowsay | 
 | 179 |     if ! (dpkg -s $PKG >/dev/null 2>&1); then | 
 | 180 |         # it was never installed...set up the condition | 
 | 181 |         sudo apt-get install -y cowsay >/dev/null 2>&1 | 
 | 182 |     fi | 
 | 183 |     if (dpkg -s $PKG >/dev/null 2>&1); then | 
 | 184 |         # remove it to create the 'un' status | 
 | 185 |         sudo dpkg -P $PKG >/dev/null 2>&1 | 
 | 186 |     fi | 
 | 187 |  | 
 | 188 |     # now test the installed check on a deleted package | 
 | 189 |     is_package_installed $PKG | 
 | 190 |     VAL=$? | 
 | 191 |     if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 192 |         passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 193 |     else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 194 |         failed "is_package_installed() on deleted package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 195 |     fi | 
 | 196 | fi | 
 | 197 |  | 
 | 198 | # test isset function | 
 | 199 | echo  "Testing isset()" | 
 | 200 | you_should_not_have_this_variable=42 | 
 | 201 |  | 
 | 202 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 203 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 204 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 205 |     failed "\"you_should_not_have_this_variable\" not declared. failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 206 | fi | 
 | 207 |  | 
 | 208 | unset you_should_not_have_this_variable | 
 | 209 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 210 |     failed "\"you_should_not_have_this_variable\" looks like declared variable." | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 211 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 212 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 213 | fi | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 214 |  | 
 | 215 | report_results |