FOSSology  4.4.0
Open Source License Compliance by Open Source Software
schedulerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 require_once "SchedulerTestRunnerCli.php";
13 require_once "SchedulerTestRunnerScheduler.php";
14 
18 use Monolog\Logger;
24 
29 class OjoScheduledTest extends \PHPUnit\Framework\TestCase
30 {
31 
35  private $testDb;
39  private $dbManager;
43  private $licenseDao;
47  private $testInstaller;
51  private $uploadDao;
55  private $uploadPermDao;
59  private $cliRunner;
63  private $schedulerRunner;
67  private $regressionFile;
68 
73  protected function setUp() : void
74  {
75  $this->regressionFile = __DIR__ . DIRECTORY_SEPARATOR . "regexTest.json";
76 
77  $this->testDb = new TestPgDb("ojoSched" . time());
78  $this->dbManager = $this->testDb->getDbManager();
79 
80  $logger = new Logger("OjoSchedulerTest");
81 
82  $this->licenseDao = new LicenseDao($this->dbManager);
83  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
84  $this->uploadDao = new UploadDao($this->dbManager, $logger,
85  $this->uploadPermDao);
86 
87  $this->cliRunner = new SchedulerTestRunnerCli($this->testDb);
88  $this->schedulerRunner = new SchedulerTestRunnerScheduler($this->testDb);
89  }
90 
95  protected function tearDown() : void
96  {
97  $this->testDb->fullDestruct();
98  $this->testDb = null;
99  $this->dbManager = null;
100  $this->licenseDao = null;
101  }
102 
106  private function setUpRepo()
107  {
108  $sysConf = $this->testDb->getFossSysConf();
109  $this->testInstaller = new TestInstaller($sysConf);
110  $this->testInstaller->init();
111  $this->testInstaller->cpRepo();
112  }
113 
117  private function rmRepo()
118  {
119  $this->testInstaller->rmRepo();
120  $this->testInstaller->clear();
121  }
122 
126  private function setUpTables()
127  {
128  $this->testDb->createPlainTables(
129  array(
130  'agent',
131  'uploadtree',
132  'upload',
133  'pfile',
134  'users',
135  'groups',
136  'ars_master',
137  'license_ref',
138  'license_file',
139  'highlight'
140  ));
141  $this->testDb->createInheritedTables(
142  array(
143  'license_candidate'
144  ));
145  $this->testDb->createSequences(
146  array(
147  'agent_agent_pk_seq',
148  'upload_upload_pk_seq',
149  'pfile_pfile_pk_seq',
150  'users_user_pk_seq',
151  'group_group_pk_seq',
152  'nomos_ars_ars_pk_seq',
153  'license_ref_rf_pk_seq',
154  'license_file_fl_pk_seq',
155  'license_file_fl_pk_seq'
156  ));
157  $this->testDb->createConstraints(
158  array(
159  'agent_pkey',
160  'upload_pkey_idx',
161  'pfile_pkey',
162  'user_pkey',
163  'license_file_pkey'
164  ));
165  $this->testDb->alterTables(
166  array(
167  'agent',
168  'pfile',
169  'upload',
170  'ars_master',
171  'users',
172  'groups',
173  'license_ref',
174  'license_file'
175  ));
176  $this->testDb->createInheritedTables(array(
177  'uploadtree_a'
178  ));
179 
180  $this->testDb->insertData(
181  array(
182  'upload',
183  'pfile',
184  'uploadtree_a',
185  'users',
186  'license_ref'
187  ), false);
188  }
189 
196  private function resultArrayContainsLicense($resultArray, $licenseName)
197  {
198  foreach ($resultArray as $result) {
199  if (strcmp($result["license"], $licenseName) === 0) {
200  return true;
201  }
202  }
203  return false;
204  }
205 
220  private function compareMatches($left, $right)
221  {
222  $leftFile = basename($left["file"]);
223  $rightFile = basename($right["file"]);
224  if (strcmp($leftFile, $rightFile) !== 0) {
225  return strcmp($leftFile, $rightFile);
226  }
227  if ($left["results"] === null) {
228  if ($right["results"] === null) {
229  return 0;
230  }
231  return 1;
232  }
233  if ($right["results"] === null) {
234  return -1;
235  }
236  if (count($left["results"]) !== count($right["results"])) {
237  return count($left["results"]) - count($right["results"]);
238  }
239  foreach ($left["results"] as $key => $result) {
240  if (strcmp($result["license"], $right["results"][$key]["license"]) !== 0) {
241  return strcmp($result["license"], $right[$key]["license"]);
242  }
243  }
244  return 0;
245  }
246 
253  private function compareMatchesFiles($left, $right)
254  {
255  return strcmp($left["file"], $right["file"]);
256  }
257 
267  public function testRun()
268  {
269  $this->setUpTables();
270  $this->setUpRepo();
271  $uploadId = 44;
272  list ($success, $output, $retCode) = $this->schedulerRunner->run($uploadId);
273  $this->rmRepo();
274  $this->assertTrue($success, 'running ojo failed');
275  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
276 
277  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
278  $uploadParent = $this->uploadDao->getItemTreeBounds(460, $uploadTreeTableName);
279  $licenseMatches = $this->licenseDao->getLicensesPerFileNameForAgentId(
280  $uploadParent, [1]);
281 
282  $this->assertGreaterThan(8, count($licenseMatches), $output);
283  $this->assertContains("Classpath-exception-2.0",
284  $licenseMatches["spdx.tar/spdx/GPL-2.0_WITH_Classpath-exception-2.0"]["scanResults"]);
285  $this->assertContains("GPL-2.0-only",
286  $licenseMatches["spdx.tar/spdx/GPL-2.0_WITH_Classpath-exception-2.0"]["scanResults"]);
287 
288  $this->assertContains("GPL-2.0-only",
289  $licenseMatches["spdx.tar/spdx/GPL-2.0_OR_MIT"]["scanResults"]);
290  $this->assertContains("MIT",
291  $licenseMatches["spdx.tar/spdx/GPL-2.0_OR_MIT"]["scanResults"]);
292 
293  $this->assertContains("GPL-2.0-or-later",
294  $licenseMatches["spdx.tar/spdx/GPL-2.0-or-later"]["scanResults"]);
295 
296  $this->assertContains("GPL-2.0-only",
297  $licenseMatches["spdx.tar/spdx/GPL-2.0-only"]["scanResults"]);
298 
299  $this->assertContains("LGPL-2.1-or-later",
300  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
301  $this->assertContains("GPL-2.0-only",
302  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
303  $this->assertContains("MIT",
304  $licenseMatches["spdx.tar/spdx/GPL-2.0_AND_LGPL-2.1-or-later_OR_MIT"]["scanResults"]);
305 
306  $this->assertContains("GPL-2.0-or-later",
307  $licenseMatches["spdx.tar/spdx/GPL-2.0+"]["scanResults"]);
308 
309  $this->assertContains("GPL-2.0-only",
310  $licenseMatches["spdx.tar/spdx/GPL-2.0"]["scanResults"]);
311  }
312 
320  public function testCli()
321  {
322  $testFile = dirname(__DIR__, 3)."/nomos/agent_tests/testdata/NomosTestfiles/SPDX/MPL-2.0_AND_BSD-2-Clause_AND_MIT_OR_Apache-2.0.txt";
323 
324  $args = "--json $testFile";
325  list ($success, $output, $retCode) = $this->cliRunner->run($args);
326  $this->assertTrue($success, 'running ojo failed');
327  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
328 
329  $this->assertJson($output);
330  $result = json_decode($output, true);
331  $resultArray = $result[0]["results"];
332 
333  $this->assertEquals($testFile, $result[0]["file"]);
334  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
335  "MPL-2.0-no-copyleft-exception"));
336  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
337  "BSD-2-Clause"));
338  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
339  "MIT"));
340  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
341  "Apache-2.0"));
342  $this->assertTrue($this->resultArrayContainsLicense($resultArray,
343  "Dual-license"));
344  }
345 
353  public function regressionTest()
354  {
355  $testDir = dirname(__DIR__, 3)."/nomos/agent_tests/testdata/NomosTestfiles/SPDX";
356 
357  $args = "--json --directory $testDir";
358  list ($success, $output, $retCode) = $this->cliRunner->run($args);
359  $this->assertTrue($success, 'running ojo failed');
360  $this->assertEquals($retCode, 0, "ojo failed ($retCode): $output");
361  $this->assertJson($output);
362 
363  // Load data from last run
364  $jsonFromFile = json_decode(file_get_contents($this->regressionFile), true);
365  // Load result from agent
366  $jsonFromOutput = json_decode($output, true);
367 
368  // Sort the data to reduce differences
369  usort($jsonFromFile, array('OjoScheduledTest', 'compareMatchesFiles'));
370  usort($jsonFromOutput, array('OjoScheduledTest', 'compareMatchesFiles'));
371 
372  // Find the difference
373  $jsonDiff = array_udiff($jsonFromFile, $jsonFromOutput,
374  array('OjoScheduledTest', 'compareMatches'));
375 
376  $outputDiff = "JSON does not match regression test file.\n";
377  $outputDiff .= "Following are the results not in regression test file.\n";
378  $outputDiff .= print_r($jsonDiff, true);
379 
380  $this->assertEquals(0, count($jsonDiff), $outputDiff);
381  }
382 }
Functional test cases for ojo agent using scheduler.
resultArrayContainsLicense($resultArray, $licenseName)
testRun()
Run the test.
compareMatches($left, $right)
Compare two matches from OJO (slow)
setUpRepo()
Setup test repo mimicking install.
rmRepo()
Remove the test repo.
setUp()
Setup the test cases and initialize the objects.
compareMatchesFiles($left, $right)
setUpTables()
Setup tables required by copyright agent.
testCli()
Run the test for CLI.
regressionTest()
Run a regression test for OJO.
tearDown()
Destruct the objects initialized during setUp()
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16