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