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 */
25 
41 use Mockery as M;
42 
43 include_once(__DIR__.'/../../../lib/php/Test/Agent/AgentTestMockHelper.php');
44 include_once(__DIR__.'/../../../lib/php/Plugin/FO_Plugin.php');
45 include_once(__DIR__.'/SchedulerTestRunnerCli.php');
46 include_once(__DIR__.'/SchedulerTestRunnerMock.php');
47 
52 class SchedulerTest extends \PHPUnit\Framework\TestCase
53 {
55  private $testDb;
57  private $dbManager;
59  private $testInstaller;
60 
62  private $licenseDao;
64  private $clearingDao;
66  private $clearingDecisionProcessor;
68  private $agentLicenseEventProcessor;
70  private $uploadDao;
72  private $uploadPermDao;
74  private $highlightDao;
75 
77  private $runnerCli;
79  private $runnerMock;
80 
84  protected function setUp()
85  {
86  $this->testDb = new TestPgDb("deciderJobSched".time());
87  $this->dbManager = $this->testDb->getDbManager();
88  $logger = M::mock('Monolog\Logger');
89 
90  $this->licenseDao = new LicenseDao($this->dbManager);
91  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
92  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
93  $this->highlightDao = new HighlightDao($this->dbManager);
94  $agentDao = new AgentDao($this->dbManager, $logger);
95  $this->agentLicenseEventProcessor = new AgentLicenseEventProcessor($this->licenseDao, $agentDao);
96  $clearingEventProcessor = new ClearingEventProcessor();
97  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
98  $this->clearingDecisionProcessor = new ClearingDecisionProcessor($this->clearingDao, $this->agentLicenseEventProcessor, $clearingEventProcessor, $this->dbManager);
99 
100  $this->runnerMock = new SchedulerTestRunnerMock($this->dbManager, $agentDao, $this->clearingDao, $this->uploadDao, $this->highlightDao, $this->clearingDecisionProcessor, $this->agentLicenseEventProcessor);
101  $this->runnerCli = new SchedulerTestRunnerCli($this->testDb);
102  }
103 
107  protected function tearDown()
108  {
109  $this->testDb->fullDestruct();
110  $this->testDb = null;
111  $this->dbManager = null;
112  $this->licenseDao = null;
113  $this->highlightDao = null;
114  $this->clearingDao = null;
115  }
116 
120  private function setUpRepo()
121  {
122  $sysConf = $this->testDb->getFossSysConf();
123  $this->testInstaller = new TestInstaller($sysConf);
124  $this->testInstaller->init();
125  $this->testInstaller->cpRepo();
126  }
127 
131  private function rmRepo()
132  {
133  $this->testInstaller->rmRepo();
134  $this->testInstaller->clear();
135  }
136 
140  private function setUpTables()
141  {
142  $this->testDb->createPlainTables(array('upload','upload_reuse','uploadtree','uploadtree_a','license_ref','license_ref_bulk','clearing_decision','clearing_decision_event','clearing_event','license_file','highlight','highlight_bulk','agent','pfile','ars_master','users','group_user_member','license_map'),false);
143  $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);
144  $this->testDb->createViews(array('license_file_ref'),false);
145  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','clearing_event_pkey'),false);
146  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','clearing_event','clearing_decision','license_file','highlight'),false);
147  $this->testDb->createInheritedTables();
148  $this->testDb->createInheritedArsTables(array('nomos','monk'));
149 
150  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users','group_user_member','agent','license_file','nomos_ars','monk_ars'), false);
151  $this->testDb->insertData_license_ref();
152 
153  $this->testDb->resetSequenceAsMaxOf('agent_agent_pk_seq', 'agent', 'agent_pk');
154  }
155 
161  private function getHeartCount($output)
162  {
163  $matches = array();
164  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
165  return intval($matches[1]);
166  } else {
167  return -1;
168  }
169  }
170 
171 
181  {
182  $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerMock);
183  }
184 
194  {
195  $this->runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($this->runnerCli);
196  }
197 
201  private function runnerDeciderScanWithTwoEventAndNoAgentShouldMakeADecision($runner)
202  {
203  $this->setUpTables();
204  $this->setUpRepo();
205 
206  $jobId = 42;
207 
208  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
209  $licenseRef2 = $this->licenseDao->getLicenseByShortName("Glide")->getRef();
210 
211  $addedLicenses = array($licenseRef1, $licenseRef2);
212 
213  assertThat($addedLicenses, not(arrayContaining(null)));
214 
215  $eventId1 = $this->clearingDao->insertClearingEvent($originallyClearedItemId=23, $userId=2, $groupId=3, $licenseRef1->getId(), false);
216  $eventId2 = $this->clearingDao->insertClearingEvent($originallyClearedItemId, 5, $groupId, $licenseRef2->getId(), true);
217 
218  $this->dbManager->queryOnce("UPDATE clearing_event SET job_fk=$jobId");
219 
220  $addedEventIds = array($eventId1, $eventId2);
221 
222  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args="");
223 
224  $this->assertTrue($success, 'cannot run runner');
225  $this->assertEquals($retCode, 0, 'decider failed (did you make test?): '.$output);
226 
227  assertThat($this->getHeartCount($output), equalTo(1));
228 
229  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
230  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
231  assertThat($decisions, is(arrayWithSize(1)));
232 
234  $deciderMadeDecision = $decisions[0];
235 
236  foreach ($deciderMadeDecision->getClearingEvents() as $event) {
237  assertThat($event->getEventId(), is(anyOf($addedEventIds)));
238  }
239 
240  $this->rmRepo();
241  }
242 
251  {
253  }
254 
263  {
265  }
266 
271  {
272  $this->setUpTables();
273  $this->setUpRepo();
274 
275  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
276 
277  $licId1 = $licenseRef1->getId();
278 
279  $agentNomosId = 6;
280  $pfile = 4;
281 
282  $this->dbManager->queryOnce("DELETE FROM license_file");
283  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$licId1,$pfile,$agentNomosId)");
284  $this->dbManager->queryOnce("INSERT INTO highlight (fl_fk,start,len) VALUES(12222,12,3)");
285  $this->dbManager->queryOnce("INSERT INTO highlight (fl_fk,start,len) VALUES(12222,18,3)");
286 
287  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId=6, $groupId=4, $jobId=31, $args="");
288 
289  $this->assertTrue($success, 'cannot run runner');
290  $this->assertEquals($retCode, 0, 'decider failed (did you make test?): '.$output);
291 
292  assertThat($this->getHeartCount($output), equalTo(0));
293 
294  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
295  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
296  assertThat($decisions, is(arrayWithSize(0)));
297 
298  $this->rmRepo();
299  }
300 
307  {
308  $this->setUpTables();
309  $this->setUpRepo();
310 
311  $dbManager = M::mock(DbManager::class);
312  $agentDao = M::mock(AgentDao::class);
313  $clearingDao = M::mock(ClearingDao::class);
314  $uploadDao = M::mock(UploadDao::class);
315  $highlightDao = M::mock(HighlightDao::class);
316  $decisionProcessor = M::mock(ClearingDecisionProcessor::class);
317  $agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
318 
319  $uploadId = 13243;
320 
321  /*mock for Agent class **/
322  $agentDao->shouldReceive('arsTableExists')->andReturn(true);
323  $agentDao->shouldReceive('getCurrentAgentId')->andReturn($agentId=24);
324  $agentDao->shouldReceive('writeArsRecord')->with(anything(), $agentId, $uploadId)->andReturn($arsId=2);
325  $agentDao->shouldReceive('writeArsRecord')->with(anything(), $agentId, $uploadId, $arsId, true)->andReturn(0);
326 
327  $jobId = 42;
328  $groupId = 6;
329  $userId = 2;
330 
331  $itemIds = array(4343, 43);
332 
333  $bounds0 = M::mock(ItemTreeBounds::class);
334  $bounds0->shouldReceive('getItemId')->andReturn($itemIds[0]);
335  $bounds0->shouldReceive('containsFiles')->andReturn(false);
336  $bounds1 = M::mock(ItemTreeBounds::class);
337  $bounds1->shouldReceive('getItemId')->andReturn($itemIds[1]);
338  $bounds1->shouldReceive('containsFiles')->andReturn(false);
339  $bounds = array($bounds0, $bounds1);
340 
341  $uploadDao->shouldReceive('getItemTreeBounds')->with($itemIds[0])->andReturn($bounds[0]);
342  $uploadDao->shouldReceive('getItemTreeBounds')->with($itemIds[1])->andReturn($bounds[1]);
343 
344  $clearingDao->shouldReceive('getEventIdsOfJob')->with($jobId)
345  ->andReturn(array($itemIds[0] => array(), $itemIds[1] => array()));
346 
347  $dbManager->shouldReceive('begin')->times(count($itemIds));
348  $dbManager->shouldReceive('commit')->times(count($itemIds));
349 
350  /* dummy expectations needed for unmockable LicenseMap constructor */
351  $dbManager->shouldReceive('prepare');
352  $res = M::Mock(DbManager::class);
353  $dbManager->shouldReceive('execute')->andReturn($res);
354  $row1 = array('rf_fk' => 2334, 'parent_fk' => 1);
355  $row2 = array('rf_fk' => 2333, 'parent_fk' => 1);
356  $dbManager->shouldReceive('fetchArray')->with($res)->andReturn($row1, $row2, false);
357  $dbManager->shouldReceive('freeResult')->with($res);
358  /* /expectations for LicenseMap */
359 
360  $decisionProcessor->shouldReceive('hasUnhandledScannerDetectedLicenses')
361  ->with($bounds0, $groupId, array(), anything())->andReturn(true);
362  $clearingDao->shouldReceive('markDecisionAsWip')
363  ->with($itemIds[0], $userId, $groupId);
364 
365  $decisionProcessor->shouldReceive('hasUnhandledScannerDetectedLicenses')
366  ->with($bounds1, $groupId, array(), anything())->andReturn(false);
367  $decisionProcessor->shouldReceive('makeDecisionFromLastEvents')
368  ->with($bounds1, $userId, $groupId, DecisionTypes::IDENTIFIED, false, array());
369 
370  $runner = new SchedulerTestRunnerMock($dbManager, $agentDao, $clearingDao, $uploadDao, $highlightDao, $decisionProcessor, $agentLicenseEventProcessor);
371 
372  list($success,$output,$retCode) = $runner->run($uploadId, $userId, $groupId, $jobId, $args="");
373 
374  $this->assertTrue($success, 'cannot run decider');
375  $this->assertEquals($retCode, 0, 'decider failed: '.$output);
376  assertThat($this->getHeartCount($output), equalTo(count($itemIds)));
377 
378  $this->rmRepo();
379  }
380 
391  {
392  $this->runnerDeciderScanWithForceDecision($this->runnerMock);
393  }
394 
405  {
406  $this->runnerDeciderScanWithForceDecision($this->runnerCli);
407  }
408 
412  private function runnerDeciderScanWithForceDecision($runner)
413  {
414  $this->setUpTables();
415  $this->setUpRepo();
416 
417  $jobId = 42;
418 
419  $licenseRef1 = $this->licenseDao->getLicenseByShortName("SPL-1.0")->getRef();
420  $licenseRef2 = $this->licenseDao->getLicenseByShortName("Glide")->getRef();
421 
422  $agentLicId = $this->licenseDao->getLicenseByShortName("Adaptec")->getRef()->getId();
423 
424  $addedLicenses = array($licenseRef1, $licenseRef2);
425 
426  assertThat($addedLicenses, not(arrayContaining(null)));
427 
428  $agentId = 5;
429  $pfile = 4;
430 
431  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk,rf_fk,pfile_fk,agent_fk) VALUES(12222,$agentLicId,$pfile,$agentId)");
432 
433  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId=23);
434  assertThat($this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds), is(not(emptyArray())));
435 
436  $eventId1 = $this->clearingDao->insertClearingEvent($itemId, $userId=2, $groupId=3, $licenseRef1->getId(), false);
437  $eventId2 = $this->clearingDao->insertClearingEvent($itemId, 5, $groupId, $licenseRef2->getId(), true);
438 
439  $this->dbManager->queryOnce("UPDATE clearing_event SET job_fk=$jobId");
440 
441  $addedEventIds = array($eventId1, $eventId2);
442 
443  list($success,$output,$retCode) = $runner->run($uploadId=2, $userId, $groupId, $jobId, $args="-k1");
444 
445  $this->assertTrue($success, 'cannot run runner');
446  $this->assertEquals($retCode, 0, 'decider failed: '.$output);
447  assertThat($this->getHeartCount($output), equalTo(1));
448 
449  $uploadBounds = $this->uploadDao->getParentItemBounds($uploadId);
450  $decisions = $this->clearingDao->getFileClearingsFolder($uploadBounds, $groupId);
451  assertThat($decisions, is(arrayWithSize(1)));
452 
454  $deciderMadeDecision = $decisions[0];
455 
456  $eventIds = array();
457  foreach ($deciderMadeDecision->getClearingEvents() as $event) {
458  $eventIds[] = $event->getEventId();
459  }
460 
461  assertThat($eventIds, arrayValue($addedEventIds[0]));
462  assertThat($eventIds, arrayValue($addedEventIds[1]));
463  assertThat($eventIds, arrayWithSize(1+count($addedEventIds)));
464 
465  $this->rmRepo();
466  }
467 }
Namespace for decider job test cases.
Utility functions to process ClearingDecision.
rmRepo()
Destroy test repository.
getHeartCount($output)
Get the heart count value from the agent output.
tearDown()
Destroy objects, database and repository.
setUp()
Setup the objects, database and repository.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
Test interactions between scheduler and agent.
setUpTables()
Create test tables required by agent.
runnerDeciderScanWithNoEventsAndOnlyNomosShouldNotMakeADecision($runner)
run decider with no events