Add kernel uid/gid contexts
[lttng-tools.git] / config / 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 # :; is there to work around a bug in bash 3.2 (and earlier) which
121 # does not always set '$?' properly on redirection failure.
122 # See the Autoconf manual for more details.
123 :;{
124 (
125 # Ignore common signals (in this subshell only!), to avoid potential
126 # problems with Korn shells. Some Korn shells are known to propagate
127 # to themselves signals that have killed a child process they were
128 # waiting for; this is done at least for SIGINT (and usually only for
129 # it, in truth). Without the `trap' below, such a behaviour could
130 # cause a premature exit in the current subshell, e.g., in case the
131 # test command it runs gets terminated by a SIGINT. Thus, the awk
132 # script we are piping into would never seen the exit status it
133 # expects on its last input line (which is displayed below by the
134 # last `echo $?' statement), and would thus die reporting an internal
135 # error.
136 # For more information, see the Autoconf manual and the threads:
137 # <http://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
138 # <http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/004121.html>
139 trap : 1 3 2 13 15
140 if test $merge -gt 0; then
141 exec 2>&1
142 else
143 exec 2>&3
144 fi
145 "$@"
146 echo $?
147 ) | LC_ALL=C ${AM_TAP_AWK-awk} \
148 -v me="$me" \
149 -v test_script_name="$test_name" \
150 -v log_file="$log_file" \
151 -v trs_file="$trs_file" \
152 -v expect_failure="$expect_failure" \
153 -v merge="$merge" \
154 -v ignore_exit="$ignore_exit" \
155 -v comments="$comments" \
156 -v diag_string="$diag_string" \
157 '
158 # TODO: the usages of "cat >&3" below could be optimized when using
159 # GNU awk, and/on on systems that supports /dev/fd/.
160
161 # Implementation note: in what follows, `result_obj` will be an
162 # associative array that (partly) simulates a TAP result object
163 # from the `TAP::Parser` perl module.
164
165 ## ----------- ##
166 ## FUNCTIONS ##
167 ## ----------- ##
168
169 function fatal(msg)
170 {
171 print me ": " msg | "cat >&2"
172 exit 1
173 }
174
175 function abort(where)
176 {
177 fatal("internal error " where)
178 }
179
180 # Convert a boolean to a "yes"/"no" string.
181 function yn(bool)
182 {
183 return bool ? "yes" : "no";
184 }
185
186 function add_test_result(result)
187 {
188 if (!test_results_index)
189 test_results_index = 0
190 test_results_list[test_results_index] = result
191 test_results_index += 1
192 test_results_seen[result] = 1;
193 }
194
195 # Whether the test script should be re-run by "make recheck".
196 function must_recheck()
197 {
198 for (k in test_results_seen)
199 if (k != "XFAIL" && k != "PASS" && k != "SKIP")
200 return 1
201 return 0
202 }
203
204 # Whether the content of the log file associated to this test should
205 # be copied into the "global" test-suite.log.
206 function copy_in_global_log()
207 {
208 for (k in test_results_seen)
209 if (k != "PASS")
210 return 1
211 return 0
212 }
213
214 function get_global_test_result()
215 {
216 if ("ERROR" in test_results_seen)
217 return "ERROR"
218 if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
219 return "FAIL"
220 all_skipped = 1
221 for (k in test_results_seen)
222 if (k != "SKIP")
223 all_skipped = 0
224 if (all_skipped)
225 return "SKIP"
226 return "PASS";
227 }
228
229 function stringify_result_obj(result_obj)
230 {
231 if (result_obj["is_unplanned"] || result_obj["number"] != testno)
232 return "ERROR"
233
234 if (plan_seen == LATE_PLAN)
235 return "ERROR"
236
237 if (result_obj["directive"] == "TODO")
238 return result_obj["is_ok"] ? "XPASS" : "XFAIL"
239
240 if (result_obj["directive"] == "SKIP")
241 return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
242
243 if (length(result_obj["directive"]))
244 abort("in function stringify_result_obj()")
245
246 return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
247 }
248
249 function decorate_result(result)
250 {
251 color_name = color_for_result[result]
252 if (color_name)
253 return color_map[color_name] "" result "" color_map["std"]
254 # If we are not using colorized output, or if we do not know how
255 # to colorize the given result, we should return it unchanged.
256 return result
257 }
258
259 function report(result, details)
260 {
261 if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
262 {
263 msg = ": " test_script_name
264 add_test_result(result)
265 }
266 else if (result == "#")
267 {
268 msg = " " test_script_name ":"
269 }
270 else
271 {
272 abort("in function report()")
273 }
274 if (length(details))
275 msg = msg " " details
276 # Output on console might be colorized.
277 print decorate_result(result) msg
278 # Flush stdout after each test result, this is useful when stdout
279 # is buffered, for example in a CI system.
280 fflush()
281 # Log the result in the log file too, to help debugging (this is
282 # especially true when said result is a TAP error or "Bail out!").
283 print result msg | "cat >&3";
284 }
285
286 function testsuite_error(error_message)
287 {
288 report("ERROR", "- " error_message)
289 }
290
291 function handle_tap_result()
292 {
293 details = result_obj["number"];
294 if (length(result_obj["description"]))
295 details = details " " result_obj["description"]
296
297 if (plan_seen == LATE_PLAN)
298 {
299 details = details " # AFTER LATE PLAN";
300 }
301 else if (result_obj["is_unplanned"])
302 {
303 details = details " # UNPLANNED";
304 }
305 else if (result_obj["number"] != testno)
306 {
307 details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
308 details, testno);
309 }
310 else if (result_obj["directive"])
311 {
312 details = details " # " result_obj["directive"];
313 if (length(result_obj["explanation"]))
314 details = details " " result_obj["explanation"]
315 }
316
317 report(stringify_result_obj(result_obj), details)
318 }
319
320 # `skip_reason` should be empty whenever planned > 0.
321 function handle_tap_plan(planned, skip_reason)
322 {
323 planned += 0 # Avoid getting confused if, say, `planned` is "00"
324 if (length(skip_reason) && planned > 0)
325 abort("in function handle_tap_plan()")
326 if (plan_seen)
327 {
328 # Error, only one plan per stream is acceptable.
329 testsuite_error("multiple test plans")
330 return;
331 }
332 planned_tests = planned
333 # The TAP plan can come before or after *all* the TAP results; we speak
334 # respectively of an "early" or a "late" plan. If we see the plan line
335 # after at least one TAP result has been seen, assume we have a late
336 # plan; in this case, any further test result seen after the plan will
337 # be flagged as an error.
338 plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
339 # If testno > 0, we have an error ("too many tests run") that will be
340 # automatically dealt with later, so do not worry about it here. If
341 # $plan_seen is true, we have an error due to a repeated plan, and that
342 # has already been dealt with above. Otherwise, we have a valid "plan
343 # with SKIP" specification, and should report it as a particular kind
344 # of SKIP result.
345 if (planned == 0 && testno == 0)
346 {
347 if (length(skip_reason))
348 skip_reason = "- " skip_reason;
349 report("SKIP", skip_reason);
350 }
351 }
352
353 function extract_tap_comment(line)
354 {
355 if (index(line, diag_string) == 1)
356 {
357 # Strip leading `diag_string` from `line`.
358 line = substr(line, length(diag_string) + 1)
359 # And strip any leading and trailing whitespace left.
360 sub("^[ \t]*", "", line)
361 sub("[ \t]*$", "", line)
362 # Return what is left (if any).
363 return line;
364 }
365 return "";
366 }
367
368 # When this function is called, we know that line is a TAP result line,
369 # so that it matches the (perl) RE "^(not )?ok\b".
370 function setup_result_obj(line)
371 {
372 # Get the result, and remove it from the line.
373 result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
374 sub("^(not )?ok[ \t]*", "", line)
375
376 # If the result has an explicit number, get it and strip it; otherwise,
377 # automatically assing the next progresive number to it.
378 if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
379 {
380 match(line, "^[0-9]+")
381 # The final `+ 0` is to normalize numbers with leading zeros.
382 result_obj["number"] = substr(line, 1, RLENGTH) + 0
383 line = substr(line, RLENGTH + 1)
384 }
385 else
386 {
387 result_obj["number"] = testno
388 }
389
390 if (plan_seen == LATE_PLAN)
391 # No further test results are acceptable after a "late" TAP plan
392 # has been seen.
393 result_obj["is_unplanned"] = 1
394 else if (plan_seen && testno > planned_tests)
395 result_obj["is_unplanned"] = 1
396 else
397 result_obj["is_unplanned"] = 0
398
399 # Strip trailing and leading whitespace.
400 sub("^[ \t]*", "", line)
401 sub("[ \t]*$", "", line)
402
403 # This will have to be corrected if we have a "TODO"/"SKIP" directive.
404 result_obj["description"] = line
405 result_obj["directive"] = ""
406 result_obj["explanation"] = ""
407
408 if (index(line, "#") == 0)
409 return # No possible directive, nothing more to do.
410
411 # Directives are case-insensitive.
412 rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
413
414 # See whether we have the directive, and if yes, where.
415 pos = match(line, rx "$")
416 if (!pos)
417 pos = match(line, rx "[^a-zA-Z0-9_]")
418
419 # If there was no TAP directive, we have nothing more to do.
420 if (!pos)
421 return
422
423 # Let`s now see if the TAP directive has been escaped. For example:
424 # escaped: ok \# SKIP
425 # not escaped: ok \\# SKIP
426 # escaped: ok \\\\\# SKIP
427 # not escaped: ok \ # SKIP
428 if (substr(line, pos, 1) == "#")
429 {
430 bslash_count = 0
431 for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
432 bslash_count += 1
433 if (bslash_count % 2)
434 return # Directive was escaped.
435 }
436
437 # Strip the directive and its explanation (if any) from the test
438 # description.
439 result_obj["description"] = substr(line, 1, pos - 1)
440 # Now remove the test description from the line, that has been dealt
441 # with already.
442 line = substr(line, pos)
443 # Strip the directive, and save its value (normalized to upper case).
444 sub("^[ \t]*#[ \t]*", "", line)
445 result_obj["directive"] = toupper(substr(line, 1, 4))
446 line = substr(line, 5)
447 # Now get the explanation for the directive (if any), with leading
448 # and trailing whitespace removed.
449 sub("^[ \t]*", "", line)
450 sub("[ \t]*$", "", line)
451 result_obj["explanation"] = line
452 }
453
454 function get_test_exit_message(status)
455 {
456 if (status == 0)
457 return ""
458 if (status !~ /^[1-9][0-9]*$/)
459 abort("getting exit status")
460 if (status < 127)
461 exit_details = ""
462 else if (status == 127)
463 exit_details = " (command not found?)"
464 else if (status >= 128 && status <= 255)
465 exit_details = sprintf(" (terminated by signal %d?)", status - 128)
466 else if (status > 256 && status <= 384)
467 # We used to report an "abnormal termination" here, but some Korn
468 # shells, when a child process die due to signal number n, can leave
469 # in $? an exit status of 256+n instead of the more standard 128+n.
470 # Apparently, both behaviours are allowed by POSIX (2008), so be
471 # prepared to handle them both. See also Austing Group report ID
472 # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
473 exit_details = sprintf(" (terminated by signal %d?)", status - 256)
474 else
475 # Never seen in practice.
476 exit_details = " (abnormal termination)"
477 return sprintf("exited with status %d%s", status, exit_details)
478 }
479
480 function write_test_results()
481 {
482 print ":global-test-result: " get_global_test_result() > trs_file
483 print ":recheck: " yn(must_recheck()) > trs_file
484 print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
485 for (i = 0; i < test_results_index; i += 1)
486 print ":test-result: " test_results_list[i] > trs_file
487 close(trs_file);
488 }
489
490 BEGIN {
491
492 ## ------- ##
493 ## SETUP ##
494 ## ------- ##
495
496 '"$init_colors"'
497
498 # Properly initialized once the TAP plan is seen.
499 planned_tests = 0
500
501 COOKED_PASS = expect_failure ? "XPASS": "PASS";
502 COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
503
504 # Enumeration-like constants to remember which kind of plan (if any)
505 # has been seen. It is important that NO_PLAN evaluates "false" as
506 # a boolean.
507 NO_PLAN = 0
508 EARLY_PLAN = 1
509 LATE_PLAN = 2
510
511 testno = 0 # Number of test results seen so far.
512 bailed_out = 0 # Whether a "Bail out!" directive has been seen.
513
514 # Whether the TAP plan has been seen or not, and if yes, which kind
515 # it is ("early" is seen before any test result, "late" otherwise).
516 plan_seen = NO_PLAN
517
518 ## --------- ##
519 ## PARSING ##
520 ## --------- ##
521
522 is_first_read = 1
523
524 while (1)
525 {
526 # Involutions required so that we are able to read the exit status
527 # from the last input line.
528 st = getline
529 if (st < 0) # I/O error.
530 fatal("I/O error while reading from input stream")
531 else if (st == 0) # End-of-input
532 {
533 if (is_first_read)
534 abort("in input loop: only one input line")
535 break
536 }
537 if (is_first_read)
538 {
539 is_first_read = 0
540 nextline = $0
541 continue
542 }
543 else
544 {
545 curline = nextline
546 nextline = $0
547 $0 = curline
548 }
549 # Copy any input line verbatim into the log file.
550 print | "cat >&3"
551 # Parsing of TAP input should stop after a "Bail out!" directive.
552 if (bailed_out)
553 continue
554
555 # TAP test result.
556 if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
557 {
558 testno += 1
559 setup_result_obj($0)
560 handle_tap_result()
561 }
562 # TAP plan (normal or "SKIP" without explanation).
563 else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
564 {
565 # The next two lines will put the number of planned tests in $0.
566 sub("^1\\.\\.", "")
567 sub("[^0-9]*$", "")
568 handle_tap_plan($0, "")
569 continue
570 }
571 # TAP "SKIP" plan, with an explanation.
572 else if ($0 ~ /^1\.\.0+[ \t]*#/)
573 {
574 # The next lines will put the skip explanation in $0, stripping
575 # any leading and trailing whitespace. This is a little more
576 # tricky in truth, since we want to also strip a potential leading
577 # "SKIP" string from the message.
578 sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
579 sub("[ \t]*$", "");
580 handle_tap_plan(0, $0)
581 }
582 # "Bail out!" magic.
583 # Older versions of prove and TAP::Harness (e.g., 3.17) did not
584 # recognize a "Bail out!" directive when preceded by leading
585 # whitespace, but more modern versions (e.g., 3.23) do. So we
586 # emulate the latter, "more modern" behaviour.
587 else if ($0 ~ /^[ \t]*Bail out!/)
588 {
589 bailed_out = 1
590 # Get the bailout message (if any), with leading and trailing
591 # whitespace stripped. The message remains stored in `$0`.
592 sub("^[ \t]*Bail out![ \t]*", "");
593 sub("[ \t]*$", "");
594 # Format the error message for the
595 bailout_message = "Bail out!"
596 if (length($0))
597 bailout_message = bailout_message " " $0
598 testsuite_error(bailout_message)
599 }
600 # Maybe we have too look for dianogtic comments too.
601 else if (comments != 0)
602 {
603 comment = extract_tap_comment($0);
604 if (length(comment))
605 report("#", comment);
606 }
607 }
608
609 ## -------- ##
610 ## FINISH ##
611 ## -------- ##
612
613 # A "Bail out!" directive should cause us to ignore any following TAP
614 # error, as well as a non-zero exit status from the TAP producer.
615 if (!bailed_out)
616 {
617 if (!plan_seen)
618 {
619 testsuite_error("missing test plan")
620 }
621 else if (planned_tests != testno)
622 {
623 bad_amount = testno > planned_tests ? "many" : "few"
624 testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
625 bad_amount, planned_tests, testno))
626 }
627 if (!ignore_exit)
628 {
629 # Fetch exit status from the last line.
630 exit_message = get_test_exit_message(nextline)
631 if (exit_message)
632 testsuite_error(exit_message)
633 }
634 }
635
636 write_test_results()
637
638 exit 0
639
640 } # End of "BEGIN" block.
641 '
642
643 # TODO: document that we consume the file descriptor 3 :-(
644 } 3>"$log_file"
645
646 test $? -eq 0 || fatal "I/O or internal error"
647
648 if test ! -z $post_script ; then
649 $post_script
650 test $? -eq 0 || fatal "Post script returned an error. See $log_file"
651 fi
652
653 # Local Variables:
654 # mode: shell-script
655 # sh-indentation: 2
656 # eval: (add-hook 'write-file-hooks 'time-stamp)
657 # time-stamp-start: "scriptversion="
658 # time-stamp-format: "%:y-%02m-%02d.%02H"
659 # time-stamp-time-zone: "UTC"
660 # time-stamp-end: "; # UTC"
661 # End:
This page took 0.055205 seconds and 4 git commands to generate.