xref: /freebsd/contrib/kyua/integration/cmd_report_test.sh (revision c5c02a131a0e2ef52771e683269bc8778fe511f3)
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
30# Executes a mock test suite to generate data in the database.
31#
32# \param mock_env The value to store in a MOCK variable in the environment.
33#     Use this to be able to differentiate executions by inspecting the
34#     context of the output.
35# \param dbfile_name File to which to write the path to the generated database
36#     file.
37run_tests() {
38    local mock_env="${1}"; shift
39    local dbfile_name="${1}"; shift
40
41    cat >Kyuafile <<EOF
42syntax(2)
43test_suite("integration")
44atf_test_program{name="simple_all_pass"}
45EOF
46
47    utils_cp_helper simple_all_pass .
48    atf_check -s exit:0 -o save:stdout -e empty env \
49        MOCK="${mock_env}" _='fake-value' kyua test
50    grep '^Results saved to ' stdout | cut -d ' ' -f 4 >"${dbfile_name}"
51    rm stdout
52
53    # Ensure the results of 'report' come from the database.
54    rm Kyuafile simple_all_pass
55}
56
57
58utils_test_case default_behavior__ok
59default_behavior__ok_body() {
60    utils_install_times_wrapper
61
62    run_tests "mock1" dbfile_name1
63
64    cat >expout <<EOF
65===> Skipped tests
66simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
67===> Summary
68Results read from $(cat dbfile_name1)
69Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
70Total time: S.UUUs
71EOF
72    atf_check -s exit:0 -o file:expout -e empty kyua report
73
74    run_tests "mock2" dbfile_name2
75
76    cat >expout <<EOF
77===> Skipped tests
78simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
79===> Summary
80Results read from $(cat dbfile_name2)
81Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
82Total time: S.UUUs
83EOF
84    atf_check -s exit:0 -o file:expout -e empty kyua report
85}
86
87
88utils_test_case default_behavior__no_store
89default_behavior__no_store_body() {
90    echo 'kyua: E: No previous results file found for test suite' \
91        "$(utils_test_suite_id)." >experr
92    atf_check -s exit:2 -o empty -e file:experr kyua report
93}
94
95
96utils_test_case results_file__explicit
97results_file__explicit_body() {
98    run_tests "mock1" dbfile_name1
99    run_tests "mock2" dbfile_name2
100
101    atf_check -s exit:0 -o match:"MOCK=mock1" -o not-match:"MOCK=mock2" \
102        -e empty kyua report --results-file="$(cat dbfile_name1)" \
103        --verbose
104    atf_check -s exit:0 -o not-match:"MOCK=mock1" -o match:"MOCK=mock2" \
105        -e empty kyua report --results-file="$(cat dbfile_name2)" \
106        --verbose
107}
108
109
110utils_test_case results_file__not_found
111results_file__not_found_body() {
112    atf_check -s exit:2 -o empty -e match:"kyua: E: No previous results.*foo" \
113        kyua report --results-file=foo
114}
115
116
117utils_test_case output__explicit
118output__explicit_body() {
119    run_tests unused_mock dbfile_name
120
121    cat >report <<EOF
122===> Skipped tests
123simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
124===> Summary
125Results read from $(cat dbfile_name)
126Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
127Total time: S.UUUs
128EOF
129
130    atf_check -s exit:0 -o file:report -e empty -x kyua report \
131        --output=/dev/stdout "| ${utils_strip_times_but_not_ids}"
132    atf_check -s exit:0 -o empty -e save:stderr kyua report \
133        --output=/dev/stderr
134    atf_check -s exit:0 -o file:report -x cat stderr \
135        "| ${utils_strip_times_but_not_ids}"
136
137    atf_check -s exit:0 -o empty -e empty kyua report \
138        --output=my-file
139    atf_check -s exit:0 -o file:report -x cat my-file \
140        "| ${utils_strip_times_but_not_ids}"
141}
142
143
144utils_test_case filter__ok
145filter__ok_body() {
146    utils_install_times_wrapper
147
148    run_tests "mock1" dbfile_name1
149
150    cat >expout <<EOF
151===> Skipped tests
152simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
153===> Summary
154Results read from $(cat dbfile_name1)
155Test cases: 1 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
156Total time: S.UUUs
157EOF
158    atf_check -s exit:0 -o file:expout -e empty kyua report \
159        simple_all_pass:skip
160}
161
162
163utils_test_case filter__ok_passed_excluded_by_default
164filter__ok_passed_excluded_by_default_body() {
165    utils_install_times_wrapper
166
167    run_tests "mock1" dbfile_name1
168
169    # Passed results are excluded by default so they are not displayed even if
170    # requested with a test case filter.  This might be somewhat confusing...
171    cat >expout <<EOF
172===> Summary
173Results read from $(cat dbfile_name1)
174Test cases: 1 total, 0 skipped, 0 expected failures, 0 broken, 0 failed
175Total time: S.UUUs
176EOF
177    atf_check -s exit:0 -o file:expout -e empty kyua report \
178        simple_all_pass:pass
179    cat >expout <<EOF
180===> Passed tests
181simple_all_pass:pass  ->  passed  [S.UUUs]
182===> Summary
183Results read from $(cat dbfile_name1)
184Test cases: 1 total, 0 skipped, 0 expected failures, 0 broken, 0 failed
185Total time: S.UUUs
186EOF
187    atf_check -s exit:0 -o file:expout -e empty kyua report \
188        --results-filter= simple_all_pass:pass
189}
190
191
192utils_test_case filter__no_match
193filter__no_match_body() {
194    utils_install_times_wrapper
195
196    run_tests "mock1" dbfile_name1
197
198    cat >expout <<EOF
199===> Skipped tests
200simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
201===> Summary
202Results read from $(cat dbfile_name1)
203Test cases: 1 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
204Total time: S.UUUs
205EOF
206    cat >experr <<EOF
207kyua: W: No test cases matched by the filter 'first'.
208kyua: W: No test cases matched by the filter 'simple_all_pass:second'.
209EOF
210    atf_check -s exit:1 -o file:expout -e file:experr kyua report \
211        first simple_all_pass:skip simple_all_pass:second
212}
213
214
215utils_test_case verbose
216verbose_body() {
217    # Switch to the current directory using its physical location and update
218    # HOME accordingly.  Otherwise, the test below where we compare the value
219    # of HOME in the output might fail if the path to HOME contains a symlink
220    # (as is the case in OS X when HOME points to the temporary directory.)
221    local real_cwd="$(pwd -P)"
222    cd "${real_cwd}"
223    HOME="${real_cwd}"
224
225    run_tests "mock1
226has multiple lines
227and terminates here" dbfile_name
228
229    cat >expout <<EOF
230===> Execution context
231Current directory: ${real_cwd}
232Environment variables:
233EOF
234    # $_ is a bash variable.  To keep our tests stable, we override its value
235    # below to match the hardcoded value in run_tests.
236    env \
237        HOME="${real_cwd}" \
238        MOCK="mock1
239has multiple lines
240and terminates here" \
241        _='fake-value' \
242        "$(atf_get_srcdir)/helpers/dump_env" '    ' '        ' >>expout
243    cat >>expout <<EOF
244===> simple_all_pass:skip
245Result:     skipped: The reason for skipping is this
246Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
247End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
248Duration:   S.UUUs
249
250Metadata:
251    allowed_architectures is empty
252    allowed_platforms is empty
253    description is empty
254    execenv is empty
255    execenv_jail_params is empty
256    has_cleanup = false
257    is_exclusive = false
258    required_configs is empty
259    required_disk_space = 0
260    required_files is empty
261    required_memory = 0
262    required_programs is empty
263    required_user is empty
264    timeout = 300
265
266Standard output:
267This is the stdout of skip
268
269Standard error:
270This is the stderr of skip
271===> Skipped tests
272simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
273===> Summary
274Results read from $(cat dbfile_name)
275Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
276Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
277End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
278Total time: S.UUUs
279EOF
280    atf_check -s exit:0 -o file:expout -e empty -x kyua report --verbose \
281        "| ${utils_strip_times_but_not_ids}"
282}
283
284
285utils_test_case results_filter__empty
286results_filter__empty_body() {
287    utils_install_times_wrapper
288
289    run_tests "mock1" dbfile_name1
290
291    cat >expout <<EOF
292===> Passed tests
293simple_all_pass:pass  ->  passed  [S.UUUs]
294===> Skipped tests
295simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
296===> Summary
297Results read from $(cat dbfile_name1)
298Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
299Total time: S.UUUs
300EOF
301    atf_check -s exit:0 -o file:expout -e empty kyua report --results-filter=
302}
303
304
305utils_test_case results_filter__one
306results_filter__one_body() {
307    utils_install_times_wrapper
308
309    run_tests "mock1" dbfile_name1
310
311    cat >expout <<EOF
312===> Passed tests
313simple_all_pass:pass  ->  passed  [S.UUUs]
314===> Summary
315Results read from $(cat dbfile_name1)
316Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
317Total time: S.UUUs
318EOF
319    atf_check -s exit:0 -o file:expout -e empty kyua report \
320        --results-filter=passed
321}
322
323
324utils_test_case results_filter__multiple_all_match
325results_filter__multiple_all_match_body() {
326    utils_install_times_wrapper
327
328    run_tests "mock1" dbfile_name1
329
330    cat >expout <<EOF
331===> Skipped tests
332simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
333===> Passed tests
334simple_all_pass:pass  ->  passed  [S.UUUs]
335===> Summary
336Results read from $(cat dbfile_name1)
337Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
338Total time: S.UUUs
339EOF
340    atf_check -s exit:0 -o file:expout -e empty kyua report \
341        --results-filter=skipped,passed
342}
343
344
345utils_test_case results_filter__multiple_some_match
346results_filter__multiple_some_match_body() {
347    utils_install_times_wrapper
348
349    run_tests "mock1" dbfile_name1
350
351    cat >expout <<EOF
352===> Skipped tests
353simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
354===> Summary
355Results read from $(cat dbfile_name1)
356Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
357Total time: S.UUUs
358EOF
359    atf_check -s exit:0 -o file:expout -e empty kyua report \
360        --results-filter=skipped,xfail,broken,failed
361}
362
363
364atf_init_test_cases() {
365    atf_add_test_case default_behavior__ok
366    atf_add_test_case default_behavior__no_store
367
368    atf_add_test_case results_file__explicit
369    atf_add_test_case results_file__not_found
370
371    atf_add_test_case filter__ok
372    atf_add_test_case filter__ok_passed_excluded_by_default
373    atf_add_test_case filter__no_match
374
375    atf_add_test_case verbose
376
377    atf_add_test_case output__explicit
378
379    atf_add_test_case results_filter__empty
380    atf_add_test_case results_filter__one
381    atf_add_test_case results_filter__multiple_all_match
382    atf_add_test_case results_filter__multiple_some_match
383}
384