source: branches/profile/UNIT_TESTER/reporter.pl

Last change on this file was 12414, checked in by westram, 10 years ago
  • only perform unit tests when test-code, needed library or config has changed
    • create .done file for each successful unit
    • use separate directories for all/non-slow tests
    • define some special dependencies
      • for arb_test.o (depends on called command line tools)
      • for unit-tests connection to pt_server
  • only dump log if tests were skipped and slow tests are performed (shows disabled tests)
  • Property svn:executable set to *
File size: 12.0 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6# --------------------------------------------------------------------------------
7
8my $logdirectory = undef;
9my $slow_delay   = undef;
10
11# --------------------------------------------------------------------------------
12
13sub getModtime($) {
14  my ($file_or_dir) = @_;
15  if (-f $file_or_dir or -d $file_or_dir) {
16    my @st = stat($file_or_dir);
17    if (not @st) { die "can't stat '$file_or_dir' ($!)"; }
18    return $st[9];
19  }
20  return 0; # does not exist -> use epoch
21}
22sub getAge($) { my ($file_or_dir) = @_; return time-getModtime($file_or_dir); }
23
24# --------------------------------------------------------------------------------
25
26# when file $slow_stamp exists, slow tests get skipped (see sym2testcode.pl@SkipSlow)
27my $slow_stamp = 'skipslow.stamp';
28my $slow_age   = getAge($slow_stamp); # seconds since of last successful slow test
29
30sub shall_run_slow() { return (($slow_delay==0) or ($slow_age>($slow_delay*60))); }
31
32sub slow_init() {
33  if (shall_run_slow()) {
34    print "Running SLOW tests\n";
35    unlink($slow_stamp);
36  }
37  else {
38    print "Skipping SLOW tests\n";
39  }
40}
41
42sub slow_cleanup($) {
43  my ($tests_failed) = @_;
44
45  if (shall_run_slow() and not $tests_failed) {
46    system("touch $slow_stamp");
47  }
48}
49
50# --------------------------------------------------------------------------------
51
52sub get_existing_logs() {
53  my @logs;
54  opendir(LOGDIR,$logdirectory) || die "can't read directory '$logdirectory' (Reason: $!)";
55  foreach (readdir(LOGDIR)) {
56    if (/\.log$/o) { push @logs, $logdirectory.'/'.$_; }
57  }
58  closedir(LOGDIR);
59  return @logs;
60}
61
62sub do_init() {
63  if (-d $logdirectory) {
64    my @logs = get_existing_logs();
65    foreach (@logs) { unlink($_) || die "can't unlink '$_' (Reason: $!)"; }
66  }
67  slow_init();
68  return undef;
69}
70# --------------------------------------------------------------------------------
71
72my $expand_list_read = 0;
73my @expand_list = ();
74
75sub read_expand_list() {
76  my $expand_list = "../SOURCE_TOOLS/valgrind2grep.lst";
77
78  if (not -f $expand_list) {
79    my $cmd = '(cd ../SOURCE_TOOLS; make valgrind_update)';
80    system($cmd)==0 || die "failed to execute '$cmd' (Reason: $?)";
81  }
82
83  my $dir = `pwd`;
84  open(LIST,'<'.$expand_list) || die "can't read '$expand_list' (Reason: $!) in dir '$dir'";
85  my $line;
86  while (defined($line=<LIST>)) {
87    chomp($line);
88    push @expand_list, $line;
89  }
90  close(LIST);
91
92  $expand_list_read = 1;
93}
94
95my %expanded = (); # key=$filename, value=ref to array of possible expanded filenames.
96
97sub get_expanded_filenames($) {
98  my ($file) = @_;
99
100  my $found_r = $expanded{$file};
101  if (not defined $found_r) {
102    if ($expand_list_read==0) { read_expand_list(); }
103    my @expanded = ();
104    foreach (@expand_list) { if (/\/$file$/) { push @expanded, $_; } }
105    $expanded{$file} = \@expanded;
106    $found_r = $expanded{$file};
107  }
108  return @$found_r;
109}
110
111sub print_expand_pathless_messages($) {
112  my ($line) = @_;
113  chomp($line);
114  if ($line =~ /^([a-z0-9_\.]+):([0-9:]+):/oi) {
115    my ($file,$lineCol,$rest) = ($1,$2,$');
116    my @expanded = get_expanded_filenames($file);
117
118    if (scalar(@expanded)==0) {
119      print "$file:$lineCol: [unknown -> call 'make valgrind_update'] $rest\n";
120    }
121    else {
122      foreach (@expanded) {
123        print "$_:$lineCol: $rest\n";
124      }
125    }
126  }
127  else {
128    print $line."\n";
129  }
130}
131
132# --------------------------------------------------------------------------------
133
134sub dump_junitlog(\@) {
135  my ($content_r) = @_;
136  my $logfile = "logs/junit_log.xml"; # see also Makefile.suite@JUNITLOGNAME
137  open(JLOG,'>'.$logfile) || die "can't write '$logfile' (Reason: $!)";
138  print JLOG "<testsuites>\n";
139  foreach (@$content_r) {
140    print JLOG $_."\n";
141  }
142  print JLOG "</testsuites>\n";
143  close(JLOG);
144}
145
146# --------------------------------------------------------------------------------
147
148my $tests    = 0;
149my $skipped  = 0;
150my $passed   = 0;
151my $failed   = 0;
152my $warnings = 0;
153my $elapsed  = 0;
154my $crashed  = 0;
155my $valgrind = 0;
156
157my %duration = (); # key=unit, value=ms
158
159sub parse_log($\@) {
160  # parse reports generated by UnitTester.cxx@generateReport
161  my ($log,$junit_r) = @_;
162  open(LOG,$log) || die "can't open '$log' (Reason: $!)";
163
164  my $tests_this   = 0;
165  my $skipped_this = 0;
166  my $passedALL    = 0;
167  my $seenSummary  = 0;
168
169  my $curr_target        = undef;
170  my $last_error_message = undef;
171
172  my $unitName = 'unknownUnit';
173  if ($log =~ /\/([^\.\/]+)\.[^\/]+$/o) { $unitName = $1; }
174
175  my $dump_log = 0;
176
177  my @testcases   = ();
178  my $case_ok     = 0;
179  my $case_failed = 0;
180
181  while ($_ = <LOG>) {
182    chomp;
183    if (/^UnitTester:/) {
184      my $rest = $';
185      if ($rest =~ /^\s+\*\s+([A-Za-z0-9_]+)\s+=\s+([A-Z]*)/o) {
186        my ($testname,$result) = ($1,$2);
187        my $err = undef;
188        if ($result ne 'OK') {
189          if (defined $last_error_message) {
190            $err = $last_error_message;
191          }
192          else {
193            $err = 'unknown failure reason';
194          }
195        }
196        # append to junit log
197        my $testcase = "  <testcase name=\"$testname\" classname=\"$unitName.noclass\"";
198        if (defined $err) {
199          $testcase .= ">\n";
200          $testcase .= "   <error message=\"$err\"/>\n";
201          $testcase .= "  </testcase>";
202          $case_failed++;
203        }
204        else {
205          $testcase .= '/>';
206          $case_ok++;
207        }
208        push @testcases, $testcase;
209        $last_error_message = undef;
210      }
211
212      if (/tests=([0-9]+)/)   { $tests_this += $1; $seenSummary=1; }
213      if (/skipped=([0-9]+)/) {
214        $skipped_this += $1;
215        if (shall_run_slow()) {
216          $dump_log = 1; # @@@ TODO: should dump log only if warnings are enabled
217        }
218      }
219
220      if (/passed=([0-9]+)/)  { $passed += $1; }
221      if (/passed=ALL/)       { $passedALL = 1; }
222
223      if (/failed=([0-9]+)/)  { $failed += $1; $dump_log = 1; }
224      if (/warnings=([0-9]+)/)  { $warnings += $1; if ($failed==0) { $dump_log = 1; } }
225      if (/target=([^\s]+)/)  { $curr_target = $1; }
226      if (/time=([0-9.]+)/)   {
227        $elapsed += $1;
228        if (not defined $curr_target) { die "Don't know current target"; }
229        $duration{$curr_target} = $1;
230      }
231      if (/valgrind.*error/)  { $valgrind++; $dump_log = 1; }
232      if (/coverage/)  { $dump_log = 1; }
233    }
234    elsif (/^[^\s:]+:[0-9]+:\s+Error:\s+/o) {
235      if (not /\(details\sabove\)/) {
236        $last_error_message = $';
237      }
238    }
239    elsif (/^-+\s+(ARB-backtrace.*):$/) {
240      $last_error_message = $1;
241    }
242  }
243  close(LOG);
244
245  # write whole suite to junit log
246  {
247    my $case_all = $case_ok + $case_failed;
248    # my $stamp    = localtime;
249    my $stamp    = `date "+%Y-%m-%dT%T.%N%:z"`;
250    chomp($stamp);
251    push @$junit_r, " <testsuite name=\"$unitName\" tests=\"$case_all\" failures=\"$case_failed\" timestamp=\"$stamp\">";
252    foreach (@testcases) { push @$junit_r, $_; }
253    push @$junit_r, " </testsuite>";
254  }
255
256  if (not $seenSummary) { $dump_log = 1; }
257
258  if ($dump_log==1) {
259    open(LOG,$log) || die "can't open '$log' (Reason: $!)";
260    my $line;
261    while (defined($line=<LOG>)) { print_expand_pathless_messages($line); }
262    close(LOG);
263  }
264  else {
265    my $log_ptr = $log;
266    $log_ptr =~ s/^\./UNIT_TESTER/;
267    # print "Suppressing dispensable $log_ptr\n";
268  }
269
270  if (not $seenSummary) {
271    my $ARBHOME = $ENV{ARBHOME};
272    print "$ARBHOME/UNIT_TESTER/$log:1:0: Warning: No summary found in '$log' (maybe the test did not compile or crashed)\n";
273    $crashed++;
274  }
275
276  $tests   += $tests_this;
277  $skipped += $skipped_this;
278
279  if ($passedALL==1) { $passed += ($tests_this-$skipped_this); }
280}
281
282sub percent($$) {
283  my ($part,$all) = @_;
284  if ($all) {
285    my $percent = 100*$part/$all;
286    return sprintf("%5.1f%%", $percent);
287  }
288  else {
289    $part==0 || die;
290    return "  0.0%";
291  }
292}
293
294sub slow_note() {
295  return (shall_run_slow() ? "" : " (slow tests skipped)");
296}
297
298my $BigOk = <<EndOk;
299  __  __ _    _  _
300 /  \\(  / )  (_)( \\
301(  O ))  (    _  ) )
302 \\__/(__\\_)  (_)(_/
303EndOk
304
305my $BigFailed = <<EndFailed;
306 ____  __   __  __    ____  ____   _
307(  __)/ _\\ (  )(  )  (  __)(    \\ / \\
308 ) _)/    \\ )( / (_/\\ ) _)  ) D ( \\_/
309(__) \\_/\\_/(__)\\____/(____)(____/ (_)
310EndFailed
311
312
313sub readableDuration($) {
314  # result should not be longer than 9 characters! (5 chars value, space, 3 chars unit)
315  my ($ms) = @_;
316  if ($ms>5000) {
317    my $sec = $ms / 1000.0;
318    if ($sec>99) {
319      my $min = $sec / 60.0;
320      return sprintf("%5.2f min", $min);
321    }
322    return sprintf("%5.2f sec", $sec);
323  }
324  return sprintf("%5i ms ", $ms);
325}
326
327sub print_summary($) {
328  my ($tests_failed) = @_;
329  print "\n-------------------- [ Unit-test summary ] --------------------\n";
330
331  my @summary = ();
332
333  push @summary, sprintf(" Tests   : %5i", $tests);
334  push @summary, sprintf(" Skipped : %5i =%s%s", $skipped, percent($skipped,$tests), slow_note());
335  push @summary, sprintf(" Passed  : %5i =%s", $passed, percent($passed,$tests));
336  push @summary, sprintf(" Failed  : %5i =%s", $failed, percent($failed,$tests));
337  push @summary, sprintf(" Sum.dur.: %9s", readableDuration($elapsed));
338  {
339    my @slowest = sort {
340      $duration{$b} <=> $duration{$a};
341    } map {
342      if ((defined $_) and ($_ ne '') and $duration{$_}>0) { $_; }
343      else { ; }
344    } keys %duration;
345
346    my $show = scalar(@slowest);
347    if ($show>3) { $show = 3; }
348    if ($show>0) {
349      for (my $s=0; $s<$show; ++$s) {
350        my $slowunit = $slowest[$s];
351        push @summary, sprintf("%s%9s (%s)", ($s==0 ? " Max.dur.: " : "           "), readableDuration($duration{$slowunit}), $slowunit);
352      }
353    }
354  }
355  push @summary, sprintf(" Crashed : %5i units", $crashed);
356  push @summary, sprintf(" Warnings: %5i", $warnings);
357  if ($valgrind>0) {
358    push @summary, sprintf(" Valgrind: %5i failures", $valgrind);
359  }
360
361  my @big;
362  my $Big = $tests_failed ? $BigFailed : $BigOk;
363  @big= split '\n', $Big;
364
365  my $vOffset = scalar(@summary) - scalar(@big);
366  if ($vOffset<0) { $vOffset = 0; }
367
368  my $col = 0;
369  for (my $i=0; $i<scalar(@big); $i++) {
370    my $j = $i+$vOffset;
371    my $len = length($summary[$j]);
372    if ($len>$col) { $col = $len; }
373  }
374
375  $col += 6; # add horizontal offset
376
377  for (my $i=0; $i<scalar(@big); $i++) {
378    my $j = $i+$vOffset;
379    my $padded = $summary[$j];
380    my $len = length($padded);
381    while ($len<$col) { $padded .= ' '; $len++; }
382    $summary[$j] = $padded.$big[$i];
383  }
384
385  foreach (@summary) { print $_."\n"; }
386}
387
388sub do_report() {
389  my @junit = ();
390  my @logs = get_existing_logs();
391  foreach (@logs) {
392    parse_log($_,@junit);
393  }
394
395  dump_junitlog(@junit);
396
397  my $tests_failed = (($failed>0) or ($crashed>0) or ($valgrind>0));
398  print_summary($tests_failed);
399  slow_cleanup($tests_failed);
400  if ($tests_failed) {
401    die "tests failed\n";
402  }
403  return undef;
404}
405
406sub check_obsolete_restricts() {
407  my $restrict = $ENV{CHECK_RESTRICT};
408  if (not defined $restrict) {
409    print "Can't check restriction (empty)\n";
410  }
411  else {
412    $restrict = ':'.$restrict.':';
413    if ($restrict =~ /:(WINDOW|ARBDB|AWT|CORE):/) {
414      my $lib = $1;
415      my $msl = 'Makefile.setup.local';
416
417      print "UNIT_TESTER/$msl:1: Error: Obsolete restriction '$lib' (should be 'lib$lib')\n";
418      my $grepcmd = "grep -n \'RESTRICT_LIB.*=.*$lib\' $msl";
419      open(GREP,$grepcmd.'|') || die "can't execute '$grepcmd' (Reason: $?)";
420      foreach (<GREP>) {
421        print "UNIT_TESTER/$msl:$_";
422      }
423      die;
424    }
425  }
426}
427
428# --------------------------------------------------------------------------------
429
430sub main() {
431  my $error = undef;
432  my $cb    = undef;
433  {
434    my $args = scalar(@ARGV);
435    if ($args==3) {
436      my $command   = shift @ARGV;
437
438      if ($command eq 'init') { $cb = \&do_init; }
439      elsif ($command eq 'report') { $cb = \&do_report; }
440      else { $error = "Unknown command '$command'"; }
441
442      if (not $error) {
443        $logdirectory = shift @ARGV;
444        $slow_delay = shift @ARGV;
445      }
446    }
447    else {
448      $error = 'Wrong number of arguments';
449    }
450  }
451  if ($error) {
452    print "Usage: reporter.pl [init|report] logdirectory slow-delay\n";
453    print "       slow-delay    >0 => run slow tests only every slow-delay minutes\n";
454  }
455  else {
456    check_obsolete_restricts();
457    eval { $error = &$cb(); };
458    if ($@) { $error = $@; }
459  }
460  if ($error) { die "Error: ".$error; }
461}
462main();
Note: See TracBrowser for help on using the repository browser.