blob: 087aaf468b5f0e522c5d935f47ba1d5b7e20db6b [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
Sean Dague22b63662016-12-09 07:33:01 -050032function warn {
33 return 0
34}
35
Dean Troyer893e6632013-09-13 15:05:51 -050036TEST_1C_ADD="[eee]
37type=new
38multi = foo2"
39
Ian Wienandaee18c72014-02-21 15:35:08 +110040function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050041 cat >test1c.conf <<EOF
42[eee]
43# original comment
44type=original
45EOF
46}
47
Ian Wienandaee18c72014-02-21 15:35:08 +110048function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050049 cat >test2a.conf <<EOF
50[ddd]
51# original comment
52type=original
53EOF
54}
55
Ryota MIBU410f5c02014-04-04 02:00:31 +090056function setup_test4 {
57 mkdir -p test-etc
58 cat >test-etc/test4.conf <<EOF
59[fff]
60# original comment
61type=original
62EOF
63 TEST4_DIR="test-etc"
64 TEST4_FILE="test4.conf"
65}
66
Dean Troyer893e6632013-09-13 15:05:51 -050067cat >test.conf <<EOF
68[[test1|test1a.conf]]
69[default]
70# comment an option
71#log_file=./log.conf
72log_file=/etc/log.conf
73handlers=do not disturb
74
75[aaa]
76# the commented option should not change
77#handlers=cc,dd
78handlers = aa, bb
79
80[[test1|test1b.conf]]
81[bbb]
82handlers=ee,ff
83
84[ ccc ]
85spaces = yes
86
87[[test2|test2a.conf]]
88[ddd]
89# new comment
90type=new
91additional=true
92
93[[test1|test1c.conf]]
94$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060095
96[[test3|test-space.conf]]
97[DEFAULT]
98attribute=value
Sean Dague22b63662016-12-09 07:33:01 -050099
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600100# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -0500101
Ryota MIBU410f5c02014-04-04 02:00:31 +0900102[[test4|\$TEST4_DIR/\$TEST4_FILE]]
103[fff]
104type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +1000105
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100106[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +1000107[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100108foo=\${FOO_BAR_BAZ}
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400109
110[[test5|test-equals.conf]]
111[DEFAULT]
112drivers = driver=python.import.path.Driver
113
114[[test6|test-strip.conf]]
115[DEFAULT]
116# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700117attr = strip_trailing_space
118
119[[test7|test-colon.conf]]
120[DEFAULT]
121servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400122
Thomas Morin85f42f62015-09-01 10:33:10 +0200123[[test8|/permission-denied.conf]]
124foo=bar
125
126[[test9|\$UNDEF]]
127foo=bar
128
129[[test10|does-not-exist-dir/test.conf]]
130foo=bar
131
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900132[[test11|test-same.conf]]
133[DEFAULT]
134foo=bar
135
136[[test11|test-same.conf]]
137[some]
138random=config
139
Robert Li751ad1a2014-10-15 21:40:53 -0400140[[test-multi-sections|test-multi-sections.conf]]
141[sec-1]
142cfg_item1 = abcd
143cfg_item2 = efgh
144
145[sec-2]
146cfg_item1 = abcd
147cfg_item3 = /1/2/3/4:5
148cfg_item4 = end
149
150[sec-3]
151cfg_item5 = 5555
152cfg_item6 = 6666
153cfg_item5 = 5555another
154
155[[test-multiline|test-multiline.conf]]
156[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100157cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400158cfg_item1 = abcd
159cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100160cfg_item2 = \${FOO_BAR_BAZ}
161
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900162[[test11|test-same.conf]]
163[another]
164non = sense
Ryota MIBU410f5c02014-04-04 02:00:31 +0900165EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500166
167echo -n "get_meta_section_files: test0 doesn't exist: "
168VAL=$(get_meta_section_files test.conf test0)
169check_result "$VAL" ""
170
171echo -n "get_meta_section_files: test1 3 files: "
172VAL=$(get_meta_section_files test.conf test1)
173EXPECT_VAL="test1a.conf
174test1b.conf
175test1c.conf"
176check_result "$VAL" "$EXPECT_VAL"
177
178echo -n "get_meta_section_files: test2 1 file: "
179VAL=$(get_meta_section_files test.conf test2)
180EXPECT_VAL="test2a.conf"
181check_result "$VAL" "$EXPECT_VAL"
182
183
184# Get a section from a group that doesn't exist
185echo -n "get_meta_section: test0 doesn't exist: "
186VAL=$(get_meta_section test.conf test0 test0.conf)
187check_result "$VAL" ""
188
189# Get a single section from a group with multiple files
190echo -n "get_meta_section: test1c single section: "
191VAL=$(get_meta_section test.conf test1 test1c.conf)
192check_result "$VAL" "$TEST_1C_ADD"
193
194# Get a single section from a group with a single file
195echo -n "get_meta_section: test2a single section: "
196VAL=$(get_meta_section test.conf test2 test2a.conf)
197EXPECT_VAL="[ddd]
198# new comment
199type=new
200additional=true"
201check_result "$VAL" "$EXPECT_VAL"
202
203# Get a single section that doesn't exist from a group
204echo -n "get_meta_section: test2z.conf not in test2: "
205VAL=$(get_meta_section test.conf test2 test2z.conf)
206check_result "$VAL" ""
207
208# Get a section from a conf file that doesn't exist
209echo -n "get_meta_section: nofile doesn't exist: "
210VAL=$(get_meta_section nofile.ini test1)
211check_result "$VAL" ""
212
213echo -n "get_meta_section: nofile doesn't exist: "
214VAL=$(get_meta_section nofile.ini test0 test0.conf)
215check_result "$VAL" ""
216
217echo -n "merge_config_file test1c exists: "
218create_test1c
219merge_config_file test.conf test1 test1c.conf
220VAL=$(cat test1c.conf)
221# iniset adds values immediately under the section header
222EXPECT_VAL="[eee]
223multi = foo2
224# original comment
225type=new"
226check_result "$VAL" "$EXPECT_VAL"
227
228echo -n "merge_config_file test2a exists: "
229create_test2a
230merge_config_file test.conf test2 test2a.conf
231VAL=$(cat test2a.conf)
232# iniset adds values immediately under the section header
233EXPECT_VAL="[ddd]
234additional = true
235# original comment
236type=new"
237check_result "$VAL" "$EXPECT_VAL"
238
239echo -n "merge_config_file test2a not exist: "
240rm test2a.conf
241merge_config_file test.conf test2 test2a.conf
242VAL=$(cat test2a.conf)
243# iniset adds a blank line if it creates the file...
244EXPECT_VAL="
245[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400246type = new
247additional = true"
248check_result "$VAL" "$EXPECT_VAL"
249
250echo -n "merge_config_file test-multi-sections: "
251rm -f test-multi-sections.conf
252merge_config_file test.conf test-multi-sections test-multi-sections.conf
253VAL=$(cat test-multi-sections.conf)
254EXPECT_VAL='
255[sec-1]
256cfg_item1 = abcd
257cfg_item2 = efgh
258
259[sec-2]
260cfg_item1 = abcd
261cfg_item3 = /1/2/3/4:5
262cfg_item4 = end
263
264[sec-3]
265cfg_item5 = 5555
266cfg_item5 = 5555another
267cfg_item6 = 6666'
268check_result "$VAL" "$EXPECT_VAL"
269
270echo -n "merge_config_file test-multiline: "
271rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100272FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400273merge_config_file test.conf test-multiline test-multiline.conf
274VAL=$(cat test-multiline.conf)
275EXPECT_VAL='
276[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100277cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400278cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100279cfg_item2 = efgh
280cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500281check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100282unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500283
284echo -n "merge_config_group test2: "
285rm test2a.conf
286merge_config_group test.conf test2
287VAL=$(cat test2a.conf)
288# iniset adds a blank line if it creates the file...
289EXPECT_VAL="
290[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400291type = new
292additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500293check_result "$VAL" "$EXPECT_VAL"
294
295echo -n "merge_config_group test2 no conf file: "
296rm test2a.conf
297merge_config_group x-test.conf test2
298if [[ ! -r test2a.conf ]]; then
299 echo "OK"
300else
301 echo "failed: $VAL != $EXPECT_VAL"
302fi
303
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600304echo -n "merge_config_file test-space: "
305rm -f test-space.conf
306merge_config_file test.conf test3 test-space.conf
307VAL=$(cat test-space.conf)
308# iniset adds a blank line if it creates the file...
309EXPECT_VAL="
310[DEFAULT]
311attribute = value"
312check_result "$VAL" "$EXPECT_VAL"
313
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100314echo -n "merge_config_file test-env: "
315rm -f test-env.conf
316FOO_BAR_BAZ="foo bar baz"
317merge_config_file test.conf test-env test-env.conf
318VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000319EXPECT_VAL='
320[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100321foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000322check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100323unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000324
Ryota MIBU410f5c02014-04-04 02:00:31 +0900325echo -n "merge_config_group test4 variable filename: "
326setup_test4
327merge_config_group test.conf test4
328VAL=$(cat test-etc/test4.conf)
329EXPECT_VAL="[fff]
330# original comment
331type=new"
332check_result "$VAL" "$EXPECT_VAL"
333
334echo -n "merge_config_group test4 variable filename (not exist): "
335setup_test4
336rm test-etc/test4.conf
337merge_config_group test.conf test4
338VAL=$(cat test-etc/test4.conf)
339EXPECT_VAL="
340[fff]
341type = new"
342check_result "$VAL" "$EXPECT_VAL"
343
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400344echo -n "merge_config_file test5 equals in value: "
345rm -f test-equals.conf
346merge_config_file test.conf test5 test-equals.conf
347VAL=$(cat test-equals.conf)
348# iniset adds a blank line if it creates the file...
349EXPECT_VAL="
350[DEFAULT]
351drivers = driver=python.import.path.Driver"
352check_result "$VAL" "$EXPECT_VAL"
353
354echo -n "merge_config_file test6 value stripped: "
355rm -f test-strip.conf
356merge_config_file test.conf test6 test-strip.conf
357VAL=$(cat test-strip.conf)
358# iniset adds a blank line if it creates the file...
359EXPECT_VAL="
360[DEFAULT]
361attr = strip_trailing_space"
362check_result "$VAL" "$EXPECT_VAL"
363
Kevin Bentone0d6a462014-10-14 04:54:42 -0700364echo -n "merge_config_file test7 colon in value: "
365rm -f test-colon.conf
366merge_config_file test.conf test7 test-colon.conf
367VAL=$(cat test-colon.conf)
368EXPECT_VAL="
369[DEFAULT]
370servers = 10.11.12.13:80"
371check_result "$VAL" "$EXPECT_VAL"
372
Thomas Morin85f42f62015-09-01 10:33:10 +0200373echo "merge_config_file test8 non-touchable conf file: "
374set +e
375# function is expected to fail and exit, running it
376# in a subprocess to let this script proceed
377(merge_config_file test.conf test8 /permission-denied.conf)
378VAL=$?
379EXPECT_VAL=255
380check_result "$VAL" "$EXPECT_VAL"
381set -e
382
383echo -n "merge_config_group test9 undefined conf file: "
384set +e
Sean Dague22b63662016-12-09 07:33:01 -0500385# function is expected to trigger warn and continue
Thomas Morin85f42f62015-09-01 10:33:10 +0200386(merge_config_group test.conf test9)
387VAL=$?
Sean Dague22b63662016-12-09 07:33:01 -0500388EXPECT_VAL=0
Thomas Morin85f42f62015-09-01 10:33:10 +0200389check_result "$VAL" "$EXPECT_VAL"
390set -e
391
392echo -n "merge_config_group test10 not directory: "
393set +e
394# function is expected to fail and exit, running it
395# in a subprocess to let this script proceed
396(merge_config_group test.conf test10)
397VAL=$?
398EXPECT_VAL=255
399check_result "$VAL" "$EXPECT_VAL"
400set -e
401
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900402echo -n "merge_config_file test11 same section: "
403rm -f test-same.conf
404merge_config_group test.conf test11
405VAL=$(cat test-same.conf)
406EXPECT_VAL='
407[DEFAULT]
408foo = bar
409
410[some]
411random = config
412
413[another]
414non = sense'
415check_result "$VAL" "$EXPECT_VAL"
416
417
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100418rm -f test.conf test1c.conf test2a.conf \
419 test-space.conf test-equals.conf test-strip.conf \
420 test-colon.conf test-env.conf test-multiline.conf \
YAMAMOTO Takashi02f3f9a2016-11-26 00:43:07 +0900421 test-multi-sections.conf test-same.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900422rm -rf test-etc