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