N°2793 log rotation test : fix timezone issues

This commit is contained in:
odain
2020-03-04 11:43:26 +01:00
committed by Pierre Goiffon
parent 6675d7d42a
commit c06f8e9a98
3 changed files with 46 additions and 13 deletions

View File

@@ -406,7 +406,7 @@ class Config
'log_filename_builder_impl' => array(
'type' => 'string',
'description' => 'Name of the iLogFileNameBuilder to use',
'default' => 'MonthlyRotatingLogFileNameBuilder',
'default' => 'timezone',
'value' => '',
'source_of_value' => '',
'show_in_conf_sample' => false,

View File

@@ -128,6 +128,10 @@ abstract class RotatingLogFileNameBuilder implements iLogFileNameBuilder
*/
public function CheckAndRotateLogFile()
{
$oConfig = utils::GetConfig();
$sItopTimeZone = $oConfig->Get('timezone');
$timezone = new DateTimeZone($sItopTimeZone);
if ($this->GetLastModifiedDateForFile() === null)
{
if (!$this->IsLogFileExists())
@@ -140,10 +144,12 @@ abstract class RotatingLogFileNameBuilder implements iLogFileNameBuilder
{
return;
}
$this->SetLastModifiedDateForFile(DateTime::createFromFormat('U', $iLogDateLastModifiedTimeStamp));
$oDateTime = DateTime::createFromFormat('U', $iLogDateLastModifiedTimeStamp);
$oDateTime->setTimezone($timezone);
$this->SetLastModifiedDateForFile($oDateTime);
}
$oNow = new DateTime();
$oNow = new DateTime('now', $timezone);
$bShouldRotate = $this->ShouldRotate($this->GetLastModifiedDateForFile(), $oNow);
if (!$bShouldRotate)
{
@@ -276,10 +282,22 @@ class DailyRotatingLogFileNameBuilder extends RotatingLogFileNameBuilder
*/
public function ShouldRotate($oLogFileLastModified, $oNow)
{
$oInterval = $oNow->diff($oLogFileLastModified);
$iDaysDiff = $oInterval->d;
$iLogYear = $oLogFileLastModified->format('Y');
$iLogDay = $oLogFileLastModified->format('z');
$iNowYear = $oNow->format('Y');
$iNowDay = $oNow->format('z');
return $iDaysDiff > 0;
if ($iLogYear !== $iNowYear)
{
return true;
}
if ($iLogDay !== $iNowDay)
{
return true;
}
return false;
}
/**

View File

@@ -17,6 +17,9 @@ use DateTime;
*/
class LogFileNameBuilderTest extends ItopTestCase
{
const TEST_LOGFILE_PREFIX = 'fileNameBuilder.test';
const TEST_LOGFILE_EXTENSION = 'log';
/**
* @param $sLogFile
* @param \DateTime $oNewModificationDate
@@ -36,9 +39,21 @@ class LogFileNameBuilderTest extends ItopTestCase
require_once APPROOT.'core/log.class.inc.php';
}
protected function tearDown()
{
parent::tearDown();
// remove log files created in the test
$aTestLogFiles = glob(__DIR__.DIRECTORY_SEPARATOR.self::TEST_LOGFILE_PREFIX.'*.'.self::TEST_LOGFILE_EXTENSION);
foreach ($aTestLogFiles as $sLogFile)
{
unlink($sLogFile);
}
}
public function testCheckAndRotateLogFile()
{
$sLogFile = __DIR__.DIRECTORY_SEPARATOR.'fileNameBuilder.test.log';
$sLogFile = __DIR__.DIRECTORY_SEPARATOR.self::TEST_LOGFILE_PREFIX.'.'.self::TEST_LOGFILE_EXTENSION;
$oFileBuilder = new DailyRotatingLogFileNameBuilder($sLogFile);
if (file_exists($sLogFile))
@@ -62,29 +77,29 @@ class LogFileNameBuilderTest extends ItopTestCase
$this->assertFileExists($sLogFile, 'Test log file modification date is today, original file still exists after rotation call');
$this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet');
// changing modification date, but do not reset cached filebuilder date => no change
$oTimeYesterday = new DateTime('yesterday');
touch($sLogFile, $oTimeYesterday->getTimestamp());
$sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday);
$this->ChangeFileModificationDate($sLogFile, $oTimeYesterday);
// changing modification date, but do not reset cached filebuilder date => no change
$oFileBuilder->CheckAndRotateLogFile();
$this->assertFileExists($sLogFile, 'Test log file modification date is yesterday but filebuilder use its cache, original file still exists after rotation call');
$this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet');
// changing modification date AND resetting filebuilder date cache
$oTimeYesterday = new DateTime('yesterday');
$this->ChangeFileModificationDate($sLogFile, $oTimeYesterday);
$oFileBuilder->ResetLastModifiedDateForFile();
$sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday);
$oFileBuilder->CheckAndRotateLogFile();
$this->assertFileNotExists($sLogFile, 'Test log file modification date is yesterday, file rotated !');
$this->assertFileExists($sRotatedLogFile, 'Rotation was done');
unlink($sRotatedLogFile);
// file cleanup will be done in tearDown !
}
public function ShouldRotateProvider()
{
return array(
'DAILY Same day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 15:42', false),
'DAILY Same week, different day less 24h diff' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 12:00', '2020-02-02 09:00', true),
'DAILY Same week, different day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-02 00:00', true),
'DAILY 1 week diff' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true),
'WEEKLY Same week' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 00:00', false),