blob: 7cf75d0c4fcddbb34457f7ed0a402bf0503f05d6 [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"
Ryota MIBU410f5c02014-04-04 02:00:31 +090098EOF
Dean Troyer893e6632013-09-13 15:05:51 -050099
100echo -n "get_meta_section_files: test0 doesn't exist: "
101VAL=$(get_meta_section_files test.conf test0)
102check_result "$VAL" ""
103
104echo -n "get_meta_section_files: test1 3 files: "
105VAL=$(get_meta_section_files test.conf test1)
106EXPECT_VAL="test1a.conf
107test1b.conf
108test1c.conf"
109check_result "$VAL" "$EXPECT_VAL"
110
111echo -n "get_meta_section_files: test2 1 file: "
112VAL=$(get_meta_section_files test.conf test2)
113EXPECT_VAL="test2a.conf"
114check_result "$VAL" "$EXPECT_VAL"
115
116
117# Get a section from a group that doesn't exist
118echo -n "get_meta_section: test0 doesn't exist: "
119VAL=$(get_meta_section test.conf test0 test0.conf)
120check_result "$VAL" ""
121
122# Get a single section from a group with multiple files
123echo -n "get_meta_section: test1c single section: "
124VAL=$(get_meta_section test.conf test1 test1c.conf)
125check_result "$VAL" "$TEST_1C_ADD"
126
127# Get a single section from a group with a single file
128echo -n "get_meta_section: test2a single section: "
129VAL=$(get_meta_section test.conf test2 test2a.conf)
130EXPECT_VAL="[ddd]
131# new comment
132type=new
133additional=true"
134check_result "$VAL" "$EXPECT_VAL"
135
136# Get a single section that doesn't exist from a group
137echo -n "get_meta_section: test2z.conf not in test2: "
138VAL=$(get_meta_section test.conf test2 test2z.conf)
139check_result "$VAL" ""
140
141# Get a section from a conf file that doesn't exist
142echo -n "get_meta_section: nofile doesn't exist: "
143VAL=$(get_meta_section nofile.ini test1)
144check_result "$VAL" ""
145
146echo -n "get_meta_section: nofile doesn't exist: "
147VAL=$(get_meta_section nofile.ini test0 test0.conf)
148check_result "$VAL" ""
149
150echo -n "merge_config_file test1c exists: "
151create_test1c
152merge_config_file test.conf test1 test1c.conf
153VAL=$(cat test1c.conf)
154# iniset adds values immediately under the section header
155EXPECT_VAL="[eee]
156multi = foo2
157# original comment
158type=new"
159check_result "$VAL" "$EXPECT_VAL"
160
161echo -n "merge_config_file test2a exists: "
162create_test2a
163merge_config_file test.conf test2 test2a.conf
164VAL=$(cat test2a.conf)
165# iniset adds values immediately under the section header
166EXPECT_VAL="[ddd]
167additional = true
168# original comment
169type=new"
170check_result "$VAL" "$EXPECT_VAL"
171
172echo -n "merge_config_file test2a not exist: "
173rm test2a.conf
174merge_config_file test.conf test2 test2a.conf
175VAL=$(cat test2a.conf)
176# iniset adds a blank line if it creates the file...
177EXPECT_VAL="
178[ddd]
179additional = true
180type = new"
181check_result "$VAL" "$EXPECT_VAL"
182
183echo -n "merge_config_group test2: "
184rm test2a.conf
185merge_config_group test.conf test2
186VAL=$(cat test2a.conf)
187# iniset adds a blank line if it creates the file...
188EXPECT_VAL="
189[ddd]
190additional = true
191type = new"
192check_result "$VAL" "$EXPECT_VAL"
193
194echo -n "merge_config_group test2 no conf file: "
195rm test2a.conf
196merge_config_group x-test.conf test2
197if [[ ! -r test2a.conf ]]; then
198 echo "OK"
199else
200 echo "failed: $VAL != $EXPECT_VAL"
201fi
202
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600203echo -n "merge_config_file test-space: "
204rm -f test-space.conf
205merge_config_file test.conf test3 test-space.conf
206VAL=$(cat test-space.conf)
207# iniset adds a blank line if it creates the file...
208EXPECT_VAL="
209[DEFAULT]
210attribute = value"
211check_result "$VAL" "$EXPECT_VAL"
212
Ian Wienande2c9fee2014-09-26 09:42:11 +1000213echo -n "merge_config_file test-quote: "
214rm -f test-quote.conf
215merge_config_file test.conf test-quote test-quote.conf
216VAL=$(cat test-quote.conf)
217EXPECT_VAL='
218[foo]
219foo = "foo bar" "baz"'
220check_result "$VAL" "$EXPECT_VAL"
221
Ryota MIBU410f5c02014-04-04 02:00:31 +0900222echo -n "merge_config_group test4 variable filename: "
223setup_test4
224merge_config_group test.conf test4
225VAL=$(cat test-etc/test4.conf)
226EXPECT_VAL="[fff]
227# original comment
228type=new"
229check_result "$VAL" "$EXPECT_VAL"
230
231echo -n "merge_config_group test4 variable filename (not exist): "
232setup_test4
233rm test-etc/test4.conf
234merge_config_group test.conf test4
235VAL=$(cat test-etc/test4.conf)
236EXPECT_VAL="
237[fff]
238type = new"
239check_result "$VAL" "$EXPECT_VAL"
240
Ian Wienande2c9fee2014-09-26 09:42:11 +1000241rm -f test.conf test1c.conf test2a.conf test-quote.conf test-space.conf
Ryota MIBU410f5c02014-04-04 02:00:31 +0900242rm -rf test-etc