FOSSology  4.4.0
Open Source License Compliance by Open Source Software
regressiontest.php
1 #!/usr/bin/php
2 <?php
3 /*
4 SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
5 
6 SPDX-License-Identifier: GPL-2.0-only
7 */
8 
41 // $DATAROOTDIR and $PROJECT come from Makefile
42 //require_once "$DATAROOTDIR/$PROJECT/lib/php/bootstrap.php";
43 require_once "/usr/local/share/fossology/lib/php/bootstrap.php";
44 
45 $SysConf = array(); // fo system configuration variables
46 $PG_CONN = 0; // Database connection
47 
48 /* Set SYSCONFDIR and set global (for backward compatibility) */
49 $SysConf = bootstrap();
50 
51 /* Initialize global system configuration variables $SysConfig[] */
52 ConfigInit($SYSCONFDIR, $SysConf);
53 
54 /* Directories */
55 $GoodDir = "regression/good/";
56 $PostDir = "regression/post/";
57 $OutputDirTemplate = "regression/output_"; /* output dir is suffixed with pid */
58 $OutputDir = $OutputDirTemplate . posix_getpid();
59 
60 /* -h webhost
61  */
62 $Options = getopt("hw:");
63 if ( array_key_exists('h', $Options))
64 {
65  Usage($argc, $argv);
66  exit(0);
67 }
68 
69 if ( array_key_exists('w', $Options))
70 {
71  $WebHost = $Options['w'];
72 }
73 else
74 {
75  $WebHost = "localhost";
76 }
77 
78 /* Create directory to put results */
79 if (mkdir($OutputDir, 0777, true) === false)
80 {
81  echo "Fatal: Could not create $OutputDir\n";
82  exit(-1);
83 }
84 
85 /* Open $GoodDir */
86 if (($DirH = opendir($GoodDir)) === false)
87 {
88  echo "Fatal: Could not create $OutputDir\n";
89  exit(-1);
90 }
91 
92 /* Loop through $GoodDir files */
93 $FileCount = 0;
94 $GoodFileCount = 0;
95 $BadFileCount = 0;
96 while (($FileName = readdir($DirH)) !== false)
97 {
98  if ($FileName[0] == '.') continue;
99 
100  $FileCount++;
101 
102  /* $FileName is a URL, hit it and save the results. */
103  $URL = $WebHost . "/$FileName";
104 //echo "URL is $URL\n";
105  $ch = curl_init($URL);
106  SetCurlArgs($ch);
107  $contents = curl_exec( $ch );
108  curl_close( $ch );
109 
110  /* Save the output in $OutputDir */
111  $OutFileName = $OutputDir . "/$FileName";
112  if (file_put_contents($OutFileName, $contents) === false)
113  {
114  echo "Failed to write contents to $OutFileName.\n";
115  }
116 
117  /* Get the good file contents */
118  $GoodFileName = $GoodDir . "$FileName";
119  if (($GoodContents = file_get_contents($GoodFileName)) === false)
120  {
121  echo "Failed to read good contents from $GoodFileName.\n";
122  }
123 
124  /* compare good and output file contents */
125  if ($GoodContents != $contents)
126  {
127  echo "Regression found: Baseline is $GoodFileName, new content is $OutFileName\n";
128  $BadFileCount++;
129  }
130  else
131  $GoodFileCount++;
132 }
133 
134 echo "Total files checked: $FileCount\n";
135 echo "No regression in: $GoodFileCount\n";
136 echo "Regression found in: $BadFileCount\n";
137 
138 return (0);
139 
144 function SetCurlArgs($ch)
145 {
146  global $SysConf;
147 // curl_setopt($ch,CURLOPT_USERAGENT,'Curl-php');
148  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
149  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
150  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
151  curl_setopt($ch,
152  CURLOPT_HTTPHEADER, array("Content-Type:
153  text/html; charset=utf-8"));
154 
155  /* parse http_proxy server and port */
156  $http_proxy = $SysConf['FOSSOLOGY']['http_proxy'];
157  $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy, ":"));
158  $ProxyPort = substr(strrchr($http_proxy, ":"), 1);
159  if (!empty($ProxyServer))
160  {
161  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
162  curl_setopt($ch, CURLOPT_PROXY, $ProxyServer);
163  if (!empty($ProxyPort)) curl_setopt($ch, CURLOPT_PROXYPORT, $ProxyPort);
164  curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
165  }
166 }
167 
173 function Usage($argc, $argv)
174 {
175  echo "$argv[0] -h -w {web host}\n";
176  echo " -h help\n";
177  echo " -w Web Host. Optional.\n";
178  echo 'e.g.: ./regressiontest.php -w "http://bobg.fc.hp.com/trunk"\n';
179 }
ConfigInit($sysconfdir, &$SysConf, $exitOnDbFail=true)
Initialize the fossology system after bootstrap().
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:82