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