Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Tests for DevStack functions |
| 4 | |
| 5 | TOP=$(cd $(dirname "$0")/.. && pwd) |
| 6 | |
| 7 | # Import common functions |
| 8 | source $TOP/functions |
| 9 | |
| 10 | # Import configuration |
| 11 | source $TOP/openrc |
| 12 | |
| 13 | |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 14 | echo "Testing die_if_not_set()" |
| 15 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 16 | bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 17 | if [[ $? != 0 ]]; then |
| 18 | echo "die_if_not_set [X='Y' true] Failed" |
| 19 | else |
| 20 | echo 'OK' |
| 21 | fi |
| 22 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 23 | bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 24 | if [[ $? = 0 ]]; then |
| 25 | echo "die_if_not_set [X='' true] Failed" |
| 26 | fi |
| 27 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 28 | bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 29 | if [[ $? != 0 ]]; then |
| 30 | echo "die_if_not_set [X='Y' false] Failed" |
| 31 | else |
| 32 | echo 'OK' |
| 33 | fi |
| 34 | |
Dean Troyer | 27e3269 | 2012-03-16 16:16:56 -0500 | [diff] [blame] | 35 | bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'" |
Dean Troyer | 489bd2a | 2012-03-02 10:44:29 -0600 | [diff] [blame] | 36 | if [[ $? = 0 ]]; then |
| 37 | echo "die_if_not_set [X='' false] Failed" |
| 38 | fi |
| 39 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 40 | |
| 41 | echo "Testing INI functions" |
| 42 | |
| 43 | cat >test.ini <<EOF |
| 44 | [default] |
| 45 | # comment an option |
| 46 | #log_file=./log.conf |
| 47 | log_file=/etc/log.conf |
| 48 | handlers=do not disturb |
| 49 | |
| 50 | [aaa] |
| 51 | # the commented option should not change |
| 52 | #handlers=cc,dd |
| 53 | handlers = aa, bb |
| 54 | |
| 55 | [bbb] |
| 56 | handlers=ee,ff |
| 57 | EOF |
| 58 | |
| 59 | # Test with spaces |
| 60 | |
| 61 | VAL=$(iniget test.ini aaa handlers) |
| 62 | if [[ "$VAL" == "aa, bb" ]]; then |
| 63 | echo "OK: $VAL" |
| 64 | else |
| 65 | echo "iniget failed: $VAL" |
| 66 | fi |
| 67 | |
| 68 | iniset test.ini aaa handlers "11, 22" |
| 69 | |
| 70 | VAL=$(iniget test.ini aaa handlers) |
| 71 | if [[ "$VAL" == "11, 22" ]]; then |
| 72 | echo "OK: $VAL" |
| 73 | else |
| 74 | echo "iniget failed: $VAL" |
| 75 | fi |
| 76 | |
| 77 | |
| 78 | # Test without spaces, end of file |
| 79 | |
| 80 | VAL=$(iniget test.ini bbb handlers) |
| 81 | if [[ "$VAL" == "ee,ff" ]]; then |
| 82 | echo "OK: $VAL" |
| 83 | else |
| 84 | echo "iniget failed: $VAL" |
| 85 | fi |
| 86 | |
| 87 | iniset test.ini bbb handlers "33,44" |
| 88 | |
| 89 | VAL=$(iniget test.ini bbb handlers) |
| 90 | if [[ "$VAL" == "33,44" ]]; then |
| 91 | echo "OK: $VAL" |
| 92 | else |
| 93 | echo "iniget failed: $VAL" |
| 94 | fi |
| 95 | |
| 96 | |
| 97 | # Test section not exist |
| 98 | |
| 99 | VAL=$(iniget test.ini zzz handlers) |
| 100 | if [[ -z "$VAL" ]]; then |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 101 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 102 | else |
| 103 | echo "iniget failed: $VAL" |
| 104 | fi |
| 105 | |
| 106 | iniset test.ini zzz handlers "999" |
| 107 | |
| 108 | VAL=$(iniget test.ini zzz handlers) |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 109 | if [[ -n "$VAL" ]]; then |
| 110 | echo "OK: zzz not present" |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 111 | else |
| 112 | echo "iniget failed: $VAL" |
| 113 | fi |
| 114 | |
| 115 | |
Dean Troyer | 09e636e | 2012-03-19 16:31:12 -0500 | [diff] [blame] | 116 | # Test option not exist |
| 117 | |
| 118 | VAL=$(iniget test.ini aaa debug) |
| 119 | if [[ -z "$VAL" ]]; then |
| 120 | echo "OK aaa.debug not present" |
| 121 | else |
| 122 | echo "iniget failed: $VAL" |
| 123 | fi |
| 124 | |
| 125 | iniset test.ini aaa debug "999" |
| 126 | |
| 127 | VAL=$(iniget test.ini aaa debug) |
| 128 | if [[ -n "$VAL" ]]; then |
| 129 | echo "OK aaa.debug present" |
| 130 | else |
| 131 | echo "iniget failed: $VAL" |
| 132 | fi |
| 133 | |
Dean Troyer | 13dc5cc | 2012-03-27 14:50:45 -0500 | [diff] [blame] | 134 | # Test comments |
| 135 | |
| 136 | inicomment test.ini aaa handlers |
| 137 | |
| 138 | VAL=$(iniget test.ini aaa handlers) |
| 139 | if [[ -z "$VAL" ]]; then |
| 140 | echo "OK" |
| 141 | else |
| 142 | echo "inicomment failed: $VAL" |
| 143 | fi |
Vincent Untz | bf39231 | 2012-06-13 11:26:31 +0200 | [diff] [blame] | 144 | |
| 145 | rm test.ini |
Doug Hellmann | f04178f | 2012-07-05 17:10:03 -0400 | [diff] [blame] | 146 | |
| 147 | # Enabling/disabling services |
| 148 | |
| 149 | echo "Testing enable_service()" |
| 150 | |
| 151 | function test_enable_service() { |
| 152 | local start="$1" |
| 153 | local add="$2" |
| 154 | local finish="$3" |
| 155 | |
| 156 | ENABLED_SERVICES="$start" |
| 157 | enable_service $add |
| 158 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 159 | then |
| 160 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 161 | else |
| 162 | echo "changing $start to $finish with $add failed: $ENABLED_SERVICES" |
| 163 | fi |
| 164 | } |
| 165 | |
| 166 | test_enable_service '' a 'a' |
| 167 | test_enable_service 'a' b 'a,b' |
| 168 | test_enable_service 'a,b' c 'a,b,c' |
| 169 | test_enable_service 'a,b' c 'a,b,c' |
| 170 | test_enable_service 'a,b,' c 'a,b,c' |
| 171 | test_enable_service 'a,b' c,d 'a,b,c,d' |
| 172 | test_enable_service 'a,b' "c d" 'a,b,c,d' |
| 173 | test_enable_service 'a,b,c' c 'a,b,c' |
| 174 | |
| 175 | test_enable_service 'a,b,-c' c 'a,b' |
| 176 | test_enable_service 'a,b,c' -c 'a,b' |
| 177 | |
| 178 | function test_disable_service() { |
| 179 | local start="$1" |
| 180 | local del="$2" |
| 181 | local finish="$3" |
| 182 | |
| 183 | ENABLED_SERVICES="$start" |
| 184 | disable_service "$del" |
| 185 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 186 | then |
| 187 | echo "OK: $start - $del -> $ENABLED_SERVICES" |
| 188 | else |
| 189 | echo "changing $start to $finish with $del failed: $ENABLED_SERVICES" |
| 190 | fi |
| 191 | } |
| 192 | |
| 193 | echo "Testing disable_service()" |
| 194 | test_disable_service 'a,b,c' a 'b,c' |
| 195 | test_disable_service 'a,b,c' b 'a,c' |
| 196 | test_disable_service 'a,b,c' c 'a,b' |
| 197 | |
| 198 | test_disable_service 'a,b,c' a 'b,c' |
| 199 | test_disable_service 'b,c' b 'c' |
| 200 | test_disable_service 'c' c '' |
| 201 | test_disable_service '' d '' |
| 202 | |
| 203 | test_disable_service 'a,b,c,' c 'a,b' |
| 204 | test_disable_service 'a,b' c 'a,b' |
| 205 | |
| 206 | |
| 207 | echo "Testing disable_all_services()" |
| 208 | ENABLED_SERVICES=a,b,c |
| 209 | disable_all_services |
| 210 | |
| 211 | if [[ -z "$ENABLED_SERVICES" ]] |
| 212 | then |
| 213 | echo "OK" |
| 214 | else |
| 215 | echo "disabling all services FAILED: $ENABLED_SERVICES" |
| 216 | fi |
| 217 | |
| 218 | echo "Testing disable_negated_services()" |
| 219 | |
| 220 | |
| 221 | function test_disable_negated_services() { |
| 222 | local start="$1" |
| 223 | local finish="$2" |
| 224 | |
| 225 | ENABLED_SERVICES="$start" |
| 226 | disable_negated_services |
| 227 | if [ "$ENABLED_SERVICES" = "$finish" ] |
| 228 | then |
| 229 | echo "OK: $start + $add -> $ENABLED_SERVICES" |
| 230 | else |
| 231 | echo "changing $start to $finish failed: $ENABLED_SERVICES" |
| 232 | fi |
| 233 | } |
| 234 | |
| 235 | test_disable_negated_services '-a' '' |
| 236 | test_disable_negated_services '-a,a' '' |
| 237 | test_disable_negated_services '-a,-a' '' |
| 238 | test_disable_negated_services 'a,-a' '' |
| 239 | test_disable_negated_services 'b,a,-a' 'b' |
| 240 | test_disable_negated_services 'a,b,-a' 'b' |
| 241 | test_disable_negated_services 'a,-a,b' 'b' |
Vincent Untz | 71ebc6f | 2012-06-12 13:45:15 +0200 | [diff] [blame] | 242 | |
| 243 | |
| 244 | echo "Testing is_package_installed()" |
| 245 | |
| 246 | if [[ -z "$os_PACKAGE" ]]; then |
| 247 | GetOSVersion |
| 248 | fi |
| 249 | |
| 250 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 251 | is_package_installed dpkg |
| 252 | VAL=$? |
| 253 | else |
| 254 | is_package_installed rpm |
| 255 | VAL=$? |
| 256 | fi |
| 257 | if [[ "$VAL" -eq 0 ]]; then |
| 258 | echo "OK" |
| 259 | else |
| 260 | echo "is_package_installed() on existing package failed" |
| 261 | fi |
| 262 | |
| 263 | if [[ "$os_PACKAGE" = "deb" ]]; then |
| 264 | is_package_installed dpkg bash |
| 265 | VAL=$? |
| 266 | else |
| 267 | is_package_installed rpm bash |
| 268 | VAL=$? |
| 269 | fi |
| 270 | if [[ "$VAL" -eq 0 ]]; then |
| 271 | echo "OK" |
| 272 | else |
| 273 | echo "is_package_installed() on more than one existing package failed" |
| 274 | fi |
| 275 | |
| 276 | is_package_installed zzzZZZzzz |
| 277 | VAL=$? |
| 278 | if [[ "$VAL" -ne 0 ]]; then |
| 279 | echo "OK" |
| 280 | else |
| 281 | echo "is_package_installed() on non-existing package failed" |
| 282 | fi |