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    has_cleanup = false
255    is_exclusive = false
256    required_configs is empty
257    required_disk_space = 0
258    required_files is empty
259    required_memory = 0
260    required_programs is empty
261    required_user is empty
262    timeout = 300
263
264Standard output:
265This is the stdout of skip
266
267Standard error:
268This is the stderr of skip
269===> Skipped tests
270simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
271===> Summary
272Results read from $(cat dbfile_name)
273Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
274Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
275End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
276Total time: S.UUUs
277EOF
278    atf_check -s exit:0 -o file:expout -e empty -x kyua report --verbose \
279        "| ${utils_strip_times_but_not_ids}"
280}
281
282
283utils_test_case results_filter__empty
284results_filter__empty_body() {
285    utils_install_times_wrapper
286
287    run_tests "mock1" dbfile_name1
288
289    cat >expout <<EOF
290===> Passed tests
291simple_all_pass:pass  ->  passed  [S.UUUs]
292===> Skipped tests
293simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
294===> Summary
295Results read from $(cat dbfile_name1)
296Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
297Total time: S.UUUs
298EOF
299    atf_check -s exit:0 -o file:expout -e empty kyua report --results-filter=
300}
301
302
303utils_test_case results_filter__one
304results_filter__one_body() {
305    utils_install_times_wrapper
306
307    run_tests "mock1" dbfile_name1
308
309    cat >expout <<EOF
310===> Passed tests
311simple_all_pass:pass  ->  passed  [S.UUUs]
312===> Summary
313Results read from $(cat dbfile_name1)
314Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
315Total time: S.UUUs
316EOF
317    atf_check -s exit:0 -o file:expout -e empty kyua report \
318        --results-filter=passed
319}
320
321
322utils_test_case results_filter__multiple_all_match
323results_filter__multiple_all_match_body() {
324    utils_install_times_wrapper
325
326    run_tests "mock1" dbfile_name1
327
328    cat >expout <<EOF
329===> Skipped tests
330simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
331===> Passed tests
332simple_all_pass:pass  ->  passed  [S.UUUs]
333===> Summary
334Results read from $(cat dbfile_name1)
335Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
336Total time: S.UUUs
337EOF
338    atf_check -s exit:0 -o file:expout -e empty kyua report \
339        --results-filter=skipped,passed
340}
341
342
343utils_test_case results_filter__multiple_some_match
344results_filter__multiple_some_match_body() {
345    utils_install_times_wrapper
346
347    run_tests "mock1" dbfile_name1
348
349    cat >expout <<EOF
350===> Skipped tests
351simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
352===> Summary
353Results read from $(cat dbfile_name1)
354Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
355Total time: S.UUUs
356EOF
357    atf_check -s exit:0 -o file:expout -e empty kyua report \
358        --results-filter=skipped,xfail,broken,failed
359}
360
361
362atf_init_test_cases() {
363    atf_add_test_case default_behavior__ok
364    atf_add_test_case default_behavior__no_store
365
366    atf_add_test_case results_file__explicit
367    atf_add_test_case results_file__not_found
368
369    atf_add_test_case filter__ok
370    atf_add_test_case filter__ok_passed_excluded_by_default
371    atf_add_test_case filter__no_match
372
373    atf_add_test_case verbose
374
375    atf_add_test_case output__explicit
376
377    atf_add_test_case results_filter__empty
378    atf_add_test_case results_filter__one
379    atf_add_test_case results_filter__multiple_all_match
380    atf_add_test_case results_filter__multiple_some_match
381}
382