| 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 |  | 
| Mark Vanderwiel | d99c290 | 2016-03-17 12:19:16 -0500 | [diff] [blame] | 12 | echo "Testing generate_hex_string()" | 
|  | 13 |  | 
|  | 14 | VAL=$(generate_hex_string 16) | 
|  | 15 | if [[ ${#VAL} -eq 32 ]]; then | 
|  | 16 | passed "OK" | 
|  | 17 | else | 
|  | 18 | failed "generate_hex_string 16 failed ${#VAL}" | 
|  | 19 | fi | 
|  | 20 |  | 
|  | 21 | VAL=$(generate_hex_string 32) | 
|  | 22 | if [[ ${#VAL} -eq 64 ]]; then | 
|  | 23 | passed "OK" | 
|  | 24 | else | 
|  | 25 | failed "generate_hex_string 32 failed ${#VAL}" | 
|  | 26 | fi | 
|  | 27 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 28 | echo "Testing die_if_not_set()" | 
|  | 29 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 30 | 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] | 31 | if [[ $? != 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 32 | failed "die_if_not_set [X='Y' true] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 33 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 34 | passed 'OK' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 35 | fi | 
|  | 36 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 37 | 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] | 38 | if [[ $? = 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 39 | failed "die_if_not_set [X='' true] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 40 | fi | 
|  | 41 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 42 | 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] | 43 | if [[ $? != 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 44 | failed "die_if_not_set [X='Y' false] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 45 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 46 | passed 'OK' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 47 | fi | 
|  | 48 |  | 
| Ian Wienand | 9b845da | 2015-04-17 13:10:33 +1000 | [diff] [blame] | 49 | 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] | 50 | if [[ $? = 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 51 | failed "die_if_not_set [X='' false] Failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 52 | fi | 
|  | 53 |  | 
|  | 54 |  | 
|  | 55 | # Enabling/disabling services | 
|  | 56 |  | 
|  | 57 | echo "Testing enable_service()" | 
|  | 58 |  | 
|  | 59 | function test_enable_service { | 
|  | 60 | local start="$1" | 
|  | 61 | local add="$2" | 
|  | 62 | local finish="$3" | 
|  | 63 |  | 
|  | 64 | ENABLED_SERVICES="$start" | 
|  | 65 | enable_service $add | 
|  | 66 | if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 67 | passed "OK: $start + $add -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 68 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 69 | failed "changing $start to $finish with $add failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 70 | fi | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 73 | test_enable_service '' a 'a' | 
|  | 74 | test_enable_service 'a' b 'a,b' | 
|  | 75 | test_enable_service 'a,b' c 'a,b,c' | 
|  | 76 | test_enable_service 'a,b' c 'a,b,c' | 
|  | 77 | test_enable_service 'a,b,' c 'a,b,c' | 
|  | 78 | test_enable_service 'a,b' c,d 'a,b,c,d' | 
|  | 79 | test_enable_service 'a,b' "c d" 'a,b,c,d' | 
|  | 80 | test_enable_service 'a,b,c' c 'a,b,c' | 
| Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 81 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 82 | test_enable_service 'a,b,-c' c 'a,b' | 
|  | 83 | test_enable_service 'a,b,c' -c 'a,b' | 
|  | 84 |  | 
|  | 85 | function test_disable_service { | 
|  | 86 | local start="$1" | 
|  | 87 | local del="$2" | 
|  | 88 | local finish="$3" | 
|  | 89 |  | 
|  | 90 | ENABLED_SERVICES="$start" | 
|  | 91 | disable_service "$del" | 
|  | 92 | if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 93 | passed "OK: $start - $del -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 94 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 95 | failed "changing $start to $finish with $del failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 96 | fi | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | echo "Testing disable_service()" | 
|  | 100 | test_disable_service 'a,b,c' a 'b,c' | 
|  | 101 | test_disable_service 'a,b,c' b 'a,c' | 
|  | 102 | test_disable_service 'a,b,c' c 'a,b' | 
|  | 103 |  | 
|  | 104 | test_disable_service 'a,b,c' a 'b,c' | 
|  | 105 | test_disable_service 'b,c' b 'c' | 
|  | 106 | test_disable_service 'c' c '' | 
|  | 107 | test_disable_service '' d '' | 
|  | 108 |  | 
|  | 109 | test_disable_service 'a,b,c,' c 'a,b' | 
|  | 110 | test_disable_service 'a,b' c 'a,b' | 
|  | 111 |  | 
|  | 112 |  | 
|  | 113 | echo "Testing disable_all_services()" | 
|  | 114 | ENABLED_SERVICES=a,b,c | 
|  | 115 | disable_all_services | 
|  | 116 |  | 
|  | 117 | if [[ -z "$ENABLED_SERVICES" ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 118 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 119 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 120 | failed "disabling all services FAILED: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 121 | fi | 
|  | 122 |  | 
|  | 123 | echo "Testing disable_negated_services()" | 
|  | 124 |  | 
|  | 125 |  | 
|  | 126 | function test_disable_negated_services { | 
|  | 127 | local start="$1" | 
|  | 128 | local finish="$2" | 
|  | 129 |  | 
|  | 130 | ENABLED_SERVICES="$start" | 
|  | 131 | disable_negated_services | 
|  | 132 | if [ "$ENABLED_SERVICES" = "$finish" ]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 133 | passed "OK: $start + $add -> $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 134 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 135 | failed "changing $start to $finish failed: $ENABLED_SERVICES" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 136 | fi | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | test_disable_negated_services '-a' '' | 
|  | 140 | test_disable_negated_services '-a,a' '' | 
|  | 141 | test_disable_negated_services '-a,-a' '' | 
|  | 142 | test_disable_negated_services 'a,-a' '' | 
|  | 143 | test_disable_negated_services 'b,a,-a' 'b' | 
|  | 144 | test_disable_negated_services 'a,b,-a' 'b' | 
|  | 145 | test_disable_negated_services 'a,-a,b' 'b' | 
| Ian Wienand | 2796a82 | 2015-04-15 08:59:04 +1000 | [diff] [blame] | 146 | test_disable_negated_services 'a,aa,-a' 'aa' | 
|  | 147 | test_disable_negated_services 'aa,-a' 'aa' | 
|  | 148 | test_disable_negated_services 'a_a, -a_a' '' | 
|  | 149 | test_disable_negated_services 'a-b, -a-b' '' | 
|  | 150 | test_disable_negated_services 'a-b, b, -a-b' 'b' | 
|  | 151 | test_disable_negated_services 'a,-a,av2,b' 'av2,b' | 
|  | 152 | test_disable_negated_services 'a,aa,-a' 'aa' | 
|  | 153 | test_disable_negated_services 'a,av2,-a,a' 'av2' | 
|  | 154 | test_disable_negated_services 'a,-a,av2' 'av2' | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 155 |  | 
| fumihiko kakuma | 8606c98 | 2015-04-13 09:55:06 +0900 | [diff] [blame] | 156 | echo "Testing remove_disabled_services()" | 
|  | 157 |  | 
|  | 158 | function test_remove_disabled_services { | 
|  | 159 | local service_list="$1" | 
|  | 160 | local remove_list="$2" | 
|  | 161 | local expected="$3" | 
|  | 162 |  | 
|  | 163 | results=$(remove_disabled_services "$service_list" "$remove_list") | 
|  | 164 | if [ "$results" = "$expected" ]; then | 
|  | 165 | passed "OK: '$service_list' - '$remove_list' -> '$results'" | 
|  | 166 | else | 
|  | 167 | failed "getting '$expected' from '$service_list' - '$remove_list' failed: '$results'" | 
|  | 168 | fi | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | test_remove_disabled_services 'a,b,c' 'a,c' 'b' | 
|  | 172 | test_remove_disabled_services 'a,b,c' 'b' 'a,c' | 
|  | 173 | test_remove_disabled_services 'a,b,c,d' 'a,c d' 'b' | 
|  | 174 | test_remove_disabled_services 'a,b c,d' 'a d' 'b,c' | 
|  | 175 | test_remove_disabled_services 'a,b,c' 'a,b,c' '' | 
|  | 176 | test_remove_disabled_services 'a,b,c' 'd' 'a,b,c' | 
|  | 177 | test_remove_disabled_services 'a,b,c' '' 'a,b,c' | 
|  | 178 | test_remove_disabled_services '' 'a,b,c' '' | 
|  | 179 | test_remove_disabled_services '' '' '' | 
|  | 180 |  | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 181 | echo "Testing is_package_installed()" | 
|  | 182 |  | 
|  | 183 | if [[ -z "$os_PACKAGE" ]]; then | 
|  | 184 | GetOSVersion | 
|  | 185 | fi | 
|  | 186 |  | 
|  | 187 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
|  | 188 | is_package_installed dpkg | 
|  | 189 | VAL=$? | 
|  | 190 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
|  | 191 | is_package_installed rpm | 
|  | 192 | VAL=$? | 
|  | 193 | else | 
|  | 194 | VAL=1 | 
|  | 195 | fi | 
|  | 196 | if [[ "$VAL" -eq 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 197 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 198 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 199 | failed "is_package_installed() on existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 200 | fi | 
|  | 201 |  | 
|  | 202 | if [[ "$os_PACKAGE" = "deb" ]]; then | 
|  | 203 | is_package_installed dpkg bash | 
|  | 204 | VAL=$? | 
|  | 205 | elif [[ "$os_PACKAGE" = "rpm" ]]; then | 
|  | 206 | is_package_installed rpm bash | 
|  | 207 | VAL=$? | 
|  | 208 | else | 
|  | 209 | VAL=1 | 
|  | 210 | fi | 
|  | 211 | if [[ "$VAL" -eq 0 ]]; then | 
| 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 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 214 | failed "is_package_installed() on more than one existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 215 | fi | 
|  | 216 |  | 
|  | 217 | is_package_installed zzzZZZzzz | 
|  | 218 | VAL=$? | 
|  | 219 | if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 220 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 221 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 222 | failed "is_package_installed() on non-existing package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 223 | fi | 
|  | 224 |  | 
|  | 225 | # test against removed package...was a bug on Ubuntu | 
|  | 226 | if is_ubuntu; then | 
| Sean Dague | 4da0fa8 | 2017-04-13 08:56:44 -0400 | [diff] [blame] | 227 | PKG=cowsay-off | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 228 | if ! (dpkg -s $PKG >/dev/null 2>&1); then | 
|  | 229 | # it was never installed...set up the condition | 
|  | 230 | sudo apt-get install -y cowsay >/dev/null 2>&1 | 
|  | 231 | fi | 
|  | 232 | if (dpkg -s $PKG >/dev/null 2>&1); then | 
|  | 233 | # remove it to create the 'un' status | 
|  | 234 | sudo dpkg -P $PKG >/dev/null 2>&1 | 
|  | 235 | fi | 
|  | 236 |  | 
|  | 237 | # now test the installed check on a deleted package | 
|  | 238 | is_package_installed $PKG | 
|  | 239 | VAL=$? | 
|  | 240 | if [[ "$VAL" -ne 0 ]]; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 241 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 242 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 243 | failed "is_package_installed() on deleted package failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 244 | fi | 
|  | 245 | fi | 
|  | 246 |  | 
|  | 247 | # test isset function | 
|  | 248 | echo  "Testing isset()" | 
|  | 249 | you_should_not_have_this_variable=42 | 
|  | 250 |  | 
|  | 251 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 252 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 253 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 254 | failed "\"you_should_not_have_this_variable\" not declared. failed" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 255 | fi | 
|  | 256 |  | 
|  | 257 | unset you_should_not_have_this_variable | 
|  | 258 | if isset "you_should_not_have_this_variable"; then | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 259 | failed "\"you_should_not_have_this_variable\" looks like declared variable." | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 260 | else | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 261 | passed "OK" | 
| Ian Wienand | 9b0ebc4 | 2015-04-17 13:06:47 +1000 | [diff] [blame] | 262 | fi | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 263 |  | 
| Mahito OGURA | 54dc19e | 2015-07-14 17:16:42 +0900 | [diff] [blame] | 264 | function test_export_proxy_variables { | 
|  | 265 | echo "Testing export_proxy_variables()" | 
|  | 266 |  | 
|  | 267 | local expected results | 
|  | 268 |  | 
|  | 269 | http_proxy=http_proxy_test | 
|  | 270 | https_proxy=https_proxy_test | 
|  | 271 | no_proxy=no_proxy_test | 
|  | 272 |  | 
|  | 273 | export_proxy_variables | 
|  | 274 | expected=$(echo -e "http_proxy=$http_proxy\nhttps_proxy=$https_proxy\nno_proxy=$no_proxy") | 
| whoami-rajat | deadc7c | 2019-03-14 11:01:18 +0530 | [diff] [blame] | 275 | results=$(env | egrep '(http(s)?|no)_proxy=' | sort) | 
| Mahito OGURA | 54dc19e | 2015-07-14 17:16:42 +0900 | [diff] [blame] | 276 | if [[ $expected = $results ]]; then | 
|  | 277 | passed "OK: Proxy variables are exported when proxy variables are set" | 
|  | 278 | else | 
|  | 279 | failed "Expected: $expected, Failed: $results" | 
|  | 280 | fi | 
|  | 281 |  | 
|  | 282 | unset http_proxy https_proxy no_proxy | 
|  | 283 | export_proxy_variables | 
|  | 284 | results=$(env | egrep '(http(s)?|no)_proxy=') | 
|  | 285 | if [[ "" = $results ]]; then | 
|  | 286 | passed "OK: Proxy variables aren't exported when proxy variables aren't set" | 
|  | 287 | else | 
|  | 288 | failed "Expected: '', Failed: $results" | 
|  | 289 | fi | 
|  | 290 | } | 
|  | 291 | test_export_proxy_variables | 
|  | 292 |  | 
| Ian Wienand | 09f4ad2 | 2015-04-17 13:13:04 +1000 | [diff] [blame] | 293 | report_results |