blob: 3252104bf17d74c2f14b019a9bc59a6e684c1fa4 [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
Ian Wienandf3bf8b62014-10-29 21:53:56 +110095[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +100096[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +110097foo=\${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]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100129cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400130cfg_item1 = abcd
131cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100132cfg_item2 = \${FOO_BAR_BAZ}
133
Ryota MIBU410f5c02014-04-04 02:00:31 +0900134EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500135
136echo -n "get_meta_section_files: test0 doesn't exist: "
137VAL=$(get_meta_section_files test.conf test0)
138check_result "$VAL" ""
139
140echo -n "get_meta_section_files: test1 3 files: "
141VAL=$(get_meta_section_files test.conf test1)
142EXPECT_VAL="test1a.conf
143test1b.conf
144test1c.conf"
145check_result "$VAL" "$EXPECT_VAL"
146
147echo -n "get_meta_section_files: test2 1 file: "
148VAL=$(get_meta_section_files test.conf test2)
149EXPECT_VAL="test2a.conf"
150check_result "$VAL" "$EXPECT_VAL"
151
152
153# Get a section from a group that doesn't exist
154echo -n "get_meta_section: test0 doesn't exist: "
155VAL=$(get_meta_section test.conf test0 test0.conf)
156check_result "$VAL" ""
157
158# Get a single section from a group with multiple files
159echo -n "get_meta_section: test1c single section: "
160VAL=$(get_meta_section test.conf test1 test1c.conf)
161check_result "$VAL" "$TEST_1C_ADD"
162
163# Get a single section from a group with a single file
164echo -n "get_meta_section: test2a single section: "
165VAL=$(get_meta_section test.conf test2 test2a.conf)
166EXPECT_VAL="[ddd]
167# new comment
168type=new
169additional=true"
170check_result "$VAL" "$EXPECT_VAL"
171
172# Get a single section that doesn't exist from a group
173echo -n "get_meta_section: test2z.conf not in test2: "
174VAL=$(get_meta_section test.conf test2 test2z.conf)
175check_result "$VAL" ""
176
177# Get a section from a conf file that doesn't exist
178echo -n "get_meta_section: nofile doesn't exist: "
179VAL=$(get_meta_section nofile.ini test1)
180check_result "$VAL" ""
181
182echo -n "get_meta_section: nofile doesn't exist: "
183VAL=$(get_meta_section nofile.ini test0 test0.conf)
184check_result "$VAL" ""
185
186echo -n "merge_config_file test1c exists: "
187create_test1c
188merge_config_file test.conf test1 test1c.conf
189VAL=$(cat test1c.conf)
190# iniset adds values immediately under the section header
191EXPECT_VAL="[eee]
192multi = foo2
193# original comment
194type=new"
195check_result "$VAL" "$EXPECT_VAL"
196
197echo -n "merge_config_file test2a exists: "
198create_test2a
199merge_config_file test.conf test2 test2a.conf
200VAL=$(cat test2a.conf)
201# iniset adds values immediately under the section header
202EXPECT_VAL="[ddd]
203additional = true
204# original comment
205type=new"
206check_result "$VAL" "$EXPECT_VAL"
207
208echo -n "merge_config_file test2a not exist: "
209rm test2a.conf
210merge_config_file test.conf test2 test2a.conf
211VAL=$(cat test2a.conf)
212# iniset adds a blank line if it creates the file...
213EXPECT_VAL="
214[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400215type = new
216additional = true"
217check_result "$VAL" "$EXPECT_VAL"
218
219echo -n "merge_config_file test-multi-sections: "
220rm -f test-multi-sections.conf
221merge_config_file test.conf test-multi-sections test-multi-sections.conf
222VAL=$(cat test-multi-sections.conf)
223EXPECT_VAL='
224[sec-1]
225cfg_item1 = abcd
226cfg_item2 = efgh
227
228[sec-2]
229cfg_item1 = abcd
230cfg_item3 = /1/2/3/4:5
231cfg_item4 = end
232
233[sec-3]
234cfg_item5 = 5555
235cfg_item5 = 5555another
236cfg_item6 = 6666'
237check_result "$VAL" "$EXPECT_VAL"
238
239echo -n "merge_config_file test-multiline: "
240rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100241FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400242merge_config_file test.conf test-multiline test-multiline.conf
243VAL=$(cat test-multiline.conf)
244EXPECT_VAL='
245[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100246cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400247cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100248cfg_item2 = efgh
249cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500250check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100251unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500252
253echo -n "merge_config_group test2: "
254rm test2a.conf
255merge_config_group test.conf test2
256VAL=$(cat test2a.conf)
257# iniset adds a blank line if it creates the file...
258EXPECT_VAL="
259[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400260type = new
261additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500262check_result "$VAL" "$EXPECT_VAL"
263
264echo -n "merge_config_group test2 no conf file: "
265rm test2a.conf
266merge_config_group x-test.conf test2
267if [[ ! -r test2a.conf ]]; then
268 echo "OK"
269else
270 echo "failed: $VAL != $EXPECT_VAL"
271fi
272
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600273echo -n "merge_config_file test-space: "
274rm -f test-space.conf
275merge_config_file test.conf test3 test-space.conf
276VAL=$(cat test-space.conf)
277# iniset adds a blank line if it creates the file...
278EXPECT_VAL="
279[DEFAULT]
280attribute = value"
281check_result "$VAL" "$EXPECT_VAL"
282
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100283echo -n "merge_config_file test-env: "
284rm -f test-env.conf
285FOO_BAR_BAZ="foo bar baz"
286merge_config_file test.conf test-env test-env.conf
287VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000288EXPECT_VAL='
289[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100290foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000291check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100292unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000293
Ryota MIBU410f5c02014-04-04 02:00:31 +0900294echo -n "merge_config_group test4 variable filename: "
295setup_test4
296merge_config_group test.conf test4
297VAL=$(cat test-etc/test4.conf)
298EXPECT_VAL="[fff]
299# original comment
300type=new"
301check_result "$VAL" "$EXPECT_VAL"
302
303echo -n "merge_config_group test4 variable filename (not exist): "
304setup_test4
305rm test-etc/test4.conf
306merge_config_group test.conf test4
307VAL=$(cat test-etc/test4.conf)
308EXPECT_VAL="
309[fff]
310type = new"
311check_result "$VAL" "$EXPECT_VAL"
312
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400313echo -n "merge_config_file test5 equals in value: "
314rm -f test-equals.conf
315merge_config_file test.conf test5 test-equals.conf
316VAL=$(cat test-equals.conf)
317# iniset adds a blank line if it creates the file...
318EXPECT_VAL="
319[DEFAULT]
320drivers = driver=python.import.path.Driver"
321check_result "$VAL" "$EXPECT_VAL"
322
323echo -n "merge_config_file test6 value stripped: "
324rm -f test-strip.conf
325merge_config_file test.conf test6 test-strip.conf
326VAL=$(cat test-strip.conf)
327# iniset adds a blank line if it creates the file...
328EXPECT_VAL="
329[DEFAULT]
330attr = strip_trailing_space"
331check_result "$VAL" "$EXPECT_VAL"
332
Kevin Bentone0d6a462014-10-14 04:54:42 -0700333echo -n "merge_config_file test7 colon in value: "
334rm -f test-colon.conf
335merge_config_file test.conf test7 test-colon.conf
336VAL=$(cat test-colon.conf)
337EXPECT_VAL="
338[DEFAULT]
339servers = 10.11.12.13:80"
340check_result "$VAL" "$EXPECT_VAL"
341
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100342rm -f test.conf test1c.conf test2a.conf \
343 test-space.conf test-equals.conf test-strip.conf \
344 test-colon.conf test-env.conf test-multiline.conf \
345 test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900346rm -rf test-etc