xref: /freebsd/contrib/kyua/integration/utils.sh (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis# Copyright 2011 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# Subcommand to strip out the durations and timestamps in a report.
31*b0d29bc4SBrooks Davis#
32*b0d29bc4SBrooks Davis# This is to make the reports deterministic and thus easily testable.  The
33*b0d29bc4SBrooks Davis# time deltas are replaced by the fixed string S.UUU and the timestamps are
34*b0d29bc4SBrooks Davis# replaced by the fixed strings YYYYMMDD.HHMMSS.ssssss and
35*b0d29bc4SBrooks Davis# YYYY-MM-DDTHH:MM:SS.ssssssZ depending on their original format.
36*b0d29bc4SBrooks Davis#
37*b0d29bc4SBrooks Davis# This variable should be used as shown here:
38*b0d29bc4SBrooks Davis#
39*b0d29bc4SBrooks Davis#     atf_check ... -x kyua report "| ${utils_strip_times}"
40*b0d29bc4SBrooks Davis#
41*b0d29bc4SBrooks Davis# Use the utils_install_times_wrapper function to create a 'kyua' wrapper
42*b0d29bc4SBrooks Davis# script that automatically does this.
43*b0d29bc4SBrooks Davis# CHECK_STYLE_DISABLE
44*b0d29bc4SBrooks Davisutils_strip_times='sed -E \
45*b0d29bc4SBrooks Davis    -e "s,( |\[|\")[0-9][0-9]*.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
46*b0d29bc4SBrooks Davis    -e "s,[0-9]{8}-[0-9]{6}-[0-9]{6},YYYYMMDD-HHMMSS-ssssss,g" \
47*b0d29bc4SBrooks 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"'
48*b0d29bc4SBrooks Davis# CHECK_STYLE_ENABLE
49*b0d29bc4SBrooks Davis
50*b0d29bc4SBrooks Davis
51*b0d29bc4SBrooks Davis# Same as utils_strip_times but avoids stripping timestamp-based report IDs.
52*b0d29bc4SBrooks Davis#
53*b0d29bc4SBrooks Davis# This is to make the reports deterministic and thus easily testable.  The
54*b0d29bc4SBrooks Davis# time deltas are replaced by the fixed string S.UUU and the timestamps are
55*b0d29bc4SBrooks Davis# replaced by the fixed string YYYY-MM-DDTHH:MM:SS.ssssssZ.
56*b0d29bc4SBrooks Davis# CHECK_STYLE_DISABLE
57*b0d29bc4SBrooks Davisutils_strip_times_but_not_ids='sed -E \
58*b0d29bc4SBrooks Davis    -e "s,( |\[|\")[0-9][0-9]*.[0-9][0-9][0-9](s]|s|\"),\1S.UUU\2,g" \
59*b0d29bc4SBrooks 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"'
60*b0d29bc4SBrooks Davis# CHECK_STYLE_ENABLE
61*b0d29bc4SBrooks Davis
62*b0d29bc4SBrooks Davis
63*b0d29bc4SBrooks Davis# Computes the results id for a test suite run.
64*b0d29bc4SBrooks Davis#
65*b0d29bc4SBrooks Davis# The computed path is "generic" in the sense that it does not include a
66*b0d29bc4SBrooks Davis# real timestamp: it only includes a placeholder.  This function should be
67*b0d29bc4SBrooks Davis# used along the utils_strip_times function so that the timestamps of
68*b0d29bc4SBrooks Davis# the real results files are stripped out.
69*b0d29bc4SBrooks Davis#
70*b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd.
71*b0d29bc4SBrooks Davisutils_results_id() {
72*b0d29bc4SBrooks Davis    local test_suite_id="$(utils_test_suite_id "${@}")"
73*b0d29bc4SBrooks Davis    echo "${test_suite_id}.YYYYMMDD-HHMMSS-ssssss"
74*b0d29bc4SBrooks Davis}
75*b0d29bc4SBrooks Davis
76*b0d29bc4SBrooks Davis
77*b0d29bc4SBrooks Davis# Computes the results file for a test suite run.
78*b0d29bc4SBrooks Davis#
79*b0d29bc4SBrooks Davis# The computed path is "generic" in the sense that it does not include a
80*b0d29bc4SBrooks Davis# real timestamp: it only includes a placeholder.  This function should be
81*b0d29bc4SBrooks Davis# used along the utils_strip_times function so that the timestampts of the
82*b0d29bc4SBrooks Davis# real results files are stripped out.
83*b0d29bc4SBrooks Davis#
84*b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd.
85*b0d29bc4SBrooks Davisutils_results_file() {
86*b0d29bc4SBrooks Davis    echo "${HOME}/.kyua/store/results.$(utils_results_id "${@}").db"
87*b0d29bc4SBrooks Davis}
88*b0d29bc4SBrooks Davis
89*b0d29bc4SBrooks Davis
90*b0d29bc4SBrooks Davis# Copies a helper binary from the source directory to the work directory.
91*b0d29bc4SBrooks Davis#
92*b0d29bc4SBrooks Davis# \param name The name of the binary to copy.
93*b0d29bc4SBrooks Davis# \param destination The target location for the binary; can be either
94*b0d29bc4SBrooks Davis#     a directory name or a file name.
95*b0d29bc4SBrooks Davisutils_cp_helper() {
96*b0d29bc4SBrooks Davis    local name="${1}"; shift
97*b0d29bc4SBrooks Davis    local destination="${1}"; shift
98*b0d29bc4SBrooks Davis
99*b0d29bc4SBrooks Davis    ln -s "$(atf_get_srcdir)"/helpers/"${name}" "${destination}"
100*b0d29bc4SBrooks Davis}
101*b0d29bc4SBrooks Davis
102*b0d29bc4SBrooks Davis
103*b0d29bc4SBrooks Davis# Creates a 'kyua' binary in the path that strips timing data off the output.
104*b0d29bc4SBrooks Davis#
105*b0d29bc4SBrooks Davis# Call this on test cases that wish to replace timing data in the *stdout* of
106*b0d29bc4SBrooks Davis# Kyua with the deterministic strings.  This is to be used by tests that
107*b0d29bc4SBrooks Davis# validate the 'test' and 'report' subcommands.
108*b0d29bc4SBrooks Davisutils_install_times_wrapper() {
109*b0d29bc4SBrooks Davis    [ ! -x kyua ] || return
110*b0d29bc4SBrooks Davis    cat >kyua <<EOF
111*b0d29bc4SBrooks Davis#! /bin/sh
112*b0d29bc4SBrooks Davis
113*b0d29bc4SBrooks DavisPATH=${PATH}
114*b0d29bc4SBrooks Davis
115*b0d29bc4SBrooks Daviskyua "\${@}" >kyua.tmpout
116*b0d29bc4SBrooks Davisresult=\${?}
117*b0d29bc4SBrooks Daviscat kyua.tmpout | ${utils_strip_times}
118*b0d29bc4SBrooks Davisexit \${result}
119*b0d29bc4SBrooks DavisEOF
120*b0d29bc4SBrooks Davis    chmod +x kyua
121*b0d29bc4SBrooks Davis    PATH="$(pwd):${PATH}"
122*b0d29bc4SBrooks Davis}
123*b0d29bc4SBrooks Davis
124*b0d29bc4SBrooks Davis
125*b0d29bc4SBrooks Davis# Creates a 'kyua' binary in the path that makes the output of 'test' stable.
126*b0d29bc4SBrooks Davis#
127*b0d29bc4SBrooks Davis# Call this on test cases that wish to replace timing data with deterministic
128*b0d29bc4SBrooks Davis# strings and that need the result lines in the output to be sorted
129*b0d29bc4SBrooks Davis# lexicographically.  The latter hides the indeterminism caused by parallel
130*b0d29bc4SBrooks Davis# execution so that the output can be verified.  For these reasons, this is to
131*b0d29bc4SBrooks Davis# be used exclusively by tests that validate the 'test' subcommand.
132*b0d29bc4SBrooks Davisutils_install_stable_test_wrapper() {
133*b0d29bc4SBrooks Davis    [ ! -x kyua ] || return
134*b0d29bc4SBrooks Davis    cat >kyua <<EOF
135*b0d29bc4SBrooks Davis#! /bin/sh
136*b0d29bc4SBrooks Davis
137*b0d29bc4SBrooks DavisPATH=${PATH}
138*b0d29bc4SBrooks Davis
139*b0d29bc4SBrooks Daviskyua "\${@}" >kyua.tmpout
140*b0d29bc4SBrooks Davisresult=\${?}
141*b0d29bc4SBrooks Daviscat kyua.tmpout | ${utils_strip_times} >kyua.tmpout2
142*b0d29bc4SBrooks Davis
143*b0d29bc4SBrooks Davis# Sort the test result lines but keep the rest intact.
144*b0d29bc4SBrooks Davisgrep '[^ ]*:[^ ]*' kyua.tmpout2 | sort >kyua.tmpout3
145*b0d29bc4SBrooks Davisgrep -v '[^ ]*:[^ ]*' kyua.tmpout2 >kyua.tmpout4
146*b0d29bc4SBrooks Daviscat kyua.tmpout3 kyua.tmpout4
147*b0d29bc4SBrooks Davis
148*b0d29bc4SBrooks Davisexit \${result}
149*b0d29bc4SBrooks DavisEOF
150*b0d29bc4SBrooks Davis    chmod +x kyua
151*b0d29bc4SBrooks Davis    PATH="$(pwd):${PATH}"
152*b0d29bc4SBrooks Davis}
153*b0d29bc4SBrooks Davis
154*b0d29bc4SBrooks Davis
155*b0d29bc4SBrooks Davis# Defines a test case with a default head.
156*b0d29bc4SBrooks Davisutils_test_case() {
157*b0d29bc4SBrooks Davis    local name="${1}"; shift
158*b0d29bc4SBrooks Davis
159*b0d29bc4SBrooks Davis    atf_test_case "${name}"
160*b0d29bc4SBrooks Davis    eval "${name}_head() {
161*b0d29bc4SBrooks Davis        atf_set require.progs kyua
162*b0d29bc4SBrooks Davis    }"
163*b0d29bc4SBrooks Davis}
164*b0d29bc4SBrooks Davis
165*b0d29bc4SBrooks Davis
166*b0d29bc4SBrooks Davis# Computes the test suite identifier for results files files.
167*b0d29bc4SBrooks Davis#
168*b0d29bc4SBrooks Davis# \param path Optional path to use; if not given, use the cwd.
169*b0d29bc4SBrooks Davisutils_test_suite_id() {
170*b0d29bc4SBrooks Davis    local path=
171*b0d29bc4SBrooks Davis    if [ ${#} -gt 0 ]; then
172*b0d29bc4SBrooks Davis        path="$(cd ${1} && pwd)"; shift
173*b0d29bc4SBrooks Davis    else
174*b0d29bc4SBrooks Davis        path="$(pwd)"
175*b0d29bc4SBrooks Davis    fi
176*b0d29bc4SBrooks Davis    echo "${path}" | sed -e 's,^/,,' -e 's,/,_,g'
177*b0d29bc4SBrooks Davis}
178