1*11be35a1SLionel Sambuc# Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc# All rights reserved.
3*11be35a1SLionel Sambuc#
4*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc# met:
7*11be35a1SLionel Sambuc#
8*11be35a1SLionel Sambuc# * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc#   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc# * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc#   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc#   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc# * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc#   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc#   without specific prior written permission.
16*11be35a1SLionel Sambuc#
17*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc# Executes a mock test suite to generate data in the database.
31*11be35a1SLionel Sambuc#
32*11be35a1SLionel Sambuc# \param mock_env The value to store in a MOCK variable in the environment.
33*11be35a1SLionel Sambuc#     Use this to be able to differentiate executions by inspecting the
34*11be35a1SLionel Sambuc#     context of the output.
35*11be35a1SLionel Sambuc#
36*11be35a1SLionel Sambuc# \return The action identifier of the committed action.
37*11be35a1SLionel Sambucrun_tests() {
38*11be35a1SLionel Sambuc    local mock_env="${1}"
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc    mkdir testsuite
41*11be35a1SLionel Sambuc    cd testsuite
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc    cat >Kyuafile <<EOF
44*11be35a1SLionel Sambucsyntax(2)
45*11be35a1SLionel Sambuctest_suite("integration")
46*11be35a1SLionel Sambucatf_test_program{name="simple_all_pass"}
47*11be35a1SLionel SambucEOF
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc    utils_cp_helper simple_all_pass .
50*11be35a1SLionel Sambuc    test -d ../.kyua || mkdir ../.kyua
51*11be35a1SLionel Sambuc    kyua=$(which kyua)
52*11be35a1SLionel Sambuc    atf_check -s exit:0 -o save:stdout -e empty env \
53*11be35a1SLionel Sambuc        HOME="$(pwd)/home" MOCK="${mock_env}" \
54*11be35a1SLionel Sambuc        "${kyua}" test --store=../.kyua/store.db
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc    action_id=$(grep '^Committed action ' stdout | cut -d ' ' -f 3)
57*11be35a1SLionel Sambuc    echo "New action is ${action_id}"
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc    cd -
60*11be35a1SLionel Sambuc    # Ensure the results of 'report' come from the database.
61*11be35a1SLionel Sambuc    rm -rf testsuite
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc    return "${action_id}"
64*11be35a1SLionel Sambuc}
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambucutils_test_case default_behavior__ok
68*11be35a1SLionel Sambucdefault_behavior__ok_body() {
69*11be35a1SLionel Sambuc    utils_install_timestamp_wrapper
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc    run_tests "mock1"
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc    cat >expout <<EOF
74*11be35a1SLionel Sambuc===> Skipped tests
75*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
76*11be35a1SLionel Sambuc===> Summary
77*11be35a1SLionel SambucAction: 1
78*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
79*11be35a1SLionel SambucTotal time: S.UUUs
80*11be35a1SLionel SambucEOF
81*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc    run_tests "mock2"
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc    cat >expout <<EOF
86*11be35a1SLionel Sambuc===> Skipped tests
87*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
88*11be35a1SLionel Sambuc===> Summary
89*11be35a1SLionel SambucAction: 2
90*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
91*11be35a1SLionel SambucTotal time: S.UUUs
92*11be35a1SLionel SambucEOF
93*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report
94*11be35a1SLionel Sambuc}
95*11be35a1SLionel Sambuc
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambucutils_test_case default_behavior__no_actions
98*11be35a1SLionel Sambucdefault_behavior__no_actions_body() {
99*11be35a1SLionel Sambuc    kyua db-exec "SELECT * FROM actions"
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc    echo 'kyua: E: No actions in the database.' >experr
102*11be35a1SLionel Sambuc    atf_check -s exit:2 -o empty -e file:experr kyua report
103*11be35a1SLionel Sambuc}
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambucutils_test_case default_behavior__no_store
107*11be35a1SLionel Sambucdefault_behavior__no_store_body() {
108*11be35a1SLionel Sambuc    atf_check -s exit:2 -o empty \
109*11be35a1SLionel Sambuc        -e match:"kyua: E: Cannot open '.*/.kyua/store.db': " kyua report
110*11be35a1SLionel Sambuc}
111*11be35a1SLionel Sambuc
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambucutils_test_case action__explicit
114*11be35a1SLionel Sambucaction__explicit_body() {
115*11be35a1SLionel Sambuc    run_tests "mock1"; action1=$?
116*11be35a1SLionel Sambuc    run_tests "mock2"; action2=$?
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc    atf_check -s exit:0 -o match:"MOCK=mock1" -o not-match:"MOCK=mock2" \
119*11be35a1SLionel Sambuc        -o match:"Action: 1" -o not-match:"Action: 2" \
120*11be35a1SLionel Sambuc        -e empty kyua report --action="${action1}" --show-context
121*11be35a1SLionel Sambuc    atf_check -s exit:0 -o not-match:"MOCK=mock1" -o match:"MOCK=mock2" \
122*11be35a1SLionel Sambuc        -o match:"Action: 2" -o not-match:"Action: 1" \
123*11be35a1SLionel Sambuc        -e empty kyua report --action="${action2}" --show-context
124*11be35a1SLionel Sambuc}
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambucutils_test_case action__not_found
128*11be35a1SLionel Sambucaction__not_found_body() {
129*11be35a1SLionel Sambuc    kyua db-exec "SELECT * FROM actions"
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc    echo 'kyua: E: Error loading action 514: does not exist.' >experr
132*11be35a1SLionel Sambuc    atf_check -s exit:2 -o empty -e file:experr kyua report --action=514
133*11be35a1SLionel Sambuc}
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambucutils_test_case show_context
137*11be35a1SLionel Sambucshow_context_body() {
138*11be35a1SLionel Sambuc    run_tests "mock1"
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc    cat >expout <<EOF
141*11be35a1SLionel Sambuc===> Execution context
142*11be35a1SLionel SambucCurrent directory: $(pwd)/testsuite
143*11be35a1SLionel SambucEnvironment variables:
144*11be35a1SLionel SambucEOF
145*11be35a1SLionel Sambuc    mkdir testsuite
146*11be35a1SLionel Sambuc    ( cd testsuite && HOME=$(pwd)/home MOCK=mock1 env ) \
147*11be35a1SLionel Sambuc        | sort | sed -e 's,^,    ,' | grep -v '^    _.*=.*' >>expout
148*11be35a1SLionel Sambuc    rmdir testsuite
149*11be35a1SLionel Sambuc    cat >>expout <<EOF
150*11be35a1SLionel Sambuc===> Skipped tests
151*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
152*11be35a1SLionel Sambuc===> Summary
153*11be35a1SLionel SambucAction: 1
154*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
155*11be35a1SLionel SambucTotal time: S.UUUs
156*11be35a1SLionel SambucEOF
157*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty -x kyua report --show-context \
158*11be35a1SLionel Sambuc        "| ${utils_strip_timestamp} | grep -v '^    _.*=.*'"
159*11be35a1SLionel Sambuc}
160*11be35a1SLionel Sambuc
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambucutils_test_case output__change_file
163*11be35a1SLionel Sambucoutput__change_file_body() {
164*11be35a1SLionel Sambuc    run_tests
165*11be35a1SLionel Sambuc
166*11be35a1SLionel Sambuc    cat >report <<EOF
167*11be35a1SLionel Sambuc===> Skipped tests
168*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
169*11be35a1SLionel Sambuc===> Summary
170*11be35a1SLionel SambucAction: 1
171*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
172*11be35a1SLionel SambucTotal time: S.UUUs
173*11be35a1SLionel SambucEOF
174*11be35a1SLionel Sambuc
175*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:report -e empty -x kyua report \
176*11be35a1SLionel Sambuc        --output=/dev/stdout "| ${utils_strip_timestamp}"
177*11be35a1SLionel Sambuc    atf_check -s exit:0 -o empty -e save:stderr kyua report \
178*11be35a1SLionel Sambuc        --output=/dev/stderr
179*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:report -x cat stderr \
180*11be35a1SLionel Sambuc        "| ${utils_strip_timestamp}"
181*11be35a1SLionel Sambuc
182*11be35a1SLionel Sambuc    atf_check -s exit:0 -o empty -e empty kyua report \
183*11be35a1SLionel Sambuc        --output=my-file
184*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:report -x cat my-file \
185*11be35a1SLionel Sambuc        "| ${utils_strip_timestamp}"
186*11be35a1SLionel Sambuc}
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambucutils_test_case results_filter__empty
190*11be35a1SLionel Sambucresults_filter__empty_body() {
191*11be35a1SLionel Sambuc    utils_install_timestamp_wrapper
192*11be35a1SLionel Sambuc
193*11be35a1SLionel Sambuc    run_tests "mock1"
194*11be35a1SLionel Sambuc
195*11be35a1SLionel Sambuc    cat >expout <<EOF
196*11be35a1SLionel Sambuc===> Passed tests
197*11be35a1SLionel Sambucsimple_all_pass:pass  ->  passed  [S.UUUs]
198*11be35a1SLionel Sambuc===> Skipped tests
199*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
200*11be35a1SLionel Sambuc===> Summary
201*11be35a1SLionel SambucAction: 1
202*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
203*11be35a1SLionel SambucTotal time: S.UUUs
204*11be35a1SLionel SambucEOF
205*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report --results-filter=
206*11be35a1SLionel Sambuc}
207*11be35a1SLionel Sambuc
208*11be35a1SLionel Sambuc
209*11be35a1SLionel Sambucutils_test_case results_filter__one
210*11be35a1SLionel Sambucresults_filter__one_body() {
211*11be35a1SLionel Sambuc    utils_install_timestamp_wrapper
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc    run_tests "mock1"
214*11be35a1SLionel Sambuc
215*11be35a1SLionel Sambuc    cat >expout <<EOF
216*11be35a1SLionel Sambuc===> Passed tests
217*11be35a1SLionel Sambucsimple_all_pass:pass  ->  passed  [S.UUUs]
218*11be35a1SLionel Sambuc===> Summary
219*11be35a1SLionel SambucAction: 1
220*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
221*11be35a1SLionel SambucTotal time: S.UUUs
222*11be35a1SLionel SambucEOF
223*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report \
224*11be35a1SLionel Sambuc        --results-filter=passed
225*11be35a1SLionel Sambuc}
226*11be35a1SLionel Sambuc
227*11be35a1SLionel Sambuc
228*11be35a1SLionel Sambucutils_test_case results_filter__multiple_all_match
229*11be35a1SLionel Sambucresults_filter__multiple_all_match_body() {
230*11be35a1SLionel Sambuc    utils_install_timestamp_wrapper
231*11be35a1SLionel Sambuc
232*11be35a1SLionel Sambuc    run_tests "mock1"
233*11be35a1SLionel Sambuc
234*11be35a1SLionel Sambuc    cat >expout <<EOF
235*11be35a1SLionel Sambuc===> Skipped tests
236*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
237*11be35a1SLionel Sambuc===> Passed tests
238*11be35a1SLionel Sambucsimple_all_pass:pass  ->  passed  [S.UUUs]
239*11be35a1SLionel Sambuc===> Summary
240*11be35a1SLionel SambucAction: 1
241*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
242*11be35a1SLionel SambucTotal time: S.UUUs
243*11be35a1SLionel SambucEOF
244*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report \
245*11be35a1SLionel Sambuc        --results-filter=skipped,passed
246*11be35a1SLionel Sambuc}
247*11be35a1SLionel Sambuc
248*11be35a1SLionel Sambuc
249*11be35a1SLionel Sambucutils_test_case results_filter__multiple_some_match
250*11be35a1SLionel Sambucresults_filter__multiple_some_match_body() {
251*11be35a1SLionel Sambuc    utils_install_timestamp_wrapper
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc    run_tests "mock1"
254*11be35a1SLionel Sambuc
255*11be35a1SLionel Sambuc    cat >expout <<EOF
256*11be35a1SLionel Sambuc===> Skipped tests
257*11be35a1SLionel Sambucsimple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
258*11be35a1SLionel Sambuc===> Summary
259*11be35a1SLionel SambucAction: 1
260*11be35a1SLionel SambucTest cases: 2 total, 1 skipped, 0 expected failures, 0 broken, 0 failed
261*11be35a1SLionel SambucTotal time: S.UUUs
262*11be35a1SLionel SambucEOF
263*11be35a1SLionel Sambuc    atf_check -s exit:0 -o file:expout -e empty kyua report \
264*11be35a1SLionel Sambuc        --results-filter=skipped,xfail,broken,failed
265*11be35a1SLionel Sambuc}
266*11be35a1SLionel Sambuc
267*11be35a1SLionel Sambuc
268*11be35a1SLionel Sambucatf_init_test_cases() {
269*11be35a1SLionel Sambuc    atf_add_test_case default_behavior__ok
270*11be35a1SLionel Sambuc    atf_add_test_case default_behavior__no_actions
271*11be35a1SLionel Sambuc    atf_add_test_case default_behavior__no_store
272*11be35a1SLionel Sambuc
273*11be35a1SLionel Sambuc    atf_add_test_case action__explicit
274*11be35a1SLionel Sambuc    atf_add_test_case action__not_found
275*11be35a1SLionel Sambuc
276*11be35a1SLionel Sambuc    atf_add_test_case show_context
277*11be35a1SLionel Sambuc
278*11be35a1SLionel Sambuc    atf_add_test_case output__change_file
279*11be35a1SLionel Sambuc
280*11be35a1SLionel Sambuc    atf_add_test_case results_filter__empty
281*11be35a1SLionel Sambuc    atf_add_test_case results_filter__one
282*11be35a1SLionel Sambuc    atf_add_test_case results_filter__multiple_all_match
283*11be35a1SLionel Sambuc    atf_add_test_case results_filter__multiple_some_match
284*11be35a1SLionel Sambuc}
285