FOSSology  4.4.0
Open Source License Compliance by Open Source Software
schedulerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4  SPDX-FileCopyrightText: © 2017 TNG Technology Consulting GmbH
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
18 namespace Fossology\SpdxTwo\Test;
19 
23 use ZipArchive;
24 
25 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
26 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
27 
32 class SchedulerTest extends \PHPUnit\Framework\TestCase
33 {
37  private $userId = 2;
41  private $groupId = 2;
42 
46  private $testDb;
50  private $dbManager;
54  private $testInstaller;
58  private $runnerCli;
59 
63  protected function setUp() : void
64  {
65  $this->testDb = new TestPgDb("spdx2test");
66  $this->dbManager = $this->testDb->getDbManager();
67 
68  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
69  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
70 
71  $this->agentDir = dirname(__DIR__, 4) . '/build/src/spdx2';
72  }
73 
77  protected function tearDown() : void
78  {
79  $this->testDb->fullDestruct();
80  $this->testDb = null;
81  $this->dbManager = null;
82  }
83 
87  private function setUpRepo()
88  {
89  $sysConf = $this->testDb->getFossSysConf();
90  $this->testInstaller = new TestInstaller($sysConf);
91  $this->testInstaller->init();
92  $this->testInstaller->cpRepo();
93  $this->testInstaller->install($this->agentDir);
94  }
95 
99  private function rmRepo()
100  {
101  $this->testInstaller->uninstall($this->agentDir);
102  $this->testInstaller->rmRepo();
103  $this->testInstaller->clear();
104  }
105 
109  private function setUpTables()
110  {
111  $this->testDb->createPlainTables(array(),true);
112  $this->testDb->createInheritedTables();
113  $this->dbManager->queryOnce("CREATE TABLE copyright_ars () INHERITS (ars_master)");
114 
115  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq','upload_upload_pk_seq',
116  'nomos_ars_ars_pk_seq','license_file_fl_pk_seq','license_ref_rf_pk_seq',
117  'license_ref_bulk_lrb_pk_seq','clearing_decision_clearing_decision_pk_seq',
118  'clearing_event_clearing_event_pk_seq'));
119  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx',
120  'FileLicense_pkey','clearing_event_pkey'));
121  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk',
122  'clearing_event','clearing_decision','license_file','license_ref','highlight'));
123 
124  $this->testDb->insertData(array('mimetype_ars','pkgagent_ars','ununpack_ars','decider_ars'),true,__DIR__.'/fo_report.sql');
125  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
126  }
127 
131  private function getHeartCount($output)
132  {
133  $matches = array();
134  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
135  return intval($matches[1]);
136  }
137  return -1;
138  }
139 
146  {
147  $this->setUpTables();
148  $this->setUpRepo();
149  $this->runAndTestReportRDF();
150  }
151 
158  {
159  $this->setUpTables();
160  $this->setUpRepo();
161 
162  $uploadId = 1;
163  $this->dbManager->queryOnce("ALTER TABLE uploadtree_a RENAME TO uploadtree_$uploadId", __METHOD__.'.alterUploadtree');
164  $this->dbManager->getSingleRow("UPDATE upload SET uploadtree_tablename=$1 WHERE upload_pk=$2",
165  array("uploadtree_$uploadId",$uploadId),__METHOD__.'.alterUpload');
166 
167  $this->runAndTestReportRDF($uploadId);
168  }
169 
175  public function runJobFromJobque($uploadId, $jobId)
176  {
177  list($success,$output,$retCode) = $this->runnerCli->run($uploadId, $this->userId, $this->groupId, $jobId);
178 
179  assertThat('cannot run runner', $success, equalTo(true));
180  assertThat('report failed: "'.$output.'"', $retCode, equalTo(0));
181  assertThat($this->getHeartCount($output), greaterThan(0));
182  }
183 
190  public function getReportFilepathFromJob($uploadId, $jobId)
191  {
192  $row = $this->dbManager->getSingleRow("SELECT upload_fk,job_fk,filepath FROM reportgen WHERE job_fk = $1", array($jobId),
193  "reportFileName");
194  assertThat($row, hasKeyValuePair('upload_fk', $uploadId));
195  assertThat($row, hasKeyValuePair('job_fk', $jobId));
196  $filepath = $row['filepath'];
197  assertThat($filepath, endsWith('.rdf'));
198  $this->assertFileExists($filepath, "RDF report does not exists.");
199 
200  return $filepath;
201  }
202 
213  public function runAndTestReportRDF($uploadId=1)
214  {
215  $jobId=7;
216 
217  $this->runJobFromJobque($uploadId, $jobId);
218  $filepath = $this->getReportFilepathFromJob($uploadId, $jobId);
219 
220  $copyrightStatement = 'Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved';
221  assertThat(file_get_contents($filepath), stringContainsInOrder($copyrightStatement));
222 
223  $email = 'condor-admin@cs.wisc.edu';
224  assertThat(file_get_contents($filepath), not(stringContainsInOrder($email)));
225 
226  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
227 
228  $this->verifyRdf($filepath);
229  $this->rmRepo();
230  }
231 
236  protected function verifyRdf($filepath)
237  {
238  $toolJarFile = $this->pullSpdxTools();
239 
240  $verification = exec("java -jar $toolJarFile Verify $filepath");
241  assertThat($verification,equalTo('This SPDX Document is valid.'));
242  unlink($filepath);
243  }
244 
253  protected function pullSpdxTools()
254  {
255  $this-> verifyJavaIsInstalled();
256 
257  $version='1.1.4';
258  $tag='v'.$version;
259 
260  $jarFileBasename = 'tools-java-'.$version.'-jar-with-dependencies.jar';
261  $jarFile = __DIR__.'/'.$jarFileBasename;
262  if (!file_exists($jarFile)) {
263  $zipFile = __DIR__ . "/spdx-tools-java-$version.zip";
264  file_put_contents($zipFile, fopen("https://github.com/spdx/tools-java/releases/download/$tag/tools-java-$version.zip", 'r'));
265  $zip = new ZipArchive;
266  if ($zip->open($zipFile) === TRUE) {
267  $zip->extractTo(__DIR__, [$jarFileBasename]);
268  $zip->close();
269  }
270  unlink($zipFile);
271  }
272  $this->assertFileExists($jarFile, 'could not download SPDXTools');
273  return $jarFile;
274  }
275 
279  protected function verifyJavaIsInstalled()
280  {
281  $lines = '';
282  $returnVar = 0;
283  exec('which java', $lines, $returnVar);
284  $this->assertEquals(0,$returnVar,'java required for this test');
285  }
286 }
Tests for SPDX2 agent and scheduler interaction.
testSpdxForSpecialUploadtreeTable()
Test SPDX2 agent for RDF.
verifyJavaIsInstalled()
Verify if java is intalled on the system.
setUpTables()
Setup tables required for test.
verifyRdf($filepath)
Use SPDX toolkit to verify RDF file format.
pullSpdxTools()
Pull SPDX toolkit from github if not found.
runAndTestReportRDF($uploadId=1)
Create RDF report and check it.
runJobFromJobque($uploadId, $jobId)
Run jobs from queue.
testSpdxForNormalUploadtreeTable()
Test SPDX2 agent for RDF.
getHeartCount($output)
Get the heart count from agent.
getReportFilepathFromJob($uploadId, $jobId)
Get the file path for report from DB.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace to hold test cases for SPDX2 agent.