2015-07-02 01:23:09 +03:00
|
|
|
#
|
|
|
|
# CDDL HEADER START
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the terms of the
|
|
|
|
# Common Development and Distribution License (the "License").
|
|
|
|
# You may not use this file except in compliance with the License.
|
|
|
|
#
|
|
|
|
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-12 00:16:13 +03:00
|
|
|
# or https://opensource.org/licenses/CDDL-1.0.
|
2015-07-02 01:23:09 +03:00
|
|
|
# See the License for the specific language governing permissions
|
|
|
|
# and limitations under the License.
|
|
|
|
#
|
|
|
|
# When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
# If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
# fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
# information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
#
|
|
|
|
# CDDL HEADER END
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Copyright 2007 Sun Microsystems, Inc. All rights reserved.
|
|
|
|
# Use is subject to license terms.
|
|
|
|
#
|
2020-07-13 19:19:18 +03:00
|
|
|
# Copyright (c) 2012, 2020 by Delphix. All rights reserved.
|
2015-07-02 01:23:09 +03:00
|
|
|
#
|
|
|
|
|
2022-03-24 15:15:00 +03:00
|
|
|
STF_PASS=0
|
|
|
|
STF_FAIL=1
|
|
|
|
STF_UNRESOLVED=2
|
|
|
|
STF_UNSUPPORTED=4
|
|
|
|
STF_UNTESTED=5
|
2015-07-02 01:23:09 +03:00
|
|
|
|
|
|
|
# Output an assertion
|
|
|
|
#
|
|
|
|
# $@ - assertion text
|
|
|
|
|
|
|
|
function log_assert
|
|
|
|
{
|
|
|
|
_printline ASSERTION: "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output a comment
|
|
|
|
#
|
|
|
|
# $@ - comment text
|
|
|
|
|
|
|
|
function log_note
|
|
|
|
{
|
|
|
|
_printline NOTE: "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute and print command with status where success equals non-zero result
|
|
|
|
#
|
|
|
|
# $@ - command to execute
|
|
|
|
#
|
|
|
|
# return 0 if command fails, otherwise return 1
|
|
|
|
|
|
|
|
function log_neg
|
|
|
|
{
|
2022-03-12 19:52:38 +03:00
|
|
|
log_neg_expect "" "$@"
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Execute a positive test and exit $STF_FAIL is test fails
|
|
|
|
#
|
|
|
|
# $@ - command to execute
|
|
|
|
|
|
|
|
function log_must
|
|
|
|
{
|
2022-02-23 04:44:53 +03:00
|
|
|
log_pos "$@" || log_fail
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 02:07:19 +03:00
|
|
|
# Execute a positive test (expecting no stderr) and exit $STF_FAIL
|
|
|
|
# if test fails
|
|
|
|
# $@ - command to execute
|
|
|
|
|
|
|
|
function log_must_nostderr
|
|
|
|
{
|
2022-02-23 04:44:53 +03:00
|
|
|
log_pos_nostderr "$@" || log_fail
|
2021-10-21 02:07:19 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:28:51 +03:00
|
|
|
# Execute a positive test but retry the command on failure if the output
|
|
|
|
# matches an expected pattern. Otherwise behave like log_must and exit
|
|
|
|
# $STF_FAIL is test fails.
|
|
|
|
#
|
|
|
|
# $1 - retry keyword
|
|
|
|
# $2 - retry attempts
|
|
|
|
# $3-$@ - command to execute
|
|
|
|
#
|
|
|
|
function log_must_retry
|
|
|
|
{
|
|
|
|
typeset logfile="/tmp/log.$$"
|
|
|
|
typeset status=1
|
|
|
|
typeset expect=$1
|
|
|
|
typeset retry=$2
|
|
|
|
typeset delay=1
|
|
|
|
shift 2
|
|
|
|
|
|
|
|
while [[ -e $logfile ]]; do
|
|
|
|
logfile="$logfile.$$"
|
|
|
|
done
|
|
|
|
|
|
|
|
while (( $retry > 0 )); do
|
|
|
|
"$@" 2>$logfile
|
|
|
|
status=$?
|
|
|
|
|
|
|
|
if (( $status == 0 )); then
|
2022-03-12 19:52:38 +03:00
|
|
|
if grep -qEi "internal error|assertion failed" $logfile; then
|
|
|
|
cat $logfile >&2
|
2016-06-21 00:28:51 +03:00
|
|
|
_printerror "$@" "internal error or" \
|
|
|
|
" assertion failure exited $status"
|
|
|
|
status=1
|
|
|
|
else
|
2020-10-11 23:08:56 +03:00
|
|
|
[[ -n $LOGAPI_DEBUG ]] && cat $logfile
|
2016-06-21 00:28:51 +03:00
|
|
|
_printsuccess "$@"
|
|
|
|
fi
|
|
|
|
break
|
|
|
|
else
|
2022-03-12 19:52:38 +03:00
|
|
|
if grep -qi "$expect" $logfile; then
|
|
|
|
cat $logfile >&2
|
2016-06-21 00:28:51 +03:00
|
|
|
_printerror "$@" "Retry in $delay seconds"
|
2017-04-06 03:18:22 +03:00
|
|
|
sleep $delay
|
2016-06-21 00:28:51 +03:00
|
|
|
|
|
|
|
(( retry=retry - 1 ))
|
|
|
|
(( delay=delay * 2 ))
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if (( $status != 0 )) ; then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2016-06-21 00:28:51 +03:00
|
|
|
_printerror "$@" "exited $status"
|
|
|
|
fi
|
|
|
|
|
|
|
|
_recursive_output $logfile "false"
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute a positive test and exit $STF_FAIL is test fails after being
|
|
|
|
# retried up to 5 times when the command returns the keyword "busy".
|
|
|
|
#
|
|
|
|
# $@ - command to execute
|
|
|
|
function log_must_busy
|
|
|
|
{
|
2022-02-23 04:44:53 +03:00
|
|
|
log_must_retry "busy" 5 "$@" || log_fail
|
2016-06-21 00:28:51 +03:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
# Execute a negative test and exit $STF_FAIL if test passes
|
|
|
|
#
|
|
|
|
# $@ - command to execute
|
|
|
|
|
|
|
|
function log_mustnot
|
|
|
|
{
|
2022-02-23 04:44:53 +03:00
|
|
|
log_neg "$@" || log_fail
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Execute a negative test with keyword expected, and exit
|
|
|
|
# $STF_FAIL if test passes
|
|
|
|
#
|
|
|
|
# $1 - keyword expected
|
|
|
|
# $2-$@ - command to execute
|
|
|
|
|
|
|
|
function log_mustnot_expect
|
|
|
|
{
|
2022-02-23 04:44:53 +03:00
|
|
|
log_neg_expect "$@" || log_fail
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
2020-03-17 19:49:58 +03:00
|
|
|
# Signal numbers are platform-dependent
|
2020-03-12 20:50:51 +03:00
|
|
|
case $(uname) in
|
|
|
|
Darwin|FreeBSD)
|
|
|
|
SIGBUS=10
|
|
|
|
SIGSEGV=11
|
|
|
|
;;
|
2020-03-17 19:49:58 +03:00
|
|
|
illumos|Linux|*)
|
2020-03-12 20:50:51 +03:00
|
|
|
SIGBUS=7
|
|
|
|
SIGSEGV=11
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
EXIT_SUCCESS=0
|
|
|
|
EXIT_NOTFOUND=127
|
2020-03-17 19:49:58 +03:00
|
|
|
EXIT_SIGNAL=256
|
2020-03-12 20:50:51 +03:00
|
|
|
EXIT_SIGBUS=$((EXIT_SIGNAL + SIGBUS))
|
|
|
|
EXIT_SIGSEGV=$((EXIT_SIGNAL + SIGSEGV))
|
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
# Execute and print command with status where success equals non-zero result
|
|
|
|
# or output includes expected keyword
|
|
|
|
#
|
|
|
|
# $1 - keyword expected
|
|
|
|
# $2-$@ - command to execute
|
|
|
|
#
|
|
|
|
# return 0 if command fails, or the output contains the keyword expected,
|
|
|
|
# return 1 otherwise
|
|
|
|
|
|
|
|
function log_neg_expect
|
|
|
|
{
|
|
|
|
typeset logfile="/tmp/log.$$"
|
|
|
|
typeset ret=1
|
|
|
|
typeset expect=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
while [[ -e $logfile ]]; do
|
|
|
|
logfile="$logfile.$$"
|
|
|
|
done
|
|
|
|
|
|
|
|
"$@" 2>$logfile
|
|
|
|
typeset status=$?
|
|
|
|
|
|
|
|
# unexpected status
|
2020-03-12 20:50:51 +03:00
|
|
|
if (( $status == EXIT_SUCCESS )); then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "unexpectedly exited $status"
|
|
|
|
# missing binary
|
2020-03-12 20:50:51 +03:00
|
|
|
elif (( $status == EXIT_NOTFOUND )); then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "unexpectedly exited $status (File not found)"
|
2020-03-12 20:50:51 +03:00
|
|
|
# bus error - core dump
|
|
|
|
elif (( $status == EXIT_SIGBUS )); then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "unexpectedly exited $status (Bus Error)"
|
2020-03-12 20:50:51 +03:00
|
|
|
# segmentation violation - core dump
|
|
|
|
elif (( $status == EXIT_SIGSEGV )); then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "unexpectedly exited $status (SEGV)"
|
|
|
|
else
|
2022-03-12 19:52:38 +03:00
|
|
|
if grep -qEi "internal error|assertion failed" $logfile; then
|
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "internal error or assertion failure" \
|
|
|
|
" exited $status"
|
|
|
|
elif [[ -n $expect ]] ; then
|
2022-03-12 19:52:38 +03:00
|
|
|
if grep -qi "$expect" $logfile; then
|
2015-07-02 01:23:09 +03:00
|
|
|
ret=0
|
|
|
|
else
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "unexpectedly exited $status"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
ret=0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if (( $ret == 0 )); then
|
2020-10-11 23:08:56 +03:00
|
|
|
[[ -n $LOGAPI_DEBUG ]] && cat $logfile
|
2015-07-02 01:23:09 +03:00
|
|
|
_printsuccess "$@" "exited $status"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
_recursive_output $logfile "false"
|
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute and print command with status where success equals zero result
|
|
|
|
#
|
|
|
|
# $@ command to execute
|
|
|
|
#
|
|
|
|
# return command exit status
|
|
|
|
|
|
|
|
function log_pos
|
|
|
|
{
|
|
|
|
typeset logfile="/tmp/log.$$"
|
|
|
|
|
|
|
|
while [[ -e $logfile ]]; do
|
|
|
|
logfile="$logfile.$$"
|
|
|
|
done
|
|
|
|
|
|
|
|
"$@" 2>$logfile
|
|
|
|
typeset status=$?
|
|
|
|
|
|
|
|
if (( $status != 0 )) ; then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "exited $status"
|
|
|
|
else
|
2022-03-12 19:52:38 +03:00
|
|
|
if grep -qEi "internal error|assertion failed" $logfile; then
|
|
|
|
cat $logfile >&2
|
2015-07-02 01:23:09 +03:00
|
|
|
_printerror "$@" "internal error or assertion failure" \
|
|
|
|
" exited $status"
|
|
|
|
status=1
|
|
|
|
else
|
2020-10-11 23:08:56 +03:00
|
|
|
[[ -n $LOGAPI_DEBUG ]] && cat $logfile
|
2015-07-02 01:23:09 +03:00
|
|
|
_printsuccess "$@"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
_recursive_output $logfile "false"
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
2021-10-21 02:07:19 +03:00
|
|
|
# Execute and print command with status where success equals zero result
|
|
|
|
# and no stderr output
|
|
|
|
#
|
|
|
|
# $@ command to execute
|
|
|
|
#
|
|
|
|
# return 0 if command succeeds and no stderr output
|
|
|
|
# return 1 othersie
|
|
|
|
|
|
|
|
function log_pos_nostderr
|
|
|
|
{
|
|
|
|
typeset logfile="/tmp/log.$$"
|
|
|
|
|
|
|
|
while [[ -e $logfile ]]; do
|
|
|
|
logfile="$logfile.$$"
|
|
|
|
done
|
|
|
|
|
|
|
|
"$@" 2>$logfile
|
|
|
|
typeset status=$?
|
|
|
|
|
|
|
|
if (( $status != 0 )) ; then
|
2022-03-12 19:52:38 +03:00
|
|
|
cat $logfile >&2
|
2021-10-21 02:07:19 +03:00
|
|
|
_printerror "$@" "exited $status"
|
|
|
|
else
|
2022-03-12 19:52:38 +03:00
|
|
|
if [ -s "$logfile" ]; then
|
|
|
|
cat $logfile >&2
|
2021-10-21 02:07:19 +03:00
|
|
|
_printerror "$@" "message in stderr" \
|
|
|
|
" exited $status"
|
|
|
|
status=1
|
|
|
|
else
|
|
|
|
[[ -n $LOGAPI_DEBUG ]] && cat $logfile
|
|
|
|
_printsuccess "$@"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
_recursive_output $logfile "false"
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
# Set an exit handler
|
|
|
|
#
|
|
|
|
# $@ - function(s) to perform on exit
|
|
|
|
|
|
|
|
function log_onexit
|
|
|
|
{
|
2020-03-03 21:28:09 +03:00
|
|
|
_CLEANUP=("$*")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Push an exit handler on the cleanup stack
|
|
|
|
#
|
|
|
|
# $@ - function(s) to perform on exit
|
|
|
|
|
|
|
|
function log_onexit_push
|
|
|
|
{
|
|
|
|
_CLEANUP+=("$*")
|
|
|
|
}
|
|
|
|
|
|
|
|
# Pop an exit handler off the cleanup stack
|
|
|
|
|
|
|
|
function log_onexit_pop
|
|
|
|
{
|
|
|
|
_CLEANUP=("${_CLEANUP[@]:0:${#_CLEANUP[@]}-1}")
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Exit functions
|
|
|
|
#
|
|
|
|
|
|
|
|
# Perform cleanup and exit $STF_PASS
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function log_pass
|
|
|
|
{
|
|
|
|
_endlog $STF_PASS "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform cleanup and exit $STF_FAIL
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function log_fail
|
|
|
|
{
|
|
|
|
_endlog $STF_FAIL "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform cleanup and exit $STF_UNRESOLVED
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function log_unresolved
|
|
|
|
{
|
|
|
|
_endlog $STF_UNRESOLVED "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform cleanup and exit $STF_UNSUPPORTED
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function log_unsupported
|
|
|
|
{
|
|
|
|
_endlog $STF_UNSUPPORTED "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Perform cleanup and exit $STF_UNTESTED
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function log_untested
|
|
|
|
{
|
|
|
|
_endlog $STF_UNTESTED "$@"
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:19:18 +03:00
|
|
|
function set_main_pid
|
|
|
|
{
|
|
|
|
_MAINPID=$1
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
#
|
|
|
|
# Internal functions
|
|
|
|
#
|
|
|
|
|
2017-04-12 23:36:48 +03:00
|
|
|
# Execute custom callback scripts on test failure
|
|
|
|
#
|
|
|
|
# callback script paths are stored in TESTFAIL_CALLBACKS, delimited by ':'.
|
|
|
|
|
|
|
|
function _execute_testfail_callbacks
|
|
|
|
{
|
|
|
|
typeset callback
|
|
|
|
|
2022-03-12 19:52:38 +03:00
|
|
|
while read -d ":" callback; do
|
2017-04-12 23:36:48 +03:00
|
|
|
if [[ -n "$callback" ]] ; then
|
|
|
|
log_note "Performing test-fail callback ($callback)"
|
|
|
|
$callback
|
|
|
|
fi
|
2022-03-12 19:52:38 +03:00
|
|
|
done <<<"$TESTFAIL_CALLBACKS:"
|
2017-04-12 23:36:48 +03:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
# Perform cleanup and exit
|
|
|
|
#
|
|
|
|
# $1 - stf exit code
|
|
|
|
# $2-$n - message text
|
|
|
|
|
|
|
|
function _endlog
|
|
|
|
{
|
|
|
|
typeset logfile="/tmp/log.$$"
|
|
|
|
_recursive_output $logfile
|
|
|
|
|
2017-06-30 21:12:29 +03:00
|
|
|
typeset exitcode=$1
|
|
|
|
shift
|
|
|
|
(( ${#@} > 0 )) && _printline "$@"
|
|
|
|
|
2020-07-13 19:19:18 +03:00
|
|
|
#
|
|
|
|
# If we're running in a subshell then just exit and let
|
|
|
|
# the parent handle the failures
|
|
|
|
#
|
|
|
|
if [[ -n "$_MAINPID" && $$ != "$_MAINPID" ]]; then
|
|
|
|
log_note "subshell exited: "$_MAINPID
|
|
|
|
exit $exitcode
|
|
|
|
fi
|
|
|
|
|
2017-06-30 21:12:29 +03:00
|
|
|
if [[ $exitcode == $STF_FAIL ]] ; then
|
2017-04-12 23:36:48 +03:00
|
|
|
_execute_testfail_callbacks
|
|
|
|
fi
|
|
|
|
|
2020-03-03 21:28:09 +03:00
|
|
|
typeset stack=("${_CLEANUP[@]}")
|
|
|
|
log_onexit ""
|
|
|
|
typeset i=${#stack[@]}
|
|
|
|
while (( i-- )); do
|
|
|
|
typeset cleanup="${stack[i]}"
|
2015-07-02 01:23:09 +03:00
|
|
|
log_note "Performing local cleanup via log_onexit ($cleanup)"
|
|
|
|
$cleanup
|
2020-03-03 21:28:09 +03:00
|
|
|
done
|
2017-06-30 21:12:29 +03:00
|
|
|
|
2015-07-02 01:23:09 +03:00
|
|
|
exit $exitcode
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output a formatted line
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function _printline
|
|
|
|
{
|
2022-03-12 19:52:38 +03:00
|
|
|
echo "$@"
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Output an error message
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function _printerror
|
|
|
|
{
|
|
|
|
_printline ERROR: "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output a success message
|
|
|
|
#
|
|
|
|
# $@ - message text
|
|
|
|
|
|
|
|
function _printsuccess
|
|
|
|
{
|
|
|
|
_printline SUCCESS: "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output logfiles recursively
|
|
|
|
#
|
|
|
|
# $1 - start file
|
|
|
|
# $2 - indicate whether output the start file itself, default as yes.
|
|
|
|
|
|
|
|
function _recursive_output #logfile
|
|
|
|
{
|
|
|
|
typeset logfile=$1
|
|
|
|
|
|
|
|
while [[ -e $logfile ]]; do
|
|
|
|
if [[ -z $2 || $logfile != $1 ]]; then
|
2017-04-06 03:18:22 +03:00
|
|
|
cat $logfile
|
2015-07-02 01:23:09 +03:00
|
|
|
fi
|
2017-04-06 03:18:22 +03:00
|
|
|
rm -f $logfile
|
2015-07-02 01:23:09 +03:00
|
|
|
logfile="$logfile.$$"
|
2022-02-23 04:44:53 +03:00
|
|
|
done
|
2015-07-02 01:23:09 +03:00
|
|
|
}
|