xref: /freebsd/contrib/kyua/integration/cmd_debug_test.sh (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 no_args
31no_args_body() {
32    cat >Kyuafile <<EOF
33syntax(2)
34test_suite("integration")
35atf_test_program{name="simple_all_pass"}
36EOF
37    utils_cp_helper simple_all_pass .
38
39    cat >experr <<EOF
40Usage error for command debug: Not enough arguments.
41Type 'kyua help debug' for usage information.
42EOF
43    atf_check -s exit:3 -o empty -e file:experr kyua debug
44}
45
46
47utils_test_case many_args
48many_args_body() {
49    cat >Kyuafile <<EOF
50syntax(2)
51test_suite("integration")
52atf_test_program{name="first"}
53atf_test_program{name="second"}
54EOF
55    utils_cp_helper simple_all_pass first
56    utils_cp_helper simple_all_pass second
57
58    cat >experr <<EOF
59Usage error for command debug: Too many arguments.
60Type 'kyua help debug' for usage information.
61EOF
62    atf_check -s exit:3 -o empty -e file:experr kyua debug first:pass \
63        second:pass
64}
65
66
67utils_test_case one_arg__ok_pass
68one_arg__ok_pass_body() {
69    cat >Kyuafile <<EOF
70syntax(2)
71test_suite("integration")
72atf_test_program{name="first"}
73atf_test_program{name="second"}
74EOF
75    utils_cp_helper expect_all_pass first
76    utils_cp_helper simple_all_pass second
77
78    cat >expout <<EOF
79This is the stdout of pass
80second:pass  ->  passed
81EOF
82cat >experr <<EOF
83This is the stderr of pass
84EOF
85    atf_check -s exit:0 -o file:expout -e file:experr kyua debug second:pass
86}
87
88
89utils_test_case one_arg__ok_fail
90one_arg__ok_fail_body() {
91    cat >Kyuafile <<EOF
92syntax(2)
93test_suite("integration")
94atf_test_program{name="first"}
95EOF
96    utils_cp_helper simple_some_fail first
97
98    cat >expout <<EOF
99This is the stdout of fail
100first:fail  ->  failed: This fails on purpose
101EOF
102    cat >experr <<EOF
103This is the stderr of fail
104EOF
105    atf_check -s exit:1 -o file:expout -e file:experr kyua debug first:fail
106}
107
108
109utils_test_case one_arg__no_match
110one_arg__no_match_body() {
111    cat >Kyuafile <<EOF
112syntax(2)
113test_suite("integration")
114atf_test_program{name="first"}
115atf_test_program{name="second"}
116EOF
117    utils_cp_helper expect_all_pass first
118    utils_cp_helper simple_all_pass second
119
120    cat >experr <<EOF
121kyua: E: Unknown test case 'second:die'.
122EOF
123    atf_check -s exit:2 -o empty -e file:experr kyua debug second:die
124}
125
126
127utils_test_case one_arg__no_test_case
128one_arg__no_test_case_body() {
129    # CHECK_STYLE_DISABLE
130    cat >experr <<EOF
131Usage error for command debug: 'foo' is not a test case identifier (missing ':'?).
132Type 'kyua help debug' for usage information.
133EOF
134    # CHECK_STYLE_ENABLE
135    atf_check -s exit:3 -o empty -e file:experr kyua debug foo
136}
137
138
139utils_test_case one_arg__bad_filter
140one_arg__bad_filter_body() {
141    cat >experr <<EOF
142kyua: E: Test case component in 'foo:' is empty.
143EOF
144    atf_check -s exit:2 -o empty -e file:experr kyua debug foo:
145}
146
147
148utils_test_case body_and_cleanup
149body_and_cleanup_body() {
150    cat >Kyuafile <<EOF
151syntax(2)
152test_suite("integration")
153atf_test_program{name="single"}
154EOF
155    utils_cp_helper metadata single
156
157    cat >expout <<EOF
158single:with_cleanup  ->  passed
159EOF
160    atf_check -s exit:0 -o file:expout -e empty kyua debug \
161        --stdout=saved.out --stderr=saved.err single:with_cleanup
162
163    cat >expout <<EOF
164Body message to stdout
165Cleanup message to stdout
166EOF
167    atf_check -s exit:0 -o file:expout -e empty cat saved.out
168
169    cat >experr <<EOF
170Body message to stderr
171Cleanup message to stderr
172EOF
173    atf_check -s exit:0 -o file:experr -e empty cat saved.err
174}
175
176
177utils_test_case stdout_stderr_flags
178stdout_stderr_flags_body() {
179    cat >Kyuafile <<EOF
180syntax(2)
181test_suite("integration")
182atf_test_program{name="first"}
183atf_test_program{name="second"}
184EOF
185    utils_cp_helper expect_all_pass first
186    utils_cp_helper simple_all_pass second
187
188    cat >expout <<EOF
189second:pass  ->  passed
190EOF
191    atf_check -s exit:0 -o file:expout -e empty kyua debug \
192        --stdout=saved.out --stderr=saved.err second:pass
193
194    cat >expout <<EOF
195This is the stdout of pass
196EOF
197    cmp -s saved.out expout || atf_fail "--stdout did not redirect the" \
198        "standard output to the desired file"
199
200    cat >experr <<EOF
201This is the stderr of pass
202EOF
203    cmp -s saved.err experr || atf_fail "--stderr did not redirect the" \
204        "standard error to the desired file"
205}
206
207
208utils_test_case args_are_relative
209args_are_relative_body() {
210    mkdir root
211    cat >root/Kyuafile <<EOF
212syntax(2)
213test_suite("integration")
214include("subdir/Kyuafile")
215atf_test_program{name="prog"}
216EOF
217    utils_cp_helper simple_all_pass root/prog
218
219    mkdir root/subdir
220    cat >root/subdir/Kyuafile <<EOF
221syntax(2)
222test_suite("integration")
223atf_test_program{name="prog"}
224EOF
225    utils_cp_helper simple_some_fail root/subdir/prog
226
227    cat >expout <<EOF
228This is the stdout of fail
229subdir/prog:fail  ->  failed: This fails on purpose
230EOF
231    cat >experr <<EOF
232This is the stderr of fail
233EOF
234    atf_check -s exit:1 -o file:expout -e file:experr kyua debug \
235        -k "$(pwd)/root/Kyuafile" subdir/prog:fail
236}
237
238
239utils_test_case only_load_used_test_programs
240only_load_used_test_programs_body() {
241    cat >Kyuafile <<EOF
242syntax(2)
243test_suite("integration")
244atf_test_program{name="first"}
245atf_test_program{name="second"}
246EOF
247    utils_cp_helper simple_all_pass first
248    utils_cp_helper bad_test_program second
249
250    cat >expout <<EOF
251This is the stdout of pass
252first:pass  ->  passed
253EOF
254    cat >experr <<EOF
255This is the stderr of pass
256EOF
257    CREATE_COOKIE="$(pwd)/cookie"; export CREATE_COOKIE
258    atf_check -s exit:0 -o file:expout -e file:experr kyua debug first:pass
259    if [ -f "${CREATE_COOKIE}" ]; then
260        atf_fail "An unmatched test case has been executed, which harms" \
261            "performance"
262    fi
263}
264
265
266utils_test_case config_behavior
267config_behavior_body() {
268    cat >"my-config" <<EOF
269syntax(2)
270test_suites.suite1["the-variable"] = "value1"
271test_suites.suite2["the-variable"] = "override me"
272EOF
273
274    cat >Kyuafile <<EOF
275syntax(2)
276atf_test_program{name="config1", test_suite="suite1"}
277atf_test_program{name="config2", test_suite="suite2"}
278atf_test_program{name="config3", test_suite="suite3"}
279EOF
280    utils_cp_helper config config1
281    utils_cp_helper config config2
282    utils_cp_helper config config3
283
284    atf_check -s exit:1 -o match:'failed' -e empty \
285        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
286        debug config1:get_variable
287    atf_check -s exit:0 -o match:'passed' -e empty \
288        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
289        debug config2:get_variable
290    atf_check -s exit:0 -o match:'skipped' -e empty \
291        kyua -c my-config -v test_suites.suite2.the-variable=value2 \
292        debug config3:get_variable
293}
294
295
296utils_test_case build_root_flag
297build_root_flag_body() {
298    cat >Kyuafile <<EOF
299syntax(2)
300test_suite("integration")
301atf_test_program{name="first"}
302atf_test_program{name="second"}
303EOF
304    mkdir build
305    utils_cp_helper expect_all_pass build/first
306    utils_cp_helper simple_all_pass build/second
307
308    cat >expout <<EOF
309This is the stdout of pass
310second:pass  ->  passed
311EOF
312cat >experr <<EOF
313This is the stderr of pass
314EOF
315    atf_check -s exit:0 -o file:expout -e file:experr \
316        kyua debug --build-root=build second:pass
317}
318
319
320utils_test_case kyuafile_flag__ok
321kyuafile_flag__ok_body() {
322    cat >Kyuafile <<EOF
323This file is bogus but it is not loaded.
324EOF
325
326    cat >myfile <<EOF
327syntax(2)
328test_suite("hello-world")
329atf_test_program{name="sometest"}
330EOF
331    utils_cp_helper simple_all_pass sometest
332
333    atf_check -s exit:0 -o match:passed -e empty kyua test -k myfile sometest
334    atf_check -s exit:0 -o match:passed -e empty kyua test --kyuafile=myfile \
335        sometest
336}
337
338
339utils_test_case missing_kyuafile
340missing_kyuafile_body() {
341    cat >experr <<EOF
342kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
343EOF
344    atf_check -s exit:2 -o empty -e file:experr kyua debug foo:bar
345}
346
347
348utils_test_case bogus_kyuafile
349bogus_kyuafile_body() {
350    cat >Kyuafile <<EOF
351Hello, world.
352EOF
353    atf_check -s exit:2 -o empty \
354        -e match:"Load of 'Kyuafile' failed: .* Kyuafile:2:" kyua list
355}
356
357
358utils_test_case bogus_test_program
359bogus_test_program_body() {
360    cat >Kyuafile <<EOF
361syntax(2)
362test_suite("integration")
363atf_test_program{name="crash_on_list"}
364atf_test_program{name="non_executable"}
365EOF
366    utils_cp_helper bad_test_program crash_on_list
367    echo 'I am not executable' >non_executable
368
369    cat >experr <<EOF
370kyua: E: Unknown test case 'crash_on_list:a'.
371EOF
372    atf_check -s exit:2 -o empty -e file:experr kyua debug crash_on_list:a
373
374    cat >experr <<EOF
375kyua: E: Unknown test case 'non_executable:a'.
376EOF
377    atf_check -s exit:2 -o empty -e file:experr kyua debug non_executable:a
378
379    # CHECK_STYLE_DISABLE
380    cat >expout <<EOF
381crash_on_list:__test_cases_list__  ->  broken: Invalid header for test case list; expecting Content-Type for application/X-atf-tp version 1, got ''
382EOF
383    # CHECK_STYLE_ENABLE
384    atf_check -s exit:1 -o file:expout -e empty kyua debug \
385        crash_on_list:__test_cases_list__
386
387    # CHECK_STYLE_DISABLE
388    cat >expout <<EOF
389non_executable:__test_cases_list__  ->  broken: Permission denied to run test program
390EOF
391    # CHECK_STYLE_ENABLE
392    atf_check -s exit:1 -o file:expout -e empty kyua debug \
393        non_executable:__test_cases_list__
394}
395
396
397atf_init_test_cases() {
398    atf_add_test_case no_args
399    atf_add_test_case many_args
400    atf_add_test_case one_arg__ok_pass
401    atf_add_test_case one_arg__ok_fail
402    atf_add_test_case one_arg__no_match
403    atf_add_test_case one_arg__no_test_case
404    atf_add_test_case one_arg__bad_filter
405
406    atf_add_test_case body_and_cleanup
407
408    atf_add_test_case stdout_stderr_flags
409
410    atf_add_test_case args_are_relative
411
412    atf_add_test_case only_load_used_test_programs
413
414    atf_add_test_case config_behavior
415
416    atf_add_test_case build_root_flag
417    atf_add_test_case kyuafile_flag__ok
418    atf_add_test_case missing_kyuafile
419    atf_add_test_case bogus_kyuafile
420    atf_add_test_case bogus_test_program
421}
422