blob: b2529ac4c9c9b0986dc1dae323a78febe7b3694d [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
Dean Troyerbf2ad702015-03-09 15:16:10 -05007# Import config functions
8source $TOP/inc/ini-config
Dean Troyer2ac8b3f2013-12-04 17:20:28 -06009
Ian Wienandfcdca052015-04-17 13:02:49 +100010source $TOP/tests/unittest.sh
11
12set -e
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060013
14echo "Testing INI functions"
15
16cat >test.ini <<EOF
17[default]
18# comment an option
19#log_file=./log.conf
20log_file=/etc/log.conf
21handlers=do not disturb
22
23[aaa]
24# the commented option should not change
25#handlers=cc,dd
26handlers = aa, bb
27
28[bbb]
29handlers=ee,ff
30
31[ ccc ]
32spaces = yes
33
34[ddd]
35empty =
36
37[eee]
38multi = foo1
39multi = foo2
Doug Wiegley1f65fd62014-12-13 11:56:16 -070040
41# inidelete(a)
42[del_separate_options]
43a=b
44b=c
45
46# inidelete(a)
47[del_same_option]
48a=b
49a=c
50
51# inidelete(a)
52[del_missing_option]
53b=c
54
55# inidelete(a)
56[del_missing_option_multi]
57b=c
58b=d
59
60# inidelete(a)
61[del_no_options]
62
63# inidelete(a)
64# no section - del_no_section
65
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060066EOF
67
68# Test with missing arguments
69
70BEFORE=$(cat test.ini)
71
72echo -n "iniset: test missing attribute argument: "
73iniset test.ini aaa
74NO_ATTRIBUTE=$(cat test.ini)
75if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +100076 passed
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060077else
Ian Wienandfcdca052015-04-17 13:02:49 +100078 failed "failed"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060079fi
80
81echo -n "iniset: test missing section argument: "
82iniset test.ini
83NO_SECTION=$(cat test.ini)
84if [[ "$BEFORE" == "$NO_SECTION" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +100085 passed
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060086else
Ian Wienandfcdca052015-04-17 13:02:49 +100087 failed "failed"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060088fi
89
90# Test with spaces
91
92VAL=$(iniget test.ini aaa handlers)
93if [[ "$VAL" == "aa, bb" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +100094 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060095else
Ian Wienandfcdca052015-04-17 13:02:49 +100096 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060097fi
98
99iniset test.ini aaa handlers "11, 22"
100
101VAL=$(iniget test.ini aaa handlers)
102if [[ "$VAL" == "11, 22" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000103 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600104else
Ian Wienandfcdca052015-04-17 13:02:49 +1000105 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600106fi
107
108# Test with spaces in section header
109
110VAL=$(iniget test.ini " ccc " spaces)
111if [[ "$VAL" == "yes" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000112 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600113else
Ian Wienandfcdca052015-04-17 13:02:49 +1000114 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600115fi
116
117iniset test.ini "b b" opt_ion 42
118
119VAL=$(iniget test.ini "b b" opt_ion)
120if [[ "$VAL" == "42" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000121 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600122else
Ian Wienandfcdca052015-04-17 13:02:49 +1000123 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600124fi
125
126# Test without spaces, end of file
127
128VAL=$(iniget test.ini bbb handlers)
129if [[ "$VAL" == "ee,ff" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000130 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600131else
Ian Wienandfcdca052015-04-17 13:02:49 +1000132 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600133fi
134
135iniset test.ini bbb handlers "33,44"
136
137VAL=$(iniget test.ini bbb handlers)
138if [[ "$VAL" == "33,44" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000139 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600140else
Ian Wienandfcdca052015-04-17 13:02:49 +1000141 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600142fi
143
144# test empty option
145if ini_has_option test.ini ddd empty; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000146 passed "OK: ddd.empty present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600147else
Ian Wienandfcdca052015-04-17 13:02:49 +1000148 failed "ini_has_option failed: ddd.empty not found"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600149fi
150
151# test non-empty option
152if ini_has_option test.ini bbb handlers; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000153 passed "OK: bbb.handlers present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600154else
Ian Wienandfcdca052015-04-17 13:02:49 +1000155 failed "ini_has_option failed: bbb.handlers not found"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600156fi
157
158# test changing empty option
159iniset test.ini ddd empty "42"
160
161VAL=$(iniget test.ini ddd empty)
162if [[ "$VAL" == "42" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000163 passed "OK: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600164else
Ian Wienandfcdca052015-04-17 13:02:49 +1000165 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600166fi
167
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000168# test pipe in option
169iniset test.ini aaa handlers "a|b"
170
171VAL=$(iniget test.ini aaa handlers)
172if [[ "$VAL" == "a|b" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000173 passed "OK: $VAL"
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000174else
Ian Wienandfcdca052015-04-17 13:02:49 +1000175 failed "iniget failed: $VAL"
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000176fi
177
178# test space in option
179iniset test.ini aaa handlers "a b"
180
181VAL="$(iniget test.ini aaa handlers)"
182if [[ "$VAL" == "a b" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000183 passed "OK: $VAL"
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000184else
Ian Wienandfcdca052015-04-17 13:02:49 +1000185 failed "iniget failed: $VAL"
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000186fi
187
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600188# Test section not exist
189
190VAL=$(iniget test.ini zzz handlers)
191if [[ -z "$VAL" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000192 passed "OK: zzz not present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600193else
Ian Wienandfcdca052015-04-17 13:02:49 +1000194 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600195fi
196
197iniset test.ini zzz handlers "999"
198
199VAL=$(iniget test.ini zzz handlers)
200if [[ -n "$VAL" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000201 passed "OK: zzz not present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600202else
Ian Wienandfcdca052015-04-17 13:02:49 +1000203 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600204fi
205
206# Test option not exist
207
208VAL=$(iniget test.ini aaa debug)
209if [[ -z "$VAL" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000210 passed "OK aaa.debug not present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600211else
Ian Wienandfcdca052015-04-17 13:02:49 +1000212 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600213fi
214
215if ! ini_has_option test.ini aaa debug; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000216 passed "OK aaa.debug not present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600217else
Ian Wienandfcdca052015-04-17 13:02:49 +1000218 failed "ini_has_option failed: aaa.debug"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600219fi
220
221iniset test.ini aaa debug "999"
222
223VAL=$(iniget test.ini aaa debug)
224if [[ -n "$VAL" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000225 passed "OK aaa.debug present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600226else
Ian Wienandfcdca052015-04-17 13:02:49 +1000227 failed "iniget failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600228fi
229
230# Test comments
231
232inicomment test.ini aaa handlers
233
234VAL=$(iniget test.ini aaa handlers)
235if [[ -z "$VAL" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000236 passed "OK"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600237else
Ian Wienandfcdca052015-04-17 13:02:49 +1000238 failed "inicomment failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600239fi
240
241# Test multiple line iniset/iniget
242iniset_multiline test.ini eee multi bar1 bar2
243
244VAL=$(iniget_multiline test.ini eee multi)
245if [[ "$VAL" == "bar1 bar2" ]]; then
246 echo "OK: iniset_multiline"
247else
Ian Wienandfcdca052015-04-17 13:02:49 +1000248 failed "iniset_multiline failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600249fi
250
251# Test iniadd with exiting values
252iniadd test.ini eee multi bar3
253VAL=$(iniget_multiline test.ini eee multi)
254if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000255 passed "OK: iniadd"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600256else
Ian Wienandfcdca052015-04-17 13:02:49 +1000257 failed "iniadd failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600258fi
259
260# Test iniadd with non-exiting values
261iniadd test.ini eee non-multi foobar1 foobar2
262VAL=$(iniget_multiline test.ini eee non-multi)
263if [[ "$VAL" == "foobar1 foobar2" ]]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000264 passed "OK: iniadd with non-exiting value"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600265else
Ian Wienandfcdca052015-04-17 13:02:49 +1000266 failed "iniadd with non-exsting failed: $VAL"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600267fi
268
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700269# Test inidelete
270del_cases="
271 del_separate_options
272 del_same_option
273 del_missing_option
274 del_missing_option_multi
275 del_no_options
276 del_no_section"
277
278for x in $del_cases; do
279 inidelete test.ini $x a
280 VAL=$(iniget_multiline test.ini $x a)
281 if [ -z "$VAL" ]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000282 passed "OK: inidelete $x"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700283 else
Ian Wienandfcdca052015-04-17 13:02:49 +1000284 failed "inidelete $x failed: $VAL"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700285 fi
286 if [ "$x" = "del_separate_options" -o \
287 "$x" = "del_missing_option" -o \
288 "$x" = "del_missing_option_multi" ]; then
289 VAL=$(iniget_multiline test.ini $x b)
290 if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
Ian Wienandfcdca052015-04-17 13:02:49 +1000291 passed "OK: inidelete other_options $x"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700292 else
Ian Wienandfcdca052015-04-17 13:02:49 +1000293 failed "inidelete other_option $x failed: $VAL"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700294 fi
295 fi
296done
297
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600298rm test.ini
Ian Wienandfcdca052015-04-17 13:02:49 +1000299
300report_results