blob: cd74cee6f08b21786757c0dd661fc41a64af0ac1 [file] [log] [blame]
Dean Troyer893e6632013-09-13 15:05:51 -05001#!/usr/bin/env bash
2
3# Tests for DevStack meta-config functions
4
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
7# Import common functions
8source $TOP/functions
9
10# Import config functions
11source $TOP/lib/config
12
13# check_result() tests and reports the result values
14# check_result "actual" "expected"
Ian Wienandaee18c72014-02-21 15:35:08 +110015function check_result {
Dean Troyer893e6632013-09-13 15:05:51 -050016 local actual=$1
17 local expected=$2
18 if [[ "$actual" == "$expected" ]]; then
19 echo "OK"
20 else
21 echo -e "failed: $actual != $expected\n"
22 fi
23}
24
25TEST_1C_ADD="[eee]
26type=new
27multi = foo2"
28
Ian Wienandaee18c72014-02-21 15:35:08 +110029function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050030 cat >test1c.conf <<EOF
31[eee]
32# original comment
33type=original
34EOF
35}
36
Ian Wienandaee18c72014-02-21 15:35:08 +110037function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050038 cat >test2a.conf <<EOF
39[ddd]
40# original comment
41type=original
42EOF
43}
44
Ryota MIBU410f5c02014-04-04 02:00:31 +090045function setup_test4 {
46 mkdir -p test-etc
47 cat >test-etc/test4.conf <<EOF
48[fff]
49# original comment
50type=original
51EOF
52 TEST4_DIR="test-etc"
53 TEST4_FILE="test4.conf"
54}
55
Dean Troyer893e6632013-09-13 15:05:51 -050056cat >test.conf <<EOF
57[[test1|test1a.conf]]
58[default]
59# comment an option
60#log_file=./log.conf
61log_file=/etc/log.conf
62handlers=do not disturb
63
64[aaa]
65# the commented option should not change
66#handlers=cc,dd
67handlers = aa, bb
68
69[[test1|test1b.conf]]
70[bbb]
71handlers=ee,ff
72
73[ ccc ]
74spaces = yes
75
76[[test2|test2a.conf]]
77[ddd]
78# new comment
79type=new
80additional=true
81
82[[test1|test1c.conf]]
83$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060084
85[[test3|test-space.conf]]
86[DEFAULT]
87attribute=value
88
89# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050090
Ryota MIBU410f5c02014-04-04 02:00:31 +090091[[test4|\$TEST4_DIR/\$TEST4_FILE]]
92[fff]
93type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +100094
95[[test-quote|test-quote.conf]]
96[foo]
97foo="foo bar" "baz"
Fergal Mc Carthycc87c282014-10-09 16:16:42 -040098
99[[test5|test-equals.conf]]
100[DEFAULT]
101drivers = driver=python.import.path.Driver
102
103[[test6|test-strip.conf]]
104[DEFAULT]
105# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700106attr = strip_trailing_space
107
108[[test7|test-colon.conf]]
109[DEFAULT]
110servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400111
112[[test-multi-sections|test-multi-sections.conf]]
113[sec-1]
114cfg_item1 = abcd
115cfg_item2 = efgh
116
117[sec-2]
118cfg_item1 = abcd
119cfg_item3 = /1/2/3/4:5
120cfg_item4 = end
121
122[sec-3]
123cfg_item5 = 5555
124cfg_item6 = 6666
125cfg_item5 = 5555another
126
127[[test-multiline|test-multiline.conf]]
128[multi]
129cfg_item1 = "ab":"cd", "ef": "gh"
130cfg_item1 = abcd
131cfg_item2 = efgh
Ryota MIBU410f5c02014-04-04 02:00:31 +0900132EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500133
134echo -n "get_meta_section_files: test0 doesn't exist: "
135VAL=$(get_meta_section_files test.conf test0)
136check_result "$VAL" ""
137
138echo -n "get_meta_section_files: test1 3 files: "
139VAL=$(get_meta_section_files test.conf test1)
140EXPECT_VAL="test1a.conf
141test1b.conf
142test1c.conf"
143check_result "$VAL" "$EXPECT_VAL"
144
145echo -n "get_meta_section_files: test2 1 file: "
146VAL=$(get_meta_section_files test.conf test2)
147EXPECT_VAL="test2a.conf"
148check_result "$VAL" "$EXPECT_VAL"
149
150
151# Get a section from a group that doesn't exist
152echo -n "get_meta_section: test0 doesn't exist: "
153VAL=$(get_meta_section test.conf test0 test0.conf)
154check_result "$VAL" ""
155
156# Get a single section from a group with multiple files
157echo -n "get_meta_section: test1c single section: "
158VAL=$(get_meta_section test.conf test1 test1c.conf)
159check_result "$VAL" "$TEST_1C_ADD"
160
161# Get a single section from a group with a single file
162echo -n "get_meta_section: test2a single section: "
163VAL=$(get_meta_section test.conf test2 test2a.conf)
164EXPECT_VAL="[ddd]
165# new comment
166type=new
167additional=true"
168check_result "$VAL" "$EXPECT_VAL"
169
170# Get a single section that doesn't exist from a group
171echo -n "get_meta_section: test2z.conf not in test2: "
172VAL=$(get_meta_section test.conf test2 test2z.conf)
173check_result "$VAL" ""
174
175# Get a section from a conf file that doesn't exist
176echo -n "get_meta_section: nofile doesn't exist: "
177VAL=$(get_meta_section nofile.ini test1)
178check_result "$VAL" ""
179
180echo -n "get_meta_section: nofile doesn't exist: "
181VAL=$(get_meta_section nofile.ini test0 test0.conf)
182check_result "$VAL" ""
183
184echo -n "merge_config_file test1c exists: "
185create_test1c
186merge_config_file test.conf test1 test1c.conf
187VAL=$(cat test1c.conf)
188# iniset adds values immediately under the section header
189EXPECT_VAL="[eee]
190multi = foo2
191# original comment
192type=new"
193check_result "$VAL" "$EXPECT_VAL"
194
195echo -n "merge_config_file test2a exists: "
196create_test2a
197merge_config_file test.conf test2 test2a.conf
198VAL=$(cat test2a.conf)
199# iniset adds values immediately under the section header
200EXPECT_VAL="[ddd]
201additional = true
202# original comment
203type=new"
204check_result "$VAL" "$EXPECT_VAL"
205
206echo -n "merge_config_file test2a not exist: "
207rm test2a.conf
208merge_config_file test.conf test2 test2a.conf
209VAL=$(cat test2a.conf)
210# iniset adds a blank line if it creates the file...
211EXPECT_VAL="
212[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400213type = new
214additional = true"
215check_result "$VAL" "$EXPECT_VAL"
216
217echo -n "merge_config_file test-multi-sections: "
218rm -f test-multi-sections.conf
219merge_config_file test.conf test-multi-sections test-multi-sections.conf
220VAL=$(cat test-multi-sections.conf)
221EXPECT_VAL='
222[sec-1]
223cfg_item1 = abcd
224cfg_item2 = efgh
225
226[sec-2]
227cfg_item1 = abcd
228cfg_item3 = /1/2/3/4:5
229cfg_item4 = end
230
231[sec-3]
232cfg_item5 = 5555
233cfg_item5 = 5555another
234cfg_item6 = 6666'
235check_result "$VAL" "$EXPECT_VAL"
236
237echo -n "merge_config_file test-multiline: "
238rm -f test-multiline.conf
239merge_config_file test.conf test-multiline test-multiline.conf
240VAL=$(cat test-multiline.conf)
241EXPECT_VAL='
242[multi]
243cfg_item1 = "ab":"cd", "ef": "gh"
244cfg_item1 = abcd
245cfg_item2 = efgh'
Dean Troyer893e6632013-09-13 15:05:51 -0500246check_result "$VAL" "$EXPECT_VAL"
247
248echo -n "merge_config_group test2: "
249rm test2a.conf
250merge_config_group test.conf test2
251VAL=$(cat test2a.conf)
252# iniset adds a blank line if it creates the file...
253EXPECT_VAL="
254[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400255type = new
256additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500257check_result "$VAL" "$EXPECT_VAL"
258
259echo -n "merge_config_group test2 no conf file: "
260rm test2a.conf
261merge_config_group x-test.conf test2
262if [[ ! -r test2a.conf ]]; then
263 echo "OK"
264else
265 echo "failed: $VAL != $EXPECT_VAL"
266fi
267
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600268echo -n "merge_config_file test-space: "
269rm -f test-space.conf
270merge_config_file test.conf test3 test-space.conf
271VAL=$(cat test-space.conf)
272# iniset adds a blank line if it creates the file...
273EXPECT_VAL="
274[DEFAULT]
275attribute = value"
276check_result "$VAL" "$EXPECT_VAL"
277
Ian Wienande2c9fee2014-09-26 09:42:11 +1000278echo -n "merge_config_file test-quote: "
279rm -f test-quote.conf
280merge_config_file test.conf test-quote test-quote.conf
281VAL=$(cat test-quote.conf)
282EXPECT_VAL='
283[foo]
284foo = "foo bar" "baz"'
285check_result "$VAL" "$EXPECT_VAL"
286
Ryota MIBU410f5c02014-04-04 02:00:31 +0900287echo -n "merge_config_group test4 variable filename: "
288setup_test4
289merge_config_group test.conf test4
290VAL=$(cat test-etc/test4.conf)
291EXPECT_VAL="[fff]
292# original comment
293type=new"
294check_result "$VAL" "$EXPECT_VAL"
295
296echo -n "merge_config_group test4 variable filename (not exist): "
297setup_test4
298rm test-etc/test4.conf
299merge_config_group test.conf test4
300VAL=$(cat test-etc/test4.conf)
301EXPECT_VAL="
302[fff]
303type = new"
304check_result "$VAL" "$EXPECT_VAL"
305
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400306echo -n "merge_config_file test5 equals in value: "
307rm -f test-equals.conf
308merge_config_file test.conf test5 test-equals.conf
309VAL=$(cat test-equals.conf)
310# iniset adds a blank line if it creates the file...
311EXPECT_VAL="
312[DEFAULT]
313drivers = driver=python.import.path.Driver"
314check_result "$VAL" "$EXPECT_VAL"
315
316echo -n "merge_config_file test6 value stripped: "
317rm -f test-strip.conf
318merge_config_file test.conf test6 test-strip.conf
319VAL=$(cat test-strip.conf)
320# iniset adds a blank line if it creates the file...
321EXPECT_VAL="
322[DEFAULT]
323attr = strip_trailing_space"
324check_result "$VAL" "$EXPECT_VAL"
325
Kevin Bentone0d6a462014-10-14 04:54:42 -0700326echo -n "merge_config_file test7 colon in value: "
327rm -f test-colon.conf
328merge_config_file test.conf test7 test-colon.conf
329VAL=$(cat test-colon.conf)
330EXPECT_VAL="
331[DEFAULT]
332servers = 10.11.12.13:80"
333check_result "$VAL" "$EXPECT_VAL"
334
335rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf test-equals.conf test-strip.conf test-colon.conf
Robert Li751ad1a2014-10-15 21:40:53 -0400336rm -f test-multiline.conf test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900337rm -rf test-etc