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