blob: a7914f7a1b99b2f9715903e30256509a2d81115f [file] [log] [blame]
Sean Dague53753292014-12-04 19:38:15 -05001#!/usr/bin/env bash
2
Ian Wienand9b0ebc42015-04-17 13:06:47 +10003# Tests for DevStack functions
Sean Dague53753292014-12-04 19:38:15 -05004
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
Sean Dague53753292014-12-04 19:38:15 -05009
Ian Wienand9b0ebc42015-04-17 13:06:47 +100010# Import configuration
11source $TOP/openrc
12
Ian Wienand09f4ad22015-04-17 13:13:04 +100013source $TOP/tests/unittest.sh
Ian Wienand9b0ebc42015-04-17 13:06:47 +100014
15echo "Testing die_if_not_set()"
16
Ian Wienand9b845da2015-04-17 13:10:33 +100017bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100018if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100019 failed "die_if_not_set [X='Y' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100020else
Ian Wienand09f4ad22015-04-17 13:13:04 +100021 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100022fi
23
Ian Wienand9b845da2015-04-17 13:10:33 +100024bash -c "source $TOP/functions; X=`true`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100025if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100026 failed "die_if_not_set [X='' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100027fi
28
Ian Wienand9b845da2015-04-17 13:10:33 +100029bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100030if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100031 failed "die_if_not_set [X='Y' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100032else
Ian Wienand09f4ad22015-04-17 13:13:04 +100033 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100034fi
35
Ian Wienand9b845da2015-04-17 13:10:33 +100036bash -c "source $TOP/functions; X=`false`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100037if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100038 failed "die_if_not_set [X='' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100039fi
40
41
42# Enabling/disabling services
43
44echo "Testing enable_service()"
45
46function 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 Wienand09f4ad22015-04-17 13:13:04 +100054 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100055 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100056 failed "changing $start to $finish with $add failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100057 fi
Sean Dague53753292014-12-04 19:38:15 -050058}
59
Ian Wienand9b0ebc42015-04-17 13:06:47 +100060test_enable_service '' a 'a'
61test_enable_service 'a' b 'a,b'
62test_enable_service 'a,b' c 'a,b,c'
63test_enable_service 'a,b' c 'a,b,c'
64test_enable_service 'a,b,' c 'a,b,c'
65test_enable_service 'a,b' c,d 'a,b,c,d'
66test_enable_service 'a,b' "c d" 'a,b,c,d'
67test_enable_service 'a,b,c' c 'a,b,c'
Sean Dague53753292014-12-04 19:38:15 -050068
Ian Wienand9b0ebc42015-04-17 13:06:47 +100069test_enable_service 'a,b,-c' c 'a,b'
70test_enable_service 'a,b,c' -c 'a,b'
71
72function 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 Wienand09f4ad22015-04-17 13:13:04 +100080 passed "OK: $start - $del -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100081 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100082 failed "changing $start to $finish with $del failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100083 fi
84}
85
86echo "Testing disable_service()"
87test_disable_service 'a,b,c' a 'b,c'
88test_disable_service 'a,b,c' b 'a,c'
89test_disable_service 'a,b,c' c 'a,b'
90
91test_disable_service 'a,b,c' a 'b,c'
92test_disable_service 'b,c' b 'c'
93test_disable_service 'c' c ''
94test_disable_service '' d ''
95
96test_disable_service 'a,b,c,' c 'a,b'
97test_disable_service 'a,b' c 'a,b'
98
99
100echo "Testing disable_all_services()"
101ENABLED_SERVICES=a,b,c
102disable_all_services
103
104if [[ -z "$ENABLED_SERVICES" ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000105 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000106else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000107 failed "disabling all services FAILED: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000108fi
109
110echo "Testing disable_negated_services()"
111
112
113function 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 Wienand09f4ad22015-04-17 13:13:04 +1000120 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000121 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000122 failed "changing $start to $finish failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000123 fi
124}
125
126test_disable_negated_services '-a' ''
127test_disable_negated_services '-a,a' ''
128test_disable_negated_services '-a,-a' ''
129test_disable_negated_services 'a,-a' ''
130test_disable_negated_services 'b,a,-a' 'b'
131test_disable_negated_services 'a,b,-a' 'b'
132test_disable_negated_services 'a,-a,b' 'b'
133
134
135echo "Testing is_package_installed()"
136
137if [[ -z "$os_PACKAGE" ]]; then
138 GetOSVersion
139fi
140
141if [[ "$os_PACKAGE" = "deb" ]]; then
142 is_package_installed dpkg
143 VAL=$?
144elif [[ "$os_PACKAGE" = "rpm" ]]; then
145 is_package_installed rpm
146 VAL=$?
147else
148 VAL=1
149fi
150if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000151 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000152else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000153 failed "is_package_installed() on existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000154fi
155
156if [[ "$os_PACKAGE" = "deb" ]]; then
157 is_package_installed dpkg bash
158 VAL=$?
159elif [[ "$os_PACKAGE" = "rpm" ]]; then
160 is_package_installed rpm bash
161 VAL=$?
162else
163 VAL=1
164fi
165if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000166 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000167else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000168 failed "is_package_installed() on more than one existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000169fi
170
171is_package_installed zzzZZZzzz
172VAL=$?
173if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000174 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000175else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000176 failed "is_package_installed() on non-existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000177fi
178
179# test against removed package...was a bug on Ubuntu
180if is_ubuntu; then
181 PKG=cowsay
182 if ! (dpkg -s $PKG >/dev/null 2>&1); then
183 # it was never installed...set up the condition
184 sudo apt-get install -y cowsay >/dev/null 2>&1
185 fi
186 if (dpkg -s $PKG >/dev/null 2>&1); then
187 # remove it to create the 'un' status
188 sudo dpkg -P $PKG >/dev/null 2>&1
189 fi
190
191 # now test the installed check on a deleted package
192 is_package_installed $PKG
193 VAL=$?
194 if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000195 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000196 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000197 failed "is_package_installed() on deleted package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000198 fi
199fi
200
201# test isset function
202echo "Testing isset()"
203you_should_not_have_this_variable=42
204
205if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000206 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000207else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000208 failed "\"you_should_not_have_this_variable\" not declared. failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000209fi
210
211unset you_should_not_have_this_variable
212if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000213 failed "\"you_should_not_have_this_variable\" looks like declared variable."
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000214else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000215 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000216fi
Ian Wienand09f4ad22015-04-17 13:13:04 +1000217
218report_results