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 */
14 
30 use Mockery as M;
31 
32 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
33 include_once(__DIR__.'/../../../lib/php/Plugin/FO_Plugin.php');
34 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
35 include_once(__DIR__.'/SchedulerTestRunnerMock.php');
36 
41 class SchedulerTest extends \PHPUnit\Framework\TestCase
42 {
44  private $testDb;
46  private $dbManager;
48  private $testInstaller;
49 
51  private $licenseDao;
53  private $clearingDao;
55  private $clearingDecisionProcessor;
57  private $agentLicenseEventProcessor;
59  private $uploadDao;
61  private $uploadPermDao;
63  private $highlightDao;
64 
66  private $runnerCli;
68  private $runnerMock;
69 
73  protected function setUp() : void
74  {
75  $this->testDb = new TestPgDb("deciderJobSched".time());
76  $this->dbManager = $this->testDb->getDbManager();
77  $logger = M::mock('Monolog\Logger');
78  $this->testDb->setupSysconfig();
79 
80  $this->licenseDao = new LicenseDao($this->dbManager);
81  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
82  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
83  $this->highlightDao = new HighlightDao($this->dbManager);
84  $agentDao = new AgentDao($this->dbManager, $logger);
85  $this->agentLicenseEventProcessor = new AgentLicenseEventProcessor($this->licenseDao, $agentDao);
86  $clearingEventProcessor = new ClearingEventProcessor();
87  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
88  $this->clearingDecisionProcessor = new ClearingDecisionProcessor($this->clearingDao, $this->agentLicenseEventProcessor, $clearingEventProcessor, $this->dbManager);
89 
90  $this->runnerMock = new SchedulerTestRunnerMock($this->dbManager, $agentDao, $this->clearingDao, $this->uploadDao, $this->highlightDao, $this->clearingDecisionProcessor, $this->agentLicenseEventProcessor);
91  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
92  }
93 
97  protected function tearDown() : void
98  {
99  $this->testDb->fullDestruct();
100  $this->testDb = null;
101  $this->dbManager = null;
102  $this->licenseDao = null;
103  $this->highlightDao = null;
104  $this->clearingDao = null;
105  }
106 
110  private function setUpRepo()
111  {
112  $sysConf = $this->testDb->getFossSysConf();
113  $this->testInstaller = new TestInstaller($sysConf);
114  $this->testInstaller->init();
115  $this->testInstaller->cpRepo();
116  }
117 
121  private function rmRepo()
122  {
123  $this->testInstaller->rmRepo();
124  $this->testInstaller->clear();
125  }
126 
130  private function setUpTables()
131  {
132  $this->testDb->createPlainTables(array('upload','upload_reuse','uploadtree',
133  'uploadtree_a','license_ref','license_ref_bulk','clearing_decision',
134  'clearing_decision_event','clearing_event','license_file','highlight',
135  'highlight_bulk','agent','pfile','ars_master','users','group_user_member',
136  'license_map','report_info'),false);
137  $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','FileLicense_pkey'),false);
138  $this->testDb->createViews(array('license_file_ref'),false);
139  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','clearing_event_pkey'),false);
140  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_ref','clearing_event','clearing_decision','license_file','highlight'),false);
141  $this->testDb->createInheritedTables();
142  $this->testDb->createInheritedArsTables(array('nomos','monk'));
143 
144  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users','group_user_member','agent','license_file','nomos_ars','monk_ars'), false);
145  $this->testDb->insertData_license_ref();
146 
147  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
148  }
149 
155  private function getHeartCount($output)
156  {
157  $matches = array();
158  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
159  return intval($matches[1]);
160  } else {
161  return -1;
162  }
163  }
164 
165 
175  {
176  $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerMock);
177  }
178 
188  {
189  $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerCli);
190  }
191 
195  private function runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($runner)
196  {
197  $this->setUpTables();
198  $this->setUpRepo();
199 
200  $jobId = 42;
201 
202  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
203  $licenseRef2 = $this->licenseDao->getLicenseByShortName("Glide")->getRef();
204 
205  $addedLicenses = array($licenseRef1, $licenseRef2);
206 
207  assertThat($addedLicenses, not(arrayContaining(null)));
208 
209  $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId=23, $userId=2, $groupId=3, $licenseRef1->getId(), false);
210  $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $groupId, $licenseRef2->getId(), true);
211 
212  $this->dbManager->queryOnce("UPDATE clearing_event SET job_fk=$jobId");
213 
214  $addedEventIds = array($eventId1, $eventId2);
215 
216  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args="");
217 
218  $this->assertTrue($success, 'cannot run runner');
219  $this->assertEquals($retCode, 0, 'decider failed (did you make test?): '.$output);
220 
221  assertThat($this->getHeartCount($output), equalTo(1));
222 
223  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
224  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
225  assertThat($decisions, is(arrayWithSize(1)));
226 
228  $deciderMadeDecision = $decisions[0];
229 
230  foreach ($deciderMadeDecision->getClearingEvents() as $event) {
231  assertThat($event->getEventId(), is(anyOf($addedEventIds)));
232  }
233 
234  $this->rmRepo();
235  }
236 
245  {
247  }
248 
257  {
259  }
260 
265  {
266  $this->setUpTables();
267  $this->setUpRepo();
268 
269  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
270 
271  $licId1 = $licenseRef1->getId();
272 
273  $agentNomosId = 6;
274  $pfile = 4;
275 
276  $this->dbManager->queryOnce("DELETE FROM license_file");
277  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$licId1,$pfile,$agentNomosId)");
278  $this->dbManager->queryOnce("INSERT INTO highlight (fl_fk,start,len) VALUES(12222,12,3)");
279  $this->dbManager->queryOnce("INSERT INTO highlight (fl_fk,start,len) VALUES(12222,18,3)");
280 
281  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId=6, $groupId=4, $jobId=31, $args="");
282 
283  $this->assertTrue($success, 'cannot run runner');
284  $this->assertEquals($retCode, 0, 'decider failed (did you make test?): '.$output);
285 
286  assertThat($this->getHeartCount($output), equalTo(0));
287 
288  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
289  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
290  assertThat($decisions, is(arrayWithSize(0)));
291 
292  $this->rmRepo();
293  }
294 
301  {
302  $this->setUpTables();
303  $this->setUpRepo();
304 
305  $dbManager = M::mock(DbManager::class);
306  $agentDao = M::mock(AgentDao::class);
307  $clearingDao = M::mock(ClearingDao::class);
308  $uploadDao = M::mock(UploadDao::class);
309  $highlightDao = M::mock(HighlightDao::class);
310  $decisionProcessor = M::mock(ClearingDecisionProcessor::class);
311  $agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
312 
313  $uploadId = 13243;
314 
315  /*mock for Agent class **/
316  $agentDao->shouldReceive('arsTableExists')->andReturn(true);
317  $agentDao->shouldReceive('getCurrentAgentId')->andReturn($agentId=24);
318  $agentDao->shouldReceive('writeArsRecord')->with(anything(), $agentId, $uploadId)->andReturn($arsId=2);
319  $agentDao->shouldReceive('writeArsRecord')->with(anything(), $agentId, $uploadId, $arsId, true)->andReturn(0);
320 
321  $jobId = 42;
322  $groupId = 6;
323  $userId = 2;
324 
325  $itemIds = array(4343, 43);
326 
327  $bounds0 = M::mock(ItemTreeBounds::class);
328  $bounds0->shouldReceive('getItemId')->andReturn($itemIds[0]);
329  $bounds0->shouldReceive('containsFiles')->andReturn(false);
330  $bounds1 = M::mock(ItemTreeBounds::class);
331  $bounds1->shouldReceive('getItemId')->andReturn($itemIds[1]);
332  $bounds1->shouldReceive('containsFiles')->andReturn(false);
333  $bounds = array($bounds0, $bounds1);
334 
335  $uploadDao->shouldReceive('getItemTreeBounds')->with($itemIds[0])->andReturn($bounds[0]);
336  $uploadDao->shouldReceive('getItemTreeBounds')->with($itemIds[1])->andReturn($bounds[1]);
337 
338  $clearingDao->shouldReceive('getEventIdsOfJob')->with($jobId)
339  ->andReturn(array($itemIds[0] => array(), $itemIds[1] => array()));
340 
341  $dbManager->shouldReceive('begin')->times(count($itemIds));
342  $dbManager->shouldReceive('commit')->times(count($itemIds));
343 
344  /* dummy expectations needed for unmockable LicenseMap constructor */
345  $dbManager->shouldReceive('prepare');
346  $res = M::Mock(DbManager::class);
347  $dbManager->shouldReceive('execute')->andReturn($res);
348  $row1 = array('rf_fk' => 2334, 'parent_fk' => 1);
349  $row2 = array('rf_fk' => 2333, 'parent_fk' => 1);
350  $dbManager->shouldReceive('fetchArray')->with($res)->andReturn($row1, $row2, false);
351  $dbManager->shouldReceive('freeResult')->with($res);
352  /* /expectations for LicenseMap */
353 
354  $decisionProcessor->shouldReceive('hasUnhandledScannerDetectedLicenses')
355  ->with($bounds0, $groupId, array(), anything())->andReturn(true);
356  $clearingDao->shouldReceive('markDecisionAsWip')
357  ->with($itemIds[0], $userId, $groupId);
358 
359  $decisionProcessor->shouldReceive('hasUnhandledScannerDetectedLicenses')
360  ->with($bounds1, $groupId, array(), anything())->andReturn(false);
361  $decisionProcessor->shouldReceive('makeDecisionFromLastEvents')
362  ->with($bounds1, $userId, $groupId, DecisionTypes::IDENTIFIED, false, array());
363 
364  $runner = new SchedulerTestRunnerMock($dbManager, $agentDao, $clearingDao, $uploadDao, $highlightDao, $decisionProcessor, $agentLicenseEventProcessor);
365 
366  list($success,$output,$retCode) = $runner->run($uploadId, $userId, $groupId, $jobId, $args="");
367 
368  $this->assertTrue($success, 'cannot run decider');
369  $this->assertEquals($retCode, 0, 'decider failed: '.$output);
370  assertThat($this->getHeartCount($output), equalTo(count($itemIds)));
371 
372  $this->rmRepo();
373  }
374 
385  {
386  $this->runnerDeciderScanWithForceDecision($this->runnerMock);
387  }
388 
399  {
400  $this->runnerDeciderScanWithForceDecision($this->runnerCli);
401  }
402 
406  private function runnerDeciderScanWithForceDecision($runner)
407  {
408  $this->setUpTables();
409  $this->setUpRepo();
410 
411  $jobId = 42;
412 
413  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
414  $licenseRef2 = $this->licenseDao->getLicenseByShortName("Glide")->getRef();
415 
416  $agentLicId = $this->licenseDao->getLicenseByShortName("Adaptec")->getRef()->getId();
417 
418  $addedLicenses = array($licenseRef1, $licenseRef2);
419 
420  assertThat($addedLicenses, not(arrayContaining(null)));
421 
422  $agentId = 5;
423  $pfile = 4;
424 
425  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$agentLicId,$pfile,$agentId)");
426 
427  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId=23);
428  assertThat($this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds), is(not(emptyArray())));
429 
430  $eventId1 = $this->clearingDao->insertClearingEvent($itemId, $userId=2, $groupId=3, $licenseRef1->getId(), false);
431  $eventId2 = $this->clearingDao->insertClearingEvent($itemId, 5, $groupId, $licenseRef2->getId(), true);
432 
433  $this->dbManager->queryOnce("UPDATE clearing_event SET job_fk=$jobId");
434 
435  $addedEventIds = array($eventId1, $eventId2);
436 
437  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args="-k1");
438 
439  $this->assertTrue($success, 'cannot run runner');
440  $this->assertEquals($retCode, 0, 'decider failed: '.$output);
441  assertThat($this->getHeartCount($output), equalTo(1));
442 
443  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
444  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
445  assertThat($decisions, is(arrayWithSize(1)));
446 
448  $deciderMadeDecision = $decisions[0];
449 
450  $eventIds = array();
451  foreach ($deciderMadeDecision->getClearingEvents() as $event) {
452  $eventIds[] = $event->getEventId();
453  }
454 
455  assertThat($eventIds, arrayValue($addedEventIds[0]));
456  assertThat($eventIds, arrayValue($addedEventIds[1]));
457  assertThat($eventIds, arrayWithSize(1+count($addedEventIds)));
458 
459  $this->rmRepo();
460  }
461 }
Test interactions between scheduler and agent.
getHeartCount($output)
Get the heart count value from the agent output.
tearDown()
Destroy objects, database and repository.
setUpTables()
Create test tables required by agent.
setUp()
Setup the objects, database and repository.
runnerDeciderScanWithNoEventsAndOnlyNomosShouldNotMakeADecision($runner)
run decider with no events
Utility functions to process ClearingDecision.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace for decider job test cases.