#!/usr/bin/perl -w # Generate HTML summary page for a collection of GUI reports (with kpi.csv) # (C) 2020 Candela Technologies Inc. # use strict; use warnings; use diagnostics; use Carp; $SIG{ __DIE__ } = sub { Carp::confess( @_ ) }; $SIG{ __WARN__ } = sub { Carp::confess( @_ ) }; # Un-buffer output $| = 1; # use lib prepends to @INC, so put lower priority first # This is before run-time, so cannot condition this with normal 'if' logic. use lib '/home/lanforge/scripts'; use lib "../"; use lib "./"; use Getopt::Long; use HTML::Entities; our $dir = ""; our $notes = ""; our $gitlog = ""; our $dutgitlog = ""; our $title = "Automated test results."; ######################################################################## # Nothing to configure below here, most likely. ######################################################################## our $usage = <<"__EndOfUsage__"; $0 [ --dir directory-to-process --notes testbed-notes-file.html --dutgitlog dut-gitlog-output.html --gitlog gitlog-output.html ] < html_template.html Example: cat html-template | $0 --dir ~/tmp/results --title "My Title" --notes testbeds/my_testbed/testbed_notes.html --dutgitlog /tmp/dgitlog.html --gitlog /tmp/gitlog.html __EndOfUsage__ my $i = 0; my $show_help = 0; GetOptions ( 'dir|d=s' => \$::dir, 'notes|n=s' => \$::notes, 'gitlog|g=s' => \$::gitlog, 'dutgitlog=s' => \$::dutgitlog, 'title|t=s' => \$::title, 'help|h' => \$show_help, ) || die("$::usage"); if ($show_help) { print $usage; exit 0; } my $testbed_notes = ""; if (-f "$notes") { $testbed_notes .= "Test Bed Notes.
\n"; $testbed_notes .= `cat $notes`; } if (-f "$dutgitlog") { $testbed_notes .= "

\n"; $testbed_notes .= `cat $dutgitlog`; $testbed_notes .= "

\n"; } if (-f "$gitlog") { $testbed_notes .= "

\n"; $testbed_notes .= `cat $gitlog`; $testbed_notes .= "

\n"; } $testbed_notes .= "

Top lanforge-scripts commits.

\n";
$testbed_notes .= `git log -n 8 --oneline`;
$testbed_notes .= "
\n"; chdir($dir); my @files = `ls`; chomp(@files); my $line; # Find some html helpers and copy them to current dir. foreach $line (@files) { if ( -d $line) { if ( -f "$line/canvil.ico") { `cp $line/canvil.ico ./`; `cp $line/*.css ./`; `cp $line/candela_swirl* ./`; `cp $line/CandelaLogo* ./`; last; } } } my $dut_tr = ""; my $kpi_tr = ""; my $tests_tr = ""; # TODO: Add git commit history for other repositories perhaps? foreach my $line (@files) { if ( -d $line) { #print "Checking report: $line\n"; if ( -d "$line/logs") { processLogs("$line/logs"); my $log_links = ""; my $li = 0; my $iline; my @ifiles = `ls $line/logs/*-idx.html`; chomp(@ifiles); foreach $iline (@ifiles) { $log_links .= " [$li]"; } if ($log_links ne "") { $log_links = "Errors: $log_links"; } $tests_tr .= "$lineLogs $log_links\n"; } else { $tests_tr .= "$line\n"; } if ( -f "$line/kpi.csv") { my @kpi = `cat $line/kpi.csv`; chomp(@kpi); my $i = 0; foreach my $k (@kpi) { $i++; if ($i == 1) { next; # skip header } my @cols = split(/\t/, $k); if ($dut_tr eq "") { $dut_tr = "$cols[1]$cols[2]$cols[3]$cols[4]$cols[5]\n"; } my $nval = $cols[10]; if ( $nval =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i ) { $nval = sprintf("%.2f", $nval); } my $s_passed = "0"; my $s_failed = "0"; if (@cols >= 16) { $s_passed = $cols[14]; $s_failed = $cols[15]; } $kpi_tr .= "$cols[7]$cols[8]$cols[9]$s_passed$s_failed$nval$cols[11]\n"; } } } } my $date = `date`; while (<>) { my $ln = $_; chomp($ln); $ln =~ s/___TITLE___/$title/g; $ln =~ s/___DATE___/$date/g; $ln =~ s/___TR_DUT___/$dut_tr/g; $ln =~ s/___TR_KPI___/$kpi_tr/g; $ln =~ s/___TR_TESTS___/$tests_tr/g; $ln =~ s/___TESTBED_NOTES___/$testbed_notes/g; print "$ln\n"; } exit(0); sub processLogs { my $ldir = shift; my @files = `ls $ldir`; chomp(@files); open(CSV, ">$ldir/logs.csv"); print CSV "FILE\tBUGS\tWARNINGS\tCRASHED\tRESTARTING\n"; foreach $line (@files) { if ($line =~ /console.*\.txt$/) { my $bugs = 0; my $warnings = 0; my $crashed = 0; my $restarting = 0; my $tag = 0; my $logf = $ldir . "/" . $line; my $logh = $logf . ".html"; my $loghb = $line . ".html"; my $loghi = $logf . "-idx.html"; #print("Processing log file: $logf\n"); open(IFILE, "<", $logf ) or die( "could not read $logf"); open(IDX, ">$loghi"); open(LOGH, ">$logh"); print IDX getHtmlHdr("Log file index: $line"); print LOGH getHtmlHdr("Log file: $line"); print LOGH "
\n";

         print IDX "Full Logs in HTML format
\n"; print IDX "Full Logs in Text format

\n"; print IDX "Interesting log sections.
\n"; print IDX "

    \n"; while () { my $ln = $_; chomp($ln); #print("ln: $ln\n"); my $enc_ln = encode_entities($ln); # Get rid of some funk $enc_ln =~ s/\&\#0\;//g; if (($ln =~ /WARNING:/) || ($ln =~ /BUG:/) || ($ln =~ /Hardware became unavailable during restart/) || ($ln =~ /restarting hardware/) || ($ln =~ /crashed/)) { if ($ln =~ /WARNING:/) { $warnings++; } elsif ($ln =~ /BUG:/) { $bugs++; } elsif ($ln =~ /restarting hardware/) { $restarting++; } elsif (($ln =~ /crashed/) || # software/firmware crashed ($ln =~ /became unavailable/)) { # hardware crashed $crashed++; } print IDX "
  1. $enc_ln
  2. \n"; print LOGH "\n"; print LOGH "
    $enc_ln
    \n"; $tag++; } else { print LOGH "$enc_ln\n"; } } #print ("Done with file\n"); print LOGH "
\n"; print IDX "\n"; print LOGH getHtmlFooter(); print IDX getHtmlFooter(); close(IDX); close(LOGH); close(IFILE); print CSV "$line\t$bugs\t$warnings\t$crashed\t$restarting\n"; if ($bugs + $warnings +$crashed + $restarting == 0) { # Remove index since it has no useful data unlink($loghi); } } } #print("Done processing logs.\n"); } sub getHtmlHdr { my $title = shift; return "\n" . "\n" . " \n" . " \n" . " \n" . " $title \n" . " \n" . " \n" . " \n" . " \n" . " \n" . "
\n" . "

$title


\n" . "\n" . "
\n"; } sub getHtmlFooter { return "
\n" . "
Generated by Candela Technologies LANforge network testing tool.
\n" . " www.candelatech.com\n" . "
\n" . "

\n" . " \n" . "\n"; } # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----