xref: /freebsd/sys/contrib/openzfs/tests/test-runner/include/logapi.shlib (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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
71*6ba2210eSMartin Matuska# Execute a positive test (expecting no stderr) and exit $STF_FAIL
72*6ba2210eSMartin Matuska# if test fails
73*6ba2210eSMartin Matuska# $@ - command to execute
74*6ba2210eSMartin Matuska
75*6ba2210eSMartin Matuskafunction log_must_nostderr
76*6ba2210eSMartin Matuska{
77*6ba2210eSMartin Matuska	log_pos_nostderr "$@"
78*6ba2210eSMartin Matuska	(( $? != 0 )) && log_fail
79*6ba2210eSMartin Matuska}
80*6ba2210eSMartin Matuska
81eda14cbcSMatt Macy# Execute a positive test but retry the command on failure if the output
82eda14cbcSMatt Macy# matches an expected pattern.  Otherwise behave like log_must and exit
83eda14cbcSMatt Macy# $STF_FAIL is test fails.
84eda14cbcSMatt Macy#
85eda14cbcSMatt Macy# $1 - retry keyword
86eda14cbcSMatt Macy# $2 - retry attempts
87eda14cbcSMatt Macy# $3-$@ - command to execute
88eda14cbcSMatt Macy#
89eda14cbcSMatt Macyfunction log_must_retry
90eda14cbcSMatt Macy{
91eda14cbcSMatt Macy	typeset out=""
92eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
93eda14cbcSMatt Macy	typeset status=1
94eda14cbcSMatt Macy	typeset expect=$1
95eda14cbcSMatt Macy	typeset retry=$2
96eda14cbcSMatt Macy	typeset delay=1
97eda14cbcSMatt Macy	shift 2
98eda14cbcSMatt Macy
99eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
100eda14cbcSMatt Macy		logfile="$logfile.$$"
101eda14cbcSMatt Macy	done
102eda14cbcSMatt Macy
103eda14cbcSMatt Macy	while (( $retry > 0 )); do
104eda14cbcSMatt Macy		"$@" 2>$logfile
105eda14cbcSMatt Macy		status=$?
106eda14cbcSMatt Macy		out="cat $logfile"
107eda14cbcSMatt Macy
108eda14cbcSMatt Macy		if (( $status == 0 )); then
109eda14cbcSMatt Macy			$out | egrep -i "internal error|assertion failed" \
110eda14cbcSMatt Macy				> /dev/null 2>&1
111eda14cbcSMatt Macy			# internal error or assertion failed
112eda14cbcSMatt Macy			if [[ $? -eq 0 ]]; then
113eda14cbcSMatt Macy				print -u2 $($out)
114eda14cbcSMatt Macy				_printerror "$@" "internal error or" \
115eda14cbcSMatt Macy					" assertion failure exited $status"
116eda14cbcSMatt Macy				status=1
117eda14cbcSMatt Macy			else
118184c1b94SMartin Matuska				[[ -n $LOGAPI_DEBUG ]] && cat $logfile
119eda14cbcSMatt Macy				_printsuccess "$@"
120eda14cbcSMatt Macy			fi
121eda14cbcSMatt Macy			break
122eda14cbcSMatt Macy		else
123eda14cbcSMatt Macy			$out | grep -i "$expect" > /dev/null 2>&1
124eda14cbcSMatt Macy			if (( $? == 0 )); then
125eda14cbcSMatt Macy				print -u2 $($out)
126eda14cbcSMatt Macy				_printerror "$@" "Retry in $delay seconds"
127eda14cbcSMatt Macy				sleep $delay
128eda14cbcSMatt Macy
129eda14cbcSMatt Macy				(( retry=retry - 1 ))
130eda14cbcSMatt Macy				(( delay=delay * 2 ))
131eda14cbcSMatt Macy			else
132eda14cbcSMatt Macy				break;
133eda14cbcSMatt Macy			fi
134eda14cbcSMatt Macy		fi
135eda14cbcSMatt Macy	done
136eda14cbcSMatt Macy
137eda14cbcSMatt Macy	if (( $status != 0 )) ; then
138eda14cbcSMatt Macy		print -u2 $($out)
139eda14cbcSMatt Macy		_printerror "$@" "exited $status"
140eda14cbcSMatt Macy	fi
141eda14cbcSMatt Macy
142eda14cbcSMatt Macy	_recursive_output $logfile "false"
143eda14cbcSMatt Macy	return $status
144eda14cbcSMatt Macy}
145eda14cbcSMatt Macy
146eda14cbcSMatt Macy# Execute a positive test and exit $STF_FAIL is test fails after being
147eda14cbcSMatt Macy# retried up to 5 times when the command returns the keyword "busy".
148eda14cbcSMatt Macy#
149eda14cbcSMatt Macy# $@ - command to execute
150eda14cbcSMatt Macyfunction log_must_busy
151eda14cbcSMatt Macy{
152eda14cbcSMatt Macy	log_must_retry "busy" 5 "$@"
153eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
154eda14cbcSMatt Macy}
155eda14cbcSMatt Macy
156eda14cbcSMatt Macy# Execute a negative test and exit $STF_FAIL if test passes
157eda14cbcSMatt Macy#
158eda14cbcSMatt Macy# $@ - command to execute
159eda14cbcSMatt Macy
160eda14cbcSMatt Macyfunction log_mustnot
161eda14cbcSMatt Macy{
162eda14cbcSMatt Macy	log_neg "$@"
163eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
164eda14cbcSMatt Macy}
165eda14cbcSMatt Macy
166eda14cbcSMatt Macy# Execute a negative test with keyword expected, and exit
167eda14cbcSMatt Macy# $STF_FAIL if test passes
168eda14cbcSMatt Macy#
169eda14cbcSMatt Macy# $1 - keyword expected
170eda14cbcSMatt Macy# $2-$@ - command to execute
171eda14cbcSMatt Macy
172eda14cbcSMatt Macyfunction log_mustnot_expect
173eda14cbcSMatt Macy{
174eda14cbcSMatt Macy	log_neg_expect "$@"
175eda14cbcSMatt Macy	(( $? != 0 )) && log_fail
176eda14cbcSMatt Macy}
177eda14cbcSMatt Macy
178eda14cbcSMatt Macy# Signal numbers are platform-dependent
179eda14cbcSMatt Macycase $(uname) in
180eda14cbcSMatt MacyDarwin|FreeBSD)
181eda14cbcSMatt Macy	SIGBUS=10
182eda14cbcSMatt Macy	SIGSEGV=11
183eda14cbcSMatt Macy	;;
184eda14cbcSMatt Macyillumos|Linux|*)
185eda14cbcSMatt Macy	SIGBUS=7
186eda14cbcSMatt Macy	SIGSEGV=11
187eda14cbcSMatt Macy	;;
188eda14cbcSMatt Macyesac
189eda14cbcSMatt MacyEXIT_SUCCESS=0
190eda14cbcSMatt MacyEXIT_NOTFOUND=127
191eda14cbcSMatt MacyEXIT_SIGNAL=256
192eda14cbcSMatt MacyEXIT_SIGBUS=$((EXIT_SIGNAL + SIGBUS))
193eda14cbcSMatt MacyEXIT_SIGSEGV=$((EXIT_SIGNAL + SIGSEGV))
194eda14cbcSMatt Macy
195eda14cbcSMatt Macy# Execute and print command with status where success equals non-zero result
196eda14cbcSMatt Macy# or output includes expected keyword
197eda14cbcSMatt Macy#
198eda14cbcSMatt Macy# $1 - keyword expected
199eda14cbcSMatt Macy# $2-$@ - command to execute
200eda14cbcSMatt Macy#
201eda14cbcSMatt Macy# return 0 if command fails, or the output contains the keyword expected,
202eda14cbcSMatt Macy# return 1 otherwise
203eda14cbcSMatt Macy
204eda14cbcSMatt Macyfunction log_neg_expect
205eda14cbcSMatt Macy{
206eda14cbcSMatt Macy	typeset out=""
207eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
208eda14cbcSMatt Macy	typeset ret=1
209eda14cbcSMatt Macy	typeset expect=$1
210eda14cbcSMatt Macy	shift
211eda14cbcSMatt Macy
212eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
213eda14cbcSMatt Macy		logfile="$logfile.$$"
214eda14cbcSMatt Macy	done
215eda14cbcSMatt Macy
216eda14cbcSMatt Macy	"$@" 2>$logfile
217eda14cbcSMatt Macy	typeset status=$?
218eda14cbcSMatt Macy	out="cat $logfile"
219eda14cbcSMatt Macy
220eda14cbcSMatt Macy	# unexpected status
221eda14cbcSMatt Macy	if (( $status == EXIT_SUCCESS )); then
222eda14cbcSMatt Macy		 print -u2 $($out)
223eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status"
224eda14cbcSMatt Macy	# missing binary
225eda14cbcSMatt Macy	elif (( $status == EXIT_NOTFOUND )); then
226eda14cbcSMatt Macy		print -u2 $($out)
227eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (File not found)"
228eda14cbcSMatt Macy	# bus error - core dump
229eda14cbcSMatt Macy	elif (( $status == EXIT_SIGBUS )); then
230eda14cbcSMatt Macy		print -u2 $($out)
231eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (Bus Error)"
232eda14cbcSMatt Macy	# segmentation violation - core dump
233eda14cbcSMatt Macy	elif (( $status == EXIT_SIGSEGV )); then
234eda14cbcSMatt Macy		print -u2 $($out)
235eda14cbcSMatt Macy		_printerror "$@" "unexpectedly exited $status (SEGV)"
236eda14cbcSMatt Macy	else
237eda14cbcSMatt Macy		$out | egrep -i "internal error|assertion failed" \
238eda14cbcSMatt Macy			> /dev/null 2>&1
239eda14cbcSMatt Macy		# internal error or assertion failed
240eda14cbcSMatt Macy		if (( $? == 0 )); then
241eda14cbcSMatt Macy			print -u2 $($out)
242eda14cbcSMatt Macy			_printerror "$@" "internal error or assertion failure" \
243eda14cbcSMatt Macy				" exited $status"
244eda14cbcSMatt Macy		elif [[ -n $expect ]] ; then
245eda14cbcSMatt Macy			$out | grep -i "$expect" > /dev/null 2>&1
246eda14cbcSMatt Macy			if (( $? == 0 )); then
247eda14cbcSMatt Macy				ret=0
248eda14cbcSMatt Macy			else
249eda14cbcSMatt Macy				print -u2 $($out)
250eda14cbcSMatt Macy				_printerror "$@" "unexpectedly exited $status"
251eda14cbcSMatt Macy			fi
252eda14cbcSMatt Macy		else
253eda14cbcSMatt Macy			ret=0
254eda14cbcSMatt Macy		fi
255eda14cbcSMatt Macy
256eda14cbcSMatt Macy		if (( $ret == 0 )); then
257184c1b94SMartin Matuska			[[ -n $LOGAPI_DEBUG ]] && cat $logfile
258eda14cbcSMatt Macy			_printsuccess "$@" "exited $status"
259eda14cbcSMatt Macy		fi
260eda14cbcSMatt Macy	fi
261eda14cbcSMatt Macy	_recursive_output $logfile "false"
262eda14cbcSMatt Macy	return $ret
263eda14cbcSMatt Macy}
264eda14cbcSMatt Macy
265eda14cbcSMatt Macy# Execute and print command with status where success equals zero result
266eda14cbcSMatt Macy#
267eda14cbcSMatt Macy# $@ command to execute
268eda14cbcSMatt Macy#
269eda14cbcSMatt Macy# return command exit status
270eda14cbcSMatt Macy
271eda14cbcSMatt Macyfunction log_pos
272eda14cbcSMatt Macy{
273eda14cbcSMatt Macy	typeset out=""
274eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
277eda14cbcSMatt Macy		logfile="$logfile.$$"
278eda14cbcSMatt Macy	done
279eda14cbcSMatt Macy
280eda14cbcSMatt Macy	"$@" 2>$logfile
281eda14cbcSMatt Macy	typeset status=$?
282eda14cbcSMatt Macy	out="cat $logfile"
283eda14cbcSMatt Macy
284eda14cbcSMatt Macy	if (( $status != 0 )) ; then
285eda14cbcSMatt Macy		print -u2 $($out)
286eda14cbcSMatt Macy		_printerror "$@" "exited $status"
287eda14cbcSMatt Macy	else
288eda14cbcSMatt Macy		$out | egrep -i "internal error|assertion failed" \
289eda14cbcSMatt Macy			> /dev/null 2>&1
290eda14cbcSMatt Macy		# internal error or assertion failed
291eda14cbcSMatt Macy		if [[ $? -eq 0 ]]; then
292eda14cbcSMatt Macy			print -u2 $($out)
293eda14cbcSMatt Macy			_printerror "$@" "internal error or assertion failure" \
294eda14cbcSMatt Macy				" exited $status"
295eda14cbcSMatt Macy			status=1
296eda14cbcSMatt Macy		else
297184c1b94SMartin Matuska			[[ -n $LOGAPI_DEBUG ]] && cat $logfile
298eda14cbcSMatt Macy			_printsuccess "$@"
299eda14cbcSMatt Macy		fi
300eda14cbcSMatt Macy	fi
301eda14cbcSMatt Macy	_recursive_output $logfile "false"
302eda14cbcSMatt Macy	return $status
303eda14cbcSMatt Macy}
304eda14cbcSMatt Macy
305*6ba2210eSMartin Matuska# Execute and print command with status where success equals zero result
306*6ba2210eSMartin Matuska# and no stderr output
307*6ba2210eSMartin Matuska#
308*6ba2210eSMartin Matuska# $@ command to execute
309*6ba2210eSMartin Matuska#
310*6ba2210eSMartin Matuska# return 0 if command succeeds and no stderr output
311*6ba2210eSMartin Matuska# return 1 othersie
312*6ba2210eSMartin Matuska
313*6ba2210eSMartin Matuskafunction log_pos_nostderr
314*6ba2210eSMartin Matuska{
315*6ba2210eSMartin Matuska	typeset out=""
316*6ba2210eSMartin Matuska	typeset logfile="/tmp/log.$$"
317*6ba2210eSMartin Matuska
318*6ba2210eSMartin Matuska	while [[ -e $logfile ]]; do
319*6ba2210eSMartin Matuska		logfile="$logfile.$$"
320*6ba2210eSMartin Matuska	done
321*6ba2210eSMartin Matuska
322*6ba2210eSMartin Matuska	"$@" 2>$logfile
323*6ba2210eSMartin Matuska	typeset status=$?
324*6ba2210eSMartin Matuska	out="cat $logfile"
325*6ba2210eSMartin Matuska	typeset out_msg=$($out)
326*6ba2210eSMartin Matuska
327*6ba2210eSMartin Matuska	if (( $status != 0 )) ; then
328*6ba2210eSMartin Matuska		print -u2 $out_msg
329*6ba2210eSMartin Matuska		_printerror "$@" "exited $status"
330*6ba2210eSMartin Matuska	else
331*6ba2210eSMartin Matuska		if [[ ! -z "$out_msg" ]]; then
332*6ba2210eSMartin Matuska			print -u2 $out_msg
333*6ba2210eSMartin Matuska			_printerror "$@" "message in stderr" \
334*6ba2210eSMartin Matuska				" exited $status"
335*6ba2210eSMartin Matuska			status=1
336*6ba2210eSMartin Matuska		else
337*6ba2210eSMartin Matuska			[[ -n $LOGAPI_DEBUG ]] && cat $logfile
338*6ba2210eSMartin Matuska			_printsuccess "$@"
339*6ba2210eSMartin Matuska		fi
340*6ba2210eSMartin Matuska	fi
341*6ba2210eSMartin Matuska	_recursive_output $logfile "false"
342*6ba2210eSMartin Matuska	return $status
343*6ba2210eSMartin Matuska}
344*6ba2210eSMartin Matuska
345eda14cbcSMatt Macy# Set an exit handler
346eda14cbcSMatt Macy#
347eda14cbcSMatt Macy# $@ - function(s) to perform on exit
348eda14cbcSMatt Macy
349eda14cbcSMatt Macyfunction log_onexit
350eda14cbcSMatt Macy{
351eda14cbcSMatt Macy	_CLEANUP=("$*")
352eda14cbcSMatt Macy}
353eda14cbcSMatt Macy
354eda14cbcSMatt Macy# Push an exit handler on the cleanup stack
355eda14cbcSMatt Macy#
356eda14cbcSMatt Macy# $@ - function(s) to perform on exit
357eda14cbcSMatt Macy
358eda14cbcSMatt Macyfunction log_onexit_push
359eda14cbcSMatt Macy{
360eda14cbcSMatt Macy	_CLEANUP+=("$*")
361eda14cbcSMatt Macy}
362eda14cbcSMatt Macy
363eda14cbcSMatt Macy# Pop an exit handler off the cleanup stack
364eda14cbcSMatt Macy
365eda14cbcSMatt Macyfunction log_onexit_pop
366eda14cbcSMatt Macy{
367eda14cbcSMatt Macy	_CLEANUP=("${_CLEANUP[@]:0:${#_CLEANUP[@]}-1}")
368eda14cbcSMatt Macy}
369eda14cbcSMatt Macy
370eda14cbcSMatt Macy#
371eda14cbcSMatt Macy# Exit functions
372eda14cbcSMatt Macy#
373eda14cbcSMatt Macy
374eda14cbcSMatt Macy# Perform cleanup and exit $STF_PASS
375eda14cbcSMatt Macy#
376eda14cbcSMatt Macy# $@ - message text
377eda14cbcSMatt Macy
378eda14cbcSMatt Macyfunction log_pass
379eda14cbcSMatt Macy{
380eda14cbcSMatt Macy	_endlog $STF_PASS "$@"
381eda14cbcSMatt Macy}
382eda14cbcSMatt Macy
383eda14cbcSMatt Macy# Perform cleanup and exit $STF_FAIL
384eda14cbcSMatt Macy#
385eda14cbcSMatt Macy# $@ - message text
386eda14cbcSMatt Macy
387eda14cbcSMatt Macyfunction log_fail
388eda14cbcSMatt Macy{
389eda14cbcSMatt Macy	_endlog $STF_FAIL "$@"
390eda14cbcSMatt Macy}
391eda14cbcSMatt Macy
392eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNRESOLVED
393eda14cbcSMatt Macy#
394eda14cbcSMatt Macy# $@ - message text
395eda14cbcSMatt Macy
396eda14cbcSMatt Macyfunction log_unresolved
397eda14cbcSMatt Macy{
398eda14cbcSMatt Macy	_endlog $STF_UNRESOLVED "$@"
399eda14cbcSMatt Macy}
400eda14cbcSMatt Macy
401eda14cbcSMatt Macy# Perform cleanup and exit $STF_NOTINUSE
402eda14cbcSMatt Macy#
403eda14cbcSMatt Macy# $@ - message text
404eda14cbcSMatt Macy
405eda14cbcSMatt Macyfunction log_notinuse
406eda14cbcSMatt Macy{
407eda14cbcSMatt Macy	_endlog $STF_NOTINUSE "$@"
408eda14cbcSMatt Macy}
409eda14cbcSMatt Macy
410eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNSUPPORTED
411eda14cbcSMatt Macy#
412eda14cbcSMatt Macy# $@ - message text
413eda14cbcSMatt Macy
414eda14cbcSMatt Macyfunction log_unsupported
415eda14cbcSMatt Macy{
416eda14cbcSMatt Macy	_endlog $STF_UNSUPPORTED "$@"
417eda14cbcSMatt Macy}
418eda14cbcSMatt Macy
419eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNTESTED
420eda14cbcSMatt Macy#
421eda14cbcSMatt Macy# $@ - message text
422eda14cbcSMatt Macy
423eda14cbcSMatt Macyfunction log_untested
424eda14cbcSMatt Macy{
425eda14cbcSMatt Macy	_endlog $STF_UNTESTED "$@"
426eda14cbcSMatt Macy}
427eda14cbcSMatt Macy
428eda14cbcSMatt Macy# Perform cleanup and exit $STF_UNINITIATED
429eda14cbcSMatt Macy#
430eda14cbcSMatt Macy# $@ - message text
431eda14cbcSMatt Macy
432eda14cbcSMatt Macyfunction log_uninitiated
433eda14cbcSMatt Macy{
434eda14cbcSMatt Macy	_endlog $STF_UNINITIATED "$@"
435eda14cbcSMatt Macy}
436eda14cbcSMatt Macy
437eda14cbcSMatt Macy# Perform cleanup and exit $STF_NORESULT
438eda14cbcSMatt Macy#
439eda14cbcSMatt Macy# $@ - message text
440eda14cbcSMatt Macy
441eda14cbcSMatt Macyfunction log_noresult
442eda14cbcSMatt Macy{
443eda14cbcSMatt Macy	_endlog $STF_NORESULT "$@"
444eda14cbcSMatt Macy}
445eda14cbcSMatt Macy
446eda14cbcSMatt Macy# Perform cleanup and exit $STF_WARNING
447eda14cbcSMatt Macy#
448eda14cbcSMatt Macy# $@ - message text
449eda14cbcSMatt Macy
450eda14cbcSMatt Macyfunction log_warning
451eda14cbcSMatt Macy{
452eda14cbcSMatt Macy	_endlog $STF_WARNING "$@"
453eda14cbcSMatt Macy}
454eda14cbcSMatt Macy
455eda14cbcSMatt Macy# Perform cleanup and exit $STF_TIMED_OUT
456eda14cbcSMatt Macy#
457eda14cbcSMatt Macy# $@ - message text
458eda14cbcSMatt Macy
459eda14cbcSMatt Macyfunction log_timed_out
460eda14cbcSMatt Macy{
461eda14cbcSMatt Macy	_endlog $STF_TIMED_OUT "$@"
462eda14cbcSMatt Macy}
463eda14cbcSMatt Macy
464eda14cbcSMatt Macy# Perform cleanup and exit $STF_OTHER
465eda14cbcSMatt Macy#
466eda14cbcSMatt Macy# $@ - message text
467eda14cbcSMatt Macy
468eda14cbcSMatt Macyfunction log_other
469eda14cbcSMatt Macy{
470eda14cbcSMatt Macy	_endlog $STF_OTHER "$@"
471eda14cbcSMatt Macy}
472eda14cbcSMatt Macy
473eda14cbcSMatt Macyfunction set_main_pid
474eda14cbcSMatt Macy{
475eda14cbcSMatt Macy	_MAINPID=$1
476eda14cbcSMatt Macy}
477eda14cbcSMatt Macy
478eda14cbcSMatt Macy#
479eda14cbcSMatt Macy# Internal functions
480eda14cbcSMatt Macy#
481eda14cbcSMatt Macy
482eda14cbcSMatt Macy# Execute custom callback scripts on test failure
483eda14cbcSMatt Macy#
484eda14cbcSMatt Macy# callback script paths are stored in TESTFAIL_CALLBACKS, delimited by ':'.
485eda14cbcSMatt Macy
486eda14cbcSMatt Macyfunction _execute_testfail_callbacks
487eda14cbcSMatt Macy{
488eda14cbcSMatt Macy	typeset callback
489eda14cbcSMatt Macy
490eda14cbcSMatt Macy	print "$TESTFAIL_CALLBACKS:" | while read -d ":" callback; do
491eda14cbcSMatt Macy		if [[ -n "$callback" ]] ; then
492eda14cbcSMatt Macy			log_note "Performing test-fail callback ($callback)"
493eda14cbcSMatt Macy			$callback
494eda14cbcSMatt Macy		fi
495eda14cbcSMatt Macy	done
496eda14cbcSMatt Macy}
497eda14cbcSMatt Macy
498eda14cbcSMatt Macy# Perform cleanup and exit
499eda14cbcSMatt Macy#
500eda14cbcSMatt Macy# $1 - stf exit code
501eda14cbcSMatt Macy# $2-$n - message text
502eda14cbcSMatt Macy
503eda14cbcSMatt Macyfunction _endlog
504eda14cbcSMatt Macy{
505eda14cbcSMatt Macy	typeset logfile="/tmp/log.$$"
506eda14cbcSMatt Macy	_recursive_output $logfile
507eda14cbcSMatt Macy
508eda14cbcSMatt Macy	typeset exitcode=$1
509eda14cbcSMatt Macy	shift
510eda14cbcSMatt Macy	(( ${#@} > 0 )) && _printline "$@"
511eda14cbcSMatt Macy
512eda14cbcSMatt Macy	#
513eda14cbcSMatt Macy	# If we're running in a subshell then just exit and let
514eda14cbcSMatt Macy	# the parent handle the failures
515eda14cbcSMatt Macy	#
516eda14cbcSMatt Macy	if [[ -n "$_MAINPID" && $$ != "$_MAINPID" ]]; then
517eda14cbcSMatt Macy		log_note "subshell exited: "$_MAINPID
518eda14cbcSMatt Macy		exit $exitcode
519eda14cbcSMatt Macy	fi
520eda14cbcSMatt Macy
521eda14cbcSMatt Macy	if [[ $exitcode == $STF_FAIL ]] ; then
522eda14cbcSMatt Macy		_execute_testfail_callbacks
523eda14cbcSMatt Macy	fi
524eda14cbcSMatt Macy
525eda14cbcSMatt Macy	typeset stack=("${_CLEANUP[@]}")
526eda14cbcSMatt Macy	log_onexit ""
527eda14cbcSMatt Macy	typeset i=${#stack[@]}
528eda14cbcSMatt Macy	while (( i-- )); do
529eda14cbcSMatt Macy		typeset cleanup="${stack[i]}"
530eda14cbcSMatt Macy		log_note "Performing local cleanup via log_onexit ($cleanup)"
531eda14cbcSMatt Macy		$cleanup
532eda14cbcSMatt Macy	done
533eda14cbcSMatt Macy
534eda14cbcSMatt Macy	exit $exitcode
535eda14cbcSMatt Macy}
536eda14cbcSMatt Macy
537eda14cbcSMatt Macy# Output a formatted line
538eda14cbcSMatt Macy#
539eda14cbcSMatt Macy# $@ - message text
540eda14cbcSMatt Macy
541eda14cbcSMatt Macyfunction _printline
542eda14cbcSMatt Macy{
543eda14cbcSMatt Macy	print "$@"
544eda14cbcSMatt Macy}
545eda14cbcSMatt Macy
546eda14cbcSMatt Macy# Output an error message
547eda14cbcSMatt Macy#
548eda14cbcSMatt Macy# $@ - message text
549eda14cbcSMatt Macy
550eda14cbcSMatt Macyfunction _printerror
551eda14cbcSMatt Macy{
552eda14cbcSMatt Macy	_printline ERROR: "$@"
553eda14cbcSMatt Macy}
554eda14cbcSMatt Macy
555eda14cbcSMatt Macy# Output a success message
556eda14cbcSMatt Macy#
557eda14cbcSMatt Macy# $@ - message text
558eda14cbcSMatt Macy
559eda14cbcSMatt Macyfunction _printsuccess
560eda14cbcSMatt Macy{
561eda14cbcSMatt Macy	_printline SUCCESS: "$@"
562eda14cbcSMatt Macy}
563eda14cbcSMatt Macy
564eda14cbcSMatt Macy# Output logfiles recursively
565eda14cbcSMatt Macy#
566eda14cbcSMatt Macy# $1 - start file
567eda14cbcSMatt Macy# $2 - indicate whether output the start file itself, default as yes.
568eda14cbcSMatt Macy
569eda14cbcSMatt Macyfunction _recursive_output #logfile
570eda14cbcSMatt Macy{
571eda14cbcSMatt Macy	typeset logfile=$1
572eda14cbcSMatt Macy
573eda14cbcSMatt Macy	while [[ -e $logfile ]]; do
574eda14cbcSMatt Macy		if [[ -z $2 || $logfile != $1 ]]; then
575eda14cbcSMatt Macy			cat $logfile
576eda14cbcSMatt Macy		fi
577eda14cbcSMatt Macy		rm -f $logfile
578eda14cbcSMatt Macy		logfile="$logfile.$$"
579eda14cbcSMatt Macy        done
580eda14cbcSMatt Macy}
581