blob: 6fff29c3968d812edb674748f28f4d287ef604a9 [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
Ryota MIBU410f5c02014-04-04 02:00:31 +0900107EOF
Dean Troyer893e6632013-09-13 15:05:51 -0500108
109echo -n "get_meta_section_files: test0 doesn't exist: "
110VAL=$(get_meta_section_files test.conf test0)
111check_result "$VAL" ""
112
113echo -n "get_meta_section_files: test1 3 files: "
114VAL=$(get_meta_section_files test.conf test1)
115EXPECT_VAL="test1a.conf
116test1b.conf
117test1c.conf"
118check_result "$VAL" "$EXPECT_VAL"
119
120echo -n "get_meta_section_files: test2 1 file: "
121VAL=$(get_meta_section_files test.conf test2)
122EXPECT_VAL="test2a.conf"
123check_result "$VAL" "$EXPECT_VAL"
124
125
126# Get a section from a group that doesn't exist
127echo -n "get_meta_section: test0 doesn't exist: "
128VAL=$(get_meta_section test.conf test0 test0.conf)
129check_result "$VAL" ""
130
131# Get a single section from a group with multiple files
132echo -n "get_meta_section: test1c single section: "
133VAL=$(get_meta_section test.conf test1 test1c.conf)
134check_result "$VAL" "$TEST_1C_ADD"
135
136# Get a single section from a group with a single file
137echo -n "get_meta_section: test2a single section: "
138VAL=$(get_meta_section test.conf test2 test2a.conf)
139EXPECT_VAL="[ddd]
140# new comment
141type=new
142additional=true"
143check_result "$VAL" "$EXPECT_VAL"
144
145# Get a single section that doesn't exist from a group
146echo -n "get_meta_section: test2z.conf not in test2: "
147VAL=$(get_meta_section test.conf test2 test2z.conf)
148check_result "$VAL" ""
149
150# Get a section from a conf file that doesn't exist
151echo -n "get_meta_section: nofile doesn't exist: "
152VAL=$(get_meta_section nofile.ini test1)
153check_result "$VAL" ""
154
155echo -n "get_meta_section: nofile doesn't exist: "
156VAL=$(get_meta_section nofile.ini test0 test0.conf)
157check_result "$VAL" ""
158
159echo -n "merge_config_file test1c exists: "
160create_test1c
161merge_config_file test.conf test1 test1c.conf
162VAL=$(cat test1c.conf)
163# iniset adds values immediately under the section header
164EXPECT_VAL="[eee]
165multi = foo2
166# original comment
167type=new"
168check_result "$VAL" "$EXPECT_VAL"
169
170echo -n "merge_config_file test2a exists: "
171create_test2a
172merge_config_file test.conf test2 test2a.conf
173VAL=$(cat test2a.conf)
174# iniset adds values immediately under the section header
175EXPECT_VAL="[ddd]
176additional = true
177# original comment
178type=new"
179check_result "$VAL" "$EXPECT_VAL"
180
181echo -n "merge_config_file test2a not exist: "
182rm test2a.conf
183merge_config_file test.conf test2 test2a.conf
184VAL=$(cat test2a.conf)
185# iniset adds a blank line if it creates the file...
186EXPECT_VAL="
187[ddd]
188additional = true
189type = new"
190check_result "$VAL" "$EXPECT_VAL"
191
192echo -n "merge_config_group test2: "
193rm test2a.conf
194merge_config_group test.conf test2
195VAL=$(cat test2a.conf)
196# iniset adds a blank line if it creates the file...
197EXPECT_VAL="
198[ddd]
199additional = true
200type = new"
201check_result "$VAL" "$EXPECT_VAL"
202
203echo -n "merge_config_group test2 no conf file: "
204rm test2a.conf
205merge_config_group x-test.conf test2
206if [[ ! -r test2a.conf ]]; then
207 echo "OK"
208else
209 echo "failed: $VAL != $EXPECT_VAL"
210fi
211
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600212echo -n "merge_config_file test-space: "
213rm -f test-space.conf
214merge_config_file test.conf test3 test-space.conf
215VAL=$(cat test-space.conf)
216# iniset adds a blank line if it creates the file...
217EXPECT_VAL="
218[DEFAULT]
219attribute = value"
220check_result "$VAL" "$EXPECT_VAL"
221
Ian Wienande2c9fee2014-09-26 09:42:11 +1000222echo -n "merge_config_file test-quote: "
223rm -f test-quote.conf
224merge_config_file test.conf test-quote test-quote.conf
225VAL=$(cat test-quote.conf)
226EXPECT_VAL='
227[foo]
228foo = "foo bar" "baz"'
229check_result "$VAL" "$EXPECT_VAL"
230
Ryota MIBU410f5c02014-04-04 02:00:31 +0900231echo -n "merge_config_group test4 variable filename: "
232setup_test4
233merge_config_group test.conf test4
234VAL=$(cat test-etc/test4.conf)
235EXPECT_VAL="[fff]
236# original comment
237type=new"
238check_result "$VAL" "$EXPECT_VAL"
239
240echo -n "merge_config_group test4 variable filename (not exist): "
241setup_test4
242rm test-etc/test4.conf
243merge_config_group test.conf test4
244VAL=$(cat test-etc/test4.conf)
245EXPECT_VAL="
246[fff]
247type = new"
248check_result "$VAL" "$EXPECT_VAL"
249
Fergal Mc Carthycc87c282014-10-09 16:16:42 -0400250echo -n "merge_config_file test5 equals in value: "
251rm -f test-equals.conf
252merge_config_file test.conf test5 test-equals.conf
253VAL=$(cat test-equals.conf)
254# iniset adds a blank line if it creates the file...
255EXPECT_VAL="
256[DEFAULT]
257drivers = driver=python.import.path.Driver"
258check_result "$VAL" "$EXPECT_VAL"
259
260echo -n "merge_config_file test6 value stripped: "
261rm -f test-strip.conf
262merge_config_file test.conf test6 test-strip.conf
263VAL=$(cat test-strip.conf)
264# iniset adds a blank line if it creates the file...
265EXPECT_VAL="
266[DEFAULT]
267attr = strip_trailing_space"
268check_result "$VAL" "$EXPECT_VAL"
269
270rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf test-equals.conf test-strip.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900271rm -rf test-etc