FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
schedulerTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015,2019 Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
31 use Monolog\Logger;
32 
33 class MonkScheduledTest extends \PHPUnit\Framework\TestCase
34 {
36  private $testDb;
38  private $dbManager;
40  private $testInstaller;
41 
43  private $licenseDao;
45  private $clearingDao;
47  private $uploadDao;
49  private $uploadPermDao;
51  private $highlightDao;
52 
53  protected function setUp()
54  {
55  $this->testDb = new TestPgDb("monkSched");
56  $this->dbManager = $this->testDb->getDbManager();
57 
58  $this->licenseDao = new LicenseDao($this->dbManager);
59  $logger = new Logger("SchedulerTest");
60  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
61  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
62  $this->highlightDao = new HighlightDao($this->dbManager);
63  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
64 
65  $this->agentDir = dirname(dirname(__DIR__));
66  }
67 
68  protected function tearDown()
69  {
70  $this->testDb->fullDestruct();
71  $this->testDb = null;
72  $this->dbManager = null;
73  $this->licenseDao = null;
74  $this->highlightDao = null;
75  $this->clearingDao = null;
76  }
77 
78  private function runMonk($uploadId, $userId=2, $groupId=2, $jobId=1, $args="")
79  {
80  $sysConf = $this->testDb->getFossSysConf();
81 
82  $agentName = "monk";
83  $execDir = __DIR__;
84 
85  $pipeFd = popen("echo $uploadId | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start $args", "r");
86  $this->assertTrue($pipeFd !== false, 'running monk failed');
87 
88  $output = "";
89  while (($buffer = fgets($pipeFd, 4096)) !== false) {
90  $output .= $buffer;
91  }
92  $retCode = pclose($pipeFd);
93 
94  return array($output,$retCode);
95  }
96 
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 
106  private function rmRepo()
107  {
108  $this->testInstaller->uninstall($this->agentDir);
109  $this->testInstaller->clear();
110  $this->testInstaller->rmRepo();
111  }
112 
113  private function setUpTables()
114  {
115  $this->testDb->createPlainTables(array('upload','uploadtree','uploadtree_a','license_ref','license_ref_bulk','license_set_bulk',
116  'clearing_event','clearing_decision','clearing_decision_event','license_file','highlight','highlight_bulk','agent','pfile','ars_master','users'),false);
117  $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);
118  $this->testDb->createViews(array('license_file_ref'),false);
119  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','FileLicense_pkey','clearing_event_pkey','clearing_decision_pkey'),false);
120  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk','clearing_event','license_file','highlight','clearing_decision'),false);
121  $this->testDb->createInheritedTables();
122  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users'), false);
123  $this->testDb->insertData_license_ref(200);
124  }
125 
126  private function getHeartCount($output)
127  {
128  $matches = array();
129  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches))
130  {
131  return intval($matches[1]);
132  }
133  return 0;
134  }
135 
137  public function testRunMonkScan()
138  {
139  $this->setUpTables();
140  $this->setUpRepo();
141 
142  list($output,$retCode) = $this->runMonk($uploadId=1);
143 
144  $this->rmRepo();
145 
146  $this->assertEquals($retCode, 0, 'monk failed: '.$output);
147 
148  $this->assertEquals(6, $this->getHeartCount($output));
149 
150  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
151  $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
152 
153  $this->assertEquals($expected=2, count($matches));
154 
156  $licenseMatch = $matches[0];
157 
158  $this->assertEquals($expected=4, $licenseMatch->getFileId());
159 
161  $matchedLicense = $licenseMatch->getLicenseRef();
162  $this->assertEquals($matchedLicense->getShortName(), "GPL-3.0");
163 
165  $agentRef = $licenseMatch->getAgentRef();
166  $this->assertEquals($agentRef->getAgentName(), "monk");
167 
168  $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(7));
169 
170  $expectedHighlight = new Highlight(18, 35825, Highlight::MATCH, 20, 35819);
171  $expectedHighlight->setLicenseId($matchedLicense->getId());
172 
173  $this->assertEquals(array($expectedHighlight), $highlights);
174 
175  $highlights = $this->highlightDao->getHighlightDiffs($this->uploadDao->getItemTreeBounds(11));
176 
177  $expectedHighlights = array();
178  $expectedHighlights[] = new Highlight(18, 339, Highlight::MATCH, 20, 350);
179  $expectedHighlights[] = new Highlight(340, 347, Highlight::CHANGED, 351, 357);
180  $expectedHighlights[] = new Highlight(348, 35149, Highlight::MATCH, 358, 35819);
181  foreach($expectedHighlights as $expectedHighlight) {
182  $expectedHighlight->setLicenseId($matchedLicense->getId());
183  }
184  assertThat($highlights, containsInAnyOrder($expectedHighlights));
185  }
186 
188  public function testRunMonkTwiceOnAScan()
189  {
190  $this->setUpTables();
191  $this->setUpRepo();
192 
193  list($output,$retCode) = $this->runMonk($uploadId=1);
194  list($output2,$retCode2) = $this->runMonk($uploadId);
195 
196  $this->assertEquals($retCode, 0, 'monk failed: '.$output);
197  $this->assertEquals(6, $this->getHeartCount($output));
198 
199  $this->assertEquals($retCode2, 0, 'monk failed: '.$output2);
200  $this->assertEquals(0, $this->getHeartCount($output2));
201 
202  $this->rmRepo();
203 
204  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
205  $matches = $this->licenseDao->getAgentFileLicenseMatches($bounds);
206  $this->assertEquals($expected=2, count($matches));
207 
209  $licenseMatch = $matches[0];
210  $this->assertEquals($expected=4, $licenseMatch->getFileId());
211 
213  $matchedLicense = $licenseMatch->getLicenseRef();
214  $this->assertEquals($matchedLicense->getShortName(), "GPL-3.0");
215 
217  $agentRef = $licenseMatch->getAgentRef();
218  $this->assertEquals($agentRef->getAgentName(), "monk");
219  }
220 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28