xref: /freebsd/sys/contrib/openzfs/tests/test-runner/include/logapi.shlib (revision 184c1b943937986c81e1996d999d21626ec7a4ff)
1eda14cbcSMatt Macy#
2eda14cbcSMatt Macy# CDDL HEADER START
3eda14cbcSMatt Macy#
4eda14cbcSMatt Macy# The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy# Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy# You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy#
8eda14cbcSMatt Macy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy# or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy# See the License for the specific language governing permissions
11eda14cbcSMatt Macy# and limitations under the License.
12eda14cbcSMatt Macy#
13eda14cbcSMatt Macy# When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy# If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy# fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy# information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy#
19eda14cbcSMatt Macy# CDDL HEADER END
20eda14cbcSMatt Macy#
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy#
23eda14cbcSMatt Macy# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy# Use is subject to license terms.
25eda14cbcSMatt Macy#
26eda14cbcSMatt Macy# Copyright (c) 2012, 2020 by Delphix. All rights reserved.
27eda14cbcSMatt Macy#
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy. ${STF_TOOLS}/include/stf.shlib
30eda14cbcSMatt Macy
31eda14cbcSMatt Macy# Output an assertion
32eda14cbcSMatt Macy#
33eda14cbcSMatt Macy# $@ - assertion text
34eda14cbcSMatt Macy
35eda14cbcSMatt Macyfunction log_assert
36eda14cbcSMatt Macy{
37eda14cbcSMatt Macy	_printline ASSERTION: "$@"
38eda14cbcSMatt Macy}
39eda14cbcSMatt Macy
40eda14cbcSMatt Macy# Output a comment
41eda14cbcSMatt Macy#
42eda14cbcSMatt Macy# $@ - comment text
43eda14cbcSMatt Macy
44eda14cbcSMatt Macyfunction log_note
45eda14cbcSMatt Macy{
46eda14cbcSMatt Macy	_printline NOTE: "$@"
47eda14cbcSMatt Macy}
48eda14cbcSMatt Macy
49eda14cbcSMatt Macy# Execute and print command with status where success equals non-zero result
50eda14cbcSMatt Macy#
51eda14cbcSMatt Macy# $@ - command to execute
52eda14cbcSMatt Macy#
53eda14cbcSMatt Macy# return 0 if command fails, otherwise return 1
54eda14cbcSMatt Macy
55eda14cbcSMatt Macyfunction log_neg
56eda14cbcSMatt Macy{
57eda14cbcSMatt Macy	log_neg_expect ""  "$@"
58eda14cbcSMatt Macy	return $?
59eda14cbcSMatt Macy}
60eda14cbcSMatt Macy
61eda14cbcSMatt Macy# Execute a positive test and exit $STF_FAIL is test fails
62eda14cbcSMatt Macy#
63eda14cbcSMatt Macy# $@ - command to execute
64eda14cbcSMatt Macy
65eda14cbcSMatt Macyfunction log_must
66eda14cbcSMatt Macy{
67eda14cbcSMatt Macy	log_pos "$@"
68eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
69eda14cbcSMatt Macy}
70eda14cbcSMatt Macy
71eda14cbcSMatt Macy# Execute a positive test but retry the command on failure if the output
72eda14cbcSMatt Macy# matches an expected pattern.  Otherwise behave like log_must and exit
73eda14cbcSMatt Macy# $STF_FAIL is test fails.
74eda14cbcSMatt Macy#
75eda14cbcSMatt Macy# $1 - retry keyword
76eda14cbcSMatt Macy# $2 - retry attempts
77eda14cbcSMatt Macy# $3-$@ - command to execute
78eda14cbcSMatt Macy#
79eda14cbcSMatt Macyfunction log_must_retry
80eda14cbcSMatt Macy{
81eda14cbcSMatt Macy	typeset out=""
82eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
83eda14cbcSMatt Macy	typeset status=1
84eda14cbcSMatt Macy	typeset expect=$1
85eda14cbcSMatt Macy	typeset retry=$2
86eda14cbcSMatt Macy	typeset delay=1
87eda14cbcSMatt Macy	shift 2
88eda14cbcSMatt Macy
89eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
90eda14cbcSMatt Macy		logfile="$logfile.$$"
91eda14cbcSMatt Macy	done
92eda14cbcSMatt Macy
93eda14cbcSMatt Macy	while (( $retry > 0 )); do
94eda14cbcSMatt Macy		"$@" 2>$logfile
95eda14cbcSMatt Macy		status=$?
96eda14cbcSMatt Macy		out="cat $logfile"
97eda14cbcSMatt Macy
98eda14cbcSMatt Macy		if (( $status == 0 )); then
99eda14cbcSMatt Macy			$out | egrep -i "internal error|assertion failed" \
100eda14cbcSMatt Macy				> /dev/null 2>&1
101eda14cbcSMatt Macy			# internal error or assertion failed
102eda14cbcSMatt Macy			if [[ $? -eq 0 ]]; then
103eda14cbcSMatt Macy				print -u2 $($out)
104eda14cbcSMatt Macy				_printerror "$@" "internal error or" \
105eda14cbcSMatt Macy					" assertion failure exited $status"
106eda14cbcSMatt Macy				status=1
107eda14cbcSMatt Macy			else
108*184c1b94SMartin Matuska				[[ -n $LOGAPI_DEBUG ]] && cat $logfile
109eda14cbcSMatt Macy				_printsuccess "$@"
110eda14cbcSMatt Macy			fi
111eda14cbcSMatt Macy			break
112eda14cbcSMatt Macy		else
113eda14cbcSMatt Macy			$out | grep -i "$expect" > /dev/null 2>&1
114eda14cbcSMatt Macy			if (( $? == 0 )); then
115eda14cbcSMatt Macy				print -u2 $($out)
116eda14cbcSMatt Macy				_printerror "$@" "Retry in $delay seconds"
117eda14cbcSMatt Macy				sleep $delay
118eda14cbcSMatt Macy
119eda14cbcSMatt Macy				(( retry=retry - 1 ))
120eda14cbcSMatt Macy				(( delay=delay * 2 ))
121eda14cbcSMatt Macy			else
122eda14cbcSMatt Macy				break;
123eda14cbcSMatt Macy			fi
124eda14cbcSMatt Macy		fi
125eda14cbcSMatt Macy	done
126eda14cbcSMatt Macy
127eda14cbcSMatt Macy	if (( $status != 0 )) ; then
128eda14cbcSMatt Macy		print -u2 $($out)
129eda14cbcSMatt Macy		_printerror "$@" "exited $status"
130eda14cbcSMatt Macy	fi
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy	_recursive_output $logfile "false"
133eda14cbcSMatt Macy	return $status
134eda14cbcSMatt Macy}
135eda14cbcSMatt Macy
136eda14cbcSMatt Macy# Execute a positive test and exit $STF_FAIL is test fails after being
137eda14cbcSMatt Macy# retried up to 5 times when the command returns the keyword "busy".
138eda14cbcSMatt Macy#
139eda14cbcSMatt Macy# $@ - command to execute
140eda14cbcSMatt Macyfunction log_must_busy
141eda14cbcSMatt Macy{
142eda14cbcSMatt Macy	log_must_retry "busy" 5 "$@"
143eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
144eda14cbcSMatt Macy}
145eda14cbcSMatt Macy
146eda14cbcSMatt Macy# Execute a negative test and exit $STF_FAIL if test passes
147eda14cbcSMatt Macy#
148eda14cbcSMatt Macy# $@ - command to execute
149eda14cbcSMatt Macy
150eda14cbcSMatt Macyfunction log_mustnot
151eda14cbcSMatt Macy{
152eda14cbcSMatt Macy	log_neg "$@"
153eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
154eda14cbcSMatt Macy}
155eda14cbcSMatt Macy
156eda14cbcSMatt Macy# Execute a negative test with keyword expected, and exit
157eda14cbcSMatt Macy# $STF_FAIL if test passes
158eda14cbcSMatt Macy#
159eda14cbcSMatt Macy# $1 - keyword expected
160eda14cbcSMatt Macy# $2-$@ - command to execute
161eda14cbcSMatt Macy
162eda14cbcSMatt Macyfunction log_mustnot_expect
163eda14cbcSMatt Macy{
164eda14cbcSMatt Macy	log_neg_expect "$@"
165eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
166eda14cbcSMatt Macy}
167eda14cbcSMatt Macy
168eda14cbcSMatt Macy# Signal numbers are platform-dependent
169eda14cbcSMatt Macycase $(uname) in
170eda14cbcSMatt MacyDarwin|FreeBSD)
171eda14cbcSMatt Macy	SIGBUS=10
172eda14cbcSMatt Macy	SIGSEGV=11
173eda14cbcSMatt Macy	;;
174eda14cbcSMatt Macyillumos|Linux|*)
175eda14cbcSMatt Macy	SIGBUS=7
176eda14cbcSMatt Macy	SIGSEGV=11
177eda14cbcSMatt Macy	;;
178eda14cbcSMatt Macyesac
179eda14cbcSMatt MacyEXIT_SUCCESS=0
180eda14cbcSMatt MacyEXIT_NOTFOUND=127
181eda14cbcSMatt MacyEXIT_SIGNAL=256
182eda14cbcSMatt MacyEXIT_SIGBUS=$((EXIT_SIGNAL + SIGBUS))
183eda14cbcSMatt MacyEXIT_SIGSEGV=$((EXIT_SIGNAL + SIGSEGV))
184eda14cbcSMatt Macy
185eda14cbcSMatt Macy# Execute and print command with status where success equals non-zero result
186eda14cbcSMatt Macy# or output includes expected keyword
187eda14cbcSMatt Macy#
188eda14cbcSMatt Macy# $1 - keyword expected
189eda14cbcSMatt Macy# $2-$@ - command to execute
190eda14cbcSMatt Macy#
191eda14cbcSMatt Macy# return 0 if command fails, or the output contains the keyword expected,
192eda14cbcSMatt Macy# return 1 otherwise
193eda14cbcSMatt Macy
194eda14cbcSMatt Macyfunction log_neg_expect
195eda14cbcSMatt Macy{
196eda14cbcSMatt Macy	typeset out=""
197eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
198eda14cbcSMatt Macy	typeset ret=1
199eda14cbcSMatt Macy	typeset expect=$1
200eda14cbcSMatt Macy	shift
201eda14cbcSMatt Macy
202eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
203eda14cbcSMatt Macy		logfile="$logfile.$$"
204eda14cbcSMatt Macy	done
205eda14cbcSMatt Macy
206eda14cbcSMatt Macy	"$@" 2>$logfile
207eda14cbcSMatt Macy	typeset status=$?
208eda14cbcSMatt Macy	out="cat $logfile"
209eda14cbcSMatt Macy
210eda14cbcSMatt Macy	# unexpected status
211eda14cbcSMatt Macy	if (( $status == EXIT_SUCCESS )); then
212eda14cbcSMatt Macy		 print -u2 $($out)
213eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status"
214eda14cbcSMatt Macy	# missing binary
215eda14cbcSMatt Macy	elif (( $status == EXIT_NOTFOUND )); then
216eda14cbcSMatt Macy		print -u2 $($out)
217eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (File not found)"
218eda14cbcSMatt Macy	# bus error - core dump
219eda14cbcSMatt Macy	elif (( $status == EXIT_SIGBUS )); then
220eda14cbcSMatt Macy		print -u2 $($out)
221eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (Bus Error)"
222eda14cbcSMatt Macy	# segmentation violation - core dump
223eda14cbcSMatt Macy	elif (( $status == EXIT_SIGSEGV )); then
224eda14cbcSMatt Macy		print -u2 $($out)
225eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (SEGV)"
226eda14cbcSMatt Macy	else
227eda14cbcSMatt Macy		$out | egrep -i "internal error|assertion failed" \
228eda14cbcSMatt Macy			> /dev/null 2>&1
229eda14cbcSMatt Macy		# internal error or assertion failed
230eda14cbcSMatt Macy		if (( $? == 0 )); then
231eda14cbcSMatt Macy			print -u2 $($out)
232eda14cbcSMatt Macy			_printerror "$@" "internal error or assertion failure" \
233eda14cbcSMatt Macy				" exited $status"
234eda14cbcSMatt Macy		elif [[ -n $expect ]] ; then
235eda14cbcSMatt Macy			$out | grep -i "$expect" > /dev/null 2>&1
236eda14cbcSMatt Macy			if (( $? == 0 )); then
237eda14cbcSMatt Macy				ret=0
238eda14cbcSMatt Macy			else
239eda14cbcSMatt Macy				print -u2 $($out)
240eda14cbcSMatt Macy				_printerror "$@" "unexpectedly exited $status"
241eda14cbcSMatt Macy			fi
242eda14cbcSMatt Macy		else
243eda14cbcSMatt Macy			ret=0
244eda14cbcSMatt Macy		fi
245eda14cbcSMatt Macy
246eda14cbcSMatt Macy		if (( $ret == 0 )); then
247*184c1b94SMartin Matuska			[[ -n $LOGAPI_DEBUG ]] && cat $logfile
248eda14cbcSMatt Macy			_printsuccess "$@" "exited $status"
249eda14cbcSMatt Macy		fi
250eda14cbcSMatt Macy	fi
251eda14cbcSMatt Macy	_recursive_output $logfile "false"
252eda14cbcSMatt Macy	return $ret
253eda14cbcSMatt Macy}
254eda14cbcSMatt Macy
255eda14cbcSMatt Macy# Execute and print command with status where success equals zero result
256eda14cbcSMatt Macy#
257eda14cbcSMatt Macy# $@ command to execute
258eda14cbcSMatt Macy#
259eda14cbcSMatt Macy# return command exit status
260eda14cbcSMatt Macy
261eda14cbcSMatt Macyfunction log_pos
262eda14cbcSMatt Macy{
263eda14cbcSMatt Macy	typeset out=""
264eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
265eda14cbcSMatt Macy
266eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
267eda14cbcSMatt Macy		logfile="$logfile.$$"
268eda14cbcSMatt Macy	done
269eda14cbcSMatt Macy
270eda14cbcSMatt Macy	"$@" 2>$logfile
271eda14cbcSMatt Macy	typeset status=$?
272eda14cbcSMatt Macy	out="cat $logfile"
273eda14cbcSMatt Macy
274eda14cbcSMatt Macy	if (( $status != 0 )) ; then
275eda14cbcSMatt Macy		print -u2 $($out)
276eda14cbcSMatt Macy		_printerror "$@" "exited $status"
277eda14cbcSMatt Macy	else
278eda14cbcSMatt Macy		$out | egrep -i "internal error|assertion failed" \
279eda14cbcSMatt Macy			> /dev/null 2>&1
280eda14cbcSMatt Macy		# internal error or assertion failed
281eda14cbcSMatt Macy		if [[ $? -eq 0 ]]; then
282eda14cbcSMatt Macy			print -u2 $($out)
283eda14cbcSMatt Macy			_printerror "$@" "internal error or assertion failure" \
284eda14cbcSMatt Macy				" exited $status"
285eda14cbcSMatt Macy			status=1
286eda14cbcSMatt Macy		else
287*184c1b94SMartin Matuska			[[ -n $LOGAPI_DEBUG ]] && cat $logfile
288eda14cbcSMatt Macy			_printsuccess "$@"
289eda14cbcSMatt Macy		fi
290eda14cbcSMatt Macy	fi
291eda14cbcSMatt Macy	_recursive_output $logfile "false"
292eda14cbcSMatt Macy	return $status
293eda14cbcSMatt Macy}
294eda14cbcSMatt Macy
295eda14cbcSMatt Macy# Set an exit handler
296eda14cbcSMatt Macy#
297eda14cbcSMatt Macy# $@ - function(s) to perform on exit
298eda14cbcSMatt Macy
299eda14cbcSMatt Macyfunction log_onexit
300eda14cbcSMatt Macy{
301eda14cbcSMatt Macy	_CLEANUP=("$*")
302eda14cbcSMatt Macy}
303eda14cbcSMatt Macy
304eda14cbcSMatt Macy# Push an exit handler on the cleanup stack
305eda14cbcSMatt Macy#
306eda14cbcSMatt Macy# $@ - function(s) to perform on exit
307eda14cbcSMatt Macy
308eda14cbcSMatt Macyfunction log_onexit_push
309eda14cbcSMatt Macy{
310eda14cbcSMatt Macy	_CLEANUP+=("$*")
311eda14cbcSMatt Macy}
312eda14cbcSMatt Macy
313eda14cbcSMatt Macy# Pop an exit handler off the cleanup stack
314eda14cbcSMatt Macy
315eda14cbcSMatt Macyfunction log_onexit_pop
316eda14cbcSMatt Macy{
317eda14cbcSMatt Macy	_CLEANUP=("${_CLEANUP[@]:0:${#_CLEANUP[@]}-1}")
318eda14cbcSMatt Macy}
319eda14cbcSMatt Macy
320eda14cbcSMatt Macy#
321eda14cbcSMatt Macy# Exit functions
322eda14cbcSMatt Macy#
323eda14cbcSMatt Macy
324eda14cbcSMatt Macy# Perform cleanup and exit $STF_PASS
325eda14cbcSMatt Macy#
326eda14cbcSMatt Macy# $@ - message text
327eda14cbcSMatt Macy
328eda14cbcSMatt Macyfunction log_pass
329eda14cbcSMatt Macy{
330eda14cbcSMatt Macy	_endlog $STF_PASS "$@"
331eda14cbcSMatt Macy}
332eda14cbcSMatt Macy
333eda14cbcSMatt Macy# Perform cleanup and exit $STF_FAIL
334eda14cbcSMatt Macy#
335eda14cbcSMatt Macy# $@ - message text
336eda14cbcSMatt Macy
337eda14cbcSMatt Macyfunction log_fail
338eda14cbcSMatt Macy{
339eda14cbcSMatt Macy	_endlog $STF_FAIL "$@"
340eda14cbcSMatt Macy}
341eda14cbcSMatt Macy
342eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNRESOLVED
343eda14cbcSMatt Macy#
344eda14cbcSMatt Macy# $@ - message text
345eda14cbcSMatt Macy
346eda14cbcSMatt Macyfunction log_unresolved
347eda14cbcSMatt Macy{
348eda14cbcSMatt Macy	_endlog $STF_UNRESOLVED "$@"
349eda14cbcSMatt Macy}
350eda14cbcSMatt Macy
351eda14cbcSMatt Macy# Perform cleanup and exit $STF_NOTINUSE
352eda14cbcSMatt Macy#
353eda14cbcSMatt Macy# $@ - message text
354eda14cbcSMatt Macy
355eda14cbcSMatt Macyfunction log_notinuse
356eda14cbcSMatt Macy{
357eda14cbcSMatt Macy	_endlog $STF_NOTINUSE "$@"
358eda14cbcSMatt Macy}
359eda14cbcSMatt Macy
360eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNSUPPORTED
361eda14cbcSMatt Macy#
362eda14cbcSMatt Macy# $@ - message text
363eda14cbcSMatt Macy
364eda14cbcSMatt Macyfunction log_unsupported
365eda14cbcSMatt Macy{
366eda14cbcSMatt Macy	_endlog $STF_UNSUPPORTED "$@"
367eda14cbcSMatt Macy}
368eda14cbcSMatt Macy
369eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNTESTED
370eda14cbcSMatt Macy#
371eda14cbcSMatt Macy# $@ - message text
372eda14cbcSMatt Macy
373eda14cbcSMatt Macyfunction log_untested
374eda14cbcSMatt Macy{
375eda14cbcSMatt Macy	_endlog $STF_UNTESTED "$@"
376eda14cbcSMatt Macy}
377eda14cbcSMatt Macy
378eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNINITIATED
379eda14cbcSMatt Macy#
380eda14cbcSMatt Macy# $@ - message text
381eda14cbcSMatt Macy
382eda14cbcSMatt Macyfunction log_uninitiated
383eda14cbcSMatt Macy{
384eda14cbcSMatt Macy	_endlog $STF_UNINITIATED "$@"
385eda14cbcSMatt Macy}
386eda14cbcSMatt Macy
387eda14cbcSMatt Macy# Perform cleanup and exit $STF_NORESULT
388eda14cbcSMatt Macy#
389eda14cbcSMatt Macy# $@ - message text
390eda14cbcSMatt Macy
391eda14cbcSMatt Macyfunction log_noresult
392eda14cbcSMatt Macy{
393eda14cbcSMatt Macy	_endlog $STF_NORESULT "$@"
394eda14cbcSMatt Macy}
395eda14cbcSMatt Macy
396eda14cbcSMatt Macy# Perform cleanup and exit $STF_WARNING
397eda14cbcSMatt Macy#
398eda14cbcSMatt Macy# $@ - message text
399eda14cbcSMatt Macy
400eda14cbcSMatt Macyfunction log_warning
401eda14cbcSMatt Macy{
402eda14cbcSMatt Macy	_endlog $STF_WARNING "$@"
403eda14cbcSMatt Macy}
404eda14cbcSMatt Macy
405eda14cbcSMatt Macy# Perform cleanup and exit $STF_TIMED_OUT
406eda14cbcSMatt Macy#
407eda14cbcSMatt Macy# $@ - message text
408eda14cbcSMatt Macy
409eda14cbcSMatt Macyfunction log_timed_out
410eda14cbcSMatt Macy{
411eda14cbcSMatt Macy	_endlog $STF_TIMED_OUT "$@"
412eda14cbcSMatt Macy}
413eda14cbcSMatt Macy
414eda14cbcSMatt Macy# Perform cleanup and exit $STF_OTHER
415eda14cbcSMatt Macy#
416eda14cbcSMatt Macy# $@ - message text
417eda14cbcSMatt Macy
418eda14cbcSMatt Macyfunction log_other
419eda14cbcSMatt Macy{
420eda14cbcSMatt Macy	_endlog $STF_OTHER "$@"
421eda14cbcSMatt Macy}
422eda14cbcSMatt Macy
423eda14cbcSMatt Macyfunction set_main_pid
424eda14cbcSMatt Macy{
425eda14cbcSMatt Macy	_MAINPID=$1
426eda14cbcSMatt Macy}
427eda14cbcSMatt Macy
428eda14cbcSMatt Macy#
429eda14cbcSMatt Macy# Internal functions
430eda14cbcSMatt Macy#
431eda14cbcSMatt Macy
432eda14cbcSMatt Macy# Execute custom callback scripts on test failure
433eda14cbcSMatt Macy#
434eda14cbcSMatt Macy# callback script paths are stored in TESTFAIL_CALLBACKS, delimited by ':'.
435eda14cbcSMatt Macy
436eda14cbcSMatt Macyfunction _execute_testfail_callbacks
437eda14cbcSMatt Macy{
438eda14cbcSMatt Macy	typeset callback
439eda14cbcSMatt Macy
440eda14cbcSMatt Macy	print "$TESTFAIL_CALLBACKS:" | while read -d ":" callback; do
441eda14cbcSMatt Macy		if [[ -n "$callback" ]] ; then
442eda14cbcSMatt Macy			log_note "Performing test-fail callback ($callback)"
443eda14cbcSMatt Macy			$callback
444eda14cbcSMatt Macy		fi
445eda14cbcSMatt Macy	done
446eda14cbcSMatt Macy}
447eda14cbcSMatt Macy
448eda14cbcSMatt Macy# Perform cleanup and exit
449eda14cbcSMatt Macy#
450eda14cbcSMatt Macy# $1 - stf exit code
451eda14cbcSMatt Macy# $2-$n - message text
452eda14cbcSMatt Macy
453eda14cbcSMatt Macyfunction _endlog
454eda14cbcSMatt Macy{
455eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
456eda14cbcSMatt Macy	_recursive_output $logfile
457eda14cbcSMatt Macy
458eda14cbcSMatt Macy	typeset exitcode=$1
459eda14cbcSMatt Macy	shift
460eda14cbcSMatt Macy	(( ${#@} > 0 )) && _printline "$@"
461eda14cbcSMatt Macy
462eda14cbcSMatt Macy	#
463eda14cbcSMatt Macy	# If we're running in a subshell then just exit and let
464eda14cbcSMatt Macy	# the parent handle the failures
465eda14cbcSMatt Macy	#
466eda14cbcSMatt Macy	if [[ -n "$_MAINPID" && $$ != "$_MAINPID" ]]; then
467eda14cbcSMatt Macy		log_note "subshell exited: "$_MAINPID
468eda14cbcSMatt Macy		exit $exitcode
469eda14cbcSMatt Macy	fi
470eda14cbcSMatt Macy
471eda14cbcSMatt Macy	if [[ $exitcode == $STF_FAIL ]] ; then
472eda14cbcSMatt Macy		_execute_testfail_callbacks
473eda14cbcSMatt Macy	fi
474eda14cbcSMatt Macy
475eda14cbcSMatt Macy	typeset stack=("${_CLEANUP[@]}")
476eda14cbcSMatt Macy	log_onexit ""
477eda14cbcSMatt Macy	typeset i=${#stack[@]}
478eda14cbcSMatt Macy	while (( i-- )); do
479eda14cbcSMatt Macy		typeset cleanup="${stack[i]}"
480eda14cbcSMatt Macy		log_note "Performing local cleanup via log_onexit ($cleanup)"
481eda14cbcSMatt Macy		$cleanup
482eda14cbcSMatt Macy	done
483eda14cbcSMatt Macy
484eda14cbcSMatt Macy	exit $exitcode
485eda14cbcSMatt Macy}
486eda14cbcSMatt Macy
487eda14cbcSMatt Macy# Output a formatted line
488eda14cbcSMatt Macy#
489eda14cbcSMatt Macy# $@ - message text
490eda14cbcSMatt Macy
491eda14cbcSMatt Macyfunction _printline
492eda14cbcSMatt Macy{
493eda14cbcSMatt Macy	print "$@"
494eda14cbcSMatt Macy}
495eda14cbcSMatt Macy
496eda14cbcSMatt Macy# Output an error message
497eda14cbcSMatt Macy#
498eda14cbcSMatt Macy# $@ - message text
499eda14cbcSMatt Macy
500eda14cbcSMatt Macyfunction _printerror
501eda14cbcSMatt Macy{
502eda14cbcSMatt Macy	_printline ERROR: "$@"
503eda14cbcSMatt Macy}
504eda14cbcSMatt Macy
505eda14cbcSMatt Macy# Output a success message
506eda14cbcSMatt Macy#
507eda14cbcSMatt Macy# $@ - message text
508eda14cbcSMatt Macy
509eda14cbcSMatt Macyfunction _printsuccess
510eda14cbcSMatt Macy{
511eda14cbcSMatt Macy	_printline SUCCESS: "$@"
512eda14cbcSMatt Macy}
513eda14cbcSMatt Macy
514eda14cbcSMatt Macy# Output logfiles recursively
515eda14cbcSMatt Macy#
516eda14cbcSMatt Macy# $1 - start file
517eda14cbcSMatt Macy# $2 - indicate whether output the start file itself, default as yes.
518eda14cbcSMatt Macy
519eda14cbcSMatt Macyfunction _recursive_output #logfile
520eda14cbcSMatt Macy{
521eda14cbcSMatt Macy	typeset logfile=$1
522eda14cbcSMatt Macy
523eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
524eda14cbcSMatt Macy		if [[ -z $2 || $logfile != $1 ]]; then
525eda14cbcSMatt Macy			cat $logfile
526eda14cbcSMatt Macy		fi
527eda14cbcSMatt Macy		rm -f $logfile
528eda14cbcSMatt Macy		logfile="$logfile.$$"
529eda14cbcSMatt Macy        done
530eda14cbcSMatt Macy}
531