blob: 598cd578f60a79b4f03ea3e949eee429c4584956 [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
37EOF
38
39# Test with missing arguments
40
41BEFORE=$(cat test.ini)
42
43echo -n "iniset: test missing attribute argument: "
44iniset test.ini aaa
45NO_ATTRIBUTE=$(cat test.ini)
46if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
47 echo "OK"
48else
49 echo "failed"
50fi
51
52echo -n "iniset: test missing section argument: "
53iniset test.ini
54NO_SECTION=$(cat test.ini)
55if [[ "$BEFORE" == "$NO_SECTION" ]]; then
56 echo "OK"
57else
58 echo "failed"
59fi
60
61# Test with spaces
62
63VAL=$(iniget test.ini aaa handlers)
64if [[ "$VAL" == "aa, bb" ]]; then
65 echo "OK: $VAL"
66else
67 echo "iniget failed: $VAL"
68fi
69
70iniset test.ini aaa handlers "11, 22"
71
72VAL=$(iniget test.ini aaa handlers)
73if [[ "$VAL" == "11, 22" ]]; then
74 echo "OK: $VAL"
75else
76 echo "iniget failed: $VAL"
77fi
78
79# Test with spaces in section header
80
81VAL=$(iniget test.ini " ccc " spaces)
82if [[ "$VAL" == "yes" ]]; then
83 echo "OK: $VAL"
84else
85 echo "iniget failed: $VAL"
86fi
87
88iniset test.ini "b b" opt_ion 42
89
90VAL=$(iniget test.ini "b b" opt_ion)
91if [[ "$VAL" == "42" ]]; then
92 echo "OK: $VAL"
93else
94 echo "iniget failed: $VAL"
95fi
96
97# Test without spaces, end of file
98
99VAL=$(iniget test.ini bbb handlers)
100if [[ "$VAL" == "ee,ff" ]]; then
101 echo "OK: $VAL"
102else
103 echo "iniget failed: $VAL"
104fi
105
106iniset test.ini bbb handlers "33,44"
107
108VAL=$(iniget test.ini bbb handlers)
109if [[ "$VAL" == "33,44" ]]; then
110 echo "OK: $VAL"
111else
112 echo "iniget failed: $VAL"
113fi
114
115# test empty option
116if ini_has_option test.ini ddd empty; then
117 echo "OK: ddd.empty present"
118else
119 echo "ini_has_option failed: ddd.empty not found"
120fi
121
122# test non-empty option
123if ini_has_option test.ini bbb handlers; then
124 echo "OK: bbb.handlers present"
125else
126 echo "ini_has_option failed: bbb.handlers not found"
127fi
128
129# test changing empty option
130iniset test.ini ddd empty "42"
131
132VAL=$(iniget test.ini ddd empty)
133if [[ "$VAL" == "42" ]]; then
134 echo "OK: $VAL"
135else
136 echo "iniget failed: $VAL"
137fi
138
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000139# test pipe in option
140iniset test.ini aaa handlers "a|b"
141
142VAL=$(iniget test.ini aaa handlers)
143if [[ "$VAL" == "a|b" ]]; then
144 echo "OK: $VAL"
145else
146 echo "iniget failed: $VAL"
147fi
148
149# test space in option
150iniset test.ini aaa handlers "a b"
151
152VAL="$(iniget test.ini aaa handlers)"
153if [[ "$VAL" == "a b" ]]; then
154 echo "OK: $VAL"
155else
156 echo "iniget failed: $VAL"
157fi
158
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600159# Test section not exist
160
161VAL=$(iniget test.ini zzz handlers)
162if [[ -z "$VAL" ]]; then
163 echo "OK: zzz not present"
164else
165 echo "iniget failed: $VAL"
166fi
167
168iniset test.ini zzz handlers "999"
169
170VAL=$(iniget test.ini zzz handlers)
171if [[ -n "$VAL" ]]; then
172 echo "OK: zzz not present"
173else
174 echo "iniget failed: $VAL"
175fi
176
177# Test option not exist
178
179VAL=$(iniget test.ini aaa debug)
180if [[ -z "$VAL" ]]; then
181 echo "OK aaa.debug not present"
182else
183 echo "iniget failed: $VAL"
184fi
185
186if ! ini_has_option test.ini aaa debug; then
187 echo "OK aaa.debug not present"
188else
189 echo "ini_has_option failed: aaa.debug"
190fi
191
192iniset test.ini aaa debug "999"
193
194VAL=$(iniget test.ini aaa debug)
195if [[ -n "$VAL" ]]; then
196 echo "OK aaa.debug present"
197else
198 echo "iniget failed: $VAL"
199fi
200
201# Test comments
202
203inicomment test.ini aaa handlers
204
205VAL=$(iniget test.ini aaa handlers)
206if [[ -z "$VAL" ]]; then
207 echo "OK"
208else
209 echo "inicomment failed: $VAL"
210fi
211
212# Test multiple line iniset/iniget
213iniset_multiline test.ini eee multi bar1 bar2
214
215VAL=$(iniget_multiline test.ini eee multi)
216if [[ "$VAL" == "bar1 bar2" ]]; then
217 echo "OK: iniset_multiline"
218else
219 echo "iniset_multiline failed: $VAL"
220fi
221
222# Test iniadd with exiting values
223iniadd test.ini eee multi bar3
224VAL=$(iniget_multiline test.ini eee multi)
225if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
226 echo "OK: iniadd"
227else
228 echo "iniadd failed: $VAL"
229fi
230
231# Test iniadd with non-exiting values
232iniadd test.ini eee non-multi foobar1 foobar2
233VAL=$(iniget_multiline test.ini eee non-multi)
234if [[ "$VAL" == "foobar1 foobar2" ]]; then
235 echo "OK: iniadd with non-exiting value"
236else
237 echo "iniadd with non-exsting failed: $VAL"
238fi
239
240rm test.ini