1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00
pgbackrest/test/lib/BackRestTest/CommonTest.pm

360 lines
11 KiB
Perl
Raw Normal View History

#!/usr/bin/perl
####################################################################################################################################
# CommonTest.pm - Common globals used for testing
####################################################################################################################################
package BackRestTest::CommonTest;
####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
use warnings;
use english;
use Carp;
use File::Basename;
use Cwd 'abs_path';
2014-06-22 21:51:28 +03:00
use Config::IniFiles;
2014-07-03 02:15:21 +03:00
use IPC::Open3;
use lib dirname($0) . "/../lib";
use BackRest::Utility;
2014-06-22 21:51:28 +03:00
use BackRest::File;
use Exporter qw(import);
2014-07-03 02:15:21 +03:00
our @EXPORT = qw(BackRestTestCommon_Setup BackRestTestCommon_Execute BackRestTestCommon_ExecuteBackRest
BackRestTestCommon_ConfigCreate BackRestTestCommon_Run BackRestTestCommon_Cleanup
2014-06-22 21:51:28 +03:00
BackRestTestCommon_StanzaGet BackRestTestCommon_CommandMainGet BackRestTestCommon_CommandRemoteGet
2014-06-22 17:54:31 +03:00
BackRestTestCommon_HostGet BackRestTestCommon_UserGet BackRestTestCommon_GroupGet
BackRestTestCommon_UserBackRestGet BackRestTestCommon_TestPathGet BackRestTestCommon_DataPathGet
BackRestTestCommon_BackupPathGet BackRestTestCommon_ArchivePathGet BackRestTestCommon_DbPathGet
BackRestTestCommon_DbCommonPathGet BackRestTestCommon_DbPortGet);
my $strCommonStanza;
2014-06-22 21:51:28 +03:00
my $strCommonCommandMain;
my $strCommonCommandRemote;
2014-06-22 21:51:28 +03:00
my $strCommonCommandPsql;
my $strCommonHost;
my $strCommonUser;
my $strCommonGroup;
my $strCommonUserBackRest;
my $strCommonTestPath;
my $strCommonDataPath;
2014-06-22 21:51:28 +03:00
my $strCommonBackupPath;
my $strCommonArchivePath;
2014-06-22 21:51:28 +03:00
my $strCommonDbPath;
my $strCommonDbCommonPath;
my $iCommonDbPort;
my $iModuleTestRun;
my $bDryRun;
my $bNoCleanup;
####################################################################################################################################
# BackRestTestBackup_Run
####################################################################################################################################
sub BackRestTestCommon_Run
{
my $iRun = shift;
my $strLog = shift;
if (defined($iModuleTestRun) && $iModuleTestRun != $iRun)
{
return false;
}
&log(INFO, "run " . sprintf("%03d", $iRun) . " - " . $strLog);
if ($bDryRun)
{
return false;
}
return true;
}
####################################################################################################################################
# BackRestTestBackup_Cleanup
####################################################################################################################################
sub BackRestTestCommon_Cleanup
{
return !$bNoCleanup && !$bDryRun;
}
####################################################################################################################################
# BackRestTestBackup_Execute
####################################################################################################################################
sub BackRestTestCommon_Execute
{
my $strCommand = shift;
my $bRemote = shift;
my $bSuppressError = shift;
# Set defaults
$bRemote = defined($bRemote) ? $bRemote : false;
$bSuppressError = defined($bSuppressError) ? $bSuppressError : false;
if ($bRemote)
{
$strCommand = "ssh ${strCommonUserBackRest}\@${strCommonHost} '${strCommand}'";
}
2014-07-13 02:03:39 +03:00
# system($strCommand);
2014-07-03 02:15:21 +03:00
my $strError;
my $hError;
open($hError, '>', \$strError) or confess "unable to open handle to stderr string: $!\n";
my $strOut;
my $hOut;
open($hOut, '>', \$strOut) or confess "unable to open handle to stdout string: $!\n";
my $pId = open3(undef, $hOut, $hError, $strCommand);
2014-07-03 02:15:21 +03:00
# Wait for the process to finish and report any errors
waitpid($pId, 0);
my $iExitStatus = ${^CHILD_ERROR_NATIVE} >> 8;
if ($iExitStatus != 0 && !$bSuppressError)
{
while (my $strLine = readline($hError))
{
print $strLine;
}
confess &log(ERROR, "command '${strCommand}' returned " . $iExitStatus);
}
close($hError);
close($hOut);
2014-07-13 02:03:39 +03:00
# while (my $strLine = readline($hOut))
# {
# print $strLine;
# }
}
####################################################################################################################################
2014-06-22 17:54:31 +03:00
# BackRestTestCommon_Setup
####################################################################################################################################
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_Setup
{
my $iModuleTestRunParam = shift;
my $bDryRunParam = shift;
my $bNoCleanupParam = shift;
$strCommonStanza = "db";
2014-06-22 21:51:28 +03:00
$strCommonCommandMain = '/Users/dsteele/pg_backrest/bin/pg_backrest.pl';
$strCommonCommandRemote = '/Users/dsteele/pg_backrest/bin/pg_backrest_remote.pl';
$strCommonCommandPsql = '/Library/PostgreSQL/9.3/bin/psql -X %option%';
$strCommonHost = '127.0.0.1';
$strCommonUser = getpwuid($<);
$strCommonGroup = getgrgid($();
$strCommonUserBackRest = 'backrest';
2014-06-22 21:51:28 +03:00
$strCommonTestPath = dirname(abs_path($0)) . '/test';
$strCommonDataPath = dirname(abs_path($0)) . '/data';
2014-06-22 21:51:28 +03:00
$strCommonBackupPath = "${strCommonTestPath}/backrest";
$strCommonArchivePath = "${strCommonTestPath}/archive";
2014-06-22 21:51:28 +03:00
$strCommonDbPath = "${strCommonTestPath}/db";
$strCommonDbCommonPath = "${strCommonTestPath}/db/common";
$iCommonDbPort = 6543;
$iModuleTestRun = $iModuleTestRunParam;
$bDryRun = $bDryRunParam;
$bNoCleanup = $bNoCleanupParam;
}
2014-06-22 21:51:28 +03:00
####################################################################################################################################
# BackRestTestCommon_ConfigCreate
####################################################################################################################################
sub BackRestTestCommon_ConfigCreate
{
my $strLocal = shift;
2014-06-22 21:51:28 +03:00
my $strRemote = shift;
my $bCompress = shift;
my $bChecksum = shift;
my $bHardlink = shift;
my $iThreadMax = shift;
2014-07-13 02:03:39 +03:00
my $bArchiveLocal = shift;
my $bCompressAsync = shift;
# my $oParamHashRef = shift;
2014-06-24 01:54:00 +03:00
2014-06-22 21:51:28 +03:00
my %oParamHash;
tie %oParamHash, 'Config::IniFiles';
if (defined($strRemote))
{
$oParamHash{'global:command'}{'remote'} = $strCommonCommandRemote;
}
2014-06-22 21:51:28 +03:00
$oParamHash{'global:command'}{'psql'} = $strCommonCommandPsql;
if (defined($strRemote) && $strRemote eq REMOTE_BACKUP)
{
$oParamHash{'global:backup'}{'host'} = $strCommonHost;
$oParamHash{'global:backup'}{'user'} = $strCommonUserBackRest;
}
elsif (defined($strRemote) && $strRemote eq REMOTE_DB)
{
$oParamHash{$strCommonStanza}{'host'} = $strCommonHost;
$oParamHash{$strCommonStanza}{'user'} = $strCommonUser;
}
2014-06-24 01:54:00 +03:00
$oParamHash{'global:log'}{'level-console'} = 'error';
$oParamHash{'global:log'}{'level-file'} = 'trace';
if ($strLocal eq REMOTE_BACKUP)
{
if (defined($bHardlink) && $bHardlink)
{
$oParamHash{'global:backup'}{'hardlink'} = 'y';
}
2014-06-22 21:51:28 +03:00
}
elsif ($strLocal eq REMOTE_DB)
{
if (defined($strRemote))
{
$oParamHash{'global:log'}{'level-console'} = 'trace';
}
if ($bArchiveLocal)
{
$oParamHash{'global:archive'}{path} = BackRestTestCommon_ArchivePathGet();
if (!$bCompressAsync)
{
$oParamHash{'global:archive'}{'compress_async'} = 'n';
}
}
}
else
{
confess "invalid local type ${strLocal}";
}
2014-06-22 21:51:28 +03:00
if (($strLocal eq REMOTE_BACKUP) || ($strLocal eq REMOTE_DB && !defined($strRemote)))
{
$oParamHash{'db:command:option'}{'psql'} = "--port=${iCommonDbPort}";
}
if (defined($bCompress) && !$bCompress)
{
$oParamHash{'global:backup'}{'compress'} = 'n';
}
if (defined($bChecksum) && !$bChecksum)
2014-07-13 02:03:39 +03:00
{
$oParamHash{'global:backup'}{'checksum'} = 'n';
2014-07-13 02:03:39 +03:00
}
2014-06-24 01:54:00 +03:00
$oParamHash{$strCommonStanza}{'path'} = $strCommonDbCommonPath;
2014-06-22 21:51:28 +03:00
$oParamHash{'global:backup'}{'path'} = $strCommonBackupPath;
2014-06-24 01:54:00 +03:00
if (defined($iThreadMax))
2014-06-28 21:32:34 +03:00
{
$oParamHash{'global:backup'}{'thread-max'} = $iThreadMax;
2014-06-28 21:32:34 +03:00
}
# foreach my $strSection (keys $oParamHashRef)
# {
# foreach my $strKey (keys ${$oParamHashRef}{$strSection})
# {
# $oParamHash{$strSection}{$strKey} = ${$oParamHashRef}{$strSection}{$strKey};
# }
# }
# Write out the configuration file
my $strFile = BackRestTestCommon_TestPathGet() . '/pg_backrest.conf';
2014-06-22 21:51:28 +03:00
tied(%oParamHash)->WriteConfig($strFile) or die "could not write config file ${strFile}";
chmod(0660, $strFile) or die "unable to set permissions for ${strFile}";
2014-06-24 01:54:00 +03:00
# Move the configuration file based on local
if ($strLocal eq 'db')
{
rename($strFile, BackRestTestCommon_DbPathGet() . '/pg_backrest.conf')
or die "unable to move ${strFile} to " . BackRestTestCommon_DbPathGet() . '/pg_backrest.conf path';
}
elsif ($strLocal eq 'backup' && !defined($strRemote))
{
rename($strFile, BackRestTestCommon_BackupPathGet() . '/pg_backrest.conf')
or die "unable to move ${strFile} to " . BackRestTestCommon_BackupPathGet() . '/pg_backrest.conf path';
}
else
{
2014-07-03 02:15:21 +03:00
BackRestTestCommon_Execute("mv $strFile " . BackRestTestCommon_BackupPathGet() . '/pg_backrest.conf', true);
}
2014-06-22 21:51:28 +03:00
}
####################################################################################################################################
# Get Methods
####################################################################################################################################
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_StanzaGet
{
return $strCommonStanza;
}
2014-06-22 21:51:28 +03:00
sub BackRestTestCommon_CommandMainGet
{
return $strCommonCommandMain;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_CommandRemoteGet
{
return $strCommonCommandRemote;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_HostGet
{
return $strCommonHost;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_UserGet
{
return $strCommonUser;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_GroupGet
{
return $strCommonGroup;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_UserBackRestGet
{
return $strCommonUserBackRest;
}
2014-06-22 17:54:31 +03:00
sub BackRestTestCommon_TestPathGet
{
return $strCommonTestPath;
}
sub BackRestTestCommon_DataPathGet
{
return $strCommonDataPath;
}
2014-06-22 21:51:28 +03:00
sub BackRestTestCommon_BackupPathGet
{
return $strCommonBackupPath;
}
sub BackRestTestCommon_ArchivePathGet
{
return $strCommonArchivePath;
}
2014-06-22 21:51:28 +03:00
sub BackRestTestCommon_DbPathGet
{
return $strCommonDbPath;
}
sub BackRestTestCommon_DbCommonPathGet
{
return $strCommonDbCommonPath;
}
sub BackRestTestCommon_DbPortGet
{
return $iCommonDbPort;
}
1;