blob: 696e57f9090b29fb90e30dd5d3b553c1baa8e173 [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
95[[test-quote|test-quote.conf]]
96[foo]
97foo="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
106attr = strip_trailing_space
Robert Li6ff21ac2014-10-10 12:43:05 -0400107
108[[test-multi-sections|test-multi-sections.conf]]
109[sec-1]
110cfg_item1 = abcd
111cfg_item2 = efgh
112
113[sec-2]
114cfg_item1 = abcd
115cfg_item3 = /1/2/3/4:5
116cfg_item4 = end
117
118[sec-3]
119cfg_item5 = 5555
120cfg_item6 = 6666
121cfg_item5 = 5555another
122
123[[test-multiline|test-multiline.conf]]
124[multi]
125cfg_item1 = "ab":"cd", "ef": "gh"
126cfg_item1 = abcd
127cfg_item2 = efgh
Ryota MIBU410f5c02014-04-04 02:00:31 +0900128EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500129
130echo -n "get_meta_section_files: test0 doesn't exist: "
131VAL=$(get_meta_section_files test.conf test0)
132check_result "$VAL" ""
133
134echo -n "get_meta_section_files: test1 3 files: "
135VAL=$(get_meta_section_files test.conf test1)
136EXPECT_VAL="test1a.conf
137test1b.conf
138test1c.conf"
139check_result "$VAL" "$EXPECT_VAL"
140
141echo -n "get_meta_section_files: test2 1 file: "
142VAL=$(get_meta_section_files test.conf test2)
143EXPECT_VAL="test2a.conf"
144check_result "$VAL" "$EXPECT_VAL"
145
146
147# Get a section from a group that doesn't exist
148echo -n "get_meta_section: test0 doesn't exist: "
149VAL=$(get_meta_section test.conf test0 test0.conf)
150check_result "$VAL" ""
151
152# Get a single section from a group with multiple files
153echo -n "get_meta_section: test1c single section: "
154VAL=$(get_meta_section test.conf test1 test1c.conf)
155check_result "$VAL" "$TEST_1C_ADD"
156
157# Get a single section from a group with a single file
158echo -n "get_meta_section: test2a single section: "
159VAL=$(get_meta_section test.conf test2 test2a.conf)
160EXPECT_VAL="[ddd]
161# new comment
162type=new
163additional=true"
164check_result "$VAL" "$EXPECT_VAL"
165
166# Get a single section that doesn't exist from a group
167echo -n "get_meta_section: test2z.conf not in test2: "
168VAL=$(get_meta_section test.conf test2 test2z.conf)
169check_result "$VAL" ""
170
171# Get a section from a conf file that doesn't exist
172echo -n "get_meta_section: nofile doesn't exist: "
173VAL=$(get_meta_section nofile.ini test1)
174check_result "$VAL" ""
175
176echo -n "get_meta_section: nofile doesn't exist: "
177VAL=$(get_meta_section nofile.ini test0 test0.conf)
178check_result "$VAL" ""
179
180echo -n "merge_config_file test1c exists: "
181create_test1c
182merge_config_file test.conf test1 test1c.conf
183VAL=$(cat test1c.conf)
184# iniset adds values immediately under the section header
185EXPECT_VAL="[eee]
186multi = foo2
187# original comment
188type=new"
189check_result "$VAL" "$EXPECT_VAL"
190
191echo -n "merge_config_file test2a exists: "
192create_test2a
193merge_config_file test.conf test2 test2a.conf
194VAL=$(cat test2a.conf)
195# iniset adds values immediately under the section header
196EXPECT_VAL="[ddd]
197additional = true
198# original comment
199type=new"
200check_result "$VAL" "$EXPECT_VAL"
201
202echo -n "merge_config_file test2a not exist: "
203rm test2a.conf
204merge_config_file test.conf test2 test2a.conf
205VAL=$(cat test2a.conf)
206# iniset adds a blank line if it creates the file...
207EXPECT_VAL="
208[ddd]
Robert Li6ff21ac2014-10-10 12:43:05 -0400209type = new
210additional = true"
211check_result "$VAL" "$EXPECT_VAL"
212
213echo -n "merge_config_file test-multi-sections: "
214rm -f test-multi-sections.conf
215merge_config_file test.conf test-multi-sections test-multi-sections.conf
216VAL=$(cat test-multi-sections.conf)
217EXPECT_VAL='
218[sec-1]
219cfg_item1 = abcd
220cfg_item2 = efgh
221
222[sec-2]
223cfg_item1 = abcd
224cfg_item3 = /1/2/3/4:5
225cfg_item4 = end
226
227[sec-3]
228cfg_item5 = 5555
229cfg_item5 = 5555another
230cfg_item6 = 6666'
231check_result "$VAL" "$EXPECT_VAL"
232
233echo -n "merge_config_file test-multiline: "
234rm -f test-multiline.conf
235merge_config_file test.conf test-multiline test-multiline.conf
236VAL=$(cat test-multiline.conf)
237EXPECT_VAL='
238[multi]
239cfg_item1 = "ab":"cd", "ef": "gh"
240cfg_item1 = abcd
241cfg_item2 = efgh'
Dean Troyer893e6632013-09-13 15:05:51 -0500242check_result "$VAL" "$EXPECT_VAL"
243
244echo -n "merge_config_group test2: "
245rm test2a.conf
246merge_config_group test.conf test2
247VAL=$(cat test2a.conf)
248# iniset adds a blank line if it creates the file...
249EXPECT_VAL="
250[ddd]
Robert Li6ff21ac2014-10-10 12:43:05 -0400251type = new
252additional = true"
Dean Troyer893e6632013-09-13 15:05:51 -0500253check_result "$VAL" "$EXPECT_VAL"
254
255echo -n "merge_config_group test2 no conf file: "
256rm test2a.conf
257merge_config_group x-test.conf test2
258if [[ ! -r test2a.conf ]]; then
259 echo "OK"
260else
261 echo "failed: $VAL != $EXPECT_VAL"
262fi
263
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600264echo -n "merge_config_file test-space: "
265rm -f test-space.conf
266merge_config_file test.conf test3 test-space.conf
267VAL=$(cat test-space.conf)
268# iniset adds a blank line if it creates the file...
269EXPECT_VAL="
270[DEFAULT]
271attribute = value"
272check_result "$VAL" "$EXPECT_VAL"
273
Ian Wienande2c9fee2014-09-26 09:42:11 +1000274echo -n "merge_config_file test-quote: "
275rm -f test-quote.conf
276merge_config_file test.conf test-quote test-quote.conf
277VAL=$(cat test-quote.conf)
278EXPECT_VAL='
279[foo]
280foo = "foo bar" "baz"'
281check_result "$VAL" "$EXPECT_VAL"
282
Ryota MIBU410f5c02014-04-04 02:00:31 +0900283echo -n "merge_config_group test4 variable filename: "
284setup_test4
285merge_config_group test.conf test4
286VAL=$(cat test-etc/test4.conf)
287EXPECT_VAL="[fff]
288# original comment
289type=new"
290check_result "$VAL" "$EXPECT_VAL"
291
292echo -n "merge_config_group test4 variable filename (not exist): "
293setup_test4
294rm test-etc/test4.conf
295merge_config_group test.conf test4
296VAL=$(cat test-etc/test4.conf)
297EXPECT_VAL="
298[fff]
299type = new"
300check_result "$VAL" "$EXPECT_VAL"
301
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400302echo -n "merge_config_file test5 equals in value: "
303rm -f test-equals.conf
304merge_config_file test.conf test5 test-equals.conf
305VAL=$(cat test-equals.conf)
306# iniset adds a blank line if it creates the file...
307EXPECT_VAL="
308[DEFAULT]
309drivers = driver=python.import.path.Driver"
310check_result "$VAL" "$EXPECT_VAL"
311
312echo -n "merge_config_file test6 value stripped: "
313rm -f test-strip.conf
314merge_config_file test.conf test6 test-strip.conf
315VAL=$(cat test-strip.conf)
316# iniset adds a blank line if it creates the file...
317EXPECT_VAL="
318[DEFAULT]
319attr = strip_trailing_space"
320check_result "$VAL" "$EXPECT_VAL"
321
322rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf test-equals.conf test-strip.conf
Robert Li6ff21ac2014-10-10 12:43:05 -0400323rm -f test-multiline.conf test-multi-sections.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900324rm -rf test-etc