FOSSology  4.5.1
Open Source License Compliance by Open Source Software
ShowJobsDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4  Author: Johannes Najjar, anupam.ghosh@siemens.com, Shaheem Azmal
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 namespace Fossology\Lib\Dao;
9 
13 use Mockery as M;
14 
15 class ShowJobsDaoTest extends \PHPUnit\Framework\TestCase
16 {
18  private $testDb;
20  private $dbManager;
22  private $uploadDao;
24  private $showJobsDao;
26  private $uploadPermissionDao;
27 
28  private $job_pks = array(2,1);
29 
30  protected function setUp() : void
31  {
32  $this->testDb = new TestPgDb();
33  $this->dbManager = &$this->testDb->getDbManager();
34 
35  $this->testDb->createPlainTables(
36  array(
37  'upload',
38  'uploadtree',
39  'job',
40  'perm_upload',
41  'jobqueue',
42  'jobdepends',
43  ));
44  $this->testDb->createInheritedTables(array('uploadtree_a'));
45 
46  $uploadArray = array(array('upload_pk'=>1, 'uploadtree_tablename'=>'uploadtree'),
47  array('upload_pk'=>2, 'uploadtree_tablename'=>'uploadtree_a'));
48  foreach ($uploadArray as $uploadEntry) {
49  $this->dbManager->insertTableRow('upload', $uploadEntry);
50  }
51 
52  $this->dbManager->prepare($stmt = 'insert.job',
53  "INSERT INTO job (job_pk, job_queued, job_name, job_upload_fk, job_user_fk) VALUES ($1, $2, $3, $4, $5)");
54  $jobArray = array(array(1,date('c',time()-5), "FCKeditor_2.6.4.zip", 1,1 ),
55  array(2,date('c'), "zlib_1.2.8.zip", 2,2));
56  foreach ($jobArray as $uploadEntry) {
57  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
58  }
59 
60  $logger = M::mock('Monolog\Logger');
61  $logger->shouldReceive('debug');
62  $this->uploadPermissionDao = M::mock('Fossology\Lib\Dao\UploadPermissionDao');
63  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermissionDao);
64  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
65 
66  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
67  }
68 
69  protected function tearDown() : void
70  {
71  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
72  $this->testDb = null;
73  $this->dbManager = null;
74  }
75 
76 
77 
78  public function testUploads2Jobs()
79  {
80  $groupId = 2;
81  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
82  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
83  $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId))
84  ->andReturnUsing(function($upload,$group)
85  {
86  return ($upload==1 || $upload==2 || $upload==3 || $upload==4 || $upload==5);
87  });
88 
89  $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
90  foreach ($jobs as $jobId => $jobUpload) {
91  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
92  }
93  $jobsWithoutUpload = $this->showJobsDao->uploads2Jobs(array());
94  assertThat($jobsWithoutUpload, is(emptyArray()));
95  $jobsWithUploadIdOne = $this->showJobsDao->uploads2Jobs(array(1));
96  assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0)));
97  $jobsAtAll = $this->showJobsDao->uploads2Jobs(array(1,2,3,4,5));
98  assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0)));
99  $jobsWithUploadFour = $this->showJobsDao->uploads2Jobs(array(4));
100  assertThat($jobsWithUploadFour[0], is(emptyArray()));
101  }
102 
103  public function testUploads2JobsPaged()
104  {
105  $groupId = 2;
106  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
107  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
108 
109  $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId))
110  ->andReturnUsing(function($upload,$group)
111  {
112  return range(1, 17);
113  });
114 
115  $jobs = array_combine(range(3,13),range(3,13));
116  foreach ($jobs as $jobId => $jobUpload) {
117  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
118  }
119 
120  $jobsPage1 = $this->showJobsDao->uploads2Jobs(range(1,17),0);
121  assertThat($jobsPage1[0], arrayWithSize(10));
122  assertThat($jobsPage1[1], is(1));
123  $jobsPage2 = $this->showJobsDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
124  assertThat($jobsPage2[0], arrayWithSize(3));
125  assertThat($jobsPage2[1], is(0));
126  $jobsPage3 = $this->showJobsDao->uploads2Jobs(array(),2);
127  assertThat($jobsPage3, arrayWithSize(0));
128  }
129 
130 
131  public function testgetJobName()
132  {
133  $testJobName = $this->showJobsDao->getJobName(1);
134  assertThat($testJobName, equalTo("FCKeditor_2.6.4.zip"));
135 
136  $testJobNameIfNothingQueued = $this->showJobsDao->getJobName($uploadId = 3);
137  assertThat($testJobNameIfNothingQueued, equalTo($uploadId));
138  }
139 
140  public function testMyJobs()
141  {
142  $groupId = 2;
143  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
144  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
145 
146  $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId))
147  ->andReturnUsing(function($upload,$group)
148  {
149  return ($upload==1 || $upload==2 || $upload==4);
150  });
151  $testOurJobs = $this->showJobsDao->myJobs(true);
152  assertThat($testOurJobs[0], is(arrayContainingInAnyOrder($this->job_pks)));
153  $testMyJobs = $this->showJobsDao->myJobs(false);
154  assertThat($testMyJobs, equalTo(array(array(1), 0)));
155 
156  $this->dbManager->queryOnce("UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1");
157  $this->dbManager->prepare(__METHOD__.'insert.perm_upload',
158  "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES ($1, $2, $3, $4)");
159  $testOutdatedJobs = $this->showJobsDao->myJobs(true);
160  assertThat($testOutdatedJobs, equalTo(array(array(2), 0)));
161  }
162 
163  public function testgetNumItemsPerSec()
164  {
165  $numSecs = 30;
166  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(5*$numSecs, $numSecs);
167  assertThat($testFilesPerSec,is(greaterThan(1)));
168 
169  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(0.9*$numSecs, $numSecs);
170  assertThat($testFilesPerSec,is(lessThanOrEqualTo(1)));
171  }
172 
173  public function testGetJobInfo()
174  {
175  $this->dbManager->prepare($stmt = 'insert.jobqueue',
176  "INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed)"
177  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
178 
179  $nowTime = time();
180  $diffTime = 2345;
181  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
182  $uploadArrayQue = array(array(8, $jobId=1, "nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
183  array(1, $jobId, "ununpack", 1, "2015-04-21 18:29:19.23825+05:30", "2015-04-21 18:29:26.396562+05:30", "Completed",1,null,$itemCount=646 ));
184  foreach ($uploadArrayQue as $uploadEntry) {
185  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
186  }
187 
188  $this->dbManager->prepare($stmt = 'insert.uploadtree_a',
189  "INSERT INTO uploadtree_a (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
190  . "VALUES ($1, $2, $3, $4,$5, $6, $7, $8)");
191  $uploadTreeArray = array(array(123, 121, 1, 103, 32768, 542, 543, "fckeditorcode_ie.js"),
192  array(121,120, 1, 0, 536888320, 537, 544, "js"),
193  array(715,651, 2,607 ,33188 ,534 ,535 ,"zconf.h.cmakein"),
194  array(915, 651, 2, 606 ,33188 ,532 ,533 ,"zconf.h"),
195  );
196  foreach ($uploadTreeArray as $uploadEntry) {
197  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
198  }
199 
200  $this->dbManager->prepare($stmt = 'insert.jobdepends',
201  "INSERT INTO jobdepends (jdep_jq_fk, jdep_jq_depends_fk) VALUES ($1, $2 )");
202  $jqWithTwoDependencies = 8;
203  $jobDependsArray = array(array(2,1),
204  array(3,2),
205  array(4,2),
206  array(5,2),
207  array(6,2),
208  array($jqWithTwoDependencies,4),
209  array($jqWithTwoDependencies,4),
210  );
211  foreach ($jobDependsArray as $uploadEntry) {
212  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
213  }
214 
215  $testMyJobInfo = $this->showJobsDao->getJobInfo($this->job_pks);
216  assertThat($testMyJobInfo,hasKey($jobId));
217  assertThat($testMyJobInfo[$jobId]['jobqueue'][$jqWithTwoDependencies]['depends'], is(arrayWithSize(2)));
218 
219  $testFilesPerSec = 0.23;
220  $formattedEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $testFilesPerSec);
221  assertThat($formattedEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
222  $hourMinSec = explode(':', $formattedEstimatedTime);
223  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
224  is(closeTo(($itemCount-$itemNomos)/$testFilesPerSec,0.5+$testFilesPerSec)));
225 
226  $testGetEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type, 0);
227  assertThat($testGetEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
228  $hourMinSec = explode(':', $testGetEstimatedTime);
229  $tolerance = 0.5+($itemCount-$itemNomos)/$itemNomos+(time()-$nowTime);
230  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
231  is(closeTo(($itemCount-$itemNomos)/$itemNomos*$diffTime,$tolerance)));
232 
233  $fewFilesPerSec = 0.003;
234  $formattedLongTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $fewFilesPerSec);
235  assertThat($formattedLongTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
236  $hourMinSec = explode(':', $formattedLongTime);
237  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
238  is(closeTo(($itemCount-$itemNomos)/$fewFilesPerSec,0.5+$fewFilesPerSec)));
239  }
240 
241  public function testGetEstimatedTimeShouldNotDivideByZero()
242  {
243  $this->dbManager->prepare($stmt = 'insert.jobqueue',
244  "INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed)"
245  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
246 
247  $nowTime = time();
248  $diffTime = 2345;
249  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
250  $uploadArrayQue = array(array(8, $jobId=1, $jqType="nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
251  array(1, $jobId, "ununpack", 1, "2015-04-21 18:29:19.23825+05:30", "2015-04-21 18:29:26.396562+05:30", "Completed",1,null,$itemCount=646 ));
252  foreach ($uploadArrayQue as $uploadEntry) {
253  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
254  }
255 
256  $showJobsDaoMock = M::mock('Fossology\\Lib\\Dao\\ShowJobsDao[getNumItemsPerSec]',array($this->dbManager, $this->uploadDao));
257  $showJobsDaoMock->shouldReceive('getNumItemsPerSec')->andReturn(0);
258 
259  $estimated = $showJobsDaoMock->getEstimatedTime($jobId, $jqType);
260  assertThat($estimated, equalTo('0:00:00'));
261  }
262  private function insertJobQue($jobQueueEndBits = 2)
263  {
264  $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
265  foreach ($jobs as $jobId => $jobUpload) {
266  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
267  }
268  $this->dbManager->prepare($stmt = 'insert.jobqueue',
269  "INSERT INTO jobqueue (jq_pk, jq_job_fk, jq_type, jq_args, jq_starttime, jq_endtime, jq_endtext, jq_end_bits, jq_schedinfo, jq_itemsprocessed)"
270  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
271 
272  $nowTime = time();
273  $diffTime = 2345;
274  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
275  $uploadArrayQue = array(array(8, $jobId=1, $jqType="nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
276  array(1, $jobId, "ununpack", 1, "2015-04-21 18:29:19.23825+05:30", "2015-04-21 18:29:26.396562+05:30", "Completed",$jobQueueEndBits,null,$itemCount=646 ));
277  foreach ($uploadArrayQue as $uploadEntry) {
278  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
279  }
280  }
281  public function testGetJobsForAll()
282  {
283  $this->insertJobQue();
284  $result = $this->showJobsDao->getJobsForAll();
285  $this->assertNotNull($result);
286  $this->assertEquals(array(array('job' => 'nomos', 'jq_job_fk' => "1","upload_fk" => "1", "status" => "running")),$result);
287  }
288  public function testGetItemsProcessedForDecider()
289  {
290  $this->insertJobQue();
291  $result = $this->showJobsDao->getItemsProcessedForDecider("nomos",1);
292  $expectedArray = array("147","1");
293  $this->assertNotNull($result);
294  $this->assertEquals($expectedArray,$result);
295  }
296  public function testGetJobStatusFalse()
297  {
298  $this->insertJobQue();
299  $jobStatus = $this->showJobsDao->getJobStatus(1);
300  $this->assertNotNull($jobStatus);
301  $this->assertFalse($jobStatus);
302  }
303  public function testGetJobStatusTrue()
304  {
305  $this->insertJobQue(4);
306  $jobStatus = $this->showJobsDao->getJobStatus(1);
307  $this->assertNotNull($jobStatus);
308  $this->assertTrue($jobStatus);
309  }
310  public function testGetDataASingleJob()
311  {
312  $this->insertJobQue();
313  $result = $this->showJobsDao->getDataForASingleJob(1);
314  $this->assertNotNull($result);
315  $expectedData = array(
316  'jq_pk' => "1",
317  'jq_job_fk' => "1",
318  'jq_type' => "ununpack",
319  'jq_args' => "1",
320  'jq_starttime' => $result['jq_starttime'],
321  'jq_endtime' => $result['jq_endtime'],
322  'jq_endtext' => "Completed",
323  'jq_end_bits' => "2",
324  'jq_schedinfo' => NULL,
325  'jq_itemsprocessed' => "646",
326  'jq_log' => NULL,
327  'jq_runonpfile' => NULL,
328  'jq_host' => NULL,
329  'jq_cmd_args' => NULL,
330  'job_pk' => "1",
331  'job_queued' => $result['job_queued'],
332  'job_priority' => NULL,
333  'job_email_notify' => NULL,
334  'job_name' => "FCKeditor_2.6.4.zip",
335  'job_upload_fk' => "1",
336  'job_folder_fk' => NULL,
337  'job_user_fk' => "1",
338  'job_group_fk' => NULL,
339  'elapsed' => "00:00:07.158312"
340  );
341  $this->assertEquals($expectedData,$result);
342  }
343 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16