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