FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
bulkTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, 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 
29 use Monolog\Logger;
30 
31 class MonkBulkTest extends \PHPUnit\Framework\TestCase
32 {
34  private $testDb;
36  private $dbManager;
38  private $licenseDao;
40  private $clearingDao;
42  private $uploadDao;
44  private $uploadPermDao;
46  private $highlightDao;
47 
48  protected function setUp()
49  {
50  $this->testDb = new TestPgDb("monkBulk");
51  $this->dbManager = $this->testDb->getDbManager();
52 
53  $this->licenseDao = new LicenseDao($this->dbManager);
54  $logger = new Logger("MonkBulkTest");
55  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
56  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
57  $this->highlightDao = new HighlightDao($this->dbManager);
58  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
59  }
60 
61  protected function tearDown()
62  {
63  $this->testDb->fullDestruct();
64  $this->testDb = null;
65  $this->dbManager = null;
66  $this->licenseDao = null;
67  $this->highlightDao = null;
68  $this->clearingDao = null;
69  }
70 
71  private function runBulkMonk($userId = 2, $groupId = 2, $jobId = 1, $bulkId = 3)
72  {
73  $sysConf = $this->testDb->getFossSysConf();
74 
75  $agentName = "monkbulk";
76 
77  $agentDir = dirname(dirname(__DIR__));
78  $execDir = __DIR__;
79  system("install -D $agentDir/VERSION-monkbulk $sysConf/mods-enabled/$agentName/VERSION");
80 
81  $pipeFd = popen("echo '0\n$bulkId\n0' | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start", "r");
82  $this->assertTrue($pipeFd !== false, 'running monk bulk failed');
83 
84  $output = "";
85  while (($buffer = fgets($pipeFd, 4096)) !== false) {
86  $output .= $buffer;
87  }
88  $retCode = pclose($pipeFd);
89 
90  unlink("$sysConf/mods-enabled/$agentName/VERSION");
91  rmdir("$sysConf/mods-enabled/$agentName");
92  rmdir("$sysConf/mods-enabled");
93 
94  return array($output,$retCode);
95  }
96 
97  private function setUpRepo()
98  {
99  $sysConf = $this->testDb->getFossSysConf();
100 
101  $confFile = $sysConf."/fossology.conf";
102  system("touch ".$confFile);
103  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
104  file_put_contents($confFile, $config);
105 
106  $testRepoDir = dirname(dirname(dirname(__DIR__)))."/lib/php/Test/";
107  system("cp -a $testRepoDir/repo $sysConf/");
108  }
109 
110  private function rmRepo()
111  {
112  $sysConf = $this->testDb->getFossSysConf();
113  system("rm $sysConf/repo -rf");
114  unlink($sysConf."/fossology.conf");
115  }
116 
117  private function setUpTables()
118  {
119  $this->testDb->createPlainTables(array('upload','uploadtree','license_ref','license_ref_bulk', 'license_set_bulk',
120  'clearing_event','clearing_decision','clearing_decision_event','license_file','highlight','highlight_bulk','agent','pfile','ars_master','users'),false);
121  $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'),false);
122  $this->testDb->createViews(array('license_file_ref'),false);
123  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','FileLicense_pkey','clearing_event_pkey', 'license_ref_bulk_pkey', 'license_set_bulk_fkey'),false);
124  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_set_bulk','clearing_event','license_file','highlight'),false);
125  $this->testDb->createInheritedTables();
126  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users'), false);
127  $this->testDb->insertData_license_ref();
128  }
129 
130  private function getHeartCount($output)
131  {
132  $matches = array();
133  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
134  return intval($matches[1]);
135  }
136  else {
137  return 0;
138  }
139  }
140 
142  public function testDatabaseSetup()
143  {
144  $this->setUpTables();
145  $this->setUpRepo();
146 
147  $this->rmRepo();
148  }
149 
152  {
153  $this->setUpTables();
154  $this->setUpRepo();
155 
156  $userId = 2;
157  $groupId = 2;
158  $uploadTreeId = 1;
159 
160  $licenseId = 225;
161  $removing = false;
162  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
163 
164  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseId => $removing), $refText);
165 
166  $this->assertGreaterThan($expected=0, $bulkId);
167 
168  $jobId = 64;
169  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
170 
171  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
172  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
173  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
174  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
175  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
176 
177  assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
178  assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
179  assertThat($relevantDecisionsItem6,hasKeyInArray($licenseId));
180 
181  $refSecondText = "Our General Public Licenses are designed to make sure that you " .
182  "have the freedom to distribute copies of free software";
183  $licenseSecondId = 215;
184  $bulkSecondId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseSecondId => $removing), $refSecondText);
185 
186  $jobId++;
187  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkSecondId);
188 
189  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
190  $relevantDecisionsItemPfile3 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
191  $relevantDecisionsItemPfile4 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
192  assertThat(count($relevantDecisionsItemPfile3), is(equalTo(1)));
193 
194  assertThat(count($relevantDecisionsItemPfile4), is(equalTo(2)));
195  assertThat($relevantDecisionsItemPfile4, hasKeyInArray($licenseSecondId));
196 
197  $this->rmRepo();
198  }
199 
201  public function testRunMonkBulkScan()
202  {
203  $this->setUpTables();
204  $this->setUpRepo();
205 
206  $userId = 2;
207  $groupId = 2;
208  $uploadTreeId = 1;
209 
210  $licenseId1 = 225;
211  $removing1 = false;
212  $licenseId2 = 213;
213  $removing2 = false;
214  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
215 
216  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
217  array($licenseId1 => $removing1, $licenseId2 => $removing2), $refText);
218 
219  $this->assertGreaterThan($expected=0, $bulkId);
220 
221  $jobId = 64;
222  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
223  $this->assertEquals(6, $this->getHeartCount($output));
224  $this->rmRepo();
225 
226  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
227  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
228  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
229  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
230  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
231 
232  assertThat(count($relevantDecisionsItem6),is(equalTo(2)));
233  assertThat(count($relevantDecisionsItem7),is(equalTo(2)));
234  $rfForACE = 225;
235  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
237  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
238  $eventId = $clearingEvent->getEventId();
239  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
240 
241  assertThat(count($bulkHighlights), is(1));
242 
244  $bulkHighlight1 = $bulkHighlights[0];
245  assertThat($bulkHighlight1->getLicenseId(), is(equalTo($licenseId1)));
246  assertThat($bulkHighlight1->getType(), is(equalTo(Highlight::BULK)));
247  assertThat($bulkHighlight1->getStart(), is(3));
248  assertThat($bulkHighlight1->getEnd(), is(103));
249 
250  $rfForACE = 213;
251  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
253  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
254  $eventId = $clearingEvent->getEventId();
255  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
256 
257  assertThat(count($bulkHighlights), is(1));
258 
260  $bulkHighlight2 = $bulkHighlights[0];
261  assertThat($bulkHighlight2->getLicenseId(), is(equalTo($licenseId2)));
262  assertThat($bulkHighlight2->getType(), is(equalTo(Highlight::BULK)));
263  assertThat($bulkHighlight2->getStart(), is(3));
264  assertThat($bulkHighlight2->getEnd(), is(103));
265 
266  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
267 
268  assertThat(count($bulkHighlights), is(equalTo(2)));
269  assertThat($bulkHighlights, containsInAnyOrder($bulkHighlight1, $bulkHighlight2));
270  }
271 
273  public function testRunMonkBulkScanWithMultipleLicenses()
274  {
275  $this->setUpTables();
276  $this->setUpRepo();
277 
278  $userId = 2;
279  $groupId = 2;
280  $uploadTreeId = 1;
281 
282  $licenseId = 225;
283  $removing = false;
284  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
285 
286  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseId => $removing), $refText);
287 
288  $this->assertGreaterThan($expected = 0, $bulkId);
289 
290  $jobId = 64;
291  list($output, $retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
292  $this->assertEquals(6, $this->getHeartCount($output));
293  $this->rmRepo();
294 
295  $this->assertEquals($retCode, 0, 'monk bulk failed: ' . $output);
296  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
297  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
298  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
299  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
300 
301  assertThat(count($relevantDecisionsItem6), is(equalTo(1)));
302  assertThat(count($relevantDecisionsItem7), is(equalTo(1)));
303  $rfForACE = 225;
304  assertThat($relevantDecisionsItem6, hasKeyInArray($rfForACE));
306  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
307  $eventId = $clearingEvent->getEventId();
308  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
309 
310  $this->assertEquals(1, count($bulkHighlights));
311 
313  $bulkHighlight = $bulkHighlights[0];
314  $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
315  $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
316  $this->assertEquals(3, $bulkHighlight->getStart());
317  $this->assertEquals(103, $bulkHighlight->getEnd());
318 
319  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
320 
321  $this->assertEquals(1, count($bulkHighlights));
322  $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
323  }
324 
327  {
328  $this->setUpTables();
329  $this->setUpRepo();
330 
331  $userId = 2;
332  $groupId = 2;
333  $uploadTreeId = 1;
334 
335  $licenseId = 225;
336  $removing = false;
337  $refText = "The GNU General Public License is copyleft license for software and other kinds of works.";
338 
339  $jobId = 64;
340 
341  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseId => $removing), $refText);
342 
343  $this->assertGreaterThan($expected=0, $bulkId);
344 
345  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
346 
347  $this->rmRepo();
348 
349  $this->assertEquals($retCode, 0, "monk bulk failed: $output");
350  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
351  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
352  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
353  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
354 
355  $this->assertEquals($expected=0, count($relevantDecisionsItem6));
356  $this->assertEquals($expected=0, count($relevantDecisionsItem7));
357  }
358 
360  public function testRunMonkBulkScanWithAShortSearch()
361  {
362  $this->setUpTables();
363  $this->setUpRepo();
364 
365  $userId = 2;
366  $groupId = 2;
367  $uploadTreeId = 1;
368 
369  $licenseId = 225;
370  $removing = false;
371  $refText = "The GNU";
372 
373  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseId => $removing), $refText);
374 
375  $this->assertGreaterThan($expected=0, $bulkId);
376 
377  $jobId = 64;
378  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
379 
380  $this->rmRepo();
381 
382  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
383  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
384  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
385  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
386  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
387 
388  assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
389  assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
390  $rfForACE = 225;
391  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
393  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
394  $eventId = $clearingEvent->getEventId();
395  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
396 
397  $this->assertEquals(1, count($bulkHighlights));
398 
400  $bulkHighlight = $bulkHighlights[0];
401  $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
402  $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
403  $this->assertEquals(3, $bulkHighlight->getStart());
404  $this->assertEquals(10, $bulkHighlight->getEnd());
405 
406  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
407 
408  $this->assertEquals(1, count($bulkHighlights));
409  $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
410  }
411 
414  {
415  $this->setUpTables();
416  $this->setUpRepo();
417 
418  $userId = 2;
419  $groupId = 2;
420  $uploadTreeId = 1;
421 
422  $licenseId = 225;
423  $removing = false;
424  $refText = "";
425 
426  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId, array($licenseId => $removing), $refText);
427 
428  $this->assertGreaterThan($expected=0, $bulkId);
429 
430  $jobId = 64;
431  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
432 
433  $this->rmRepo();
434 
435  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
436  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
437  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
438  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
439  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
440 
441  assertThat(count($relevantDecisionsItem6),is(equalTo(0)));
442  assertThat(count($relevantDecisionsItem7),is(equalTo(0)));
443 
444  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
445 
446  assertThat(count($bulkHighlights),is(equalTo(0)));
447  }
448 }
testDatabaseSetup()
Definition: bulkTest.php:142
testRunMonkBulkScanWithBadSearchForDiff()
Definition: bulkTest.php:326
testRunTwoIndependentMonkBulkScans()
Definition: bulkTest.php:151
testRunMonkBulkScanWithAnEmptySearchText()
Definition: bulkTest.php:413
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28