FOSSology  4.4.0
Open Source License Compliance by Open Source Software
schedulerTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\CliXml\Test;
9 
13 
14 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
15 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
16 
17 class SchedulerTest extends \PHPUnit\Framework\TestCase
18 {
20  private $userId = 2;
22  private $groupId = 2;
23 
25  private $testDb;
27  private $dbManager;
29  private $testInstaller;
31  private $runnerCli;
32 
33  protected function setUp() : void
34  {
35  $this->testDb = new TestPgDb("clixmltest");
36  $this->dbManager = $this->testDb->getDbManager();
37 
38  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
39  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
40 
41  $this->agentDir = dirname(dirname(__DIR__));
42  }
43 
44  protected function tearDown() : void
45  {
46  $this->testDb->fullDestruct();
47  $this->testDb = null;
48  $this->dbManager = null;
49  }
50 
51  private function setUpRepo()
52  {
53  $sysConf = $this->testDb->getFossSysConf();
54  $this->testInstaller = new TestInstaller($sysConf);
55  $this->testInstaller->init();
56  $this->testInstaller->cpRepo();
57  $this->testInstaller->install($this->agentDir);
58  }
59 
60  private function rmRepo()
61  {
62  $this->testInstaller->uninstall($this->agentDir);
63  $this->testInstaller->rmRepo();
64  $this->testInstaller->clear();
65  }
66 
67  private function setUpTables()
68  {
69  $this->testDb->createPlainTables(array(),true);
70  $this->testDb->createInheritedTables();
71  $this->dbManager->queryOnce("CREATE TABLE copyright_ars () INHERITS (ars_master)");
72 
73  $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','license_ref_bulk_lrb_pk_seq','clearing_decision_clearing_decision_pk_seq','clearing_event_clearing_event_pk_seq'));
74  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','FileLicense_pkey','clearing_event_pkey'));
75  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk','clearing_event','clearing_decision','license_file','highlight'));
76 
77  $this->testDb->insertData(array('mimetype_ars','pkgagent_ars','ununpack_ars','decider_ars'),true,__DIR__.'/fo_report.sql');
78  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
79  }
80 
81  private function getHeartCount($output)
82  {
83  $matches = array();
84  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
85  return intval($matches[1]);
86  }
87  return -1;
88  }
89 
92  {
93  $this->setUpTables();
94  $this->setUpRepo();
95  $this->runAndTestReport();
96  }
97 
100  {
101  $this->setUpTables();
102  $this->setUpRepo();
103 
104  $uploadId = 1;
105  $this->dbManager->queryOnce("ALTER TABLE uploadtree_a RENAME TO uploadtree_$uploadId", __METHOD__.'.alterUploadtree');
106  $this->dbManager->getSingleRow("UPDATE upload SET uploadtree_tablename=$1 WHERE upload_pk=$2",
107  array("uploadtree_$uploadId",$uploadId),__METHOD__.'.alterUpload');
108 
109  $this->runAndTestReport($uploadId);
110  }
111 
112  public function runAndTestReport($uploadId=1)
113  {
114  list($success,$output,$retCode) = $this->runnerCli->run($uploadId, $this->userId, $this->groupId, $jobId=7);
115 
116  assertThat('cannot run runner', $success, equalTo(true));
117  assertThat( 'report failed: "'.$output.'"', $retCode, equalTo(0));
118  assertThat($this->getHeartCount($output), greaterThan(0));
119 
120  $row = $this->dbManager->getSingleRow("SELECT upload_fk,job_fk,filepath FROM reportgen WHERE job_fk = $1", array($jobId), "reportFileName");
121  assertThat($row, hasKeyValuePair('upload_fk', $uploadId));
122  assertThat($row, hasKeyValuePair('job_fk', $jobId));
123  $filepath = $row['filepath'];
124  assertThat($filepath, endsWith('.xml'));
125  assertThat(file_exists($filepath),equalTo(true));
126 
127  $licenseNameTest = 'Condor/condor-1.1';
128  assertThat(file_get_contents($filepath), stringContainsInOrder($licenseNameTest));
129 
130  $licenseTextTest = 'Here is an alternative license text';
131  assertThat(file_get_contents($filepath), stringContainsInOrder($licenseTextTest));
132 
133  $licenseNameNotTest = 'LGPL-2.1+';
134  assertThat(file_get_contents($filepath), not(stringContainsInOrder($licenseNameNotTest)));
135 
136  $copyrightStatement = 'Copyright (c) 1999 University of Chicago and The University of Southern California';
137  assertThat(file_get_contents($filepath), stringContainsInOrder($copyrightStatement));
138 
139  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
140  unlink($filepath);
141  $this->rmRepo();
142  }
143 }
int Test
Definition: util.c:20
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308