FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_cp2foss.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 require_once("./test_common.php");
13 
17 class test_cp2foss extends \PHPUnit\Framework\TestCase {
18 
19  public $DB_NAME;
20  public $PG_CONN;
21  public $DB_COMMAND;
22 
23  // fossology_testconfig is the temporary system configuration directory
24  // created by the src/testing/db/create_test_database.php script.
25  // It is initialized via the Makefile and passed in via the
26  // FOSSOLOGY_TESTCONFIG environment variable.
27  public $fossology_testconfig;
28 
29  // scheduler_path is the absolute path to the scheduler binary
30  public $scheduler_path;
31 
32  // cp2foss_path is the absolute path to the cp2foss binary
33  public $cp2foss_path;
34 
35  /* initialization */
36 
37  // this method is run once for the entire test class, before any of the
38  // test methods are executed.
39  public static function setUpBeforeClass() {
40 
41  global $fossology_testconfig;
42  global $scheduler_path;
43  global $cp2foss_path;
44  global $PG_CONN;
45 
46  $fossology_testconfig = getenv('FOSSOLOGY_TESTCONFIG');
48  if (empty($fossology_testconfig)) $fossology_testconfig = "/etc/fossology/";
49  fwrite(STDOUT, __METHOD__ . " got fossology_testconfig = '$fossology_testconfig'\n");
50 
51  $cp2foss_path = "cp2foss";
52 
53  /* locate the scheduler binary */
54  $scheduler_path = $fossology_testconfig . "/mods-enabled/scheduler/agent/fo_scheduler";
55  if (!is_executable($scheduler_path)) {
56  print "Error: Scheduler path '$scheduler_path' is not executable!\n";
57  exit(1);
58  }
59 
60  /* invoke the scheduler */
61  $scheduler_cmd = "$scheduler_path --daemon --reset --verbose=952 -c $fossology_testconfig";
62  print "DEBUG: Starting scheduler with '$scheduler_cmd'\n";
63  exec($scheduler_cmd, $output, $return_var);
64  //print_r($output);
65  if ( $return_var != 0 ) {
66  print "Error: Could not start scheduler '$scheduler_path'\n";
67  print "$output\n";
68  exit(1);
69  }
70 
71  connect_to_DB($fossology_testconfig); // connect db
72  add_user("fossy", "fossy"); // add account fossy/fossy
73 
74  print "\nStarting functional test for cp2foss. \n";
75 
76  }
77 
88  global $fossology_testconfig;
89  global $cp2foss_path;
90  global $PG_CONN;
91 
92  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
93 
94  $test_dbh = $PG_CONN;
95 
96  $auth = "--user fossy --password fossy -c $fossology_testconfig";
98  $out = "";
99  $pos = 0;
100  $command = "$cp2foss_path $auth ./test_cp2foss.php";
101  fwrite(STDOUT, "DEBUG: test_upload_from_server executing '$command'\n");
102  $last = exec("$command 2>&1", $out, $rtn);
103  #print "DEBUG: output is:\n";
104  #print_r($out);
105  fwrite(STDOUT, "DEBUG: Sleeping for 10 seconds (why?), because you have to wait for all the scheduled agents are finished.\n");
106  sleep(10);
107  //DEBUG
108  $repo_string = "Uploading to folder: 'Software Repository'";
109  $repo_pos = strpos($out[1], $repo_string);
110  $output_msg_count = count($out);
111  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos);\n";
112  $this->assertGreaterThan(0, $repo_pos);
113  print "DEBUG: \$this->assertEquals(4, $output_msg_count);\n";
114  $this->assertEquals(4, $output_msg_count);
115  $upload_id = 0;
117  if ($out && $out[3]) {
118  $upload_id = get_upload_id($out[3]);
119  print "DEBUG: Upload_id is $upload_id\n";
120  }
121  else {
122  print "DEBUG: Did not get an upload_id!\n";
123  $this->assertFalse(TRUE);
124  }
125  $agent_status = 0;
126  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
127  $this->assertEquals(1, $agent_status);
128 
130  $out = "";
131  $pos = 0;
132  $command = "$cp2foss_path $auth ./";
133  fwrite(STDOUT, "DEBUG: test_upload_from_server executing '$command'\n");
134  $last = exec("$command 2>&1", $out, $rtn);
135  print "DEBUG: output is:\n";
136  print_r($out);
137  print "DEBUG: Sleeping for 10 seconds (why?), because you have to wait for all the scheduled agents are finished.\n";
138  sleep(10);
139  // print_r($out);
140  $repo_string = "Uploading to folder: 'Software Repository'";
141  $repo_pos = strpos($out[1], $repo_string);
142  $output_msg_count = count($out);
143  $this->assertGreaterThan(0, $repo_pos);
144  $this->assertEquals(4, $output_msg_count);
145  $upload_id = 0;
147  if ($out && $out[3]) {
148  $upload_id = get_upload_id($out[3]);
149  } else $this->assertFalse(TRUE);
150  $agent_status = 0;
151  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
152  $this->assertEquals(1, $agent_status);
153 
155  $out = "";
156  $pos = 0;
157  $upload_path = "upload_path";
158  $command = "$cp2foss_path $auth ./ -f $upload_path -d upload_des -q all -v";
159  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
160  $last = exec("$command 2>&1", $out, $rtn);
161  sleep(10);
162  // print_r($out);
163  $repo_string = "Uploading to folder: '/$upload_path'";
164  $repo_pos = strpos($out[7], $repo_string);
165  $output_msg_count = count($out);
166  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos)\n";
167  $this->assertGreaterThan(0, $repo_pos);
168  $scheduled_agent_info_1 = "agent_pkgagent is queued to run on";
169  $scheduled_agent_info_2 = "agent_nomos is queued to run on";
170  $scheduled_agent_info_3 = "agent_mimetype is queued to run on";
171  $scheduled_agent_info_4 = "agent_copyright is queued to run on";
172  $pos = false;
173  $pos = strpos($out[$output_msg_count - 1], $scheduled_agent_info_1);
174  $this->assertEquals(0, $pos);
175  $pos = false;
176  $pos = strpos($out[$output_msg_count - 2], $scheduled_agent_info_2);
177  $this->assertEquals(0, $pos);
178  $pos = false;
179  $pos = strpos($out[$output_msg_count - 3], $scheduled_agent_info_3);
180  $this->assertEquals(0, $pos);
181  $pos = false;
182  $pos = strpos($out[$output_msg_count - 4], $scheduled_agent_info_4);
183  $this->assertEquals(0, $pos);
184  $upload_id = 0;
185 
187  if ($out && $out[11]) {
188  $upload_id = get_upload_id($out[11]);
189  } else $this->assertFalse(TRUE);
190  $agent_status = 0;
191  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
192  $this->assertEquals(1, $agent_status);
193  $agent_status = 0;
194  $agent_status = check_agent_status($test_dbh, "copyright", $upload_id);
195  $this->assertEquals(1, $agent_status);
196  $agent_status = 0;
197  $agent_status = check_agent_status($test_dbh, "nomos", $upload_id);
198  $this->assertEquals(1, $agent_status);
199  $agent_status = 0;
200  $agent_status = check_agent_status($test_dbh, "mimetype", $upload_id);
201  $this->assertEquals(1, $agent_status);
202  $agent_status = 0;
203  $agent_status = check_agent_status($test_dbh, "pkgagent", $upload_id);
204  $this->assertEquals(1, $agent_status);
205 
208  $out = "";
209  $pos = 0;
210  $command = "$cp2foss_path $auth -q all -A -f test/exclude -n 'test exclue dir' -d 'test des exclude dir' -X .svn ./ -v";
211  fwrite(STDOUT, "DEBUG: Running $command\n");
212  $last = exec("$command 2>&1", $out, $rtn);
213  sleep(10);
214  // print_r($out);
215  $upload_id = 0;
217  if ($out && $out[23]) {
218  $upload_id = get_upload_id($out[23]);
219  } else $this->assertFalse(TRUE);
220  $agent_status = 0;
221  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
222  $this->assertEquals(1, $agent_status);
223 
224  pg_close($test_dbh);
225 
226  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
227  }
228 
229  // this method is run once for the entire test class, after all of the
230  // test methods are executed.
231  public static function tearDownAfterClass() {
232 
233  global $fossology_testconfig;
234  global $scheduler_path;
235 
236  // stop the scheduler
237  print "Stopping the scheduler\n";
238  $scheduler_cmd = "$scheduler_path -k -c $fossology_testconfig";
239  exec($scheduler_cmd, $output, $return_var);
240  if ( $return_var != 0 ) {
241  print "Error: Could not stop scheduler via '$scheduler_cmd'\n";
242  print "$output\n";
243 # exit(1);
244  }
245 
246  // time to drop the database
247 
248  print "End of functional tests for cp2foss \n";
249 
250  }
251 
252 }
test cli cp2foss
test_upload_from_server()
upload from server
static setUpBeforeClass()
add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.