FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
schedulerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2019, Siemens AG
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * version 2 as published by the Free Software Foundation.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14  */
15 
20 require_once "./SchedulerTestRunnerCli.php";
21 require_once "./SchedulerTestRunnerScheduler.php";
22 
26 use Monolog\Logger;
32 
37 class OjoScheduledTest extends \PHPUnit\Framework\TestCase
38 {
39 
43  private $testDb;
47  private $dbManager;
51  private $licenseDao;
55  private $testInstaller;
59  private $uploadDao;
63  private $uploadPermDao;
67  private $cliRunner;
71  private $schedulerRunner;
75  private $regressionFile;
76 
81  protected function setUp()
82  {
83  $this->regressionFile = "regexTest.json";
84 
85  $this->testDb = new TestPgDb("ojoSched" . time());
86  $this->dbManager = $this->testDb->getDbManager();
87 
88  $logger = new Logger("OjoSchedulerTest");
89 
90  $this->licenseDao = new LicenseDao($this->dbManager);
91  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
92  $this->uploadDao = new UploadDao($this->dbManager, $logger,
93  $this->uploadPermDao);
94 
95  $this->cliRunner = new SchedulerTestRunnerCli($this->testDb);
96  $this->schedulerRunner = new SchedulerTestRunnerScheduler($this->testDb);
97  }
98 
103  protected function tearDown()
104  {
105  $this->testDb->fullDestruct();
106  $this->testDb = null;
107  $this->dbManager = null;
108  $this->licenseDao = null;
109  }
110 
114  private function setUpRepo()
115  {
116  $sysConf = $this->testDb->getFossSysConf();
117  $this->testInstaller = new TestInstaller($sysConf);
118  $this->testInstaller->init();
119  $this->testInstaller->cpRepo();
120  }
121 
125  private function rmRepo()
126  {
127  $this->testInstaller->rmRepo();
128  $this->testInstaller->clear();
129  }
130 
134  private function setUpTables()
135  {
136  $this->testDb->createPlainTables(
137  array(
138  'agent',
139  'uploadtree',
140  'upload',
141  'pfile',
142  'users',
143  'ars_master',
144  'license_ref',
145  'license_file',
146  'highlight'
147  ));
148  $this->testDb->createSequences(
149  array(
150  'agent_agent_pk_seq',
151  'upload_upload_pk_seq',
152  'pfile_pfile_pk_seq',
153  'users_user_pk_seq',
154  'nomos_ars_ars_pk_seq',
155  'license_ref_rf_pk_seq',
156  'license_file_fl_pk_seq',
157  'license_file_fl_pk_seq'
158  ));
159  $this->testDb->createConstraints(
160  array(
161  'agent_pkey',
162  'upload_pkey_idx',
163  'pfile_pkey',
164  'user_pkey',
165  'license_file_pkey'
166  ));
167  $this->testDb->alterTables(
168  array(
169  'agent',
170  'pfile',
171  'upload',
172  'ars_master',
173  'users',
174  'license_ref',
175  'license_file'
176  ));
177  $this->testDb->createInheritedTables(array(
178  'uploadtree_a'
179  ));
180 
181  $this->testDb->insertData(
182  array(
183  'upload',
184  'pfile',
185  'uploadtree_a',
186  'users',
187  'license_ref'
188  ), false);
189  }
190 
197  private function resultArrayContainsLicense($resultArray, $licenseName)
198  {
199  foreach ($resultArray as $result) {
200  if (strcmp($result["license"], $licenseName) === 0) {
201  return true;
202  }
203  }
204  return false;
205  }
206 
221  private function compareMatches($left, $right)
222  {
223  if (strcmp($left["file"], $right["file"]) !== 0) {
224  return strcmp($left["file"], $right["file"]);
225  }
226  if ($left["results"] === null) {
227  if ($right["results"] === null) {
228  return 0;
229  }
230  return 1;
231  }
232  if ($right["results"] === null) {
233  return -1;
234  }
235  if (count($left["results"]) !== count($right["results"])) {
236  return count($left["results"]) - count($right["results"]);
237  }
238  foreach ($left["results"] as $key => $result) {
239  if (strcmp($result["license"], $right["results"][$key]["license"]) !== 0) {
240  return strcmp($result["license"], $right[$key]["license"]);
241  }
242  }
243  return 0;
244  }
245 
252  private function compareMatchesFiles($left, $right)
253  {
254  return strcmp($left["file"], $right["file"]);
255  }
256 
266  public function testRun()
267  {
268  $this->setUpTables();
269  $this->setUpRepo();
270  $uploadId = 44;
271  list ($success, $output, $retCode) = $this->schedulerRunner->run($uploadId);
272  $this->rmRepo();
273  $this->assertTrue($success, 'running ojo failed');
274  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
275 
276  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
277  $uploadParent = $this->uploadDao->getItemTreeBounds(460, $uploadTreeTableName);
278  $licenseMatches = $this->licenseDao->getLicensesPerFileNameForAgentId(
279  $uploadParent, [1]);
280 
281  $this->assertGreaterThan(8, count($licenseMatches), $output);
282  $this->assertContains("Classpath-exception-2.0",
283  $licenseMatches["spdx.tar/spdx/GPL-2.0_WITH_Classpath-exception-2.0"]["scanResults"]);
284  $this->assertContains("GPL-2.0-only",
285  $licenseMatches["spdx.tar/spdx/GPL-2.0_WITH_Classpath-exception-2.0"]["scanResults"]);
286 
287  $this->assertContains("GPL-2.0-only",
288  $licenseMatches["spdx.tar/spdx/GPL-2.0_OR_MIT"]["scanResults"]);
289  $this->assertContains("MIT",
290  $licenseMatches["spdx.tar/spdx/GPL-2.0_OR_MIT"]["scanResults"]);
291 
292  $this->assertContains("GPL-2.0-or-later",
293  $licenseMatches["spdx.tar/spdx/GPL-2.0-or-later"]["scanResults"]);
294 
295  $this->assertContains("GPL-2.0-only",
296  $licenseMatches["spdx.tar/spdx/GPL-2.0-only"]["scanResults"]);
297 
298  $this->assertContains("LGPL-2.1-or-later",
299  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
300  $this->assertContains("GPL-2.0-only",
301  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
302  $this->assertContains("MIT",
303  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
304 
305  $this->assertContains("GPL-2.0-or-later",
306  $licenseMatches["spdx.tar/spdx/GPL-2.0+"]["scanResults"]);
307 
308  $this->assertContains("GPL-2.0-only",
309  $licenseMatches["spdx.tar/spdx/GPL-2.0"]["scanResults"]);
310  }
311 
319  public function testCli()
320  {
321  $testFile = "../../../nomos/agent_tests/testdata/NomosTestfiles/SPDX/MPL-2.0_AND_BSD-2-Clause_AND_MIT_OR_Apache-2.0.txt";
322 
323  $args = "--json $testFile";
324  list ($success, $output, $retCode) = $this->cliRunner->run($args);
325  $this->assertTrue($success, 'running ojo failed');
326  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
327 
328  $this->assertJson($output);
329  $result = json_decode($output, true);
330  $resultArray = $result[0]["results"];
331 
332  $this->assertEquals($testFile, $result[0]["file"]);
333  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
334  "MPL-2.0-no-copyleft-exception"));
335  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
336  "BSD-2-Clause"));
337  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
338  "MIT"));
339  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
340  "Apache-2.0"));
341  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
342  "Dual-license"));
343  }
344 
352  public function regressionTest()
353  {
354  $testDir = "../../../nomos/agent_tests/testdata/NomosTestfiles/SPDX";
355 
356  $args = "--json --directory $testDir";
357  list ($success, $output, $retCode) = $this->cliRunner->run($args);
358  $this->assertTrue($success, 'running ojo failed');
359  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
360  $this->assertJson($output);
361 
362  // Load data from last run
363  $jsonFromFile = json_decode(file_get_contents($this->regressionFile), true);
364  // Load result from agent
365  $jsonFromOutput = json_decode($output, true);
366 
367  // Sort the data to reduce differences
368  usort($jsonFromFile, array('OjoScheduledTest', 'compareMatchesFiles'));
369  usort($jsonFromOutput, array('OjoScheduledTest', 'compareMatchesFiles'));
370 
371  // Find the difference
372  $jsonDiff = array_udiff($jsonFromFile, $jsonFromOutput,
373  array('OjoScheduledTest', 'compareMatches'));
374 
375  $outputDiff = "JSON does not match regression test file.\n";
376  $outputDiff .= "Following are the results not in regression test file.\n";
377  $outputDiff .= print_r($jsonDiff, true);
378 
379  $this->assertEquals(0, count($jsonDiff), $outputDiff);
380  }
381 }
setUpTables()
Setup tables required by copyright agent.
setUp()
Setup the test cases and initialize the objects.
resultArrayContainsLicense($resultArray, $licenseName)
testCli()
Run the test for CLI.
compareMatches($left, $right)
Compare two matches from OJO (slow)
Functional test cases for ojo agent using scheduler.
tearDown()
Destruct the objects initialized during setUp()
compareMatchesFiles($left, $right)
regressionTest()
Run a regression test for OJO.
rmRepo()
Remove the test repo.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
setUpRepo()
Setup test repo mimicking install.
testRun()
Run the test.