| 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' | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 130 | test_disable_negated_services 'a,aa,-a' 'aa' | 
 | 131 | test_disable_negated_services 'aa,-a' 'aa' | 
 | 132 | test_disable_negated_services 'a_a, -a_a' '' | 
 | 133 | test_disable_negated_services 'a-b, -a-b' '' | 
 | 134 | test_disable_negated_services 'a-b, b, -a-b' 'b' | 
 | 135 | test_disable_negated_services 'a,-a,av2,b' 'av2,b' | 
 | 136 | test_disable_negated_services 'a,aa,-a' 'aa' | 
 | 137 | test_disable_negated_services 'a,av2,-a,a' 'av2' | 
 | 138 | test_disable_negated_services 'a,-a,av2' 'av2' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 139 |  | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 140 | echo "Testing remove_disabled_services()" | 
 | 141 |  | 
 | 142 | function test_remove_disabled_services { | 
 | 143 |     local service_list="$1" | 
 | 144 |     local remove_list="$2" | 
 | 145 |     local expected="$3" | 
 | 146 |  | 
 | 147 |     results=$(remove_disabled_services "$service_list" "$remove_list") | 
 | 148 |     if [ "$results" = "$expected" ]; then | 
 | 149 |         passed "OK: '$service_list' - '$remove_list' -> '$results'" | 
 | 150 |     else | 
 | 151 |         failed "getting '$expected' from '$service_list' - '$remove_list' failed: '$results'" | 
 | 152 |     fi | 
 | 153 | } | 
 | 154 |  | 
 | 155 | test_remove_disabled_services 'a,b,c' 'a,c' 'b' | 
 | 156 | test_remove_disabled_services 'a,b,c' 'b' 'a,c' | 
 | 157 | test_remove_disabled_services 'a,b,c,d' 'a,c d' 'b' | 
 | 158 | test_remove_disabled_services 'a,b c,d' 'a d' 'b,c' | 
 | 159 | test_remove_disabled_services 'a,b,c' 'a,b,c' '' | 
 | 160 | test_remove_disabled_services 'a,b,c' 'd' 'a,b,c' | 
 | 161 | test_remove_disabled_services 'a,b,c' '' 'a,b,c' | 
 | 162 | test_remove_disabled_services '' 'a,b,c' '' | 
 | 163 | test_remove_disabled_services '' '' '' | 
 | 164 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 165 | echo "Testing is_package_installed()" | 
 | 166 |  | 
 | 167 | if [[ -z "$os_PACKAGE" ]]; then | 
 | 168 |     GetOSVersion | 
 | 169 | fi | 
 | 170 |  | 
 | 171 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 172 |     is_package_installed dpkg | 
 | 173 |     VAL=$? | 
 | 174 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
 | 175 |     is_package_installed rpm | 
 | 176 |     VAL=$? | 
 | 177 | else | 
 | 178 |     VAL=1 | 
 | 179 | fi | 
 | 180 | if [[ "$VAL" -eq 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 181 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 182 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 183 |     failed "is_package_installed() on existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 184 | fi | 
 | 185 |  | 
 | 186 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
 | 187 |     is_package_installed dpkg bash | 
 | 188 |     VAL=$? | 
 | 189 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
 | 190 |     is_package_installed rpm bash | 
 | 191 |     VAL=$? | 
 | 192 | else | 
 | 193 |     VAL=1 | 
 | 194 | fi | 
 | 195 | if [[ "$VAL" -eq 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 196 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 197 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 198 |     failed "is_package_installed() on more than one existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 199 | fi | 
 | 200 |  | 
 | 201 | is_package_installed zzzZZZzzz | 
 | 202 | VAL=$? | 
 | 203 | if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 204 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 205 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 206 |     failed "is_package_installed() on non-existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 207 | fi | 
 | 208 |  | 
 | 209 | # test against removed package...was a bug on Ubuntu | 
 | 210 | if is_ubuntu; then | 
 | 211 |     PKG=cowsay | 
 | 212 |     if ! (dpkg -s $PKG >/dev/null 2>&1); then | 
 | 213 |         # it was never installed...set up the condition | 
 | 214 |         sudo apt-get install -y cowsay >/dev/null 2>&1 | 
 | 215 |     fi | 
 | 216 |     if (dpkg -s $PKG >/dev/null 2>&1); then | 
 | 217 |         # remove it to create the 'un' status | 
 | 218 |         sudo dpkg -P $PKG >/dev/null 2>&1 | 
 | 219 |     fi | 
 | 220 |  | 
 | 221 |     # now test the installed check on a deleted package | 
 | 222 |     is_package_installed $PKG | 
 | 223 |     VAL=$? | 
 | 224 |     if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 225 |         passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 226 |     else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 227 |         failed "is_package_installed() on deleted package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 228 |     fi | 
 | 229 | fi | 
 | 230 |  | 
 | 231 | # test isset function | 
 | 232 | echo  "Testing isset()" | 
 | 233 | you_should_not_have_this_variable=42 | 
 | 234 |  | 
 | 235 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 236 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 237 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 238 |     failed "\"you_should_not_have_this_variable\" not declared. failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 239 | fi | 
 | 240 |  | 
 | 241 | unset you_should_not_have_this_variable | 
 | 242 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 243 |     failed "\"you_should_not_have_this_variable\" looks like declared variable." | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 244 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 245 |     passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 246 | fi | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 247 |  | 
 | 248 | report_results |