FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
regressiontest.php
1 #!/usr/bin/php
2 <?php
3 /***********************************************************
4  Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
52 // $DATAROOTDIR and $PROJECT come from Makefile
53 //require_once "$DATAROOTDIR/$PROJECT/lib/php/bootstrap.php";
54 require_once "/usr/local/share/fossology/lib/php/bootstrap.php";
55 
56 $SysConf = array(); // fo system configuration variables
57 $PG_CONN = 0; // Database connection
58 
59 /* Set SYSCONFDIR and set global (for backward compatibility) */
60 $SysConf = bootstrap();
61 
62 /* Initialize global system configuration variables $SysConfig[] */
63 ConfigInit($SYSCONFDIR, $SysConf);
64 
65 /* Directories */
66 $GoodDir = "regression/good/";
67 $PostDir = "regression/post/";
68 $OutputDirTemplate = "regression/output_"; /* output dir is suffixed with pid */
69 $OutputDir = $OutputDirTemplate . posix_getpid();
70 
71 /* -h webhost
72  */
73 $Options = getopt("hw:");
74 if ( array_key_exists('h', $Options))
75 {
76  Usage($argc, $argv);
77  exit(0);
78 }
79 
80 if ( array_key_exists('w', $Options))
81 {
82  $WebHost = $Options['w'];
83 }
84 else
85 {
86  $WebHost = "localhost";
87 }
88 
89 /* Create directory to put results */
90 if (mkdir($OutputDir, 0777, true) === false)
91 {
92  echo "Fatal: Could not create $OutputDir\n";
93  exit(-1);
94 }
95 
96 /* Open $GoodDir */
97 if (($DirH = opendir($GoodDir)) === false)
98 {
99  echo "Fatal: Could not create $OutputDir\n";
100  exit(-1);
101 }
102 
103 /* Loop through $GoodDir files */
104 $FileCount = 0;
105 $GoodFileCount = 0;
106 $BadFileCount = 0;
107 while (($FileName = readdir($DirH)) !== false)
108 {
109  if ($FileName[0] == '.') continue;
110 
111  $FileCount++;
112 
113  /* $FileName is a URL, hit it and save the results. */
114  $URL = $WebHost . "/$FileName";
115 //echo "URL is $URL\n";
116  $ch = curl_init($URL);
117  SetCurlArgs($ch);
118  $contents = curl_exec( $ch );
119  curl_close( $ch );
120 
121  /* Save the output in $OutputDir */
122  $OutFileName = $OutputDir . "/$FileName";
123  if (file_put_contents($OutFileName, $contents) === false)
124  {
125  echo "Failed to write contents to $OutFileName.\n";
126  }
127 
128  /* Get the good file contents */
129  $GoodFileName = $GoodDir . "$FileName";
130  if (($GoodContents = file_get_contents($GoodFileName)) === false)
131  {
132  echo "Failed to read good contents from $GoodFileName.\n";
133  }
134 
135  /* compare good and output file contents */
136  if ($GoodContents != $contents)
137  {
138  echo "Regression found: Baseline is $GoodFileName, new content is $OutFileName\n";
139  $BadFileCount++;
140  }
141  else
142  $GoodFileCount++;
143 }
144 
145 echo "Total files checked: $FileCount\n";
146 echo "No regression in: $GoodFileCount\n";
147 echo "Regression found in: $BadFileCount\n";
148 
149 return (0);
150 
155 function SetCurlArgs($ch)
156 {
157  global $SysConf;
158 // curl_setopt($ch,CURLOPT_USERAGENT,'Curl-php');
159  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
160  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
161  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
162  curl_setopt($ch,
163  CURLOPT_HTTPHEADER, array("Content-Type:
164  text/html; charset=utf-8"));
165 
166  /* parse http_proxy server and port */
167  $http_proxy = $SysConf['FOSSOLOGY']['http_proxy'];
168  $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy, ":"));
169  $ProxyPort = substr(strrchr($http_proxy, ":"), 1);
170  if (!empty($ProxyServer))
171  {
172  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
173  curl_setopt($ch, CURLOPT_PROXY, $ProxyServer);
174  if (!empty($ProxyPort)) curl_setopt($ch, CURLOPT_PROXYPORT, $ProxyPort);
175  curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
176  }
177 }
178 
184 function Usage($argc, $argv)
185 {
186  echo "$argv[0] -h -w {web host}\n";
187  echo " -h help\n";
188  echo " -w Web Host. Optional.\n";
189  echo 'e.g.: ./regressiontest.php -w "http://bobg.fc.hp.com/trunk"\n';
190 }
191 
192 ?>
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:75
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:93
ConfigInit($sysconfdir, &$SysConf)
Initialize the fossology system after bootstrap().