xref: /netbsd-src/external/bsd/kyua-cli/dist/integration/cmd_report_test.sh (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
1# Copyright 2011 Google Inc.
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#
36# \return The action identifier of the committed action.
37run_tests() {
38    local mock_env="${1}"
39
40    mkdir testsuite
41    cd testsuite
42
43    cat >Kyuafile <<EOF
44syntax(2)
45test_suite("integration")
46atf_test_program{name="simple_all_pass"}
47EOF
48
49    utils_cp_helper simple_all_pass .
50    test -d ../.kyua || mkdir ../.kyua
51    kyua=$(which kyua)
52    atf_check -s exit:0 -o save:stdout -e empty env \
53        HOME="$(pwd)/home" MOCK="${mock_env}" \
54        "${kyua}" test --store=../.kyua/store.db
55
56    action_id=$(grep '^Committed action ' stdout | cut -d ' ' -f 3)
57    echo "New action is ${action_id}"
58
59    cd -
60    # Ensure the results of 'report' come from the database.
61    rm -rf testsuite
62
63    return "${action_id}"
64}
65
66
67utils_test_case default_behavior__ok
68default_behavior__ok_body() {
69    utils_install_timestamp_wrapper
70
71    run_tests "mock1"
72
73    cat >expout <<EOF
74===> Skipped tests
75simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
76===> Summary
77Action: 1
78Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
79Total time: S.UUUs
80EOF
81    atf_check -s exit:0 -o file:expout -e empty kyua report
82
83    run_tests "mock2"
84
85    cat >expout <<EOF
86===> Skipped tests
87simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
88===> Summary
89Action: 2
90Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
91Total time: S.UUUs
92EOF
93    atf_check -s exit:0 -o file:expout -e empty kyua report
94}
95
96
97utils_test_case default_behavior__no_actions
98default_behavior__no_actions_body() {
99    kyua db-exec "SELECT * FROM actions"
100
101    echo 'kyua: E: No actions in the database.' >experr
102    atf_check -s exit:2 -o empty -e file:experr kyua report
103}
104
105
106utils_test_case default_behavior__no_store
107default_behavior__no_store_body() {
108    atf_check -s exit:2 -o empty \
109        -e match:"kyua: E: Cannot open '.*/.kyua/store.db': " kyua report
110}
111
112
113utils_test_case action__explicit
114action__explicit_body() {
115    run_tests "mock1"; action1=$?
116    run_tests "mock2"; action2=$?
117
118    atf_check -s exit:0 -o match:"MOCK=mock1" -o not-match:"MOCK=mock2" \
119        -o match:"Action: 1" -o not-match:"Action: 2" \
120        -e empty kyua report --action="${action1}" --show-context
121    atf_check -s exit:0 -o not-match:"MOCK=mock1" -o match:"MOCK=mock2" \
122        -o match:"Action: 2" -o not-match:"Action: 1" \
123        -e empty kyua report --action="${action2}" --show-context
124}
125
126
127utils_test_case action__not_found
128action__not_found_body() {
129    kyua db-exec "SELECT * FROM actions"
130
131    echo 'kyua: E: Error loading action 514: does not exist.' >experr
132    atf_check -s exit:2 -o empty -e file:experr kyua report --action=514
133}
134
135
136utils_test_case show_context
137show_context_body() {
138    run_tests "mock1"
139
140    cat >expout <<EOF
141===> Execution context
142Current directory: $(pwd)/testsuite
143Environment variables:
144EOF
145    mkdir testsuite
146    ( cd testsuite && HOME=$(pwd)/home MOCK=mock1 env ) \
147        | sort | sed -e 's,^,    ,' | grep -v '^    _.*=.*' >>expout
148    rmdir testsuite
149    cat >>expout <<EOF
150===> Skipped tests
151simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
152===> Summary
153Action: 1
154Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
155Total time: S.UUUs
156EOF
157    atf_check -s exit:0 -o file:expout -e empty -x kyua report --show-context \
158        "| ${utils_strip_timestamp} | grep -v '^    _.*=.*'"
159}
160
161
162utils_test_case output__change_file
163output__change_file_body() {
164    run_tests
165
166    cat >report <<EOF
167===> Skipped tests
168simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
169===> Summary
170Action: 1
171Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
172Total time: S.UUUs
173EOF
174
175    atf_check -s exit:0 -o file:report -e empty -x kyua report \
176        --output=/dev/stdout "| ${utils_strip_timestamp}"
177    atf_check -s exit:0 -o empty -e save:stderr kyua report \
178        --output=/dev/stderr
179    atf_check -s exit:0 -o file:report -x cat stderr \
180        "| ${utils_strip_timestamp}"
181
182    atf_check -s exit:0 -o empty -e empty kyua report \
183        --output=my-file
184    atf_check -s exit:0 -o file:report -x cat my-file \
185        "| ${utils_strip_timestamp}"
186}
187
188
189utils_test_case results_filter__empty
190results_filter__empty_body() {
191    utils_install_timestamp_wrapper
192
193    run_tests "mock1"
194
195    cat >expout <<EOF
196===> Passed tests
197simple_all_pass:pass  ->  passed  [S.UUUs]
198===> Skipped tests
199simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
200===> Summary
201Action: 1
202Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
203Total time: S.UUUs
204EOF
205    atf_check -s exit:0 -o file:expout -e empty kyua report --results-filter=
206}
207
208
209utils_test_case results_filter__one
210results_filter__one_body() {
211    utils_install_timestamp_wrapper
212
213    run_tests "mock1"
214
215    cat >expout <<EOF
216===> Passed tests
217simple_all_pass:pass  ->  passed  [S.UUUs]
218===> Summary
219Action: 1
220Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
221Total time: S.UUUs
222EOF
223    atf_check -s exit:0 -o file:expout -e empty kyua report \
224        --results-filter=passed
225}
226
227
228utils_test_case results_filter__multiple_all_match
229results_filter__multiple_all_match_body() {
230    utils_install_timestamp_wrapper
231
232    run_tests "mock1"
233
234    cat >expout <<EOF
235===> Skipped tests
236simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
237===> Passed tests
238simple_all_pass:pass  ->  passed  [S.UUUs]
239===> Summary
240Action: 1
241Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
242Total time: S.UUUs
243EOF
244    atf_check -s exit:0 -o file:expout -e empty kyua report \
245        --results-filter=skipped,passed
246}
247
248
249utils_test_case results_filter__multiple_some_match
250results_filter__multiple_some_match_body() {
251    utils_install_timestamp_wrapper
252
253    run_tests "mock1"
254
255    cat >expout <<EOF
256===> Skipped tests
257simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
258===> Summary
259Action: 1
260Test cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
261Total time: S.UUUs
262EOF
263    atf_check -s exit:0 -o file:expout -e empty kyua report \
264        --results-filter=skipped,xfail,broken,failed
265}
266
267
268atf_init_test_cases() {
269    atf_add_test_case default_behavior__ok
270    atf_add_test_case default_behavior__no_actions
271    atf_add_test_case default_behavior__no_store
272
273    atf_add_test_case action__explicit
274    atf_add_test_case action__not_found
275
276    atf_add_test_case show_context
277
278    atf_add_test_case output__change_file
279
280    atf_add_test_case results_filter__empty
281    atf_add_test_case results_filter__one
282    atf_add_test_case results_filter__multiple_all_match
283    atf_add_test_case results_filter__multiple_some_match
284}
285