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