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
100has_cleanup = false
101is_exclusive = false
102required_configs is empty
103required_disk_space = 0
104required_files is empty
105required_memory = 0
106required_programs is empty
107required_user is empty
108timeout = 300
109
110Timing information
111------------------
112
113Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
114End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
115Duration:   S.UUUs
116
117Original stderr
118---------------
119
120This is the stderr of pass
121</system-err>
122</testcase>
123<testcase classname="simple_all_pass" name="skip" time="S.UUU">
124<skipped/>
125<system-out>This is the stdout of skip
126</system-out>
127<system-err>Skipped result details
128----------------------
129
130The reason for skipping is this
131
132Test case metadata
133------------------
134
135allowed_architectures is empty
136allowed_platforms is empty
137description is empty
138has_cleanup = false
139is_exclusive = false
140required_configs is empty
141required_disk_space = 0
142required_files is empty
143required_memory = 0
144required_programs is empty
145required_user is empty
146timeout = 300
147
148Timing information
149------------------
150
151Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
152End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
153Duration:   S.UUUs
154
155Original stderr
156---------------
157
158This is the stderr of skip
159</system-err>
160</testcase>
161</testsuite>
162EOF
163    atf_check -s exit:0 -o file:expout -e empty -x "kyua report-junit" \
164        "| ${strip_properties}"
165}
166
167
168utils_test_case default_behavior__no_store
169default_behavior__no_store_body() {
170    echo 'kyua: E: No previous results file found for test suite' \
171        "$(utils_test_suite_id)." >experr
172    atf_check -s exit:2 -o empty -e file:experr kyua report-junit
173}
174
175
176utils_test_case results_file__explicit
177results_file__explicit_body() {
178    run_tests "mock1" dbfile_name1
179    run_tests "mock2" dbfile_name2
180
181    atf_check -s exit:0 -o match:"MOCK.*mock1" -o not-match:"MOCK.*mock2" \
182        -e empty kyua report-junit --results-file="$(cat dbfile_name1)"
183    atf_check -s exit:0 -o not-match:"MOCK.*mock1" -o match:"MOCK.*mock2" \
184        -e empty kyua report-junit --results-file="$(cat dbfile_name2)"
185}
186
187
188utils_test_case results_file__not_found
189results_file__not_found_body() {
190    atf_check -s exit:2 -o empty -e match:"kyua: E: No previous results.*foo" \
191        kyua report-junit --results-file=foo
192}
193
194
195utils_test_case output__explicit
196output__explicit_body() {
197    run_tests unused_mock unused_dbfile_name
198
199    cat >report <<EOF
200<?xml version="1.0" encoding="iso-8859-1"?>
201<testsuite>
202<properties>
203CONTENTS STRIPPED BY TEST
204</properties>
205<testcase classname="simple_all_pass" name="pass" time="S.UUU">
206<system-out>This is the stdout of pass
207</system-out>
208<system-err>Test case metadata
209------------------
210
211allowed_architectures is empty
212allowed_platforms is empty
213description is empty
214has_cleanup = false
215is_exclusive = false
216required_configs is empty
217required_disk_space = 0
218required_files is empty
219required_memory = 0
220required_programs is empty
221required_user is empty
222timeout = 300
223
224Timing information
225------------------
226
227Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
228End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
229Duration:   S.UUUs
230
231Original stderr
232---------------
233
234This is the stderr of pass
235</system-err>
236</testcase>
237<testcase classname="simple_all_pass" name="skip" time="S.UUU">
238<skipped/>
239<system-out>This is the stdout of skip
240</system-out>
241<system-err>Skipped result details
242----------------------
243
244The reason for skipping is this
245
246Test case metadata
247------------------
248
249allowed_architectures is empty
250allowed_platforms is empty
251description is empty
252has_cleanup = false
253is_exclusive = false
254required_configs is empty
255required_disk_space = 0
256required_files is empty
257required_memory = 0
258required_programs is empty
259required_user is empty
260timeout = 300
261
262Timing information
263------------------
264
265Start time: YYYY-MM-DDTHH:MM:SS.ssssssZ
266End time:   YYYY-MM-DDTHH:MM:SS.ssssssZ
267Duration:   S.UUUs
268
269Original stderr
270---------------
271
272This is the stderr of skip
273</system-err>
274</testcase>
275</testsuite>
276EOF
277
278    atf_check -s exit:0 -o file:report -e empty -x kyua report-junit \
279        --output=/dev/stdout "| ${strip_properties} | ${utils_strip_times}"
280    atf_check -s exit:0 -o empty -e save:stderr kyua report-junit \
281        --output=/dev/stderr
282    atf_check -s exit:0 -o file:report -x cat stderr \
283        "| ${strip_properties} | ${utils_strip_times}"
284
285    atf_check -s exit:0 -o empty -e empty kyua report-junit \
286        --output=my-file
287    atf_check -s exit:0 -o file:report -x cat my-file \
288        "| ${strip_properties} | ${utils_strip_times}"
289}
290
291
292atf_init_test_cases() {
293    atf_add_test_case default_behavior__ok
294    atf_add_test_case default_behavior__no_store
295
296    atf_add_test_case results_file__explicit
297    atf_add_test_case results_file__not_found
298
299    atf_add_test_case output__explicit
300}
301