fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[lttng-tools.git] / tests / utils / tap-driver.sh
1 #! /bin/sh
2 # Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 # As a special exception to the GNU General Public License, if you
18 # distribute this file as part of a program that contains a
19 # configuration script generated by Autoconf, you may include it under
20 # the same distribution terms that you use for the rest of that program.
21
22 # This file is maintained in Automake, please report
23 # bugs to <bug-automake@gnu.org> or send patches to
24 # <automake-patches@gnu.org>.
25
26 scriptversion=2013-12-23.17; # UTC
27
28 # Make unconditional expansion of undefined variables an error. This
29 # helps a lot in preventing typo-related bugs.
30 set -u
31
32 me=tap-driver.sh
33
34 fatal ()
35 {
36 echo "$me: fatal: $*" >&2
37 exit 1
38 }
39
40 usage_error ()
41 {
42 echo "$me: $*" >&2
43 print_usage >&2
44 exit 2
45 }
46
47 print_usage ()
48 {
49 cat <<END
50 Usage:
51 tap-driver.sh --test-name=NAME --log-file=PATH --trs-file=PATH
52 [--expect-failure={yes|no}] [--color-tests={yes|no}]
53 [--enable-hard-errors={yes|no}] [--ignore-exit]
54 [--diagnostic-string=STRING] [--merge|--no-merge]
55 [--comments|--no-comments] [--post-script] [--] TEST-COMMAND
56 The '--test-name', '-log-file' and '--trs-file' options are mandatory.
57 END
58 }
59
60 # TODO: better error handling in option parsing (in particular, ensure
61 # TODO: $log_file, $trs_file and $test_name are defined).
62 test_name= # Used for reporting.
63 log_file= # Where to save the result and output of the test script.
64 trs_file= # Where to save the metadata of the test run.
65 post_script= # Script to be run after the test.
66 expect_failure=0
67 color_tests=0
68 merge=0
69 ignore_exit=0
70 comments=0
71 diag_string='#'
72 while test $# -gt 0; do
73 case $1 in
74 --help) print_usage; exit $?;;
75 --version) echo "$me $scriptversion"; exit $?;;
76 --test-name) test_name=$2; shift;;
77 --log-file) log_file=$2; shift;;
78 --trs-file) trs_file=$2; shift;;
79 --color-tests) color_tests=$2; shift;;
80 --expect-failure) expect_failure=$2; shift;;
81 --enable-hard-errors) shift;; # No-op.
82 --merge) merge=1;;
83 --no-merge) merge=0;;
84 --ignore-exit) ignore_exit=1;;
85 --comments) comments=1;;
86 --no-comments) comments=0;;
87 --diagnostic-string) diag_string=$2; shift;;
88 --post-script) post_script=$2; shift;;
89 --) shift; break;;
90 -*) usage_error "invalid option: '$1'";;
91 esac
92 shift
93 done
94
95 test $# -gt 0 || usage_error "missing test command"
96
97 case $expect_failure in
98 yes) expect_failure=1;;
99 *) expect_failure=0;;
100 esac
101
102 if test $color_tests = yes; then
103 init_colors='
104 color_map["red"]="\e[0;31m" # Red.
105 color_map["grn"]="\e[0;32m" # Green.
106 color_map["lgn"]="\e[1;32m" # Light green.
107 color_map["blu"]="\e[1;34m" # Blue.
108 color_map["mgn"]="\e[0;35m" # Magenta.
109 color_map["std"]="\e[m" # No color.
110 color_for_result["ERROR"] = "mgn"
111 color_for_result["PASS"] = "grn"
112 color_for_result["XPASS"] = "red"
113 color_for_result["FAIL"] = "red"
114 color_for_result["XFAIL"] = "lgn"
115 color_for_result["SKIP"] = "blu"'
116 else
117 init_colors=''
118 fi
119
120 TIME_SCRIPT="$(realpath -e -- "$(dirname "$0")")/tap/clock"
121
122 # :; is there to work around a bug in bash 3.2 (and earlier) which
123 # does not always set '$?' properly on redirection failure.
124 # See the Autoconf manual for more details.
125 :;{
126 (
127 # Ignore common signals (in this subshell only!), to avoid potential
128 # problems with Korn shells. Some Korn shells are known to propagate
129 # to themselves signals that have killed a child process they were
130 # waiting for; this is done at least for SIGINT (and usually only for
131 # it, in truth). Without the `trap' below, such a behaviour could
132 # cause a premature exit in the current subshell, e.g., in case the
133 # test command it runs gets terminated by a SIGINT. Thus, the awk
134 # script we are piping into would never seen the exit status it
135 # expects on its last input line (which is displayed below by the
136 # last `echo $?' statement), and would thus die reporting an internal
137 # error.
138 # For more information, see the Autoconf manual and the threads:
139 # <http://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
140 # <http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/004121.html>
141 trap : 1 3 2 13 15
142 if test $merge -gt 0; then
143 exec 2>&1
144 else
145 exec 2>&3
146 fi
147 # Since stderr and stdout may go to the same file and the script
148 # may run in situations with different types of buffering, enforce
149 # line buffering for both FDs. This helps to prevent interleave between
150 # the outputs in the resulting file for half written lines, eg.
151 # ok 93 - Tes# PERROR - xxxx
152 # t some function
153 if [ "${LTTNG_TESTS_TAP_AUTOTIME:-}" != 0 ]; then
154 stdbuf -eL -oL -- "$@"
155 else
156 "$@"
157 fi
158 echo $?
159 ) | LC_ALL=C ${AM_TAP_AWK-awk} \
160 -v me="$me" \
161 -v test_script_name="$test_name" \
162 -v log_file="$log_file" \
163 -v trs_file="$trs_file" \
164 -v expect_failure="$expect_failure" \
165 -v merge="$merge" \
166 -v ignore_exit="$ignore_exit" \
167 -v comments="$comments" \
168 -v diag_string="$diag_string" \
169 -v time_script="${TIME_SCRIPT}" \
170 '
171 # TODO: the usages of "cat >&3" below could be optimized when using
172 # GNU awk, and/on on systems that supports /dev/fd/.
173
174 # Implementation note: in what follows, `result_obj` will be an
175 # associative array that (partly) simulates a TAP result object
176 # from the `TAP::Parser` perl module.
177
178 ## ----------- ##
179 ## FUNCTIONS ##
180 ## ----------- ##
181
182 function fatal(msg)
183 {
184 print me ": " msg | "cat >&2"
185 exit 1
186 }
187
188 function abort(where)
189 {
190 fatal("internal error " where)
191 }
192
193 # Convert a boolean to a "yes"/"no" string.
194 function yn(bool)
195 {
196 return bool ? "yes" : "no";
197 }
198
199 function add_test_result(result)
200 {
201 if (!test_results_index)
202 test_results_index = 0
203 test_results_list[test_results_index] = result
204 test_results_index += 1
205 test_results_seen[result] = 1;
206 }
207
208 # Whether the test script should be re-run by "make recheck".
209 function must_recheck()
210 {
211 for (k in test_results_seen)
212 if (k != "XFAIL" && k != "PASS" && k != "SKIP")
213 return 1
214 return 0
215 }
216
217 # Whether the content of the log file associated to this test should
218 # be copied into the "global" test-suite.log.
219 function copy_in_global_log()
220 {
221 for (k in test_results_seen)
222 if (k != "PASS")
223 return 1
224 return 0
225 }
226
227 function gettime_ns( r)
228 {
229 r = ""
230 if (time_script | getline r) {
231 r = r
232 }
233 close(time_script)
234 return sprintf("%f", r)
235 }
236
237 function get_global_test_result()
238 {
239 if ("ERROR" in test_results_seen)
240 return "ERROR"
241 if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
242 return "FAIL"
243 all_skipped = 1
244 for (k in test_results_seen)
245 if (k != "SKIP")
246 all_skipped = 0
247 if (all_skipped)
248 return "SKIP"
249 return "PASS";
250 }
251
252 function stringify_result_obj(result_obj)
253 {
254 if (result_obj["is_unplanned"] || result_obj["number"] != testno)
255 return "ERROR"
256
257 if (plan_seen == LATE_PLAN)
258 return "ERROR"
259
260 if (result_obj["directive"] == "TODO")
261 return result_obj["is_ok"] ? "XPASS" : "XFAIL"
262
263 if (result_obj["directive"] == "SKIP")
264 return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
265
266 if (length(result_obj["directive"]))
267 abort("in function stringify_result_obj()")
268
269 return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
270 }
271
272 function decorate_result(result)
273 {
274 color_name = color_for_result[result]
275 if (color_name)
276 return color_map[color_name] "" result "" color_map["std"]
277 # If we are not using colorized output, or if we do not know how
278 # to colorize the given result, we should return it unchanged.
279 return result
280 }
281
282 function report(result, details)
283 {
284 if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
285 {
286 msg = ": " test_script_name
287 add_test_result(result)
288 }
289 else if (result == "#")
290 {
291 msg = " " test_script_name ":"
292 }
293 else
294 {
295 abort("in function report()")
296 }
297 if (length(details))
298 msg = msg " " details
299 # Output on console might be colorized.
300 print decorate_result(result) msg
301 # Flush stdout after each test result, this is useful when stdout
302 # is buffered, for example in a CI system.
303 fflush()
304 # Log the result in the log file too, to help debugging (this is
305 # especially true when said result is a TAP error or "Bail out!").
306 print result msg | "cat >&3";
307 }
308
309 function testsuite_error(error_message)
310 {
311 report("ERROR", "- " error_message)
312 }
313
314 function handle_tap_result()
315 {
316 details = result_obj["number"];
317 if (length(result_obj["description"]))
318 details = details " " result_obj["description"]
319
320 if (plan_seen == LATE_PLAN)
321 {
322 details = details " # AFTER LATE PLAN";
323 }
324 else if (result_obj["is_unplanned"])
325 {
326 details = details " # UNPLANNED";
327 }
328 else if (result_obj["number"] != testno)
329 {
330 details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
331 details, testno);
332 }
333 else if (result_obj["directive"])
334 {
335 details = details " # " result_obj["directive"];
336 if (length(result_obj["explanation"]))
337 details = details " " result_obj["explanation"]
338 }
339
340 report(stringify_result_obj(result_obj), details)
341 }
342
343 # `skip_reason` should be empty whenever planned > 0.
344 function handle_tap_plan(planned, skip_reason)
345 {
346 planned += 0 # Avoid getting confused if, say, `planned` is "00"
347 if (length(skip_reason) && planned > 0)
348 abort("in function handle_tap_plan()")
349 if (plan_seen)
350 {
351 # Error, only one plan per stream is acceptable.
352 testsuite_error("multiple test plans")
353 return;
354 }
355 planned_tests = planned
356 # The TAP plan can come before or after *all* the TAP results; we speak
357 # respectively of an "early" or a "late" plan. If we see the plan line
358 # after at least one TAP result has been seen, assume we have a late
359 # plan; in this case, any further test result seen after the plan will
360 # be flagged as an error.
361 plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
362 # If testno > 0, we have an error ("too many tests run") that will be
363 # automatically dealt with later, so do not worry about it here. If
364 # $plan_seen is true, we have an error due to a repeated plan, and that
365 # has already been dealt with above. Otherwise, we have a valid "plan
366 # with SKIP" specification, and should report it as a particular kind
367 # of SKIP result.
368 if (planned == 0 && testno == 0)
369 {
370 if (length(skip_reason))
371 skip_reason = "- " skip_reason;
372 report("SKIP", skip_reason);
373 }
374 }
375
376 function extract_tap_comment(line)
377 {
378 if (index(line, diag_string) == 1)
379 {
380 # Strip leading `diag_string` from `line`.
381 line = substr(line, length(diag_string) + 1)
382 # And strip any leading and trailing whitespace left.
383 sub("^[ \t]*", "", line)
384 sub("[ \t]*$", "", line)
385 # Return what is left (if any).
386 return line;
387 }
388 return "";
389 }
390
391 # When this function is called, we know that line is a TAP result line,
392 # so that it matches the (perl) RE "^(not )?ok\b".
393 function setup_result_obj(line)
394 {
395 # Get the result, and remove it from the line.
396 result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
397 sub("^(not )?ok[ \t]*", "", line)
398
399 # If the result has an explicit number, get it and strip it; otherwise,
400 # automatically assing the next progresive number to it.
401 if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
402 {
403 match(line, "^[0-9]+")
404 # The final `+ 0` is to normalize numbers with leading zeros.
405 result_obj["number"] = substr(line, 1, RLENGTH) + 0
406 line = substr(line, RLENGTH + 1)
407 }
408 else
409 {
410 result_obj["number"] = testno
411 }
412
413 if (plan_seen == LATE_PLAN)
414 # No further test results are acceptable after a "late" TAP plan
415 # has been seen.
416 result_obj["is_unplanned"] = 1
417 else if (plan_seen && testno > planned_tests)
418 result_obj["is_unplanned"] = 1
419 else
420 result_obj["is_unplanned"] = 0
421
422 # Strip trailing and leading whitespace.
423 sub("^[ \t]*", "", line)
424 sub("[ \t]*$", "", line)
425
426 # This will have to be corrected if we have a "TODO"/"SKIP" directive.
427 result_obj["description"] = line
428 result_obj["directive"] = ""
429 result_obj["explanation"] = ""
430
431 if (index(line, "#") == 0)
432 return # No possible directive, nothing more to do.
433
434 # Directives are case-insensitive.
435 rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
436
437 # See whether we have the directive, and if yes, where.
438 pos = match(line, rx "$")
439 if (!pos)
440 pos = match(line, rx "[^a-zA-Z0-9_]")
441
442 # If there was no TAP directive, we have nothing more to do.
443 if (!pos)
444 return
445
446 # Let`s now see if the TAP directive has been escaped. For example:
447 # escaped: ok \# SKIP
448 # not escaped: ok \\# SKIP
449 # escaped: ok \\\\\# SKIP
450 # not escaped: ok \ # SKIP
451 if (substr(line, pos, 1) == "#")
452 {
453 bslash_count = 0
454 for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
455 bslash_count += 1
456 if (bslash_count % 2)
457 return # Directive was escaped.
458 }
459
460 # Strip the directive and its explanation (if any) from the test
461 # description.
462 result_obj["description"] = substr(line, 1, pos - 1)
463 # Now remove the test description from the line, that has been dealt
464 # with already.
465 line = substr(line, pos)
466 # Strip the directive, and save its value (normalized to upper case).
467 sub("^[ \t]*#[ \t]*", "", line)
468 result_obj["directive"] = toupper(substr(line, 1, 4))
469 line = substr(line, 5)
470 # Now get the explanation for the directive (if any), with leading
471 # and trailing whitespace removed.
472 sub("^[ \t]*", "", line)
473 sub("[ \t]*$", "", line)
474 result_obj["explanation"] = line
475 }
476
477 function get_test_exit_message(status)
478 {
479 if (status == 0)
480 return ""
481 if (status !~ /^[1-9][0-9]*$/)
482 abort("getting exit status")
483 if (status < 127)
484 exit_details = ""
485 else if (status == 127)
486 exit_details = " (command not found?)"
487 else if (status >= 128 && status <= 255)
488 exit_details = sprintf(" (terminated by signal %d?)", status - 128)
489 else if (status > 256 && status <= 384)
490 # We used to report an "abnormal termination" here, but some Korn
491 # shells, when a child process die due to signal number n, can leave
492 # in $? an exit status of 256+n instead of the more standard 128+n.
493 # Apparently, both behaviours are allowed by POSIX (2008), so be
494 # prepared to handle them both. See also Austing Group report ID
495 # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
496 exit_details = sprintf(" (terminated by signal %d?)", status - 256)
497 else
498 # Never seen in practice.
499 exit_details = " (abnormal termination)"
500 return sprintf("exited with status %d%s", status, exit_details)
501 }
502
503 function write_test_results()
504 {
505 print ":global-test-result: " get_global_test_result() > trs_file
506 print ":recheck: " yn(must_recheck()) > trs_file
507 print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
508 print ":time-taken: " sprintf("%f", gettime_ns() - TIME_START) > trs_file
509 for (i = 0; i < test_results_index; i += 1)
510 print ":test-result: " test_results_list[i] > trs_file
511 close(trs_file);
512 }
513
514 BEGIN {
515
516 ## ------- ##
517 ## SETUP ##
518 ## ------- ##
519
520 '"$init_colors"'
521
522 # Properly initialized once the TAP plan is seen.
523 planned_tests = 0
524
525 COOKED_PASS = expect_failure ? "XPASS": "PASS";
526 COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
527
528 # Enumeration-like constants to remember which kind of plan (if any)
529 # has been seen. It is important that NO_PLAN evaluates "false" as
530 # a boolean.
531 NO_PLAN = 0
532 EARLY_PLAN = 1
533 LATE_PLAN = 2
534
535 testno = 0 # Number of test results seen so far.
536 bailed_out = 0 # Whether a "Bail out!" directive has been seen.
537
538 # Whether the TAP plan has been seen or not, and if yes, which kind
539 # it is ("early" is seen before any test result, "late" otherwise).
540 plan_seen = NO_PLAN
541
542 TIME_START = gettime_ns();
543 ## --------- ##
544 ## PARSING ##
545 ## --------- ##
546
547 is_first_read = 1
548
549 while (1)
550 {
551 # Involutions required so that we are able to read the exit status
552 # from the last input line.
553 st = getline
554 if (st < 0) # I/O error.
555 fatal("I/O error while reading from input stream")
556 else if (st == 0) # End-of-input
557 {
558 if (is_first_read)
559 abort("in input loop: only one input line")
560 break
561 }
562 if (is_first_read)
563 {
564 is_first_read = 0
565 nextline = $0
566 continue
567 }
568 else
569 {
570 curline = nextline
571 nextline = $0
572 $0 = curline
573 }
574 # Copy any input line verbatim into the log file.
575 print | "cat >&3"
576 # Parsing of TAP input should stop after a "Bail out!" directive.
577 if (bailed_out)
578 continue
579
580 # TAP test result.
581 if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
582 {
583 testno += 1
584 setup_result_obj($0)
585 handle_tap_result()
586 }
587 # TAP plan (normal or "SKIP" without explanation).
588 else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
589 {
590 # The next two lines will put the number of planned tests in $0.
591 sub("^1\\.\\.", "")
592 sub("[^0-9]*$", "")
593 handle_tap_plan($0, "")
594 continue
595 }
596 # TAP "SKIP" plan, with an explanation.
597 else if ($0 ~ /^1\.\.0+[ \t]*#/)
598 {
599 # The next lines will put the skip explanation in $0, stripping
600 # any leading and trailing whitespace. This is a little more
601 # tricky in truth, since we want to also strip a potential leading
602 # "SKIP" string from the message.
603 sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
604 sub("[ \t]*$", "");
605 handle_tap_plan(0, $0)
606 }
607 # "Bail out!" magic.
608 # Older versions of prove and TAP::Harness (e.g., 3.17) did not
609 # recognize a "Bail out!" directive when preceded by leading
610 # whitespace, but more modern versions (e.g., 3.23) do. So we
611 # emulate the latter, "more modern" behaviour.
612 else if ($0 ~ /^[ \t]*Bail out!/)
613 {
614 bailed_out = 1
615 # Get the bailout message (if any), with leading and trailing
616 # whitespace stripped. The message remains stored in `$0`.
617 sub("^[ \t]*Bail out![ \t]*", "");
618 sub("[ \t]*$", "");
619 # Format the error message for the
620 bailout_message = "Bail out!"
621 if (length($0))
622 bailout_message = bailout_message " " $0
623 testsuite_error(bailout_message)
624 }
625 # Maybe we have too look for dianogtic comments too.
626 else if (comments != 0)
627 {
628 comment = extract_tap_comment($0);
629 if (length(comment))
630 report("#", comment);
631 }
632 }
633
634 ## -------- ##
635 ## FINISH ##
636 ## -------- ##
637
638 # A "Bail out!" directive should cause us to ignore any following TAP
639 # error, as well as a non-zero exit status from the TAP producer.
640 if (!bailed_out)
641 {
642 if (!plan_seen)
643 {
644 testsuite_error("missing test plan")
645 }
646 else if (planned_tests != testno)
647 {
648 bad_amount = testno > planned_tests ? "many" : "few"
649 testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
650 bad_amount, planned_tests, testno))
651 }
652 if (!ignore_exit)
653 {
654 # Fetch exit status from the last line.
655 exit_message = get_test_exit_message(nextline)
656 if (exit_message)
657 testsuite_error(exit_message)
658 }
659 }
660
661 write_test_results()
662
663 exit 0
664
665 } # End of "BEGIN" block.
666 '
667
668 # TODO: document that we consume the file descriptor 3 :-(
669 } 3>"$log_file"
670
671 test $? -eq 0 || fatal "I/O or internal error"
672
673 if test ! -z $post_script ; then
674 $post_script
675 test $? -eq 0 || fatal "Post script returned an error. See $log_file"
676 fi
677
678 # Local Variables:
679 # mode: shell-script
680 # sh-indentation: 2
681 # eval: (add-hook 'write-file-hooks 'time-stamp)
682 # time-stamp-start: "scriptversion="
683 # time-stamp-format: "%:y-%02m-%02d.%02H"
684 # time-stamp-time-zone: "UTC"
685 # time-stamp-end: "; # UTC"
686 # End:
This page took 0.042235 seconds and 4 git commands to generate.