blob: d2cc5c4438cb4b8e6445b476ec889ef3783d0816 [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
Dean Troyer13dc5cc2012-03-27 14:50:45 -050060EOF
61
62# Test with spaces
63
64VAL=$(iniget test.ini aaa handlers)
65if [[ "$VAL" == "aa, bb" ]]; then
66 echo "OK: $VAL"
67else
68 echo "iniget failed: $VAL"
69fi
70
71iniset test.ini aaa handlers "11, 22"
72
73VAL=$(iniget test.ini aaa handlers)
74if [[ "$VAL" == "11, 22" ]]; then
75 echo "OK: $VAL"
76else
77 echo "iniget failed: $VAL"
78fi
79
Dean Troyere8335622012-11-27 17:00:11 -060080# Test with spaces in section header
81
82VAL=$(iniget test.ini ccc spaces)
83if [[ "$VAL" == "yes" ]]; then
84 echo "OK: $VAL"
85else
86 echo "iniget failed: $VAL"
87fi
Dean Troyer13dc5cc2012-03-27 14:50:45 -050088
89# Test without spaces, end of file
90
91VAL=$(iniget test.ini bbb handlers)
92if [[ "$VAL" == "ee,ff" ]]; then
93 echo "OK: $VAL"
94else
95 echo "iniget failed: $VAL"
96fi
97
98iniset test.ini bbb handlers "33,44"
99
100VAL=$(iniget test.ini bbb handlers)
101if [[ "$VAL" == "33,44" ]]; then
102 echo "OK: $VAL"
103else
104 echo "iniget failed: $VAL"
105fi
106
107
108# Test section not exist
109
110VAL=$(iniget test.ini zzz handlers)
111if [[ -z "$VAL" ]]; then
Dean Troyer09e636e2012-03-19 16:31:12 -0500112 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500113else
114 echo "iniget failed: $VAL"
115fi
116
117iniset test.ini zzz handlers "999"
118
119VAL=$(iniget test.ini zzz handlers)
Dean Troyer09e636e2012-03-19 16:31:12 -0500120if [[ -n "$VAL" ]]; then
121 echo "OK: zzz not present"
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500122else
123 echo "iniget failed: $VAL"
124fi
125
Dean Troyer09e636e2012-03-19 16:31:12 -0500126# Test option not exist
127
128VAL=$(iniget test.ini aaa debug)
129if [[ -z "$VAL" ]]; then
130 echo "OK aaa.debug not present"
131else
132 echo "iniget failed: $VAL"
133fi
134
135iniset test.ini aaa debug "999"
136
137VAL=$(iniget test.ini aaa debug)
138if [[ -n "$VAL" ]]; then
139 echo "OK aaa.debug present"
140else
141 echo "iniget failed: $VAL"
142fi
143
Dean Troyer13dc5cc2012-03-27 14:50:45 -0500144# Test comments
145
146inicomment test.ini aaa handlers
147
148VAL=$(iniget test.ini aaa handlers)
149if [[ -z "$VAL" ]]; then
150 echo "OK"
151else
152 echo "inicomment failed: $VAL"
153fi
Vincent Untzbf392312012-06-13 11:26:31 +0200154
155rm test.ini
Doug Hellmannf04178f2012-07-05 17:10:03 -0400156
157# Enabling/disabling services
158
159echo "Testing enable_service()"
160
161function test_enable_service() {
162 local start="$1"
163 local add="$2"
164 local finish="$3"
165
166 ENABLED_SERVICES="$start"
167 enable_service $add
168 if [ "$ENABLED_SERVICES" = "$finish" ]
169 then
170 echo "OK: $start + $add -> $ENABLED_SERVICES"
171 else
172 echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
173 fi
174}
175
176test_enable_service '' a 'a'
177test_enable_service 'a' b 'a,b'
178test_enable_service 'a,b' c 'a,b,c'
179test_enable_service 'a,b' c 'a,b,c'
180test_enable_service 'a,b,' c 'a,b,c'
181test_enable_service 'a,b' c,d 'a,b,c,d'
182test_enable_service 'a,b' "c d" 'a,b,c,d'
183test_enable_service 'a,b,c' c 'a,b,c'
184
185test_enable_service 'a,b,-c' c 'a,b'
186test_enable_service 'a,b,c' -c 'a,b'
187
188function test_disable_service() {
189 local start="$1"
190 local del="$2"
191 local finish="$3"
192
193 ENABLED_SERVICES="$start"
194 disable_service "$del"
195 if [ "$ENABLED_SERVICES" = "$finish" ]
196 then
197 echo "OK: $start - $del -> $ENABLED_SERVICES"
198 else
199 echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
200 fi
201}
202
203echo "Testing disable_service()"
204test_disable_service 'a,b,c' a 'b,c'
205test_disable_service 'a,b,c' b 'a,c'
206test_disable_service 'a,b,c' c 'a,b'
207
208test_disable_service 'a,b,c' a 'b,c'
209test_disable_service 'b,c' b 'c'
210test_disable_service 'c' c ''
211test_disable_service '' d ''
212
213test_disable_service 'a,b,c,' c 'a,b'
214test_disable_service 'a,b' c 'a,b'
215
216
217echo "Testing disable_all_services()"
218ENABLED_SERVICES=a,b,c
219disable_all_services
220
221if [[ -z "$ENABLED_SERVICES" ]]
222then
223 echo "OK"
224else
225 echo "disabling all services FAILED: $ENABLED_SERVICES"
226fi
227
228echo "Testing disable_negated_services()"
229
230
231function test_disable_negated_services() {
232 local start="$1"
233 local finish="$2"
234
235 ENABLED_SERVICES="$start"
236 disable_negated_services
237 if [ "$ENABLED_SERVICES" = "$finish" ]
238 then
239 echo "OK: $start + $add -> $ENABLED_SERVICES"
240 else
241 echo "changing $start to $finish failed: $ENABLED_SERVICES"
242 fi
243}
244
245test_disable_negated_services '-a' ''
246test_disable_negated_services '-a,a' ''
247test_disable_negated_services '-a,-a' ''
248test_disable_negated_services 'a,-a' ''
249test_disable_negated_services 'b,a,-a' 'b'
250test_disable_negated_services 'a,b,-a' 'b'
251test_disable_negated_services 'a,-a,b' 'b'
Vincent Untz71ebc6f2012-06-12 13:45:15 +0200252
253
254echo "Testing is_package_installed()"
255
256if [[ -z "$os_PACKAGE" ]]; then
257 GetOSVersion
258fi
259
260if [[ "$os_PACKAGE" = "deb" ]]; then
261 is_package_installed dpkg
262 VAL=$?
263else
264 is_package_installed rpm
265 VAL=$?
266fi
267if [[ "$VAL" -eq 0 ]]; then
268 echo "OK"
269else
270 echo "is_package_installed() on existing package failed"
271fi
272
273if [[ "$os_PACKAGE" = "deb" ]]; then
274 is_package_installed dpkg bash
275 VAL=$?
276else
277 is_package_installed rpm bash
278 VAL=$?
279fi
280if [[ "$VAL" -eq 0 ]]; then
281 echo "OK"
282else
283 echo "is_package_installed() on more than one existing package failed"
284fi
285
286is_package_installed zzzZZZzzz
287VAL=$?
288if [[ "$VAL" -ne 0 ]]; then
289 echo "OK"
290else
291 echo "is_package_installed() on non-existing package failed"
292fi