blob: b0dc6b176b2bf3cbd76f9d8357618c2486429b55 [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
139# Test section not exist
140
141VAL=$(iniget test.ini zzz handlers)
142if [[ -z "$VAL" ]]; then
143 echo "OK: zzz not present"
144else
145 echo "iniget failed: $VAL"
146fi
147
148iniset test.ini zzz handlers "999"
149
150VAL=$(iniget test.ini zzz handlers)
151if [[ -n "$VAL" ]]; then
152 echo "OK: zzz not present"
153else
154 echo "iniget failed: $VAL"
155fi
156
157# Test option not exist
158
159VAL=$(iniget test.ini aaa debug)
160if [[ -z "$VAL" ]]; then
161 echo "OK aaa.debug not present"
162else
163 echo "iniget failed: $VAL"
164fi
165
166if ! ini_has_option test.ini aaa debug; then
167 echo "OK aaa.debug not present"
168else
169 echo "ini_has_option failed: aaa.debug"
170fi
171
172iniset test.ini aaa debug "999"
173
174VAL=$(iniget test.ini aaa debug)
175if [[ -n "$VAL" ]]; then
176 echo "OK aaa.debug present"
177else
178 echo "iniget failed: $VAL"
179fi
180
181# Test comments
182
183inicomment test.ini aaa handlers
184
185VAL=$(iniget test.ini aaa handlers)
186if [[ -z "$VAL" ]]; then
187 echo "OK"
188else
189 echo "inicomment failed: $VAL"
190fi
191
192# Test multiple line iniset/iniget
193iniset_multiline test.ini eee multi bar1 bar2
194
195VAL=$(iniget_multiline test.ini eee multi)
196if [[ "$VAL" == "bar1 bar2" ]]; then
197 echo "OK: iniset_multiline"
198else
199 echo "iniset_multiline failed: $VAL"
200fi
201
202# Test iniadd with exiting values
203iniadd test.ini eee multi bar3
204VAL=$(iniget_multiline test.ini eee multi)
205if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
206 echo "OK: iniadd"
207else
208 echo "iniadd failed: $VAL"
209fi
210
211# Test iniadd with non-exiting values
212iniadd test.ini eee non-multi foobar1 foobar2
213VAL=$(iniget_multiline test.ini eee non-multi)
214if [[ "$VAL" == "foobar1 foobar2" ]]; then
215 echo "OK: iniadd with non-exiting value"
216else
217 echo "iniadd with non-exsting failed: $VAL"
218fi
219
220rm test.ini