blob: 9d65280b8c9658429b09e47ed3c075c7e96ce025 [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"
20 fi
21}
22
23TEST_1C_ADD="[eee]
24type=new
25multi = foo2"
26
Ian Wienandaee18c72014-02-21 15:35:08 +110027function create_test1c {
Dean Troyer893e6632013-09-13 15:05:51 -050028 cat >test1c.conf <<EOF
29[eee]
30# original comment
31type=original
32EOF
33}
34
Ian Wienandaee18c72014-02-21 15:35:08 +110035function create_test2a {
Dean Troyer893e6632013-09-13 15:05:51 -050036 cat >test2a.conf <<EOF
37[ddd]
38# original comment
39type=original
40EOF
41}
42
Ryota MIBU410f5c02014-04-04 02:00:31 +090043function setup_test4 {
44 mkdir -p test-etc
45 cat >test-etc/test4.conf <<EOF
46[fff]
47# original comment
48type=original
49EOF
50 TEST4_DIR="test-etc"
51 TEST4_FILE="test4.conf"
52}
53
Dean Troyer893e6632013-09-13 15:05:51 -050054cat >test.conf <<EOF
55[[test1|test1a.conf]]
56[default]
57# comment an option
58#log_file=./log.conf
59log_file=/etc/log.conf
60handlers=do not disturb
61
62[aaa]
63# the commented option should not change
64#handlers=cc,dd
65handlers = aa, bb
66
67[[test1|test1b.conf]]
68[bbb]
69handlers=ee,ff
70
71[ ccc ]
72spaces = yes
73
74[[test2|test2a.conf]]
75[ddd]
76# new comment
77type=new
78additional=true
79
80[[test1|test1c.conf]]
81$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060082
83[[test3|test-space.conf]]
84[DEFAULT]
85attribute=value
86
87# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050088
Ryota MIBU410f5c02014-04-04 02:00:31 +090089[[test4|\$TEST4_DIR/\$TEST4_FILE]]
90[fff]
91type=new
Ian Wienande2c9fee2014-09-26 09:42:11 +100092
Ian Wienandf3bf8b62014-10-29 21:53:56 +110093[[test-env|test-env.conf]]
Ian Wienande2c9fee2014-09-26 09:42:11 +100094[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +110095foo=\${FOO_BAR_BAZ}
Fergal Mc Carthycc87c282014-10-09 16:16:42 -040096
97[[test5|test-equals.conf]]
98[DEFAULT]
99drivers = driver=python.import.path.Driver
100
101[[test6|test-strip.conf]]
102[DEFAULT]
103# next line has trailing space
Kevin Bentone0d6a462014-10-14 04:54:42 -0700104attr = strip_trailing_space
105
106[[test7|test-colon.conf]]
107[DEFAULT]
108servers=10.11.12.13:80
Robert Li751ad1a2014-10-15 21:40:53 -0400109
110[[test-multi-sections|test-multi-sections.conf]]
111[sec-1]
112cfg_item1 = abcd
113cfg_item2 = efgh
114
115[sec-2]
116cfg_item1 = abcd
117cfg_item3 = /1/2/3/4:5
118cfg_item4 = end
119
120[sec-3]
121cfg_item5 = 5555
122cfg_item6 = 6666
123cfg_item5 = 5555another
124
125[[test-multiline|test-multiline.conf]]
126[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100127cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400128cfg_item1 = abcd
129cfg_item2 = efgh
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100130cfg_item2 = \${FOO_BAR_BAZ}
131
Ryota MIBU410f5c02014-04-04 02:00:31 +0900132EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500133
134echo -n "get_meta_section_files: test0 doesn't exist: "
135VAL=$(get_meta_section_files test.conf test0)
136check_result "$VAL" ""
137
138echo -n "get_meta_section_files: test1 3 files: "
139VAL=$(get_meta_section_files test.conf test1)
140EXPECT_VAL="test1a.conf
141test1b.conf
142test1c.conf"
143check_result "$VAL" "$EXPECT_VAL"
144
145echo -n "get_meta_section_files: test2 1 file: "
146VAL=$(get_meta_section_files test.conf test2)
147EXPECT_VAL="test2a.conf"
148check_result "$VAL" "$EXPECT_VAL"
149
150
151# Get a section from a group that doesn't exist
152echo -n "get_meta_section: test0 doesn't exist: "
153VAL=$(get_meta_section test.conf test0 test0.conf)
154check_result "$VAL" ""
155
156# Get a single section from a group with multiple files
157echo -n "get_meta_section: test1c single section: "
158VAL=$(get_meta_section test.conf test1 test1c.conf)
159check_result "$VAL" "$TEST_1C_ADD"
160
161# Get a single section from a group with a single file
162echo -n "get_meta_section: test2a single section: "
163VAL=$(get_meta_section test.conf test2 test2a.conf)
164EXPECT_VAL="[ddd]
165# new comment
166type=new
167additional=true"
168check_result "$VAL" "$EXPECT_VAL"
169
170# Get a single section that doesn't exist from a group
171echo -n "get_meta_section: test2z.conf not in test2: "
172VAL=$(get_meta_section test.conf test2 test2z.conf)
173check_result "$VAL" ""
174
175# Get a section from a conf file that doesn't exist
176echo -n "get_meta_section: nofile doesn't exist: "
177VAL=$(get_meta_section nofile.ini test1)
178check_result "$VAL" ""
179
180echo -n "get_meta_section: nofile doesn't exist: "
181VAL=$(get_meta_section nofile.ini test0 test0.conf)
182check_result "$VAL" ""
183
184echo -n "merge_config_file test1c exists: "
185create_test1c
186merge_config_file test.conf test1 test1c.conf
187VAL=$(cat test1c.conf)
188# iniset adds values immediately under the section header
189EXPECT_VAL="[eee]
190multi = foo2
191# original comment
192type=new"
193check_result "$VAL" "$EXPECT_VAL"
194
195echo -n "merge_config_file test2a exists: "
196create_test2a
197merge_config_file test.conf test2 test2a.conf
198VAL=$(cat test2a.conf)
199# iniset adds values immediately under the section header
200EXPECT_VAL="[ddd]
201additional = true
202# original comment
203type=new"
204check_result "$VAL" "$EXPECT_VAL"
205
206echo -n "merge_config_file test2a not exist: "
207rm test2a.conf
208merge_config_file test.conf test2 test2a.conf
209VAL=$(cat test2a.conf)
210# iniset adds a blank line if it creates the file...
211EXPECT_VAL="
212[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400213type = new
214additional = true"
215check_result "$VAL" "$EXPECT_VAL"
216
217echo -n "merge_config_file test-multi-sections: "
218rm -f test-multi-sections.conf
219merge_config_file test.conf test-multi-sections test-multi-sections.conf
220VAL=$(cat test-multi-sections.conf)
221EXPECT_VAL='
222[sec-1]
223cfg_item1 = abcd
224cfg_item2 = efgh
225
226[sec-2]
227cfg_item1 = abcd
228cfg_item3 = /1/2/3/4:5
229cfg_item4 = end
230
231[sec-3]
232cfg_item5 = 5555
233cfg_item5 = 5555another
234cfg_item6 = 6666'
235check_result "$VAL" "$EXPECT_VAL"
236
237echo -n "merge_config_file test-multiline: "
238rm -f test-multiline.conf
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100239FOO_BAR_BAZ="foo bar baz"
Robert Li751ad1a2014-10-15 21:40:53 -0400240merge_config_file test.conf test-multiline test-multiline.conf
241VAL=$(cat test-multiline.conf)
242EXPECT_VAL='
243[multi]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100244cfg_item1 = ab:cd:ef:gh
Robert Li751ad1a2014-10-15 21:40:53 -0400245cfg_item1 = abcd
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100246cfg_item2 = efgh
247cfg_item2 = foo bar baz'
Dean Troyer893e6632013-09-13 15:05:51 -0500248check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100249unset FOO_BAR_BAZ
Dean Troyer893e6632013-09-13 15:05:51 -0500250
251echo -n "merge_config_group test2: "
252rm test2a.conf
253merge_config_group test.conf test2
254VAL=$(cat test2a.conf)
255# iniset adds a blank line if it creates the file...
256EXPECT_VAL="
257[ddd]
Robert Li751ad1a2014-10-15 21:40:53 -0400258type = new
259additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500260check_result "$VAL" "$EXPECT_VAL"
261
262echo -n "merge_config_group test2 no conf file: "
263rm test2a.conf
264merge_config_group x-test.conf test2
265if [[ ! -r test2a.conf ]]; then
266 echo "OK"
267else
268 echo "failed: $VAL != $EXPECT_VAL"
269fi
270
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600271echo -n "merge_config_file test-space: "
272rm -f test-space.conf
273merge_config_file test.conf test3 test-space.conf
274VAL=$(cat test-space.conf)
275# iniset adds a blank line if it creates the file...
276EXPECT_VAL="
277[DEFAULT]
278attribute = value"
279check_result "$VAL" "$EXPECT_VAL"
280
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100281echo -n "merge_config_file test-env: "
282rm -f test-env.conf
283FOO_BAR_BAZ="foo bar baz"
284merge_config_file test.conf test-env test-env.conf
285VAL=$(cat test-env.conf)
Ian Wienande2c9fee2014-09-26 09:42:11 +1000286EXPECT_VAL='
287[foo]
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100288foo = foo bar baz'
Ian Wienande2c9fee2014-09-26 09:42:11 +1000289check_result "$VAL" "$EXPECT_VAL"
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100290unset FOO_BAR_BAZ
Ian Wienande2c9fee2014-09-26 09:42:11 +1000291
Ryota MIBU410f5c02014-04-04 02:00:31 +0900292echo -n "merge_config_group test4 variable filename: "
293setup_test4
294merge_config_group test.conf test4
295VAL=$(cat test-etc/test4.conf)
296EXPECT_VAL="[fff]
297# original comment
298type=new"
299check_result "$VAL" "$EXPECT_VAL"
300
301echo -n "merge_config_group test4 variable filename (not exist): "
302setup_test4
303rm test-etc/test4.conf
304merge_config_group test.conf test4
305VAL=$(cat test-etc/test4.conf)
306EXPECT_VAL="
307[fff]
308type = new"
309check_result "$VAL" "$EXPECT_VAL"
310
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400311echo -n "merge_config_file test5 equals in value: "
312rm -f test-equals.conf
313merge_config_file test.conf test5 test-equals.conf
314VAL=$(cat test-equals.conf)
315# iniset adds a blank line if it creates the file...
316EXPECT_VAL="
317[DEFAULT]
318drivers = driver=python.import.path.Driver"
319check_result "$VAL" "$EXPECT_VAL"
320
321echo -n "merge_config_file test6 value stripped: "
322rm -f test-strip.conf
323merge_config_file test.conf test6 test-strip.conf
324VAL=$(cat test-strip.conf)
325# iniset adds a blank line if it creates the file...
326EXPECT_VAL="
327[DEFAULT]
328attr = strip_trailing_space"
329check_result "$VAL" "$EXPECT_VAL"
330
Kevin Bentone0d6a462014-10-14 04:54:42 -0700331echo -n "merge_config_file test7 colon in value: "
332rm -f test-colon.conf
333merge_config_file test.conf test7 test-colon.conf
334VAL=$(cat test-colon.conf)
335EXPECT_VAL="
336[DEFAULT]
337servers = 10.11.12.13:80"
338check_result "$VAL" "$EXPECT_VAL"
339
Ian Wienandf3bf8b62014-10-29 21:53:56 +1100340rm -f test.conf test1c.conf test2a.conf \
341 test-space.conf test-equals.conf test-strip.conf \
342 test-colon.conf test-env.conf test-multiline.conf \
343 test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900344rm -rf test-etc