Skip to content

Commit

Permalink
Merge pull request #17020 from edsantiago/logformatter_tweaks
Browse files Browse the repository at this point in the history
logformatter: nicer formatting for bats failures
  • Loading branch information
openshift-merge-robot authored Jan 6, 2023
2 parents 6f4eafe + a7f5393 commit ea63b27
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
84 changes: 48 additions & 36 deletions contrib/cirrus/logformatter
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use warnings;

(our $ME = $0) =~ s|.*/||;

our $VERSION = '0.1';
our $VERSION = '0.2';

# Autoflush stdout
$| = 1;
Expand All @@ -40,20 +40,20 @@ table.synopsis { border: none; border-collapse: collapse; margin-left: 2em; marg
.synopsis td { font-weight: bold; font-size: 120%; font-family: monospace; }
/* test results */
.testname { font-size: 125%; color: #444; }
.boring { color: #999; }
.timestamp { color: #999; }
.log-debug { color: #999; }
.log-info { color: #333; }
.log-warn { color: #f60; }
.log-error { color: #900; font-weight: bold; }
.log-skip { color: #F90; }
.log-slow { background: #FF0; color: #000; font-weight: bold; }
.subtest { background: #eee; }
.subsubtest { color: #F39; font-weight: bold; }
.string { color: #00c; }
.command { font-weight: bold; color: #000; }
.changed { color: #000; font-weight: bold; }
.testname { font-size: 125%; color: #444; }
.boring { color: #999; }
.timestamp { color: #999; }
.log-debug { color: #999; }
.log-info { color: #333; }
.log-warning { color: #f60; }
.log-error { color: #900; font-weight: bold; }
.log-skip { color: #F90; }
.log-slow { background: #FF0; color: #000; font-weight: bold; }
.subtest { background: #eee; }
.subsubtest { color: #F39; font-weight: bold; }
.string { color: #00c; }
.command { font-weight: bold; color: #000; }
.changed { color: #000; font-weight: bold; }
/* links to source files: not as prominent as links to errors */
a.codelink:link { color: #000; }
Expand All @@ -68,8 +68,8 @@ a.timing { text-decoration: none; }
.bats-passed { color: #393; }
.bats-failed { color: #F00; font-weight: bold; }
.bats-skipped { color: #F90; }
.bats-log { color: #900; }
.bats-log-esm { color: #b00; font-weight: bold; }
.bats-log { color: #933; }
.bats-log-failblock { color: #b00; background-color: #fee; display: inline-flex; margin: 0 -500%; padding: 0 500% !important; }
.bats-summary { font-size: 150%; }
Expand Down Expand Up @@ -251,6 +251,19 @@ END_HTML
$previous_timestamp = $timestamp;
}

# Helper function for printing a formatted line. This should always
# be followed by 'next LINE'.
my $print_line = sub {
my $css = shift;

print { $out_fh } "<span class=\"timestamp\">$timestamp</span>"
if $timestamp;
print { $out_fh } "<span class='$css'>" if $css;
print { $out_fh } $line;
print { $out_fh } "</span>" if $css;
print { $out_fh } "\n";
};

# Try to identify the git commit we're working with...
if ($line =~ m!/define.gitCommit=([0-9a-f]+)!) {
$git_commit = $1;
Expand All @@ -272,6 +285,13 @@ END_HTML
$cirrus_task = $1;
}

# logrus messages, always highlighted
# 1 2 2 13 34 4 5 56 6 7 78 8
if ($line =~ /^(\s*(#\s)?)(time=)(.*) (level=)(\S+) (msg=)(.*)/) {
my $span = "<span class='log-$6'>";
$line = "$1$3$span$4</span> $5$span$6</span> $7$span$8</span>";
}

# BATS handling. This will recognize num_tests both at start and end
if ($line =~ /^1\.\.(\d+)$/) {
$looks_like_bats = 1;
Expand Down Expand Up @@ -305,9 +325,7 @@ END_HTML
if ($line =~ /^\s*(OK|FAILED)\s+\(/) {
undef $looks_like_python;
my $css = ($1 eq 'OK' ? 'passed' : 'failed');
print { $out_fh } "<span class=\"timestamp\">$timestamp</span>"
if $timestamp;
print { $out_fh } "<span class='bats-$css'>", $line, "</span>\n";
$print_line->("bats-$css");
next LINE;
}
}
Expand All @@ -320,11 +338,11 @@ END_HTML
$line =~ s{^(#\s+(#|\$)\s+)(\S+/)(podman\S*)(\s.*)}
{$1<b><span title="$3$4">$4</span>$5</b>};

if ($line =~ /^ok\s.*\s# skip/) { $css = 'skipped' }
elsif ($line =~ /^ok\s/) { $css = 'passed' }
elsif ($line =~ /^not\s+ok\s/) { $css = 'failed' }
elsif ($line =~ /^#\s#\|\s/) { $css = 'log-esm' }
elsif ($line =~ /^#\s/) { $css = 'log' }
if ($line =~ /^ok\s.*\s# skip/) { $css = 'skipped' }
elsif ($line =~ /^ok\s/) { $css = 'passed' }
elsif ($line =~ /^not\s+ok\s/) { $css = 'failed' }
elsif ($line =~ /^# #(\/v|\| |\\\^)/) { $css = 'log-failblock' }
elsif ($line =~ /^#\s/) { $css = 'log' }

# Link to source file. This is ugly: we have to hardcode 'podman'
# and 'test/system' because there's no way to get them from log.
Expand All @@ -339,14 +357,11 @@ END_HTML

push @{$bats_count{__fail_list}}, [ $2, $3 ] if $1;
}
$line = "<span class='bats-$css'>$line</span>";

$bats_count{$css}++;
$css = "bats-$css";
}

print { $out_fh } "<span class=\"timestamp\">$timestamp</span>"
if $timestamp;
print { $out_fh } $line, "\n";
$print_line->($css);
next LINE;
}
elsif ($looks_like_python) {
Expand All @@ -362,13 +377,10 @@ END_HTML
}

if ($css) {
$line = "<span class='bats-$css'>$line</span>";

$bats_count{$css}++;
$css = "bats-$css";
}
print { $out_fh } "<span class=\"timestamp\">$timestamp</span>"
if $timestamp;
print { $out_fh } $line, "\n";
$print_line->($css);
next LINE;
}

Expand Down Expand Up @@ -439,7 +451,7 @@ END_HTML
}
}
elsif ($line =~ /^Error:/ || $line =~ / level=(warning|error) /) {
$line = "<span class='log-warn'>" . $line . "</span>";
$line = "<span class='log-warning'>" . $line . "</span>";
}
elsif ($line =~ /^panic:/) {
$line = "<span class='log-error'>" . $line . "</span>";
Expand Down
8 changes: 6 additions & 2 deletions contrib/cirrus/logformatter.t
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ not ok 3 fail
# from function `expect_output' in file ./helpers.bash, line 370,
# in test file ./run.bats, line 786)
# $ /path/to/podman foo -bar
# time="2023-01-05T15:15:20Z" level=debug msg="this is debug"
# time="2023-01-05T15:15:20Z" level=warning msg="this is warning"
# #| FAIL: exit code is 123; expected 321
ok 4 blah
>>>
Expand All @@ -106,7 +108,9 @@ ok 4 blah
<span class='bats-log'># from function `expect_output&#39; in file ./<a class="codelink" href="https://github.com/containers/podman/blob/ceci-nest-pas-une-sha/test/system/helpers.bash#L370">helpers.bash, line 370</a>,</span>
<span class='bats-log'># in test file ./<a class="codelink" href="https://github.com/containers/podman/blob/ceci-nest-pas-une-sha/test/system/run.bats#L786">run.bats, line 786</a>)</span>
<span class='bats-log'># $ <b><span title="/path/to/podman">podman</span> foo -bar</b></span>
<span class='bats-log-esm'># #| FAIL: exit code is 123; expected 321</span>
<span class='bats-log'># time=<span class='log-debug'>&quot;2023-01-05T15:15:20Z&quot;</span> level=<span class='log-debug'>debug</span> msg=<span class='log-debug'>&quot;this is debug&quot;</span></span>
<span class='bats-log'># time=<span class='log-warning'>&quot;2023-01-05T15:15:20Z&quot;</span> level=<span class='log-warning'>warning</span> msg=<span class='log-warning'>&quot;this is warning&quot;</span></span>
<span class='bats-log-failblock'># #| FAIL: exit code is 123; expected 321</span>
<span class='bats-passed'><a name='t--00004'>ok 4 blah</a></span>
<hr/><span class='bats-summary'>Summary: <span class='bats-passed'>2 Passed</span>, <span class='bats-failed'>1 Failed</span>, <span class='bats-skipped'>1 Skipped</span>. Total tests: 4</span>
Expand Down Expand Up @@ -209,7 +213,7 @@ $SCRIPT_BASE/integration_test.sh |&amp; ${TIMESTAMP}
--tmpdir /tmp/podman_test553496330
--events-backend file
--storage-driver vfs">[options]</span><b> pod restart 4810be0cfbd42241e349dbe7d50fbc54405cd320a6637c65fd5323f34d64af89</b>
<span class="timestamp"> </span><span class='log-warn'>Error: no containers in pod 4810be0cfbd42241e349dbe7d50fbc54405cd320a6637c65fd5323f34d64af89 have no dependencies, cannot start pod: no such container</span>
<span class="timestamp"> </span><span class='log-warning'>Error: no containers in pod 4810be0cfbd42241e349dbe7d50fbc54405cd320a6637c65fd5323f34d64af89 have no dependencies, cannot start pod: no such container</span>
<span class="timestamp"> </span>output:
<span class="timestamp"> </span>[AfterEach] Podman pod restart
<span class="timestamp"> </span> /var/tmp/go/src/github.com<a class="codelink" href='https://github.com/containers/podman/blob/40f5d8b1becd381c4e8283ed3940d09193e4fe06/test/e2e/pod_restart_test.go#L28'>/containers/podman/test/e2e/pod_restart_test.go:28</a>
Expand Down

0 comments on commit ea63b27

Please sign in to comment.