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