NEW Add method executeCLI and a phpunit

This commit is contained in:
Laurent Destailleur
2017-11-06 20:24:12 +01:00
parent 1f1809dd3e
commit d90e3e88c2
4 changed files with 236 additions and 76 deletions

View File

@@ -449,4 +449,78 @@ class Utils
return 0; return 0;
} }
/**
* Execute a CLI command.
*
* @param string $command Command line to execute.
* @param string $outputfile Output file (used only when method is 2). For exemple $conf->admin->dir_temp.'/out.tmp';
* @param int $execmethod 0=Use default method (that is 1 by default), 1=Use the PHP 'exec', 2=Use the 'popen' method
* @return array array('result'=>...,'output'=>...,'error'=>...). result = 0 means OK.
*/
function executeCLI($command, $outputfile, $execmethod=0)
{
global $conf, $langs;
$result = 0;
$output = '';
$error = '';
$command=escapeshellcmd($command);
$command.=" 2>&1";
if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN;
if (empty($execmethod)) $execmethod=1;
//$execmethod=1;
dol_syslog("Utils::executeCLI execmethod=".$execmethod." system:".$command, LOG_DEBUG);
$output_arr=array();
if ($execmethod == 1)
{
exec($command, $output_arr, $retval);
$result = $retval;
if ($retval != 0)
{
$langs->load("errors");
dol_syslog("Utils::executeCLI retval after exec=".$retval, LOG_ERR);
$error = 'Error '.$retval;
}
}
if ($execmethod == 2) // With this method, there is no way to get the return code, only output
{
$ok=0;
$handle = fopen($outputfile, 'w+b');
if ($handle)
{
dol_syslog("Utils::executeCLI run command ".$command);
$handlein = popen($command, 'r');
while (!feof($handlein))
{
$read = fgets($handlein);
fwrite($handle,$read);
$output_arr[]=$read;
}
pclose($handlein);
fclose($handle);
}
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
}
// Update with result
if (is_array($output_arr) && count($output_arr)>0)
{
foreach($output_arr as $val)
{
$output.=$val.($execmethod == 2 ? '' : "\n");
}
}
dol_syslog("Utils::executeCLI result=".$result." output=".$output." error=".$error, LOG_DEBUG);
return array('result'=>$result, 'output'=>$output, 'error'=>$error);
}
} }

View File

@@ -1060,7 +1060,14 @@ class Cronjob extends CommonObject
$outputfile=$outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method $outputfile=$outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
// Execute a CLI // Execute a CLI
$retval = $this->executeCLI($this->command, $outputfile); include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
$utils = new Utils($db);
$arrayresult = $utils->executeCLI($this->command, $outputfile);
$retval = $arrayresult['result'];
$this->error = $arrayresult['error'];
$this->lastoutput = $arrayresult['output'];
$this->lastresult = $arrayresult['result'];
} }
} }
@@ -1082,81 +1089,6 @@ class Cronjob extends CommonObject
} }
/**
* Execute a CLI command.
* this->error, this->lastoutput, this->lastresult are also set.
*
* @param string $command Command line
* @param string $outputfile Output file
* @param int $execmethod 0=Use default method, 1=Use the PHP 'exec', 2=Use the 'popen' method
* @return int Retval
*/
function executeCLI($command, $outputfile, $execmethod=0)
{
global $conf, $langs;
$retval = 0;
$command=escapeshellcmd($command);
$command.=" 2>&1";
if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN;
if (empty($execmethod)) $execmethod=1;
//$execmethod=1;
dol_syslog(get_class($this)."::executeCLI execmethod=".$execmethod." system:".$command, LOG_DEBUG);
$output_arr=array();
if ($execmethod == 1)
{
exec($command, $output_arr, $retval);
if ($retval != 0)
{
$langs->load("errors");
dol_syslog(get_class($this)."::executeCLI retval after exec=".$retval, LOG_ERR);
$this->error = 'Error '.$retval;
$this->lastoutput = ''; // Will be filled later from $output_arr
$this->lastresult = $retval;
$retval = $this->lastresult;
}
}
if ($execmethod == 2) // With this method, there is no way to get the return code, only output
{
$ok=0;
$handle = fopen($outputfile, 'w+b');
if ($handle)
{
dol_syslog(get_class($this)."::executeCLI run command ".$command);
$handlein = popen($command, 'r');
while (!feof($handlein))
{
$read = fgets($handlein);
fwrite($handle,$read);
$output_arr[]=$read;
}
pclose($handlein);
fclose($handle);
}
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
}
// Update with result
if (is_array($output_arr) && count($output_arr)>0)
{
foreach($output_arr as $val)
{
$this->lastoutput.=$val.($execmethod == 2 ? '' : "\n");
}
}
$this->lastresult=$retval;
dol_syslog(get_class($this)."::executeCLI output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG);
return $retval;
}
/** /**
* Reprogram a job * Reprogram a job
* *

View File

@@ -85,6 +85,8 @@ class AllTests
$suite->addTestSuite('CompanyLibTest'); $suite->addTestSuite('CompanyLibTest');
require_once dirname(__FILE__).'/DateLibTest.php'; require_once dirname(__FILE__).'/DateLibTest.php';
$suite->addTestSuite('DateLibTest'); $suite->addTestSuite('DateLibTest');
require_once dirname(__FILE__).'/UtilsTest.php';
$suite->addTestSuite('UtilsTest');
//require_once dirname(__FILE__).'/DateLibTzFranceTest.php'; //require_once dirname(__FILE__).'/DateLibTzFranceTest.php';
//$suite->addTestSuite('DateLibTzFranceTest'); //$suite->addTestSuite('DateLibTzFranceTest');
require_once dirname(__FILE__).'/MarginsLibTest.php'; require_once dirname(__FILE__).'/MarginsLibTest.php';

152
test/phpunit/UtilsTest.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
/* Copyright (C) 2010-2015 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* or see http://www.gnu.org/
*/
/**
* \file test/phpunit/UtilsTest.php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI: phpunit filename.php
*/
global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/class/utils.class.php';
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS=1;
/**
* Class for PHPUnit tests
*
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class UtilsTest extends PHPUnit_Framework_TestCase
{
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return UserTest
*/
function __construct()
{
//$this->sharedFixture
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db;
print "\n";
}
// Static methods
public static function setUpBeforeClass()
{
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
print __METHOD__."\n";
}
// tear down after class
public static function tearDownAfterClass()
{
global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n";
}
/**
* Init phpunit tests
*
* @return void
*/
protected function setUp()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
print __METHOD__."\n";
}
/**
* End phpunit tests
*
* @return void
*/
protected function tearDown()
{
print __METHOD__."\n";
}
/**
* testExecuteCLI
*
* @return void
*/
public function testExecuteCLI()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Utils($this->savdb);
$result = $localobject->executeCLI('ls', $conf->admin->dir_temp.'/out.tmp', 1);
print var_export($result, true);
$this->assertEquals($result['result'], 0);
$this->assertEquals($result['error'], '');
$this->assertEquals(preg_match('/phpunit/', $result['output']), true);
$localobject=new Utils($this->savdb);
$result = $localobject->executeCLI('ls', $conf->admin->dir_temp.'/out.tmp', 2);
print var_export($result, true);
$this->assertEquals($result['result'], 0);
$this->assertEquals($result['error'], '');
$this->assertEquals(preg_match('/phpunit/', $result['output']), true);
print __METHOD__." result=".$result."\n";
return $result;
}
}