1*b0d29bc4SBrooks Davis# Copyright 2012 The Kyua Authors. 2*b0d29bc4SBrooks Davis# All rights reserved. 3*b0d29bc4SBrooks Davis# 4*b0d29bc4SBrooks Davis# Redistribution and use in source and binary forms, with or without 5*b0d29bc4SBrooks Davis# modification, are permitted provided that the following conditions are 6*b0d29bc4SBrooks Davis# met: 7*b0d29bc4SBrooks Davis# 8*b0d29bc4SBrooks Davis# * Redistributions of source code must retain the above copyright 9*b0d29bc4SBrooks Davis# notice, this list of conditions and the following disclaimer. 10*b0d29bc4SBrooks Davis# * Redistributions in binary form must reproduce the above copyright 11*b0d29bc4SBrooks Davis# notice, this list of conditions and the following disclaimer in the 12*b0d29bc4SBrooks Davis# documentation and/or other materials provided with the distribution. 13*b0d29bc4SBrooks Davis# * Neither the name of Google Inc. nor the names of its contributors 14*b0d29bc4SBrooks Davis# may be used to endorse or promote products derived from this software 15*b0d29bc4SBrooks Davis# without specific prior written permission. 16*b0d29bc4SBrooks Davis# 17*b0d29bc4SBrooks Davis# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*b0d29bc4SBrooks Davis# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*b0d29bc4SBrooks Davis# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*b0d29bc4SBrooks Davis# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*b0d29bc4SBrooks Davis# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*b0d29bc4SBrooks Davis# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*b0d29bc4SBrooks Davis# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*b0d29bc4SBrooks Davis# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*b0d29bc4SBrooks Davis# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*b0d29bc4SBrooks Davis# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*b0d29bc4SBrooks Davis# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*b0d29bc4SBrooks Davis 29*b0d29bc4SBrooks Davis 30*b0d29bc4SBrooks Davis# Executes a mock test suite to generate data in the database. 31*b0d29bc4SBrooks Davis# 32*b0d29bc4SBrooks Davis# \param mock_env The value to store in a MOCK variable in the environment. 33*b0d29bc4SBrooks Davis# Use this to be able to differentiate executions by inspecting the 34*b0d29bc4SBrooks Davis# context of the output. 35*b0d29bc4SBrooks Davis# \param dbfile_name File to which to write the path to the generated database 36*b0d29bc4SBrooks Davis# file. 37*b0d29bc4SBrooks Davisrun_tests() { 38*b0d29bc4SBrooks Davis local mock_env="${1}"; shift 39*b0d29bc4SBrooks Davis local dbfile_name="${1}"; shift 40*b0d29bc4SBrooks Davis 41*b0d29bc4SBrooks Davis cat >Kyuafile <<EOF 42*b0d29bc4SBrooks Davissyntax(2) 43*b0d29bc4SBrooks Davistest_suite("integration") 44*b0d29bc4SBrooks Davisatf_test_program{name="simple_all_pass"} 45*b0d29bc4SBrooks Davisatf_test_program{name="simple_some_fail"} 46*b0d29bc4SBrooks Davisatf_test_program{name="metadata"} 47*b0d29bc4SBrooks DavisEOF 48*b0d29bc4SBrooks Davis 49*b0d29bc4SBrooks Davis utils_cp_helper simple_all_pass . 50*b0d29bc4SBrooks Davis utils_cp_helper simple_some_fail . 51*b0d29bc4SBrooks Davis utils_cp_helper metadata . 52*b0d29bc4SBrooks Davis atf_check -s exit:1 -o save:stdout -e empty env MOCK="${mock_env}" kyua test 53*b0d29bc4SBrooks Davis grep '^Results saved to ' stdout | cut -d ' ' -f 4 >"${dbfile_name}" 54*b0d29bc4SBrooks Davis rm stdout 55*b0d29bc4SBrooks Davis 56*b0d29bc4SBrooks Davis # Ensure the results of 'report-html' come from the database. 57*b0d29bc4SBrooks Davis rm Kyuafile simple_all_pass simple_some_fail metadata 58*b0d29bc4SBrooks Davis} 59*b0d29bc4SBrooks Davis 60*b0d29bc4SBrooks Davis 61*b0d29bc4SBrooks Davis# Ensure a file has a set of strings. 62*b0d29bc4SBrooks Davis# 63*b0d29bc4SBrooks Davis# \param file The name of the file to check. 64*b0d29bc4SBrooks Davis# \param ... List of strings to check. 65*b0d29bc4SBrooks Davischeck_in_file() { 66*b0d29bc4SBrooks Davis local file="${1}"; shift 67*b0d29bc4SBrooks Davis 68*b0d29bc4SBrooks Davis while [ ${#} -gt 0 ]; do 69*b0d29bc4SBrooks Davis echo "Checking for presence of '${1}' in ${file}" 70*b0d29bc4SBrooks Davis if grep "${1}" "${file}" >/dev/null; then 71*b0d29bc4SBrooks Davis : 72*b0d29bc4SBrooks Davis else 73*b0d29bc4SBrooks Davis atf_fail "Test case output not found in HTML page ${file}" 74*b0d29bc4SBrooks Davis fi 75*b0d29bc4SBrooks Davis shift 76*b0d29bc4SBrooks Davis done 77*b0d29bc4SBrooks Davis} 78*b0d29bc4SBrooks Davis 79*b0d29bc4SBrooks Davis 80*b0d29bc4SBrooks Davis# Ensure a file does not have a set of strings. 81*b0d29bc4SBrooks Davis# 82*b0d29bc4SBrooks Davis# \param file The name of the file to check. 83*b0d29bc4SBrooks Davis# \param ... List of strings to check. 84*b0d29bc4SBrooks Davischeck_not_in_file() { 85*b0d29bc4SBrooks Davis local file="${1}"; shift 86*b0d29bc4SBrooks Davis 87*b0d29bc4SBrooks Davis while [ ${#} -gt 0 ]; do 88*b0d29bc4SBrooks Davis echo "Checking for lack of '${1}' in ${file}" 89*b0d29bc4SBrooks Davis if grep "${1}" "${file}" >/dev/null; then 90*b0d29bc4SBrooks Davis atf_fail "Spurious test case output found in HTML page" 91*b0d29bc4SBrooks Davis fi 92*b0d29bc4SBrooks Davis shift 93*b0d29bc4SBrooks Davis done 94*b0d29bc4SBrooks Davis} 95*b0d29bc4SBrooks Davis 96*b0d29bc4SBrooks Davis 97*b0d29bc4SBrooks Davisutils_test_case default_behavior__ok 98*b0d29bc4SBrooks Davisdefault_behavior__ok_body() { 99*b0d29bc4SBrooks Davis run_tests "mock1" unused_dbfile_name 100*b0d29bc4SBrooks Davis 101*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html 102*b0d29bc4SBrooks Davis for f in \ 103*b0d29bc4SBrooks Davis html/index.html \ 104*b0d29bc4SBrooks Davis html/context.html \ 105*b0d29bc4SBrooks Davis html/simple_all_pass_skip.html \ 106*b0d29bc4SBrooks Davis html/simple_some_fail_fail.html 107*b0d29bc4SBrooks Davis do 108*b0d29bc4SBrooks Davis test -f "${f}" || atf_fail "Missing ${f}" 109*b0d29bc4SBrooks Davis done 110*b0d29bc4SBrooks Davis 111*b0d29bc4SBrooks Davis atf_check -o match:"2 TESTS FAILING" cat html/index.html 112*b0d29bc4SBrooks Davis 113*b0d29bc4SBrooks Davis check_in_file html/simple_all_pass_skip.html \ 114*b0d29bc4SBrooks Davis "This is the stdout of skip" "This is the stderr of skip" 115*b0d29bc4SBrooks Davis check_not_in_file html/simple_all_pass_skip.html \ 116*b0d29bc4SBrooks Davis "This is the stdout of pass" "This is the stderr of pass" \ 117*b0d29bc4SBrooks Davis "This is the stdout of fail" "This is the stderr of fail" \ 118*b0d29bc4SBrooks Davis "Test case did not write anything to" 119*b0d29bc4SBrooks Davis 120*b0d29bc4SBrooks Davis check_in_file html/simple_some_fail_fail.html \ 121*b0d29bc4SBrooks Davis "This is the stdout of fail" "This is the stderr of fail" 122*b0d29bc4SBrooks Davis check_not_in_file html/simple_some_fail_fail.html \ 123*b0d29bc4SBrooks Davis "This is the stdout of pass" "This is the stderr of pass" \ 124*b0d29bc4SBrooks Davis "This is the stdout of skip" "This is the stderr of skip" \ 125*b0d29bc4SBrooks Davis "Test case did not write anything to" 126*b0d29bc4SBrooks Davis 127*b0d29bc4SBrooks Davis check_in_file html/metadata_one_property.html \ 128*b0d29bc4SBrooks Davis "description = Does nothing but has one metadata property" 129*b0d29bc4SBrooks Davis check_not_in_file html/metadata_one_property.html \ 130*b0d29bc4SBrooks Davis "allowed_architectures = some-architecture" 131*b0d29bc4SBrooks Davis 132*b0d29bc4SBrooks Davis check_in_file html/metadata_many_properties.html \ 133*b0d29bc4SBrooks Davis "allowed_architectures = some-architecture" 134*b0d29bc4SBrooks Davis check_not_in_file html/metadata_many_properties.html \ 135*b0d29bc4SBrooks Davis "description = Does nothing but has one metadata property" 136*b0d29bc4SBrooks Davis} 137*b0d29bc4SBrooks Davis 138*b0d29bc4SBrooks Davis 139*b0d29bc4SBrooks Davisutils_test_case default_behavior__no_store 140*b0d29bc4SBrooks Davisdefault_behavior__no_store_body() { 141*b0d29bc4SBrooks Davis echo 'kyua: E: No previous results file found for test suite' \ 142*b0d29bc4SBrooks Davis "$(utils_test_suite_id)." >experr 143*b0d29bc4SBrooks Davis atf_check -s exit:2 -o empty -e file:experr kyua report-html 144*b0d29bc4SBrooks Davis} 145*b0d29bc4SBrooks Davis 146*b0d29bc4SBrooks Davis 147*b0d29bc4SBrooks Davisutils_test_case results_file__explicit 148*b0d29bc4SBrooks Davisresults_file__explicit_body() { 149*b0d29bc4SBrooks Davis run_tests "mock1" dbfile_name1 150*b0d29bc4SBrooks Davis run_tests "mock2" dbfile_name2 151*b0d29bc4SBrooks Davis 152*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html \ 153*b0d29bc4SBrooks Davis --results-file="$(cat dbfile_name1)" 154*b0d29bc4SBrooks Davis grep "MOCK.*mock1" html/context.html || atf_fail "Invalid context in report" 155*b0d29bc4SBrooks Davis 156*b0d29bc4SBrooks Davis rm -rf html 157*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html \ 158*b0d29bc4SBrooks Davis --results-file="$(cat dbfile_name2)" 159*b0d29bc4SBrooks Davis grep "MOCK.*mock2" html/context.html || atf_fail "Invalid context in report" 160*b0d29bc4SBrooks Davis} 161*b0d29bc4SBrooks Davis 162*b0d29bc4SBrooks Davis 163*b0d29bc4SBrooks Davisutils_test_case results_file__not_found 164*b0d29bc4SBrooks Davisresults_file__not_found_body() { 165*b0d29bc4SBrooks Davis atf_check -s exit:2 -o empty -e match:"kyua: E: No previous results.*foo" \ 166*b0d29bc4SBrooks Davis kyua report-html --results-file=foo 167*b0d29bc4SBrooks Davis} 168*b0d29bc4SBrooks Davis 169*b0d29bc4SBrooks Davis 170*b0d29bc4SBrooks Davisutils_test_case force__yes 171*b0d29bc4SBrooks Davisforce__yes_body() { 172*b0d29bc4SBrooks Davis run_tests "mock1" unused_dbfile_name 173*b0d29bc4SBrooks Davis 174*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html 175*b0d29bc4SBrooks Davis test -f html/index.html || atf_fail "Expected file not created" 176*b0d29bc4SBrooks Davis rm html/index.html 177*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html --force 178*b0d29bc4SBrooks Davis test -f html/index.html || atf_fail "Expected file not created" 179*b0d29bc4SBrooks Davis} 180*b0d29bc4SBrooks Davis 181*b0d29bc4SBrooks Davis 182*b0d29bc4SBrooks Davisutils_test_case force__no 183*b0d29bc4SBrooks Davisforce__no_body() { 184*b0d29bc4SBrooks Davis run_tests "mock1" unused_dbfile_name 185*b0d29bc4SBrooks Davis 186*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html 187*b0d29bc4SBrooks Davis test -f html/index.html || atf_fail "Expected file not created" 188*b0d29bc4SBrooks Davis rm html/index.html 189*b0d29bc4SBrooks Davis 190*b0d29bc4SBrooks Daviscat >experr <<EOF 191*b0d29bc4SBrooks Daviskyua: E: Output directory 'html' already exists; maybe use --force?. 192*b0d29bc4SBrooks DavisEOF 193*b0d29bc4SBrooks Davis atf_check -s exit:2 -o empty -e file:experr kyua report-html 194*b0d29bc4SBrooks Davis test ! -f html/index.html || atf_fail "Not expected file created" 195*b0d29bc4SBrooks Davis} 196*b0d29bc4SBrooks Davis 197*b0d29bc4SBrooks Davis 198*b0d29bc4SBrooks Davisutils_test_case output__explicit 199*b0d29bc4SBrooks Davisoutput__explicit_body() { 200*b0d29bc4SBrooks Davis run_tests "mock1" unused_dbfile_name 201*b0d29bc4SBrooks Davis 202*b0d29bc4SBrooks Davis mkdir output 203*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html --output=output/foo 204*b0d29bc4SBrooks Davis test ! -d html || atf_fail "Not expected directory created" 205*b0d29bc4SBrooks Davis test -f output/foo/index.html || atf_fail "Expected file not created" 206*b0d29bc4SBrooks Davis} 207*b0d29bc4SBrooks Davis 208*b0d29bc4SBrooks Davis 209*b0d29bc4SBrooks Davisutils_test_case results_filter__ok 210*b0d29bc4SBrooks Davisresults_filter__ok_body() { 211*b0d29bc4SBrooks Davis run_tests "mock1" unused_dbfile_name 212*b0d29bc4SBrooks Davis 213*b0d29bc4SBrooks Davis atf_check -s exit:0 -o ignore -e empty kyua report-html \ 214*b0d29bc4SBrooks Davis --results-filter=passed 215*b0d29bc4SBrooks Davis for f in \ 216*b0d29bc4SBrooks Davis html/index.html \ 217*b0d29bc4SBrooks Davis html/context.html \ 218*b0d29bc4SBrooks Davis html/simple_all_pass_pass.html \ 219*b0d29bc4SBrooks Davis html/simple_some_fail_pass.html \ 220*b0d29bc4SBrooks Davis html/metadata_no_properties.html \ 221*b0d29bc4SBrooks Davis html/metadata_with_cleanup.html 222*b0d29bc4SBrooks Davis do 223*b0d29bc4SBrooks Davis test -f "${f}" || atf_fail "Missing ${f}" 224*b0d29bc4SBrooks Davis done 225*b0d29bc4SBrooks Davis 226*b0d29bc4SBrooks Davis atf_check -o match:"2 TESTS FAILING" cat html/index.html 227*b0d29bc4SBrooks Davis 228*b0d29bc4SBrooks Davis check_in_file html/simple_all_pass_pass.html \ 229*b0d29bc4SBrooks Davis "This is the stdout of pass" "This is the stderr of pass" 230*b0d29bc4SBrooks Davis check_not_in_file html/simple_all_pass_pass.html \ 231*b0d29bc4SBrooks Davis "This is the stdout of skip" "This is the stderr of skip" \ 232*b0d29bc4SBrooks Davis "This is the stdout of fail" "This is the stderr of fail" \ 233*b0d29bc4SBrooks Davis "Test case did not write anything to" 234*b0d29bc4SBrooks Davis 235*b0d29bc4SBrooks Davis check_in_file html/simple_some_fail_pass.html \ 236*b0d29bc4SBrooks Davis "Test case did not write anything to stdout" \ 237*b0d29bc4SBrooks Davis "Test case did not write anything to stderr" 238*b0d29bc4SBrooks Davis check_not_in_file html/simple_some_fail_pass.html \ 239*b0d29bc4SBrooks Davis "This is the stdout of pass" "This is the stderr of pass" \ 240*b0d29bc4SBrooks Davis "This is the stdout of skip" "This is the stderr of skip" \ 241*b0d29bc4SBrooks Davis "This is the stdout of fail" "This is the stderr of fail" 242*b0d29bc4SBrooks Davis} 243*b0d29bc4SBrooks Davis 244*b0d29bc4SBrooks Davis 245*b0d29bc4SBrooks Davisutils_test_case results_filter__invalid 246*b0d29bc4SBrooks Davisresults_filter__invalid_body() { 247*b0d29bc4SBrooks Davis echo "kyua: E: Unknown result type 'foo-bar'." >experr 248*b0d29bc4SBrooks Davis atf_check -s exit:2 -o empty -e file:experr kyua report-html \ 249*b0d29bc4SBrooks Davis --results-filter=passed,foo-bar 250*b0d29bc4SBrooks Davis} 251*b0d29bc4SBrooks Davis 252*b0d29bc4SBrooks Davis 253*b0d29bc4SBrooks Davisatf_init_test_cases() { 254*b0d29bc4SBrooks Davis atf_add_test_case default_behavior__ok 255*b0d29bc4SBrooks Davis atf_add_test_case default_behavior__no_store 256*b0d29bc4SBrooks Davis 257*b0d29bc4SBrooks Davis atf_add_test_case results_file__explicit 258*b0d29bc4SBrooks Davis atf_add_test_case results_file__not_found 259*b0d29bc4SBrooks Davis 260*b0d29bc4SBrooks Davis atf_add_test_case force__yes 261*b0d29bc4SBrooks Davis atf_add_test_case force__no 262*b0d29bc4SBrooks Davis 263*b0d29bc4SBrooks Davis atf_add_test_case output__explicit 264*b0d29bc4SBrooks Davis 265*b0d29bc4SBrooks Davis atf_add_test_case results_filter__ok 266*b0d29bc4SBrooks Davis atf_add_test_case results_filter__invalid 267*b0d29bc4SBrooks Davis} 268