FOSSology  4.4.0
Open Source License Compliance by Open Source Software
schedulerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015, 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
17 namespace Fossology\Reuser\Test;
18 
37 use Monolog\Logger;
38 
39 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
40 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
41 include_once(__DIR__.'/SchedulerTestRunnerMock.php');
42 
47 class SchedulerTest extends \PHPUnit\Framework\TestCase
48 {
52  private $groupId = 3;
56  private $userId = 2;
60  private $testDb;
64  private $dbManager;
68  private $testInstaller;
72  private $licenseDao;
76  private $clearingDao;
80  private $copyrightDao;
88  private $uploadDao;
92  private $uploadPermDao;
96  private $highlightDao;
100  private $treeDao;
101 
105  private $runnerCli;
106 
110  private $runnerMock;
111 
115  protected function setUp() : void
116  {
117  $this->testDb = new TestPgDb("reuserSched");
118  $this->dbManager = $this->testDb->getDbManager();
119 
120  $this->licenseDao = new LicenseDao($this->dbManager);
121  $logger = new Logger("ReuserSchedulerTest");
122  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
123  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
124  $this->highlightDao = new HighlightDao($this->dbManager);
125  $this->clearingDecisionFilter = new ClearingDecisionFilter();
126  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
127  $this->copyrightDao = new CopyrightDao($this->dbManager, $this->uploadDao);
128  $this->treeDao = \Mockery::mock(TreeDao::class);
129 
130  $agentDao = new AgentDao($this->dbManager, $logger);
131 
132  $this->runnerMock = new SchedulerTestRunnerMock($this->dbManager, $agentDao,
133  $this->clearingDao, $this->uploadDao, $this->clearingDecisionFilter,
134  $this->treeDao, $this->copyrightDao);
135  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
136  }
137 
141  protected function tearDown() : void
142  {
143  $this->testDb->fullDestruct();
144  $this->testDb = null;
145  $this->dbManager = null;
146  $this->licenseDao = null;
147  $this->highlightDao = null;
148  $this->clearingDao = null;
149  $this->copyrightDao = null;
150  }
151 
155  private function setUpRepo()
156  {
157  $sysConf = $this->testDb->getFossSysConf();
158  $this->testInstaller = new TestInstaller($sysConf);
159  $this->testInstaller->init();
160  $this->testInstaller->cpRepo();
161  }
162 
166  private function rmRepo()
167  {
168  $this->testInstaller->rmRepo();
169  $this->testInstaller->clear();
170  }
171 
175  private function setUpTables()
176  {
177  $this->testDb->createPlainTables(array('upload','upload_reuse','uploadtree',
178  'uploadtree_a','license_ref','license_ref_bulk','clearing_decision',
179  'clearing_decision_event','clearing_event','license_file','highlight',
180  'highlight_bulk','agent','pfile','ars_master','users','group_user_member',
181  'upload_clearing_license','report_info'),false);
182  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq',
183  'upload_upload_pk_seq','nomos_ars_ars_pk_seq','license_file_fl_pk_seq',
184  'license_ref_rf_pk_seq','license_ref_bulk_lrb_pk_seq',
185  'clearing_decision_clearing_decision_pk_seq',
186  'clearing_event_clearing_event_pk_seq','report_info_pk_seq'),false);
187  $this->testDb->createViews(array('license_file_ref'),false);
188  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey',
189  'upload_pkey_idx','FileLicense_pkey','clearing_event_pkey'),false);
190  $this->testDb->alterTables(array('agent','pfile','upload','ars_master',
191  'license_ref_bulk','license_ref','clearing_event','clearing_decision','license_file','highlight'),false);
192  $this->testDb->createInheritedTables();
193  $this->testDb->createInheritedArsTables(array('monk'));
194 
195  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users',
196  'group_user_member','agent','license_file','monk_ars','report_info'),
197  false);
198  $this->testDb->insertData_license_ref(80);
199 
200  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
201 
202  $this->testDb->setupSysconfig();
203  }
204 
208  private function getHeartCount($output)
209  {
210  $matches = array();
211  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
212  return intval($matches[1]);
213  } else {
214  return -1;
215  }
216  }
217 
224  private function getFilteredClearings($uploadId, $groupId)
225  {
226  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
227  return $this->clearingDao->getFileClearingsFolder($bounds, $groupId);
228  }
229 
238  {
239  $this->runnerReuserScanWithoutAnyUploadToCopyAndNoClearing($this->runnerMock);
240  }
241 
250  {
252  }
253 
259  {
260  $this->setUpTables();
261  $this->setUpRepo();
262 
263  list($success, $output,$retCode) = $runner->run($uploadId=1, $this->userId);
264 
265  $this->assertTrue($success, 'cannot run runner');
266  $this->assertEquals($retCode, 0, 'reuser failed: '.$output);
267 
268  assertThat($this->getHeartCount($output), equalTo(0));
269 
270  $bounds = $this->uploadDao->getParentItemBounds($uploadId);
271  assertThat($this->clearingDao->getFileClearingsFolder($bounds, $groupId=5), is(emptyArray()));
272 
273  $this->rmRepo();
274  }
275 
282  protected function insertDecisionFromTwoEvents($scope=DecisionScopes::ITEM,$originallyClearedItemId=23)
283  {
284  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
285  $licenseRef2 = $this->licenseDao->getLicenseByShortName("Glide")->getRef();
286 
287  $addedLicenses = array($licenseRef1, $licenseRef2);
288  assertThat($addedLicenses, not(arrayContaining(null)));
289 
290  $clearingLicense1 = new ClearingLicense($licenseRef1, false, ClearingEventTypes::USER, "42", "44");
291  $clearingLicense2 = new ClearingLicense($licenseRef2, true, ClearingEventTypes::USER, "-42", "-44");
292 
293  $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, $this->userId, $this->groupId,
294  $licenseRef1->getId(), $clearingLicense1->isRemoved(),
295  $clearingLicense1->getType(), $clearingLicense1->getReportinfo(), $clearingLicense1->getComment());
296  $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $this->groupId,
297  $licenseRef2->getId(), $clearingLicense2->isRemoved(),
298  $clearingLicense2->getType(), $clearingLicense2->getReportinfo(), $clearingLicense2->getComment());
299 
300  $addedEventIds = array($eventId1, $eventId2);
301 
302  $this->clearingDao->createDecisionFromEvents($originallyClearedItemId, $this->userId,
303  $this->groupId, DecisionTypes::IDENTIFIED, $scope, $addedEventIds);
304 
305  return array($clearingLicense1, $clearingLicense2, $addedEventIds);
306  }
307 
315  {
316  $this->runnerReuserScanWithoutAnyUploadToCopyAndAClearing($this->runnerMock);
317  }
318 
326  {
328  }
329 
335  {
336  $this->setUpTables();
337  $this->setUpRepo();
338 
340 
341  list($success,$output,$retCode) = $runner->run($uploadId=3);
342 
343  $this->assertTrue($success, 'cannot run runner');
344  $this->assertEquals($retCode, 0, 'reuser failed: '.$output);
345 
346  assertThat($this->getHeartCount($output), equalTo(0));
347 
348  $decisions = $this->getFilteredClearings($uploadId, $this->groupId);
349  assertThat($decisions, is(emptyArray()));
350 
351  $this->rmRepo();
352  }
353 
365  {
366  $this->runnerReuserScanWithALocalClearing($this->runnerMock,1);
367  }
368 
380  {
381  $this->runnerReuserScanWithALocalClearing($this->runnerCli,1);
382  }
383 
389  private function runnerReuserScanWithALocalClearing($runner, $heartBeat=0)
390  {
391  $this->setUpTables();
392  $this->setUpRepo();
393 
394  $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId);
395 
396  list($clearingLicense1, $clearingLicense2, $addedEventIds) = $this->insertDecisionFromTwoEvents();
397 
398  /* upload 3 in the test db is the same as upload 2
399  * items 13-24 in upload 2 correspond to 33-44 */
400  $reusingUploadItemShift = 20;
401 
402  list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
403 
404  $this->assertTrue($success, 'cannot run runner');
405  $this->assertEquals($retCode, 0, 'reuser failed: '.$output);
406  assertThat($this->getHeartCount($output), equalTo($heartBeat));
407 
408  $newUploadClearings = $this->getFilteredClearings($uploadId, $this->groupId);
409  $potentiallyReusableClearings = $this->getFilteredClearings($reusedUpload, $this->groupId);
410 
411  assertThat($newUploadClearings, is(arrayWithSize(1)));
412 
413  assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
415  $potentiallyReusableClearing = $potentiallyReusableClearings[0];
417  $newClearing = $newUploadClearings[0];
418 
419  assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
420  assertThat($newClearing->getClearingId(), not(equalTo($potentiallyReusableClearing->getClearingId())));
421 
422  assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicense1, $clearingLicense2));
423 
424  assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
425  assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
426 
427  assertThat($newClearing->getUploadTreeId(),
428  equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
429 
430  $this->rmRepo();
431  }
432 
444  {
445  $this->runnerReuserScanWithARepoClearing($this->runnerMock);
446  }
447 
459  {
460  $this->runnerReuserScanWithARepoClearing($this->runnerCli);
461  }
462 
467  private function runnerReuserScanWithARepoClearing($runner)
468  {
469  $this->setUpTables();
470  $this->setUpRepo();
471 
472  $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId);
473 
474  list($clearingLicense1, $clearingLicense2, $addedEventIds) = $this->insertDecisionFromTwoEvents(
475  DecisionScopes::REPO,$originallyClearedItemId=23);
476  $clearingLicenses = array($clearingLicense1, $clearingLicense2);
477 
478  /* upload 3 in the test db is the same as upload 2
479  * items 13-24 in upload 2 correspond to 33-44 */
480  $reusingUploadItemShift = 20;
481 
482  list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
483 
484  $this->assertTrue($success, 'cannot run runner');
485  $this->assertEquals($retCode, 0, 'reuser failed: '.$output);
486 
487  assertThat($this->getHeartCount($output), equalTo(0));
488 
489  $newUploadClearings = $this->getFilteredClearings($uploadId, $this->groupId);
490  $potentiallyReusableClearings = $this->getFilteredClearings($reusedUpload, $this->groupId);
491 
492  assertThat($newUploadClearings, is(arrayWithSize(1)));
493 
494  assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
496  $potentiallyReusableClearing = $potentiallyReusableClearings[0];
498  $newClearing = $newUploadClearings[0];
499 
500  /* they are actually the same ClearingDecision
501  * only sameFolder and sameUpload are different */
502  assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
503 
504  /* reuser should have not created a new clearing decision */
505  assertThat($newClearing->getClearingId(), equalTo($potentiallyReusableClearing->getClearingId()));
506 
507  assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicenses));
508 
509  assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
510  assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
511 
512  assertThat($newClearing->getUploadTreeId(),
513  equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
514 
515  /* reuser should have not created a correct local event history */
516  $bounds = $this->uploadDao->getItemTreeBounds($originallyClearedItemId + $reusingUploadItemShift);
517  $newEvents = $this->clearingDao->getRelevantClearingEvents($bounds, $this->groupId);
518 
519  assertThat($newEvents, is(arrayWithSize(count($clearingLicenses))));
520 
522  foreach ($newEvents as $newEvent) {
523  assertThat($newEvent->getEventId(), anyOf($addedEventIds));
524  assertThat($newEvent->getClearingLicense(), anyOf($clearingLicenses));
525  }
526 
527  $this->rmRepo();
528  }
529 
542  {
543  $this->runnerReuserScanWithARepoClearingEnhanced($this->runnerMock);
544  }
545 
550  private function runnerReuserScanWithARepoClearingEnhanced($runner)
551  {
552  $this->setUpTables();
553  $this->setUpRepo();
554 
555  $originallyClearedItemId = 23;
556  /* upload 3 in the test db is the same as upload 2 -> items 13-24 in upload 2 correspond to 33-44 */
557  $reusingUploadItemShift = 20;
558 
559  $this->uploadDao->addReusedUpload($uploadId=3,$reusedUpload=2,$this->groupId,$this->groupId,$reuseMode=2);
560 
561  $repoPath = $this->testDb->getFossSysConf().'/repo/files/';
562  $this->treeDao->shouldReceive('getRepoPathOfPfile')->with(4)->andReturn($repoPath
563  .'04621571bcbabce75c4dd1c6445b87dec0995734.59cacdfce5051cd8a1d8a1f2dcce40a5.12320');
564  $this->treeDao->shouldReceive('getRepoPathOfPfile')->with(351)->andReturn($repoPath
565  .'c518ce1658140b65fa0132ad1130cb91512416bf.8e913e594d24ff3aeabe350107d97815.35829');
566 
567  list($clearingLicense1, $clearingLicense2, $addedEventIds) = $this->insertDecisionFromTwoEvents(
568  DecisionScopes::REPO,$originallyClearedItemId);
569  $clearingLicenses = array($clearingLicense1, $clearingLicense2);
570 
571  list($success,$output,$retCode) = $runner->run($uploadId, $this->userId, $this->groupId);
572 
573  $this->assertTrue($success, 'cannot run runner');
574  $this->assertEquals($retCode, 0, 'reuser failed: '.$output);
575 
576  $newUploadClearings = $this->getFilteredClearings($uploadId, $this->groupId);
577  $potentiallyReusableClearings = $this->getFilteredClearings($reusedUpload, $this->groupId);
578 
579  assertThat($newUploadClearings, is(arrayWithSize(1)));
580 
581  assertThat($potentiallyReusableClearings, is(arrayWithSize(1)));
583  $potentiallyReusableClearing = $potentiallyReusableClearings[0];
585  $newClearing = $newUploadClearings[0];
586 
587  /* they are actually the same ClearingDecision
588  * only sameFolder and sameUpload are different */
589  assertThat($newClearing, not(equalTo($potentiallyReusableClearing)));
590 
591  assertThat($newClearing->getClearingLicenses(), arrayContainingInAnyOrder($clearingLicenses));
592 
593  assertThat($newClearing->getType(), equalTo($potentiallyReusableClearing->getType()));
594  assertThat($newClearing->getScope(), equalTo($potentiallyReusableClearing->getScope()));
595 
596  assertThat($newClearing->getUploadTreeId(),
597  equalTo($potentiallyReusableClearing->getUploadTreeId() + $reusingUploadItemShift));
598 
599  /* reuser should have not created a correct local event history */
600  $bounds = $this->uploadDao->getItemTreeBounds($originallyClearedItemId + $reusingUploadItemShift);
601  $newEvents = $this->clearingDao->getRelevantClearingEvents($bounds, $this->groupId);
602 
603  assertThat($newEvents, is(arrayWithSize(count($clearingLicenses))));
604 
606  foreach ($newEvents as $newEvent) {
607  assertThat($newEvent->getEventId(), anyOf($addedEventIds));
608  assertThat($newEvent->getClearingLicense(), anyOf($clearingLicenses));
609  }
610  /*reuse main license*/
611  $this->clearingDao->makeMainLicense($uploadId=2, $this->groupId, $mainLicenseId=402);
612  $mainLicenseIdForReuse = $this->clearingDao->getMainLicenseIds($reusedUploadId=2, $this->groupId);
613  $mainLicenseIdForReuseSingle = array_values($mainLicenseIdForReuse);
614  $this->clearingDao->makeMainLicense($uploadId=3, $this->groupId, $mainLicenseIdForReuseSingle[0]);
615  $mainLicense=$this->clearingDao->getMainLicenseIds($uploadId=3, $this->groupId);
616  $mainLicenseSingle = array_values($mainLicense);
617  $this->assertEquals($mainLicenseIdForReuseSingle, $mainLicenseSingle);
618  $this->rmRepo();
619  }
620 }
Various utility functions to filter ClearingDecision.
Tests for Reuser agent and scheduler interaction.
insertDecisionFromTwoEvents($scope=DecisionScopes::ITEM, $originallyClearedItemId=23)
Creates two clearing decisions.
testReuserMockedScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
testReuserRealScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
testReuserRealScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
testReuserRealScanWithARepoClearing()
Call runnerReuserScanWithARepoClearing()
getFilteredClearings($uploadId, $groupId)
Get clearings for a given upload id.
testReuserMockedScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
testReuserMockedScanWithoutAnyUploadToCopyAndAClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndAClearing()
getHeartCount($output)
Get the heart count from agent.
runnerReuserScanWithoutAnyUploadToCopyAndNoClearing(SchedulerTestRunner $runner)
Test on an upload with no clearing decisions.
setUpTables()
Setup tables required by the agent.
testReuserRealScanWithoutAnyUploadToCopyAndNoClearing()
Call runnerReuserScanWithoutAnyUploadToCopyAndNoClearing()
testReuserMockedScanWithALocalClearing()
Call runnerReuserScanWithALocalClearing()
runnerReuserScanWithoutAnyUploadToCopyAndAClearing($runner)
Run reuser agent with no upload to copy decisions from.
testReuserRealScanWithARepoClearingEnhanced()
Call runnerReuserScanWithARepoClearingEnhanced()
run($uploadId, $userId=2, $groupId=2, $jobId=1, $args="")
Function to run agent from scheduler.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace to hold test cases for Reuser agent.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308