blob: 106cc9507fa0b00d230158464d5d67521dedc578 [file] [log] [blame]
Dean Troyer2ac8b3f2013-12-04 17:20:28 -06001#!/usr/bin/env bash
2
3# Tests for DevStack INI functions
4
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
9
10
11echo "Testing INI functions"
12
13cat >test.ini <<EOF
14[default]
15# comment an option
16#log_file=./log.conf
17log_file=/etc/log.conf
18handlers=do not disturb
19
20[aaa]
21# the commented option should not change
22#handlers=cc,dd
23handlers = aa, bb
24
25[bbb]
26handlers=ee,ff
27
28[ ccc ]
29spaces = yes
30
31[ddd]
32empty =
33
34[eee]
35multi = foo1
36multi = foo2
Doug Wiegley1f65fd62014-12-13 11:56:16 -070037
38# inidelete(a)
39[del_separate_options]
40a=b
41b=c
42
43# inidelete(a)
44[del_same_option]
45a=b
46a=c
47
48# inidelete(a)
49[del_missing_option]
50b=c
51
52# inidelete(a)
53[del_missing_option_multi]
54b=c
55b=d
56
57# inidelete(a)
58[del_no_options]
59
60# inidelete(a)
61# no section - del_no_section
62
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060063EOF
64
65# Test with missing arguments
66
67BEFORE=$(cat test.ini)
68
69echo -n "iniset: test missing attribute argument: "
70iniset test.ini aaa
71NO_ATTRIBUTE=$(cat test.ini)
72if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
73 echo "OK"
74else
75 echo "failed"
76fi
77
78echo -n "iniset: test missing section argument: "
79iniset test.ini
80NO_SECTION=$(cat test.ini)
81if [[ "$BEFORE" == "$NO_SECTION" ]]; then
82 echo "OK"
83else
84 echo "failed"
85fi
86
87# Test with spaces
88
89VAL=$(iniget test.ini aaa handlers)
90if [[ "$VAL" == "aa, bb" ]]; then
91 echo "OK: $VAL"
92else
93 echo "iniget failed: $VAL"
94fi
95
96iniset test.ini aaa handlers "11, 22"
97
98VAL=$(iniget test.ini aaa handlers)
99if [[ "$VAL" == "11, 22" ]]; then
100 echo "OK: $VAL"
101else
102 echo "iniget failed: $VAL"
103fi
104
105# Test with spaces in section header
106
107VAL=$(iniget test.ini " ccc " spaces)
108if [[ "$VAL" == "yes" ]]; then
109 echo "OK: $VAL"
110else
111 echo "iniget failed: $VAL"
112fi
113
114iniset test.ini "b b" opt_ion 42
115
116VAL=$(iniget test.ini "b b" opt_ion)
117if [[ "$VAL" == "42" ]]; then
118 echo "OK: $VAL"
119else
120 echo "iniget failed: $VAL"
121fi
122
123# Test without spaces, end of file
124
125VAL=$(iniget test.ini bbb handlers)
126if [[ "$VAL" == "ee,ff" ]]; then
127 echo "OK: $VAL"
128else
129 echo "iniget failed: $VAL"
130fi
131
132iniset test.ini bbb handlers "33,44"
133
134VAL=$(iniget test.ini bbb handlers)
135if [[ "$VAL" == "33,44" ]]; then
136 echo "OK: $VAL"
137else
138 echo "iniget failed: $VAL"
139fi
140
141# test empty option
142if ini_has_option test.ini ddd empty; then
143 echo "OK: ddd.empty present"
144else
145 echo "ini_has_option failed: ddd.empty not found"
146fi
147
148# test non-empty option
149if ini_has_option test.ini bbb handlers; then
150 echo "OK: bbb.handlers present"
151else
152 echo "ini_has_option failed: bbb.handlers not found"
153fi
154
155# test changing empty option
156iniset test.ini ddd empty "42"
157
158VAL=$(iniget test.ini ddd empty)
159if [[ "$VAL" == "42" ]]; then
160 echo "OK: $VAL"
161else
162 echo "iniget failed: $VAL"
163fi
164
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000165# test pipe in option
166iniset test.ini aaa handlers "a|b"
167
168VAL=$(iniget test.ini aaa handlers)
169if [[ "$VAL" == "a|b" ]]; then
170 echo "OK: $VAL"
171else
172 echo "iniget failed: $VAL"
173fi
174
175# test space in option
176iniset test.ini aaa handlers "a b"
177
178VAL="$(iniget test.ini aaa handlers)"
179if [[ "$VAL" == "a b" ]]; then
180 echo "OK: $VAL"
181else
182 echo "iniget failed: $VAL"
183fi
184
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600185# Test section not exist
186
187VAL=$(iniget test.ini zzz handlers)
188if [[ -z "$VAL" ]]; then
189 echo "OK: zzz not present"
190else
191 echo "iniget failed: $VAL"
192fi
193
194iniset test.ini zzz handlers "999"
195
196VAL=$(iniget test.ini zzz handlers)
197if [[ -n "$VAL" ]]; then
198 echo "OK: zzz not present"
199else
200 echo "iniget failed: $VAL"
201fi
202
203# Test option not exist
204
205VAL=$(iniget test.ini aaa debug)
206if [[ -z "$VAL" ]]; then
207 echo "OK aaa.debug not present"
208else
209 echo "iniget failed: $VAL"
210fi
211
212if ! ini_has_option test.ini aaa debug; then
213 echo "OK aaa.debug not present"
214else
215 echo "ini_has_option failed: aaa.debug"
216fi
217
218iniset test.ini aaa debug "999"
219
220VAL=$(iniget test.ini aaa debug)
221if [[ -n "$VAL" ]]; then
222 echo "OK aaa.debug present"
223else
224 echo "iniget failed: $VAL"
225fi
226
227# Test comments
228
229inicomment test.ini aaa handlers
230
231VAL=$(iniget test.ini aaa handlers)
232if [[ -z "$VAL" ]]; then
233 echo "OK"
234else
235 echo "inicomment failed: $VAL"
236fi
237
238# Test multiple line iniset/iniget
239iniset_multiline test.ini eee multi bar1 bar2
240
241VAL=$(iniget_multiline test.ini eee multi)
242if [[ "$VAL" == "bar1 bar2" ]]; then
243 echo "OK: iniset_multiline"
244else
245 echo "iniset_multiline failed: $VAL"
246fi
247
248# Test iniadd with exiting values
249iniadd test.ini eee multi bar3
250VAL=$(iniget_multiline test.ini eee multi)
251if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
252 echo "OK: iniadd"
253else
254 echo "iniadd failed: $VAL"
255fi
256
257# Test iniadd with non-exiting values
258iniadd test.ini eee non-multi foobar1 foobar2
259VAL=$(iniget_multiline test.ini eee non-multi)
260if [[ "$VAL" == "foobar1 foobar2" ]]; then
261 echo "OK: iniadd with non-exiting value"
262else
263 echo "iniadd with non-exsting failed: $VAL"
264fi
265
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700266# Test inidelete
267del_cases="
268 del_separate_options
269 del_same_option
270 del_missing_option
271 del_missing_option_multi
272 del_no_options
273 del_no_section"
274
275for x in $del_cases; do
276 inidelete test.ini $x a
277 VAL=$(iniget_multiline test.ini $x a)
278 if [ -z "$VAL" ]; then
279 echo "OK: inidelete $x"
280 else
281 echo "inidelete $x failed: $VAL"
282 fi
283 if [ "$x" = "del_separate_options" -o \
284 "$x" = "del_missing_option" -o \
285 "$x" = "del_missing_option_multi" ]; then
286 VAL=$(iniget_multiline test.ini $x b)
287 if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
288 echo "OK: inidelete other_options $x"
289 else
290 echo "inidelete other_option $x failed: $VAL"
291 fi
292 fi
293done
294
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600295rm test.ini