FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
schedulerTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, 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 */
18 
30 use Monolog\Logger;
31 
32 if (!function_exists('Traceback_uri'))
33 {
34  function Traceback_uri(){
35  return 'Traceback_uri_if_desired';
36  }
37 }
38 
43 class CopyrightScheduledTest extends \PHPUnit\Framework\TestCase
44 {
48  private $testDb;
52  private $dbManager;
56  private $licenseDao;
60  private $uploadDao;
64  private $uploadPermDao;
68  private $copyrightDao;
69 
74  protected function setUp()
75  {
76  $this->testDb = new TestPgDb("copyrightSched".time());
77  $this->dbManager = $this->testDb->getDbManager();
78 
79  $logger = new Logger("CopyrightSchedulerTest");
80 
81  $this->licenseDao = new LicenseDao($this->dbManager);
82  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
83  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
84  $this->copyrightDao = new CopyrightDao($this->dbManager, $this->uploadDao);
85  }
86 
91  protected function tearDown()
92  {
93  $this->testDb->fullDestruct();
94  $this->testDb = null;
95  $this->dbManager = null;
96  $this->licenseDao = null;
97  }
98 
109  private function runCopyright($uploadId)
110  {
111  $sysConf = $this->testDb->getFossSysConf();
112 
113  $agentName = "copyright";
114 
115  $agentDir = dirname(dirname(__DIR__));
116  $execDir = "$agentDir/agent";
117  system("install -D $agentDir/VERSION-copyright $sysConf/mods-enabled/$agentName/VERSION");
118  system("install -D $agentDir/agent/copyright.conf $sysConf/mods-enabled/$agentName/agent/copyright.conf");
119  $pCmd = "echo $uploadId | $execDir/$agentName -c $sysConf --scheduler_start";
120  $pipeFd = popen($pCmd, "r");
121  $this->assertTrue($pipeFd !== false, 'running copyright failed');
122 
123  $output = "";
124  while (($buffer = fgets($pipeFd, 4096)) !== false) {
125  $output .= $buffer;
126  }
127  $retCode = pclose($pipeFd);
128 
129  unlink("$sysConf/mods-enabled/$agentName/VERSION");
130  unlink("$sysConf/mods-enabled/$agentName/agent/copyright.conf");
131  rmdir("$sysConf/mods-enabled/$agentName/agent/");
132  rmdir("$sysConf/mods-enabled/$agentName");
133  rmdir("$sysConf/mods-enabled");
134  unlink($sysConf."/fossology.conf");
135 
136  $this->assertEquals($retCode, 0, "copyright failed ($retCode): $output [$pCmd]");
137  return $output;
138  }
139 
143  private function setUpRepo()
144  {
145  $sysConf = $this->testDb->getFossSysConf();
146 
147  $confFile = $sysConf."/fossology.conf";
148  system("touch ".$confFile);
149  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
150  file_put_contents($confFile, $config);
151 
152  $testRepoDir = dirname(dirname(dirname(__DIR__)))."/lib/php/Test/";
153  system("cp -a $testRepoDir/repo $sysConf/");
154  }
155 
159  private function rmRepo()
160  {
161  $sysConf = $this->testDb->getFossSysConf();
162  system("rm $sysConf/repo -rf");
163  }
164 
168  private function setUpTables()
169  {
170  $this->testDb->createPlainTables(array('agent','uploadtree','upload','pfile','users','bucketpool','mimetype','ars_master','author'));
171  $this->testDb->createSequences(array('agent_agent_pk_seq','upload_upload_pk_seq','pfile_pfile_pk_seq','users_user_pk_seq','nomos_ars_ars_pk_seq'));
172  $this->testDb->createConstraints(array('agent_pkey','upload_pkey_idx','pfile_pkey','user_pkey'));
173  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','users'));
174  $this->testDb->createInheritedTables(array('uploadtree_a'));
175 
176  $this->testDb->insertData(array('upload','pfile','uploadtree_a','bucketpool','mimetype','users'), false);
177  }
178 
188  public function testRun()
189  {
190  $this->setUpTables();
191  $this->setUpRepo();
192  $output = $this->runCopyright($uploadId=1);
193  $this->rmRepo();
194 
195  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
196  $matches = $this->copyrightDao->getAllEntries("copyright", $uploadId, $uploadTreeTableName);
197  $this->assertGreaterThan($expected=5, count($matches), $output);
198  }
199 
200 }
Traceback_uri()
Get the URI without query to this location.
setUp()
Setup the test cases and initialize the objects.
testRun()
Run the test.
setUpTables()
Setup tables required by copyright agent.
rmRepo()
Remove the test repo.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
tearDown()
Destruct the objects initialized during setUp()
runCopyright($uploadId)
Run copyright on a given upload id.
Unit test cases for copyright agent using scheduler.
setUpRepo()
Setup test repo mimicking install.