4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
26 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29 . ${STF_TOOLS}/include/stf.shlib
37 _printline ASSERTION: "$@"
49 # Execute and print command with status where success equals non-zero result
51 # $@ - command to execute
53 # return 0 if command fails, otherwise return 1
57 log_neg_expect "" "$@"
61 # Execute a positive test and exit $STF_FAIL is test fails
63 # $@ - command to execute
68 (( $? != 0 )) && log_fail
71 # Execute a positive test but retry the command on failure if the output
72 # matches an expected pattern. Otherwise behave like log_must and exit
73 # $STF_FAIL is test fails.
77 # $3-$@ - command to execute
79 function log_must_retry
82 typeset logfile="/tmp/log.$$"
89 while [[ -e $logfile ]]; do
93 while (( $retry > 0 )); do
98 if (( $status == 0 )); then
99 $out | egrep -i "internal error|assertion failed" \
101 # internal error or assertion failed
102 if [[ $? -eq 0 ]]; then
104 _printerror "$@" "internal error or" \
105 " assertion failure exited $status"
108 [[ -n $LOGAPI_DEBUG ]] && print $($out)
113 $out | grep -i "$expect" > /dev/null 2>&1
114 if (( $? == 0 )); then
116 _printerror "$@" "Retry in $delay seconds"
119 (( retry=retry - 1 ))
120 (( delay=delay * 2 ))
127 if (( $status != 0 )) ; then
129 _printerror "$@" "exited $status"
132 _recursive_output $logfile "false"
136 # Execute a positive test and exit $STF_FAIL is test fails after being
137 # retried up to 5 times when the command returns the keyword "busy".
139 # $@ - command to execute
140 function log_must_busy
142 log_must_retry "busy" 5 "$@"
143 (( $? != 0 )) && log_fail
146 # Execute a negative test and exit $STF_FAIL if test passes
148 # $@ - command to execute
153 (( $? != 0 )) && log_fail
156 # Execute a negative test with keyword expected, and exit
157 # $STF_FAIL if test passes
159 # $1 - keyword expected
160 # $2-$@ - command to execute
162 function log_mustnot_expect
165 (( $? != 0 )) && log_fail
168 # Execute and print command with status where success equals non-zero result
169 # or output includes expected keyword
171 # $1 - keyword expected
172 # $2-$@ - command to execute
174 # return 0 if command fails, or the output contains the keyword expected,
177 function log_neg_expect
180 typeset logfile="/tmp/log.$$"
185 while [[ -e $logfile ]]; do
186 logfile="$logfile.$$"
194 if (( $status == 0 )); then
196 _printerror "$@" "unexpectedly exited $status"
198 elif (( $status == 127 )); then
200 _printerror "$@" "unexpectedly exited $status (File not found)"
201 # bus error - core dump
202 elif (( $status == 138 )); then
204 _printerror "$@" "unexpectedly exited $status (Bus Error)"
205 # segmentation violation - core dump
206 elif (( $status == 139 )); then
208 _printerror "$@" "unexpectedly exited $status (SEGV)"
210 $out | egrep -i "internal error|assertion failed" \
212 # internal error or assertion failed
213 if (( $? == 0 )); then
215 _printerror "$@" "internal error or assertion failure" \
217 elif [[ -n $expect ]] ; then
218 $out | grep -i "$expect" > /dev/null 2>&1
219 if (( $? == 0 )); then
223 _printerror "$@" "unexpectedly exited $status"
229 if (( $ret == 0 )); then
230 [[ -n $LOGAPI_DEBUG ]] && print $($out)
231 _printsuccess "$@" "exited $status"
234 _recursive_output $logfile "false"
238 # Execute and print command with status where success equals zero result
240 # $@ command to execute
242 # return command exit status
247 typeset logfile="/tmp/log.$$"
249 while [[ -e $logfile ]]; do
250 logfile="$logfile.$$"
257 if (( $status != 0 )) ; then
259 _printerror "$@" "exited $status"
261 $out | egrep -i "internal error|assertion failed" \
263 # internal error or assertion failed
264 if [[ $? -eq 0 ]]; then
266 _printerror "$@" "internal error or assertion failure" \
270 [[ -n $LOGAPI_DEBUG ]] && print $($out)
274 _recursive_output $logfile "false"
278 # Set an exit handler
280 # $@ - function(s) to perform on exit
291 # Perform cleanup and exit $STF_PASS
297 _endlog $STF_PASS "$@"
300 # Perform cleanup and exit $STF_FAIL
306 _endlog $STF_FAIL "$@"
309 # Perform cleanup and exit $STF_UNRESOLVED
313 function log_unresolved
315 _endlog $STF_UNRESOLVED "$@"
318 # Perform cleanup and exit $STF_NOTINUSE
322 function log_notinuse
324 _endlog $STF_NOTINUSE "$@"
327 # Perform cleanup and exit $STF_UNSUPPORTED
331 function log_unsupported
333 _endlog $STF_UNSUPPORTED "$@"
336 # Perform cleanup and exit $STF_UNTESTED
340 function log_untested
342 _endlog $STF_UNTESTED "$@"
345 # Perform cleanup and exit $STF_UNINITIATED
349 function log_uninitiated
351 _endlog $STF_UNINITIATED "$@"
354 # Perform cleanup and exit $STF_NORESULT
358 function log_noresult
360 _endlog $STF_NORESULT "$@"
363 # Perform cleanup and exit $STF_WARNING
369 _endlog $STF_WARNING "$@"
372 # Perform cleanup and exit $STF_TIMED_OUT
376 function log_timed_out
378 _endlog $STF_TIMED_OUT "$@"
381 # Perform cleanup and exit $STF_OTHER
387 _endlog $STF_OTHER "$@"
394 # Execute custom callback scripts on test failure
396 # callback script paths are stored in TESTFAIL_CALLBACKS, delimited by ':'.
398 function _execute_testfail_callbacks
402 print "$TESTFAIL_CALLBACKS:" | while read -d ":" callback; do
403 if [[ -n "$callback" ]] ; then
404 log_note "Performing test-fail callback ($callback)"
410 # Perform cleanup and exit
413 # $2-$n - message text
417 typeset logfile="/tmp/log.$$"
418 _recursive_output $logfile
422 (( ${#@} > 0 )) && _printline "$@"
424 if [[ $exitcode == $STF_FAIL ]] ; then
425 _execute_testfail_callbacks
428 if [[ -n $_CLEANUP ]] ; then
429 typeset cleanup=$_CLEANUP
431 log_note "Performing local cleanup via log_onexit ($cleanup)"
438 # Output a formatted line
447 # Output an error message
453 _printline ERROR: "$@"
456 # Output a success message
460 function _printsuccess
462 _printline SUCCESS: "$@"
465 # Output logfiles recursively
468 # $2 - indicate whether output the start file itself, default as yes.
470 function _recursive_output #logfile
474 while [[ -e $logfile ]]; do
475 if [[ -z $2 || $logfile != $1 ]]; then
479 logfile="$logfile.$$"