1b0d29bc4SBrooks Davis# Copyright 2011 The Kyua Authors. 2b0d29bc4SBrooks Davis# All rights reserved. 3b0d29bc4SBrooks Davis# 4b0d29bc4SBrooks Davis# Redistribution and use in source and binary forms, with or without 5b0d29bc4SBrooks Davis# modification, are permitted provided that the following conditions are 6b0d29bc4SBrooks Davis# met: 7b0d29bc4SBrooks Davis# 8b0d29bc4SBrooks Davis# * Redistributions of source code must retain the above copyright 9b0d29bc4SBrooks Davis# notice, this list of conditions and the following disclaimer. 10b0d29bc4SBrooks Davis# * Redistributions in binary form must reproduce the above copyright 11b0d29bc4SBrooks Davis# notice, this list of conditions and the following disclaimer in the 12b0d29bc4SBrooks Davis# documentation and/or other materials provided with the distribution. 13b0d29bc4SBrooks Davis# * Neither the name of Google Inc. nor the names of its contributors 14b0d29bc4SBrooks Davis# may be used to endorse or promote products derived from this software 15b0d29bc4SBrooks Davis# without specific prior written permission. 16b0d29bc4SBrooks Davis# 17b0d29bc4SBrooks Davis# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18b0d29bc4SBrooks Davis# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19b0d29bc4SBrooks Davis# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20b0d29bc4SBrooks Davis# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21b0d29bc4SBrooks Davis# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22b0d29bc4SBrooks Davis# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23b0d29bc4SBrooks Davis# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24b0d29bc4SBrooks Davis# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25b0d29bc4SBrooks Davis# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26b0d29bc4SBrooks Davis# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27b0d29bc4SBrooks Davis# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28b0d29bc4SBrooks Davis 29b0d29bc4SBrooks Davis 30b0d29bc4SBrooks Davis# Subcommand to strip out the durations and timestamps in a report. 31b0d29bc4SBrooks Davis# 32b0d29bc4SBrooks Davis# This is to make the reports deterministic and thus easily testable. The 33b0d29bc4SBrooks Davis# time deltas are replaced by the fixed string S.UUU and the timestamps are 34b0d29bc4SBrooks Davis# replaced by the fixed strings YYYYMMDD.HHMMSS.ssssss and 35b0d29bc4SBrooks Davis# YYYY-MM-DDTHH:MM:SS.ssssssZ depending on their original format. 36b0d29bc4SBrooks Davis# 37b0d29bc4SBrooks Davis# This variable should be used as shown here: 38b0d29bc4SBrooks Davis# 39b0d29bc4SBrooks Davis# atf_check ... -x kyua report "| ${utils_strip_times}" 40b0d29bc4SBrooks Davis# 41b0d29bc4SBrooks Davis# Use the utils_install_times_wrapper function to create a 'kyua' wrapper 42b0d29bc4SBrooks Davis# script that automatically does this. 43b0d29bc4SBrooks Davis# CHECK_STYLE_DISABLE 44b0d29bc4SBrooks Davisutils_strip_times='sed -E \ 45*6b13d60bSMuhammad Moinur Rahman -e "s,( |\[|\")[0-9][0-9]*\.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \ 46b0d29bc4SBrooks Davis -e "s,[0-9]{8}-[0-9]{6}-[0-9]{6},YYYYMMDD-HHMMSS-ssssss,g" \ 47b0d29bc4SBrooks Davis -e "s,[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}Z,YYYY-MM-DDTHH:MM:SS.ssssssZ,g"' 48b0d29bc4SBrooks Davis# CHECK_STYLE_ENABLE 49b0d29bc4SBrooks Davis 50b0d29bc4SBrooks Davis 51b0d29bc4SBrooks Davis# Same as utils_strip_times but avoids stripping timestamp-based report IDs. 52b0d29bc4SBrooks Davis# 53b0d29bc4SBrooks Davis# This is to make the reports deterministic and thus easily testable. The 54b0d29bc4SBrooks Davis# time deltas are replaced by the fixed string S.UUU and the timestamps are 55b0d29bc4SBrooks Davis# replaced by the fixed string YYYY-MM-DDTHH:MM:SS.ssssssZ. 56b0d29bc4SBrooks Davis# CHECK_STYLE_DISABLE 57b0d29bc4SBrooks Davisutils_strip_times_but_not_ids='sed -E \ 58*6b13d60bSMuhammad Moinur Rahman -e "s,( |\[|\")[0-9][0-9]*\.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \ 59b0d29bc4SBrooks Davis -e "s,[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{6}Z,YYYY-MM-DDTHH:MM:SS.ssssssZ,g"' 60b0d29bc4SBrooks Davis# CHECK_STYLE_ENABLE 61b0d29bc4SBrooks Davis 62b0d29bc4SBrooks Davis 63b0d29bc4SBrooks Davis# Computes the results id for a test suite run. 64b0d29bc4SBrooks Davis# 65b0d29bc4SBrooks Davis# The computed path is "generic" in the sense that it does not include a 66b0d29bc4SBrooks Davis# real timestamp: it only includes a placeholder. This function should be 67b0d29bc4SBrooks Davis# used along the utils_strip_times function so that the timestamps of 68b0d29bc4SBrooks Davis# the real results files are stripped out. 69b0d29bc4SBrooks Davis# 70b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd. 71b0d29bc4SBrooks Davisutils_results_id() { 72b0d29bc4SBrooks Davis local test_suite_id="$(utils_test_suite_id "${@}")" 73b0d29bc4SBrooks Davis echo "${test_suite_id}.YYYYMMDD-HHMMSS-ssssss" 74b0d29bc4SBrooks Davis} 75b0d29bc4SBrooks Davis 76b0d29bc4SBrooks Davis 77b0d29bc4SBrooks Davis# Computes the results file for a test suite run. 78b0d29bc4SBrooks Davis# 79b0d29bc4SBrooks Davis# The computed path is "generic" in the sense that it does not include a 80b0d29bc4SBrooks Davis# real timestamp: it only includes a placeholder. This function should be 81b0d29bc4SBrooks Davis# used along the utils_strip_times function so that the timestampts of the 82b0d29bc4SBrooks Davis# real results files are stripped out. 83b0d29bc4SBrooks Davis# 84b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd. 85b0d29bc4SBrooks Davisutils_results_file() { 86b0d29bc4SBrooks Davis echo "${HOME}/.kyua/store/results.$(utils_results_id "${@}").db" 87b0d29bc4SBrooks Davis} 88b0d29bc4SBrooks Davis 89b0d29bc4SBrooks Davis 90b0d29bc4SBrooks Davis# Copies a helper binary from the source directory to the work directory. 91b0d29bc4SBrooks Davis# 92b0d29bc4SBrooks Davis# \param name The name of the binary to copy. 93b0d29bc4SBrooks Davis# \param destination The target location for the binary; can be either 94b0d29bc4SBrooks Davis# a directory name or a file name. 95b0d29bc4SBrooks Davisutils_cp_helper() { 96b0d29bc4SBrooks Davis local name="${1}"; shift 97b0d29bc4SBrooks Davis local destination="${1}"; shift 98b0d29bc4SBrooks Davis 99b0d29bc4SBrooks Davis ln -s "$(atf_get_srcdir)"/helpers/"${name}" "${destination}" 100b0d29bc4SBrooks Davis} 101b0d29bc4SBrooks Davis 102b0d29bc4SBrooks Davis 103b0d29bc4SBrooks Davis# Creates a 'kyua' binary in the path that strips timing data off the output. 104b0d29bc4SBrooks Davis# 105b0d29bc4SBrooks Davis# Call this on test cases that wish to replace timing data in the *stdout* of 106b0d29bc4SBrooks Davis# Kyua with the deterministic strings. This is to be used by tests that 107b0d29bc4SBrooks Davis# validate the 'test' and 'report' subcommands. 108b0d29bc4SBrooks Davisutils_install_times_wrapper() { 109b0d29bc4SBrooks Davis [ ! -x kyua ] || return 110b0d29bc4SBrooks Davis cat >kyua <<EOF 111b0d29bc4SBrooks Davis#! /bin/sh 112b0d29bc4SBrooks Davis 113b0d29bc4SBrooks DavisPATH=${PATH} 114b0d29bc4SBrooks Davis 115b0d29bc4SBrooks Daviskyua "\${@}" >kyua.tmpout 116b0d29bc4SBrooks Davisresult=\${?} 117b0d29bc4SBrooks Daviscat kyua.tmpout | ${utils_strip_times} 118b0d29bc4SBrooks Davisexit \${result} 119b0d29bc4SBrooks DavisEOF 120b0d29bc4SBrooks Davis chmod +x kyua 121b0d29bc4SBrooks Davis PATH="$(pwd):${PATH}" 122b0d29bc4SBrooks Davis} 123b0d29bc4SBrooks Davis 124b0d29bc4SBrooks Davis 125b0d29bc4SBrooks Davis# Creates a 'kyua' binary in the path that makes the output of 'test' stable. 126b0d29bc4SBrooks Davis# 127b0d29bc4SBrooks Davis# Call this on test cases that wish to replace timing data with deterministic 128b0d29bc4SBrooks Davis# strings and that need the result lines in the output to be sorted 129b0d29bc4SBrooks Davis# lexicographically. The latter hides the indeterminism caused by parallel 130b0d29bc4SBrooks Davis# execution so that the output can be verified. For these reasons, this is to 131b0d29bc4SBrooks Davis# be used exclusively by tests that validate the 'test' subcommand. 132b0d29bc4SBrooks Davisutils_install_stable_test_wrapper() { 133b0d29bc4SBrooks Davis [ ! -x kyua ] || return 134b0d29bc4SBrooks Davis cat >kyua <<EOF 135b0d29bc4SBrooks Davis#! /bin/sh 136b0d29bc4SBrooks Davis 137b0d29bc4SBrooks DavisPATH=${PATH} 138b0d29bc4SBrooks Davis 139b0d29bc4SBrooks Daviskyua "\${@}" >kyua.tmpout 140b0d29bc4SBrooks Davisresult=\${?} 141b0d29bc4SBrooks Daviscat kyua.tmpout | ${utils_strip_times} >kyua.tmpout2 142b0d29bc4SBrooks Davis 143b0d29bc4SBrooks Davis# Sort the test result lines but keep the rest intact. 144b0d29bc4SBrooks Davisgrep '[^ ]*:[^ ]*' kyua.tmpout2 | sort >kyua.tmpout3 145b0d29bc4SBrooks Davisgrep -v '[^ ]*:[^ ]*' kyua.tmpout2 >kyua.tmpout4 146b0d29bc4SBrooks Daviscat kyua.tmpout3 kyua.tmpout4 147b0d29bc4SBrooks Davis 148b0d29bc4SBrooks Davisexit \${result} 149b0d29bc4SBrooks DavisEOF 150b0d29bc4SBrooks Davis chmod +x kyua 151b0d29bc4SBrooks Davis PATH="$(pwd):${PATH}" 152b0d29bc4SBrooks Davis} 153b0d29bc4SBrooks Davis 154b0d29bc4SBrooks Davis 155b0d29bc4SBrooks Davis# Defines a test case with a default head. 156b0d29bc4SBrooks Davisutils_test_case() { 157b0d29bc4SBrooks Davis local name="${1}"; shift 158b0d29bc4SBrooks Davis 159b0d29bc4SBrooks Davis atf_test_case "${name}" 160b0d29bc4SBrooks Davis eval "${name}_head() { 161b0d29bc4SBrooks Davis atf_set require.progs kyua 162b0d29bc4SBrooks Davis }" 163b0d29bc4SBrooks Davis} 164b0d29bc4SBrooks Davis 165b0d29bc4SBrooks Davis 166b0d29bc4SBrooks Davis# Computes the test suite identifier for results files files. 167b0d29bc4SBrooks Davis# 168b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd. 169b0d29bc4SBrooks Davisutils_test_suite_id() { 170b0d29bc4SBrooks Davis local path= 171b0d29bc4SBrooks Davis if [ ${#} -gt 0 ]; then 172b0d29bc4SBrooks Davis path="$(cd ${1} && pwd)"; shift 173b0d29bc4SBrooks Davis else 174b0d29bc4SBrooks Davis path="$(pwd)" 175b0d29bc4SBrooks Davis fi 176b0d29bc4SBrooks Davis echo "${path}" | sed -e 's,^/,,' -e 's,/,_,g' 177b0d29bc4SBrooks Davis} 178