FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
CommonCliTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2015 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 
25 
31 class CommonCliTest extends \PHPUnit\Framework\TestCase
32 {
33 
38  protected $testDb;
39 
44  protected $testInstaller;
45 
50  protected $agentDir;
51 
56  protected function setUp()
57  {
58  $this->testDb = new TestPgDb("nomosfun" . time());
59  $this->agentDir = dirname(dirname(__DIR__));
60  $this->testdir = dirname(dirname(__DIR__)) .
61  "/agent_tests/testdata/NomosTestfiles/";
62 
63  $sysConf = $this->testDb->getFossSysConf();
64  $this->testInstaller = new TestInstaller($sysConf);
65  $this->testInstaller->init();
66  $this->testInstaller->install($this->agentDir);
67 
68  $this->testDb->createSequences(array(
69  'license_ref_rf_pk_seq'
70  ), false);
71  $this->testDb->createPlainTables(array(
72  'agent',
73  'license_ref'
74  ), false);
75  $this->testDb->alterTables(array(
76  'license_ref'
77  ), false);
78  }
79 
84  protected function tearDown()
85  {
86  $this->testInstaller->uninstall($this->agentDir);
87  $this->testInstaller->clear();
88  $this->testInstaller->rmRepo();
89  $this->testDb = null;
90  }
91 
101  protected function runNomos($args = "", $files = array())
102  {
103  $sysConf = $this->testDb->getFossSysConf();
104 
105  $confFile = $sysConf . "/fossology.conf";
106  system("touch " . $confFile);
107  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
108  file_put_contents($confFile, $config);
109 
110  $execDir = $this->agentDir . '/agent';
111  system(
112  "install -D $this->agentDir/VERSION $sysConf/mods-enabled/nomos/VERSION");
113 
114  foreach ($files as $file) {
115  $args .= " " . escapeshellarg($file);
116  }
117 
118  $pipeFd = popen("$execDir/nomos -c $sysConf $args", "r");
119  $this->assertTrue($pipeFd !== false, 'running nomos failed');
120 
121  $output = "";
122  while (($buffer = fgets($pipeFd, 4096)) !== false) {
123  $output .= $buffer;
124  }
125  $retCode = pclose($pipeFd);
126 
127  unlink("$sysConf/mods-enabled/nomos/VERSION");
128  // unlink("$sysConf/mods-enabled/nomos");
129  // rmdir("$sysConf/mods-enabled");
130  unlink($confFile);
131 
132  return array(
133  $output,
134  $retCode
135  );
136  }
137 
144  public function testHelp()
145  {
146  $nomos = dirname(dirname(__DIR__)) . '/agent/nomos';
147  list ($output,) = $this->runNomos($args = "-h"); // exec("$nomos -h 2>&1",
148  // $out, $rtn);
149  $out = explode("\n", $output);
150  $usage = "Usage: $nomos [options] [file [file [...]]";
151  $this->assertEquals($usage, $out[0]);
152  }
153 }
runNomos($args="", $files=array())
Run nomos using the arguments passed.
Tests for common CLI operations.
tearDown()
Destruct the objects initialized during setUp()
testHelp()
Test for nomos help message.
setUp()
Setup the test cases and initialize the objects.