Browse code

add local.conf modifying functions

This adds a set of local.conf modifying functions which make it easier
for consuming projects like devstack-gate to programatically add
elements to local.conf structured files.

Change-Id: I3427968c2bd43aba12b3619acc27f73c74f0dabb
Co-Authored-By: fumihiko kakuma <kakuma@valinux.co.jp>
(cherry picked from commit bb35715cfe68ad8d1d2ccb2b2e7eb4143e87d678)

Sean Dague authored on 2016/06/08 00:20:55
Showing 2 changed files
... ...
@@ -274,6 +274,170 @@ function iniget_sections {
274 274
     $xtrace
275 275
 }
276 276
 
277
+# Set a localrc var
278
+function localrc_set {
279
+    local file=$1
280
+    local group="local"
281
+    local conf="localrc"
282
+    local section=""
283
+    local option=$2
284
+    local value=$3
285
+    localconf_set "$file" "$group" "$conf" "$section" "$option" "$value"
286
+}
287
+
288
+# Check if local.conf has section.
289
+function localconf_has_section {
290
+    local file=$1
291
+    local group=$2
292
+    local conf=$3
293
+    local section=$4
294
+    local sep
295
+    sep=$(echo -ne "\x01")
296
+    local line
297
+    line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
298
+        /\[${section}\]/p
299
+    }" "$file")
300
+    [ -n "$line" ]
301
+}
302
+
303
+# Check if local.conf has option.
304
+function localconf_has_option {
305
+    local file=$1
306
+    local group=$2
307
+    local conf=$3
308
+    local section=$4
309
+    local option=$5
310
+    local sep
311
+    sep=$(echo -ne "\x01")
312
+    local line
313
+    if [[ -z "$section" ]]; then
314
+        line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
315
+            /${option}[ \t]*=.*$/p
316
+        }" "$file")
317
+    else
318
+        line=$(sed -ne "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
319
+            /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/{
320
+                /${option}[ \t]*=.*$/p}
321
+        }" "$file")
322
+    fi
323
+    [ -n "$line" ]
324
+}
325
+
326
+# Update option in local.conf.
327
+function localconf_update_option {
328
+    local sudo=$1
329
+    local file=$2
330
+    local group=$3
331
+    local conf=$4
332
+    local section=$5
333
+    local option=$6
334
+    local value=$7
335
+    local sep
336
+    sep=$(echo -ne "\x01")
337
+    if [[ -z "$section" ]]; then
338
+        $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
339
+            s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
340
+        }" "$file"
341
+    else
342
+        $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
343
+            /\[${section}\]/,/\[\[.*\]\]\|\[.*\]/s${sep}^\(${option}[ \t]*=[ \t]*\).*\$${sep}\1${value}${sep}
344
+        }" "$file"
345
+    fi
346
+}
347
+
348
+# Add option in local.conf.
349
+function localconf_add_option {
350
+    local sudo=$1
351
+    local file=$2
352
+    local group=$3
353
+    local conf=$4
354
+    local section=$5
355
+    local option=$6
356
+    local value=$7
357
+    local sep
358
+    sep=$(echo -ne "\x01")
359
+    if [[ -z "$section" ]]; then
360
+        $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} a $option=$value" "$file"
361
+    else
362
+        $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep},\\${sep}\[\[.*\]\]${sep}{
363
+            /\[${section}\]/ a $option=$value
364
+        }" "$file"
365
+    fi
366
+}
367
+
368
+# Add section and option in local.conf.
369
+function localconf_add_section_and_option {
370
+    local sudo=$1
371
+    local file=$2
372
+    local group=$3
373
+    local conf=$4
374
+    local section=$5
375
+    local option=$6
376
+    local value=$7
377
+    local sep
378
+    sep=$(echo -ne "\x01")
379
+    $sudo sed -i -e "\\${sep}^\[\[${group}|${conf}\]\]${sep} {
380
+        a [$section]
381
+        a $option=$value
382
+    }" "$file"
383
+}
384
+
385
+# Set an option in a local.conf file.
386
+# localconf_set [-sudo] config-file group conf-name section option value
387
+#  - if the file does not exist, it is created
388
+function localconf_set {
389
+    local xtrace
390
+    xtrace=$(set +o | grep xtrace)
391
+    set +o xtrace
392
+    local sep
393
+    sep=$(echo -ne "\x01")
394
+    local sudo=""
395
+    if [ $1 == "-sudo" ]; then
396
+        sudo="sudo "
397
+        shift
398
+    fi
399
+    local file=$1
400
+    local group=$2
401
+    local conf=$3
402
+    local section=$4
403
+    local option=$5
404
+    local value=$6
405
+
406
+    if [[ -z $group || -z $conf || -z $option || -z $value ]]; then
407
+        $xtrace
408
+        return
409
+    fi
410
+
411
+    if ! grep -q "^\[\[${group}|${conf}\]\]" "$file" 2>/dev/null; then
412
+        # Add meta section at the end if it does not exist
413
+        echo -e "\n[[${group}|${conf}]]" | $sudo tee --append "$file" > /dev/null
414
+        # Add section at the end
415
+        if [[ -n "$section" ]]; then
416
+            echo -e "[$section]" | $sudo tee --append "$file" > /dev/null
417
+        fi
418
+        # Add option at the end
419
+        echo -e "$option=$value" | $sudo tee --append "$file" > /dev/null
420
+    elif [[ -z "$section" ]]; then
421
+        if ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
422
+            # Add option
423
+            localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
424
+        else
425
+            # Replace it
426
+            localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
427
+        fi
428
+    elif ! localconf_has_section "$file" "$group" "$conf" "$section"; then
429
+        # Add section and option in specified meta section
430
+        localconf_add_section_and_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
431
+    elif ! localconf_has_option "$file" "$group" "$conf" "$section" "$option"; then
432
+        # Add option
433
+        localconf_add_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
434
+    else
435
+        # Replace it
436
+        localconf_update_option "$sudo" "$file" "$group" "$conf" "$section" "$option" "$value"
437
+    fi
438
+    $xtrace
439
+}
440
+
277 441
 # Restore xtrace
