blob: fed2e7d477e4326fd66da2415fc2147aecf814e0 [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"
15function check_result() {
16 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
29function create_test1c() {
30 cat >test1c.conf <<EOF
31[eee]
32# original comment
33type=original
34EOF
35}
36
37function create_test2a() {
38 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
73EOF
74
75
76echo -n "get_meta_section_files: test0 doesn't exist: "
77VAL=$(get_meta_section_files test.conf test0)
78check_result "$VAL" ""
79
80echo -n "get_meta_section_files: test1 3 files: "
81VAL=$(get_meta_section_files test.conf test1)
82EXPECT_VAL="test1a.conf
83test1b.conf
84test1c.conf"
85check_result "$VAL" "$EXPECT_VAL"
86
87echo -n "get_meta_section_files: test2 1 file: "
88VAL=$(get_meta_section_files test.conf test2)
89EXPECT_VAL="test2a.conf"
90check_result "$VAL" "$EXPECT_VAL"
91
92
93# Get a section from a group that doesn't exist
94echo -n "get_meta_section: test0 doesn't exist: "
95VAL=$(get_meta_section test.conf test0 test0.conf)
96check_result "$VAL" ""
97
98# Get a single section from a group with multiple files
99echo -n "get_meta_section: test1c single section: "
100VAL=$(get_meta_section test.conf test1 test1c.conf)
101check_result "$VAL" "$TEST_1C_ADD"
102
103# Get a single section from a group with a single file
104echo -n "get_meta_section: test2a single section: "
105VAL=$(get_meta_section test.conf test2 test2a.conf)
106EXPECT_VAL="[ddd]
107# new comment
108type=new
109additional=true"
110check_result "$VAL" "$EXPECT_VAL"
111
112# Get a single section that doesn't exist from a group
113echo -n "get_meta_section: test2z.conf not in test2: "
114VAL=$(get_meta_section test.conf test2 test2z.conf)
115check_result "$VAL" ""
116
117# Get a section from a conf file that doesn't exist
118echo -n "get_meta_section: nofile doesn't exist: "
119VAL=$(get_meta_section nofile.ini test1)
120check_result "$VAL" ""
121
122echo -n "get_meta_section: nofile doesn't exist: "
123VAL=$(get_meta_section nofile.ini test0 test0.conf)
124check_result "$VAL" ""
125
126echo -n "merge_config_file test1c exists: "
127create_test1c
128merge_config_file test.conf test1 test1c.conf
129VAL=$(cat test1c.conf)
130# iniset adds values immediately under the section header
131EXPECT_VAL="[eee]
132multi = foo2
133# original comment
134type=new"
135check_result "$VAL" "$EXPECT_VAL"
136
137echo -n "merge_config_file test2a exists: "
138create_test2a
139merge_config_file test.conf test2 test2a.conf
140VAL=$(cat test2a.conf)
141# iniset adds values immediately under the section header
142EXPECT_VAL="[ddd]
143additional = true
144# original comment
145type=new"
146check_result "$VAL" "$EXPECT_VAL"
147
148echo -n "merge_config_file test2a not exist: "
149rm test2a.conf
150merge_config_file test.conf test2 test2a.conf
151VAL=$(cat test2a.conf)
152# iniset adds a blank line if it creates the file...
153EXPECT_VAL="
154[ddd]
155additional = true
156type = new"
157check_result "$VAL" "$EXPECT_VAL"
158
159echo -n "merge_config_group test2: "
160rm test2a.conf
161merge_config_group test.conf test2
162VAL=$(cat test2a.conf)
163# iniset adds a blank line if it creates the file...
164EXPECT_VAL="
165[ddd]
166additional = true
167type = new"
168check_result "$VAL" "$EXPECT_VAL"
169
170echo -n "merge_config_group test2 no conf file: "
171rm test2a.conf
172merge_config_group x-test.conf test2
173if [[ ! -r test2a.conf ]]; then
174 echo "OK"
175else
176 echo "failed: $VAL != $EXPECT_VAL"
177fi
178
179rm -f test.conf test1c.conf test2a.conf