xref: /freebsd/contrib/kyua/integration/cmd_report_junit_test.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1# Copyright 2014 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 MOCK="${mock_env}" kyua test
49    grep '^Results saved to ' stdout | cut -d ' ' -f 4 >"${dbfile_name}"
50    rm stdout
51
52    # Ensure the results of 'report-junit' come from the database.
53    rm Kyuafile simple_all_pass
54}
55
56
57# Removes the contents of a properties tag from stdout.
58strip_properties='awk "
59BEGIN { skip = 0; }
60
61/<\/properties>/ {
62    print \"</properties>\";
63    skip = 0;
64    next;
65}
66
67/<properties>/ {
68    print \"<properties>\";
69    print \"CONTENTS STRIPPED BY TEST\";
70    skip = 1;
71    next;
72}
73
74{ if (!skip) print; }"'
75
76
77utils_test_case default_behavior__ok
78default_behavior__ok_body() {
79    utils_install_times_wrapper
80
81    run_tests "mock1
82this should not be seen
83mock1 new line" unused_dbfile_name
84
85    cat >expout <<EOF
86<?xml version="1.0" encoding="iso-8859-1"?>
87<testsuite>
88<properties>
89CONTENTS STRIPPED BY TEST
90</properties>
91<testcase classname="simple_all_pass" name="pass" time="S.UUU">
92<system-out>This is the stdout of pass
93</system-out>
94<system-err>Test case metadata
95------------------
96
97allowed_architectures is empty
98allowed_platforms is empty
99description is empty
100execenv is empty
101execenv_jail_params is empty
102has_cleanup = false
103is_exclusive = false
104required_configs is empty
105required_disk_space = 0
106required_files is empty
107required_kmods is empty
108required_memory = 0
109required_programs is empty
110required_user is empty
111timeout = 300
112
113Timing information
114------------------
115
116Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
117End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
118Duration:   S.UUUs
119
120Original stderr
121---------------
122
123This is the stderr of pass
124</system-err>
125</testcase>
126<testcase classname="simple_all_pass" name="skip" time="S.UUU">
127<skipped/>
128<system-out>This is the stdout of skip
129</system-out>
130<system-err>Skipped result details
131----------------------
132
133The reason for skipping is this
134
135Test case metadata
136------------------
137
138allowed_architectures is empty
139allowed_platforms is empty
140description is empty
141execenv is empty
142execenv_jail_params is empty
143has_cleanup = false
144is_exclusive = false
145required_configs is empty
146required_disk_space = 0
147required_files is empty
148required_kmods is empty
149required_memory = 0
150required_programs is empty
151required_user is empty
152timeout = 300
153
154Timing information
155------------------
156
157Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
158End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
159Duration:   S.UUUs
160
161Original stderr
162---------------
163
164This is the stderr of skip
165</system-err>
166</testcase>
167</testsuite>
168EOF
169    atf_check -s exit:0 -o file:expout -e empty -x "kyua report-junit" \
170        "| ${strip_properties}"
171}
172
173
174utils_test_case default_behavior__no_store
175default_behavior__no_store_body() {
176    echo 'kyua: E: No previous results file found for test suite' \
177        "$(utils_test_suite_id)." >experr
178    atf_check -s exit:2 -o empty -e file:experr kyua report-junit
179}
180
181
182utils_test_case results_file__explicit
183results_file__explicit_body() {
184    run_tests "mock1" dbfile_name1
185    run_tests "mock2" dbfile_name2
186
187    atf_check -s exit:0 -o match:"MOCK.*mock1" -o not-match:"MOCK.*mock2" \
188        -e empty kyua report-junit --results-file="$(cat dbfile_name1)"
189    atf_check -s exit:0 -o not-match:"MOCK.*mock1" -o match:"MOCK.*mock2" \
190        -e empty kyua report-junit --results-file="$(cat dbfile_name2)"
191}
192
193
194utils_test_case results_file__not_found
195results_file__not_found_body() {
196    atf_check -s exit:2 -o empty -e match:"kyua: E: No previous results.*foo" \
197        kyua report-junit --results-file=foo
198}
199
200
201utils_test_case output__explicit
202output__explicit_body() {
203    run_tests unused_mock unused_dbfile_name
204
205    cat >report <<EOF
206<?xml version="1.0" encoding="iso-8859-1"?>
207<testsuite>
208<properties>
209CONTENTS STRIPPED BY TEST
210</properties>
211<testcase classname="simple_all_pass" name="pass" time="S.UUU">
212<system-out>This is the stdout of pass
213</system-out>
214<system-err>Test case metadata
215------------------
216
217allowed_architectures is empty
218allowed_platforms is empty
219description is empty
220execenv is empty
221execenv_jail_params is empty
222has_cleanup = false
223is_exclusive = false
224required_configs is empty
225required_disk_space = 0
226required_files is empty
227required_kmods is empty
228required_memory = 0
229required_programs is empty
230required_user is empty
231timeout = 300
232
233Timing information
234------------------
235
236Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
237End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
238Duration:   S.UUUs
239
240Original stderr
241---------------
242
243This is the stderr of pass
244</system-err>
245</testcase>
246<testcase classname="simple_all_pass" name="skip" time="S.UUU">
247<skipped/>
248<system-out>This is the stdout of skip
249</system-out>
250<system-err>Skipped result details
251----------------------
252
253The reason for skipping is this
254
255Test case metadata
256------------------
257
258allowed_architectures is empty
259allowed_platforms is empty
260description is empty
261execenv is empty
262execenv_jail_params is empty
263has_cleanup = false
264is_exclusive = false
265required_configs is empty
266required_disk_space = 0
267required_files is empty
268required_kmods is empty
269required_memory = 0
270required_programs is empty
271required_user is empty
272timeout = 300
273
274Timing information
275------------------
276
277Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
278End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
279Duration:   S.UUUs
280
281Original stderr
282---------------
283
284This is the stderr of skip
285</system-err>
286</testcase>
287</testsuite>
288EOF
289
290    atf_check -s exit:0 -o file:report -e empty -x kyua report-junit \
291        --output=/dev/stdout "| ${strip_properties} | ${utils_strip_times}"
292    atf_check -s exit:0 -o empty -e save:stderr kyua report-junit \
293        --output=/dev/stderr
294    atf_check -s exit:0 -o file:report -x cat stderr \
295        "| ${strip_properties} | ${utils_strip_times}"
296
297    atf_check -s exit:0 -o empty -e empty kyua report-junit \
298        --output=my-file
299    atf_check -s exit:0 -o file:report -x cat my-file \
300        "| ${strip_properties} | ${utils_strip_times}"
301}
302
303
304atf_init_test_cases() {
305    atf_add_test_case default_behavior__ok
306    atf_add_test_case default_behavior__no_store
307
308    atf_add_test_case results_file__explicit
309    atf_add_test_case results_file__not_found
310
311    atf_add_test_case output__explicit
312}
313