blob: 327fb561852c3dda1e7ea17dce259970bc7ed3d3 [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
Thomas Morin85f42f62015-09-01 10:33:10 +020026# mock function-common:die so that it does not
Swapnil Kulkarni (coolsvap)7f0be4f2015-11-20 10:52:59 +053027# interrupt our test script
Thomas Morin85f42f62015-09-01 10:33:10 +020028function die {
29 exit -1
30}
31
Dean Troyer893e6632013-09-13 15:05:51 -050032TEST_1C_ADD="[eee]
33type=new
34multi = foo2"
35
Ian Wienandaee18c72014-02-21 15:35:08 +110036function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050037 cat >test1c.conf <<EOF
38[eee]
39# original comment
40type=original
41EOF
42}
43
Ian Wienandaee18c72014-02-21 15:35:08 +110044function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050045 cat >test2a.conf <<EOF
46[ddd]
47# original comment
48type=original
49EOF
50}
51
Ryota MIBU410f5c02014-04-04 02:00:31 +090052function setup_test4 {
53 mkdir -p test-etc
54 cat >test-etc/test4.conf <<EOF
55[fff]
56# original comment
57type=original
58EOF
59 TEST4_DIR="test-etc"
60 TEST4_FILE="test4.conf"
61}
62
Dean Troyer893e6632013-09-13 15:05:51 -050063cat >test.conf <<EOF
64[[test1|test1a.conf]]
65[default]
66# comment an option
67#log_file=./log.conf
68log_file=/etc/log.conf
69handlers=do not disturb
70
71[aaa]
72# the commented option should not change
73#handlers=cc,dd
74handlers = aa, bb
75
76[[test1|test1b.conf]]
77[bbb]
78handlers=ee,ff
79
80[ ccc ]
81spaces = yes
82
83[[test2|test2a.conf]]
84[ddd]
85# new comment
86type=new
87additional=true
88
89[[test1|test1c.conf]]
90$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060091
92[[test3|test-space.conf]]
93[DEFAULT]
94attribute=value
95
96# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050097
Ryota MIBU410f5c02014-04-04 02:00:31 +090098[[test4|\$TEST4_DIR/\$TEST4_FILE]]
99[fff]
100type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +1000101
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100102[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +1000103[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100104foo=\${FOO_BAR_BAZ}
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400105
106[[test5|test-equals.conf]]
107[DEFAULT]
108drivers = driver=python.import.path.Driver
109
110[[test6|test-strip.conf]]
111[DEFAULT]
112# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700113attr = strip_trailing_space
114
115[[test7|test-colon.conf]]
116[DEFAULT]
117servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400118
Thomas Morin85f42f62015-09-01 10:33:10 +0200119[[test8|/permission-denied.conf]]
120foo=bar
121
122[[test9|\$UNDEF]]
123foo=bar
124
125[[test10|does-not-exist-dir/test.conf]]
126foo=bar
127
Robert Li751ad1a2014-10-15 21:40:53 -0400128[[test-multi-sections|test-multi-sections.conf]]
129[sec-1]
130cfg_item1 = abcd
131cfg_item2 = efgh
132
133[sec-2]
134cfg_item1 = abcd
135cfg_item3 = /1/2/3/4:5
136cfg_item4 = end
137
138[sec-3]
139cfg_item5 = 5555
140cfg_item6 = 6666
141cfg_item5 = 5555another
142
143[[test-multiline|test-multiline.conf]]
144[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100145cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400146cfg_item1 = abcd
147cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100148cfg_item2 = \${FOO_BAR_BAZ}
149
Ryota MIBU410f5c02014-04-04 02:00:31 +0900150EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500151
152echo -n "get_meta_section_files: test0 doesn't exist: "
153VAL=$(get_meta_section_files test.conf test0)
154check_result "$VAL" ""
155
156echo -n "get_meta_section_files: test1 3 files: "
157VAL=$(get_meta_section_files test.conf test1)
158EXPECT_VAL="test1a.conf
159test1b.conf
160test1c.conf"
161check_result "$VAL" "$EXPECT_VAL"
162
163echo -n "get_meta_section_files: test2 1 file: "
164VAL=$(get_meta_section_files test.conf test2)
165EXPECT_VAL="test2a.conf"
166check_result "$VAL" "$EXPECT_VAL"
167
168
169# Get a section from a group that doesn't exist
170echo -n "get_meta_section: test0 doesn't exist: "
171VAL=$(get_meta_section test.conf test0 test0.conf)
172check_result "$VAL" ""
173
174# Get a single section from a group with multiple files
175echo -n "get_meta_section: test1c single section: "
176VAL=$(get_meta_section test.conf test1 test1c.conf)
177check_result "$VAL" "$TEST_1C_ADD"
178
179# Get a single section from a group with a single file
180echo -n "get_meta_section: test2a single section: "
181VAL=$(get_meta_section test.conf test2 test2a.conf)
182EXPECT_VAL="[ddd]
183# new comment
184type=new
185additional=true"
186check_result "$VAL" "$EXPECT_VAL"
187
188# Get a single section that doesn't exist from a group
189echo -n "get_meta_section: test2z.conf not in test2: "
190VAL=$(get_meta_section test.conf test2 test2z.conf)
191check_result "$VAL" ""
192
193# Get a section from a conf file that doesn't exist
194echo -n "get_meta_section: nofile doesn't exist: "
195VAL=$(get_meta_section nofile.ini test1)
196check_result "$VAL" ""
197
198echo -n "get_meta_section: nofile doesn't exist: "
199VAL=$(get_meta_section nofile.ini test0 test0.conf)
200check_result "$VAL" ""
201
202echo -n "merge_config_file test1c exists: "
203create_test1c
204merge_config_file test.conf test1 test1c.conf
205VAL=$(cat test1c.conf)
206# iniset adds values immediately under the section header
207EXPECT_VAL="[eee]
208multi = foo2
209# original comment
210type=new"
211check_result "$VAL" "$EXPECT_VAL"
212
213echo -n "merge_config_file test2a exists: "
214create_test2a
215merge_config_file test.conf test2 test2a.conf
216VAL=$(cat test2a.conf)
217# iniset adds values immediately under the section header
218EXPECT_VAL="[ddd]
219additional = true
220# original comment
221type=new"
222check_result "$VAL" "$EXPECT_VAL"
223
224echo -n "merge_config_file test2a not exist: "
225rm test2a.conf
226merge_config_file test.conf test2 test2a.conf
227VAL=$(cat test2a.conf)
228# iniset adds a blank line if it creates the file...
229EXPECT_VAL="
230[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400231type = new
232additional = true"
233check_result "$VAL" "$EXPECT_VAL"
234
235echo -n "merge_config_file test-multi-sections: "
236rm -f test-multi-sections.conf
237merge_config_file test.conf test-multi-sections test-multi-sections.conf
238VAL=$(cat test-multi-sections.conf)
239EXPECT_VAL='
240[sec-1]
241cfg_item1 = abcd
242cfg_item2 = efgh
243
244[sec-2]
245cfg_item1 = abcd
246cfg_item3 = /1/2/3/4:5
247cfg_item4 = end
248
249[sec-3]
250cfg_item5 = 5555
251cfg_item5 = 5555another
252cfg_item6 = 6666'
253check_result "$VAL" "$EXPECT_VAL"
254
255echo -n "merge_config_file test-multiline: "
256rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100257FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400258merge_config_file test.conf test-multiline test-multiline.conf
259VAL=$(cat test-multiline.conf)
260EXPECT_VAL='
261[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100262cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400263cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100264cfg_item2 = efgh
265cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500266check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100267unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500268
269echo -n "merge_config_group test2: "
270rm test2a.conf
271merge_config_group test.conf test2
272VAL=$(cat test2a.conf)
273# iniset adds a blank line if it creates the file...
274EXPECT_VAL="
275[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400276type = new
277additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500278check_result "$VAL" "$EXPECT_VAL"
279
280echo -n "merge_config_group test2 no conf file: "
281rm test2a.conf
282merge_config_group x-test.conf test2
283if [[ ! -r test2a.conf ]]; then
284 echo "OK"
285else
286 echo "failed: $VAL != $EXPECT_VAL"
287fi
288
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600289echo -n "merge_config_file test-space: "
290rm -f test-space.conf
291merge_config_file test.conf test3 test-space.conf
292VAL=$(cat test-space.conf)
293# iniset adds a blank line if it creates the file...
294EXPECT_VAL="
295[DEFAULT]
296attribute = value"
297check_result "$VAL" "$EXPECT_VAL"
298
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100299echo -n "merge_config_file test-env: "
300rm -f test-env.conf
301FOO_BAR_BAZ="foo bar baz"
302merge_config_file test.conf test-env test-env.conf
303VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000304EXPECT_VAL='
305[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100306foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000307check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100308unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000309
Ryota MIBU410f5c02014-04-04 02:00:31 +0900310echo -n "merge_config_group test4 variable filename: "
311setup_test4
312merge_config_group test.conf test4
313VAL=$(cat test-etc/test4.conf)
314EXPECT_VAL="[fff]
315# original comment
316type=new"
317check_result "$VAL" "$EXPECT_VAL"
318
319echo -n "merge_config_group test4 variable filename (not exist): "
320setup_test4
321rm test-etc/test4.conf
322merge_config_group test.conf test4
323VAL=$(cat test-etc/test4.conf)
324EXPECT_VAL="
325[fff]
326type = new"
327check_result "$VAL" "$EXPECT_VAL"
328
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400329echo -n "merge_config_file test5 equals in value: "
330rm -f test-equals.conf
331merge_config_file test.conf test5 test-equals.conf
332VAL=$(cat test-equals.conf)
333# iniset adds a blank line if it creates the file...
334EXPECT_VAL="
335[DEFAULT]
336drivers = driver=python.import.path.Driver"
337check_result "$VAL" "$EXPECT_VAL"
338
339echo -n "merge_config_file test6 value stripped: "
340rm -f test-strip.conf
341merge_config_file test.conf test6 test-strip.conf
342VAL=$(cat test-strip.conf)
343# iniset adds a blank line if it creates the file...
344EXPECT_VAL="
345[DEFAULT]
346attr = strip_trailing_space"
347check_result "$VAL" "$EXPECT_VAL"
348
Kevin Bentone0d6a462014-10-14 04:54:42 -0700349echo -n "merge_config_file test7 colon in value: "
350rm -f test-colon.conf
351merge_config_file test.conf test7 test-colon.conf
352VAL=$(cat test-colon.conf)
353EXPECT_VAL="
354[DEFAULT]
355servers = 10.11.12.13:80"
356check_result "$VAL" "$EXPECT_VAL"
357
Thomas Morin85f42f62015-09-01 10:33:10 +0200358echo "merge_config_file test8 non-touchable conf file: "
359set +e
360# function is expected to fail and exit, running it
361# in a subprocess to let this script proceed
362(merge_config_file test.conf test8 /permission-denied.conf)
363VAL=$?
364EXPECT_VAL=255
365check_result "$VAL" "$EXPECT_VAL"
366set -e
367
368echo -n "merge_config_group test9 undefined conf file: "
369set +e
370# function is expected to fail and exit, running it
371# in a subprocess to let this script proceed
372(merge_config_group test.conf test9)
373VAL=$?
374EXPECT_VAL=255
375check_result "$VAL" "$EXPECT_VAL"
376set -e
377
378echo -n "merge_config_group test10 not directory: "
379set +e
380# function is expected to fail and exit, running it
381# in a subprocess to let this script proceed
382(merge_config_group test.conf test10)
383VAL=$?
384EXPECT_VAL=255
385check_result "$VAL" "$EXPECT_VAL"
386set -e
387
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100388rm -f test.conf test1c.conf test2a.conf \
389 test-space.conf test-equals.conf test-strip.conf \
390 test-colon.conf test-env.conf test-multiline.conf \
391 test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900392rm -rf test-etc