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