278 442
 $INC_CONF_TRACE
279 443
 
280 444
new file mode 100755
... ...
@@ -0,0 +1,475 @@
0
+#!/usr/bin/env bash
1
+#
2
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
+# not use this file except in compliance with the License. You may obtain
4
+# a copy of the License at
5
+#
6
+#    http://www.apache.org/licenses/LICENSE-2.0
7
+#
8
+# Unless required by applicable law or agreed to in writing, software
9
+# distributed under the License is distributed on an "AS IS" BASIS,
10
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11
+# implied. See the License for the specific language governing
12
+# permissions and limitations under the License.
13
+
14
+# Tests for DevStack INI functions
15
+
16
+TOP=$(cd $(dirname "$0")/.. && pwd)
17
+
18
+# Import config functions
19
+source $TOP/inc/ini-config
20
+
21
+source $TOP/tests/unittest.sh
22
+
23
+echo "Testing INI local.conf functions"
24
+
25
+# test that can determine if file has section in specified meta-section
26
+
27
+function test_localconf_has_section {
28
+    local file_localconf
29
+    local file_conf1
30
+    local file_conf2
31
+    file_localconf=`mktemp`
32
+    file_conf1=`mktemp`
33
+    file_conf2=`mktemp`
34
+
35
+    cat <<- EOF > $file_localconf
36
+[[local|localrc]]
37
+LOCALRC_VAR1=localrc_val1
38
+LOCALRC_VAR2=localrc_val2
39
+LOCALRC_VAR3=localrc_val3
40
+
41
+[[post-config|$file_conf1]]
42
+[conf1_t1]
43
+conf1_t1_opt1=conf1_t1_val1
44
+conf1_t1_opt2=conf1_t1_val2
45
+conf1_t1_opt3=conf1_t1_val3
46
+[conf1_t2]
47
+conf1_t2_opt1=conf1_t2_val1
48
+conf1_t2_opt2=conf1_t2_val2
49
+conf1_t2_opt3=conf1_t2_val3
50
+[conf1_t3]
51
+conf1_t3_opt1=conf1_t3_val1
52
+conf1_t3_opt2=conf1_t3_val2
53
+conf1_t3_opt3=conf1_t3_val3
54
+
55
+[[post-extra|$file_conf2]]
56
+[conf2_t1]
57
+conf2_t1_opt1=conf2_t1_val1
58
+conf2_t1_opt2=conf2_t1_val2
59
+conf2_t1_opt3=conf2_t1_val3
60
+EOF
61
+
62
+    localconf_has_section $file_localconf post-config $file_conf1 conf1_t1
63
+    assert_equal $? 0
64
+    localconf_has_section $file_localconf post-config $file_conf1 conf1_t2
65
+    assert_equal $? 0
66
+    localconf_has_section $file_localconf post-config $file_conf1 conf1_t3
67
+    assert_equal $? 0
68
+    localconf_has_section $file_localconf post-extra $file_conf2 conf2_t1
69
+    assert_equal $? 0
70
+    localconf_has_section $file_localconf post-config $file_conf1 conf1_t4
71
+    assert_equal $? 1
72
+    localconf_has_section $file_localconf post-install $file_conf1 conf1_t1
73
+    assert_equal $? 1
74
+    localconf_has_section $file_localconf local localrc conf1_t2
75
+    assert_equal $? 1
76
+    rm -f $file_localconf $file_conf1 $file_conf2
77
+}
78
+
79
+# test that can determine if file has option in specified meta-section and section
80
+function test_localconf_has_option {
81
+    local file_localconf
82
+    local file_conf1
83
+    local file_conf2
84
+    file_localconf=`mktemp`
85
+    file_conf1=`mktemp`
86
+    file_conf2=`mktemp`
87
+    cat <<- EOF > $file_localconf
88
+[[post-config|$file_conf1]]
89
+[conf1_t1]
90
+conf1_t1_opt1 = conf1_t1_val1
91
+conf1_t1_opt2 = conf1_t1_val2
92
+conf1_t1_opt3 = conf1_t1_val3
93
+[conf1_t2]
94
+conf1_t2_opt1=conf1_t2_val1
95
+conf1_t2_opt2=conf1_t2_val2
96
+conf1_t2_opt3=conf1_t2_val3
97
+[conf1_t3]
98
+conf1_t3_opt1=conf1_t3_val1
99
+conf1_t3_opt2=conf1_t3_val2
100
+conf1_t3_opt3=conf1_t3_val3
101
+
102
+[[local|localrc]]
103
+LOCALRC_VAR1=localrc_val1
104
+LOCALRC_VAR2=localrc_val2
105
+LOCALRC_VAR3=localrc_val3
106
+
107
+[[post-extra|$file_conf2]]
108
+[conf2_t1]
109
+conf2_t1_opt1=conf2_t1_val1
110
+conf2_t1_opt2=conf2_t1_val2
111
+conf2_t1_opt3=conf2_t1_val3
112
+EOF
113
+
114
+    localconf_has_option $file_localconf local localrc "" LOCALRC_VAR1
115
+    assert_equal $? 0
116
+    localconf_has_option $file_localconf local localrc "" LOCALRC_VAR2
117
+    assert_equal $? 0
118
+    localconf_has_option $file_localconf local localrc "" LOCALRC_VAR3
119
+    assert_equal $? 0
120
+    localconf_has_option $file_localconf post-config $file_conf1 conf1_t1 conf1_t1_opt1
121
+    assert_equal $? 0
122
+    localconf_has_option $file_localconf post-config $file_conf1 conf1_t2 conf1_t2_opt2
123
+    assert_equal $? 0
124
+    localconf_has_option $file_localconf post-config $file_conf1 conf1_t3 conf1_t3_opt3
125
+    assert_equal $? 0
126
+    localconf_has_option $file_localconf post-extra $file_conf2 conf2_t1 conf2_t1_opt2
127
+    assert_equal $? 0
128
+    localconf_has_option $file_localconf post-config $file_conf1 conf1_t1_opt4
129
+    assert_equal $? 1
130
+    localconf_has_option $file_localconf post-install $file_conf1 conf1_t1_opt1
131
+    assert_equal $? 1
132
+    localconf_has_option $file_localconf local localrc conf1_t2 conf1_t2_opt1
133
+    assert_equal $? 1
134
+    rm -f $file_localconf $file_conf1 $file_conf2
135
+}
136
+
137
+# test that update option in specified meta-section and section
138
+function test_localconf_update_option {
139
+    local file_localconf
140
+    local file_localconf_expected
141
+    local file_conf1
142
+    local file_conf2
143
+    file_localconf=`mktemp`
144
+    file_localconf_expected=`mktemp`
145
+    file_conf1=`mktemp`
146
+    file_conf2=`mktemp`
147
+    cat <<- EOF > $file_localconf
148
+[[local|localrc]]
149
+LOCALRC_VAR1 = localrc_val1
150
+LOCALRC_VAR2 = localrc_val2
151
+LOCALRC_VAR3 = localrc_val3
152
+
153
+[[post-config|$file_conf1]]
154
+[conf1_t1]
155
+conf1_t1_opt1=conf1_t1_val1
156
+conf1_t1_opt2=conf1_t1_val2
157
+conf1_t1_opt3=conf1_t1_val3
158
+[conf1_t2]
159
+conf1_t2_opt1=conf1_t2_val1
160
+conf1_t2_opt2=conf1_t2_val2
161
+conf1_t2_opt3=conf1_t2_val3
162
+[conf1_t3]
163
+conf1_t3_opt1=conf1_t3_val1
164
+conf1_t3_opt2=conf1_t3_val2
165
+conf1_t3_opt3=conf1_t3_val3
166
+
167
+[[post-extra|$file_conf2]]
168
+[conf2_t1]
169
+conf2_t1_opt1=conf2_t1_val1
170
+conf2_t1_opt2=conf2_t1_val2
171
+conf2_t1_opt3=conf2_t1_val3
172
+EOF
173
+    cat <<- EOF > $file_localconf_expected
174
+[[local|localrc]]
175
+LOCALRC_VAR1 = localrc_val1
176
+LOCALRC_VAR2 = localrc_val2_update
177
+LOCALRC_VAR3 = localrc_val3
178
+
179
+[[post-config|$file_conf1]]
180
+[conf1_t1]
181
+conf1_t1_opt1=conf1_t1_val1_update
182
+conf1_t1_opt2=conf1_t1_val2
183
+conf1_t1_opt3=conf1_t1_val3
184
+[conf1_t2]
185
+conf1_t2_opt1=conf1_t2_val1
186
+conf1_t2_opt2=conf1_t2_val2_update
187
+conf1_t2_opt3=conf1_t2_val3
188
+[conf1_t3]
189
+conf1_t3_opt1=conf1_t3_val1
190
+conf1_t3_opt2=conf1_t3_val2
191
+conf1_t3_opt3=conf1_t3_val3_update
192
+
193
+[[post-extra|$file_conf2]]
194
+[conf2_t1]
195
+conf2_t1_opt1=conf2_t1_val1
196
+conf2_t1_opt2=conf2_t1_val2
197
+conf2_t1_opt3=conf2_t1_val3_update
198
+EOF
199
+
200
+    localconf_update_option "$SUDO" $file_localconf local localrc "" LOCALRC_VAR2 localrc_val2_update
201
+    localconf_update_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t1 conf1_t1_opt1 conf1_t1_val1_update
202
+    localconf_update_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t2 conf1_t2_opt2 conf1_t2_val2_update
203
+    localconf_update_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t3 conf1_t3_opt3 conf1_t3_val3_update
204
+    localconf_update_option "$SUDO" $file_localconf post-extra $file_conf2 conf2_t1 conf2_t1_opt3 conf2_t1_val3_update
205
+    result=`cat $file_localconf`
206
+    result_expected=`cat $file_localconf_expected`
207
+    assert_equal "$result" "$result_expected"
208
+    localconf_update_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t2 conf1_t3_opt1 conf1_t3_val1_update
209
+    localconf_update_option "$SUDO" $file_localconf post-extra $file_conf2 conf2_t1 conf2_t1_opt4 conf2_t1_val4_update
210
+    localconf_update_option "$SUDO" $file_localconf post-install $file_conf2 conf2_t1 conf2_t1_opt1 conf2_t1_val1_update
211
+    localconf_update_option "$SUDO" $file_localconf local localrc "" LOCALRC_VAR4 localrc_val4_update
212
+    result=`cat $file_localconf`
213
+    result_expected=`cat $file_localconf_expected`
214
+    assert_equal "$result" "$result_expected"
215
+    rm -f $file_localconf $file_localconf_expected $file_conf1 $file_conf2
216
+}
217
+
218
+# test that add option in specified meta-section and section
219
+function test_localconf_add_option {
220
+    local file_localconf
221
+    local file_localconf_expected
222
+    local file_conf1
223
+    local file_conf2
224
+    file_localconf=`mktemp`
225
+    file_localconf_expected=`mktemp`
226
+    file_conf1=`mktemp`
227
+    file_conf2=`mktemp`
228
+    cat <<- EOF > $file_localconf
229
+[[post-config|$file_conf1]]
230
+[conf1_t1]
231
+conf1_t1_opt1=conf1_t1_val1
232
+conf1_t1_opt2=conf1_t1_val2
233
+conf1_t1_opt3=conf1_t1_val3
234
+[conf1_t2]
235
+conf1_t2_opt1=conf1_t2_val1
236
+conf1_t2_opt2=conf1_t2_val2
237
+conf1_t2_opt3=conf1_t2_val3
238
+[conf1_t3]
239
+conf1_t3_opt1=conf1_t3_val1
240
+conf1_t3_opt2=conf1_t3_val2
241
+conf1_t3_opt3=conf1_t3_val3
242
+
243
+[[local|localrc]]
244
+LOCALRC_VAR1=localrc_val1
245
+LOCALRC_VAR2=localrc_val2
246
+LOCALRC_VAR3=localrc_val3
247
+
248
+[[post-extra|$file_conf2]]
249
+[conf2_t1]
250
+conf2_t1_opt1 = conf2_t1_val1
251
+conf2_t1_opt2 = conf2_t1_val2
252
+conf2_t1_opt3 = conf2_t1_val3
253
+EOF
254
+    cat <<- EOF > $file_localconf_expected
255
+[[post-config|$file_conf1]]
256
+[conf1_t1]
257
+conf1_t1_opt4 = conf1_t1_val4
258
+conf1_t1_opt1=conf1_t1_val1
259
+conf1_t1_opt2=conf1_t1_val2
260
+conf1_t1_opt3=conf1_t1_val3
261
+[conf1_t2]
262
+conf1_t2_opt4 = conf1_t2_val4
263
+conf1_t2_opt1=conf1_t2_val1
264
+conf1_t2_opt2=conf1_t2_val2
265
+conf1_t2_opt3=conf1_t2_val3
266
+[conf1_t3]
267
+conf1_t3_opt4 = conf1_t3_val4
268
+conf1_t3_opt1=conf1_t3_val1
269
+conf1_t3_opt2=conf1_t3_val2
270
+conf1_t3_opt3=conf1_t3_val3
271
+
272
+[[local|localrc]]
273
+LOCALRC_VAR4 = localrc_val4
274
+LOCALRC_VAR1=localrc_val1
275
+LOCALRC_VAR2=localrc_val2
276
+LOCALRC_VAR3=localrc_val3
277
+
278
+[[post-extra|$file_conf2]]
279
+[conf2_t1]
280
+conf2_t1_opt4 = conf2_t1_val4
281
+conf2_t1_opt1 = conf2_t1_val1
282
+conf2_t1_opt2 = conf2_t1_val2
283
+conf2_t1_opt3 = conf2_t1_val3
284
+EOF
285
+
286
+    localconf_add_option "$SUDO" $file_localconf local localrc "" LOCALRC_VAR4 localrc_val4
287
+    localconf_add_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t1 conf1_t1_opt4 conf1_t1_val4
288
+    localconf_add_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t2 conf1_t2_opt4 conf1_t2_val4
289
+    localconf_add_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t3 conf1_t3_opt4 conf1_t3_val4
290
+    localconf_add_option "$SUDO" $file_localconf post-extra $file_conf2 conf2_t1 conf2_t1_opt4 conf2_t1_val4
291
+    result=`cat $file_localconf`
292
+    result_expected=`cat $file_localconf_expected`
293
+    assert_equal "$result" "$result_expected"
294
+    localconf_add_option "$SUDO" $file_localconf local localrc.conf "" LOCALRC_VAR4 localrc_val4_update
295
+    localconf_add_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t4 conf1_t4_opt1 conf1_t4_val1
296
+    localconf_add_option "$SUDO" $file_localconf post-extra $file_conf2 conf2_t2 conf2_t2_opt4 conf2_t2_val4
297
+    localconf_add_option "$SUDO" $file_localconf post-install $file_conf2 conf2_t1 conf2_t1_opt4 conf2_t2_val4
298
+    result=`cat $file_localconf`
299
+    result_expected=`cat $file_localconf_expected`
300
+    assert_equal "$result" "$result_expected"
301
+    rm -f $file_localconf $file_localconf_expected $file_conf1 $file_conf2
302
+}
303
+
304
+# test that add section and option in specified meta-section
305
+function test_localconf_add_section_and_option {
306
+    local file_localconf
307
+    local file_localconf_expected
308
+    local file_conf1
309
+    local file_conf2
310
+    file_localconf=`mktemp`
311
+    file_localconf_expected=`mktemp`
312
+    file_conf1=`mktemp`
313
+    file_conf2=`mktemp`
314
+    cat <<- EOF > $file_localconf
315
+[[post-config|$file_conf1]]
316
+[conf1_t1]
317
+conf1_t1_opt1=conf1_t1_val1
318
+conf1_t1_opt2=conf1_t1_val2
319
+conf1_t1_opt3=conf1_t1_val3
320
+[conf1_t2]
321
+conf1_t2_opt1=conf1_t2_val1
322
+conf1_t2_opt2=conf1_t2_val2
323
+conf1_t2_opt3=conf1_t2_val3
324
+[conf1_t3]
325
+conf1_t3_opt1=conf1_t3_val1
326
+conf1_t3_opt2=conf1_t3_val2
327
+conf1_t3_opt3=conf1_t3_val3
328
+
329
+[[local|localrc]]
330
+LOCALRC_VAR1=localrc_val1
331
+LOCALRC_VAR2=localrc_val2
332
+LOCALRC_VAR3=localrc_val3
333
+
334
+[[post-extra|$file_conf2]]
335
+[conf2_t1]
336
+conf2_t1_opt1=conf2_t1_val1
337
+conf2_t1_opt2=conf2_t1_val2
338
+conf2_t1_opt3=conf2_t1_val3
339
+EOF
340
+    cat <<- EOF > $file_localconf_expected
341
+[[post-config|$file_conf1]]
342
+[conf1_t4]
343
+conf1_t4_opt1 = conf1_t4_val1
344
+[conf1_t1]
345
+conf1_t1_opt1=conf1_t1_val1
346
+conf1_t1_opt2=conf1_t1_val2
347
+conf1_t1_opt3=conf1_t1_val3
348
+[conf1_t2]
349
+conf1_t2_opt1=conf1_t2_val1
350
+conf1_t2_opt2=conf1_t2_val2
351
+conf1_t2_opt3=conf1_t2_val3
352
+[conf1_t3]
353
+conf1_t3_opt1=conf1_t3_val1
354
+conf1_t3_opt2=conf1_t3_val2
355
+conf1_t3_opt3=conf1_t3_val3
356
+
357
+[[local|localrc]]
358
+LOCALRC_VAR1=localrc_val1
359
+LOCALRC_VAR2=localrc_val2
360
+LOCALRC_VAR3=localrc_val3
361
+
362
+[[post-extra|$file_conf2]]
363
+[conf2_t2]
364
+conf2_t2_opt1 = conf2_t2_val1
365
+[conf2_t1]
366
+conf2_t1_opt1=conf2_t1_val1
367
+conf2_t1_opt2=conf2_t1_val2
368
+conf2_t1_opt3=conf2_t1_val3
369
+EOF
370
+
371
+    localconf_add_section_and_option "$SUDO" $file_localconf post-config $file_conf1 conf1_t4 conf1_t4_opt1 conf1_t4_val1
372
+    localconf_add_section_and_option "$SUDO" $file_localconf post-extra $file_conf2 conf2_t2 conf2_t2_opt1 conf2_t2_val1
373
+    result=`cat $file_localconf`
374
+    result_expected=`cat $file_localconf_expected`
375
+    assert_equal "$result" "$result_expected"
376
+    localconf_add_section_and_option "$SUDO" $file_localconf post-install $file_conf2 conf2_t2 conf2_t2_opt1 conf2_t2_val1
377
+    result=`cat $file_localconf`
378
+    result_expected=`cat $file_localconf_expected`
379
+    assert_equal "$result" "$result_expected"
380
+    rm -f $file_localconf $file_localconf_expected $file_conf1 $file_conf2
381
+}
382
+
383
+# test that add section and option in specified meta-section
384
+function test_localconf_set {
385
+    local file_localconf
386
+    local file_localconf_expected
387
+    local file_conf1
388
+    local file_conf2
389
+    file_localconf=`mktemp`
390
+    file_localconf_expected=`mktemp`
391
+    file_conf1=`mktemp`
392
+    file_conf2=`mktemp`
393
+    cat <<- EOF > $file_localconf
394
+[[local|localrc]]
395
+LOCALRC_VAR1=localrc_val1
396
+LOCALRC_VAR2=localrc_val2
397
+LOCALRC_VAR3=localrc_val3
398
+
399
+[[post-config|$file_conf1]]
400
+[conf1_t1]
401
+conf1_t1_opt1=conf1_t1_val1
402
+conf1_t1_opt2=conf1_t1_val2
403
+conf1_t1_opt3=conf1_t1_val3
404
+[conf1_t2]
405
+conf1_t2_opt1=conf1_t2_val1
406
+conf1_t2_opt2=conf1_t2_val2
407
+conf1_t2_opt3=conf1_t2_val3
408
+[conf1_t3]
409
+conf1_t3_opt1=conf1_t3_val1
410
+conf1_t3_opt2=conf1_t3_val2
411
+conf1_t3_opt3=conf1_t3_val3
412
+
413
+[[post-extra|$file_conf2]]
414
+[conf2_t1]
415
+conf2_t1_opt1=conf2_t1_val1
416
+conf2_t1_opt2=conf2_t1_val2
417
+conf2_t1_opt3=conf2_t1_val3
418
+EOF
419
+    cat <<- EOF > $file_localconf_expected
420
+[[local|localrc]]
421
+LOCALRC_VAR1=localrc_val1
422
+LOCALRC_VAR2=localrc_val2_update
423
+LOCALRC_VAR3=localrc_val3
424
+
425
+[[post-config|$file_conf1]]
426
+[conf1_t4]
427
+conf1_t4_opt1 = conf1_t4_val1
428
+[conf1_t1]
429
+conf1_t1_opt1=conf1_t1_val1
430
+conf1_t1_opt2=conf1_t1_val2
431
+conf1_t1_opt3=conf1_t1_val3
432
+[conf1_t2]
433
+conf1_t2_opt1=conf1_t2_val1
434
+conf1_t2_opt2=conf1_t2_val2
435
+conf1_t2_opt3=conf1_t2_val3
436
+[conf1_t3]
437
+conf1_t3_opt1=conf1_t3_val1
438
+conf1_t3_opt2=conf1_t3_val2
439
+conf1_t3_opt3=conf1_t3_val3
440
+
441
+[[post-extra|$file_conf2]]
442
+[conf2_t1]
443
+conf2_t1_opt4 = conf2_t1_val4
444
+conf2_t1_opt1=conf2_t1_val1
445
+conf2_t1_opt2=conf2_t1_val2
446
+conf2_t1_opt3=conf2_t1_val3
447
+
448
+[[post-install|/etc/neutron/plugin/ml2/ml2_conf.ini]]
449
+[ml2]
450
+ml2_opt1 = ml2_val1
451
+EOF
452
+
453
+    if [[ -n "$SUDO" ]]; then
454
+        SUDO_ARG="-sudo"
455
+    else
456
+        SUDO_ARG=""
457
+    fi
458
+    localconf_set $SUDO_ARG $file_localconf post-install /etc/neutron/plugin/ml2/ml2_conf.ini ml2 ml2_opt1 ml2_val1
459
+    localconf_set $SUDO_ARG $file_localconf local localrc "" LOCALRC_VAR2 localrc_val2_update
460
+    localconf_set $SUDO_ARG $file_localconf post-config $file_conf1 conf1_t4 conf1_t4_opt1 conf1_t4_val1
461
+    localconf_set $SUDO_ARG $file_localconf post-extra $file_conf2 conf2_t1 conf2_t1_opt4 conf2_t1_val4
462
+    result=`cat $file_localconf`
463
+    result_expected=`cat $file_localconf_expected`
464
+    assert_equal "$result" "$result_expected"
465
+    rm -f $file_localconf $file_localconf_expected $file_conf1 $file_conf2
466
+}
467
+
468
+
469
+test_localconf_has_section
470
+test_localconf_has_option
471
+test_localconf_update_option
472
+test_localconf_add_option
473
+test_localconf_add_section_and_option
474
+test_localconf_set