FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-Test.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
23 function addProxy($sysConfPath, $proxy='lart.usa.hp.com:3128')
24 {
25  if(file_exists($sysConfPath . '/fossology.conf'))
26  {
27  $fossConf = file_get_contents($sysConfPath . '/fossology.conf');
28  if($fossConf === FALSE)
29  {
30  echo "ERROR! could not read\n$sysConfPath/fossology.conf\n";
31  return(FALSE);
32  }
33  $count = 0;
34  $pat = ';no_proxy = localhost,10.1.2.3';
35  $replacepat = $pat . "\n" . 'http_proxy = ' . $proxy . "\n";
36  //echo "replacepat is:$replacepat\n";
37  $testConf = preg_replace("/.*?no_proxy = .*/", $replacepat, $fossConf,-1,$count);
38  //echo "DB: matched pats is:$count\n";
39  //echo "DB: testConf is:$testConf\n";
40 
41  $stat = file_put_contents("$sysConfPath/fossology.conf",$testConf);
42  if($stat === FALSE)
43  {
44  echo "ERROR! could not write\n$sysConfPath/fossology.conf\n";
45  return(FALSE);
46  }
47  }
48  else
49  {
50  echo "ERROR! can't find fossology.conf at:\n$sysConfPath/fossology.conf\n";
51  return(FALSE);
52  }
53  return(TRUE);
54 }
55 
68 function allFilePaths($dir) {
69 
70  $fileList = array();
71  if(empty($dir)) {
72  return($fileList); // nothing to process, return empty list.
73  }
74  try {
75  foreach(new recursiveIteratorIterator(
76  new recursiveDirectoryIterator($dir)) as $file) {
77  $fileList[] = $file->getPathName($file);
78  }
79  return($fileList);
80  }
81  /*
82  * if the directory does not exist or the directory or a sub directory
83  * does not have sufficent permissions for reading return an empty list
84  */
85  catch(Exception $e) {
86  print $e->getMessage();
87  return(array());
88  }
89 } //allFilePaths
90 
99 function backToParent($howFar)
100 {
101  if(empty($howFar))
102  {
103  echo "FATAL! No input at line " . __LINE__ . " in " . __FILE__ . "\n";
104  exit(1);
105  }
106 
107  $here = getcwd();
108 
109  if(@chdir($howFar) == FALSE)
110  {
111  echo "FATAL! could not cd from:\n$here to:\n$howFar\n" .
112  "at line " . __LINE__ . " in " . __FILE__ . "\n";
113  exit(1);
114 
115  }
116 } // backToParent
117 
131 function checkTestData()
132 {
133  $WORKSPACE = NULL;
134 
135  if(array_key_exists('WORKSPACE', $_ENV))
136  {
137  $WORKSPACE = $_ENV['WORKSPACE'];
138  }
139  if(is_null($WORKSPACE))
140  {
141  // cd to ....fossology/src
142  backToParent('..');
143  }
144  else
145  {
146  if(@chdir($WORKSPACE . "/fossology/src") === FALSE)
147  {
148  echo "FATAL! runRUnit could not cd to " . $WORKSPACE . "/fossology/src\n";
149  exit(1);
150  }
151  }
152  $home = getcwd();
153  $dirs = explode('/',$home);
154  $size = count($dirs);
155  // are we being run by jenkins? if we are not in fossology/tests, cd there
156  if($dirs[$size-1] == 'workspace' )
157  {
158  if(chdir('fossology/tests') === FALSE)
159  {
160  echo "FATAL! Cannot cd to fossology/tests from" . getcwd() . "\n";
161  exit(1);
162  }
163  $home = getcwd(); // home should now be ...workspace/fossology/tests
164  }
165 
166  $redHatPath = 'nomos/testdata';
167  $unpackTestFile = '../ununpack/agent_tests/test-data/testdata4unpack/argmatch.c.gz';
168  $unpackTests = '../ununpack/agent_tests';
169  $redHatDataFile = 'RedHat.tar.gz';
170  $unpackDataFile = 'unpack-test-data.tar.bz2';
171  $wgetOptions = ' -a wget.log --tries=3 ';
172  $proxy = 'export http_proxy=lart.usa.hp.com:3128;';
173  $Url = 'http://fossology.org/testing/testFiles/';
174 
175  $errors = 0;
176  // check/install RedHat.tar.gz
177 
178  /*
179  if(!file_exists($redHatPath . "/" . $redHatDataFile))
180  {
181  if(chdir($redHatPath) === FALSE)
182  {
183  echo "ERROR! could not cd to $redHatPath, cannot download $redHatDataFile\n";
184  $errors++;
185  }
186  $cmd = $proxy . "wget" . $wgetOptions . $Url . $redHatDataFile;
187  $last = exec($cmd, $wgetOut, $wgetRtn);
188  if($wgetRtn != 0)
189  {
190  echo "ERROR! Download of $Url$redHatDataFile failed\n";
191  echo "Errors were:\n$last\n";print_r($wgetOut) . "\n";
192  $errors++;
193  }
194  }
195  else
196 
197  if(chdir($home) === FALSE)
198  {
199  echo "FATAL! could not cd to $home\n";
200  exit(1);
201  }
202  */
203 
204  // check/install ununpack data
205  echo "downloading unpack data.....\n";
206  if(!file_exists($unpackTestFile))
207  {
208  echo "$unpackTestFile DOES NOT EXIST!, need to download data files...\n";
209  if(chdir($unpackTests) === FALSE)
210  {
211  echo "FATAL! cannot cd to $unpackTests\n";
212  exit(1);
213  }
214  $cmd = $proxy . "wget" . $wgetOptions . $Url . '/' . $unpackDataFile;
215  $unpkLast = exec($cmd, $unpkOut, $unpkRtn);
216  if($unpkRtn != 0)
217  {
218  echo "ERROR! Download of $Url$unpackDataFile failed\n";
219  echo "Errors were:\n";print_r($unpkOut) . "\n";
220  $errors++;
221  }
222  // unpack the tar file.
223  $cmd = "tar -xf $unpackDataFile";
224  $tarLast = exec($cmd, $tarOut, $tarRtn);
225  if($tarRtn != 0)
226  {
227  echo "ERROR! un tar of $unpackDataFile failed\n";
228  echo "Errors were:\n$tarLast\n";print_r($tarOut) . "\n";
229  $errors++;
230  }
231  }
232 
233  if($errors)
234  {
235  exit(1);
236  }
237  exit(0);
238 } // checkTestData
239 
245 function cleanXMLFiles()
246 {
247  global $unitList;
248  reset($unitList);
249  foreach($unitList as $unitTest)
250  {
251  $other = substr($unitTest, 0, 3);
252  if($other == 'lib' || $other == 'cli')
253  {
254  if(@chdir($unitTest . '/tests') === FALSE)
255  {
256  echo "Error! cannot cd to " . $unitTest . "/tests, skipping test\n";
257  $failures++;
258  continue;
259  }
260  }
261  else
262  {
263  if(@chdir($unitTest . '/agent_tests/Unit') === FALSE)
264  {
265  echo "Error! cannot cd to " . $unitTest . "/agent_tests/Unit, skipping test\n";
266  $failures++;
267  continue;
268  }
269  }
270  foreach(glob("$unitTest*.xml") as $fName)
271  {
272  $lsOut = array();
273  $rmOut = array();
274  $fileName = lcfirst($fName);
275  $last = exec("rm $fileName", $rmOut, $rmRtn);
276  if($rmRtn != 0)
277  {
278  echo "Notice: could not remove $filename, please remove by hand\n";
279  }
280  } // foreach
281  backToParent('../../..');
282  } // foreach
283  return;
284 }
285 
293 function escapeDots($string)
294 {
295  if (empty ($string))
296  {
297  return (FALSE);
298  }
299  $estring = preg_replace('/\./', '\\.', $string);
300  //print "ED: string is:$string, estring is:$estring\n";
301  if ($estring === NULL)
302  {
303  return (FALSE);
304  }
305  return ($estring);
306 } //escapeDots
307 
308 function lastDir($dirpath) {
309  // can't have a tailing slash, remove it if there
310  $dirpath = rtrim($dirpath, '/');
311  $directories = explode('/',$dirpath);
312  return(end($directories));
313 }
314 
328 function filesByDir($dir) {
329 
330  $ByDir = array();
331  $fileList = array();
332  $subPath = '';
333 
334  if(empty($dir)) {
335  return($fileList); // nothing to process, return empty list.
336  }
337 
338  try {
339  $dirObject = new recursiveIteratorIterator(
340  new recursiveDirectoryIterator($dir),RecursiveIteratorIterator::SELF_FIRST);
341  // dirobjs is recusiveIteratorIterator object
342  foreach($dirObject as $name) {
343 
344  $aSubPath = $dirObject->getSubPath();
345 
346  /*
347  * if we changed subpaths, we are in a new sub-dir, reset the file list
348  */
349  if($aSubPath != $subPath) {
350  //print "DB: fileByDir: asb != sb, Init fileList!\n";
351  $fileList = array();
352  }
353 
354  if(is_file($name)) {
355  $subPath = $dirObject->getSubPath();
356  $spn = $dirObject->getSubPathName();
357  $subDir = dirname($spn);
358  if($subDir == $aSubPath) {
359  $fileName = $dirObject->getFilename();
360  $fileList[] = $fileName;
361  }
362  }
363  if (empty($subPath)){
364  continue;
365  }
366  else {
367  if(empty($fileList)){
368  continue;
369  }
370  $ByDir[$subPath] = $fileList;
371  }
372 
373  /* Debug
374  *
375  $subPath = $dirObject->getSubPath();
376  print "DB: fileByDir: subpath is:$subPath\n";
377  $sbn = $dirObject->getSubPathName();
378  print "DB: fileByDir: subpathname is:$sbn\n";
379  $dirpath = $dirObject->getPath();
380  print "DB: fileByDir: dirpath is:$dirpath\n";
381 
382  */
383 
384  } // foreach
385  //print "DB: fileByDir: ByDir is:\n ";print_r($ByDir) . "\n";
386  return($ByDir);
387  }
388 
389  /*
390  if the directory does not exist or the directory or a sub directory
391  does not have sufficent permissions for reading return an empty list
392  */
393  catch(Exception $e) {
394  //print "in exception!\n$e\n";
395  return(array());
396  }
397 } // fileByDir
398 
405 class RunTest
406 {
407  public $makeOutput = array();
408  protected $unitTest;
409  protected $makeErrors;
410  protected $cunitErrors;
411  protected $phpunitErrors;
412 
413  function __construct($unitTest)
414  {
415  $this->unitTest = $unitTest;
416  $this->makeErrors = FALSE;
417  $this->cunitErrors = FALSE;
418  $this->phpunitErrors = FALSE;
419  }
420 
437  function MakeTest()
438  {
439 
440  $results = array(
441  'name'=> $this->unitTest,
442  'make' => FALSE,
443  'cunit' => FALSE,
444  'phpunit' => FALSE,
445  'notest' => FALSE,
446  'other' => NULL,
447  'nomosfunc' => NULL,
448  );
449 
450  $cleanMake = exec('make clean 2>&1', $cleanOut, $cleanRtn);
451  if($cleanRtn != 0)
452  {
453  echo "Make clean of $this->unitTest did not succeed, return code:$cleanRtn\n";
454  // right now this is not reported as an error
455  // @todo figure out how to handle this. Make clean failures should not
456  // cause make to not be done.
457  }
458  //echo "ComTest: sysconfdir from globals is:{$GLOBALS['SYSCONFDIR']}\n";
459  $s = getenv('SYSCONFDIR');
460  //echo "DB: sysconfdir from env before make of $this->unitTest is:$s\n";
461  $lastMake = exec('make test 2>&1', $this->makeOutput, $makeRtn);
462  //echo "DB: Exit status of 'make test' of $this->unitTest is:$makeRtn\n";
463  //debugprint($this->makeOutput, "make output\n");
464 
465  if($makeRtn != 0)
466  {
467  $found = array();
468  $found = preg_grep('/No rule to make target/', $this->makeOutput);
469  if($found)
470  {
471  $results['notest'] = TRUE;
472  //echo "No Unit Tests for module $unitTest\n";
473  }
474  else
475  {
476  // check for real make errors and test errors.
477  if($this->CheckMakeErrors(implode("\n", $this->makeOutput)))
478  {
479  //echo "Error! There were make errors for unit test $unitTest\n";
480  $results['make'] = TRUE;
481  }
482  if($this->checkCunitTestErrors(implode("\n", $this->makeOutput)))
483  {
484  $results['cunit'] = TRUE;
485  /*
486  $pattern = $this->unitTest . "_Tests summary:";
487  $found = array_search($pattern, $this->makeOutput);
488  if($found !== FALSE)
489  {
490  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
491  }
492  */
493  }
494  if($this->checkPHPTestErrors(implode("\n", $this->makeOutput)))
495  {
496  $results['phpunit'] = TRUE;
497  }
498  $other = $this->CheckOtherErrors(implode("\n", $this->makeOutput));
499  if($other)
500  {
501  $results['other'] = $other;
502  }
503  }
504  return($results);
505  }
506  // Make returned zero
507  else
508  {
509  // no tests for is module? Skip report processing
510  $nothing = array();
511  $nothing= preg_grep("/Nothing to be done for \`test\'/", $this->makeOutput);
512  //echo "grep for Nothing to... is:\n";print_r($nothing) . "\n";
513  $noTests = array();
514  $noTests= preg_grep('/NO.*TESTS/', $this->makeOutput);
515  //echo "grep for NoTests to... is:\n";print_r($noTests) . "\n";
516  if($nothing or $noTests)
517  {
518  //echo "No Unit Tests for module $unitTest\n";
519  $results['notest'] = TRUE;
520  }
521  // There can be Cunit failures, check for them
522  if($this->checkCunitTestErrors(implode("\n", $this->makeOutput)))
523  {
524  $results['cunit'] = TRUE;
525  /*
526  $pattern = $this->unitTest . "_Tests summary:";
527  $found = array_search($pattern, $this->makeOutput);
528  if($found !== FALSE)
529  {
530  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
531  }
532  */
533  }
534  return($results);
535  }
536  }// MakeTest
537 
538  /*
539  * @todo see if checkMake and check Test Error functions can be combined into
540  * a single routine, checkPattern($string, $pat), returns boolean.
541  */
547  function CheckMakeErrors($makeString)
548  {
549  $matched = 0;
550  $matches = array();
551 
552  $pat = '/make.*?Error\s[0-9]+/';
553  $matched = preg_match($pat, $makeString, $matches);
554 
555  //echo "DB: matched is:$matched\n";
556  //echo "DB: this->makeOutput is:$this->makeOutput\n";
557  return($matched);
558  } // checkMakeErrors
559 
567  function CheckCunitTestErrors($makeString)
568  {
569  $matched = 0;
570  $matches = array();
571 
572  $pat = '/Number of failures:.*/';
573  $matched = preg_match($pat, $makeString, $matches);
574  if($matched == 0)
575  {
576  return(FALSE);
577  }
578  $number = explode(':', $matches[0]);
579  $value = trim($number[1]);
580  if($value > 0)
581  {
582  return(TRUE);
583  }
584  return(FALSE);
585  } // CheckCunitTestErrors
586 
594  function CheckPHPTestErrors($makeString)
595  {
596  $matched = 0;
597  $matches = array();
598 
599  $pat = '/FAILURES/';
600  $matched = preg_match($pat, $makeString, $matches);
601  return($matched);
602 
603  } // CheckPHPTestErrors
604 
614  function CheckOtherErrors($makeString)
615  {
616  $matched = 0;
617  $matches = array();
618 
619  $patterns = array('/.*\serror\s.*/', '/.*\sfault.*/');
620  foreach ($patterns as $pattern)
621  {
622  $matched = 0;
623  $matches = array();
624  $matched = preg_match($pattern, $makeString, $matches);
625  if($matched)
626  {
627  //debugprint($matches, "Found one: matches is:\n");
628  return($matches[0]);
629  }
630  }
631  return(implode("\n",$matches));
632  }
633 
643  function printResults($runResults)
644  {
645  global $failures;
646 
647  $failures = 0;
648  $cunitMake = FALSE;
649  $phpunitMake = FALSE;
650 
651  $test = $runResults['name'];
652  unset($runResults['name']);
653 
654  if($runResults['make'] === TRUE && $runResults['cunit'] === TRUE)
655  {
656  $cunitMake = TRUE;
657  //echo "DB: both make and cunit are true, both has been set\n";
658  }
659  if($runResults['make'] === TRUE && $runResults['phpunit'] === TRUE)
660  {
661  $phpunitMake = TRUE;
662  //echo "DB: both make and phpunit are true, both has been set\n";
663  }
664 
665  foreach($runResults as $key => $value)
666  {
667  switch($key)
668  {
669  case 'make':
670  if($value === TRUE)
671  {
672  if($cunitMake || $phpunitMake)
673  {
674  break;
675  }
676  echo "Error: there were $key errors for $test\n";
677  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
678  $failures++;
679  break;
680  }
681  case 'cunit':
682  if($value === TRUE)
683  {
684  echo "There were $key errors for $test\n";
685  $pattern = $this->unitTest . "_Tests summary:";
686  $found = array_search($pattern, $this->makeOutput);
687  if($found !== FALSE)
688  {
689  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
690  }
691  $failures++;
692  break;
693  }
694  case 'phpunit':
695  if($value === TRUE)
696  {
697  echo "Error: there were $key errors for $test\n";
698  echo implode("\n", array_slice($this->makeOutput, -7)) . "\n";
699  $failures++;
700  break;
701  }
702  case 'notest':
703  if($value === TRUE)
704  {
705  echo "No tests for $test\n";
706  $failures++;
707  break;
708  }
709  case 'other':
710  if(empty($value))
711  {
712  break;
713  }
714  echo "Other errors for $test:\n";
715  echo $value . "\n";
716  $failures++;
717  break;
718  case 'nomosfunc':
719  if(empty($value))
720  {
721  break;
722  }
723  echo "Nomos diff errors for $test:\n";
724  echo $value . " diffs.\n";
725  $failures++;
726  break;
727  }
728  } //foreach $runResults
729  if($failures == 0)
730  {
731  echo "All tests passed for $test\n";
732  }
733  return ;
734  }
735 } // class RunTest
736 
737 function debugprint($val, $title)
738 {
739  echo $title . "\n";
740  print_r($val);
741  echo "\n";
742 }
743 
749 function MakeCover($unitTest)
750 {
751  if(empty($unitTest))
752  {
753  return(NULL);
754  }
755 
756  // make coverage
757  $lastCovr = exec('make coverage 2>&1', $covrOut, $covrRtn);
758  //echo "DB: Exit status of 'make coverage' of $unitTest is:$covrRtn\n";
759  $Cover = new RunTest($unitTest);
760  if($covrRtn != 0)
761  {
762  if($Cover->checkMakeErrors(implode("\n", $covrOut)))
763  {
764  echo "Error: 'make coverage' of $unitTest did not succeed, " .
765  "return code:$covrRtn\n";
766  $covrOut = array();
767  return($unitTest);
768  }
769  }
770  return(NULL);
771 }
class for making an agent unit or functional test
printResults($runResults)
print the result array. This will print any informative messages including erorrs that may have occur...
CheckPHPTestErrors($makeString)
check for the word FAILURES in the output, this is what PHPUnit prints when there are any failures in...
CheckOtherErrors($makeString)
Check for other common error strings.
MakeTest()
make tests in a directory and check output for errors and no tests.
CheckMakeErrors($makeString)
check the output of make for errors
CheckCunitTestErrors($makeString)
check the test output for cunit style failures
debugprint($val, $title)
Print debug message.
Definition: common-ui.php:89
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690