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