FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
schedulerTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
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 
26 use Monolog\Logger;
27 
28 if (!function_exists('Traceback_uri'))
29 {
30  function Traceback_uri(){
31  return 'Traceback_uri_if_desired';
32  }
33 }
34 
35 class NinkaScheduledTest extends \PHPUnit\Framework\TestCase
36 {
38  private $testDb;
40  private $dbManager;
42  private $licenseDao;
44  private $uploadDao;
46  private $uploadPermDao;
47 
48  protected function setUp()
49  {
50  $this->testDb = new TestPgDb("ninkaSched".time());
51  $this->dbManager = $this->testDb->getDbManager();
52 
53  $this->licenseDao = new LicenseDao($this->dbManager);
54  $logger = new Logger("NinkaSchedulerTest");
55  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
56  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
57  }
58 
59  protected function tearDown()
60  {
61  $this->testDb->fullDestruct();
62  $this->testDb = null;
63  $this->dbManager = null;
64  $this->licenseDao = null;
65  }
66 
67  private function runNinka($uploadId)
68  {
69  $sysConf = $this->testDb->getFossSysConf();
70 
71  $agentName = "ninka";
72 
73  $agentDir = dirname(dirname(__DIR__));
74  $execDir = "$agentDir/agent";
75  system("install -D $agentDir/VERSION $sysConf/mods-enabled/$agentName/VERSION");
76 
77  $pipeFd = popen("echo $uploadId | $execDir/$agentName -c $sysConf --scheduler_start", "r");
78  $this->assertTrue($pipeFd !== false, 'running ninka failed');
79 
80  $output = "";
81  while (($buffer = fgets($pipeFd, 4096)) !== false) {
82  $output .= $buffer;
83  }
84  $retCode = pclose($pipeFd);
85 
86  unlink("$sysConf/mods-enabled/$agentName/VERSION");
87  rmdir("$sysConf/mods-enabled/$agentName");
88  rmdir("$sysConf/mods-enabled");
89 
90  return array($output,$retCode);
91  }
92 
93  private function setUpRepo()
94  {
95  $sysConf = $this->testDb->getFossSysConf();
96 
97  $confFile = $sysConf."/fossology.conf";
98  system("touch ".$confFile);
99  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
100  file_put_contents($confFile, $config);
101 
102  $testRepoDir = dirname(dirname(dirname(__DIR__)))."/lib/php/Test/";
103  system("cp -a $testRepoDir/repo $sysConf/");
104  }
105 
106  private function rmRepo()
107  {
108  $sysConf = $this->testDb->getFossSysConf();
109  system("rm $sysConf/repo -rf");
110 
111  unlink($sysConf."/fossology.conf");
112  }
113 
114  private function setUpTables()
115  {
116  $this->testDb->createPlainTables(array('upload','uploadtree','license_ref','license_file','highlight','agent','pfile','ars_master'),false);
117  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq','upload_upload_pk_seq','nomos_ars_ars_pk_seq','license_file_fl_pk_seq','license_ref_rf_pk_seq'),false);
118  $this->testDb->createViews(array('license_file_ref'),false);
119  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','FileLicense_pkey','rf_pkpk'),false);
120  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_file','highlight','license_ref'),false);
121  $this->testDb->createInheritedTables();
122  $this->testDb->insertData(array('pfile','upload','uploadtree_a'), false);
123  $this->testDb->insertData_license_ref();
124  $this->testDb->resetSequenceAsMaxOf('license_ref_rf_pk_seq', 'license_ref', 'rf_pk');
125  }
126 
128  public function testRun()
129  {
130  $this->setUpTables();
131  $this->setUpRepo();
132 
133  list($output,$retCode) = $this->runNinka($uploadId=1);
134 
135  $this->rmRepo();
136 
137  $this->assertEquals($retCode, 0, 'ninka failed: '.$output);
138 
139  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
140  $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
141 
142  $this->assertEquals($expected=6, count($matches));
143 
144  foreach($matches as $licenseMatch) {
146  $matchedLicense = $licenseMatch->getLicenseRef();
147 
148  switch ($licenseMatch->getFileId()) {
149  case 7:
150  case 4:
151  $expectedLicense = "GPL-3.0+";
152  break;
153  case 3:
154  $expectedLicense = "UnclassifiedLicense";
155  break;
156  default:
157  $expectedLicense = "No_license_found";
158  break;
159  }
160 
161  $this->assertEquals($expectedLicense, $matchedLicense->getShortName(), "unexpected license for fileId ".$licenseMatch->getFileId());
162 
164  $agentRef = $licenseMatch->getAgentRef();
165  $this->assertEquals($agentRef->getAgentName(), "ninka");
166  }
167  }
168 
169 }
Traceback_uri()
Get the URI without query to this location.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28