]> git.proxmox.com Git - mirror_zfs.git/blob - tests/test-runner/include/logapi.shlib
Add the ZFS Test Suite
[mirror_zfs.git] / tests / test-runner / include / logapi.shlib
1 #!/bin/ksh -p
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 #
24 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 #
27 # Copyright (c) 2012 by Delphix. All rights reserved.
28 #
29
30 . ${STF_TOOLS}/include/stf.shlib
31
32 # Output an assertion
33 #
34 # $@ - assertion text
35
36 function log_assert
37 {
38 _printline ASSERTION: "$@"
39 }
40
41 # Output a comment
42 #
43 # $@ - comment text
44
45 function log_note
46 {
47 _printline NOTE: "$@"
48 }
49
50 # Execute and print command with status where success equals non-zero result
51 #
52 # $@ - command to execute
53 #
54 # return 0 if command fails, otherwise return 1
55
56 function log_neg
57 {
58 log_neg_expect "" "$@"
59 return $?
60 }
61
62 # Execute a positive test and exit $STF_FAIL is test fails
63 #
64 # $@ - command to execute
65
66 function log_must
67 {
68 log_pos "$@"
69 (( $? != 0 )) && log_fail
70 }
71
72 # Execute a negative test and exit $STF_FAIL if test passes
73 #
74 # $@ - command to execute
75
76 function log_mustnot
77 {
78 log_neg "$@"
79 (( $? != 0 )) && log_fail
80 }
81
82 # Execute a negative test with keyword expected, and exit
83 # $STF_FAIL if test passes
84 #
85 # $1 - keyword expected
86 # $2-$@ - command to execute
87
88 function log_mustnot_expect
89 {
90 log_neg_expect "$@"
91 (( $? != 0 )) && log_fail
92 }
93
94 # Execute and print command with status where success equals non-zero result
95 # or output includes expected keyword
96 #
97 # $1 - keyword expected
98 # $2-$@ - command to execute
99 #
100 # return 0 if command fails, or the output contains the keyword expected,
101 # return 1 otherwise
102
103 function log_neg_expect
104 {
105 typeset out=""
106 typeset logfile="/tmp/log.$$"
107 typeset ret=1
108 typeset expect=$1
109 shift
110
111 while [[ -e $logfile ]]; do
112 logfile="$logfile.$$"
113 done
114
115 "$@" 2>$logfile
116 typeset status=$?
117 out="$CAT $logfile"
118
119 # unexpected status
120 if (( $status == 0 )); then
121 print -u2 $($out)
122 _printerror "$@" "unexpectedly exited $status"
123 # missing binary
124 elif (( $status == 127 )); then
125 print -u2 $($out)
126 _printerror "$@" "unexpectedly exited $status (File not found)"
127 # bus error - core dump
128 elif (( $status == 138 )); then
129 print -u2 $($out)
130 _printerror "$@" "unexpectedly exited $status (Bus Error)"
131 # segmentation violation - core dump
132 elif (( $status == 139 )); then
133 print -u2 $($out)
134 _printerror "$@" "unexpectedly exited $status (SEGV)"
135 else
136 $out | $EGREP -i "internal error|assertion failed" \
137 > /dev/null 2>&1
138 # internal error or assertion failed
139 if (( $? == 0 )); then
140 print -u2 $($out)
141 _printerror "$@" "internal error or assertion failure" \
142 " exited $status"
143 elif [[ -n $expect ]] ; then
144 $out | $GREP -i "$expect" > /dev/null 2>&1
145 if (( $? == 0 )); then
146 ret=0
147 else
148 print -u2 $($out)
149 _printerror "$@" "unexpectedly exited $status"
150 fi
151 else
152 ret=0
153 fi
154
155 if (( $ret == 0 )); then
156 [[ -n $LOGAPI_DEBUG ]] && print $($out)
157 _printsuccess "$@" "exited $status"
158 fi
159 fi
160 _recursive_output $logfile "false"
161 return $ret
162 }
163
164 # Execute and print command with status where success equals zero result
165 #
166 # $@ command to execute
167 #
168 # return command exit status
169
170 function log_pos
171 {
172 typeset out=""
173 typeset logfile="/tmp/log.$$"
174
175 while [[ -e $logfile ]]; do
176 logfile="$logfile.$$"
177 done
178
179 "$@" 2>$logfile
180 typeset status=$?
181 out="$CAT $logfile"
182
183 if (( $status != 0 )) ; then
184 print -u2 $($out)
185 _printerror "$@" "exited $status"
186 else
187 $out | $EGREP -i "internal error|assertion failed" \
188 > /dev/null 2>&1
189 # internal error or assertion failed
190 if [[ $? -eq 0 ]]; then
191 print -u2 $($out)
192 _printerror "$@" "internal error or assertion failure" \
193 " exited $status"
194 status=1
195 else
196 [[ -n $LOGAPI_DEBUG ]] && print $($out)
197 _printsuccess "$@"
198 fi
199 fi
200 _recursive_output $logfile "false"
201 return $status
202 }
203
204 # Set an exit handler
205 #
206 # $@ - function(s) to perform on exit
207
208 function log_onexit
209 {
210 _CLEANUP="$@"
211 }
212
213 #
214 # Exit functions
215 #
216
217 # Perform cleanup and exit $STF_PASS
218 #
219 # $@ - message text
220
221 function log_pass
222 {
223 _endlog $STF_PASS "$@"
224 }
225
226 # Perform cleanup and exit $STF_FAIL
227 #
228 # $@ - message text
229
230 function log_fail
231 {
232 _endlog $STF_FAIL "$@"
233 }
234
235 # Perform cleanup and exit $STF_UNRESOLVED
236 #
237 # $@ - message text
238
239 function log_unresolved
240 {
241 _endlog $STF_UNRESOLVED "$@"
242 }
243
244 # Perform cleanup and exit $STF_NOTINUSE
245 #
246 # $@ - message text
247
248 function log_notinuse
249 {
250 _endlog $STF_NOTINUSE "$@"
251 }
252
253 # Perform cleanup and exit $STF_UNSUPPORTED
254 #
255 # $@ - message text
256
257 function log_unsupported
258 {
259 _endlog $STF_UNSUPPORTED "$@"
260 }
261
262 # Perform cleanup and exit $STF_UNTESTED
263 #
264 # $@ - message text
265
266 function log_untested
267 {
268 _endlog $STF_UNTESTED "$@"
269 }
270
271 # Perform cleanup and exit $STF_UNINITIATED
272 #
273 # $@ - message text
274
275 function log_uninitiated
276 {
277 _endlog $STF_UNINITIATED "$@"
278 }
279
280 # Perform cleanup and exit $STF_NORESULT
281 #
282 # $@ - message text
283
284 function log_noresult
285 {
286 _endlog $STF_NORESULT "$@"
287 }
288
289 # Perform cleanup and exit $STF_WARNING
290 #
291 # $@ - message text
292
293 function log_warning
294 {
295 _endlog $STF_WARNING "$@"
296 }
297
298 # Perform cleanup and exit $STF_TIMED_OUT
299 #
300 # $@ - message text
301
302 function log_timed_out
303 {
304 _endlog $STF_TIMED_OUT "$@"
305 }
306
307 # Perform cleanup and exit $STF_OTHER
308 #
309 # $@ - message text
310
311 function log_other
312 {
313 _endlog $STF_OTHER "$@"
314 }
315
316 #
317 # Internal functions
318 #
319
320 # Perform cleanup and exit
321 #
322 # $1 - stf exit code
323 # $2-$n - message text
324
325 function _endlog
326 {
327 typeset logfile="/tmp/log.$$"
328 _recursive_output $logfile
329
330 if [[ -n $_CLEANUP ]] ; then
331 typeset cleanup=$_CLEANUP
332 log_onexit ""
333 log_note "Performing local cleanup via log_onexit ($cleanup)"
334 $cleanup
335 fi
336 typeset exitcode=$1
337 shift
338 (( ${#@} > 0 )) && _printline "$@"
339 exit $exitcode
340 }
341
342 # Output a formatted line
343 #
344 # $@ - message text
345
346 function _printline
347 {
348 print "$@"
349 }
350
351 # Output an error message
352 #
353 # $@ - message text
354
355 function _printerror
356 {
357 _printline ERROR: "$@"
358 }
359
360 # Output a success message
361 #
362 # $@ - message text
363
364 function _printsuccess
365 {
366 _printline SUCCESS: "$@"
367 }
368
369 # Output logfiles recursively
370 #
371 # $1 - start file
372 # $2 - indicate whether output the start file itself, default as yes.
373
374 function _recursive_output #logfile
375 {
376 typeset logfile=$1
377
378 while [[ -e $logfile ]]; do
379 if [[ -z $2 || $logfile != $1 ]]; then
380 $CAT $logfile
381 fi
382 $RM -f $logfile
383 logfile="$logfile.$$"
384 done
385 }