xref: /freebsd/contrib/kyua/integration/cmd_config_test.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1# Copyright 2011 The Kyua Authors.
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"
45parallelism = 256
46platform = "my-platform"
47unprivileged_user = "$(id -u -n)"
48test_suites.suite1.the_variable = "value1"
49test_suites.suite2.the_variable = "value2"
50EOF
51
52    cat >expout <<EOF
53architecture = my-architecture
54parallelism = 256
55platform = my-platform
56test_suites.suite1.the_variable = value1
57test_suites.suite2.the_variable = value2
58unprivileged_user = $(id -u -n)
59EOF
60
61    atf_check -s exit:0 -o file:expout -e empty kyua config
62}
63
64
65utils_test_case one__ok
66one__ok_body() {
67    mkdir "${HOME}/.kyua"
68    cat >"${HOME}/.kyua/kyua.conf" <<EOF
69syntax(2)
70test_suites.first.one = 1
71test_suites.first.two = 2
72EOF
73
74    cat >expout <<EOF
75test_suites.first.two = 2
76EOF
77
78    atf_check -s exit:0 -o file:expout -e empty kyua config \
79        test_suites.first.two
80}
81
82
83utils_test_case one__fail
84one__fail_body() {
85    mkdir "${HOME}/.kyua"
86    cat >"${HOME}/.kyua/kyua.conf" <<EOF
87syntax(2)
88test_suites.first.one = 1
89test_suites.first.three = 3
90EOF
91
92    cat >experr <<EOF
93kyua: W: 'test_suites.first.two' is not defined.
94EOF
95
96    atf_check -s exit:1 -o empty -e file:experr kyua config \
97        test_suites.first.two
98}
99
100
101utils_test_case many__ok
102many__ok_body() {
103    mkdir "${HOME}/.kyua"
104    cat >"${HOME}/.kyua/kyua.conf" <<EOF
105syntax(2)
106architecture = "overriden"
107unknown_setting = "foo"
108test_suites.first.one = 1
109test_suites.first.two = 2
110EOF
111
112    cat >expout <<EOF
113architecture = overriden
114test_suites.first.two = 2
115test_suites.first.one = 1
116EOF
117
118    atf_check -s exit:0 -o file:expout -e empty kyua config \
119        architecture \
120        test_suites.first.two \
121        test_suites.first.one  # Inverse order on purpose.
122    atf_check -s exit:0 -o match:architecture -o not-match:unknown_setting \
123        -e empty kyua config
124}
125
126
127utils_test_case many__fail
128many__fail_body() {
129    mkdir "${HOME}/.kyua"
130    cat >"${HOME}/.kyua/kyua.conf" <<EOF
131syntax(2)
132test_suites.first.one = 1
133test_suites.first.three = 3
134EOF
135
136    cat >expout <<EOF
137test_suites.first.one = 1
138test_suites.first.three = 3
139EOF
140
141    cat >experr <<EOF
142kyua: W: 'test_suites.first.two' is not defined.
143kyua: W: 'test_suites.first.fourth' is not defined.
144EOF
145
146    atf_check -s exit:1 -o file:expout -e file:experr kyua config \
147        test_suites.first.one test_suites.first.two \
148        test_suites.first.three test_suites.first.fourth
149}
150
151
152utils_test_case config_flag__default_system
153config_flag__default_system_body() {
154    cat >kyua.conf <<EOF
155syntax(2)
156test_suites.foo.var = "baz"
157EOF
158
159    atf_check -s exit:1 -o empty \
160        -e match:"kyua: W: 'test_suites.foo.var'.*not defined" \
161        kyua config test_suites.foo.var
162    export KYUA_CONFDIR="$(pwd)"
163    atf_check -s exit:0 -o match:"foo.var = baz" -e empty \
164        kyua config test_suites.foo.var
165}
166
167
168utils_test_case config_flag__default_home
169config_flag__default_home_body() {
170    cat >kyua.conf <<EOF
171syntax(2)
172test_suites.foo.var = "bar"
173EOF
174    export KYUA_CONFDIR="$(pwd)"
175    atf_check -s exit:0 -o match:"test_suites.foo.var = bar" -e empty \
176        kyua config test_suites.foo.var
177
178    # The previously-created "system-wide" file has to be ignored.
179    mkdir .kyua
180    cat >.kyua/kyua.conf <<EOF
181syntax(2)
182test_suites.foo.var = "baz"
183EOF
184    atf_check -s exit:0 -o match:"test_suites.foo.var = baz" -e empty \
185        kyua config test_suites.foo.var
186}
187
188
189utils_test_case config_flag__explicit__ok
190config_flag__explicit__ok_body() {
191    cat >kyua.conf <<EOF
192syntax(2)
193test_suites.foo.var = "baz"
194EOF
195
196    atf_check -s exit:1 -o empty \
197        -e match:"kyua: W: 'test_suites.foo.var'.*not defined" \
198        kyua config test_suites.foo.var
199    atf_check -s exit:0 -o match:"test_suites.foo.var = baz" -e empty \
200        kyua -c kyua.conf config test_suites.foo.var
201    atf_check -s exit:0 -o match:"test_suites.foo.var = baz" -e empty \
202        kyua --config=kyua.conf config test_suites.foo.var
203}
204
205
206utils_test_case config_flag__explicit__disable
207config_flag__explicit__disable_body() {
208    cat >kyua.conf <<EOF
209syntax(2)
210test_suites.foo.var = "baz"
211EOF
212    mkdir .kyua
213    cp kyua.conf .kyua/kyua.conf
214    export KYUA_CONFDIR="$(pwd)"
215
216    atf_check -s exit:0 -o match:"test_suites.foo.var = baz" -e empty \
217        kyua config test_suites.foo.var
218    atf_check -s exit:1 -o empty \
219        -e match:"kyua: W: 'test_suites.foo.var'.*not defined" \
220        kyua --config=none config test_suites.foo.var
221}
222
223
224utils_test_case config_flag__explicit__missing_file
225config_flag__explicit__missing_file_body() {
226    cat >experr <<EOF
227kyua: E: Load of 'foo' failed: File 'foo' not found.
228EOF
229    atf_check -s exit:2 -o empty -e file:experr kyua --config=foo config
230}
231
232
233utils_test_case config_flag__explicit__bad_file
234config_flag__explicit__bad_file_body() {
235    touch custom
236    atf_check -s exit:2 -o empty -e match:"No syntax defined" \
237        kyua --config=custom config
238}
239
240
241utils_test_case variable_flag__no_config
242variable_flag__no_config_body() {
243    atf_check -s exit:0 \
244        -o match:'test_suites.suite1.the_variable = value1' \
245        -o match:'test_suites.suite2.the_variable = value2' \
246        -e empty \
247        kyua \
248        -v "test_suites.suite1.the_variable=value1" \
249        -v "test_suites.suite2.the_variable=value2" \
250        config
251
252    atf_check -s exit:0 \
253        -o match:'test_suites.suite1.the_variable = value1' \
254        -o match:'test_suites.suite2.the_variable = value2' \
255        -e empty \
256        kyua \
257        --variable="test_suites.suite1.the_variable=value1" \
258        --variable="test_suites.suite2.the_variable=value2" \
259        config
260}
261
262
263utils_test_case variable_flag__override_default_config
264variable_flag__override_default_config_body() {
265    mkdir "${HOME}/.kyua"
266    cat >"${HOME}/.kyua/kyua.conf" <<EOF
267syntax(2)
268test_suites.suite1.the_variable = "value1"
269test_suites.suite2.the_variable = "should not be used"
270EOF
271
272    atf_check -s exit:0 \
273        -o match:'test_suites.suite1.the_variable = value1' \
274        -o match:'test_suites.suite2.the_variable = overriden' \
275        -o match:'test_suites.suite3.the_variable = new' \
276        -e empty kyua \
277        -v "test_suites.suite2.the_variable=overriden" \
278        -v "test_suites.suite3.the_variable=new" \
279        config
280
281    atf_check -s exit:0 \
282        -o match:'test_suites.suite1.the_variable = value1' \
283        -o match:'test_suites.suite2.the_variable = overriden' \
284        -o match:'test_suites.suite3.the_variable = new' \
285        -e empty kyua \
286        --variable="test_suites.suite2.the_variable=overriden" \
287        --variable="test_suites.suite3.the_variable=new" \
288        config
289}
290
291
292utils_test_case variable_flag__override_custom_config
293variable_flag__override_custom_config_body() {
294    cat >config <<EOF
295syntax(2)
296test_suites.suite1.the_variable = "value1"
297test_suites.suite2.the_variable = "should not be used"
298EOF
299
300    atf_check -s exit:0 \
301        -o match:'test_suites.suite2.the_variable = overriden' \
302        -e empty kyua -c config \
303        -v "test_suites.suite2.the_variable=overriden" config
304
305    atf_check -s exit:0 \
306        -o match:'test_suites.suite2.the_variable = overriden' \
307        -e empty kyua -c config \
308        --variable="test_suites.suite2.the_variable=overriden" config
309}
310
311
312utils_test_case variable_flag__invalid_key
313variable_flag__invalid_key_body() {
314    # CHECK_STYLE_DISABLE
315    cat >experr <<EOF
316Usage error: Invalid argument '' for option --variable: Argument does not have the form 'K=V'.
317Type 'kyua help' for usage information.
318EOF
319    # CHECK_STYLE_ENABLE
320    atf_check -s exit:3 -o empty -e file:experr kyua \
321        -v "test_suites.a.b=c" -v "" config
322}
323
324
325utils_test_case variable_flag__invalid_value
326variable_flag__invalid_value_body() {
327    cat >experr <<EOF
328kyua: E: Invalid value for property 'parallelism': Must be a positive integer.
329EOF
330    atf_check -s exit:2 -o empty -e file:experr kyua \
331        -v "parallelism=0" config
332}
333
334
335atf_init_test_cases() {
336    atf_add_test_case defaults
337    atf_add_test_case all
338    atf_add_test_case one__ok
339    atf_add_test_case one__fail
340    atf_add_test_case many__ok
341    atf_add_test_case many__fail
342
343    atf_add_test_case config_flag__default_system
344    atf_add_test_case config_flag__default_home
345    atf_add_test_case config_flag__explicit__ok
346    atf_add_test_case config_flag__explicit__disable
347    atf_add_test_case config_flag__explicit__missing_file
348    atf_add_test_case config_flag__explicit__bad_file
349
350    atf_add_test_case variable_flag__no_config
351    atf_add_test_case variable_flag__override_default_config
352    atf_add_test_case variable_flag__override_custom_config
353    atf_add_test_case variable_flag__invalid_key
354    atf_add_test_case variable_flag__invalid_value
355}
356