FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
schedulerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 Copyright (C) 2017 TNG Technology Consulting GmbH
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
29 namespace Fossology\SpdxTwo\Test;
30 
34 
35 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
36 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
37 
42 class SchedulerTest extends \PHPUnit\Framework\TestCase
43 {
47  private $userId = 2;
51  private $groupId = 2;
52 
56  private $testDb;
60  private $dbManager;
64  private $testInstaller;
68  private $runnerCli;
69 
73  protected function setUp()
74  {
75  $this->testDb = new TestPgDb("spdx2test");
76  $this->dbManager = $this->testDb->getDbManager();
77 
78  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
79  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
80 
81  $this->agentDir = dirname(dirname(__DIR__));
82  }
83 
87  protected function tearDown()
88  {
89  $this->testDb->fullDestruct();
90  $this->testDb = null;
91  $this->dbManager = null;
92  }
93 
97  private function setUpRepo()
98  {
99  $sysConf = $this->testDb->getFossSysConf();
100  $this->testInstaller = new TestInstaller($sysConf);
101  $this->testInstaller->init();
102  $this->testInstaller->cpRepo();
103  $this->testInstaller->install($this->agentDir);
104  }
105 
109  private function rmRepo()
110  {
111  $this->testInstaller->uninstall($this->agentDir);
112  $this->testInstaller->rmRepo();
113  $this->testInstaller->clear();
114  }
115 
119  private function setUpTables()
120  {
121  $this->testDb->createPlainTables(array(),true);
122  $this->testDb->createInheritedTables();
123  $this->dbManager->queryOnce("CREATE TABLE copyright_ars () INHERITS (ars_master)");
124 
125  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq','upload_upload_pk_seq',
126  'nomos_ars_ars_pk_seq','license_file_fl_pk_seq','license_ref_rf_pk_seq',
127  'license_ref_bulk_lrb_pk_seq','clearing_decision_clearing_decision_pk_seq',
128  'clearing_event_clearing_event_pk_seq'));
129  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx',
130  'FileLicense_pkey','clearing_event_pkey'));
131  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk',
132  'clearing_event','clearing_decision','license_file','highlight'));
133 
134  $this->testDb->insertData(array('mimetype_ars','pkgagent_ars','ununpack_ars','decider_ars'),true,__DIR__.'/fo_report.sql');
135  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
136  }
137 
141  private function getHeartCount($output)
142  {
143  $matches = array();
144  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
145  return intval($matches[1]);
146  }
147  return -1;
148  }
149 
156  {
157  $this->setUpTables();
158  $this->setUpRepo();
159  $this->runAndTestReportRDF();
160  }
161 
168  {
169  $this->setUpTables();
170  $this->setUpRepo();
171 
172  $uploadId = 1;
173  $this->dbManager->queryOnce("ALTER TABLE uploadtree_a RENAME TO uploadtree_$uploadId", __METHOD__.'.alterUploadtree');
174  $this->dbManager->getSingleRow("UPDATE upload SET uploadtree_tablename=$1 WHERE upload_pk=$2",
175  array("uploadtree_$uploadId",$uploadId),__METHOD__.'.alterUpload');
176 
177  $this->runAndTestReportRDF($uploadId);
178  }
179 
185  public function runJobFromJobque($uploadId, $jobId)
186  {
187  list($success,$output,$retCode) = $this->runnerCli->run($uploadId, $this->userId, $this->groupId, $jobId);
188 
189  assertThat('cannot run runner', $success, equalTo(true));
190  assertThat('report failed: "'.$output.'"', $retCode, equalTo(0));
191  assertThat($this->getHeartCount($output), greaterThan(0));
192  }
193 
200  public function getReportFilepathFromJob($uploadId, $jobId)
201  {
202  $row = $this->dbManager->getSingleRow("SELECT upload_fk,job_fk,filepath FROM reportgen WHERE job_fk = $1", array($jobId),
203  "reportFileName");
204  assertThat($row, hasKeyValuePair('upload_fk', $uploadId));
205  assertThat($row, hasKeyValuePair('job_fk', $jobId));
206  $filepath = $row['filepath'];
207  assertThat($filepath, endsWith('.rdf'));
208  assertThat(file_exists($filepath),equalTo(true));
209 
210  return $filepath;
211  }
212 
223  public function runAndTestReportRDF($uploadId=1)
224  {
225  $jobId=7;
226 
227  $this->runJobFromJobque($uploadId, $jobId);
228  $filepath = $this->getReportFilepathFromJob($uploadId, $jobId);
229 
230  $copyrightStatement = 'Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved';
231  assertThat(file_get_contents($filepath), stringContainsInOrder($copyrightStatement));
232 
233  $email = 'condor-admin@cs.wisc.edu';
234  assertThat(file_get_contents($filepath), not(stringContainsInOrder($email)));
235 
236  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
237 
238  $this->verifyRdf($filepath);
239  $this->rmRepo();
240  }
241 
246  protected function verifyRdf($filepath)
247  {
248  $toolJarFile = $this->pullSpdxTools();
249 
250  $verification = exec("java -jar $toolJarFile Verify $filepath");
251  assertThat($verification,equalTo('This SPDX Document is valid.'));
252  unlink($filepath);
253  }
254 
263  protected function pullSpdxTools()
264  {
265  $this-> verifyJavaIsInstalled();
266 
267  $version='2.1.0';
268  $tag='v'.$version;
269 
270  $jarFileBasename = 'spdx-tools-'.$version.'-jar-with-dependencies.jar';
271  $jarFile = __DIR__.'/'.$jarFileBasename;
272  if (!file_exists($jarFile)) {
273  $zipFileBasename='SPDXTools-'.$tag.'.zip';
274  $zipFile=__DIR__.'/'.$zipFileBasename;
275  if (!file_exists($zipFile)) {
276  file_put_contents($zipFile, fopen('https://github.com/spdx/tools/releases/download/'.$tag.'/'.$zipFileBasename, 'r'));
277 
278  }
279  $this->assertFileExists($zipFile, 'could not download SPDXTools');
280 
281  system('unzip -n -d '.__DIR__.' '.$zipFile);
282  rename (__DIR__.'/SPDXTools-'.$tag.'/'.$jarFileBasename, $jarFile);
283  }
284  $this->assertFileExists($jarFile, 'could not extract SPDXTools');
285  return $jarFile;
286  }
287 
291  protected function verifyJavaIsInstalled()
292  {
293  $lines = '';
294  $returnVar = 0;
295  exec('which java', $lines, $returnVar);
296  $this->assertEquals(0,$returnVar,'java required for this test');
297  }
298 }
testSpdxForSpecialUploadtreeTable()
Test SPDX2 agent for RDF.
runJobFromJobque($uploadId, $jobId)
Run jobs from queue.
getReportFilepathFromJob($uploadId, $jobId)
Get the file path for report from DB.
getHeartCount($output)
Get the heart count from agent.
pullSpdxTools()
Pull SPDX toolkit from github if not found.
Namespace to hold test cases for SPDX2 agent.
Tests for SPDX2 agent and scheduler interaction.
setUpTables()
Setup tables required for test.
testSpdxForNormalUploadtreeTable()
Test SPDX2 agent for RDF.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
verifyJavaIsInstalled()
Verify if java is intalled on the system.
runAndTestReportRDF($uploadId=1)
Create RDF report and check it.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
verifyRdf($filepath)
Use SPDX toolkit to verify RDF file format.