FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ft_cliPkgagentTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
20 
25 class ft_cliPkgagentTest extends \PHPUnit\Framework\TestCase {
26 
27  public $agentDir;
28  public $pkgagent;
29  public $cwd;
30  protected $testfile = __DIR__.'/../testdata/fossology-1.2.0-1.el5.i386.rpm';
31 
33  private $testDb;
35  private $testInstaller;
40  function setUp() : void {
41 /*
42  $AGENTDIR = NULL;
43  // determine where the agents are installed
44  $upStream = '/usr/local/share/fossology/php/pathinclude.php';
45  $pkg = '/usr/share/fossology/php/pathinclude.php';
46 
47  if (file_exists($upStream)) {
48  require $upStream;
49  //print "agentdir is:$AGENTDIR\n";
50  $this->agentDir = $AGENTDIR;
51  $this->pkgagent = $this->agentDir . '/pkgagent';
52  } else
53  if (file_exists($pkg)) {
54  require $pkg;
55  //print "agentdir is:$AGENTDIR\n";
56  $this->agentDir = $AGENTDIR;
57  $this->pkgagent = $this->agentDir . '/pkgagent';
58  } else {
59  $this->assertFileExists($upStream, $message = 'FATAL: cannot find pathinclude.php file, stopping test\n');
60  }
61 */
62  //print "agent:$this->agentDir\npkgagent:$this->pkgagent\n";
63  global $cwd;
64  $cwd = dirname(__DIR__, 4).'/build/src/pkgagent/agent_tests';
65 
66  $this->agentDir = dirname(__DIR__, 4).'/build/src/pkgagent/agent';
67  $this->testDb = new TestPgDb("fosspkgagenttest");
68  $this->dbManager = $this->testDb->getDbManager();
69  $tables = array('agent');
70  $this->testDb->createPlainTables($tables);
71  $this->testDb->createSequences(['agent_agent_pk_seq']);
72  $this->testDb->createConstraints(['agent_pkey']);
73  $this->testDb->alterTables($tables);
74  $db_conf = $this->testDb->getFossSysConf();
75  $this->testInstaller = new TestInstaller($db_conf);
76  $this->testInstaller->init();
77  $this->testInstaller->cpRepo();
78  $this->testInstaller->install($cwd.'/..');
79  $this->pkgagent = $this->agentDir .'/pkgagent -c ' . $db_conf;
80  return;
81  } // setUP
82 
89  function testHelp() {
90  // pkgagent -h
91  $rtn = NULL;
92  $last = exec("$this->pkgagent -h 2>&1", $usageOut, $rtn);
93  //print "testHelp: last is:$last\nusageout is:\n";
94  //print_r($usageOut) . "\n";
95  // Check a couple of options for sanity
96  $usage = "Usage: $this->agentDir/pkgagent [options] [file [file [...]]";
97  $dashI = '-i :: initialize the database, then exit.';
98  $this->assertEquals($usage, $usageOut[0]);
99  $this->assertEquals($dashI, trim($usageOut[1]));
100  return;
101  }
102 
109  function testI() {
110  // pkgagent -i
111  $rtn = NULL;
112  $last = exec("$this->pkgagent -i 2>&1", $got, $rtn);
113 
114  if($rtn != 0){
115  $this->fail("pkgagent FAILED!, return value is:$rtn\n");
116  }else{
117  $this->assertTrue(true);
118  }
119  if(!empty($got)) {
120  $this->fail("pkgagent FAILED! output in -i test\n");
121  print_r($got) . "\n";
122  }
123  return;
124  }
125 
132  function testOneRPM()
133  {
134  // pkgagent rpmfile
135 
136  $expected = array('Name:fossology',
137  'Arch:i386',
138  'License:GPLv2',
139  'Summary:FOSSology is a licenses exploration tool',
140  'OK'
141  );
142  $rtn = NULL;
143  $last = exec("$this->pkgagent -C $this->testfile 2>&1", $got, $rtn);
144  //print "testOneRpm: last is:$last\ngot is:\n";
145  //print_r($got) . "\n";
146  //$this->assertEquals($expected[0],$got[0]);
147  if(empty($got)){
148  $this->fail("pkgagent FAILED!, no output for test, stopping test");
149  exit(1);
150  }
151  $size = count($got);
152  foreach($expected as $match) {
153  if(FALSE === in_array($match, $got)){
154  $this->fail("pkgagent FAILED! did not fine $match in output\n");
155  }
156  }
157  $this->assertEquals('OK',$got[$size-1]);
158  return;
159  }
160 
167  function testOneRPMV()
168  {
169  // pkgagent -v rpmfile
170  $rtn = NULL;
171  $last = exec("$this->pkgagent -C -vv $this->testfile 2>&1", $got, $rtn);
172  //print "testOneRpm: last is:$last\ngot is:\n";
173  //print_r($got) . "\n";
174  // check the output
175  if(empty($got)){
176  $this->fail("pkgagent FAILED!, no output for -vv test, stopping test");
177  exit(1);
178  }
179  // compare output to the standard
180  /*look in the output for items that should be in the header
181  * e.g.
182  * Name:fossology
183  * Arch:i386
184  * License:GPLv2
185  * Summary:FOSSology is a licenses exploration tool
186  * Size:44
187  * Name:fossology-1.2.0-1.el5.src.rpm
188  * OK
189  */
190  $expected = array('Name:fossology',
191  'Arch:i386',
192  'License:GPLv2',
193  'Summary:FOSSology is a licenses exploration tool',
194  'Size:44',
195  'Name:fossology-1.2.0-1.el5.src.rpm',
196  'OK'
197  );
198  $size = count($got);
199  foreach($expected as $match) {
200  if(FALSE === in_array($match, $got)){
201  $this->fail("pkgagent FAILED! did not find $match in output\n");
202  }
203  }
204  $this->assertEquals('OK',$got[$size-1]);
205  return;
206  }
207 
208  protected function tearDown() : void {
209  global $cwd;
210  if (!is_callable('pg_connect')) {
211  return;
212  }
213  $this->testDb->fullDestruct();
214  $this->testDb = null;
215  $this->testInstaller->uninstall($cwd.'/..');
216  }
217 }
Test cli parameter i and v and rpm file and no parameters.
testHelp()
Test help message.
testOneRPM()
Test CLI with single RPM.
testI()
Test DB init flag.
setUp()
Set up test environment.
testOneRPMV()
Test CLI in verbose with one RPM.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16