Initial add of defaria.com
[clearscm.git] / defaria.com / GD / Rantest / rantest.php
1 <?php
2 ////////////////////////////////////////////////////////////////////////////////
3 //
4 // File:        rantest.php
5 // Revision:    1.2
6 // Description: Produce Daily Test Report, Test Suite Report and Test Steps
7 //              report.
8 // Author:      Andrew@ClearSCM.com
9 // Created:     Mon Apr 28 15:20:06 MST 2008
10 // Modified:    
11 // Language:    PHP
12 //
13 // (c) Copyright 2008, General Dynamics, all rights reserved.
14 //
15 // All rights reserved except as subject to DFARS 252.227-7014 of contract
16 // number CP02H8901N issued under prime contract N00039-04-C-2009.
17 //
18 // Warning: This document contains technical data whose export is restricted
19 // by the Arms Export Control Act (Title 22, U.S.C., Sec 2751, et seq.) or the
20 // Export Administration Act of 1979, as amended, Title, 50, U.S.C., App. 2401
21 // et seq. Violations of these export laws are subject to severe criminal
22 // penalties. Disseminate in accordance with provisions of DoD Directive
23 // 5230.25.
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 $script = basename ($_SERVER["PHP_SELF"]);
27
28 include_once "$_SERVER[DOCUMENT_ROOT]/php/Utils.php";
29 include_once "$_SERVER[DOCUMENT_ROOT]/php/RantestDB.php";
30
31 $testName       = $_REQUEST["testName"];
32 $runid          = $_REQUEST["runid"];
33 $date           = $_REQUEST["date"];
34 $suiteid        = $_REQUEST["suiteid"];
35 $user           = $_REQUEST["user"];
36
37 $type           = (empty ($_REQUEST["type"]))      ? "All"        : $_REQUEST["type"];
38 $action         = (empty ($_REQUEST["action"]))    ? "Report"     : $_REQUEST["action"];
39 $sortBy         = (empty ($_REQUEST["sortBy"]))    ? "Start"      : $_REQUEST["sortBy"];
40 $direction      = (empty ($_REQUEST["direction"])) ? "descending" : $_REQUEST["direction"];
41
42 function createTestStepsHeader ($testcase) {
43   $header = <<<END
44 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
45    "http://www.w3.org/TR/html4/strict.dtd">
46 <html>
47 <head>
48   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
49   <link rel="stylesheet" type="text/css" media="screen" href="/css/Testing.css">
50   <link rel="stylesheet" type="text/css" media="screen" href="/css/Tables.css">
51 <title>Test Steps for $testcase</title>
52 </head>
53
54 <body>
55 END;
56
57   $header .= banner ();
58   $header .= <<<END
59 <h1 align="center">Test Steps for $testcase</h1>
60 END;
61
62   return $header;
63 } // createTestStepsHeader
64
65 function createTestRunsHeader ($day) {
66   global $dateDropdown;
67
68   $header = <<<END
69 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
70    "http://www.w3.org/TR/html4/strict.dtd">
71 <html>
72 <head>
73   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
74   <link rel="stylesheet" type="text/css" media="screen" href="/css/Testing.css">
75   <link rel="stylesheet" type="text/css" media="screen" href="/css/Tables.css">
76 <title>Daily Test Report for $day</title>
77 <script language="javascript">
78 function ChangeDay (day, script) {
79   window.location = script + "?day=" + day;
80 } // ChangeDay
81 function ChangeType (day, type, script) {
82   window.location = script + "?day=" + day + "&type=" + type;
83 } // ChangeType
84 </script>
85 </head>
86 END;
87
88  $header .= banner ();
89  $header .= <<<END
90 <body>
91 <h1 align="center">Daily Test Report for $dateDropdown</h1>
92 END;
93
94   return $header;
95 } // createTestRunsHeader
96
97 function createSuiteRunsHeader ($id) {
98   $suite = getName ("suite", $id);
99   
100   $header = <<<END
101 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
102    "http://www.w3.org/TR/html4/strict.dtd">
103 <html>
104 <head>
105   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
106   <link rel="stylesheet" type="text/css" media="screen" href="/css/Testing.css">
107   <link rel="stylesheet" type="text/css" media="screen" href="/css/Tables.css">
108 <title>Suite Report for $suite</title>
109 </head>
110
111 <body>
112 END;
113
114   $header .= banner ();
115
116   return $header;
117 } // createSuiteRunsHeader
118
119 function createTestStepsPage ($testName, $forEmail = false, $message = "") {
120   global $runid, $date, $script;
121
122   $data = getTestSteps ($runid);
123
124   if (!$forEmail) {
125     if (isset ($message)) {
126       $page .= "<div align=center>$message</div>";
127     } // if
128   } // if
129
130   $page .= <<<END
131 <table align=center width=90%>
132   <caption><center>
133 DUT: <font class="standout">{$data[_header][DUTVersion]}</font>
134 EAST: <font class="standout">{$data[_header][EASTVersion]}</font>
135 TM500: <font class="standout">{$data[_header][TM500Version]}</font>
136 NMS: <font class="standout">{$data[_header][NMSVersion]}</font>
137 RANTEST: <font class="standout">{$data[_header][RANTESTVersion]}</font>
138 Date: <font class="standout">$date</font>
139   </center></caption>
140   <thead>
141 END;
142
143   if (!$forEmail) {
144     $page .= <<<END
145     <tr>
146       <th class="clear" align="left"><a href="$script?action=Export&testName=$testName&runid=$runid&date=$date"><input type="submit" value="Export to CSV"></a>&nbsp;<a href="TestHistory.php?testcase=$testName"><input type="submit" value="History"></a></th>
147       <th class=clear colspan="4" align="right"><form action="$script?action=Mail&date=$date&testName=$testName&runid=$runid" method="post">
148 END;
149
150     $page .= emailUsersDropdown ();
151     $page .= <<<END
152     </select>
153     <input type="submit" value="Send"></form>
154     </th>
155     </tr>
156 END;
157   } // if
158
159   $page .= <<<END
160     <tr>
161       <th class=left>Step</th>
162       <th>Status</th>
163       <th>Start</th>
164       <th>End</th>
165       <th class=right>Duration</th>
166     </tr>
167   </thead>
168   <tbody>
169 END;
170
171   foreach ($data["_steps"] as $line) {
172     $steps++;
173     $row_color          = setRowColor ($line["Status"]);
174     $line["Status"]     = colorResult ($line["Status"]);
175     $total_time        += $line[Duration];
176     $startTime          = substr ($line["Start"], 11, 8);
177     $endTime            = substr ($line["End"],   11, 8);
178
179     $page .= <<<END
180       <tr $row_color>
181         <td>$line[Step]</td>
182         <td>$line[Status]</td>
183         <td align="center">$startTime</td>
184         <td align="center">$endTime</td>
185         <td align="right">
186 END;
187     $page .= FormatDuration ($line[Duration]);
188     $page .= "</tr>";
189   } // foreach
190
191   $total_duration = FormatDuration ($total_time);
192
193   $logs = logs ($data["_header"]["_eastlogs"], $forEmail);
194
195   $username = getUserName ($data["_header"]["userid"]);
196
197   $page .= <<<END
198   <tfoot>
199     <tr>
200       <th align="left">Total steps run in $testName: $steps</th>
201       <th align="right" colspan="2">Run by: <a href="mailto:{$data[_header][userid]}@gdc4s.com" class="tablelink">$username</a></th>
202       <th align="center">$logs</th>
203       <th align="right">$total_duration</th>
204     </tr>
205   </tfoot>
206   </tbody>
207 </table>
208 END;
209
210   return $page;
211 } // createTestStepsPage
212
213 function createTestRunsPage ($day, $forEmail = false, $message = "") {
214   global $sortBy, $direction, $day, $type, $script, $testTypes;
215
216   if (!$forEmail) {
217     if ($sortBy == "Suite") {
218       $suiteDirection = ($direction == "ascending") 
219         ? "<img src=/images/down.png border=0>"
220         : "<img src=/images/up.png border=0>";
221     } elseif ($sortBy == "Testcase") {
222       $testcaseDirection = ($direction == "ascending") 
223         ? "<img src=/images/down.png border=0>"
224         : "<img src=/images/up.png border=0>";
225     } elseif ($sortBy == "Type") {
226       $typeDirection = ($direction == "ascending") 
227         ? "<img src=/images/down.png border=0>"
228         : "<img src=/images/up.png border=0>";
229     } elseif ($sortBy == "Unit") {
230       $unitDirection = ($direction == "ascending") 
231         ? "<img src=/images/down.png border=0>"
232         : "<img src=/images/up.png border=0>";
233     } elseif ($sortBy == "Status") {
234       $statusDirection = ($direction == "ascending") 
235         ? "<img src=/images/down.png border=0>"
236         : "<img src=/images/up.png border=0>";
237     } elseif ($sortBy == "Start") {
238       $startDirection = ($direction == "ascending") 
239         ? "<img src=/images/down.png border=0>"
240         : "<img src=/images/up.png border=0>";
241     } elseif ($sortBy == "End") {
242       $endDirection = ($direction == "ascending") 
243         ? "<img src=/images/down.png border=0>"
244         : "<img src=/images/up.png border=0>";
245     } elseif ($sortBy == "Duration") {
246       $durationDirection = ($direction == "ascending") 
247         ? "<img src=/images/down.png border=0>"
248         : "<img src=/images/up.png border=0>";
249     } // if
250
251     if (isset ($message)) {
252       $page .= "<div align=center>$message</div>";
253     } // if
254   } // if
255
256   $page .= <<<END
257 <table align=center>
258   <thead>
259 END;
260
261   if (!$forEmail) {
262     $page .= <<<END
263     <tr>
264       <th class="clear" align="left" colspan="5">
265 <a href="$script?action=Export&day=$day"><input type="submit"
266  value="Export to CSV"></a>&nbsp;<a href="FailureAnalysis.php?day=$day">
267 <input type="submit" value="Analyze Failures"></a>
268 &nbsp;Type: <select name="type" class="inputfield" onChange="ChangeType('$day',this.value,'$script');">
269 END;
270
271     foreach ($testTypes as $t) {
272       if ($type != $t) {
273         $page .= "<option>$t</option>";
274       } else {
275         $page .= "<option selected=\"selected\">$t</option>";
276       } // if
277     } // foreach
278
279     $page .= <<<END
280       </th>
281       <th class=clear colspan="5" align="right"><form action="$script?action=Mail&day=$day&type=$type" method="post">
282 END;
283
284     $page .= emailUsersDropdown ();
285     $page .= <<<END
286     </select>
287     <input type="submit" value="Send"></form>
288     </th>
289     </tr>
290 END;
291   } else {
292     $page .= "<th class=\"clear\" colspan=\"10\">Type: $type Sorted by: $sortBy ($direction)</th>";
293   } // if
294
295   $direction = ($direction == "ascending") ? "descending" : "ascending";
296   $urlParms  = "$script?day=$day&&direction=$direction&type=$type&sortBy";
297
298   $page .= <<<END
299     <tr>
300       <th class="left">#</th>
301       <th><a href="$urlParms=Suite">Suite&nbsp;$suiteDirection</a></th>
302       <th><a href="$urlParms=Testcase">Testcase&nbsp;$testcaseDirection</a></th>
303       <th><a href="$urlParms=Type">Type&nbsp;$typeDirection</a></th>
304       <th><a href="$urlParms=Unit">Unit/Version&nbsp;$unitDirection</a></th>
305       <th>Logs</th>
306       <th><a href="$urlParms=Status">Status&nbsp;$statusDirection</a></th>
307       <th><a href="$urlParms=Start">Start&nbsp;$startDirection</a></th>
308       <th><a href="$urlParms=End">End&nbsp;$endDirection</a></th>
309       <th class="right"><a href="$urlParms=Duration">Duration&nbsp;$durationDirection</</th>
310     </tr>
311   </thead>
312   <tbody>
313 END;
314
315   $page .= <<<END
316 END;
317
318   $data = getTestRuns (MDY2YMD ($day), $type);
319
320   $total_time = 0;
321
322   foreach ($data as $line) {
323     // WARNING: This is odd! $line["Suite"] should be empty if there
324     // was no suite associated with it (thereby suiteid=0) but for
325     // some odd reason due to the complex select statement used
326     // suiteid ends up being 1 which is associated with the suite name
327     // of "nightly".
328     if ($line["Suite"] == "nightly") {
329       $line["Suite"] = "<font color=#999999>Standalone</font>";
330     } else {
331       $line["Suite"] = "<a href=\"$me?suiteid=$line[_suiteid]\">$line[Suite]</a>";
332     } // if 
333
334     $row_color   = setRowColor ($line["Status"]);
335     $testResult  = colorResult ($line["Status"]);
336     $total_time += $line["Duration"];
337     $me          = ($script == "index.php") ? "" : $script;
338     $logs        = logs ($line["_eastlogs"], $forEmail);
339     $tests++;
340
341     $page .= <<<END
342     <tr $row_color>
343       <td align=center>$tests</td>
344       <td>$line[Suite]</td>
345       <td><a href=$me?testName=
346 END;
347     $page .= $line["Testcase"];
348     $page .= "&runid=$line[_runid]&date=$day>";
349     $page .= $line["Testcase"];
350     $page .= <<<END
351 </a></td>
352       <td align="center">$line[Type]</td>
353       <td align="center">
354 END;
355     $page .= $line["Unit/Version"];
356     $page .= <<<END
357 </td>
358       <td align="center">$logs</td>
359       <td align="center">$testResult</td>
360       <td align="center">$line[Start]</td>
361       <td align="center">$line[End]</td>
362       <td align="right">
363 END;
364
365     $page .= FormatDuration ($line["Duration"]);
366     $page .= "</td></tr>";
367   } // while
368
369   $total_duration = FormatDuration ($total_time);
370
371   $page .= <<<END
372   <tfoot>
373     <tr>
374       <th align="left" colspan="9">$tests Run&nbsp;
375 END;
376   $page .= stats ($day, $type);
377   $page .= <<<END
378 </th>
379       <th align="right">$total_duration</th>
380     </tr>
381   </tfoot>
382   </tbody>
383 </table>
384 END;
385   return $page;
386 } // createTestRunsPage
387
388 function createSuiteRunsPage ($suiteid, $forEmail = false, $message = "") {
389   global $sortBy, $direction, $day;
390
391   $name = getName ("suite", $suiteid);
392   $page = "<h1 align=center>Test Suite Report for $name</h1>";
393
394   if (!$forEmail) {
395     if ($sortBy == "Status") {
396       $statusDirection = ($direction == "ascending") 
397         ? "<img src=/images/down.png border=0>"
398         : "<img src=/images/up.png border=0>";
399     } elseif ($sortBy == "Start") {
400       $startDirection = ($direction == "ascending") 
401         ? "<img src=/images/down.png border=0>"
402         : "<img src=/images/up.png border=0>";
403     } elseif ($sortBy == "End") {
404       $endDirection = ($direction == "ascending") 
405         ? "<img src=/images/down.png border=0>"
406         : "<img src=/images/up.png border=0>";
407     } elseif ($sortBy == "Duration") {
408       $durationDirection = ($direction == "ascending") 
409         ? "<img src=/images/down.png border=0>"
410         : "<img src=/images/up.png border=0>";
411     } // if
412
413     if (isset ($message)) {
414       $page .= "<div align=center>$message</div>";
415     } // if
416   } // if
417
418   $page .= <<<END
419 <table align=center width="75%">
420   <thead>
421 END;
422
423   if (!$forEmail) {
424     $page .= <<<END
425     <tr>
426       <th class="clear" align="left" colspan="2"><a href="$script?action=Export&suiteid=$suiteid"><input type="submit" value="Export to CSV"></a>
427 END;
428
429     $page .= <<<END
430       </th>
431       <th class=clear colspan="2" align="right"><form action="$script?action=Mail&suiteid=$suiteid" method="post">
432 END;
433
434     $page .= emailUsersDropdown ();
435     $page .= <<<END
436     </select>
437     <input type="submit" value="Send"></form>
438     </th>
439     </tr>
440 END;
441   } // if
442
443   $direction = ($direction == "ascending") ? "descending" : "ascending";
444   $urlParms  = "$script?suiteid=$suiteid&direction=$direction&&sortBy";
445
446   $data = getSuiteRuns ($suiteid);
447
448   $page .= <<<END
449     <tr>
450       <th class=left><a href="$urlParms=Status">Status&nbsp;$statusDirection</a></th>
451       <th><a href="$urlParms=Start">Start&nbsp;$startDirection</a></th>
452       <th><a href="$urlParms=End">End&nbsp;$endDirection</a></th>
453       <th class=right><a href="$urlParms=Duration">Duration&nbsp;$durationDirection</a></th>
454     </tr>
455   </thead>
456   <tbody>
457 END;
458
459   foreach ($data as $line) {
460     $row_color  = setRowColor ($line["Status"]);
461     $status     = colorResult ($line["Status"]);
462     $duration   = FormatDuration ($line["Duration"]);
463
464     $suiteruns++;
465     $total_time += $line["Duration"];
466
467     $page .= <<<END
468       <tr $row_color>
469         <td>$status</td>
470         <td align="center">$line[Start]</td>
471         <td align="center">$line[End]</td>
472         <td align="right">$duration</td>
473       </tr>
474 END;
475   } // while
476
477   $total_duration = FormatDuration ($total_time);
478
479   $page .= <<<END
480   <tfoot>
481     <tr>
482       <th align="left" colspan="3">Total sutie runs for $name: $suiteruns</th>
483       <th align="right">$total_duration</th>
484     </tr>
485   </tfoot>
486   </tbody>
487 </table>
488 END;
489
490   return $page;
491 } // createSuiteRunsPage
492
493 function displayStepRun ($testName, $message = "") {
494   print createTestStepsHeader ($testName);
495   print createTestStepsPage ($testName, false, $message);
496
497   copyright ();
498 } // displayStepRun
499
500 function displayTestRuns ($day, $message = "") {
501   print createTestRunsHeader ($day);
502   print createTestRunsPage ($day, false, $message);
503
504   copyright ();
505 } // displayTestRuns
506
507 function displaySuiteRun ($suiteid, $message = "") {
508   print createSuiteRunsHeader ($suiteid);
509   print createSuiteRunsPage ($suiteid, false, $message);
510
511   copyright ();
512 } // displaySuiteRun
513
514 // The $data structure for test steps is unique so handle exportion
515 // here.
516 function exportStepRunCSV ($data, $title) {
517   global $na;
518
519   $csv .= "$title\n";
520
521   // Put out header information
522   $csv .= "DUT, EAST, TM500, NMS, RANTEST, USER, DATE, LOGS\n";
523
524   $versions = array (
525     "DUTVersion",
526     "EASTVersion",
527     "TM500Version",
528     "NMSVersion",
529     "RANTESTVersion"
530   );
531
532   foreach ($versions as $version) {
533     if ($data["_header"][$version] == $na) {
534       $csv .= "N/A,";
535     } else {
536       $csv .= $data["_header"][$version] . ",";
537     } // if
538   } // foreach
539
540   $csv .= $data["_header"]["userid"]                    . ",";
541   $csv .= YMD2MDY ($data["_header"]["Date"])            . ",";
542   $csv .= logs ($data["_header"]["_eastlogs"], true)    . "\n\n";
543
544   // Create header line
545   $firstTime = true;
546
547   foreach ($data["_steps"][0] as $key => $value) {
548     // Skip "hidden" fields - fields beginning with "_"
549     if (preg_match ("/^_/", $key) == 1) {
550       continue;
551     } // if
552
553     if (!$firstTime) {
554       $csv .= ",";
555     } else {
556       $firstTime = false;
557     } // if
558
559     $csv .= "\"$key\"";
560   } // foreach
561
562   $csv .= "\n";
563
564   foreach ($data["_steps"] as $entry) {
565     $firstTime = true;
566
567     foreach ($entry as $key => $value) {
568       // Skip "hidden" fields - fields beginning with "_"
569       if (preg_match ("/^_/", $key) == 1) {
570         continue;
571       } // if
572
573       if (!$firstTime) {
574         $csv .= ",";
575       } else {
576         $firstTime = false;
577       } // if
578
579       $csv .= "\"$value\"";
580     } // foreach
581
582     $csv .= "\n";
583   } // foreach
584
585   return $csv;
586 } // exportStepRunCSV
587   
588 function exportStepRun ($testcase, $runid, $date) {
589   $timestamp    = getTestRunTimestamp ($runid);
590   $filename = "Step Report." . $timestamp . ".csv";
591
592   header ("Content-Type: application/octect-stream");
593   header ("Content-Disposition: attachment; filename=\"$filename\"");
594
595   print exportStepRunCSV (getTestSteps ($runid), "Step Report for $testcase");
596
597   exit;
598 } // exportStepRun
599
600 function exportTestRuns ($day) {
601   global $type;
602
603   $filename = "Daily Test Report." . $day . ".csv";
604
605   header ("Content-Type: application/octect-stream");
606   header ("Content-Disposition: attachment; filename=\"$filename\"");
607
608   print exportCSV (getTestRuns (MDY2YMD ($day), $type, true), "Daily Test Report for $day");
609
610   exit;
611 } // exportTestRuns
612
613 function exportSuiteRun ($suiteid) {
614   $suite        = getName ("suite", $suiteid);
615   $filename     = "Suite Report." . $suite . ".csv";
616
617   header ("Content-Type: application/octect-stream");
618   header ("Content-Disposition: attachment; filename=\"$filename\"");
619
620   print exportCSV (getSuiteRuns ($suiteid), "Suite Report for $suite");
621
622   exit;
623 } // exportSuiteRun
624
625 function mailStepRunReport ($testName, $pnbr, $username) {
626   global $runid;
627
628   $subject      = "Step Report for $testName";
629   $body         = createTestStepsPage ($testName, true);
630   $timestamp    = getTestRunTimestamp ($runid);
631   $filename     = "Step Report." . $timestamp . ".csv";
632   $attachment   = exportStepRunCSV (getTestSteps ($runid), $subject);
633
634   return mailReport ($pnbr, $username, $subject, $body, $filename, $attachment);
635 } // mailStepReport
636
637 function mailTestRunsReport ($day, $pnbr, $username) {
638   global $type;
639
640   $subject      = "Daily Test Report for $day";
641   $body         = createTestRunsPage ($day, true);
642   $filename     = "TestRuns.$day.csv";
643   $attachment   = exportCSV (getTestRuns (MDY2YMD ($day), $type, true), $subject);
644
645   return mailReport ($pnbr, $username, $subject, $body, $filename, $attachment);
646 } // mailTestRunsReport
647
648 function mailSuiteRunReport ($suiteid, $pnbr, $username) {
649   $subject      = "Suite Report for $suitid";
650   $body         = createSuiteRunsPage ($suiteid, true);
651   $filename     = "Suite Runs.$suiteid.csv";
652
653   $attachment   = exportCSV (getSuiteRuns ($suiteid, true), $subject);
654
655   return mailReport ($pnbr, $username, $subject, $body, $filename, $attachment);
656 } // mailSuiteRunReport
657
658 openDB ();
659
660 $dateDropdown = reportDateDropdown ();
661
662 switch ($action) {
663   case "Export":
664     if ($suiteid != 0) {
665       exportSuiteRun ($suiteid);
666     } elseif (isset ($testName)) {
667       exportStepRun ($testName, $runid, $date);
668     } else {
669       exportTestRuns ($day);
670     } // if
671
672     break;
673
674   case "Mail":
675     list ($pnbr, $username) = explode (":", $user);
676
677     if (isset ($suiteid)) {
678       $message = mailSuiteRunReport ($suiteid, $pnbr, $username);
679       displaySuiteRun ($suiteid, $message);
680     } elseif (isset ($testName)) {
681       $message = mailStepRunReport ($testName, $pnbr, $username);
682       displayStepRun ($testName, $message);
683     } else {
684       $message = mailTestRunsReport ($day, $pnbr, $username);
685
686       displayTestRuns ($day, $message);
687     } // if
688
689     break;
690
691   default:
692     if (isset ($suiteid)) {
693       displaySuiteRun ($suiteid);
694     } elseif (isset ($testName)) {
695       displayStepRun ($testName);
696     } else {
697       displayTestRuns ($day);
698     } // if
699
700     break;
701 } // switch