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