FOSSology  4.7.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('filterAccessibleUploads')
84  ->andReturnUsing(function($uploadIds,$group)
85  {
86  return array_values(array_filter($uploadIds, function($upload) {
87  return ($upload==1 || $upload==2 || $upload==3 || $upload==4 || $upload==5);
88  }));
89  });
90 
91  $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
92  foreach ($jobs as $jobId => $jobUpload) {
93  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
94  }
95  $jobsWithoutUpload = $this->showJobsDao->uploads2Jobs(array());
96  assertThat($jobsWithoutUpload, is(emptyArray()));
97  $jobsWithUploadIdOne = $this->showJobsDao->uploads2Jobs(array(1));
98  assertThat($jobsWithUploadIdOne, equalTo(array(array(7,1),0)));
99  $jobsAtAll = $this->showJobsDao->uploads2Jobs(array(1,2,3,4,5));
100  assertThat($jobsAtAll, equalTo(array(array(8,7,6,5,4,3,2,1),0)));
101  $jobsWithUploadFour = $this->showJobsDao->uploads2Jobs(array(4));
102  assertThat($jobsWithUploadFour[0], is(emptyArray()));
103  }
104 
105  public function testUploads2JobsPaged()
106  {
107  $groupId = 2;
108  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
109  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
110 
111  $this->uploadPermissionDao->shouldReceive('filterAccessibleUploads')
112  ->andReturnUsing(function($uploadIds,$group)
113  {
114  return array_values(array_filter($uploadIds, function($upload) {
115  return in_array($upload, range(1, 17));
116  }));
117  });
118 
119  $jobs = array_combine(range(3,13),range(3,13));
120  foreach ($jobs as $jobId => $jobUpload) {
121  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
122  }
123 
124  $jobsPage1 = $this->showJobsDao->uploads2Jobs(range(1,17),0);
125  assertThat($jobsPage1[0], arrayWithSize(10));
126  assertThat($jobsPage1[1], is(1));
127  $jobsPage2 = $this->showJobsDao->uploads2Jobs(range(1,17),1);
128  assertThat($jobsPage2[0], arrayWithSize(3));
129  assertThat($jobsPage2[1], is(1));
130  $jobsPage3 = $this->showJobsDao->uploads2Jobs(array(),2);
131  assertThat($jobsPage3, arrayWithSize(0));
132  }
133 
134 
135  public function testgetJobName()
136  {
137  $testJobName = $this->showJobsDao->getJobName(1);
138  assertThat($testJobName, equalTo("FCKeditor_2.6.4.zip"));
139 
140  $testJobNameIfNothingQueued = $this->showJobsDao->getJobName($uploadId = 3);
141  assertThat($testJobNameIfNothingQueued, equalTo($uploadId));
142  }
143 
144  public function testMyJobs()
145  {
146  $groupId = 2;
147  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
148  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
149 
150  $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId))
151  ->andReturnUsing(function($upload,$group)
152  {
153  return ($upload==1 || $upload==2 || $upload==4);
154  });
155  $this->uploadPermissionDao->shouldReceive('filterAccessibleUploads')
156  ->andReturnUsing(function($uploadIds,$group)
157  {
158  return array_values(array_filter($uploadIds, function($upload) {
159  return ($upload==1 || $upload==2 || $upload==4);
160  }));
161  });
162  $testOurJobs = $this->showJobsDao->myJobs(true);
163  assertThat($testOurJobs[0], is(arrayContainingInAnyOrder($this->job_pks)));
164  $testMyJobs = $this->showJobsDao->myJobs(false);
165  assertThat($testMyJobs, equalTo(array(array(1), 0)));
166 
167  $this->dbManager->queryOnce("UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1");
168  $this->dbManager->prepare(__METHOD__.'insert.perm_upload',
169  "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES ($1, $2, $3, $4)");
170  $testOutdatedJobs = $this->showJobsDao->myJobs(true);
171  assertThat($testOutdatedJobs, equalTo(array(array(2), 0)));
172  }
173 
174  public function testgetNumItemsPerSec()
175  {
176  $numSecs = 30;
177  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(5*$numSecs, $numSecs);
178  assertThat($testFilesPerSec,is(greaterThan(1)));
179 
180  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(0.9*$numSecs, $numSecs);
181  assertThat($testFilesPerSec,is(lessThanOrEqualTo(1)));
182  }
183 
184  public function testGetJobInfo()
185  {
186  $this->dbManager->prepare($stmt = 'insert.jobqueue',
187  "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)"
188  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
189 
190  $nowTime = time();
191  $diffTime = 2345;
192  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
193  $uploadArrayQue = array(array(8, $jobId=1, "nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
194  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 ));
195  foreach ($uploadArrayQue as $uploadEntry) {
196  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
197  }
198 
199  $this->dbManager->prepare($stmt = 'insert.uploadtree_a',
200  "INSERT INTO uploadtree_a (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
201  . "VALUES ($1, $2, $3, $4,$5, $6, $7, $8)");
202  $uploadTreeArray = array(array(123, 121, 1, 103, 32768, 542, 543, "fckeditorcode_ie.js"),
203  array(121,120, 1, 0, 536888320, 537, 544, "js"),
204  array(715,651, 2,607 ,33188 ,534 ,535 ,"zconf.h.cmakein"),
205  array(915, 651, 2, 606 ,33188 ,532 ,533 ,"zconf.h"),
206  );
207  foreach ($uploadTreeArray as $uploadEntry) {
208  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
209  }
210 
211  $this->dbManager->prepare($stmt = 'insert.jobdepends',
212  "INSERT INTO jobdepends (jdep_jq_fk, jdep_jq_depends_fk) VALUES ($1, $2 )");
213  $jqWithTwoDependencies = 8;
214  $jobDependsArray = array(array(2,1),
215  array(3,2),
216  array(4,2),
217  array(5,2),
218  array(6,2),
219  array($jqWithTwoDependencies,4),
220  array($jqWithTwoDependencies,4),
221  );
222  foreach ($jobDependsArray as $uploadEntry) {
223  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
224  }
225 
226  $testMyJobInfo = $this->showJobsDao->getJobInfo($this->job_pks);
227  assertThat($testMyJobInfo,hasKey($jobId));
228  assertThat($testMyJobInfo[$jobId]['jobqueue'][$jqWithTwoDependencies]['depends'], is(arrayWithSize(2)));
229 
230  $testFilesPerSec = 0.23;
231  $formattedEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $testFilesPerSec);
232  assertThat($formattedEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
233  $hourMinSec = explode(':', $formattedEstimatedTime);
234  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
235  is(closeTo(($itemCount-$itemNomos)/$testFilesPerSec,0.5+$testFilesPerSec)));
236 
237  $testGetEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type, 0);
238  assertThat($testGetEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
239  $hourMinSec = explode(':', $testGetEstimatedTime);
240  $tolerance = 0.5+($itemCount-$itemNomos)/$itemNomos+(time()-$nowTime);
241  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
242  is(closeTo(($itemCount-$itemNomos)/$itemNomos*$diffTime,$tolerance)));
243 
244  $fewFilesPerSec = 0.003;
245  $formattedLongTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $fewFilesPerSec);
246  assertThat($formattedLongTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
247  $hourMinSec = explode(':', $formattedLongTime);
248  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
249  is(closeTo(($itemCount-$itemNomos)/$fewFilesPerSec,0.5+$fewFilesPerSec)));
250  }
251 
252  public function testGetEstimatedTimeShouldNotDivideByZero()
253  {
254  $this->dbManager->prepare($stmt = 'insert.jobqueue',
255  "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)"
256  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
257 
258  $nowTime = time();
259  $diffTime = 2345;
260  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
261  $uploadArrayQue = array(array(8, $jobId=1, $jqType="nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
262  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 ));
263  foreach ($uploadArrayQue as $uploadEntry) {
264  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
265  }
266 
267  $showJobsDaoMock = M::mock('Fossology\\Lib\\Dao\\ShowJobsDao[getNumItemsPerSec]',array($this->dbManager, $this->uploadDao));
268  $showJobsDaoMock->shouldReceive('getNumItemsPerSec')->andReturn(0);
269 
270  $estimated = $showJobsDaoMock->getEstimatedTime($jobId, $jqType);
271  assertThat($estimated, equalTo('0:00:00'));
272  }
273  private function insertJobQue($jobQueueEndBits = 2)
274  {
275  $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
276  foreach ($jobs as $jobId => $jobUpload) {
277  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
278  }
279  $this->dbManager->prepare($stmt = 'insert.jobqueue',
280  "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)"
281  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
282 
283  $nowTime = time();
284  $diffTime = 2345;
285  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
286  $uploadArrayQue = array(array(8, $jobId=1, $jqType="nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
287  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 ));
288  foreach ($uploadArrayQue as $uploadEntry) {
289  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
290  }
291  }
292  public function testGetJobsForAll()
293  {
294  $this->insertJobQue();
295  $result = $this->showJobsDao->getJobsForAll();
296  $this->assertNotNull($result);
297  $this->assertEquals(array(array('job' => 'nomos', 'jq_job_fk' => "1","upload_fk" => "1", "status" => "running")),$result);
298  }
299  public function testGetItemsProcessedForDecider()
300  {
301  $this->insertJobQue();
302  $result = $this->showJobsDao->getItemsProcessedForDecider("nomos",1);
303  $expectedArray = array("147","1");
304  $this->assertNotNull($result);
305  $this->assertEquals($expectedArray,$result);
306  }
307  public function testGetJobStatusFalse()
308  {
309  $this->insertJobQue();
310  $jobStatus = $this->showJobsDao->getJobStatus(1);
311  $this->assertNotNull($jobStatus);
312  $this->assertFalse($jobStatus);
313  }
314  public function testGetJobStatusTrue()
315  {
316  $this->insertJobQue(4);
317  $jobStatus = $this->showJobsDao->getJobStatus(1);
318  $this->assertNotNull($jobStatus);
319  $this->assertTrue($jobStatus);
320  }
321  public function testGetDataASingleJob()
322  {
323  $this->insertJobQue();
324  $result = $this->showJobsDao->getDataForASingleJob(1);
325  $this->assertNotNull($result);
326  $expectedData = array(
327  'jq_pk' => "1",
328  'jq_job_fk' => "1",
329  'jq_type' => "ununpack",
330  'jq_args' => "1",
331  'jq_starttime' => $result['jq_starttime'],
332  'jq_endtime' => $result['jq_endtime'],
333  'jq_endtext' => "Completed",
334  'jq_end_bits' => "2",
335  'jq_schedinfo' => NULL,
336  'jq_itemsprocessed' => "646",
337  'jq_log' => NULL,
338  'jq_runonpfile' => NULL,
339  'jq_host' => NULL,
340  'jq_cmd_args' => NULL,
341  'job_pk' => "1",
342  'job_queued' => $result['job_queued'],
343  'job_priority' => NULL,
344  'job_email_notify' => NULL,
345  'job_name' => "FCKeditor_2.6.4.zip",
346  'job_upload_fk' => "1",
347  'job_folder_fk' => NULL,
348  'job_user_fk' => "1",
349  'job_group_fk' => NULL,
350  'elapsed' => "00:00:07.158312"
351  );
352  $this->assertEquals($expectedData,$result);
353  }
354 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16