blob: 7d486d4cc52db937c331b3c6aecb63bd5dee9318 [file] [log] [blame]
Dean Troyer489bd2a2012-03-02 10:44:29 -06001#!/usr/bin/env bash
2
3# Tests for DevStack functions
4
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
9
10# Import configuration
11source $TOP/openrc
12
13
Dean Troyer489bd2a2012-03-02 10:44:29 -060014echo "Testing die_if_not_set()"
15
Dean Troyer27e32692012-03-16 16:16:56 -050016bash -cx "source $TOP/functions; X=`echo Y && true`; die_if_not_set X 'not OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060017if [[ $? != 0 ]]; then
18 echo "die_if_not_set [X='Y' true] Failed"
19else
20 echo 'OK'
21fi
22
Dean Troyer27e32692012-03-16 16:16:56 -050023bash -cx "source $TOP/functions; X=`true`; die_if_not_set X 'OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060024if [[ $? = 0 ]]; then
25 echo "die_if_not_set [X='' true] Failed"
26fi
27
Dean Troyer27e32692012-03-16 16:16:56 -050028bash -cx "source $TOP/functions; X=`echo Y && false`; die_if_not_set X 'not OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060029if [[ $? != 0 ]]; then
30 echo "die_if_not_set [X='Y' false] Failed"
31else
32 echo 'OK'
33fi
34
Dean Troyer27e32692012-03-16 16:16:56 -050035bash -cx "source $TOP/functions; X=`false`; die_if_not_set X 'OK'"
Dean Troyer489bd2a2012-03-02 10:44:29 -060036if [[ $? = 0 ]]; then
37 echo "die_if_not_set [X='' false] Failed"
38fi
39
Dean Troyer13dc5cc2012-03-27 14:50:45 -050040
41echo "Testing INI functions"
42
43cat >test.ini <<EOF
44[default]
45# comment an option
46#log_file=./log.conf
47log_file=/etc/log.conf
48handlers=do not disturb
49
50[aaa]
51# the commented option should not change
52#handlers=cc,dd
53handlers = aa, bb
54
55[bbb]
56handlers=ee,ff
Dean Troyere8335622012-11-27 17:00:11 -060057
58[ ccc ]
59spaces = yes
Attila Fazekas588eb412012-12-20 10:57:16 +010060
61[ddd]
62empty =
Lianhao Lu239f3242013-03-01 15:54:02 +080063
64[eee]
65multi = foo1
66multi = foo2
Dean Troyer13dc5cc2012-03-27 14:50:45 -050067EOF
68
69# Test with spaces
70
71VAL=$(iniget test.ini aaa handlers)
72if [[ "$VAL" == "aa, bb" ]]; then
73 echo "OK: $VAL"
74else
75 echo "iniget failed: $VAL"
76fi
77
78iniset test.ini aaa handlers "11, 22"
79
80VAL=$(iniget test.ini aaa handlers)
81if [[ "$VAL" == "11, 22" ]]; then
82 echo "OK: $VAL"
83else
84 echo "iniget failed: $VAL"
85fi
86
Dean Troyere8335622012-11-27 17:00:11 -060087# Test with spaces in section header
88
Attila Fazekas588eb412012-12-20 10:57:16 +010089VAL=$(iniget test.ini " ccc " spaces)
Dean Troyere8335622012-11-27 17:00:11 -060090if [[ "$VAL" == "yes" ]]; then
91 echo "OK: $VAL"
92else
93 echo "iniget failed: $VAL"
94fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -050095
Attila Fazekas588eb412012-12-20 10:57:16 +010096iniset test.ini "b b" opt_ion 42
97
98VAL=$(iniget test.ini "b b" opt_ion)
99if [[ "$VAL" == "42" ]]; then
100 echo "OK: $VAL"
101else
102 echo "iniget failed: $VAL"
103fi
104
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500105# Test without spaces, end of file
106
107VAL=$(iniget test.ini bbb handlers)
108if [[ "$VAL" == "ee,ff" ]]; then
109 echo "OK: $VAL"
110else
111 echo "iniget failed: $VAL"
112fi
113
114iniset test.ini bbb handlers "33,44"
115
116VAL=$(iniget test.ini bbb handlers)
117if [[ "$VAL" == "33,44" ]]; then
118 echo "OK: $VAL"
119else
120 echo "iniget failed: $VAL"
121fi
122
Attila Fazekas588eb412012-12-20 10:57:16 +0100123# test empty option
124if ini_has_option test.ini ddd empty; then
125 echo "OK: ddd.empty present"
126else
127 echo "ini_has_option failed: ddd.empty not found"
128fi
129
130# test non-empty option
131if ini_has_option test.ini bbb handlers; then
132 echo "OK: bbb.handlers present"
133else
134 echo "ini_has_option failed: bbb.handlers not found"
135fi
136
137# test changing empty option
138iniset test.ini ddd empty "42"
139
140VAL=$(iniget test.ini ddd empty)
141if [[ "$VAL" == "42" ]]; then
142 echo "OK: $VAL"
143else
144 echo "iniget failed: $VAL"
145fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500146
147# Test section not exist
148
149VAL=$(iniget test.ini zzz handlers)
150if [[ -z "$VAL" ]]; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500151 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500152else
153 echo "iniget failed: $VAL"
154fi
155
156iniset test.ini zzz handlers "999"
157
158VAL=$(iniget test.ini zzz handlers)
Dean Troyer09e636e2012-03-19 16:31:12 -0500159if [[ -n "$VAL" ]]; then
160 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500161else
162 echo "iniget failed: $VAL"
163fi
164
Dean Troyer09e636e2012-03-19 16:31:12 -0500165# Test option not exist
166
167VAL=$(iniget test.ini aaa debug)
168if [[ -z "$VAL" ]]; then
169 echo "OK aaa.debug not present"
170else
171 echo "iniget failed: $VAL"
172fi
173
Attila Fazekas588eb412012-12-20 10:57:16 +0100174if ! ini_has_option test.ini aaa debug; then
175 echo "OK aaa.debug not present"
176else
177 echo "ini_has_option failed: aaa.debug"
178fi
179
Dean Troyer09e636e2012-03-19 16:31:12 -0500180iniset test.ini aaa debug "999"
181
182VAL=$(iniget test.ini aaa debug)
183if [[ -n "$VAL" ]]; then
184 echo "OK aaa.debug present"
185else
186 echo "iniget failed: $VAL"
187fi
188
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500189# Test comments
190
191inicomment test.ini aaa handlers
192
193VAL=$(iniget test.ini aaa handlers)
194if [[ -z "$VAL" ]]; then
195 echo "OK"
196else
197 echo "inicomment failed: $VAL"
198fi
Vincent Untzbf392312012-06-13 11:26:31 +0200199
Lianhao Lu239f3242013-03-01 15:54:02 +0800200# Test multiple line iniset/iniget
201iniset_multiline test.ini eee multi bar1 bar2
202
203VAL=$(iniget_multiline test.ini eee multi)
204if [[ "$VAL" == "bar1 bar2" ]]; then
205 echo "OK: iniset_multiline"
206else
207 echo "iniset_multiline failed: $VAL"
208fi
209
210# Test iniadd with exiting values
211iniadd test.ini eee multi bar3
212VAL=$(iniget_multiline test.ini eee multi)
213if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
214 echo "OK: iniadd"
215else
216 echo "iniadd failed: $VAL"
217fi
218
219# Test iniadd with non-exiting values
220iniadd test.ini eee non-multi foobar1 foobar2
221VAL=$(iniget_multiline test.ini eee non-multi)
222if [[ "$VAL" == "foobar1 foobar2" ]]; then
223 echo "OK: iniadd with non-exiting value"
224else
225 echo "iniadd with non-exsting failed: $VAL"
226fi
227
Vincent Untzbf392312012-06-13 11:26:31 +0200228rm test.ini
Doug Hellmannf04178f2012-07-05 17:10:03 -0400229
230# Enabling/disabling services
231
232echo "Testing enable_service()"
233
234function test_enable_service() {
235 local start="$1"
236 local add="$2"
237 local finish="$3"
238
239 ENABLED_SERVICES="$start"
240 enable_service $add
241 if [ "$ENABLED_SERVICES" = "$finish" ]
242 then
243 echo "OK: $start + $add -> $ENABLED_SERVICES"
244 else
245 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
246 fi
247}
248
249test_enable_service '' a 'a'
250test_enable_service 'a' b 'a,b'
251test_enable_service 'a,b' c 'a,b,c'
252test_enable_service 'a,b' c 'a,b,c'
253test_enable_service 'a,b,' c 'a,b,c'
254test_enable_service 'a,b' c,d 'a,b,c,d'
255test_enable_service 'a,b' "c d" 'a,b,c,d'
256test_enable_service 'a,b,c' c 'a,b,c'
257
258test_enable_service 'a,b,-c' c 'a,b'
259test_enable_service 'a,b,c' -c 'a,b'
260
261function test_disable_service() {
262 local start="$1"
263 local del="$2"
264 local finish="$3"
265
266 ENABLED_SERVICES="$start"
267 disable_service "$del"
268 if [ "$ENABLED_SERVICES" = "$finish" ]
269 then
270 echo "OK: $start - $del -> $ENABLED_SERVICES"
271 else
272 echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
273 fi
274}
275
276echo "Testing disable_service()"
277test_disable_service 'a,b,c' a 'b,c'
278test_disable_service 'a,b,c' b 'a,c'
279test_disable_service 'a,b,c' c 'a,b'
280
281test_disable_service 'a,b,c' a 'b,c'
282test_disable_service 'b,c' b 'c'
283test_disable_service 'c' c ''
284test_disable_service '' d ''
285
286test_disable_service 'a,b,c,' c 'a,b'
287test_disable_service 'a,b' c 'a,b'
288
289
290echo "Testing disable_all_services()"
291ENABLED_SERVICES=a,b,c
292disable_all_services
293
294if [[ -z "$ENABLED_SERVICES" ]]
295then
296 echo "OK"
297else
298 echo "disabling all services FAILED: $ENABLED_SERVICES"
299fi
300
301echo "Testing disable_negated_services()"
302
303
304function test_disable_negated_services() {
305 local start="$1"
306 local finish="$2"
307
308 ENABLED_SERVICES="$start"
309 disable_negated_services
310 if [ "$ENABLED_SERVICES" = "$finish" ]
311 then
312 echo "OK: $start + $add -> $ENABLED_SERVICES"
313 else
314 echo "changing $start to $finish failed: $ENABLED_SERVICES"
315 fi
316}
317
318test_disable_negated_services '-a' ''
319test_disable_negated_services '-a,a' ''
320test_disable_negated_services '-a,-a' ''
321test_disable_negated_services 'a,-a' ''
322test_disable_negated_services 'b,a,-a' 'b'
323test_disable_negated_services 'a,b,-a' 'b'
324test_disable_negated_services 'a,-a,b' 'b'
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200325
326
327echo "Testing is_package_installed()"
328
329if [[ -z "$os_PACKAGE" ]]; then
330 GetOSVersion
331fi
332
333if [[ "$os_PACKAGE" = "deb" ]]; then
334 is_package_installed dpkg
335 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100336elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200337 is_package_installed rpm
338 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100339else
340 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200341fi
342if [[ "$VAL" -eq 0 ]]; then
343 echo "OK"
344else
345 echo "is_package_installed() on existing package failed"
346fi
347
348if [[ "$os_PACKAGE" = "deb" ]]; then
349 is_package_installed dpkg bash
350 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100351elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200352 is_package_installed rpm bash
353 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100354else
355 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200356fi
357if [[ "$VAL" -eq 0 ]]; then
358 echo "OK"
359else
360 echo "is_package_installed() on more than one existing package failed"
361fi
362
363is_package_installed zzzZZZzzz
364VAL=$?
365if [[ "$VAL" -ne 0 ]]; then
366 echo "OK"
367else
368 echo "is_package_installed() on non-existing package failed"
369fi
Dean Troyer04762cd2013-08-27 17:06:14 -0500370
371# test against removed package...was a bug on Ubuntu
372if is_ubuntu; then
373 PKG=cowsay
374 if ! (dpkg -s $PKG >/dev/null 2>&1); then
375 # it was never installed...set up the condition
376 sudo apt-get install -y cowsay >/dev/null 2>&1
377 fi
378 if (dpkg -s $PKG >/dev/null 2>&1); then
379 # remove it to create the 'un' status
380 sudo dpkg -P $PKG >/dev/null 2>&1
381 fi
382
383 # now test the installed check on a deleted package
384 is_package_installed $PKG
385 VAL=$?
386 if [[ "$VAL" -ne 0 ]]; then
387 echo "OK"
388 else
389 echo "is_package_installed() on deleted package failed"
390 fi
391fi