blob: 4fe644367d2ce924d96bf4f44d1ddd48b33b2d48 [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 =
Dean Troyer13dc5cc2012-03-27 14:50:45 -050063EOF
64
65# Test with spaces
66
67VAL=$(iniget test.ini aaa handlers)
68if [[ "$VAL" == "aa, bb" ]]; then
69 echo "OK: $VAL"
70else
71 echo "iniget failed: $VAL"
72fi
73
74iniset test.ini aaa handlers "11, 22"
75
76VAL=$(iniget test.ini aaa handlers)
77if [[ "$VAL" == "11, 22" ]]; then
78 echo "OK: $VAL"
79else
80 echo "iniget failed: $VAL"
81fi
82
Dean Troyere8335622012-11-27 17:00:11 -060083# Test with spaces in section header
84
Attila Fazekas588eb412012-12-20 10:57:16 +010085VAL=$(iniget test.ini " ccc " spaces)
Dean Troyere8335622012-11-27 17:00:11 -060086if [[ "$VAL" == "yes" ]]; then
87 echo "OK: $VAL"
88else
89 echo "iniget failed: $VAL"
90fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -050091
Attila Fazekas588eb412012-12-20 10:57:16 +010092iniset test.ini "b b" opt_ion 42
93
94VAL=$(iniget test.ini "b b" opt_ion)
95if [[ "$VAL" == "42" ]]; then
96 echo "OK: $VAL"
97else
98 echo "iniget failed: $VAL"
99fi
100
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500101# Test without spaces, end of file
102
103VAL=$(iniget test.ini bbb handlers)
104if [[ "$VAL" == "ee,ff" ]]; then
105 echo "OK: $VAL"
106else
107 echo "iniget failed: $VAL"
108fi
109
110iniset test.ini bbb handlers "33,44"
111
112VAL=$(iniget test.ini bbb handlers)
113if [[ "$VAL" == "33,44" ]]; then
114 echo "OK: $VAL"
115else
116 echo "iniget failed: $VAL"
117fi
118
Attila Fazekas588eb412012-12-20 10:57:16 +0100119# test empty option
120if ini_has_option test.ini ddd empty; then
121 echo "OK: ddd.empty present"
122else
123 echo "ini_has_option failed: ddd.empty not found"
124fi
125
126# test non-empty option
127if ini_has_option test.ini bbb handlers; then
128 echo "OK: bbb.handlers present"
129else
130 echo "ini_has_option failed: bbb.handlers not found"
131fi
132
133# test changing empty option
134iniset test.ini ddd empty "42"
135
136VAL=$(iniget test.ini ddd empty)
137if [[ "$VAL" == "42" ]]; then
138 echo "OK: $VAL"
139else
140 echo "iniget failed: $VAL"
141fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500142
143# Test section not exist
144
145VAL=$(iniget test.ini zzz handlers)
146if [[ -z "$VAL" ]]; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500147 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500148else
149 echo "iniget failed: $VAL"
150fi
151
152iniset test.ini zzz handlers "999"
153
154VAL=$(iniget test.ini zzz handlers)
Dean Troyer09e636e2012-03-19 16:31:12 -0500155if [[ -n "$VAL" ]]; then
156 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500157else
158 echo "iniget failed: $VAL"
159fi
160
Dean Troyer09e636e2012-03-19 16:31:12 -0500161# Test option not exist
162
163VAL=$(iniget test.ini aaa debug)
164if [[ -z "$VAL" ]]; then
165 echo "OK aaa.debug not present"
166else
167 echo "iniget failed: $VAL"
168fi
169
Attila Fazekas588eb412012-12-20 10:57:16 +0100170if ! ini_has_option test.ini aaa debug; then
171 echo "OK aaa.debug not present"
172else
173 echo "ini_has_option failed: aaa.debug"
174fi
175
Dean Troyer09e636e2012-03-19 16:31:12 -0500176iniset test.ini aaa debug "999"
177
178VAL=$(iniget test.ini aaa debug)
179if [[ -n "$VAL" ]]; then
180 echo "OK aaa.debug present"
181else
182 echo "iniget failed: $VAL"
183fi
184
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500185# Test comments
186
187inicomment test.ini aaa handlers
188
189VAL=$(iniget test.ini aaa handlers)
190if [[ -z "$VAL" ]]; then
191 echo "OK"
192else
193 echo "inicomment failed: $VAL"
194fi
Vincent Untzbf392312012-06-13 11:26:31 +0200195
196rm test.ini
Doug Hellmannf04178f2012-07-05 17:10:03 -0400197
198# Enabling/disabling services
199
200echo "Testing enable_service()"
201
202function test_enable_service() {
203 local start="$1"
204 local add="$2"
205 local finish="$3"
206
207 ENABLED_SERVICES="$start"
208 enable_service $add
209 if [ "$ENABLED_SERVICES" = "$finish" ]
210 then
211 echo "OK: $start + $add -> $ENABLED_SERVICES"
212 else
213 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
214 fi
215}
216
217test_enable_service '' a 'a'
218test_enable_service 'a' b 'a,b'
219test_enable_service 'a,b' c 'a,b,c'
220test_enable_service 'a,b' c 'a,b,c'
221test_enable_service 'a,b,' c 'a,b,c'
222test_enable_service 'a,b' c,d 'a,b,c,d'
223test_enable_service 'a,b' "c d" 'a,b,c,d'
224test_enable_service 'a,b,c' c 'a,b,c'
225
226test_enable_service 'a,b,-c' c 'a,b'
227test_enable_service 'a,b,c' -c 'a,b'
228
229function test_disable_service() {
230 local start="$1"
231 local del="$2"
232 local finish="$3"
233
234 ENABLED_SERVICES="$start"
235 disable_service "$del"
236 if [ "$ENABLED_SERVICES" = "$finish" ]
237 then
238 echo "OK: $start - $del -> $ENABLED_SERVICES"
239 else
240 echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
241 fi
242}
243
244echo "Testing disable_service()"
245test_disable_service 'a,b,c' a 'b,c'
246test_disable_service 'a,b,c' b 'a,c'
247test_disable_service 'a,b,c' c 'a,b'
248
249test_disable_service 'a,b,c' a 'b,c'
250test_disable_service 'b,c' b 'c'
251test_disable_service 'c' c ''
252test_disable_service '' d ''
253
254test_disable_service 'a,b,c,' c 'a,b'
255test_disable_service 'a,b' c 'a,b'
256
257
258echo "Testing disable_all_services()"
259ENABLED_SERVICES=a,b,c
260disable_all_services
261
262if [[ -z "$ENABLED_SERVICES" ]]
263then
264 echo "OK"
265else
266 echo "disabling all services FAILED: $ENABLED_SERVICES"
267fi
268
269echo "Testing disable_negated_services()"
270
271
272function test_disable_negated_services() {
273 local start="$1"
274 local finish="$2"
275
276 ENABLED_SERVICES="$start"
277 disable_negated_services
278 if [ "$ENABLED_SERVICES" = "$finish" ]
279 then
280 echo "OK: $start + $add -> $ENABLED_SERVICES"
281 else
282 echo "changing $start to $finish failed: $ENABLED_SERVICES"
283 fi
284}
285
286test_disable_negated_services '-a' ''
287test_disable_negated_services '-a,a' ''
288test_disable_negated_services '-a,-a' ''
289test_disable_negated_services 'a,-a' ''
290test_disable_negated_services 'b,a,-a' 'b'
291test_disable_negated_services 'a,b,-a' 'b'
292test_disable_negated_services 'a,-a,b' 'b'
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200293
294
295echo "Testing is_package_installed()"
296
297if [[ -z "$os_PACKAGE" ]]; then
298 GetOSVersion
299fi
300
301if [[ "$os_PACKAGE" = "deb" ]]; then
302 is_package_installed dpkg
303 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100304elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200305 is_package_installed rpm
306 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100307else
308 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200309fi
310if [[ "$VAL" -eq 0 ]]; then
311 echo "OK"
312else
313 echo "is_package_installed() on existing package failed"
314fi
315
316if [[ "$os_PACKAGE" = "deb" ]]; then
317 is_package_installed dpkg bash
318 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100319elif [[ "$os_PACKAGE" = "rpm" ]]; then
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200320 is_package_installed rpm bash
321 VAL=$?
Vincent Untz00011c02012-12-06 09:56:32 +0100322else
323 VAL=1
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200324fi
325if [[ "$VAL" -eq 0 ]]; then
326 echo "OK"
327else
328 echo "is_package_installed() on more than one existing package failed"
329fi
330
331is_package_installed zzzZZZzzz
332VAL=$?
333if [[ "$VAL" -ne 0 ]]; then
334 echo "OK"
335else
336 echo "is_package_installed() on non-existing package failed"
337fi