blob: 3ec65bf9b05af6c73eb042a4186a3ffaf65063f5 [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
11# check_result() tests and reports the result values
12# check_result "actual" "expected"
Ian Wienandaee18c72014-02-21 15:35:08 +110013function check_result {
Dean Troyer893e6632013-09-13 15:05:51 -050014 local actual=$1
15 local expected=$2
16 if [[ "$actual" == "$expected" ]]; then
17 echo "OK"
18 else
19 echo -e "failed: $actual != $expected\n"
Ian Wienandfcdca052015-04-17 13:02:49 +100020 exit 1
Dean Troyer893e6632013-09-13 15:05:51 -050021 fi
22}
23
24TEST_1C_ADD="[eee]
25type=new
26multi = foo2"
27
Ian Wienandaee18c72014-02-21 15:35:08 +110028function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050029 cat >test1c.conf <<EOF
30[eee]
31# original comment
32type=original
33EOF
34}
35
Ian Wienandaee18c72014-02-21 15:35:08 +110036function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050037 cat >test2a.conf <<EOF
38[ddd]
39# original comment
40type=original
41EOF
42}
43
Ryota MIBU410f5c02014-04-04 02:00:31 +090044function setup_test4 {
45 mkdir -p test-etc
46 cat >test-etc/test4.conf <<EOF
47[fff]
48# original comment
49type=original
50EOF
51 TEST4_DIR="test-etc"
52 TEST4_FILE="test4.conf"
53}
54
Dean Troyer893e6632013-09-13 15:05:51 -050055cat >test.conf <<EOF
56[[test1|test1a.conf]]
57[default]
58# comment an option
59#log_file=./log.conf
60log_file=/etc/log.conf
61handlers=do not disturb
62
63[aaa]
64# the commented option should not change
65#handlers=cc,dd
66handlers = aa, bb
67
68[[test1|test1b.conf]]
69[bbb]
70handlers=ee,ff
71
72[ ccc ]
73spaces = yes
74
75[[test2|test2a.conf]]
76[ddd]
77# new comment
78type=new
79additional=true
80
81[[test1|test1c.conf]]
82$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060083
84[[test3|test-space.conf]]
85[DEFAULT]
86attribute=value
87
88# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050089
Ryota MIBU410f5c02014-04-04 02:00:31 +090090[[test4|\$TEST4_DIR/\$TEST4_FILE]]
91[fff]
92type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +100093
Ian Wienandf3bf8b62014-10-29 21:53:56 +110094[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +100095[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +110096foo=\${FOO_BAR_BAZ}
Fergal Mc Carthycc87c282014-10-09 16:16:42 -040097
98[[test5|test-equals.conf]]
99[DEFAULT]
100drivers = driver=python.import.path.Driver
101
102[[test6|test-strip.conf]]
103[DEFAULT]
104# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700105attr = strip_trailing_space
106
107[[test7|test-colon.conf]]
108[DEFAULT]
109servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400110
111[[test-multi-sections|test-multi-sections.conf]]
112[sec-1]
113cfg_item1 = abcd
114cfg_item2 = efgh
115
116[sec-2]
117cfg_item1 = abcd
118cfg_item3 = /1/2/3/4:5
119cfg_item4 = end
120
121[sec-3]
122cfg_item5 = 5555
123cfg_item6 = 6666
124cfg_item5 = 5555another
125
126[[test-multiline|test-multiline.conf]]
127[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100128cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400129cfg_item1 = abcd
130cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100131cfg_item2 = \${FOO_BAR_BAZ}
132
Ryota MIBU410f5c02014-04-04 02:00:31 +0900133EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500134
135echo -n "get_meta_section_files: test0 doesn't exist: "
136VAL=$(get_meta_section_files test.conf test0)
137check_result "$VAL" ""
138
139echo -n "get_meta_section_files: test1 3 files: "
140VAL=$(get_meta_section_files test.conf test1)
141EXPECT_VAL="test1a.conf
142test1b.conf
143test1c.conf"
144check_result "$VAL" "$EXPECT_VAL"
145
146echo -n "get_meta_section_files: test2 1 file: "
147VAL=$(get_meta_section_files test.conf test2)
148EXPECT_VAL="test2a.conf"
149check_result "$VAL" "$EXPECT_VAL"
150
151
152# Get a section from a group that doesn't exist
153echo -n "get_meta_section: test0 doesn't exist: "
154VAL=$(get_meta_section test.conf test0 test0.conf)
155check_result "$VAL" ""
156
157# Get a single section from a group with multiple files
158echo -n "get_meta_section: test1c single section: "
159VAL=$(get_meta_section test.conf test1 test1c.conf)
160check_result "$VAL" "$TEST_1C_ADD"
161
162# Get a single section from a group with a single file
163echo -n "get_meta_section: test2a single section: "
164VAL=$(get_meta_section test.conf test2 test2a.conf)
165EXPECT_VAL="[ddd]
166# new comment
167type=new
168additional=true"
169check_result "$VAL" "$EXPECT_VAL"
170
171# Get a single section that doesn't exist from a group
172echo -n "get_meta_section: test2z.conf not in test2: "
173VAL=$(get_meta_section test.conf test2 test2z.conf)
174check_result "$VAL" ""
175
176# Get a section from a conf file that doesn't exist
177echo -n "get_meta_section: nofile doesn't exist: "
178VAL=$(get_meta_section nofile.ini test1)
179check_result "$VAL" ""
180
181echo -n "get_meta_section: nofile doesn't exist: "
182VAL=$(get_meta_section nofile.ini test0 test0.conf)
183check_result "$VAL" ""
184
185echo -n "merge_config_file test1c exists: "
186create_test1c
187merge_config_file test.conf test1 test1c.conf
188VAL=$(cat test1c.conf)
189# iniset adds values immediately under the section header
190EXPECT_VAL="[eee]
191multi = foo2
192# original comment
193type=new"
194check_result "$VAL" "$EXPECT_VAL"
195
196echo -n "merge_config_file test2a exists: "
197create_test2a
198merge_config_file test.conf test2 test2a.conf
199VAL=$(cat test2a.conf)
200# iniset adds values immediately under the section header
201EXPECT_VAL="[ddd]
202additional = true
203# original comment
204type=new"
205check_result "$VAL" "$EXPECT_VAL"
206
207echo -n "merge_config_file test2a not exist: "
208rm test2a.conf
209merge_config_file test.conf test2 test2a.conf
210VAL=$(cat test2a.conf)
211# iniset adds a blank line if it creates the file...
212EXPECT_VAL="
213[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400214type = new
215additional = true"
216check_result "$VAL" "$EXPECT_VAL"
217
218echo -n "merge_config_file test-multi-sections: "
219rm -f test-multi-sections.conf
220merge_config_file test.conf test-multi-sections test-multi-sections.conf
221VAL=$(cat test-multi-sections.conf)
222EXPECT_VAL='
223[sec-1]
224cfg_item1 = abcd
225cfg_item2 = efgh
226
227[sec-2]
228cfg_item1 = abcd
229cfg_item3 = /1/2/3/4:5
230cfg_item4 = end
231
232[sec-3]
233cfg_item5 = 5555
234cfg_item5 = 5555another
235cfg_item6 = 6666'
236check_result "$VAL" "$EXPECT_VAL"
237
238echo -n "merge_config_file test-multiline: "
239rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100240FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400241merge_config_file test.conf test-multiline test-multiline.conf
242VAL=$(cat test-multiline.conf)
243EXPECT_VAL='
244[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100245cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400246cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100247cfg_item2 = efgh
248cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500249check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100250unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500251
252echo -n "merge_config_group test2: "
253rm test2a.conf
254merge_config_group test.conf test2
255VAL=$(cat test2a.conf)
256# iniset adds a blank line if it creates the file...
257EXPECT_VAL="
258[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400259type = new
260additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500261check_result "$VAL" "$EXPECT_VAL"
262
263echo -n "merge_config_group test2 no conf file: "
264rm test2a.conf
265merge_config_group x-test.conf test2
266if [[ ! -r test2a.conf ]]; then
267 echo "OK"
268else
269 echo "failed: $VAL != $EXPECT_VAL"
270fi
271
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600272echo -n "merge_config_file test-space: "
273rm -f test-space.conf
274merge_config_file test.conf test3 test-space.conf
275VAL=$(cat test-space.conf)
276# iniset adds a blank line if it creates the file...
277EXPECT_VAL="
278[DEFAULT]
279attribute = value"
280check_result "$VAL" "$EXPECT_VAL"
281
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100282echo -n "merge_config_file test-env: "
283rm -f test-env.conf
284FOO_BAR_BAZ="foo bar baz"
285merge_config_file test.conf test-env test-env.conf
286VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000287EXPECT_VAL='
288[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100289foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000290check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100291unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000292
Ryota MIBU410f5c02014-04-04 02:00:31 +0900293echo -n "merge_config_group test4 variable filename: "
294setup_test4
295merge_config_group test.conf test4
296VAL=$(cat test-etc/test4.conf)
297EXPECT_VAL="[fff]
298# original comment
299type=new"
300check_result "$VAL" "$EXPECT_VAL"
301
302echo -n "merge_config_group test4 variable filename (not exist): "
303setup_test4
304rm test-etc/test4.conf
305merge_config_group test.conf test4
306VAL=$(cat test-etc/test4.conf)
307EXPECT_VAL="
308[fff]
309type = new"
310check_result "$VAL" "$EXPECT_VAL"
311
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400312echo -n "merge_config_file test5 equals in value: "
313rm -f test-equals.conf
314merge_config_file test.conf test5 test-equals.conf
315VAL=$(cat test-equals.conf)
316# iniset adds a blank line if it creates the file...
317EXPECT_VAL="
318[DEFAULT]
319drivers = driver=python.import.path.Driver"
320check_result "$VAL" "$EXPECT_VAL"
321
322echo -n "merge_config_file test6 value stripped: "
323rm -f test-strip.conf
324merge_config_file test.conf test6 test-strip.conf
325VAL=$(cat test-strip.conf)
326# iniset adds a blank line if it creates the file...
327EXPECT_VAL="
328[DEFAULT]
329attr = strip_trailing_space"
330check_result "$VAL" "$EXPECT_VAL"
331
Kevin Bentone0d6a462014-10-14 04:54:42 -0700332echo -n "merge_config_file test7 colon in value: "
333rm -f test-colon.conf
334merge_config_file test.conf test7 test-colon.conf
335VAL=$(cat test-colon.conf)
336EXPECT_VAL="
337[DEFAULT]
338servers = 10.11.12.13:80"
339check_result "$VAL" "$EXPECT_VAL"
340
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100341rm -f test.conf test1c.conf test2a.conf \
342 test-space.conf test-equals.conf test-strip.conf \
343 test-colon.conf test-env.conf test-multiline.conf \
344 test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900345rm -rf test-etc