FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ShowJobsDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 Author: Johannes Najjar, anupam.ghosh@siemens.com, Shaheem Azmal
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Dao;
21 
25 use Mockery as M;
26 
27 class ShowJobsDaoTest extends \PHPUnit\Framework\TestCase
28 {
30  private $testDb;
32  private $dbManager;
34  private $uploadDao;
36  private $showJobsDao;
38  private $uploadPermissionDao;
39 
40  private $job_pks = array(2,1);
41 
42  protected function setUp()
43  {
44  $this->testDb = new TestPgDb();
45  $this->dbManager = &$this->testDb->getDbManager();
46 
47  $this->testDb->createPlainTables(
48  array(
49  'upload',
50  'uploadtree',
51  'job',
52  'perm_upload',
53  'jobqueue',
54  'jobdepends',
55  ));
56  $this->testDb->createInheritedTables(array('uploadtree_a'));
57 
58  $uploadArray = array(array('upload_pk'=>1, 'uploadtree_tablename'=>'uploadtree'),
59  array('upload_pk'=>2, 'uploadtree_tablename'=>'uploadtree_a'));
60  foreach ($uploadArray as $uploadEntry) {
61  $this->dbManager->insertTableRow('upload', $uploadEntry);
62  }
63 
64  $this->dbManager->prepare($stmt = 'insert.job',
65  "INSERT INTO job (job_pk, job_queued, job_name, job_upload_fk, job_user_fk) VALUES ($1, $2, $3, $4, $5)");
66  $jobArray = array(array(1,date('c',time()-5), "FCKeditor_2.6.4.zip", 1,1 ),
67  array(2,date('c'), "zlib_1.2.8.zip", 2,2));
68  foreach ($jobArray as $uploadEntry) {
69  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
70  }
71 
72  $logger = M::mock('Monolog\Logger');
73  $logger->shouldReceive('debug');
74  $this->uploadPermissionDao = M::mock('Fossology\Lib\Dao\UploadPermissionDao');
75  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermissionDao);
76  $this->showJobsDao = new ShowJobsDao($this->dbManager, $this->uploadDao);
77 
78  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
79  }
80 
81  protected function tearDown()
82  {
83  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
84  $this->testDb = null;
85  $this->dbManager = null;
86  }
87 
88 
89 
90  public function testUploads2Jobs()
91  {
92  $jobs = array(3=>2, 4=>3, 5=>5, 6=>8%6, 7=>13%6, 8=>21%6);
93  foreach ($jobs as $jobId => $jobUpload) {
94  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
95  }
96  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
97  $showJobDao = new ShowJobsDao($this->dbManager,$uploadDao);
98  $jobsWithoutUpload = $showJobDao->uploads2Jobs(array());
99  assertThat($jobsWithoutUpload, is(emptyArray()));
100  $jobsWithUploadIdOne = $showJobDao->uploads2Jobs(array(1));
101  assertThat($jobsWithUploadIdOne, equalTo(array(array(1,7),0)));
102  $jobsAtAll = $showJobDao->uploads2Jobs(array(1,2,3,4,5));
103  assertThat($jobsAtAll, equalTo(array(array(1,7, 2,3,6, 4,8, 5),0)));
104  $jobsWithUploadFour = $showJobDao->uploads2Jobs(array(4));
105  assertThat($jobsWithUploadFour[0], is(emptyArray()));
106  }
107 
108  public function testUploads2JobsPaged()
109  {
110  $jobs = array_combine(range(3,13),range(3,13));
111  foreach ($jobs as $jobId => $jobUpload) {
112  $this->dbManager->insertTableRow('job', array('job_pk' => $jobId, 'job_upload_fk' => $jobUpload));
113  }
114  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
115  $showJobDao = new ShowJobsDao($this->dbManager,$uploadDao);
116 
117  $jobsPage1 = $showJobDao->uploads2Jobs(range(1,17),0);
118  assertThat($jobsPage1[0], arrayWithSize(10));
119  assertThat($jobsPage1[1], is(1));
120  $jobsPage2 = $showJobDao->uploads2Jobs(array_combine(range(10,16),range(11,17)),1);
121  assertThat($jobsPage2[0], arrayWithSize(3));
122  assertThat($jobsPage2[1], is(0));
123  $jobsPage3 = $showJobDao->uploads2Jobs(array(),2);
124  assertThat($jobsPage3, arrayWithSize(0));
125  }
126 
127 
128  public function testgetJobName()
129  {
130  $testJobName = $this->showJobsDao->getJobName(1);
131  assertThat($testJobName, equalTo("FCKeditor_2.6.4.zip"));
132 
133  $testJobNameIfNothingQueued = $this->showJobsDao->getJobName($uploadId = 3);
134  assertThat($testJobNameIfNothingQueued, equalTo($uploadId));
135  }
136 
137  public function testMyJobs()
138  {
139  $groupId = 2;
140  $GLOBALS['SysConf']['auth'][Auth::GROUP_ID] = $groupId;
141  $GLOBALS['SysConf']['auth'][Auth::USER_ID] = 1;
142 
143  $this->uploadPermissionDao->shouldReceive('isAccessible')->withArgs(array(anything(),$groupId))
144  ->andReturnUsing(function($upload,$group)
145  {
146  return ($upload==1 || $upload==2 || $upload==4);
147  });
148  $testOurJobs = $this->showJobsDao->myJobs(true);
149  assertThat($testOurJobs[0], is(arrayContainingInAnyOrder($this->job_pks)));
150  $testMyJobs = $this->showJobsDao->myJobs(false);
151  assertThat($testMyJobs, equalTo(array(array(1), 0)));
152 
153  $this->dbManager->queryOnce("UPDATE job SET job_queued=job_queued-INTERVAL '30 days' WHERE job_pk=1");
154  $this->dbManager->prepare(__METHOD__.'insert.perm_upload',
155  "INSERT INTO perm_upload (perm_upload_pk, perm, upload_fk, group_fk) VALUES ($1, $2, $3, $4)");
156  $testOutdatedJobs = $this->showJobsDao->myJobs(true);
157  assertThat($testOutdatedJobs, equalTo(array(array(2), 0)));
158  }
159 
160  public function testgetNumItemsPerSec()
161  {
162  $numSecs = 30;
163  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(5*$numSecs, $numSecs);
164  assertThat($testFilesPerSec,is(greaterThan(1)));
165 
166  $testFilesPerSec = $this->showJobsDao->getNumItemsPerSec(0.9*$numSecs, $numSecs);
167  assertThat($testFilesPerSec,is(lessThanOrEqualTo(1)));
168  }
169 
170  public function testGetJobInfo()
171  {
172  $this->dbManager->prepare($stmt = 'insert.jobqueue',
173  "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)"
174  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
175 
176  $nowTime = time();
177  $diffTime = 2345;
178  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
179  $uploadArrayQue = array(array(8, $jobId=1, "nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
180  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 ));
181  foreach ($uploadArrayQue as $uploadEntry) {
182  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
183  }
184 
185  $this->dbManager->prepare($stmt = 'insert.uploadtree_a',
186  "INSERT INTO uploadtree_a (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
187  . "VALUES ($1, $2, $3, $4,$5, $6, $7, $8)");
188  $uploadTreeArray = array(array(123, 121, 1, 103, 32768, 542, 543, "fckeditorcode_ie.js"),
189  array(121,120, 1, 0, 536888320, 537, 544, "js"),
190  array(715,651, 2,607 ,33188 ,534 ,535 ,"zconf.h.cmakein"),
191  array(915, 651, 2, 606 ,33188 ,532 ,533 ,"zconf.h"),
192  );
193  foreach ($uploadTreeArray as $uploadEntry) {
194  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
195  }
196 
197  $this->dbManager->prepare($stmt = 'insert.jobdepends',
198  "INSERT INTO jobdepends (jdep_jq_fk, jdep_jq_depends_fk) VALUES ($1, $2 )");
199  $jqWithTwoDependencies = 8;
200  $jobDependsArray = array(array(2,1),
201  array(3,2),
202  array(4,2),
203  array(5,2),
204  array(6,2),
205  array($jqWithTwoDependencies,4),
206  array($jqWithTwoDependencies,4),
207  );
208  foreach ($jobDependsArray as $uploadEntry) {
209  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
210  }
211 
212  $testMyJobInfo = $this->showJobsDao->getJobInfo($this->job_pks);
213  assertThat($testMyJobInfo,hasKey($jobId));
214  assertThat($testMyJobInfo[$jobId]['jobqueue'][$jqWithTwoDependencies]['depends'], is(arrayWithSize(2)));
215 
216  $testFilesPerSec = 0.23;
217  $formattedEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $testFilesPerSec);
218  assertThat($formattedEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
219  $hourMinSec = explode(':', $formattedEstimatedTime);
220  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
221  is(closeTo(($itemCount-$itemNomos)/$testFilesPerSec,0.5+$testFilesPerSec)));
222 
223  $testGetEstimatedTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type, 0);
224  assertThat($testGetEstimatedTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
225  $hourMinSec = explode(':', $testGetEstimatedTime);
226  $tolerance = 0.5+($itemCount-$itemNomos)/$itemNomos+(time()-$nowTime);
227  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
228  is(closeTo(($itemCount-$itemNomos)/$itemNomos*$diffTime,$tolerance)));
229 
230  $fewFilesPerSec = 0.003;
231  $formattedLongTime = $this->showJobsDao->getEstimatedTime($job_pk=1, $jq_Type="nomos", $fewFilesPerSec);
232  assertThat($formattedLongTime, matchesPattern ('/\\d+:\\d{2}:\\d{2}/'));
233  $hourMinSec = explode(':', $formattedLongTime);
234  assertThat($hourMinSec[0]*3600+$hourMinSec[1]*60+$hourMinSec[2],
235  is(closeTo(($itemCount-$itemNomos)/$fewFilesPerSec,0.5+$fewFilesPerSec)));
236  }
237 
238  public function testGetEstimatedTimeShouldNotDivideByZero()
239  {
240  $this->dbManager->prepare($stmt = 'insert.jobqueue',
241  "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)"
242  . "VALUES ($1, $2, $3, $4,$5, $6,$7,$8,$9,$10)");
243 
244  $nowTime = time();
245  $diffTime = 2345;
246  $nomosTime = date('Y-m-d H:i:sO',$nowTime-$diffTime);
247  $uploadArrayQue = array(array(8, $jobId=1, $jqType="nomos", 1,$nomosTime,null ,"Started", 0,"localhost.5963", $itemNomos=147),
248  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 ));
249  foreach ($uploadArrayQue as $uploadEntry) {
250  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
251  }
252 
253  $showJobsDaoMock = M::mock('Fossology\\Lib\\Dao\\ShowJobsDao[getNumItemsPerSec]',array($this->dbManager, $this->uploadDao));
254  $showJobsDaoMock->shouldReceive('getNumItemsPerSec')->andReturn(0);
255 
256  $estimated = $showJobsDaoMock->getEstimatedTime($jobId, $jqType);
257  assertThat($estimated, equalTo('0:00:00'));
258  }
259 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28