2014-06-22 17:30:17 +03:00
|
|
|
#!/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-01 01:35:05 +03:00
|
|
|
use IPC::Run qw(run);
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
use lib dirname($0) . "/../lib";
|
|
|
|
use BackRest::Utility;
|
2014-06-22 21:51:28 +03:00
|
|
|
use BackRest::File;
|
2014-06-22 18:56:01 +03:00
|
|
|
|
2014-06-22 17:30:17 +03:00
|
|
|
use Exporter qw(import);
|
2014-07-03 01:58:38 +03:00
|
|
|
our @EXPORT = qw(BackRestTestCommon_Setup BackRestTestCommon_Execute BackRestTestCommon_ExecuteOld BackRestTestCommon_ExecuteBackRest
|
2014-06-22 21:51:28 +03:00
|
|
|
BackRestTestCommon_ConfigCreate
|
|
|
|
BackRestTestCommon_StanzaGet BackRestTestCommon_CommandMainGet BackRestTestCommon_CommandRemoteGet
|
2014-06-22 17:54:31 +03:00
|
|
|
BackRestTestCommon_HostGet BackRestTestCommon_UserGet BackRestTestCommon_GroupGet
|
2014-06-22 21:51:28 +03:00
|
|
|
BackRestTestCommon_UserBackRestGet BackRestTestCommon_TestPathGet BackRestTestCommon_BackupPathGet
|
2014-06-29 17:53:39 +03:00
|
|
|
BackRestTestCommon_ArchivePathGet BackRestTestCommon_DbPathGet BackRestTestCommon_DbCommonPathGet
|
|
|
|
BackRestTestCommon_DbPortGet);
|
2014-06-22 17:30:17 +03:00
|
|
|
|
|
|
|
my $strCommonStanza;
|
2014-06-22 21:51:28 +03:00
|
|
|
my $strCommonCommandMain;
|
2014-06-22 17:30:17 +03:00
|
|
|
my $strCommonCommandRemote;
|
2014-06-22 21:51:28 +03:00
|
|
|
my $strCommonCommandPsql;
|
2014-06-22 17:30:17 +03:00
|
|
|
my $strCommonHost;
|
|
|
|
my $strCommonUser;
|
|
|
|
my $strCommonGroup;
|
|
|
|
my $strCommonUserBackRest;
|
|
|
|
my $strCommonTestPath;
|
2014-06-22 21:51:28 +03:00
|
|
|
my $strCommonBackupPath;
|
2014-06-29 17:53:39 +03:00
|
|
|
my $strCommonArchivePath;
|
2014-06-22 21:51:28 +03:00
|
|
|
my $strCommonDbPath;
|
|
|
|
my $strCommonDbCommonPath;
|
2014-06-22 18:56:01 +03:00
|
|
|
my $iCommonDbPort;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# BackRestTestBackup_Execute
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BackRestTestCommon_Execute
|
|
|
|
{
|
2014-07-03 01:58:38 +03:00
|
|
|
my @stryCommand = shift; # Command to execute
|
2014-07-01 01:35:05 +03:00
|
|
|
my $bRemote = shift; # Execute on remote? This will use the defined BackRest user
|
|
|
|
my $bSuppressError = shift; # Ignore any errors
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Set defaults
|
|
|
|
$bRemote = defined($bRemote) ? $bRemote : false;
|
|
|
|
$bSuppressError = defined($bSuppressError) ? $bSuppressError : false;
|
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# If remote then run the command through ssh
|
2014-06-29 17:53:39 +03:00
|
|
|
if ($bRemote)
|
|
|
|
{
|
2014-07-03 01:58:38 +03:00
|
|
|
confess 'remote not supported';
|
|
|
|
#$strCommand = "ssh ${strCommonUserBackRest}\@${strCommonHost} '${strCommand}'";
|
2014-06-29 17:53:39 +03:00
|
|
|
}
|
|
|
|
|
2014-07-01 01:35:05 +03:00
|
|
|
# Run the command
|
2014-07-03 01:58:38 +03:00
|
|
|
my $strOutput;
|
|
|
|
my $strError;
|
|
|
|
|
|
|
|
eval
|
|
|
|
{
|
|
|
|
run(@stryCommand, '>', \$strOutput, '2>', \$strError);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@)
|
2014-06-22 18:56:01 +03:00
|
|
|
{
|
2014-07-01 01:35:05 +03:00
|
|
|
if ($bSuppressError)
|
2014-06-22 18:56:01 +03:00
|
|
|
{
|
2014-07-01 01:35:05 +03:00
|
|
|
return;
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
2014-07-03 01:58:38 +03:00
|
|
|
|
|
|
|
confess &log(ERROR, "command \"@{stryCommand}\" returned: " . $@);
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
2014-07-03 01:58:38 +03:00
|
|
|
}
|
2014-07-01 01:35:05 +03:00
|
|
|
|
2014-07-03 01:58:38 +03:00
|
|
|
sub BackRestTestCommon_ExecuteOld
|
|
|
|
{
|
|
|
|
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}'";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (system($strCommand) != 0)
|
|
|
|
{
|
|
|
|
if (!$bSuppressError)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unable to execute command: ${strCommand}");
|
|
|
|
}
|
|
|
|
}
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
|
|
|
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 17:54:31 +03:00
|
|
|
# BackRestTestCommon_Setup
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_Setup
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
$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';
|
2014-06-22 17:30:17 +03:00
|
|
|
$strCommonUser = getpwuid($<);
|
|
|
|
$strCommonGroup = getgrgid($();
|
|
|
|
$strCommonUserBackRest = 'backrest';
|
2014-06-22 21:51:28 +03:00
|
|
|
$strCommonTestPath = dirname(abs_path($0)) . '/test';
|
|
|
|
$strCommonBackupPath = "${strCommonTestPath}/backrest";
|
2014-06-29 17:53:39 +03:00
|
|
|
$strCommonArchivePath = "${strCommonTestPath}/archive";
|
2014-06-22 21:51:28 +03:00
|
|
|
$strCommonDbPath = "${strCommonTestPath}/db";
|
|
|
|
$strCommonDbCommonPath = "${strCommonTestPath}/db/common";
|
2014-06-22 18:56:01 +03:00
|
|
|
$iCommonDbPort = 6543;
|
2014-06-22 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BackRestTestCommon_ConfigCreate
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BackRestTestCommon_ConfigCreate
|
|
|
|
{
|
2014-06-22 23:05:00 +03:00
|
|
|
my $strLocal = shift;
|
2014-06-22 21:51:28 +03:00
|
|
|
my $strRemote = 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';
|
|
|
|
|
|
|
|
$oParamHash{'global:command'}{'remote'} = $strCommonCommandRemote;
|
|
|
|
$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-22 23:05:00 +03:00
|
|
|
}
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-22 23:05:00 +03:00
|
|
|
if ($strLocal eq REMOTE_BACKUP)
|
|
|
|
{
|
2014-06-22 21:51:28 +03:00
|
|
|
$oParamHash{'db:command:option'}{'psql'} = "--port=${iCommonDbPort}";
|
|
|
|
}
|
2014-06-22 23:05:00 +03:00
|
|
|
elsif ($strLocal eq REMOTE_DB)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess "invalid local type ${strLocal}";
|
|
|
|
}
|
2014-06-22 21:51:28 +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-28 18:47:21 +03:00
|
|
|
$oParamHash{'global:backup'}{'thread-max'} = '8';
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-28 21:32:34 +03:00
|
|
|
$oParamHash{'global:log'}{'level-console'} = 'error';
|
2014-06-22 23:05:00 +03:00
|
|
|
$oParamHash{'global:log'}{'level-file'} = 'trace';
|
2014-06-22 21:51:28 +03:00
|
|
|
|
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};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# 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}";
|
2014-06-29 17:53:39 +03:00
|
|
|
chmod(0660, $strFile) or die "unable to set permissions for ${strFile}";
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-29 17:53:39 +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 01:58:38 +03:00
|
|
|
BackRestTestCommon_ExecuteOld("mv $strFile " . BackRestTestCommon_BackupPathGet() . '/pg_backrest.conf', true);
|
2014-06-29 17:53:39 +03:00
|
|
|
}
|
2014-06-22 21:51:28 +03:00
|
|
|
}
|
|
|
|
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# Get Methods
|
|
|
|
####################################################################################################################################
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_StanzaGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
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
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonCommandRemote;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_HostGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonHost;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_UserGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonUser;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_GroupGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonGroup;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_UserBackRestGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonUserBackRest;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:54:31 +03:00
|
|
|
sub BackRestTestCommon_TestPathGet
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
return $strCommonTestPath;
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
sub BackRestTestCommon_BackupPathGet
|
|
|
|
{
|
|
|
|
return $strCommonBackupPath;
|
|
|
|
}
|
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
sub BackRestTestCommon_ArchivePathGet
|
|
|
|
{
|
|
|
|
return $strCommonArchivePath;
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
sub BackRestTestCommon_DbPathGet
|
|
|
|
{
|
|
|
|
return $strCommonDbPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub BackRestTestCommon_DbCommonPathGet
|
|
|
|
{
|
|
|
|
return $strCommonDbCommonPath;
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
sub BackRestTestCommon_DbPortGet
|
|
|
|
{
|
|
|
|
return $iCommonDbPort;
|
|
|
|
}
|
|
|
|
|
2014-06-22 17:30:17 +03:00
|
|
|
1;
|