FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_fossjobs.php
1 <?php
2 /*
3  Copyright (C) 2013-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_fossjobs extends \PHPUnit\Framework\TestCase
29 {
30 
31  // fossology_testconfig is the temporary system configuration directory
32  // created by the src/testing/db/create_test_database.php script.
33  // It is initialized via the Makefile and passed in via the
34  // FOSSOLOGY_TESTCONFIG environment variable.
35  public $fossology_testconfig;
36 
37  // scheduler_path is the absolute path to the scheduler binary
38  public $scheduler_path;
39 
40  // cp2foss_path is the absolute path to the cp2foss binary
41  public $cp2foss_path;
42 
43  // fossjobs_path is the absolute path to the fossjobs binary
44  public $fossjobs_path;
45 
46  // this method is run once for the entire test class, before any of the
47  // test methods are executed.
48  public static function setUpBeforeClass()
49  {
50 
51  global $fossology_testconfig;
52  global $scheduler_path;
53  global $cp2foss_path;
54  global $fossjobs_path;
55 
56  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
57 
63  $fossology_testconfig = getenv('FOSSOLOGY_TESTCONFIG');
64  fwrite(STDOUT, __METHOD__ . " got fossology_testconfig = '$fossology_testconfig'\n");
65 
66  /* locate cp2foss binary */
67  // first get the absolute path to the current fossology src/ directory
68  $fo_base_dir = realpath(__DIR__ . '/../..');
69  $cp2foss_path = $fo_base_dir . "/cli/cp2foss";
70  if (!is_executable($cp2foss_path)) {
71  print "Error: cp2foss path '" . $cp2foss_path . "' is not executable!\n";
72  exit(1);
73  }
74 
75  /* locate fossjobs binary */
76  $fossjobs_path = $fo_base_dir . "/cli/fossjobs";
77  if (!is_executable($fossjobs_path)) {
78  print "Error: fossjobs path '" . $cp2foss_path . "' is not executable!\n";
79  exit(1);
80  }
81 
82  /* locate the scheduler binary */
83  $scheduler_path = $fossology_testconfig . "/mods-enabled/scheduler/agent/fo_scheduler";
84  if (!is_executable($scheduler_path)) {
85  print "Error: Scheduler path '$scheduler_path' is not executable!\n";
86  exit(1);
87  }
88 
89  /* invoke the scheduler */
90  $scheduler_cmd = "$scheduler_path --daemon --reset --verbose=952 -c $fossology_testconfig";
91  print "DEBUG: Starting scheduler with '$scheduler_cmd'\n";
92  exec($scheduler_cmd, $output, $return_var);
93  //print_r($output);
94  if ( $return_var != 0 ) {
95  print "Error: Could not start scheduler '$scheduler_path'\n";
96  print "$output\n";
97  exit(1);
98  }
99  sleep(10);
100  print "\nStarting functional test for fossjobs. \n";
101 
102  }
103 
104  /* initialization */
105  protected function setUp()
106  {
107 
108  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
109 
110  }
111 
116  {
117  global $fossology_testconfig;
118  global $scheduler_path;
119  global $cp2foss_path;
120  global $fossjobs_path;
121 
122  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
123 
124  $test_dbh = connect_to_DB($fossology_testconfig);
125 
126  $out = "";
128  $auth = "--username fossy --password fossy -c $fossology_testconfig";
129  $cp2foss_command = "$cp2foss_path -s $auth ./ -f fossjobs -d 'fossjobs testing'";
130  // print "cp2foss_command is:$cp2foss_command\n";
131  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$cp2foss_command'\n");
132  $last = exec("$cp2foss_command 2>&1", $out, $rtn);
133  //print_r($out);
134  $upload_id = 0;
136  if ($out && $out[5]) {
137  $upload_id = get_upload_id($out[5]);
138  } else {
139  $this->assertFalse(true);
140  }
141 
142  $agent_status = 0;
143  $agent_status = check_agent_status($test_dbh, "ununpack", $upload_id);
144  $this->assertEquals(1, $agent_status);
145 
147  $command = "$fossjobs_path $auth -U $upload_id -A agent_copyright,agent_mimetype,agent_nomos,agent_pkgagent -v";
148  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$command'\n");
149  $last = exec("$command 2>&1", $out, $rtn);
150  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " Waiting 300 seconds for the agents to complete\n");
151  sleep(300); //wait for the agents complete $agent_status = 0;
152  $agent_status = check_agent_status($test_dbh,"nomos", $upload_id);
153  $this->assertEquals(1, $agent_status);
154  $agent_status = 0;
155  $agent_status = check_agent_status($test_dbh,"copyright", $upload_id);
156  $this->assertEquals(1, $agent_status);
157  $agent_status = 0;
158  $agent_status = check_agent_status($test_dbh,"pkgagent", $upload_id);
159  $this->assertEquals(1, $agent_status);
160  $agent_status = 0;
161  $agent_status = check_agent_status($test_dbh,"mimetype", $upload_id);
162  $this->assertEquals(1, $agent_status);
163  $agent_status = 0;
164 
165  $out = "";
166 
168  $cp2foss_command = "$cp2foss_path -s $auth ./test_fossjobs.php -f fossjobs -d 'fossjobs testing copyright' -q agent_copyright";
169  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$cp2foss_command'\n");
170  $last = exec("$cp2foss_command 2>&1", $out, $rtn);
171  //print_r($out);
172  $upload_id = 0;
174  if ($out && $out[5]) {
175  $upload_id = get_upload_id($out[5]);
176  } else {
177  $this->assertFalse(true);
178  }
179 
180  sleep(5); //wait for the agents complete
181  $agent_status = 0;
182  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
183  $this->assertEquals(1, $agent_status);
184  $agent_status = 0;
185  $agent_status = check_agent_status($test_dbh,"copyright", $upload_id);
186  $this->assertEquals(1, $agent_status);
187 
189  $command = "$fossjobs_path $auth -U $upload_id -v -A agent_nomos";
190  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$command'\n");
191  $last = exec("$command 2>&1", $out, $rtn);
192  //print_r($out);
193  sleep(5); //wait for the agents complete
194 
195  $agent_status = 0;
196  $agent_status = check_agent_status($test_dbh,"nomos", $upload_id);
197  $this->assertEquals(1, $agent_status);
198 
199  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
200 
201  }
202 
207  {
208 
209  global $fossology_testconfig;
210  global $scheduler_path;
211  global $cp2foss_path;
212  global $fossjobs_path;
213 
214  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
215 
216  //$this->test_reschedule_agents(); // using the uloads in test case test_reschedule_agents()
218  $command = "$fossjobs_path -h -c $fossology_testconfig";
219  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$command'\n");
220  $last = exec("$command 2>&1", $out, $rtn);
221  $output_msg_count = count($out);
222  //print_r($out);
223  $this->assertEquals(18, $output_msg_count);
224  $auth = "--username fossy --password fossy -c $fossology_testconfig";
226  $out = "";
227  $pos = 0;
228  $command = "$fossjobs_path $auth -a ";
229  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$command'\n");
230  $last = exec("$command 2>&1", $out, $rtn);
231  //print_r($out);
232  $output_msg_count = count($out);
233  $this->assertEquals(10, $output_msg_count);
234 
236  $out = "";
237  $pos = 0;
238  $command = "$fossjobs_path $auth -u ";
239  fwrite(STDOUT, "DEBUG: " . __METHOD__ . " Line: " . __LINE__ . " executing '$command'\n");
240  $last = exec("$command 2>&1", $out, $rtn);
241  fwrite(STDOUT, "DEBUG: output was:\n");
242  //print_r($out);
243  $output_msg_count = count($out);
244  // TODO: / Note: This is *Highly* dependent on the execution of
245  // test_reschedule_agents() - i.e. these two test cases are
246  // tightly coupled, and they should _not_ be so.
247  // at the end of test_reschedule and this method, the number of
248  $this->assertEquals(3, $output_msg_count, $command); // have 2 = (3 -1_ uploads
249 
250  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
251  }
252 
256  protected function tearDown()
257  {
258  global $fossology_testconfig;
259 
260  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
261 
262  // TODO: Drop the test database
263 
264  //stop_scheduler();
265  //drop_db();
266  }
267 
268  // this method is run once for the entire test class, after all of the
269  // test methods are executed.
270  public static function tearDownAfterClass()
271  {
272 
273  global $fossology_testconfig;
274  global $scheduler_path;
275  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
276 
277  // stop the scheduler
278  print "Stopping the scheduler\n";
279  $scheduler_cmd = "$scheduler_path -k -c $fossology_testconfig";
280  print "DEBUG: command is $scheduler_cmd \n";
281  exec($scheduler_cmd, $output, $return_var);
282  if ( $return_var != 0 ) {
283  print "Error: Could not stop scheduler via '$scheduler_cmd'\n";
284  print "$output\n";
285  # exit(1);
286  }
287 
288  // time to drop the database
289  sleep(10);
290 
291  print "End of functional tests for cp2foss \n";
292  }
293 }
294 
295 
test_reschedule_agents()
schedule agents
test cli fossjobs
static setUpBeforeClass()
tearDown()
clean the env
test_list_agent_and_others()
list agents, list uploads, help msg