FOSSology  4.7.1
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\Spdx\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 {
34  public $agentDir;
35  public $assertCountBefore;
36 
40  private $userId = 2;
44  private $groupId = 2;
45 
49  private $testDb;
53  private $dbManager;
57  private $testInstaller;
61  private $runnerCli;
62 
66  protected function setUp() : void
67  {
68  $this->testDb = new TestPgDb("spdxtest");
69  $this->dbManager = $this->testDb->getDbManager();
70 
71  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
72  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
73 
74  $this->agentDir = dirname(__DIR__, 4) . '/build/src/spdx';
75  }
76 
80  protected function tearDown() : void
81  {
82  $this->testDb->fullDestruct();
83  $this->testDb = null;
84  $this->dbManager = null;
85  }
86 
90  private function setUpRepo()
91  {
92  $sysConf = $this->testDb->getFossSysConf();
93  $this->testInstaller = new TestInstaller($sysConf);
94  $this->testInstaller->init();
95  $this->testInstaller->cpRepo();
96  $this->testInstaller->install($this->agentDir);
97  }
98 
102  private function rmRepo()
103  {
104  $this->testInstaller->uninstall($this->agentDir);
105  $this->testInstaller->rmRepo();
106  $this->testInstaller->clear();
107  }
108 
112  private function setUpTables()
113  {
114  $this->testDb->createPlainTables(array(),true);
115  $this->testDb->createInheritedTables();
116  $this->dbManager->queryOnce("CREATE TABLE copyright_ars () INHERITS (ars_master)");
117 
118  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq','upload_upload_pk_seq',
119  'nomos_ars_ars_pk_seq','license_file_fl_pk_seq','license_ref_rf_pk_seq',
120  'license_ref_bulk_lrb_pk_seq','clearing_decision_clearing_decision_pk_seq',
121  'clearing_event_clearing_event_pk_seq'));
122  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx',
123  'FileLicense_pkey','clearing_event_pkey'));
124  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk',
125  'clearing_event','clearing_decision','license_file','license_ref','highlight'));
126 
127  $this->testDb->insertData(array('mimetype_ars','pkgagent_ars','ununpack_ars','decider_ars'),true,__DIR__.'/fo_report.sql');
128  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
129  }
130 
134  private function getHeartCount($output)
135  {
136  $matches = array();
137  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
138  return intval($matches[1]);
139  }
140  return -1;
141  }
142 
149  {
150  $this->setUpTables();
151  $this->setUpRepo();
152  $this->runAndTestReportRDF();
153  }
154 
161  {
162  $this->setUpTables();
163  $this->setUpRepo();
164 
165  $uploadId = 1;
166  $this->dbManager->queryOnce("ALTER TABLE uploadtree_a RENAME TO uploadtree_$uploadId", __METHOD__.'.alterUploadtree');
167  $this->dbManager->getSingleRow("UPDATE upload SET uploadtree_tablename=$1 WHERE upload_pk=$2",
168  array("uploadtree_$uploadId",$uploadId),__METHOD__.'.alterUpload');
169 
170  $this->runAndTestReportRDF($uploadId);
171  }
172 
178  public function runJobFromJobque($uploadId, $jobId)
179  {
180  list($success,$output,$retCode) = $this->runnerCli->run($uploadId, $this->userId, $this->groupId, $jobId);
181 
182  assertThat('cannot run runner', $success, equalTo(true));
183  assertThat('report failed: "'.$output.'"', $retCode, equalTo(0));
184  assertThat($this->getHeartCount($output), greaterThan(0));
185  }
186 
193  public function getReportFilepathFromJob($uploadId, $jobId)
194  {
195  $row = $this->dbManager->getSingleRow("SELECT upload_fk,job_fk,filepath FROM reportgen WHERE job_fk = $1", array($jobId),
196  "reportFileName");
197  assertThat($row, hasKeyValuePair('upload_fk', $uploadId));
198  assertThat($row, hasKeyValuePair('job_fk', $jobId));
199  $filepath = $row['filepath'];
200  assertThat($filepath, endsWith('.rdf'));
201  $this->assertFileExists($filepath, "RDF report does not exists.");
202 
203  return $filepath;
204  }
205 
216  public function runAndTestReportRDF($uploadId=1)
217  {
218  $jobId=7;
219 
220  $this->runJobFromJobque($uploadId, $jobId);
221  $filepath = $this->getReportFilepathFromJob($uploadId, $jobId);
222 
223  $copyrightStatement = 'Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved';
224  assertThat(file_get_contents($filepath), stringContainsInOrder($copyrightStatement));
225 
226  $email = 'condor-admin@cs.wisc.edu';
227  assertThat(file_get_contents($filepath), not(stringContainsInOrder($email)));
228 
229  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
230 
231  $this->verifyRdf($filepath);
232  $this->rmRepo();
233  }
234 
239  protected function verifyRdf($filepath)
240  {
241  $toolJarFile = $this->pullSpdxTools();
242 
243  $verification = exec("java -jar $toolJarFile Verify $filepath");
244  assertThat($verification,equalTo('This SPDX Document is valid.'));
245  unlink($filepath);
246  }
247 
256  protected function pullSpdxTools()
257  {
258  $this-> verifyJavaIsInstalled();
259 
260  $version='1.1.4';
261  $tag='v'.$version;
262 
263  $jarFileBasename = 'tools-java-'.$version.'-jar-with-dependencies.jar';
264  $jarFile = __DIR__.'/'.$jarFileBasename;
265  if (!file_exists($jarFile)) {
266  $zipFile = __DIR__ . "/spdx-tools-java-$version.zip";
267  file_put_contents($zipFile, fopen("https://github.com/spdx/tools-java/releases/download/$tag/tools-java-$version.zip", 'r'));
268  $zip = new ZipArchive;
269  if ($zip->open($zipFile) === TRUE) {
270  $zip->extractTo(__DIR__, [$jarFileBasename]);
271  $zip->close();
272  }
273  unlink($zipFile);
274  }
275  $this->assertFileExists($jarFile, 'could not download SPDXTools');
276  return $jarFile;
277  }
278 
282  protected function verifyJavaIsInstalled()
283  {
284  $lines = '';
285  $returnVar = 0;
286  exec('which java', $lines, $returnVar);
287  $this->assertEquals(0,$returnVar,'java required for this test');
288  }
289 }
Tests for Spdx agent and scheduler interaction.
testSpdxForNormalUploadtreeTable()
Test spdx agent for RDF.
setUpTables()
Setup tables required for test.
testSpdxForSpecialUploadtreeTable()
Test spdx agent for RDF.
pullSpdxTools()
Pull SPDX toolkit from github if not found.
getReportFilepathFromJob($uploadId, $jobId)
Get the file path for report from DB.
getHeartCount($output)
Get the heart count from agent.
verifyRdf($filepath)
Use SPDX toolkit to verify RDF file format.
verifyJavaIsInstalled()
Verify if java is intalled on the system.
runJobFromJobque($uploadId, $jobId)
Run jobs from queue.
runAndTestReportRDF($uploadId=1)
Create RDF report and check it.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace to hold test cases for spdx agent.