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