blob: 92f9c01f6901456d47f28ad4dfc76ddfce39ef84 [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
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900128[[test11|test-same.conf]]
129[DEFAULT]
130foo=bar
131
132[[test11|test-same.conf]]
133[some]
134random=config
135
Robert Li751ad1a2014-10-15 21:40:53 -0400136[[test-multi-sections|test-multi-sections.conf]]
137[sec-1]
138cfg_item1 = abcd
139cfg_item2 = efgh
140
141[sec-2]
142cfg_item1 = abcd
143cfg_item3 = /1/2/3/4:5
144cfg_item4 = end
145
146[sec-3]
147cfg_item5 = 5555
148cfg_item6 = 6666
149cfg_item5 = 5555another
150
151[[test-multiline|test-multiline.conf]]
152[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100153cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400154cfg_item1 = abcd
155cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100156cfg_item2 = \${FOO_BAR_BAZ}
157
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900158[[test11|test-same.conf]]
159[another]
160non = sense
Ryota MIBU410f5c02014-04-04 02:00:31 +0900161EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500162
163echo -n "get_meta_section_files: test0 doesn't exist: "
164VAL=$(get_meta_section_files test.conf test0)
165check_result "$VAL" ""
166
167echo -n "get_meta_section_files: test1 3 files: "
168VAL=$(get_meta_section_files test.conf test1)
169EXPECT_VAL="test1a.conf
170test1b.conf
171test1c.conf"
172check_result "$VAL" "$EXPECT_VAL"
173
174echo -n "get_meta_section_files: test2 1 file: "
175VAL=$(get_meta_section_files test.conf test2)
176EXPECT_VAL="test2a.conf"
177check_result "$VAL" "$EXPECT_VAL"
178
179
180# Get a section from a group that doesn't exist
181echo -n "get_meta_section: test0 doesn't exist: "
182VAL=$(get_meta_section test.conf test0 test0.conf)
183check_result "$VAL" ""
184
185# Get a single section from a group with multiple files
186echo -n "get_meta_section: test1c single section: "
187VAL=$(get_meta_section test.conf test1 test1c.conf)
188check_result "$VAL" "$TEST_1C_ADD"
189
190# Get a single section from a group with a single file
191echo -n "get_meta_section: test2a single section: "
192VAL=$(get_meta_section test.conf test2 test2a.conf)
193EXPECT_VAL="[ddd]
194# new comment
195type=new
196additional=true"
197check_result "$VAL" "$EXPECT_VAL"
198
199# Get a single section that doesn't exist from a group
200echo -n "get_meta_section: test2z.conf not in test2: "
201VAL=$(get_meta_section test.conf test2 test2z.conf)
202check_result "$VAL" ""
203
204# Get a section from a conf file that doesn't exist
205echo -n "get_meta_section: nofile doesn't exist: "
206VAL=$(get_meta_section nofile.ini test1)
207check_result "$VAL" ""
208
209echo -n "get_meta_section: nofile doesn't exist: "
210VAL=$(get_meta_section nofile.ini test0 test0.conf)
211check_result "$VAL" ""
212
213echo -n "merge_config_file test1c exists: "
214create_test1c
215merge_config_file test.conf test1 test1c.conf
216VAL=$(cat test1c.conf)
217# iniset adds values immediately under the section header
218EXPECT_VAL="[eee]
219multi = foo2
220# original comment
221type=new"
222check_result "$VAL" "$EXPECT_VAL"
223
224echo -n "merge_config_file test2a exists: "
225create_test2a
226merge_config_file test.conf test2 test2a.conf
227VAL=$(cat test2a.conf)
228# iniset adds values immediately under the section header
229EXPECT_VAL="[ddd]
230additional = true
231# original comment
232type=new"
233check_result "$VAL" "$EXPECT_VAL"
234
235echo -n "merge_config_file test2a not exist: "
236rm test2a.conf
237merge_config_file test.conf test2 test2a.conf
238VAL=$(cat test2a.conf)
239# iniset adds a blank line if it creates the file...
240EXPECT_VAL="
241[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400242type = new
243additional = true"
244check_result "$VAL" "$EXPECT_VAL"
245
246echo -n "merge_config_file test-multi-sections: "
247rm -f test-multi-sections.conf
248merge_config_file test.conf test-multi-sections test-multi-sections.conf
249VAL=$(cat test-multi-sections.conf)
250EXPECT_VAL='
251[sec-1]
252cfg_item1 = abcd
253cfg_item2 = efgh
254
255[sec-2]
256cfg_item1 = abcd
257cfg_item3 = /1/2/3/4:5
258cfg_item4 = end
259
260[sec-3]
261cfg_item5 = 5555
262cfg_item5 = 5555another
263cfg_item6 = 6666'
264check_result "$VAL" "$EXPECT_VAL"
265
266echo -n "merge_config_file test-multiline: "
267rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100268FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400269merge_config_file test.conf test-multiline test-multiline.conf
270VAL=$(cat test-multiline.conf)
271EXPECT_VAL='
272[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100273cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400274cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100275cfg_item2 = efgh
276cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500277check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100278unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500279
280echo -n "merge_config_group test2: "
281rm test2a.conf
282merge_config_group test.conf test2
283VAL=$(cat test2a.conf)
284# iniset adds a blank line if it creates the file...
285EXPECT_VAL="
286[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400287type = new
288additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500289check_result "$VAL" "$EXPECT_VAL"
290
291echo -n "merge_config_group test2 no conf file: "
292rm test2a.conf
293merge_config_group x-test.conf test2
294if [[ ! -r test2a.conf ]]; then
295 echo "OK"
296else
297 echo "failed: $VAL != $EXPECT_VAL"
298fi
299
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600300echo -n "merge_config_file test-space: "
301rm -f test-space.conf
302merge_config_file test.conf test3 test-space.conf
303VAL=$(cat test-space.conf)
304# iniset adds a blank line if it creates the file...
305EXPECT_VAL="
306[DEFAULT]
307attribute = value"
308check_result "$VAL" "$EXPECT_VAL"
309
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100310echo -n "merge_config_file test-env: "
311rm -f test-env.conf
312FOO_BAR_BAZ="foo bar baz"
313merge_config_file test.conf test-env test-env.conf
314VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000315EXPECT_VAL='
316[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100317foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000318check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100319unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000320
Ryota MIBU410f5c02014-04-04 02:00:31 +0900321echo -n "merge_config_group test4 variable filename: "
322setup_test4
323merge_config_group test.conf test4
324VAL=$(cat test-etc/test4.conf)
325EXPECT_VAL="[fff]
326# original comment
327type=new"
328check_result "$VAL" "$EXPECT_VAL"
329
330echo -n "merge_config_group test4 variable filename (not exist): "
331setup_test4
332rm test-etc/test4.conf
333merge_config_group test.conf test4
334VAL=$(cat test-etc/test4.conf)
335EXPECT_VAL="
336[fff]
337type = new"
338check_result "$VAL" "$EXPECT_VAL"
339
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400340echo -n "merge_config_file test5 equals in value: "
341rm -f test-equals.conf
342merge_config_file test.conf test5 test-equals.conf
343VAL=$(cat test-equals.conf)
344# iniset adds a blank line if it creates the file...
345EXPECT_VAL="
346[DEFAULT]
347drivers = driver=python.import.path.Driver"
348check_result "$VAL" "$EXPECT_VAL"
349
350echo -n "merge_config_file test6 value stripped: "
351rm -f test-strip.conf
352merge_config_file test.conf test6 test-strip.conf
353VAL=$(cat test-strip.conf)
354# iniset adds a blank line if it creates the file...
355EXPECT_VAL="
356[DEFAULT]
357attr = strip_trailing_space"
358check_result "$VAL" "$EXPECT_VAL"
359
Kevin Bentone0d6a462014-10-14 04:54:42 -0700360echo -n "merge_config_file test7 colon in value: "
361rm -f test-colon.conf
362merge_config_file test.conf test7 test-colon.conf
363VAL=$(cat test-colon.conf)
364EXPECT_VAL="
365[DEFAULT]
366servers = 10.11.12.13:80"
367check_result "$VAL" "$EXPECT_VAL"
368
Thomas Morin85f42f62015-09-01 10:33:10 +0200369echo "merge_config_file test8 non-touchable conf file: "
370set +e
371# function is expected to fail and exit, running it
372# in a subprocess to let this script proceed
373(merge_config_file test.conf test8 /permission-denied.conf)
374VAL=$?
375EXPECT_VAL=255
376check_result "$VAL" "$EXPECT_VAL"
377set -e
378
379echo -n "merge_config_group test9 undefined conf file: "
380set +e
381# function is expected to fail and exit, running it
382# in a subprocess to let this script proceed
383(merge_config_group test.conf test9)
384VAL=$?
385EXPECT_VAL=255
386check_result "$VAL" "$EXPECT_VAL"
387set -e
388
389echo -n "merge_config_group test10 not directory: "
390set +e
391# function is expected to fail and exit, running it
392# in a subprocess to let this script proceed
393(merge_config_group test.conf test10)
394VAL=$?
395EXPECT_VAL=255
396check_result "$VAL" "$EXPECT_VAL"
397set -e
398
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900399echo -n "merge_config_file test11 same section: "
400rm -f test-same.conf
401merge_config_group test.conf test11
402VAL=$(cat test-same.conf)
403EXPECT_VAL='
404[DEFAULT]
405foo = bar
406
407[some]
408random = config
409
410[another]
411non = sense'
412check_result "$VAL" "$EXPECT_VAL"
413
414
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100415rm -f test.conf test1c.conf test2a.conf \
416 test-space.conf test-equals.conf test-strip.conf \
417 test-colon.conf test-env.conf test-multiline.conf \
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900418 test-multi-sections.conf test-same.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900419rm -rf test-etc