FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_cp2foss.php
1 <?php
2 /*
3  Copyright (C) 2012-2014 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 require_once("./test_common.php");
20 
28 class test_cp2foss extends \PHPUnit\Framework\TestCase
29 {
30 
31  public $SYSCONF_DIR = "/usr/local/etc/fossology/";
32  public $DB_NAME;
33  public $PG_CONN;
34  public $DB_COMMAND;
35 
36  // fossology_testconfig is the temporary system configuration directory
37  // created by the src/testing/db/create_test_database.php script.
38  // It is initialized via the Makefile and passed in via the
39  // FOSSOLOGY_TESTCONFIG environment variable.
40  public $fossology_testconfig;
41 
42  // scheduler_path is the absolute path to the scheduler binary
43  public $scheduler_path;
44 
45  // cp2foss_path is the absolute path to the cp2foss binary
46  public $cp2foss_path;
47 
48  /* initialization */
49 
50  // this method is run once for the entire test class, before any of the
51  // test methods are executed.
52  public static function setUpBeforeClass()
53  {
54 
55  global $fossology_testconfig;
56  global $scheduler_path;
57  global $cp2foss_path;
58 
59  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
60 
66  $fossology_testconfig = getenv('FOSSOLOGY_TESTCONFIG');
67  fwrite(STDOUT, __METHOD__ . " got fossology_testconfig = '$fossology_testconfig'\n");
68 
69  /* locate cp2foss binary */
70  // first get the absolute path to the current fossology src/ directory
71  $fo_base_dir = realpath(__DIR__ . '/../..');
72  $cp2foss_path = $fo_base_dir . "/cli/cp2foss";
73  if (!is_executable($cp2foss_path)) {
74  print "Error: cp2foss path '" . $cp2foss_path . "' is not executable!\n";
75  exit(1);
76  }
77  $cp2foss_path .= " -s ";
78 
79  /* locate the scheduler binary */
80  $scheduler_path = $fossology_testconfig . "/mods-enabled/scheduler/agent/fo_scheduler";
81  if (!is_executable($scheduler_path)) {
82  print "Error: Scheduler path '$scheduler_path' is not executable!\n";
83  exit(1);
84  }
85 
86  /* invoke the scheduler */
87  $scheduler_cmd = "$scheduler_path --daemon --reset --verbose=952 -c $fossology_testconfig";
88  fwrite(STDOUT, "DEBUG: Starting scheduler with '$scheduler_cmd'\n");
89  exec($scheduler_cmd, $output, $return_var);
90  //print_r($output);
91  if ( $return_var != 0 ) {
92  print "Error: Could not start scheduler '$scheduler_path'\n";
93  print "$output\n";
94  exit(1);
95  }
96  print "\nStarting functional test for cp2foss. \n";
97 
98  }
99 
100 
101  // this method is run once before each test method defined for this test class.
102  protected function setUp()
103  {
104 
105  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
106 
107  //$SYSCONF_DIR = "/usr/local/etc/fossology/";
108  //$DB_NAME = "fossology";
109  //$DB_COMMAND = "../../testing/db/createTestDB.php";
110 
111  // these calls are deprecated with the new create_test_database call
112  //create_db();
113  //add_user();
114  //preparations();
115  //scheduler_operation();
116  }
117 
130  {
131  //global $SYSCONF_DIR;
132  global $fossology_testconfig;
133  global $cp2foss_path;
134 
135  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
136 
137  $test_dbh = connect_to_DB($fossology_testconfig);
138 
139  $auth = "--username fossy --password fossy -c $fossology_testconfig";
140 
142  $out = "";
143  $pos = 0;
144  $command = "$cp2foss_path $auth -q all -d 'regular expression testing' '../*.php' -v";
145  fwrite(STDOUT, "DEBUG: Running $command\n");
146  $last = exec("$command 2>&1", $out, $rtn);
147  //fwrite(STDOUT, "DEBUG: $out[5] \n");
148  //fwrite(STDOUT, print_r($out));
149  $upload_id = 0;
151  if ($out && $out[13]) {
152  $upload_id = get_upload_id($out[6]);
153  } else {
154  $this->assertFalse(true);
155  }
156  fwrite(STDOUT, "DEBUG: $upload_id \n");
157  $agent_status = 0;
158  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
159  $this->assertEquals(1, $agent_status);
160  $agent_status = 0;
161  $agent_status = check_agent_status($test_dbh, "nomos", $upload_id);
162  $this->assertEquals(1, $agent_status);
163  $agent_status = 0;
164  $agent_status = check_agent_status($test_dbh, "copyright", $upload_id);
165  $this->assertEquals(1, $agent_status);
166  $agent_status = 0;
167  $agent_status = check_agent_status($test_dbh, "pkgagent", $upload_id);
168  $this->assertEquals(1, $agent_status);
169 
171  $is_exist = check_file_uploadtree($test_dbh, 'src', $upload_id);
172  fwrite(STDOUT, "DEBUG: is_exist is:$is_exist\n");
173  $this->assertEquals(0, $is_exist);
174 
176  $out = "";
177  $pos = 0;
178  $command = "$cp2foss_path $auth ./test_cp2foss.php";
179  fwrite(STDOUT, "DEBUG: test_upload_from_server executing '$command'\n");
180  $last = exec("$command 2>&1", $out, $rtn);
181  #print "DEBUG: output is:\n";
182  #print_r($out);
183  //DEBUG
184  $repo_string = "Uploading to folder: 'Software Repository'";
185  $repo_pos = strpos($out[2], $repo_string);
186  $output_msg_count = count($out);
187  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos);\n";
188  $this->assertGreaterThan(0, $repo_pos);
189  print "DEBUG: \$this->assertEquals(4, $output_msg_count);\n";
190  $this->assertEquals(5, $output_msg_count);
191  $upload_id = 0;
193  if ($out && $out[4]) {
194  $upload_id = get_upload_id($out[4]);
195  print "DEBUG: Upload_id is $upload_id\n";
196  } else {
197  print "DEBUG: Did not get an upload_id!\n";
198  $this->assertFalse(true);
199  }
200  $agent_status = 0;
201  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
202  $this->assertEquals(1, $agent_status);
203 
205  $out = "";
206  $pos = 0;
207  $command = "$cp2foss_path $auth ./";
208  fwrite(STDOUT, "DEBUG: test_upload_from_server executing '$command'\n");
209  $last = exec("$command 2>&1", $out, $rtn);
210  // print_r($out);
211  $repo_string = "Uploading to folder: 'Software Repository'";
212  $repo_pos = strpos($out[2], $repo_string);
213  $output_msg_count = count($out);
214  $this->assertGreaterThan(0, $repo_pos);
215  $this->assertEquals(5, $output_msg_count);
216  $upload_id = 0;
218  if ($out && $out[4]) {
219  $upload_id = get_upload_id($out[4]);
220  } else {
221  $this->assertFalse(true);
222  }
223  $agent_status = 0;
224  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
225  $this->assertEquals(1, $agent_status);
226 
228  $out = "";
229  $pos = 0;
230  $upload_path = "upload_path";
231  $command = "$cp2foss_path $auth ./ -f $upload_path -d upload_des -q all -v";
232  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
233  $last = exec("$command 2>&1", $out, $rtn);
234  //print_r($out);
235  $repo_string = "Uploading to folder: '/$upload_path'";
236  $repo_pos = strpos($out[8], $repo_string);
237  $output_msg_count = count($out);
238  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos), $repo_string\n";
239  $this->assertGreaterThan(0, $repo_pos);
240  $scheduled_agent_info_1 = "agent_pkgagent is queued to run on";
241  $scheduled_agent_info_2 = "agent_nomos is queued to run on";
242  $scheduled_agent_info_3 = "agent_mimetype is queued to run on";
243  $scheduled_agent_info_4 = "agent_copyright is queued to run on";
244  $pos = false;
245  $pos = strpos($out[$output_msg_count - 1], $scheduled_agent_info_1);
246  $this->assertEquals(0, $pos);
247  $pos = false;
248  $pos = strpos($out[$output_msg_count - 2], $scheduled_agent_info_2);
249  $this->assertEquals(0, $pos);
250  $pos = false;
251  $pos = strpos($out[$output_msg_count - 4], $scheduled_agent_info_3);
252  $this->assertEquals(0, $pos);
253  $pos = false;
254  $pos = strpos($out[$output_msg_count - 6], $scheduled_agent_info_4);
255  $this->assertEquals(0, $pos, $out[$output_msg_count-4]);
256  $upload_id = 0;
257 
259  if ($out && $out[12]) {
260  $upload_id = get_upload_id($out[12]);
261  } else {
262  $this->assertFalse(true);
263  }
264  $agent_status = 0;
265  sleep(5);
266  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
267  $this->assertEquals(1, $agent_status);
268  $agent_status = 0;
269  $agent_status = check_agent_status($test_dbh, "copyright", $upload_id);
270  $this->assertEquals(1, $agent_status);
271  $agent_status = 0;
272  $agent_status = check_agent_status($test_dbh, "nomos", $upload_id);
273  $this->assertEquals(1, $agent_status);
274  $agent_status = 0;
275  $agent_status = check_agent_status($test_dbh, "mimetype", $upload_id);
276  $this->assertEquals(1, $agent_status);
277  $agent_status = 0;
278  $agent_status = check_agent_status($test_dbh, "pkgagent", $upload_id);
279  $this->assertEquals(1, $agent_status);
280 
283  $out = "";
284  $pos = 0;
285  $command = "$cp2foss_path $auth -q all -A -f test/exclude -n 'test exclue dir' -d 'test des exclude dir' -X .svn ./ -v";
286  fwrite(STDOUT, "DEBUG: Running $command\n");
287  $last = exec("$command 2>&1", $out, $rtn);
288  // print_r($out);
289  $upload_id = 0;
291  if ($out && $out[24]) {
292  $upload_id = get_upload_id($out[24]);
293  } else {
294  $this->assertFalse(true);
295  }
296  $agent_status = 0;
297  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
298  $this->assertEquals(1, $agent_status);
299 
302  $out = "";
303  $pos = 0;
304  $command = "$cp2foss_path $auth -q all -A -f 'regular expression testing' -n 'test globbing dir' -d 'test des globbing' '*.php' -v";
305  fwrite(STDOUT, "DEBUG: Running $command\n");
306  $last = exec("$command 2>&1", $out, $rtn);
307  //print_r($out);
308  $upload_id = 0;
310  if ($out && $out[18]) {
311  $upload_id = get_upload_id($out[18]);
312  } else {
313  $this->assertFalse(true);
314  }
315  fwrite(STDOUT, "Debug: upload_id is:$upload_id\n");
316  $agent_status = 0;
317  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
318  $this->assertEquals(1, $agent_status);
319  $agent_status = 0;
320  $agent_status = check_agent_status($test_dbh, "nomos", $upload_id);
321  $this->assertEquals(1, $agent_status);
322  $agent_status = 0;
323  $agent_status = check_agent_status($test_dbh, "copyright", $upload_id);
324  $this->assertEquals(1, $agent_status);
325  $agent_status = 0;
326  $agent_status = check_agent_status($test_dbh, "pkgagent", $upload_id);
327  $this->assertEquals(1, $agent_status);
328 
329  pg_close($test_dbh);
330 
331  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
332  }
333 
338  {
339  //global $SYSCONF_DIR;
340  global $fossology_testconfig;
341  global $cp2foss_path;
342 
343  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
344  $test_dbh = connect_to_DB($fossology_testconfig);
345 
346  $auth = "--username fossy --password fossy -c $fossology_testconfig";
348  $out = "";
349  $pos = 0;
350  $command = "$cp2foss_path $auth http://www.fossology.org/testdata/rpms/fedora/10/SRPMS/fossology-1.1.0-1.fc10.src.rpm -d 'fossology des' -f 'fossology path' -n 'test package'";
351  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
352  $last = exec("$command 2>&1", $out, $rtn);
353  sleep(50);
354  //print "DEBUG: output is:\n";
355  //print_r($out);
356 
357  $upload_id = 0;
359  if ($out && $out[5]) {
360  $upload_id = get_upload_id($out[5]);
361  } else {
362  $this->assertFalse(true);
363  }
364  $agent_status = 0;
365  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
366  $this->assertEquals(1, $agent_status);
367  $agent_status = 0;
369  $agent_status = check_agent_status($test_dbh,"nomos", $upload_id);
370  $this->assertEquals(0, $agent_status);
371 
372  pg_close($test_dbh);
373 
374  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
375 
376  }
377 
382  {
383  global $fossology_testconfig;
384  global $cp2foss_path;
385  //global $SYSCONF_DIR;
386 
387  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
388  $auth = "--username fossy --password fossy -c $fossology_testconfig";
390  $command = "$cp2foss_path $auth -h";
391  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
392  $last = exec("$command 2>&1", $out, $rtn);
393  $output_msg_count = count($out);
394  $this->assertEquals(68, $output_msg_count, "Test that the number of output lines from '$command' is 65");
395  // print_r($out);
397  $out = "";
398  $pos = 0;
399  $command = "$cp2foss_path $auth -Q";
400  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
401  $last = exec("$command 2>&1", $out, $rtn);
402  $output_msg_count = count($out);
403  $this->assertEquals(10, $output_msg_count);
405  $out = "";
406  $pos = 0;
407  $command = "$cp2foss_path $auth ";
408  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
409  $last = exec("$command 2>&1", $out, $rtn);
410  // print_r($out);
411  $output_msg = "FATAL: No files to upload were specified.";
412  $this->assertEquals($output_msg, $out[0]);
413 
414  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
415  }
416 
420  // this method is run once after each test method defined for this test class.
421  protected function tearDown()
422  {
423 
424  global $fossology_testconfig;
425 
426  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
427 
428  // TODO: Drop the test database
429 
430  //stop_scheduler();
431  //drop_db();
432  }
433 
434  // this method is run once for the entire test class, after all of the
435  // test methods are executed.
436  public static function tearDownAfterClass()
437  {
438 
439  global $fossology_testconfig;
440  global $scheduler_path;
441  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
442 
443  // stop the scheduler
444  print "Stopping the scheduler\n";
445  $scheduler_cmd = "$scheduler_path -k -c $fossology_testconfig";
446  fwrite(STDOUT, "DEBUG: command is $scheduler_cmd \n");
447  exec($scheduler_cmd, $output, $return_var);
448  if ( $return_var != 0 ) {
449  print "Error: Could not stop scheduler via '$scheduler_cmd'\n";
450  print "$output\n";
451  # exit(1);
452  }
453 
454  // time to drop the database
455 
456  sleep(10);
457  print "End of functional tests for cp2foss \n";
458 
459  }
460 }
461 
462 
test_upload_from_url()
upload from url
static setUpBeforeClass()
test_list_agent_and_others()
list agents and help msg, etc
tearDown()
clean the env
test_upload_from_server()
upload from server
test cli cp2foss