blob: 3aef6f39bd6601bc4f939182a5d3c9e79d3bf794 [file] [log] [blame]
Dean Troyer2ac8b3f2013-12-04 17:20:28 -06001#!/usr/bin/env bash
2
3# Tests for DevStack INI functions
4
5TOP=$(cd $(dirname "$0")/.. && pwd)
6
Dean Troyerbf2ad702015-03-09 15:16:10 -05007# Import config functions
8source $TOP/inc/ini-config
Dean Troyer2ac8b3f2013-12-04 17:20:28 -06009
Ian Wienandfcdca052015-04-17 13:02:49 +100010source $TOP/tests/unittest.sh
11
12set -e
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060013
14echo "Testing INI functions"
15
Ian Wienandb997db62015-07-22 10:05:32 +100016INI_TMP_DIR=$(mktemp -d)
17INI_TMP_ETC_DIR=$INI_TMP_DIR/etc
18TEST_INI=${INI_TMP_ETC_DIR}/test.ini
19mkdir ${INI_TMP_ETC_DIR}
20
21echo "Creating $TEST_INI"
22cat >${TEST_INI} <<EOF
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060023[default]
24# comment an option
25#log_file=./log.conf
26log_file=/etc/log.conf
27handlers=do not disturb
28
29[aaa]
30# the commented option should not change
31#handlers=cc,dd
32handlers = aa, bb
33
34[bbb]
35handlers=ee,ff
36
37[ ccc ]
38spaces = yes
39
40[ddd]
41empty =
42
43[eee]
44multi = foo1
45multi = foo2
Doug Wiegley1f65fd62014-12-13 11:56:16 -070046
47# inidelete(a)
48[del_separate_options]
49a=b
50b=c
51
52# inidelete(a)
53[del_same_option]
54a=b
55a=c
56
57# inidelete(a)
58[del_missing_option]
59b=c
60
61# inidelete(a)
62[del_missing_option_multi]
63b=c
64b=d
65
66# inidelete(a)
67[del_no_options]
68
69# inidelete(a)
70# no section - del_no_section
71
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060072EOF
73
74# Test with missing arguments
75
Ian Wienandb997db62015-07-22 10:05:32 +100076BEFORE=$(cat ${TEST_INI})
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060077
Ian Wienandb997db62015-07-22 10:05:32 +100078iniset ${TEST_INI} aaa
79NO_ATTRIBUTE=$(cat ${TEST_INI})
80assert_equal "$BEFORE" "$NO_ATTRIBUTE" "test missing attribute argument"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060081
Ian Wienandb997db62015-07-22 10:05:32 +100082iniset ${TEST_INI}
83NO_SECTION=$(cat ${TEST_INI})
84assert_equal "$BEFORE" "$NO_SECTION" "missing section argument"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060085
Ian Wienandb997db62015-07-22 10:05:32 +100086# Test with spaces in values
87VAL=$(iniget ${TEST_INI} aaa handlers)
88assert_equal "$VAL" "aa, bb" "iniget spaces in option"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060089
Ian Wienandb997db62015-07-22 10:05:32 +100090iniset ${TEST_INI} aaa handlers "11, 22"
91VAL=$(iniget ${TEST_INI} aaa handlers)
92assert_equal "$VAL" "11, 22" "iniset spaces in option"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060093
94# Test with spaces in section header
Ian Wienandb997db62015-07-22 10:05:32 +100095VAL=$(iniget ${TEST_INI} " ccc " spaces)
96assert_equal "$VAL" "yes" "iniget with section header space"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -060097
Ian Wienandb997db62015-07-22 10:05:32 +100098iniset ${TEST_INI} "b b" opt_ion 42
99VAL=$(iniget ${TEST_INI} "b b" opt_ion)
100assert_equal "$VAL" "42" "iniset with section header space"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600101
102# Test without spaces, end of file
Ian Wienandb997db62015-07-22 10:05:32 +1000103VAL=$(iniget ${TEST_INI} bbb handlers)
104assert_equal "$VAL" "ee,ff" "iniget at EOF"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600105
Ian Wienandb997db62015-07-22 10:05:32 +1000106iniset ${TEST_INI} bbb handlers "33,44"
107VAL=$(iniget ${TEST_INI} bbb handlers)
108assert_equal "$VAL" "33,44" "inset at EOF"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600109
110# test empty option
Ian Wienandb997db62015-07-22 10:05:32 +1000111if ini_has_option ${TEST_INI} ddd empty; then
112 passed "ini_has_option: ddd.empty present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600113else
Ian Wienandfcdca052015-04-17 13:02:49 +1000114 failed "ini_has_option failed: ddd.empty not found"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600115fi
116
117# test non-empty option
Ian Wienandb997db62015-07-22 10:05:32 +1000118if ini_has_option ${TEST_INI} bbb handlers; then
119 passed "ini_has_option: bbb.handlers present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600120else
Ian Wienandfcdca052015-04-17 13:02:49 +1000121 failed "ini_has_option failed: bbb.handlers not found"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600122fi
123
124# test changing empty option
Ian Wienandb997db62015-07-22 10:05:32 +1000125iniset ${TEST_INI} ddd empty "42"
126VAL=$(iniget ${TEST_INI} ddd empty)
127assert_equal "$VAL" "42" "change empty option"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600128
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000129# test pipe in option
Ian Wienandb997db62015-07-22 10:05:32 +1000130iniset ${TEST_INI} aaa handlers "a|b"
131VAL=$(iniget ${TEST_INI} aaa handlers)
132assert_equal "$VAL" "a|b" "pipe in option"
Andrea Frittolicd7d9562013-12-05 08:09:12 +0000133
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600134# Test section not exist
Ian Wienandb997db62015-07-22 10:05:32 +1000135VAL=$(iniget ${TEST_INI} zzz handlers)
136assert_empty VAL "section does not exist"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600137
138# Test option not exist
Ian Wienandb997db62015-07-22 10:05:32 +1000139VAL=$(iniget ${TEST_INI} aaa debug)
140assert_empty VAL "option does not exist"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600141
Ian Wienandb997db62015-07-22 10:05:32 +1000142if ! ini_has_option ${TEST_INI} aaa debug; then
143 passed "ini_has_option: aaa.debug not present"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600144else
Ian Wienandfcdca052015-04-17 13:02:49 +1000145 failed "ini_has_option failed: aaa.debug"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600146fi
147
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600148# Test comments
Ian Wienandb997db62015-07-22 10:05:32 +1000149inicomment ${TEST_INI} aaa handlers
150VAL=$(iniget ${TEST_INI} aaa handlers)
151assert_empty VAL "test inicomment"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600152
153# Test multiple line iniset/iniget
Ian Wienandb997db62015-07-22 10:05:32 +1000154iniset_multiline ${TEST_INI} eee multi bar1 bar2
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600155
Ian Wienandb997db62015-07-22 10:05:32 +1000156VAL=$(iniget_multiline ${TEST_INI} eee multi)
157assert_equal "$VAL" "bar1 bar2" "iniget_multiline"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600158
159# Test iniadd with exiting values
Ian Wienandb997db62015-07-22 10:05:32 +1000160iniadd ${TEST_INI} eee multi bar3
161VAL=$(iniget_multiline ${TEST_INI} eee multi)
162assert_equal "$VAL" "bar1 bar2 bar3" "iniadd with existing values"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600163
164# Test iniadd with non-exiting values
Ian Wienandb997db62015-07-22 10:05:32 +1000165iniadd ${TEST_INI} eee non-multi foobar1 foobar2
166VAL=$(iniget_multiline ${TEST_INI} eee non-multi)
167assert_equal "$VAL" "foobar1 foobar2" "iniadd non-existing values"
Dean Troyer2ac8b3f2013-12-04 17:20:28 -0600168
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700169# Test inidelete
170del_cases="
171 del_separate_options
172 del_same_option
173 del_missing_option
174 del_missing_option_multi
175 del_no_options
176 del_no_section"
177
178for x in $del_cases; do
Ian Wienandb997db62015-07-22 10:05:32 +1000179 inidelete ${TEST_INI} $x a
180 VAL=$(iniget_multiline ${TEST_INI} $x a)
181 assert_empty VAL "inidelete $x"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700182 if [ "$x" = "del_separate_options" -o \
183 "$x" = "del_missing_option" -o \
184 "$x" = "del_missing_option_multi" ]; then
Ian Wienandb997db62015-07-22 10:05:32 +1000185 VAL=$(iniget_multiline ${TEST_INI} $x b)
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700186 if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
Ian Wienandb997db62015-07-22 10:05:32 +1000187 passed "inidelete other_options $x"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700188 else
Ian Wienandb997db62015-07-22 10:05:32 +1000189 failed "inidelete other_option $x: $VAL"
Doug Wiegley1f65fd62014-12-13 11:56:16 -0700190 fi
191 fi
192done
193
Ian Wienandb997db62015-07-22 10:05:32 +1000194rm -rf ${INI_TMP_DIR}
Ian Wienandfcdca052015-04-17 13:02:49 +1000195
196report_results