xref: /netbsd-src/external/bsd/kyua-cli/dist/integration/cmd_config_test.sh (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1# Copyright 2011 Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9#   notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright
11#   notice, this list of conditions and the following disclaimer in the
12#   documentation and/or other materials provided with the distribution.
13# * Neither the name of Google Inc. nor the names of its contributors
14#   may be used to endorse or promote products derived from this software
15#   without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30utils_test_case defaults
31defaults_body() {
32    atf_check -s exit:0 \
33        -o match:'^architecture = ' \
34        -o match:'^platform = ' \
35        kyua config
36}
37
38
39utils_test_case all
40all_body() {
41    mkdir "${HOME}/.kyua"
42    cat >"${HOME}/.kyua/kyua.conf" <<EOF
43syntax(2)
44architecture = "my-architecture"
45platform = "my-platform"
46unprivileged_user = "$(id -u -n)"
47test_suites.suite1["X-the-variable"] = "value1"
48test_suites.suite2["X-the-variable"] = "value2"
49EOF
50
51    cat >expout <<EOF
52architecture = my-architecture
53platform = my-platform
54test_suites.suite1.X-the-variable = value1
55test_suites.suite2.X-the-variable = value2
56unprivileged_user = $(id -u -n)
57EOF
58
59    atf_check -s exit:0 -o file:expout -e empty kyua config
60}
61
62
63utils_test_case one__ok
64one__ok_body() {
65    mkdir "${HOME}/.kyua"
66    cat >"${HOME}/.kyua/kyua.conf" <<EOF
67syntax(2)
68test_suites.first["X-one"] = 1
69test_suites.first["X-two"] = 2
70EOF
71
72    cat >expout <<EOF
73test_suites.first.X-two = 2
74EOF
75
76    atf_check -s exit:0 -o file:expout -e empty kyua config \
77        test_suites.first.X-two
78}
79
80
81utils_test_case one__fail
82one__fail_body() {
83    mkdir "${HOME}/.kyua"
84    cat >"${HOME}/.kyua/kyua.conf" <<EOF
85syntax(2)
86test_suites.first["X-one"] = 1
87test_suites.first["X-three"] = 3
88EOF
89
90    cat >experr <<EOF
91kyua: W: 'test_suites.first.X-two' is not defined.
92EOF
93
94    atf_check -s exit:1 -o empty -e file:experr kyua config \
95        test_suites.first.X-two
96}
97
98
99utils_test_case many__ok
100many__ok_body() {
101    mkdir "${HOME}/.kyua"
102    cat >"${HOME}/.kyua/kyua.conf" <<EOF
103syntax(2)
104test_suites.first["X-one"] = 1
105test_suites.first["X-two"] = 2
106EOF
107
108    cat >expout <<EOF
109test_suites.first.X-two = 2
110test_suites.first.X-one = 1
111EOF
112
113    atf_check -s exit:0 -o file:expout -e empty kyua config \
114        test_suites.first.X-two \
115        test_suites.first.X-one  # Inverse order on purpose.
116}
117
118
119utils_test_case many__fail
120many__fail_body() {
121    mkdir "${HOME}/.kyua"
122    cat >"${HOME}/.kyua/kyua.conf" <<EOF
123syntax(2)
124test_suites.first["X-one"] = 1
125test_suites.first["X-three"] = 3
126EOF
127
128    cat >expout <<EOF
129test_suites.first.X-one = 1
130test_suites.first.X-three = 3
131EOF
132
133    cat >experr <<EOF
134kyua: W: 'test_suites.first.X-two' is not defined.
135kyua: W: 'test_suites.first.X-fourth' is not defined.
136EOF
137
138    atf_check -s exit:1 -o file:expout -e file:experr kyua config \
139        test_suites.first.X-one test_suites.first.X-two \
140        test_suites.first.X-three test_suites.first.X-fourth
141}
142
143
144utils_test_case config_flag__default_system
145config_flag__default_system_body() {
146    cat >kyua.conf <<EOF
147syntax(2)
148test_suites.foo["X-var"] = "baz"
149EOF
150
151    atf_check -s exit:1 -o empty \
152        -e match:"kyua: W: 'test_suites.foo.X-var'.*not defined" \
153        kyua config test_suites.foo.X-var
154    export KYUA_CONFDIR="$(pwd)"
155    atf_check -s exit:0 -o match:"foo.X-var = baz" -e empty \
156        kyua config test_suites.foo.X-var
157}
158
159
160utils_test_case config_flag__default_home
161config_flag__default_home_body() {
162    cat >kyua.conf <<EOF
163syntax(2)
164test_suites.foo["X-var"] = "bar"
165EOF
166    export KYUA_CONFDIR="$(pwd)"
167    atf_check -s exit:0 -o match:"test_suites.foo.X-var = bar" -e empty \
168        kyua config test_suites.foo.X-var
169
170    # The previously-created "system-wide" file has to be ignored.
171    mkdir .kyua
172    cat >.kyua/kyua.conf <<EOF
173syntax(2)
174test_suites.foo["X-var"] = "baz"
175EOF
176    atf_check -s exit:0 -o match:"test_suites.foo.X-var = baz" -e empty \
177        kyua config test_suites.foo.X-var
178}
179
180
181utils_test_case config_flag__explicit__ok
182config_flag__explicit__ok_body() {
183    cat >kyua.conf <<EOF
184syntax(2)
185test_suites.foo["X-var"] = "baz"
186EOF
187
188    atf_check -s exit:1 -o empty \
189        -e match:"kyua: W: 'test_suites.foo.X-var'.*not defined" \
190        kyua config test_suites.foo.X-var
191    atf_check -s exit:0 -o match:"test_suites.foo.X-var = baz" -e empty \
192        kyua -c kyua.conf config test_suites.foo.X-var
193    atf_check -s exit:0 -o match:"test_suites.foo.X-var = baz" -e empty \
194        kyua --config=kyua.conf config test_suites.foo.X-var
195}
196
197
198utils_test_case config_flag__explicit__disable
199config_flag__explicit__disable_body() {
200    cat >kyua.conf <<EOF
201syntax(2)
202test_suites.foo["X-var"] = "baz"
203EOF
204    mkdir .kyua
205    cp kyua.conf .kyua/kyua.conf
206    export KYUA_CONFDIR="$(pwd)"
207
208    atf_check -s exit:0 -o match:"test_suites.foo.X-var = baz" -e empty \
209        kyua config test_suites.foo.X-var
210    atf_check -s exit:1 -o empty \
211        -e match:"kyua: W: 'test_suites.foo.X-var'.*not defined" \
212        kyua --config=none config test_suites.foo.X-var
213}
214
215
216utils_test_case config_flag__explicit__missing_file
217config_flag__explicit__missing_file_body() {
218    cat >experr <<EOF
219kyua: E: Load of 'foo' failed: File 'foo' not found.
220EOF
221    atf_check -s exit:2 -o empty -e file:experr kyua --config=foo config
222}
223
224
225utils_test_case config_flag__explicit__bad_file
226config_flag__explicit__bad_file_body() {
227    touch custom
228    atf_check -s exit:2 -o empty -e match:"No syntax defined" \
229        kyua --config=custom config
230}
231
232
233utils_test_case variable_flag__no_config
234variable_flag__no_config_body() {
235    atf_check -s exit:0 \
236        -o match:'test_suites.suite1.X-the-variable = value1' \
237        -o match:'test_suites.suite2.X-the-variable = value2' \
238        -e empty \
239        kyua \
240        -v "test_suites.suite1.X-the-variable=value1" \
241        -v "test_suites.suite2.X-the-variable=value2" \
242        config
243
244    atf_check -s exit:0 \
245        -o match:'test_suites.suite1.X-the-variable = value1' \
246        -o match:'test_suites.suite2.X-the-variable = value2' \
247        -e empty \
248        kyua \
249        --variable="test_suites.suite1.X-the-variable=value1" \
250        --variable="test_suites.suite2.X-the-variable=value2" \
251        config
252}
253
254
255utils_test_case variable_flag__override_default_config
256variable_flag__override_default_config_body() {
257    mkdir "${HOME}/.kyua"
258    cat >"${HOME}/.kyua/kyua.conf" <<EOF
259syntax(2)
260test_suites.suite1["X-the-variable"] = "value1"
261test_suites.suite2["X-the-variable"] = "should not be used"
262EOF
263
264    atf_check -s exit:0 \
265        -o match:'test_suites.suite1.X-the-variable = value1' \
266        -o match:'test_suites.suite2.X-the-variable = overriden' \
267        -o match:'test_suites.suite3.X-the-variable = new' \
268        -e empty kyua \
269        -v "test_suites.suite2.X-the-variable=overriden" \
270        -v "test_suites.suite3.X-the-variable=new" \
271        config
272
273    atf_check -s exit:0 \
274        -o match:'test_suites.suite1.X-the-variable = value1' \
275        -o match:'test_suites.suite2.X-the-variable = overriden' \
276        -o match:'test_suites.suite3.X-the-variable = new' \
277        -e empty kyua \
278        --variable="test_suites.suite2.X-the-variable=overriden" \
279        --variable="test_suites.suite3.X-the-variable=new" \
280        config
281}
282
283
284utils_test_case variable_flag__override_custom_config
285variable_flag__override_custom_config_body() {
286    cat >config <<EOF
287syntax(2)
288test_suites.suite1["X-the-variable"] = "value1"
289test_suites.suite2["X-the-variable"] = "should not be used"
290EOF
291
292    atf_check -s exit:0 \
293        -o match:'test_suites.suite2.X-the-variable = overriden' \
294        -e empty kyua -c config \
295        -v "test_suites.suite2.X-the-variable=overriden" config
296
297    atf_check -s exit:0 \
298        -o match:'test_suites.suite2.X-the-variable = overriden' \
299        -e empty kyua -c config \
300        --variable="test_suites.suite2.X-the-variable=overriden" config
301}
302
303
304utils_test_case variable_flag__invalid
305variable_flag__invalid_body() {
306    cat >experr <<EOF
307Usage error: Invalid argument '' for option --variable: Argument does not have the form 'K=V'.
308Type 'kyua help' for usage information.
309EOF
310    atf_check -s exit:3 -o empty -e file:experr kyua \
311        -v "test_suites.a.b=c" -v "" config
312
313    cat >experr <<EOF
314kyua: E: Unknown configuration property 'foo'.
315EOF
316    atf_check -s exit:2 -o empty -e file:experr kyua \
317        -v "test_suites.a.b=c" -v "foo=bar" config
318}
319
320
321atf_init_test_cases() {
322    atf_add_test_case defaults
323    atf_add_test_case all
324    atf_add_test_case one__ok
325    atf_add_test_case one__fail
326    atf_add_test_case many__ok
327    atf_add_test_case many__fail
328
329    atf_add_test_case config_flag__default_system
330    atf_add_test_case config_flag__default_home
331    atf_add_test_case config_flag__explicit__ok
332    atf_add_test_case config_flag__explicit__disable
333    atf_add_test_case config_flag__explicit__missing_file
334    atf_add_test_case config_flag__explicit__bad_file
335
336    atf_add_test_case variable_flag__no_config
337    atf_add_test_case variable_flag__override_default_config
338    atf_add_test_case variable_flag__override_custom_config
339    atf_add_test_case variable_flag__invalid
340}
341