common/tools/ats/update_brag_status.pl
changeset 1040 f1f85b34a96f
child 1090 46b3578a07ad
equal deleted inserted replaced
1039:8e2498eaef25 1040:f1f85b34a96f
       
     1 #!/usr/bin/perl
       
     2 
       
     3 # Copyright (c) 2009 Symbian Foundation Ltd
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Symbian Foundation Ltd - initial contribution.
       
    11 #	Maciej Seroka, maciejs@symbian.org
       
    12 #
       
    13 # Description:
       
    14 #   This is a tool for updating BRAG status with test results.
       
    15 
       
    16 use strict;
       
    17 use Getopt::Long;
       
    18 use XML::Simple;
       
    19 use Tie::File;
       
    20 
       
    21 my $brag_file;
       
    22 my $ats_report;
       
    23 my $test_report;
       
    24 my $help;
       
    25 my $line;
       
    26 my $auto_tests_total;
       
    27 my $auto_tests_passed;
       
    28 my $auto_tests_failed;
       
    29 my $auto_tests_notrun;
       
    30 my $man_tests_total;
       
    31 my $man_tests_passed;
       
    32 my $man_tests_failed;
       
    33 my $man_tests_notrun;
       
    34 my $temp_path;
       
    35 my $detailshref;
       
    36 my $xml_report;
       
    37 my $test_case;
       
    38 my $test_brag_xml;
       
    39 my $existing_results;
       
    40 my $n;
       
    41 my $m;
       
    42 my @lines;
       
    43 
       
    44 sub usage($);
       
    45 sub help();
       
    46 sub usage_error();
       
    47 
       
    48 my %optmap = (  'brag-file' => \$brag_file,
       
    49 			    'ats-report' => \$ats_report,
       
    50 			    'test-report' => \$test_report,
       
    51 				'help' => \$help);
       
    52 
       
    53 GetOptions(\%optmap,
       
    54           'brag-file=s',
       
    55           'ats-report=s',
       
    56           'test-report=s',
       
    57 		  'help!') 
       
    58           or usage_error();
       
    59 
       
    60 if ($help) {
       
    61 	help();
       
    62 }
       
    63 
       
    64 # --brag-file is mandatory.
       
    65 usage_error(), unless (defined($brag_file));
       
    66 
       
    67 # --ats-report is mandatory.
       
    68 usage_error(), unless (defined($ats_report));
       
    69 
       
    70 open (FILE, "<$ats_report") or die ("Couldn't open $ats_report for reading: $!\n");
       
    71 my $read_flag = -1;
       
    72 print "Reading $ats_report... ";
       
    73 while ($line = <FILE>) {
       
    74 	chomp $line;
       
    75 	if ($read_flag >= 0) { $read_flag++ }
       
    76 	if ($read_flag == 1) { # Get Total number of tests.
       
    77 		$auto_tests_total = $line;
       
    78 		# Remove td tags.
       
    79 		$auto_tests_total =~ s/^.*\">//;
       
    80 		$auto_tests_total =~ s/<.*//;
       
    81 	}
       
    82 	if ($read_flag == 4) { # Get number of Passed tests.
       
    83 		($auto_tests_passed) = split /\//,$line;
       
    84 		$auto_tests_passed =~ s/^.*\">//; # Remove opening td tag.
       
    85 	}
       
    86 	if ($read_flag == 5) { # Get number of Failed tests.
       
    87 		($auto_tests_failed) = split /\//,$line;
       
    88 		$auto_tests_failed =~ s/^.*\">//; # Remove opening td tag.
       
    89 
       
    90 	}
       
    91 	if ($read_flag == 7) { # Get number of Not run tests.
       
    92 		($auto_tests_notrun) = split /\//,$line;
       
    93 		$auto_tests_notrun =~ s/^.*\">//; # Remove opening td tag.
       
    94 	}
       
    95 	if ($line eq "<td class=\"tableData\">Nested tests<\/td>") { # Set flag for reading results.
       
    96 		$read_flag = 0;
       
    97 	}
       
    98 	if (($line eq "<\/tr>") && ($read_flag >= 0)) { # Exit loop.
       
    99 		last;
       
   100 	};
       
   101 }
       
   102 close FILE;
       
   103 print "complete\n";
       
   104 print "Total number of tests: $auto_tests_total\n";
       
   105 print "Passed tests: $auto_tests_passed\n";
       
   106 print "Failed tests: $auto_tests_failed\n";
       
   107 print "Not run tests: $auto_tests_notrun\n";
       
   108 
       
   109 # Define detailshref attribute.
       
   110 $temp_path = $brag_file;
       
   111 $temp_path =~ s/\\/\//g; # Replace \ with /
       
   112 $temp_path =~ s/\/summary\/.*//;
       
   113 $detailshref = $ats_report;
       
   114 $detailshref =~ s/\\/\//g; # Replace \ with /
       
   115 $detailshref =~ s/($temp_path)/../;
       
   116 
       
   117 if (defined($test_report)) { # Get manual Smoketest results.
       
   118 	print "Parsing " . $test_report . "... ";
       
   119 	$xml_report = XMLin("$test_report", keeproot => 1, forcearray => [ 'test-set', 'test-case' ], keyattr => [] );
       
   120 	print "complete \n";
       
   121 	$man_tests_total = 0;
       
   122 	$man_tests_passed = 0;
       
   123 	$man_tests_failed = 0;
       
   124 	$man_tests_notrun = 0;
       
   125 	foreach $test_case (@{$xml_report->{'test-report'}->{'test-set'}->[0]->{'test-case'}}) {
       
   126 		if (lc($test_case->{'result'}) eq "passed") { $man_tests_passed++ }
       
   127 		if (lc($test_case->{'result'}) eq "failed") { $man_tests_failed++ }
       
   128 		if (lc($test_case->{'result'}) eq "not run") { $man_tests_notrun++ }
       
   129 		$man_tests_total++;
       
   130 	}
       
   131 	print "Total number of tests: $man_tests_total\n";
       
   132 	print "Passed tests: $man_tests_passed\n";
       
   133 	print "Failed tests: $man_tests_failed\n";
       
   134 	print "Not run tests: $man_tests_notrun\n";
       
   135 }
       
   136 
       
   137 # Check for Smoketest results in BRAG file.
       
   138 open (FILE, "<$brag_file") or die ("Couldn't open $brag_file for reading: $!\n");
       
   139 $existing_results = 0;
       
   140 $n = 0;
       
   141 while ($line = <FILE>)
       
   142 {
       
   143 	chomp $line;
       
   144 	if ($line =~ "<phase name=\"Test\">") {
       
   145 		$existing_results = 1;
       
   146 		last;
       
   147 	}
       
   148 	$n++;
       
   149 }
       
   150 close FILE;
       
   151 
       
   152 tie @lines, 'Tie::File', $brag_file or die ("Couldn't tie file $brag_file: $!\n");
       
   153 if ($existing_results) { # Delete existing Test Results from BRAG file.
       
   154 	$m = 0;
       
   155 	do {
       
   156 		$m++;
       
   157 	} while (@lines[$n+$m] !~ "<\/phase>");
       
   158 	splice @lines, $n, $m+1;
       
   159 }
       
   160 
       
   161 # Insert (New) Test Results to BRAG file.
       
   162 $n = 0;
       
   163 foreach ( @lines )
       
   164 {
       
   165 	if (lc(@lines[$n]) =~ "<\/buildstatus>") {
       
   166 		last;
       
   167 	}
       
   168 	$n++;
       
   169 }
       
   170 splice @lines, $n, 0, "<phase name=\"Test\">"; $n++;
       
   171 splice @lines, $n, 0, "<step detailshref=\"$detailshref\" name=\"Smoketest - automated tests\">"; $n++;
       
   172 splice @lines, $n, 0, "<nestedtests count=\"$auto_tests_total\" property=\"total\"/>"; $n++;
       
   173 splice @lines, $n, 0, "<nestedtests count=\"$auto_tests_passed\" property=\"passed\"/>"; $n++;
       
   174 splice @lines, $n, 0, "<nestedtests count=\"$auto_tests_failed\" property=\"failed\"/>"; $n++;
       
   175 splice @lines, $n, 0, "<nestedtests count=\"$auto_tests_notrun\" property=\"not run\"/>"; $n++;
       
   176 splice @lines, $n, 0, "<\/step>"; $n++;
       
   177 if (defined($test_report)) {
       
   178 	splice @lines, $n, 0, "<step name=\"Smoketest - manual tests\">"; $n++;
       
   179 	splice @lines, $n, 0, "<nestedtests count=\"$man_tests_total\" property=\"total\"/>"; $n++;
       
   180 	splice @lines, $n, 0, "<nestedtests count=\"$man_tests_passed\" property=\"passed\"/>"; $n++;
       
   181 	splice @lines, $n, 0, "<nestedtests count=\"$man_tests_failed\" property=\"failed\"/>"; $n++;
       
   182 	splice @lines, $n, 0, "<nestedtests count=\"$man_tests_notrun\" property=\"not run\"/>"; $n++;
       
   183 	splice @lines, $n, 0, "<\/step>"; $n++;
       
   184 }
       
   185 splice @lines, $n, 0, "<\/phase>";
       
   186 
       
   187 untie @lines;
       
   188 print "$brag_file updated\n";
       
   189 
       
   190 # Create Test_BRAG.xml
       
   191 $test_brag_xml = $temp_path . "\/summary\/" . "Test_BRAG.xml";
       
   192 if (-e $test_brag_xml) { # Delete the file if exists.
       
   193 	unlink($test_brag_xml);
       
   194 	print "$test_brag_xml deleted\n";
       
   195 }
       
   196 open (FILE, "> $test_brag_xml") or die ("Couldn't open $test_brag_xml for writing: $!\n");
       
   197 print FILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
       
   198 print FILE "<?xml-stylesheet type='text/xsl' href='brag.xsl'?>\n";
       
   199 print FILE "<buildStatus>\n";
       
   200 print FILE "<phase name=\"Test\">\n";
       
   201 print FILE "<step detailshref=\"$detailshref\" name=\"Smoketest - automated tests\">\n";
       
   202 print FILE "<nestedtests count=\"$auto_tests_total\" property=\"total\"/>\n";
       
   203 print FILE "<nestedtests count=\"$auto_tests_passed\" property=\"passed\"/>\n";
       
   204 print FILE "<nestedtests count=\"$auto_tests_failed\" property=\"failed\"/>\n";
       
   205 print FILE "<nestedtests count=\"$auto_tests_notrun\" property=\"not run\"/>\n";
       
   206 print FILE "<\/step>\n";
       
   207 if (defined($test_report)) {
       
   208 	print FILE "<step name=\"Smoketest - manual tests\">\n";
       
   209 	print FILE "<nestedtests count=\"$man_tests_total\" property=\"total\"/>\n";
       
   210 	print FILE "<nestedtests count=\"$man_tests_passed\" property=\"passed\"/>\n";
       
   211 	print FILE "<nestedtests count=\"$man_tests_failed\" property=\"failed\"/>\n";
       
   212 	print FILE "<nestedtests count=\"$man_tests_notrun\" property=\"not run\"/>\n";
       
   213 	print FILE "<\/step>\n";
       
   214 }
       
   215 print FILE "<\/phase>\n";
       
   216 print FILE "</buildStatus>\n";
       
   217 close FILE;
       
   218 print "$test_brag_xml saved\n";
       
   219 
       
   220 exit 0;
       
   221 
       
   222 sub usage($)
       
   223 {
       
   224     my $error = shift;
       
   225     my $fh = $error == 0 ? *STDOUT : *STDERR;
       
   226     print $fh "update_brag_status.pl\n" .
       
   227             "Specify the BRAG xml file and ATS report\n" .
       
   228             "synopsis:\n" .
       
   229             "  update_brag_status.pl --help\n" .
       
   230             "  update_brag_status.pl [--brag-file=XML_FILE] [--ats-report=HTML_REPORT] [--test-report=XML_REPORT] \n" .
       
   231             "options:\n" .
       
   232             "  --help                        Display this help and exit.\n" .
       
   233             "  --brag-file=XML_FILE          XML_FILE is the full path to the file containing BRAG summary.\n" .
       
   234             "  --ats-report=HTML_REPORT      HTML_REPORT is the full path to the ATS report.\n" .
       
   235             "  --test-report=XML_REPORT      XML_REPORT is the name of the test report for manually executed tests.\n" .
       
   236 			"                                If not specified only automated tests results will be taken into account.\n";
       
   237     exit $error;            
       
   238 }
       
   239 
       
   240 sub help()
       
   241 {
       
   242     usage(0);
       
   243 }
       
   244 
       
   245 sub usage_error()
       
   246 {
       
   247     usage(1);
       
   248 }