blob: adf20cdb804721e6207617d82419d52cc544e413 [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 Wienand09f4ad22015-04-17 13:13:04 +100010source $TOP/tests/unittest.sh
Ian Wienand9b0ebc42015-04-17 13:06:47 +100011
Mark Vanderwield99c2902016-03-17 12:19:16 -050012echo "Testing generate_hex_string()"
13
14VAL=$(generate_hex_string 16)
15if [[ ${#VAL} -eq 32 ]]; then
16 passed "OK"
17else
18 failed "generate_hex_string 16 failed ${#VAL}"
19fi
20
21VAL=$(generate_hex_string 32)
22if [[ ${#VAL} -eq 64 ]]; then
23 passed "OK"
24else
25 failed "generate_hex_string 32 failed ${#VAL}"
26fi
27
Ian Wienand9b0ebc42015-04-17 13:06:47 +100028echo "Testing die_if_not_set()"
29
Ian Wienand9b845da2015-04-17 13:10:33 +100030bash -c "source $TOP/functions; X=`echo Y && true`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100031if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100032 failed "die_if_not_set [X='Y' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100033else
Ian Wienand09f4ad22015-04-17 13:13:04 +100034 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100035fi
36
Ian Wienand9b845da2015-04-17 13:10:33 +100037bash -c "source $TOP/functions; X=`true`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100038if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100039 failed "die_if_not_set [X='' true] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100040fi
41
Ian Wienand9b845da2015-04-17 13:10:33 +100042bash -c "source $TOP/functions; X=`echo Y && false`; die_if_not_set $LINENO X 'not OK'"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100043if [[ $? != 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100044 failed "die_if_not_set [X='Y' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100045else
Ian Wienand09f4ad22015-04-17 13:13:04 +100046 passed 'OK'
Ian Wienand9b0ebc42015-04-17 13:06:47 +100047fi
48
Ian Wienand9b845da2015-04-17 13:10:33 +100049bash -c "source $TOP/functions; X=`false`; die_if_not_set $LINENO X 'OK'" > /dev/null 2>&1
Ian Wienand9b0ebc42015-04-17 13:06:47 +100050if [[ $? = 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +100051 failed "die_if_not_set [X='' false] Failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100052fi
53
54
55# Enabling/disabling services
56
57echo "Testing enable_service()"
58
59function 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 Wienand09f4ad22015-04-17 13:13:04 +100067 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100068 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100069 failed "changing $start to $finish with $add failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100070 fi
Sean Dague53753292014-12-04 19:38:15 -050071}
72
Ian Wienand9b0ebc42015-04-17 13:06:47 +100073test_enable_service '' a 'a'
74test_enable_service 'a' b 'a,b'
75test_enable_service 'a,b' c 'a,b,c'
76test_enable_service 'a,b' c 'a,b,c'
77test_enable_service 'a,b,' c 'a,b,c'
78test_enable_service 'a,b' c,d 'a,b,c,d'
79test_enable_service 'a,b' "c d" 'a,b,c,d'
80test_enable_service 'a,b,c' c 'a,b,c'
Sean Dague53753292014-12-04 19:38:15 -050081
Ian Wienand9b0ebc42015-04-17 13:06:47 +100082test_enable_service 'a,b,-c' c 'a,b'
83test_enable_service 'a,b,c' -c 'a,b'
84
85function 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 Wienand09f4ad22015-04-17 13:13:04 +100093 passed "OK: $start - $del -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100094 else
Ian Wienand09f4ad22015-04-17 13:13:04 +100095 failed "changing $start to $finish with $del failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +100096 fi
97}
98
99echo "Testing disable_service()"
100test_disable_service 'a,b,c' a 'b,c'
101test_disable_service 'a,b,c' b 'a,c'
102test_disable_service 'a,b,c' c 'a,b'
103
104test_disable_service 'a,b,c' a 'b,c'
105test_disable_service 'b,c' b 'c'
106test_disable_service 'c' c ''
107test_disable_service '' d ''
108
109test_disable_service 'a,b,c,' c 'a,b'
110test_disable_service 'a,b' c 'a,b'
111
112
113echo "Testing disable_all_services()"
114ENABLED_SERVICES=a,b,c
115disable_all_services
116
117if [[ -z "$ENABLED_SERVICES" ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000118 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000119else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000120 failed "disabling all services FAILED: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000121fi
122
123echo "Testing disable_negated_services()"
124
125
126function 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 Wienand09f4ad22015-04-17 13:13:04 +1000133 passed "OK: $start + $add -> $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000134 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000135 failed "changing $start to $finish failed: $ENABLED_SERVICES"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000136 fi
137}
138
139test_disable_negated_services '-a' ''
140test_disable_negated_services '-a,a' ''
141test_disable_negated_services '-a,-a' ''
142test_disable_negated_services 'a,-a' ''
143test_disable_negated_services 'b,a,-a' 'b'
144test_disable_negated_services 'a,b,-a' 'b'
145test_disable_negated_services 'a,-a,b' 'b'
Ian Wienand2796a822015-04-15 08:59:04 +1000146test_disable_negated_services 'a,aa,-a' 'aa'
147test_disable_negated_services 'aa,-a' 'aa'
148test_disable_negated_services 'a_a, -a_a' ''
149test_disable_negated_services 'a-b, -a-b' ''
150test_disable_negated_services 'a-b, b, -a-b' 'b'
151test_disable_negated_services 'a,-a,av2,b' 'av2,b'
152test_disable_negated_services 'a,aa,-a' 'aa'
153test_disable_negated_services 'a,av2,-a,a' 'av2'
154test_disable_negated_services 'a,-a,av2' 'av2'
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000155
fumihiko kakuma8606c982015-04-13 09:55:06 +0900156echo "Testing remove_disabled_services()"
157
158function 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
171test_remove_disabled_services 'a,b,c' 'a,c' 'b'
172test_remove_disabled_services 'a,b,c' 'b' 'a,c'
173test_remove_disabled_services 'a,b,c,d' 'a,c d' 'b'
174test_remove_disabled_services 'a,b c,d' 'a d' 'b,c'
175test_remove_disabled_services 'a,b,c' 'a,b,c' ''
176test_remove_disabled_services 'a,b,c' 'd' 'a,b,c'
177test_remove_disabled_services 'a,b,c' '' 'a,b,c'
178test_remove_disabled_services '' 'a,b,c' ''
179test_remove_disabled_services '' '' ''
180
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000181echo "Testing is_package_installed()"
182
183if [[ -z "$os_PACKAGE" ]]; then
184 GetOSVersion
185fi
186
187if [[ "$os_PACKAGE" = "deb" ]]; then
188 is_package_installed dpkg
189 VAL=$?
190elif [[ "$os_PACKAGE" = "rpm" ]]; then
191 is_package_installed rpm
192 VAL=$?
193else
194 VAL=1
195fi
196if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000197 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000198else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000199 failed "is_package_installed() on existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000200fi
201
202if [[ "$os_PACKAGE" = "deb" ]]; then
203 is_package_installed dpkg bash
204 VAL=$?
205elif [[ "$os_PACKAGE" = "rpm" ]]; then
206 is_package_installed rpm bash
207 VAL=$?
208else
209 VAL=1
210fi
211if [[ "$VAL" -eq 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000212 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000213else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000214 failed "is_package_installed() on more than one existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000215fi
216
217is_package_installed zzzZZZzzz
218VAL=$?
219if [[ "$VAL" -ne 0 ]]; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000220 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000221else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000222 failed "is_package_installed() on non-existing package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000223fi
224
225# test against removed package...was a bug on Ubuntu
226if is_ubuntu; then
Sean Dague4da0fa82017-04-13 08:56:44 -0400227 PKG=cowsay-off
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000228 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 Wienand09f4ad22015-04-17 13:13:04 +1000241 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000242 else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000243 failed "is_package_installed() on deleted package failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000244 fi
245fi
246
247# test isset function
248echo "Testing isset()"
249you_should_not_have_this_variable=42
250
251if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000252 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000253else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000254 failed "\"you_should_not_have_this_variable\" not declared. failed"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000255fi
256
257unset you_should_not_have_this_variable
258if isset "you_should_not_have_this_variable"; then
Ian Wienand09f4ad22015-04-17 13:13:04 +1000259 failed "\"you_should_not_have_this_variable\" looks like declared variable."
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000260else
Ian Wienand09f4ad22015-04-17 13:13:04 +1000261 passed "OK"
Ian Wienand9b0ebc42015-04-17 13:06:47 +1000262fi
Ian Wienand09f4ad22015-04-17 13:13:04 +1000263
Mahito OGURA54dc19e2015-07-14 17:16:42 +0900264function 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")
275 results=$(env | egrep '(http(s)?|no)_proxy=')
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}
291test_export_proxy_variables
292
Ian Wienand09f4ad22015-04-17 13:13:04 +1000293report_results