FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-Test.php
1 <?php
2 /*
3  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 
35 function addProxy($sysConfPath, $proxy='lart.usa.hp.com:3128')
36 {
37  if(file_exists($sysConfPath . '/fossology.conf'))
38  {
39  $fossConf = file_get_contents($sysConfPath . '/fossology.conf');
40  if($fossConf === FALSE)
41  {
42  echo "ERROR! could not read\n$sysConfPath/fossology.conf\n";
43  return(FALSE);
44  }
45  $count = 0;
46  $pat = ';no_proxy = localhost,10.1.2.3';
47  $replacepat = $pat . "\n" . 'http_proxy = ' . $proxy . "\n";
48  //echo "replacepat is:$replacepat\n";
49  $testConf = preg_replace("/.*?no_proxy = .*/", $replacepat, $fossConf,-1,$count);
50  //echo "DB: matched pats is:$count\n";
51  //echo "DB: testConf is:$testConf\n";
52 
53  $stat = file_put_contents("$sysConfPath/fossology.conf",$testConf);
54  if($stat === FALSE)
55  {
56  echo "ERROR! could not write\n$sysConfPath/fossology.conf\n";
57  return(FALSE);
58  }
59  }
60  else
61  {
62  echo "ERROR! can't find fossology.conf at:\n$sysConfPath/fossology.conf\n";
63  return(FALSE);
64  }
65  return(TRUE);
66 }
67 
80 function allFilePaths($dir) {
81 
82  $fileList = array();
83  if(empty($dir)) {
84  return($fileList); // nothing to process, return empty list.
85  }
86  try {
87  foreach(new recursiveIteratorIterator(
88  new recursiveDirectoryIterator($dir)) as $file) {
89  $fileList[] = $file->getPathName($file);
90  }
91  return($fileList);
92  }
93  /*
94  * if the directory does not exist or the directory or a sub directory
95  * does not have sufficent permissions for reading return an empty list
96  */
97  catch(Exception $e) {
98  print $e->getMessage();
99  return(array());
100  }
101 } //allFilePaths
102 
111 function backToParent($howFar)
112 {
113  if(empty($howFar))
114  {
115  echo "FATAL! No input at line " . __LINE__ . " in " . __FILE__ . "\n";
116  exit(1);
117  }
118 
119  $here = getcwd();
120 
121  if(@chdir($howFar) == FALSE)
122  {
123  echo "FATAL! could not cd from:\n$here to:\n$howFar\n" .
124  "at line " . __LINE__ . " in " . __FILE__ . "\n";
125  exit(1);
126 
127  }
128 } // backToParent
129 
143 function checkTestData()
144 {
145  $WORKSPACE = NULL;
146 
147  if(array_key_exists('WORKSPACE', $_ENV))
148  {
149  $WORKSPACE = $_ENV['WORKSPACE'];
150  }
151  if(is_null($WORKSPACE))
152  {
153  // cd to ....fossology/src
154  backToParent('..');
155  }
156  else
157  {
158  if(@chdir($WORKSPACE . "/fossology/src") === FALSE)
159  {
160  echo "FATAL! runRUnit could not cd to " . $WORKSPACE . "/fossology/src\n";
161  exit(1);
162  }
163  }
164  $home = getcwd();
165  $dirs = explode('/',$home);
166  $size = count($dirs);
167  // are we being run by jenkins? if we are not in fossology/tests, cd there
168  if($dirs[$size-1] == 'workspace' )
169  {
170  if(chdir('fossology/tests') === FALSE)
171  {
172  echo "FATAL! Cannot cd to fossology/tests from" . getcwd() . "\n";
173  exit(1);
174  }
175  $home = getcwd(); // home should now be ...workspace/fossology/tests
176  }
177 
178  $redHatPath = 'nomos/testdata';
179  $unpackTestFile = '../ununpack/agent_tests/test-data/testdata4unpack/argmatch.c.gz';
180  $unpackTests = '../ununpack/agent_tests';
181  $redHatDataFile = 'RedHat.tar.gz';
182  $unpackDataFile = 'unpack-test-data.tar.bz2';
183  $wgetOptions = ' -a wget.log --tries=3 ';
184  $proxy = 'export http_proxy=lart.usa.hp.com:3128;';
185  $Url = 'http://fossology.org/testing/testFiles/';
186 
187  $errors = 0;
188  // check/install RedHat.tar.gz
189 
190  /*
191  if(!file_exists($redHatPath . "/" . $redHatDataFile))
192  {
193  if(chdir($redHatPath) === FALSE)
194  {
195  echo "ERROR! could not cd to $redHatPath, cannot download $redHatDataFile\n";
196  $errors++;
197  }
198  $cmd = $proxy . "wget" . $wgetOptions . $Url . $redHatDataFile;
199  $last = exec($cmd, $wgetOut, $wgetRtn);
200  if($wgetRtn != 0)
201  {
202  echo "ERROR! Download of $Url$redHatDataFile failed\n";
203  echo "Errors were:\n$last\n";print_r($wgetOut) . "\n";
204  $errors++;
205  }
206  }
207  else
208 
209  if(chdir($home) === FALSE)
210  {
211  echo "FATAL! could not cd to $home\n";
212  exit(1);
213  }
214  */
215 
216  // check/install ununpack data
217  echo "downloading unpack data.....\n";
218  if(!file_exists($unpackTestFile))
219  {
220  echo "$unpackTestFile DOES NOT EXIST!, need to download data files...\n";
221  if(chdir($unpackTests) === FALSE)
222  {
223  echo "FATAL! cannot cd to $unpackTests\n";
224  exit(1);
225  }
226  $cmd = $proxy . "wget" . $wgetOptions . $Url . '/' . $unpackDataFile;
227  $unpkLast = exec($cmd, $unpkOut, $unpkRtn);
228  if($unpkRtn != 0)
229  {
230  echo "ERROR! Download of $Url$unpackDataFile failed\n";
231  echo "Errors were:\n";print_r($unpkOut) . "\n";
232  $errors++;
233  }
234  // unpack the tar file.
235  $cmd = "tar -xf $unpackDataFile";
236  $tarLast = exec($cmd, $tarOut, $tarRtn);
237  if($tarRtn != 0)
238  {
239  echo "ERROR! un tar of $unpackDataFile failed\n";
240  echo "Errors were:\n$tarLast\n";print_r($tarOut) . "\n";
241  $errors++;
242  }
243  }
244 
245  if($errors)
246  {
247  exit(1);
248  }
249  exit(0);
250 } // checkTestData
251 
257 function cleanXMLFiles()
258 {
259  global $unitList;
260  reset($unitList);
261  foreach($unitList as $unitTest)
262  {
263  $other = substr($unitTest, 0, 3);
264  if($other == 'lib' || $other == 'cli')
265  {
266  if(@chdir($unitTest . '/tests') === FALSE)
267  {
268  echo "Error! cannot cd to " . $unitTest . "/tests, skipping test\n";
269  $failures++;
270  continue;
271  }
272  }
273  else
274  {
275  if(@chdir($unitTest . '/agent_tests/Unit') === FALSE)
276  {
277  echo "Error! cannot cd to " . $unitTest . "/agent_tests/Unit, skipping test\n";
278  $failures++;
279  continue;
280  }
281  }
282  foreach(glob("$unitTest*.xml") as $fName)
283  {
284  $lsOut = array();
285  $rmOut = array();
286  $fileName = lcfirst($fName);
287  $last = exec("rm $fileName", $rmOut, $rmRtn);
288  if($rmRtn != 0)
289  {
290  echo "Notice: could not remove $filename, please remove by hand\n";
291  }
292  } // foreach
293  backToParent('../../..');
294  } // foreach
295  return;
296 }
297 
305 function escapeDots($string)
306 {
307  if (empty ($string))
308  {
309  return (FALSE);
310  }
311  $estring = preg_replace('/\./', '\\.', $string);
312  //print "ED: string is:$string, estring is:$estring\n";
313  if ($estring === NULL)
314  {
315  return (FALSE);
316  }
317  return ($estring);
318 } //escapeDots
319 
320 function lastDir($dirpath) {
321  // can't have a tailing slash, remove it if there
322  $dirpath = rtrim($dirpath, '/');
323  $directories = explode('/',$dirpath);
324  return(end($directories));
325 }
326 
340 function filesByDir($dir) {
341 
342  $ByDir = array();
343  $fileList = array();
344  $subPath = '';
345 
346  if(empty($dir)) {
347  return($fileList); // nothing to process, return empty list.
348  }
349 
350  try {
351  $dirObject = new recursiveIteratorIterator(
352  new recursiveDirectoryIterator($dir),RecursiveIteratorIterator::SELF_FIRST);
353  // dirobjs is recusiveIteratorIterator object
354  foreach($dirObject as $name) {
355 
356  $aSubPath = $dirObject->getSubPath();
357 
358  /*
359  * if we changed subpaths, we are in a new sub-dir, reset the file list
360  */
361  if($aSubPath != $subPath) {
362  //print "DB: fileByDir: asb != sb, Init fileList!\n";
363  $fileList = array();
364  }
365 
366  if(is_file($name)) {
367  $subPath = $dirObject->getSubPath();
368  $spn = $dirObject->getSubPathName();
369  $subDir = dirname($spn);
370  if($subDir == $aSubPath) {
371  $fileName = $dirObject->getFilename();
372  $fileList[] = $fileName;
373  }
374  }
375  if (empty($subPath)){
376  continue;
377  }
378  else {
379  if(empty($fileList)){
380  continue;
381  }
382  $ByDir[$subPath] = $fileList;
383  }
384 
385  /* Debug
386  *
387  $subPath = $dirObject->getSubPath();
388  print "DB: fileByDir: subpath is:$subPath\n";
389  $sbn = $dirObject->getSubPathName();
390  print "DB: fileByDir: subpathname is:$sbn\n";
391  $dirpath = $dirObject->getPath();
392  print "DB: fileByDir: dirpath is:$dirpath\n";
393 
394  */
395 
396  } // foreach
397  //print "DB: fileByDir: ByDir is:\n ";print_r($ByDir) . "\n";
398  return($ByDir);
399  }
400 
401  /*
402  if the directory does not exist or the directory or a sub directory
403  does not have sufficent permissions for reading return an empty list
404  */
405  catch(Exception $e) {
406  //print "in exception!\n$e\n";
407  return(array());
408  }
409 } // fileByDir
410 
417 class RunTest
418 {
419  public $makeOutput = array();
420  protected $unitTest;
421  protected $makeErrors;
422  protected $cunitErrors;
423  protected $phpunitErrors;
424 
425  function __construct($unitTest)
426  {
427  $this->unitTest = $unitTest;
428  $this->makeErrors = FALSE;
429  $this->cunitErrors = FALSE;
430  $this->phpunitErrors = FALSE;
431  }
432 
451  function MakeTest()
452  {
453 
454  $results = array(
455  'name'=> $this->unitTest,
456  'make' => FALSE,
457  'cunit' => FALSE,
458  'phpunit' => FALSE,
459  'notest' => FALSE,
460  'other' => NULL,
461  'nomosfunc' => NULL,
462  );
463 
464  $cleanMake = exec('make clean 2>&1', $cleanOut, $cleanRtn);
465  if($cleanRtn != 0)
466  {
467  echo "Make clean of $this->unitTest did not succeed, return code:$cleanRtn\n";
468  // right now this is not reported as an error
469  // @todo figure out how to handle this. Make clean failures should not
470  // cause make to not be done.
471  }
472  //echo "ComTest: sysconfdir from globals is:{$GLOBALS['SYSCONFDIR']}\n";
473  $s = getenv('SYSCONFDIR');
474  //echo "DB: sysconfdir from env before make of $this->unitTest is:$s\n";
475  $lastMake = exec('make test 2>&1', $this->makeOutput, $makeRtn);
476  //echo "DB: Exit status of 'make test' of $this->unitTest is:$makeRtn\n";
477  //debugprint($this->makeOutput, "make output\n");
478 
479  if($makeRtn != 0)
480  {
481  $found = array();
482  $found = preg_grep('/No rule to make target/', $this->makeOutput);
483  if($found)
484  {
485  $results['notest'] = TRUE;
486  //echo "No Unit Tests for module $unitTest\n";
487  }
488  else
489  {
490  // check for real make errors and test errors.
491  if($this->CheckMakeErrors(implode("\n", $this->makeOutput)))
492  {
493  //echo "Error! There were make errors for unit test $unitTest\n";
494  $results['make'] = TRUE;
495  }
496  if($this->checkCunitTestErrors(implode("\n", $this->makeOutput)))
497  {
498  $results['cunit'] = TRUE;
499  /*
500  $pattern = $this->unitTest . "_Tests summary:";
501  $found = array_search($pattern, $this->makeOutput);
502  if($found !== FALSE)
503  {
504  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
505  }
506  */
507  }
508  if($this->checkPHPTestErrors(implode("\n", $this->makeOutput)))
509  {
510  $results['phpunit'] = TRUE;
511  }
512  $other = $this->CheckOtherErrors(implode("\n", $this->makeOutput));
513  if($other)
514  {
515  $results['other'] = $other;
516  }
517  }
518  return($results);
519  }
520  // Make returned zero
521  else
522  {
523  // no tests for is module? Skip report processing
524  $nothing = array();
525  $nothing= preg_grep("/Nothing to be done for \`test\'/", $this->makeOutput);
526  //echo "grep for Nothing to... is:\n";print_r($nothing) . "\n";
527  $noTests = array();
528  $noTests= preg_grep('/NO.*TESTS/', $this->makeOutput);
529  //echo "grep for NoTests to... is:\n";print_r($noTests) . "\n";
530  if($nothing or $noTests)
531  {
532  //echo "No Unit Tests for module $unitTest\n";
533  $results['notest'] = TRUE;
534  }
535  // There can be Cunit failures, check for them
536  if($this->checkCunitTestErrors(implode("\n", $this->makeOutput)))
537  {
538  $results['cunit'] = TRUE;
539  /*
540  $pattern = $this->unitTest . "_Tests summary:";
541  $found = array_search($pattern, $this->makeOutput);
542  if($found !== FALSE)
543  {
544  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
545  }
546  */
547  }
548  return($results);
549  }
550  }// MakeTest
551 
552  /*
553  * @todo see if checkMake and check Test Error functions can be combined into
554  * a single routine, checkPattern($string, $pat), returns boolean.
555  */
561  function CheckMakeErrors($makeString)
562  {
563  $matched = 0;
564  $matches = array();
565 
566  $pat = '/make.*?Error\s[0-9]+/';
567  $matched = preg_match($pat, $makeString, $matches);
568 
569  //echo "DB: matched is:$matched\n";
570  //echo "DB: this->makeOutput is:$this->makeOutput\n";
571  return($matched);
572  } // checkMakeErrors
573 
581  function CheckCunitTestErrors($makeString)
582  {
583  $matched = 0;
584  $matches = array();
585 
586  $pat = '/Number of failures:.*/';
587  $matched = preg_match($pat, $makeString, $matches);
588  if($matched == 0)
589  {
590  return(FALSE);
591  }
592  $number = explode(':', $matches[0]);
593  $value = trim($number[1]);
594  if($value > 0)
595  {
596  return(TRUE);
597  }
598  return(FALSE);
599  } // CheckCunitTestErrors
600 
608  function CheckPHPTestErrors($makeString)
609  {
610  $matched = 0;
611  $matches = array();
612 
613  $pat = '/FAILURES/';
614  $matched = preg_match($pat, $makeString, $matches);
615  return($matched);
616 
617  } // CheckPHPTestErrors
618 
628  function CheckOtherErrors($makeString)
629  {
630  $matched = 0;
631  $matches = array();
632 
633  $patterns = array('/.*\serror\s.*/', '/.*\sfault.*/');
634  foreach ($patterns as $pattern)
635  {
636  $matched = 0;
637  $matches = array();
638  $matched = preg_match($pattern, $makeString, $matches);
639  if($matched)
640  {
641  //debugprint($matches, "Found one: matches is:\n");
642  return($matches[0]);
643  }
644  }
645  return(implode("\n",$matches));
646  }
647 
657  function printResults($runResults)
658  {
659  global $failures;
660 
661  $failures = 0;
662  $cunitMake = FALSE;
663  $phpunitMake = FALSE;
664 
665  $test = $runResults['name'];
666  unset($runResults['name']);
667 
668  if($runResults['make'] === TRUE && $runResults['cunit'] === TRUE)
669  {
670  $cunitMake = TRUE;
671  //echo "DB: both make and cunit are true, both has been set\n";
672  }
673  if($runResults['make'] === TRUE && $runResults['phpunit'] === TRUE)
674  {
675  $phpunitMake = TRUE;
676  //echo "DB: both make and phpunit are true, both has been set\n";
677  }
678 
679  foreach($runResults as $key => $value)
680  {
681  switch($key)
682  {
683  case 'make':
684  if($value === TRUE)
685  {
686  if($cunitMake || $phpunitMake)
687  {
688  break;
689  }
690  echo "Error: there were $key errors for $test\n";
691  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
692  $failures++;
693  break;
694  }
695  case 'cunit':
696  if($value === TRUE)
697  {
698  echo "There were $key errors for $test\n";
699  $pattern = $this->unitTest . "_Tests summary:";
700  $found = array_search($pattern, $this->makeOutput);
701  if($found !== FALSE)
702  {
703  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
704  }
705  $failures++;
706  break;
707  }
708  case 'phpunit':
709  if($value === TRUE)
710  {
711  echo "Error: there were $key errors for $test\n";
712  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
713  $failures++;
714  break;
715  }
716  case 'notest':
717  if($value === TRUE)
718  {
719  echo "No tests for $test\n";
720  $failures++;
721  break;
722  }
723  case 'other':
724  if(empty($value))
725  {
726  break;
727  }
728  echo "Other errors for $test:\n";
729  echo $value . "\n";
730  $failures++;
731  break;
732  case 'nomosfunc':
733  if(empty($value))
734  {
735  break;
736  }
737  echo "Nomos diff errors for $test:\n";
738  echo $value . " diffs.\n";
739  $failures++;
740  break;
741  }
742  } //foreach $runResults
743  if($failures == 0)
744  {
745  echo "All tests passed for $test\n";
746  }
747  return ;
748  }
749 } // class RunTest
750 
751 function debugprint($val, $title)
752 {
753  echo $title . "\n";
754  print_r($val);
755  echo "\n";
756 }
757 
763 function MakeCover($unitTest)
764 {
765  if(empty($unitTest))
766  {
767  return(NULL);
768  }
769 
770  // make coverage
771  $lastCovr = exec('make coverage 2>&1', $covrOut, $covrRtn);
772  //echo "DB: Exit status of 'make coverage' of $unitTest is:$covrRtn\n";
773  $Cover = new RunTest($unitTest);
774  if($covrRtn != 0)
775  {
776  if($Cover->checkMakeErrors(implode("\n", $covrOut)))
777  {
778  echo "Error: 'make coverage' of $unitTest did not succeed, " .
779  "return code:$covrRtn\n";
780  $covrOut = array();
781  return($unitTest);
782  }
783  }
784  return(NULL);
785 }
786 ?>
printResults($runResults)
print the result array. This will print any informative messages including erorrs that may have occur...
debugprint($val, $title)
Print debug message.
Definition: common-ui.php:98
class for making an agent unit or functional test
MakeTest()
make tests in a directory and check output for errors and no tests.
CheckPHPTestErrors($makeString)
check for the word FAILURES in the output, this is what PHPUnit prints when there are any failures in...
CheckMakeErrors($makeString)
check the output of make for errors
CheckCunitTestErrors($makeString)
check the test output for cunit style failures
CheckOtherErrors($makeString)
Check for other common error strings.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695