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