blob: 5700f8df295337de96f97e5987aeb66bc4e72a53 [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
45cat >test.conf <<EOF
46[[test1|test1a.conf]]
47[default]
48# comment an option
49#log_file=./log.conf
50log_file=/etc/log.conf
51handlers=do not disturb
52
53[aaa]
54# the commented option should not change
55#handlers=cc,dd
56handlers = aa, bb
57
58[[test1|test1b.conf]]
59[bbb]
60handlers=ee,ff
61
62[ ccc ]
63spaces = yes
64
65[[test2|test2a.conf]]
66[ddd]
67# new comment
68type=new
69additional=true
70
71[[test1|test1c.conf]]
72$TEST_1C_ADD
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060073
74[[test3|test-space.conf]]
75[DEFAULT]
76attribute=value
77
78# the above line has a single space
Dean Troyer893e6632013-09-13 15:05:51 -050079EOF
80
81
82echo -n "get_meta_section_files: test0 doesn't exist: "
83VAL=$(get_meta_section_files test.conf test0)
84check_result "$VAL" ""
85
86echo -n "get_meta_section_files: test1 3 files: "
87VAL=$(get_meta_section_files test.conf test1)
88EXPECT_VAL="test1a.conf
89test1b.conf
90test1c.conf"
91check_result "$VAL" "$EXPECT_VAL"
92
93echo -n "get_meta_section_files: test2 1 file: "
94VAL=$(get_meta_section_files test.conf test2)
95EXPECT_VAL="test2a.conf"
96check_result "$VAL" "$EXPECT_VAL"
97
98
99# Get a section from a group that doesn't exist
100echo -n "get_meta_section: test0 doesn't exist: "
101VAL=$(get_meta_section test.conf test0 test0.conf)
102check_result "$VAL" ""
103
104# Get a single section from a group with multiple files
105echo -n "get_meta_section: test1c single section: "
106VAL=$(get_meta_section test.conf test1 test1c.conf)
107check_result "$VAL" "$TEST_1C_ADD"
108
109# Get a single section from a group with a single file
110echo -n "get_meta_section: test2a single section: "
111VAL=$(get_meta_section test.conf test2 test2a.conf)
112EXPECT_VAL="[ddd]
113# new comment
114type=new
115additional=true"
116check_result "$VAL" "$EXPECT_VAL"
117
118# Get a single section that doesn't exist from a group
119echo -n "get_meta_section: test2z.conf not in test2: "
120VAL=$(get_meta_section test.conf test2 test2z.conf)
121check_result "$VAL" ""
122
123# Get a section from a conf file that doesn't exist
124echo -n "get_meta_section: nofile doesn't exist: "
125VAL=$(get_meta_section nofile.ini test1)
126check_result "$VAL" ""
127
128echo -n "get_meta_section: nofile doesn't exist: "
129VAL=$(get_meta_section nofile.ini test0 test0.conf)
130check_result "$VAL" ""
131
132echo -n "merge_config_file test1c exists: "
133create_test1c
134merge_config_file test.conf test1 test1c.conf
135VAL=$(cat test1c.conf)
136# iniset adds values immediately under the section header
137EXPECT_VAL="[eee]
138multi = foo2
139# original comment
140type=new"
141check_result "$VAL" "$EXPECT_VAL"
142
143echo -n "merge_config_file test2a exists: "
144create_test2a
145merge_config_file test.conf test2 test2a.conf
146VAL=$(cat test2a.conf)
147# iniset adds values immediately under the section header
148EXPECT_VAL="[ddd]
149additional = true
150# original comment
151type=new"
152check_result "$VAL" "$EXPECT_VAL"
153
154echo -n "merge_config_file test2a not exist: "
155rm test2a.conf
156merge_config_file test.conf test2 test2a.conf
157VAL=$(cat test2a.conf)
158# iniset adds a blank line if it creates the file...
159EXPECT_VAL="
160[ddd]
161additional = true
162type = new"
163check_result "$VAL" "$EXPECT_VAL"
164
165echo -n "merge_config_group test2: "
166rm test2a.conf
167merge_config_group test.conf test2
168VAL=$(cat test2a.conf)
169# iniset adds a blank line if it creates the file...
170EXPECT_VAL="
171[ddd]
172additional = true
173type = new"
174check_result "$VAL" "$EXPECT_VAL"
175
176echo -n "merge_config_group test2 no conf file: "
177rm test2a.conf
178merge_config_group x-test.conf test2
179if [[ ! -r test2a.conf ]]; then
180 echo "OK"
181else
182 echo "failed: $VAL != $EXPECT_VAL"
183fi
184
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600185echo -n "merge_config_file test-space: "
186rm -f test-space.conf
187merge_config_file test.conf test3 test-space.conf
188VAL=$(cat test-space.conf)
189# iniset adds a blank line if it creates the file...
190EXPECT_VAL="
191[DEFAULT]
192attribute = value"
193check_result "$VAL" "$EXPECT_VAL"
194
195rm -f test.conf test1c.conf test2a.conf test-space.conf