mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 02:28:44 +02:00
N°9104 - Be able to trigger cron remotely and asynchronously
This commit is contained in:
@@ -687,4 +687,17 @@ abstract class ItopTestCase extends KernelTestCase
|
||||
|
||||
return $this->CallUrl($sUrl, $aPostFields, $aCurlOptions, $bXDebugEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a temporary file path. that will be cleaned up by tearDown()
|
||||
*
|
||||
* @return string: temporary file path: file prefix include phpunit test method name
|
||||
*/
|
||||
public function GetTemporaryFilePath(): string
|
||||
{
|
||||
$sPrefix = $this->getName(false);
|
||||
$sPath = tempnam(sys_get_temp_dir(), $sPrefix);
|
||||
$this->aFileToClean[] = $sPath;
|
||||
return $sPath;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Webservices;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use Exception;
|
||||
use iTopMutex;
|
||||
use MetaModel;
|
||||
use utils;
|
||||
|
||||
/**
|
||||
* @group itopRequestMgmt
|
||||
* @group restApi
|
||||
* @group defaultProfiles
|
||||
*/
|
||||
class CronServiceTest extends ItopDataTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp(); // TODO: Change the autogenerated stub
|
||||
$this->RequireOnceItopFile('webservices/CronService.php');
|
||||
}
|
||||
|
||||
public function testIsStarted()
|
||||
{
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
file_put_contents($sPath, file_get_contents(__DIR__.'/resources/cron_starting.log'));
|
||||
|
||||
$this->assertEquals(true, \CronService::GetInstance()->IsStarted($sPath));
|
||||
}
|
||||
|
||||
public function testIsFailed_MissingCredentialsFailure()
|
||||
{
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
file_put_contents($sPath, file_get_contents(__DIR__.'/resources/cron_missingcreds_error.log'));
|
||||
|
||||
$this->assertEquals("Missing argument 'auth_user'", \CronService::GetInstance()->GetErrorMessage($sPath));
|
||||
$this->assertEquals(true, \CronService::GetInstance()->IsFailed($sPath));
|
||||
}
|
||||
|
||||
public static function ErrorProvider()
|
||||
{
|
||||
return [
|
||||
["Access wrong credentials ('user123')"],
|
||||
['gabuzomeu'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider ErrorProvider
|
||||
*/
|
||||
public function testIsFailed_AuthenticationFailure($sError)
|
||||
{
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
$sContent = <<<LOG
|
||||
..
|
||||
..
|
||||
ERROR: $sError
|
||||
LOG;
|
||||
|
||||
file_put_contents($sPath, $sContent);
|
||||
|
||||
$this->assertEquals($sError, \CronService::GetInstance()->GetErrorMessage($sPath));
|
||||
$this->assertEquals(true, \CronService::GetInstance()->IsFailed($sPath));
|
||||
}
|
||||
|
||||
public static function CannotStartProvider()
|
||||
{
|
||||
return [
|
||||
["cron_alreadyrunning.log", 'Already running...'],
|
||||
['cron_maintenance.log', 'A maintenance is ongoing'],
|
||||
['cron_notanadmin.log', 'Access restricted to administrators'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider CannotStartProvider
|
||||
*/
|
||||
public function testCronCannotStart(string $sLogFile, $sError)
|
||||
{
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
file_put_contents($sPath, file_get_contents(__DIR__.'/resources/'.$sLogFile));
|
||||
|
||||
$this->assertEquals($sError, \CronService::GetInstance()->GetErrorMessage($sPath));
|
||||
$this->assertEquals(true, \CronService::GetInstance()->IsFailed($sPath));
|
||||
}
|
||||
|
||||
public function testCronRunning()
|
||||
{
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
$sContent = <<<LOG
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Starting: 1772551316 (2026-03-03 16:21:56)
|
||||
Creating backup: '/var/www/html/iTopLegacy/data/backups/auto/gabuzomeuuninstall-2026-03-03_16_21.tar.gz'
|
||||
backup: creating tmp dir '/var/www/html/iTopLegacy/data/tmp-backup-1024104636'
|
||||
backup: adding resource '/var/www/html/iTopLegacy/conf/production/config-itop.php'
|
||||
backup: adding resource '/var/www/html/iTopLegacy/data/production-modules/'
|
||||
Starting backup of localhost/gabuzomeuuninstall(suffix:'')
|
||||
backup: generate data file with command: "mysqldump" --defaults-extra-file="/tmp/itop-mysqldump-CVvm4G" --opt --skip-lock-tables --default-character-set=utf8mb4 --add-drop-database --single-transaction --host='localhost' --user=xxxxx --result-file='/var/www/html/iTopLegacy/data/tmp-backup-1024104636/itop-dump.sql' 'gabuzomeuuninstall'
|
||||
LOG;
|
||||
|
||||
file_put_contents($sPath, $sContent);
|
||||
|
||||
$this->assertEquals(null, \CronService::GetInstance()->GetErrorMessage($sPath));
|
||||
$this->assertEquals(false, \CronService::GetInstance()->IsFailed($sPath));
|
||||
}
|
||||
|
||||
public function testCronStopped()
|
||||
{
|
||||
|
||||
$sPath = $this->GetTemporaryFilePath();
|
||||
file_put_contents($sPath, file_get_contents(__DIR__.'/resources/cron_stopped.log'));
|
||||
|
||||
$this->assertEquals(null, \CronService::GetInstance()->GetErrorMessage($sPath));
|
||||
$this->assertEquals(false, \CronService::GetInstance()->IsFailed($sPath));
|
||||
}
|
||||
}
|
||||
@@ -184,6 +184,92 @@ class CronTest extends ItopDataTestCase
|
||||
$this->assertEquals(static::$sLogin, $sUserLogin);
|
||||
}
|
||||
|
||||
public function testGetCronStatus_FailWhenNotAdmin()
|
||||
{
|
||||
$this->AddLoginModeAndSaveConfiguration('url');
|
||||
$this->CreateUserWithProfiles([self::$aURP_Profiles['REST Services User']]);
|
||||
|
||||
$sLogFileName = "crontest_".uniqid().'.log';
|
||||
$aPostFields = [
|
||||
'version' => '1.3',
|
||||
'verbose' => 1,
|
||||
'debug' => 1,
|
||||
];
|
||||
|
||||
$aGetFields = [
|
||||
'auth_user' => static::$sLogin,
|
||||
'auth_pwd' => static::$sPassword,
|
||||
];
|
||||
|
||||
$sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields);
|
||||
|
||||
$this->assertEquals('{"message":"Access restricted to administrators"}', $sJSONResult);
|
||||
}
|
||||
|
||||
public function testGetCronStatus_FailWhenLogFileDoesNotExist()
|
||||
{
|
||||
$this->AddLoginModeAndSaveConfiguration('url');
|
||||
$this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator']]);
|
||||
|
||||
$aPostFields = [
|
||||
'version' => '1.3',
|
||||
'verbose' => 1,
|
||||
'debug' => 1,
|
||||
'cron_log_file' => 'gabuzomeu.log',
|
||||
];
|
||||
|
||||
$aGetFields = [
|
||||
'auth_user' => static::$sLogin,
|
||||
'auth_pwd' => static::$sPassword,
|
||||
];
|
||||
|
||||
$sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields);
|
||||
|
||||
$this->assertEquals('{"message":"Cannot read log file"}', $sJSONResult);
|
||||
}
|
||||
|
||||
public static function GetCronStatusProvider()
|
||||
{
|
||||
return [
|
||||
["cron_alreadyrunning.log", 'Already running...', "error"],
|
||||
["cron_dummyerror.log", 'Already running...', "error"],
|
||||
['cron_maintenance.log', 'A maintenance is ongoing', "error"],
|
||||
['cron_missingcreds_error.log', 'A maintenance is ongoing', "error"],
|
||||
['cron_notanadmin.log', 'Access restricted to administrators', "error"],
|
||||
['cron_starting.log', '', "running"],
|
||||
['cron_stopped.log', '', "stopped"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider GetCronStatusProvider
|
||||
*/
|
||||
public function testGetCronStatus($sLogFilename, $expectedMsg, $expectedStatus)
|
||||
{
|
||||
$sLogFile = APPROOT."log/$sLogFilename";
|
||||
file_put_contents($sLogFile, file_get_contents(__DIR__.'/resources/'.$sLogFilename));
|
||||
$this->aFileToClean[] = $sLogFile;
|
||||
|
||||
$this->AddLoginModeAndSaveConfiguration('url');
|
||||
$this->CreateUserWithProfiles([self::$aURP_Profiles['Administrator']]);
|
||||
|
||||
$aPostFields = [
|
||||
'version' => '1.3',
|
||||
'verbose' => 1,
|
||||
'debug' => 1,
|
||||
'cron_log_file' => $sLogFile,
|
||||
];
|
||||
|
||||
$aGetFields = [
|
||||
'auth_user' => static::$sLogin,
|
||||
'auth_pwd' => static::$sPassword,
|
||||
];
|
||||
|
||||
$sJSONResult = $this->CallItopUri("/webservices/get_cron_status.php?".http_build_query($aGetFields), $aPostFields);
|
||||
|
||||
$this->assertEquals('{"message":"Cannot read log file"}', $sJSONResult);
|
||||
}
|
||||
|
||||
private function CreateUserWithProfiles(array $aProfileIds): ?string
|
||||
{
|
||||
if (count($aProfileIds) > 0) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Already running...
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Error: GABUZOMEU
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
A maintenance is ongoing
|
||||
@@ -0,0 +1,12 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
php /var/www/html/iTop/webservices/cron.php
|
||||
ERROR: Missing argument 'auth_user'
|
||||
|
||||
USAGE:
|
||||
|
||||
php cron.php --auth_user=<login> --auth_pwd=<password> [--param_file=<file>] [--verbose=1] [--debug=1] [--status_only=1]
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Access restricted to administrators
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Starting: 1772551316 (2026-03-03 16:21:56)
|
||||
@@ -0,0 +1,5 @@
|
||||
..
|
||||
..
|
||||
..
|
||||
..
|
||||
Exiting: 1772551316 (2026-03-03 16:21:56)
|
||||
Reference in New Issue
Block a user