cmd_report_html_test.sh revision 1.1.1.1
1# Copyright 2012 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"}
47atf_test_program{name="simple_some_fail"}
48atf_test_program{name="metadata"}
49EOF
50
51    utils_cp_helper simple_all_pass .
52    utils_cp_helper simple_some_fail .
53    utils_cp_helper metadata .
54    test -d ../.kyua || mkdir ../.kyua
55    kyua=$(which kyua)
56    atf_check -s exit:1 -o save:stdout -e empty env \
57        HOME="$(pwd)/home" MOCK="${mock_env}" \
58        "${kyua}" test --store=../.kyua/store.db
59
60    action_id=$(grep '^Committed action ' stdout | cut -d ' ' -f 3)
61    echo "New action is ${action_id}"
62
63    cd -
64    # Ensure the results of 'report' come from the database.
65    rm -rf testsuite
66
67    return "${action_id}"
68}
69
70
71# Ensure a file has a set of strings.
72#
73# \param file The name of the file to check.
74# \param ... List of strings to check.
75check_in_file() {
76    local file="${1}"; shift
77
78    while [ ${#} -gt 0 ]; do
79        echo "Checking for presence of '${1}' in ${file}"
80        if grep "${1}" "${file}" >/dev/null; then
81            :
82        else
83            atf_fail "Test case output not found in HTML page"
84        fi
85        shift
86    done
87}
88
89
90# Ensure a file does not have a set of strings.
91#
92# \param file The name of the file to check.
93# \param ... List of strings to check.
94check_not_in_file() {
95    local file="${1}"; shift
96
97    while [ ${#} -gt 0 ]; do
98        echo "Checking for lack of '${1}' in ${file}"
99        if grep "${1}" "${file}" >/dev/null; then
100            atf_fail "Spurious test case output found in HTML page"
101        fi
102        shift
103    done
104}
105
106
107utils_test_case default_behavior__ok
108default_behavior__ok_body() {
109    utils_install_timestamp_wrapper
110
111    run_tests "mock1"
112
113    atf_check -s exit:0 -o ignore -e empty kyua report-html
114    for f in \
115        html/index.html \
116        html/context.html \
117        html/simple_all_pass_pass.html \
118        html/simple_all_pass_skip.html \
119        html/simple_some_fail_fail.html \
120        html/simple_some_fail_pass.html \
121        html/metadata_no_properties.html \
122        html/metadata_one_property.html \
123        html/metadata_many_properties.html \
124        html/metadata_with_cleanup.html
125    do
126        test -f "${f}" || atf_fail "Missing ${f}"
127    done
128
129    atf_check -o match:"2 TESTS FAILING" cat html/index.html
130
131    check_in_file html/simple_all_pass_pass.html \
132        "This is the stdout of pass" "This is the stderr of pass"
133    check_not_in_file html/simple_all_pass_pass.html \
134        "This is the stdout of skip" "This is the stderr of skip" \
135        "This is the stdout of fail" "This is the stderr of fail" \
136        "Test case did not write anything to"
137
138    check_in_file html/simple_all_pass_skip.html \
139        "This is the stdout of skip" "This is the stderr of skip"
140    check_not_in_file html/simple_all_pass_skip.html \
141        "This is the stdout of pass" "This is the stderr of pass" \
142        "This is the stdout of fail" "This is the stderr of fail" \
143        "Test case did not write anything to"
144
145    check_in_file html/simple_some_fail_fail.html \
146        "This is the stdout of fail" "This is the stderr of fail"
147    check_not_in_file html/simple_some_fail_fail.html \
148        "This is the stdout of pass" "This is the stderr of pass" \
149        "This is the stdout of skip" "This is the stderr of skip" \
150        "Test case did not write anything to"
151
152    check_in_file html/simple_some_fail_pass.html \
153        "Test case did not write anything to stdout" \
154        "Test case did not write anything to stderr"
155    check_not_in_file html/simple_some_fail_pass.html \
156        "This is the stdout of pass" "This is the stderr of pass" \
157        "This is the stdout of skip" "This is the stderr of skip" \
158        "This is the stdout of fail" "This is the stderr of fail"
159
160    check_in_file html/metadata_one_property.html \
161        "description = Does nothing but has one metadata property"
162    check_not_in_file html/metadata_one_property.html \
163        "allowed_architectures = some-architecture"
164
165    check_in_file html/metadata_many_properties.html \
166        "allowed_architectures = some-architecture"
167    check_not_in_file html/metadata_many_properties.html \
168        "description = Does nothing but has one metadata property"
169}
170
171
172utils_test_case default_behavior__no_actions
173default_behavior__no_actions_body() {
174    kyua db-exec "SELECT * FROM actions"
175
176    echo 'kyua: E: No actions in the database.' >experr
177    atf_check -s exit:2 -o empty -e file:experr kyua report-html
178}
179
180
181utils_test_case default_behavior__no_store
182default_behavior__no_store_body() {
183    atf_check -s exit:2 -o empty \
184        -e match:"kyua: E: Cannot open '.*/.kyua/store.db': " kyua report-html
185}
186
187
188utils_test_case action__explicit
189action__explicit_body() {
190    run_tests "mock1"; action1=$?
191    run_tests "mock2"; action2=$?
192
193    atf_check -s exit:0 -o ignore -e empty kyua report-html \
194        --action="${action1}"
195    grep "action 1" html/index.html || atf_fail "Invalid action in report"
196    grep "MOCK.*mock1" html/context.html || atf_fail "Invalid context in report"
197
198    rm -rf html
199    atf_check -s exit:0 -o ignore -e empty kyua report-html \
200        --action="${action2}"
201    grep "action 2" html/index.html || atf_fail "Invalid action in report"
202    grep "MOCK.*mock2" html/context.html || atf_fail "Invalid context in report"
203}
204
205
206utils_test_case action__not_found
207action__not_found_body() {
208    kyua db-exec "SELECT * FROM actions"
209
210    echo 'kyua: E: Error loading action 514: does not exist.' >experr
211    atf_check -s exit:2 -o empty -e file:experr kyua report-html --action=514
212}
213
214
215utils_test_case force__yes
216force__yes_body() {
217    run_tests "mock1"
218
219    atf_check -s exit:0 -o ignore -e empty kyua report-html
220    test -f html/index.html || atf_fail "Expected file not created"
221    rm html/index.html
222    atf_check -s exit:0 -o ignore -e empty kyua report-html --force
223    test -f html/index.html || atf_fail "Expected file not created"
224}
225
226
227utils_test_case force__no
228force__no_body() {
229    run_tests "mock1"
230
231    atf_check -s exit:0 -o ignore -e empty kyua report-html
232    test -f html/index.html || atf_fail "Expected file not created"
233    rm html/index.html
234
235cat >experr <<EOF
236kyua: E: Output directory 'html' already exists; maybe use --force?.
237EOF
238    atf_check -s exit:2 -o empty -e file:experr kyua report-html
239    test ! -f html/index.html || atf_fail "Not expected file created"
240}
241
242
243utils_test_case output__explicit
244output__explicit_body() {
245    run_tests "mock1"
246
247    mkdir output
248    atf_check -s exit:0 -o ignore -e empty kyua report-html --output=output/foo
249    test ! -d html || atf_fail "Not expected directory created"
250    test -f output/foo/index.html || atf_fail "Expected file not created"
251}
252
253
254atf_init_test_cases() {
255    atf_add_test_case default_behavior__ok
256    atf_add_test_case default_behavior__no_actions
257    atf_add_test_case default_behavior__no_store
258
259    atf_add_test_case action__explicit
260    atf_add_test_case action__not_found
261
262    atf_add_test_case force__yes
263    atf_add_test_case force__no
264
265    atf_add_test_case output__explicit
266}
267