Browse code

Fix a couple of INI whitespace bugs

* iniset() bails if no section or option (attribute) is supplied
* merge_config_file() properly skips lines with only whitespace

* Also split the ini-tests into their own script

Bug 1257954

Change-Id: Ie31c5bd0df8dfed129fbcf1e37228aaf25e9305d

Dean Troyer authored on 2013/12/05 08:20:28
Showing 5 changed files
... ...
@@ -729,6 +729,8 @@ function iniset() {
729 729
     local option=$3
730 730
     local value=$4
731 731
 
732
+    [[ -z $section || -z $option ]] && return
733
+
732 734
     if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
733 735
         # Add section at the end
734 736
         echo -e "\n[$section]" >>"$file"
... ...
@@ -95,7 +95,7 @@ function merge_config_file() {
95 95
         /^ *\#/ {
96 96
             next
97 97
         }
98
-        /^.+/ {
98
+        /^[^ \t]+/ {
99 99
             split($0, d, " *= *")
100 100
             print "iniset " configfile " " section " " d[1] " \"" d[2] "\""
101 101
         }
... ...
@@ -38,195 +38,6 @@ if [[ $? = 0 ]]; then
38 38
 fi
39 39
 
40 40
 
41
-echo "Testing INI functions"
42
-
43
-cat >test.ini <<EOF
44
-[default]
45
-# comment an option
46
-#log_file=./log.conf
47
-log_file=/etc/log.conf
48
-handlers=do not disturb
49
-
50
-[aaa]
51
-# the commented option should not change
52
-#handlers=cc,dd
53
-handlers = aa, bb
54
-
55
-[bbb]
56
-handlers=ee,ff
57
-
58
-[ ccc ]
59
-spaces  =  yes
60
-
61
-[ddd]
62
-empty =
63
-
64
-[eee]
65
-multi = foo1
66
-multi = foo2
67
-EOF
68
-
69
-# Test with spaces
70
-
71
-VAL=$(iniget test.ini aaa handlers)
72
-if [[ "$VAL" == "aa, bb" ]]; then
73
-    echo "OK: $VAL"
74
-else
75
-    echo "iniget failed: $VAL"
76
-fi
77
-
78
-iniset test.ini aaa handlers "11, 22"
79
-
80
-VAL=$(iniget test.ini aaa handlers)
81
-if [[ "$VAL" == "11, 22" ]]; then
82
-    echo "OK: $VAL"
83
-else
84
-    echo "iniget failed: $VAL"
85
-fi
86
-
87
-# Test with spaces in section header
88
-
89
-VAL=$(iniget test.ini " ccc " spaces)
90
-if [[ "$VAL" == "yes" ]]; then
91
-    echo "OK: $VAL"
92
-else
93
-    echo "iniget failed: $VAL"
94
-fi
95
-
96
-iniset test.ini "b b" opt_ion 42
97
-
98
-VAL=$(iniget test.ini "b b" opt_ion)
99
-if [[ "$VAL" == "42" ]]; then
100
-    echo "OK: $VAL"
101
-else
102
-    echo "iniget failed: $VAL"
103
-fi
104
-
105
-# Test without spaces, end of file
106
-
107
-VAL=$(iniget test.ini bbb handlers)
108
-if [[ "$VAL" == "ee,ff" ]]; then
109
-    echo "OK: $VAL"
110
-else
111
-    echo "iniget failed: $VAL"
112
-fi
113
-
114
-iniset test.ini bbb handlers "33,44"
115
-
116
-VAL=$(iniget test.ini bbb handlers)
117
-if [[ "$VAL" == "33,44" ]]; then
118
-    echo "OK: $VAL"
119
-else
120
-    echo "iniget failed: $VAL"
121
-fi
122
-
123
-# test empty option
124
-if ini_has_option test.ini ddd empty; then
125
-    echo "OK: ddd.empty present"
126
-else
127
-    echo "ini_has_option failed: ddd.empty not found"
128
-fi
129
-
130
-# test non-empty option
131
-if ini_has_option test.ini bbb handlers; then
132
-    echo "OK: bbb.handlers present"
133
-else
134
-    echo "ini_has_option failed: bbb.handlers not found"
135
-fi
136
-
137
-# test changing empty option
138
-iniset test.ini ddd empty "42"
139
-
140
-VAL=$(iniget test.ini ddd empty)
141
-if [[ "$VAL" == "42" ]]; then
142
-    echo "OK: $VAL"
143
-else
144
-    echo "iniget failed: $VAL"
145
-fi
146
-
147
-# Test section not exist
148
-
149
-VAL=$(iniget test.ini zzz handlers)
150
-if [[ -z "$VAL" ]]; then
151
-    echo "OK: zzz not present"
152
-else
153
-    echo "iniget failed: $VAL"
154
-fi
155
-
156
-iniset test.ini zzz handlers "999"
157
-
158
-VAL=$(iniget test.ini zzz handlers)
159
-if [[ -n "$VAL" ]]; then
160
-    echo "OK: zzz not present"
161
-else
162
-    echo "iniget failed: $VAL"
163
-fi
164
-
165
-# Test option not exist
166
-
167
-VAL=$(iniget test.ini aaa debug)
168
-if [[ -z "$VAL" ]]; then
169
-    echo "OK aaa.debug not present"
170
-else
171
-    echo "iniget failed: $VAL"
172
-fi
173
-
174
-if ! ini_has_option test.ini aaa debug; then
175
-    echo "OK aaa.debug not present"
176
-else
177
-    echo "ini_has_option failed: aaa.debug"
178
-fi
179
-
180
-iniset test.ini aaa debug "999"
181
-
182
-VAL=$(iniget test.ini aaa debug)
183
-if [[ -n "$VAL" ]]; then
184
-    echo "OK aaa.debug present"
185
-else
186
-    echo "iniget failed: $VAL"
187
-fi
188
-
189
-# Test comments
190
-
191
-inicomment test.ini aaa handlers
192
-
193
-VAL=$(iniget test.ini aaa handlers)
194
-if [[ -z "$VAL" ]]; then
195
-    echo "OK"
196
-else
197
-    echo "inicomment failed: $VAL"
198
-fi
199
-
200
-# Test multiple line iniset/iniget
201
-iniset_multiline test.ini eee multi bar1 bar2
202
-
203
-VAL=$(iniget_multiline test.ini eee multi)
204
-if [[ "$VAL" == "bar1 bar2" ]]; then
205
-    echo "OK: iniset_multiline"
206
-else
207
-    echo "iniset_multiline failed: $VAL"
208
-fi
209
-
210
-# Test iniadd with exiting values
211
-iniadd test.ini eee multi bar3
212
-VAL=$(iniget_multiline test.ini eee multi)
213
-if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
214
-    echo "OK: iniadd"
215
-else
216
-    echo "iniadd failed: $VAL"
217
-fi
218
-
219
-# Test iniadd with non-exiting values
220
-iniadd test.ini eee non-multi foobar1 foobar2
221
-VAL=$(iniget_multiline test.ini eee non-multi)
222
-if [[ "$VAL" == "foobar1 foobar2" ]]; then
223
-    echo "OK: iniadd with non-exiting value"
224
-else
225
-    echo "iniadd with non-exsting failed: $VAL"
226
-fi
227
-
228
-rm test.ini
229
-
230 41
 # Enabling/disabling services
231 42
 
232 43
 echo "Testing enable_service()"
... ...
@@ -70,6 +70,12 @@ additional=true
70 70
 
71 71
 [[test1|test1c.conf]]
72 72
 $TEST_1C_ADD
73
+
74
+[[test3|test-space.conf]]
75
+[DEFAULT]
76
+attribute=value
77
+ 
78
+# the above line has a single space
73 79
 EOF
74 80
 
75 81
 
... ...
@@ -176,4 +182,14 @@ else
176 176
     echo "failed: $VAL != $EXPECT_VAL"
177 177
 fi
178 178
 
179
-rm -f test.conf test1c.conf test2a.conf
179
+echo -n "merge_config_file test-space: "
180
+rm -f test-space.conf
181
+merge_config_file test.conf test3 test-space.conf
182
+VAL=$(cat test-space.conf)
183
+# iniset adds a blank line if it creates the file...
184
+EXPECT_VAL="
185
+[DEFAULT]
186
+attribute = value"
187
+check_result "$VAL" "$EXPECT_VAL"
188
+
189
+rm -f test.conf test1c.conf test2a.conf test-space.conf
180 190
new file mode 100755
... ...
@@ -0,0 +1,220 @@
0
+#!/usr/bin/env bash
1
+
2
+# Tests for DevStack INI functions
3
+
4
+TOP=$(cd $(dirname "$0")/.. && pwd)
5
+
6
+# Import common functions
7
+source $TOP/functions
8
+
9
+
10
+echo "Testing INI functions"
11
+
12
+cat >test.ini <<EOF
13
+[default]
14
+# comment an option
15
+#log_file=./log.conf
16
+log_file=/etc/log.conf
17
+handlers=do not disturb
18
+
19
+[aaa]
20
+# the commented option should not change
21
+#handlers=cc,dd
22
+handlers = aa, bb
23
+
24
+[bbb]
25
+handlers=ee,ff
26
+
27
+[ ccc ]
28
+spaces  =  yes
29
+
30
+[ddd]
31
+empty =
32
+
33
+[eee]
34
+multi = foo1
35
+multi = foo2
36
+EOF
37
+
38
+# Test with missing arguments
39
+
40
+BEFORE=$(cat test.ini)
41
+
42
+echo -n "iniset: test missing attribute argument: "
43
+iniset test.ini aaa
44
+NO_ATTRIBUTE=$(cat test.ini)
45
+if [[ "$BEFORE" == "$NO_ATTRIBUTE" ]]; then
46
+    echo "OK"
47
+else
48
+    echo "failed"
49
+fi
50
+
51
+echo -n "iniset: test missing section argument: "
52
+iniset test.ini
53
+NO_SECTION=$(cat test.ini)
54
+if [[ "$BEFORE" == "$NO_SECTION" ]]; then
55
+    echo "OK"
56
+else
57
+    echo "failed"
58
+fi
59
+
60
+# Test with spaces
61
+
62
+VAL=$(iniget test.ini aaa handlers)
63
+if [[ "$VAL" == "aa, bb" ]]; then
64
+    echo "OK: $VAL"
65
+else
66
+    echo "iniget failed: $VAL"
67
+fi
68
+
69
+iniset test.ini aaa handlers "11, 22"
70
+
71
+VAL=$(iniget test.ini aaa handlers)
72
+if [[ "$VAL" == "11, 22" ]]; then
73
+    echo "OK: $VAL"
74
+else
75
+    echo "iniget failed: $VAL"
76
+fi
77
+
78
+# Test with spaces in section header
79
+
80
+VAL=$(iniget test.ini " ccc " spaces)
81
+if [[ "$VAL" == "yes" ]]; then
82
+    echo "OK: $VAL"
83
+else
84
+    echo "iniget failed: $VAL"
85
+fi
86
+
87
+iniset test.ini "b b" opt_ion 42
88
+
89
+VAL=$(iniget test.ini "b b" opt_ion)
90
+if [[ "$VAL" == "42" ]]; then
91
+    echo "OK: $VAL"
92
+else
93
+    echo "iniget failed: $VAL"
94
+fi
95
+
96
+# Test without spaces, end of file
97
+
98
+VAL=$(iniget test.ini bbb handlers)
99
+if [[ "$VAL" == "ee,ff" ]]; then
100
+    echo "OK: $VAL"
101
+else
102
+    echo "iniget failed: $VAL"
103
+fi
104
+
105
+iniset test.ini bbb handlers "33,44"
106
+
107
+VAL=$(iniget test.ini bbb handlers)
108
+if [[ "$VAL" == "33,44" ]]; then
109
+    echo "OK: $VAL"
110
+else
111
+    echo "iniget failed: $VAL"
112
+fi
113
+
114
+# test empty option
115
+if ini_has_option test.ini ddd empty; then
116
+    echo "OK: ddd.empty present"
117
+else
118
+    echo "ini_has_option failed: ddd.empty not found"
119
+fi
120
+
121
+# test non-empty option
122
+if ini_has_option test.ini bbb handlers; then
123
+    echo "OK: bbb.handlers present"
124
+else
125
+    echo "ini_has_option failed: bbb.handlers not found"
126
+fi
127
+
128
+# test changing empty option
129
+iniset test.ini ddd empty "42"
130
+
131
+VAL=$(iniget test.ini ddd empty)
132
+if [[ "$VAL" == "42" ]]; then
133
+    echo "OK: $VAL"
134
+else
135
+    echo "iniget failed: $VAL"
136
+fi
137
+
138
+# Test section not exist
139
+
140
+VAL=$(iniget test.ini zzz handlers)
141
+if [[ -z "$VAL" ]]; then
142
+    echo "OK: zzz not present"
143
+else
144
+    echo "iniget failed: $VAL"
145
+fi
146
+
147
+iniset test.ini zzz handlers "999"
148
+
149
+VAL=$(iniget test.ini zzz handlers)
150
+if [[ -n "$VAL" ]]; then
151
+    echo "OK: zzz not present"
152
+else
153
+    echo "iniget failed: $VAL"
154
+fi
155
+
156
+# Test option not exist
157
+
158
+VAL=$(iniget test.ini aaa debug)
159
+if [[ -z "$VAL" ]]; then
160
+    echo "OK aaa.debug not present"
161
+else
162
+    echo "iniget failed: $VAL"
163
+fi
164
+
165
+if ! ini_has_option test.ini aaa debug; then
166
+    echo "OK aaa.debug not present"
167
+else
168
+    echo "ini_has_option failed: aaa.debug"
169
+fi
170
+
171
+iniset test.ini aaa debug "999"
172
+
173
+VAL=$(iniget test.ini aaa debug)
174
+if [[ -n "$VAL" ]]; then
175
+    echo "OK aaa.debug present"
176
+else
177
+    echo "iniget failed: $VAL"
178
+fi
179
+
180
+# Test comments
181
+
182
+inicomment test.ini aaa handlers
183
+
184
+VAL=$(iniget test.ini aaa handlers)
185
+if [[ -z "$VAL" ]]; then
186
+    echo "OK"
187
+else
188
+    echo "inicomment failed: $VAL"
189
+fi
190
+
191
+# Test multiple line iniset/iniget
192
+iniset_multiline test.ini eee multi bar1 bar2
193
+
194
+VAL=$(iniget_multiline test.ini eee multi)
195
+if [[ "$VAL" == "bar1 bar2" ]]; then
196
+    echo "OK: iniset_multiline"
197
+else
198
+    echo "iniset_multiline failed: $VAL"
199
+fi
200
+
201
+# Test iniadd with exiting values
202
+iniadd test.ini eee multi bar3
203
+VAL=$(iniget_multiline test.ini eee multi)
204
+if [[ "$VAL" == "bar1 bar2 bar3" ]]; then
205
+    echo "OK: iniadd"
206
+else
207
+    echo "iniadd failed: $VAL"
208
+fi
209
+
210
+# Test iniadd with non-exiting values
211
+iniadd test.ini eee non-multi foobar1 foobar2
212
+VAL=$(iniget_multiline test.ini eee non-multi)
213
+if [[ "$VAL" == "foobar1 foobar2" ]]; then
214
+    echo "OK: iniadd with non-exiting value"
215
+else
216
+    echo "iniadd with non-exsting failed: $VAL"
217
+fi
218
+
219
+rm test.ini