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 
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 = "/usr/local/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  $PG_CONN = 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 = "--username fossy --password fossy -c $fossology_testconfig -s ";
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  $repo_string = "Uploading to folder: 'Software Repository'";
104  $repo_pos = strpos($out[1], $repo_string);
105  $output_msg_count = count($out);
106  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos);\n";
107  $this->assertGreaterThan(0, $repo_pos);
108  print "DEBUG: \$this->assertEquals(4, $output_msg_count);\n";
109  $this->assertEquals(4, $output_msg_count);
110  $upload_id = 0;
112  if ($out && $out[3]) {
113  $upload_id = get_upload_id($out[3]);
114  print "DEBUG: Upload_id is $upload_id\n";
115  }
116  else {
117  print "DEBUG: Did not get an upload_id!\n";
118  $this->assertFalse(TRUE);
119  }
120  $agent_status = 0;
121  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
122  $this->assertEquals(1, $agent_status);
123 
125  $out = "";
126  $pos = 0;
127  $command = "$cp2foss_path $auth ./";
128  fwrite(STDOUT, "DEBUG: test_upload_from_server executing '$command'\n");
129  $last = exec("$command 2>&1", $out, $rtn);
130  $repo_string = "Uploading to folder: 'Software Repository'";
131  $repo_pos = strpos($out[1], $repo_string);
132  $output_msg_count = count($out);
133  $this->assertGreaterThan(0, $repo_pos);
134  $this->assertEquals(4, $output_msg_count);
135  $upload_id = 0;
137  if ($out && $out[3]) {
138  $upload_id = get_upload_id($out[3]);
139  } else $this->assertFalse(TRUE);
140  $agent_status = 0;
141  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
142  $this->assertEquals(1, $agent_status);
143 
145  $out = "";
146  $pos = 0;
147  $upload_path = "upload_path";
148  $command = "$cp2foss_path $auth ./ -f $upload_path -d upload_des -q all -v";
149  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
150  $last = exec("$command 2>&1", $out, $rtn);
151  $repo_string = "Uploading to folder: '/$upload_path'";
152  $repo_pos = strpos($out[7], $repo_string);
153  $output_msg_count = count($out);
154  print "DEBUG: \$this->assertGreaterThan(0, $repo_pos)\n";
155  $this->assertGreaterThan(0, $repo_pos);
156  $scheduled_agent_info_1 = "agent_pkgagent is queued to run on";
157  $scheduled_agent_info_2 = "agent_nomos is queued to run on";
158  $scheduled_agent_info_3 = "agent_monk is queued to run on";
159  $scheduled_agent_info_4 = "agent_mimetype is queued to run on";
160  $scheduled_agent_info_5 = "agent_copyright is queued to run on";
161  $pos = false;
162  $pos = strpos($out[$output_msg_count - 1], $scheduled_agent_info_1);
163  $this->assertEquals(0, $pos);
164  $pos = false;
165  $pos = strpos($out[$output_msg_count - 2], $scheduled_agent_info_2);
166  $this->assertEquals(0, $pos);
167  $pos = false;
168  $pos = strpos($out[$output_msg_count - 3], $scheduled_agent_info_3);
169  $this->assertEquals(0, $pos);
170  $pos = false;
171  $pos = strpos($out[$output_msg_count - 4], $scheduled_agent_info_4);
172  $this->assertEquals(0, $pos);
173  $pos = false;
174  $pos = strpos($out[$output_msg_count - 5], $scheduled_agent_info_5);
175  $this->assertEquals(0, $pos);
176  $upload_id = 0;
177 
179  if ($out && $out[11]) {
180  $upload_id = get_upload_id($out[11]);
181  } else $this->assertFalse(TRUE);
182  $agent_status = 0;
183  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
184  $this->assertEquals(1, $agent_status);
185  $agent_status = 0;
186  $agent_status = check_agent_status($test_dbh, "copyright", $upload_id);
187  $this->assertEquals(1, $agent_status);
188  $agent_status = 0;
189  $agent_status = check_agent_status($test_dbh, "nomos", $upload_id);
190  $this->assertEquals(1, $agent_status);
191  $agent_status = 0;
192  $agent_status = check_agent_status($test_dbh, "mimetype", $upload_id);
193  $this->assertEquals(1, $agent_status);
194  $agent_status = 0;
195  $agent_status = check_agent_status($test_dbh, "pkgagent", $upload_id);
196  $this->assertEquals(1, $agent_status);
197 
200  $out = "";
201  $pos = 0;
202  $command = "$cp2foss_path $auth -q all -A -f test/exclude -n 'test exclue dir' -d 'test des exclude dir' -X .svn ./ -v";
203  fwrite(STDOUT, "DEBUG: Running $command\n");
204  $last = exec("$command 2>&1", $out, $rtn);
205  $upload_id = 0;
207  if ($out && $out[23]) {
208  $upload_id = get_upload_id($out[23]);
209  } else $this->assertFalse(TRUE);
210  $agent_status = 0;
211  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
212  $this->assertEquals(1, $agent_status);
213 
214  pg_close($test_dbh);
215 
216  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
217  }
218 
219  // this method is run once for the entire test class, after all of the
220  // test methods are executed.
221  public static function tearDownAfterClass() {
222 
223  global $fossology_testconfig;
224  global $scheduler_path;
225 
226  // stop the scheduler
227  print "Stopping the scheduler\n";
228  $scheduler_cmd = "$scheduler_path -k -c $fossology_testconfig";
229  exec($scheduler_cmd, $output, $return_var);
230  if ( $return_var != 0 ) {
231  print "Error: Could not stop scheduler via '$scheduler_cmd'\n";
232  print "$output\n";
233 # exit(1);
234  }
235 
236  // time to drop the database
237 
238  print "End of functional tests for cp2foss \n";
239 
240  }
241 
242 }
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.