FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_fo_nomos_license_list.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 
8 require_once("./test_common.php");
9 
17 class test_fo_nomos_license_list extends \PHPUnit\Framework\TestCase
18 {
19 
20  public $SYSCONF_DIR = "/usr/local/etc/fossology/";
21  public $DB_NAME;
22  public $PG_CONN;
23  public $DB_COMMAND;
24 
25  // fossology_testconfig is the temporary system configuration directory
26  // created by the src/testing/db/create_test_database.php script.
27  // It is initialized via the Makefile and passed in via the
28  // FOSSOLOGY_TESTCONFIG environment variable.
29  public $fossology_testconfig;
30 
31  // scheduler_path is the absolute path to the scheduler binary
32  public $scheduler_path;
33 
34  // cp2foss_path is the absolute path to the cp2foss binary
35  public $cp2foss_path;
36 
37  // fo_nomos_license_list_path is the absolute path to the fo_nomos_license_list_path binary
38  public $fo_nomos_license_list_path;
39 
40  /* initialization */
41 
42  // this method is run once for the entire test class, before any of the
43  // test methods are executed.
44  public static function setUpBeforeClass() : void
45  {
46 
47  global $fossology_testconfig;
48  global $scheduler_path;
49  global $cp2foss_path;
50  global $fo_nomos_license_list_path;
51 
52  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
53 
59  $fossology_testconfig = getenv('FOSSOLOGY_TESTCONFIG');
60  fwrite(STDOUT, __METHOD__ . " got fossology_testconfig = '$fossology_testconfig'\n");
61 
62  /* locate cp2foss binary */
63  // first get the absolute path to the current fossology src/ directory
64  $fo_base_dir = realpath(__DIR__ . '/../..');
65  $cp2foss_path = $fo_base_dir . "/cli/cp2foss";
66  if (!is_executable($cp2foss_path)) {
67  print "Error: cp2foss path '" . $cp2foss_path . "' is not executable!\n";
68  exit(1);
69  }
70  $cp2foss_path .= " -s ";
71 
72  /* locate fo_nomos_license_list binary */
73  // first get the absolute path to the current fossology src/ directory
74  $fo_base_dir = realpath(__DIR__ . '/../..');
75  $fo_nomos_license_list_path = $fo_base_dir . "/cli/fo_nomos_license_list";
76  if (!is_executable($fo_nomos_license_list_path)) {
77  print "Error: fo_nomos_license_list path '" . $fo_nomos_license_list_path . "' is not executable!\n";
78  exit(1);
79  }
80 
81  /* locate the scheduler binary */
82  $scheduler_path = $fossology_testconfig . "/mods-enabled/scheduler/agent/fo_scheduler";
83  if (!is_executable($scheduler_path)) {
84  print "Error: Scheduler path '$scheduler_path' is not executable!\n";
85  exit(1);
86  }
87 
88  /* invoke the scheduler */
89  $scheduler_cmd = "$scheduler_path --daemon --reset --verbose=952 -c $fossology_testconfig";
90  print "DEBUG: Starting scheduler with '$scheduler_cmd'\n";
91  exec($scheduler_cmd, $output, $return_var);
92  //print_r($output);
93  if ( $return_var != 0 ) {
94  print "Error: Could not start scheduler '$scheduler_path'\n";
95  print "$output\n";
96  exit(1);
97  }
98  print "\nStarting functional test for fo_nomos_license_list. \n";
99 
100  }
101 
102 
103  // this method is run once before each test method defined for this test class.
104  protected function setUp() : void
105  {
106 
107  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
108 
109  //$SYSCONF_DIR = "/usr/local/etc/fossology/";
110  //$DB_NAME = "fossology";
111  //$DB_COMMAND = "../../testing/db/createTestDB.php";
112 
113  // these calls are deprecated with the new create_test_database call
114  //create_db();
115  //add_user();
116  //preparations();
117  //scheduler_operation();
118  }
119 
124  {
125  global $fossology_testconfig;
126  global $fo_nomos_license_list_path;
127 
128  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
129  $upload = $this->upload_from_url();
130  $upload_id = $upload[0];
131 
132  $auth = "--username fossy --password fossy -c $fossology_testconfig";
133  $uploadtree_id = $upload[1]; // uploadtree_id is the 1st uploadtree_id for this upload
134 
136  $command = "$fo_nomos_license_list_path $auth -u $upload_id -t $uploadtree_id --container 1";
137  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
138  $last = exec("$command 2>&1", $out, $rtn);
139  $output_msg_count = count($out);
140 
141  sort($out, SORT_STRING);
142  print_r($out);
144  $this->assertEquals(9, $output_msg_count, " upload[1] is:$upload[1], Test that the number of output lines from '$command' is $output_msg_count, have 9 licenses");
146  $this->assertEquals("test package/usr/include/libfossdb.h: LGPL-2.1", $out[0]);
147  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
148 
150  $out = "";
151  $command = "$fo_nomos_license_list_path $auth -u $upload_id -t $uploadtree_id";
152  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
153  $last = exec("$command 2>&1", $out, $rtn);
154  $output_msg_count = count($out);
155 
156  sort($out, SORT_STRING);
158  $this->assertEquals(4, $output_msg_count, "Test that the number of output lines from '$command' is $output_msg_count, have 9 licenses");
160  $this->assertEquals("test package/usr/include/libfossdb.h: LGPL-2.1", $out[0]);
161  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
162  }
163 
167  function upload_from_url()
168  {
169  //global $SYSCONF_DIR;
170  global $fossology_testconfig;
171  global $cp2foss_path;
172 
173  $test_dbh = connect_to_DB($fossology_testconfig);
174 
175  $auth = "--username fossy --password fossy -c $fossology_testconfig";
177  $out = "";
178  $pos = 0;
179  $command = "$cp2foss_path $auth http://www.fossology.org/testdata/rpms/fedora/10/i386/fossology-devel-1.1.0-1.fc10.i386.rpm -d 'fossology des' -f 'fossology path' -n 'test package' -q 'all'";
180  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
181  $last = exec("$command 2>&1", $out, $rtn);
182 
183  $upload_id = 0;
185  if ($out && $out[5]) {
186  $upload_id = get_upload_id($out[5]);
187  } else {
188  $this->assertFalse(true);
189  }
190  $agent_status = 0;
191  $agent_status = check_agent_status($test_dbh,"ununpack", $upload_id);
192  $this->assertEquals(1, $agent_status);
193 
194  $uploadtree_id = get_uploadtree_id($test_dbh, $upload_id); // get uploadtree id
195 
196  pg_close($test_dbh);
197 
198  fwrite(STDOUT,"DEBUG: upload_id is:$upload_id, uploadtree_id, is:$uploadtree_id\n");
199  return array($upload_id, $uploadtree_id);
200  }
201 
205  function test_others()
206  {
207  global $fossology_testconfig;
208  global $fo_nomos_license_list_path;
209  //global $SYSCONF_DIR;
210 
211  fwrite(STDOUT, " ----> Running " . __METHOD__ . "\n");
212  $auth = "--user fossy --password fossy -c $fossology_testconfig";
213  //$auth = "--user fossy --password fossy -c $SYSCONF_DIR";
215  $command = "$fo_nomos_license_list_path $auth -h";
216  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
217  $last = exec("$command 2>&1", $out, $rtn);
218  $output_msg_count = count($out);
219  $this->assertEquals(14, $output_msg_count, "Test that the number of output lines from '$command' is $output_msg_count");
220  // print_r($out);
221 
223  $out = "";
224  $command = "$fo_nomos_license_list_path -h";
225  fwrite(STDOUT, "DEBUG: Executing '$command'\n");
226  $last = exec("$command 2>&1", $out, $rtn);
227  $output_msg_count = count($out);
228  $this->assertEquals(14, $output_msg_count, "Test that the number of output lines from '$command' is $output_msg_count");
229  // print_r($out);
230  fwrite(STDOUT,"DEBUG: Done running " . __METHOD__ . "\n");
231  }
232 
236  // this method is run once after each test method defined for this test class.
237  protected function tearDown() : void
238  {
239 
240  global $fossology_testconfig;
241 
242  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
243 
244  // TODO: Drop the test database
245 
246  //stop_scheduler();
247  //drop_db();
248  }
249 
250  // this method is run once for the entire test class, after all of the
251  // test methods are executed.
252  public static function tearDownAfterClass() : void
253  {
254 
255  global $fossology_testconfig;
256  global $scheduler_path;
257  fwrite(STDOUT, "--> Running " . __METHOD__ . " method.\n");
258 
259  // stop the scheduler
260  print "Stopping the scheduler\n";
261  $scheduler_cmd = "$scheduler_path -k -c $fossology_testconfig";
262  print "DEBUG: command is $scheduler_cmd \n";
263  exec($scheduler_cmd, $output, $return_var);
264  if ( $return_var != 0 ) {
265  print "Error: Could not stop scheduler via '$scheduler_cmd'\n";
266  print "$output\n";
267  # exit(1);
268  }
269 
270  // time to drop the database
271  sleep(10);
272  print "End of functional tests for fo_nomos_license_list\n";
273 
274  }
275 }
276 
277 
test cli fo_nomos_license_list
upload_from_url()
populate test data via upload from url
test_get_nomos_list()
first populate test data via upload from url, then get nomos license list