FOSSology  4.4.0
Open Source License Compliance by Open Source Software
schedulerTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015, 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
20 use Monolog\Logger;
21 
22 class MonkScheduledTest extends \PHPUnit\Framework\TestCase
23 {
25  private $testDb;
27  private $dbManager;
29  private $testInstaller;
30 
32  private $licenseDao;
34  private $clearingDao;
36  private $uploadDao;
38  private $uploadPermDao;
40  private $highlightDao;
41 
42  protected function setUp() : void
43  {
44  $this->testDb = new TestPgDb("monkSched");
45  $this->dbManager = $this->testDb->getDbManager();
46 
47  $this->licenseDao = new LicenseDao($this->dbManager);
48  $logger = new Logger("SchedulerTest");
49  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
50  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
51  $this->highlightDao = new HighlightDao($this->dbManager);
52  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
53 
54  $this->agentDir = dirname(__DIR__, 4).'/build/src/monk';
55  }
56 
57  protected function tearDown() : void
58  {
59  $this->testDb->fullDestruct();
60  $this->testDb = null;
61  $this->dbManager = null;
62  $this->licenseDao = null;
63  $this->highlightDao = null;
64  $this->clearingDao = null;
65  }
66 
67  private function runMonk($uploadId, $userId=2, $groupId=2, $jobId=1, $args="")
68  {
69  $sysConf = $this->testDb->getFossSysConf();
70 
71  $agentName = "monk";
72  $execDir = dirname(__DIR__,4).'/build/src/monk/agent';
73 
74  $pipeFd = popen("echo $uploadId | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start $args", "r");
75  $this->assertTrue($pipeFd !== false, 'running monk failed');
76 
77  $output = "";
78  while (($buffer = fgets($pipeFd, 4096)) !== false) {
79  $output .= $buffer;
80  }
81  $retCode = pclose($pipeFd);
82 
83  return array($output,$retCode);
84  }
85 
86  private function setUpRepo()
87  {
88  $sysConf = $this->testDb->getFossSysConf();
89  $this->testInstaller = new TestInstaller($sysConf);
90  $this->testInstaller->init();
91  $this->testInstaller->cpRepo();
92  $this->testInstaller->install($this->agentDir);
93  }
94 
95  private function rmRepo()
96  {
97  $this->testInstaller->uninstall($this->agentDir);
98  $this->testInstaller->clear();
99  $this->testInstaller->rmRepo();
100  }
101 
102  private function setUpTables()
103  {
104  $this->testDb->createPlainTables(array('upload','uploadtree','uploadtree_a','license_ref','license_ref_bulk','license_set_bulk',
105  'clearing_event','clearing_decision','clearing_decision_event','license_file','highlight','highlight_bulk','agent','pfile','ars_master','users'),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','license_ref_bulk_lrb_pk_seq','clearing_event_clearing_event_pk_seq','clearing_decision_clearing_decision_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','clearing_event_pkey','clearing_decision_pkey'),false);
109  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_ref','license_set_bulk','clearing_event','license_file','highlight','clearing_decision'),false);
110  $this->testDb->createInheritedTables();
111  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users'), false);
112  $this->testDb->insertData_license_ref(200);
113  }
114 
115  private function getHeartCount($output)
116  {
117  $matches = array();
118  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches))
119  {
120  return intval($matches[1]);
121  }
122  return 0;
123  }
124 
126  public function testRunMonkScan()
127  {
128  $this->setUpTables();
129  $this->setUpRepo();
130 
131  list($output,$retCode) = $this->runMonk($uploadId=1);
132 
133  $this->rmRepo();
134 
135  $this->assertEquals($retCode, 0, 'monk failed: '.$output);
136 
137  $this->assertEquals(6, $this->getHeartCount($output));
138 
139  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
140  $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
141 
142  $this->assertEquals($expected=2, count($matches));
143 
145  $licenseMatch = $matches[0];
146 
147  $this->assertEquals($expected=4, $licenseMatch->getFileId());
148 
150  $matchedLicense = $licenseMatch->getLicenseRef();
151  $this->assertEquals($matchedLicense->getShortName(), "GPL-3.0-only");
152 
154  $agentRef = $licenseMatch->getAgentRef();
155  $this->assertEquals($agentRef->getAgentName(), "monk");
156 
157  $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(7));
158 
159  $expectedHighlight = new Highlight(18, 35824, Highlight::MATCH, 0, 34505);
160  $expectedHighlight->setLicenseId($matchedLicense->getId());
161 
162  $this->assertEquals(array($expectedHighlight), $highlights);
163 
164  $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(11));
165 
166  $expectedHighlights = array();
167  $expectedHighlights[] = new Highlight(18, 338, Highlight::MATCH, 0, 265);
168  $expectedHighlights[] = new Highlight(339, 346, Highlight::CHANGED, 266, 272);
169  $expectedHighlights[] = new Highlight(347, 35148, Highlight::MATCH, 273, 34505);
170  foreach($expectedHighlights as $expectedHighlight) {
171  $expectedHighlight->setLicenseId($matchedLicense->getId());
172  }
173  assertThat($highlights, containsInAnyOrder($expectedHighlights));
174  }
175 
177  public function testRunMonkTwiceOnAScan()
178  {
179  $this->setUpTables();
180  $this->setUpRepo();
181 
182  list($output,$retCode) = $this->runMonk($uploadId=1);
183  list($output2,$retCode2) = $this->runMonk($uploadId);
184 
185  $this->assertEquals($retCode, 0, 'monk failed: '.$output);
186  $this->assertEquals(6, $this->getHeartCount($output));
187 
188  $this->assertEquals($retCode2, 0, 'monk failed: '.$output2);
189  $this->assertEquals(0, $this->getHeartCount($output2));
190 
191  $this->rmRepo();
192 
193  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
194  $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
195  $this->assertEquals($expected=2, count($matches));
196 
198  $licenseMatch = $matches[0];
199  $this->assertEquals($expected=4, $licenseMatch->getFileId());
200 
202  $matchedLicense = $licenseMatch->getLicenseRef();
203  $this->assertEquals($matchedLicense->getShortName(), "GPL-3.0-only");
204 
206  $agentRef = $licenseMatch->getAgentRef();
207  $this->assertEquals($agentRef->getAgentName(), "monk");
208  }
209 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16