blob: a04c0818549a022bf395b7f171e78fbb0139c4d5 [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
Dean Troyer893e6632013-09-13 15:05:51 -05007# Import config functions
Dean Troyerbf2ad702015-03-09 15:16:10 -05008source $TOP/inc/ini-config
9source $TOP/inc/meta-config
Dean Troyer893e6632013-09-13 15:05:51 -050010
Ian Wienandfa3e8412015-04-17 11:53:40 +100011set -e
12
Dean Troyer893e6632013-09-13 15:05:51 -050013# 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"
Ian Wienandfcdca052015-04-17 13:02:49 +100022 exit 1
Dean Troyer893e6632013-09-13 15:05:51 -050023 fi
24}
25
26TEST_1C_ADD="[eee]
27type=new
28multi = foo2"
29
Ian Wienandaee18c72014-02-21 15:35:08 +110030function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050031 cat >test1c.conf <<EOF
32[eee]
33# original comment
34type=original
35EOF
36}
37
Ian Wienandaee18c72014-02-21 15:35:08 +110038function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050039 cat >test2a.conf <<EOF
40[ddd]
41# original comment
42type=original
43EOF
44}
45
Ryota MIBU410f5c02014-04-04 02:00:31 +090046function setup_test4 {
47 mkdir -p test-etc
48 cat >test-etc/test4.conf <<EOF
49[fff]
50# original comment
51type=original
52EOF
53 TEST4_DIR="test-etc"
54 TEST4_FILE="test4.conf"
55}
56
Dean Troyer893e6632013-09-13 15:05:51 -050057cat >test.conf <<EOF
58[[test1|test1a.conf]]
59[default]
60# comment an option
61#log_file=./log.conf
62log_file=/etc/log.conf
63handlers=do not disturb
64
65[aaa]
66# the commented option should not change
67#handlers=cc,dd
68handlers = aa, bb
69
70[[test1|test1b.conf]]
71[bbb]
72handlers=ee,ff
73
74[ ccc ]
75spaces = yes
76
77[[test2|test2a.conf]]
78[ddd]
79# new comment
80type=new
81additional=true
82
83[[test1|test1c.conf]]
84$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060085
86[[test3|test-space.conf]]
87[DEFAULT]
88attribute=value
89
90# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050091
Ryota MIBU410f5c02014-04-04 02:00:31 +090092[[test4|\$TEST4_DIR/\$TEST4_FILE]]
93[fff]
94type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +100095
Ian Wienandf3bf8b62014-10-29 21:53:56 +110096[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +100097[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +110098foo=\${FOO_BAR_BAZ}
Fergal Mc Carthycc87c282014-10-09 16:16:42 -040099
100[[test5|test-equals.conf]]
101[DEFAULT]
102drivers = driver=python.import.path.Driver
103
104[[test6|test-strip.conf]]
105[DEFAULT]
106# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700107attr = strip_trailing_space
108
109[[test7|test-colon.conf]]
110[DEFAULT]
111servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400112
113[[test-multi-sections|test-multi-sections.conf]]
114[sec-1]
115cfg_item1 = abcd
116cfg_item2 = efgh
117
118[sec-2]
119cfg_item1 = abcd
120cfg_item3 = /1/2/3/4:5
121cfg_item4 = end
122
123[sec-3]
124cfg_item5 = 5555
125cfg_item6 = 6666
126cfg_item5 = 5555another
127
128[[test-multiline|test-multiline.conf]]
129[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100130cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400131cfg_item1 = abcd
132cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100133cfg_item2 = \${FOO_BAR_BAZ}
134
Ryota MIBU410f5c02014-04-04 02:00:31 +0900135EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500136
137echo -n "get_meta_section_files: test0 doesn't exist: "
138VAL=$(get_meta_section_files test.conf test0)
139check_result "$VAL" ""
140
141echo -n "get_meta_section_files: test1 3 files: "
142VAL=$(get_meta_section_files test.conf test1)
143EXPECT_VAL="test1a.conf
144test1b.conf
145test1c.conf"
146check_result "$VAL" "$EXPECT_VAL"
147
148echo -n "get_meta_section_files: test2 1 file: "
149VAL=$(get_meta_section_files test.conf test2)
150EXPECT_VAL="test2a.conf"
151check_result "$VAL" "$EXPECT_VAL"
152
153
154# Get a section from a group that doesn't exist
155echo -n "get_meta_section: test0 doesn't exist: "
156VAL=$(get_meta_section test.conf test0 test0.conf)
157check_result "$VAL" ""
158
159# Get a single section from a group with multiple files
160echo -n "get_meta_section: test1c single section: "
161VAL=$(get_meta_section test.conf test1 test1c.conf)
162check_result "$VAL" "$TEST_1C_ADD"
163
164# Get a single section from a group with a single file
165echo -n "get_meta_section: test2a single section: "
166VAL=$(get_meta_section test.conf test2 test2a.conf)
167EXPECT_VAL="[ddd]
168# new comment
169type=new
170additional=true"
171check_result "$VAL" "$EXPECT_VAL"
172
173# Get a single section that doesn't exist from a group
174echo -n "get_meta_section: test2z.conf not in test2: "
175VAL=$(get_meta_section test.conf test2 test2z.conf)
176check_result "$VAL" ""
177
178# Get a section from a conf file that doesn't exist
179echo -n "get_meta_section: nofile doesn't exist: "
180VAL=$(get_meta_section nofile.ini test1)
181check_result "$VAL" ""
182
183echo -n "get_meta_section: nofile doesn't exist: "
184VAL=$(get_meta_section nofile.ini test0 test0.conf)
185check_result "$VAL" ""
186
187echo -n "merge_config_file test1c exists: "
188create_test1c
189merge_config_file test.conf test1 test1c.conf
190VAL=$(cat test1c.conf)
191# iniset adds values immediately under the section header
192EXPECT_VAL="[eee]
193multi = foo2
194# original comment
195type=new"
196check_result "$VAL" "$EXPECT_VAL"
197
198echo -n "merge_config_file test2a exists: "
199create_test2a
200merge_config_file test.conf test2 test2a.conf
201VAL=$(cat test2a.conf)
202# iniset adds values immediately under the section header
203EXPECT_VAL="[ddd]
204additional = true
205# original comment
206type=new"
207check_result "$VAL" "$EXPECT_VAL"
208
209echo -n "merge_config_file test2a not exist: "
210rm test2a.conf
211merge_config_file test.conf test2 test2a.conf
212VAL=$(cat test2a.conf)
213# iniset adds a blank line if it creates the file...
214EXPECT_VAL="
215[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400216type = new
217additional = true"
218check_result "$VAL" "$EXPECT_VAL"
219
220echo -n "merge_config_file test-multi-sections: "
221rm -f test-multi-sections.conf
222merge_config_file test.conf test-multi-sections test-multi-sections.conf
223VAL=$(cat test-multi-sections.conf)
224EXPECT_VAL='
225[sec-1]
226cfg_item1 = abcd
227cfg_item2 = efgh
228
229[sec-2]
230cfg_item1 = abcd
231cfg_item3 = /1/2/3/4:5
232cfg_item4 = end
233
234[sec-3]
235cfg_item5 = 5555
236cfg_item5 = 5555another
237cfg_item6 = 6666'
238check_result "$VAL" "$EXPECT_VAL"
239
240echo -n "merge_config_file test-multiline: "
241rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100242FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400243merge_config_file test.conf test-multiline test-multiline.conf
244VAL=$(cat test-multiline.conf)
245EXPECT_VAL='
246[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100247cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400248cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100249cfg_item2 = efgh
250cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500251check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100252unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500253
254echo -n "merge_config_group test2: "
255rm test2a.conf
256merge_config_group test.conf test2
257VAL=$(cat test2a.conf)
258# iniset adds a blank line if it creates the file...
259EXPECT_VAL="
260[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400261type = new
262additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500263check_result "$VAL" "$EXPECT_VAL"
264
265echo -n "merge_config_group test2 no conf file: "
266rm test2a.conf
267merge_config_group x-test.conf test2
268if [[ ! -r test2a.conf ]]; then
269 echo "OK"
270else
271 echo "failed: $VAL != $EXPECT_VAL"
272fi
273
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600274echo -n "merge_config_file test-space: "
275rm -f test-space.conf
276merge_config_file test.conf test3 test-space.conf
277VAL=$(cat test-space.conf)
278# iniset adds a blank line if it creates the file...
279EXPECT_VAL="
280[DEFAULT]
281attribute = value"
282check_result "$VAL" "$EXPECT_VAL"
283
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100284echo -n "merge_config_file test-env: "
285rm -f test-env.conf
286FOO_BAR_BAZ="foo bar baz"
287merge_config_file test.conf test-env test-env.conf
288VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000289EXPECT_VAL='
290[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100291foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000292check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100293unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000294
Ryota MIBU410f5c02014-04-04 02:00:31 +0900295echo -n "merge_config_group test4 variable filename: "
296setup_test4
297merge_config_group test.conf test4
298VAL=$(cat test-etc/test4.conf)
299EXPECT_VAL="[fff]
300# original comment
301type=new"
302check_result "$VAL" "$EXPECT_VAL"
303
304echo -n "merge_config_group test4 variable filename (not exist): "
305setup_test4
306rm test-etc/test4.conf
307merge_config_group test.conf test4
308VAL=$(cat test-etc/test4.conf)
309EXPECT_VAL="
310[fff]
311type = new"
312check_result "$VAL" "$EXPECT_VAL"
313
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400314echo -n "merge_config_file test5 equals in value: "
315rm -f test-equals.conf
316merge_config_file test.conf test5 test-equals.conf
317VAL=$(cat test-equals.conf)
318# iniset adds a blank line if it creates the file...
319EXPECT_VAL="
320[DEFAULT]
321drivers = driver=python.import.path.Driver"
322check_result "$VAL" "$EXPECT_VAL"
323
324echo -n "merge_config_file test6 value stripped: "
325rm -f test-strip.conf
326merge_config_file test.conf test6 test-strip.conf
327VAL=$(cat test-strip.conf)
328# iniset adds a blank line if it creates the file...
329EXPECT_VAL="
330[DEFAULT]
331attr = strip_trailing_space"
332check_result "$VAL" "$EXPECT_VAL"
333
Kevin Bentone0d6a462014-10-14 04:54:42 -0700334echo -n "merge_config_file test7 colon in value: "
335rm -f test-colon.conf
336merge_config_file test.conf test7 test-colon.conf
337VAL=$(cat test-colon.conf)
338EXPECT_VAL="
339[DEFAULT]
340servers = 10.11.12.13:80"
341check_result "$VAL" "$EXPECT_VAL"
342
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100343rm -f test.conf test1c.conf test2a.conf \
344 test-space.conf test-equals.conf test-strip.conf \
345 test-colon.conf test-env.conf test-multiline.conf \
346 test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900347rm -rf test-etc