You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-11-06 08:49:29 +02:00
Backup unit test able to create and drop clusters.
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# BackupTest.pl - Unit Tests for BackRest::Backup
|
# BackupTest.pl - Unit Tests for BackRest::File
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
use BackRestTest::BackupTest;
|
package BackRestTest::BackupTest;
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# Perl includes
|
# Perl includes
|
||||||
@@ -13,68 +13,95 @@ use english;
|
|||||||
use Carp;
|
use Carp;
|
||||||
|
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
use Cwd 'abs_path';
|
# use Cwd 'abs_path';
|
||||||
use File::stat;
|
# use File::stat;
|
||||||
use Fcntl ':mode';
|
# use Fcntl ':mode';
|
||||||
use Scalar::Util 'blessed';
|
# use Scalar::Util 'blessed';
|
||||||
|
|
||||||
use lib dirname($0) . "/../lib";
|
use lib dirname($0) . "/../lib";
|
||||||
use BackRest::Utility;
|
use BackRest::Utility;
|
||||||
use BackRest::File;
|
use BackRest::File;
|
||||||
use BackRest::Remote;
|
use BackRest::Remote;
|
||||||
|
|
||||||
|
use BackRestTest::CommonTest;
|
||||||
|
|
||||||
use Exporter qw(import);
|
use Exporter qw(import);
|
||||||
our @EXPORT = qw(BackRestFileTest);
|
our @EXPORT = qw(BackRestTestBackup_Test);
|
||||||
|
|
||||||
# my $strTestPath;
|
my $strTestPath;
|
||||||
# my $strHost;
|
my $strHost;
|
||||||
# my $strUserBackRest;
|
my $strUserBackRest;
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# BackRestFileBackupSetup
|
# BackRestTestBackup_ClusterDrop
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
sub BackRestFileBackupSetup
|
sub BackRestTestBackup_ClusterDrop
|
||||||
|
{
|
||||||
|
my $strPath = shift;
|
||||||
|
|
||||||
|
# If the db directory already exists, stop the cluster and remove the directory
|
||||||
|
if (-e $strPath . "/postmaster.pid")
|
||||||
|
{
|
||||||
|
BackRestTestCommon_Execute("pg_ctl stop -D $strPath -w -s -m fast");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################################################################################################
|
||||||
|
# BackRestTestBackup_ClusterCreate
|
||||||
|
####################################################################################################################################
|
||||||
|
sub BackRestTestBackup_ClusterCreate
|
||||||
|
{
|
||||||
|
my $strPath = shift;
|
||||||
|
my $iPort = shift;
|
||||||
|
|
||||||
|
BackRestTestCommon_Execute("initdb -D $strPath -A trust");
|
||||||
|
BackRestTestCommon_Execute("pg_ctl start -o \"-c port=$iPort\" -D $strPath -l $strPath/postgresql.log -w -s");
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################################################################################################
|
||||||
|
# BackRestTestBackup_Setup
|
||||||
|
####################################################################################################################################
|
||||||
|
sub BackRestTestBackup_Setup
|
||||||
{
|
{
|
||||||
my $bPrivate = shift;
|
|
||||||
my $bDropOnly = shift;
|
my $bDropOnly = shift;
|
||||||
|
|
||||||
my $strTestPath = BackRestCommonTestPathGet();
|
BackRestTestBackup_ClusterDrop($strTestPath . "/db/common");
|
||||||
|
|
||||||
# Remove the backrest private directory
|
# Remove the backrest private directory
|
||||||
if (-e "${strTestPath}/private")
|
if (-e "${strTestPath}/backrest")
|
||||||
{
|
{
|
||||||
system("ssh ${strUserBackRest}\@${strHost} 'rm -rf ${strTestPath}/private'");
|
BackRestTestCommon_ExecuteBackRest("rm -rf ${strTestPath}/backrest", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove the test directory
|
# Remove the test directory
|
||||||
system("rm -rf ${strTestPath}") == 0 or die 'unable to drop test path';
|
system("rm -rf ${strTestPath}") == 0 or die 'unable to remove ${strTestPath} path';
|
||||||
|
|
||||||
if (!defined($bDropOnly) || !$bDropOnly)
|
if (!defined($bDropOnly) || !$bDropOnly)
|
||||||
{
|
{
|
||||||
# Create the test directory
|
# Create the test directory
|
||||||
mkdir($strTestPath, oct("0770")) or confess "Unable to create test directory";
|
mkdir($strTestPath, oct("0770")) or confess "Unable to create ${strTestPath} path";
|
||||||
|
|
||||||
# Create the backrest private directory
|
# Create the db directory
|
||||||
if (defined($bPrivate) && $bPrivate)
|
mkdir($strTestPath . "/db", oct("0700")) or confess "Unable to create ${strTestPath}/db path";
|
||||||
{
|
|
||||||
system("ssh backrest\@${strHost} 'mkdir -m 700 ${strTestPath}/private'") == 0 or die 'unable to create test/private path';
|
# Create the db/common directory
|
||||||
}
|
mkdir($strTestPath . "/db/common") or confess "Unable to create ${strTestPath}/db/common path";
|
||||||
|
|
||||||
|
# Create the cluster
|
||||||
|
BackRestTestBackup_ClusterCreate($strTestPath . "/db/common", BackRestTestCommon_DbPortGet);
|
||||||
|
|
||||||
|
# Create the backrest directory
|
||||||
|
BackRestTestCommon_ExecuteBackRest("mkdir -m 700 ${strTestPath}/backrest")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# BackRestBackupTest
|
# BackRestTestBackup_Test
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
sub BackRestBackupTest
|
sub BackRestTestBackup_Test
|
||||||
{
|
{
|
||||||
my $strStanza = shift;
|
|
||||||
my $strCommand = shift;
|
|
||||||
my $strHost = shift;
|
|
||||||
my $strUser = shift;
|
|
||||||
my $strGroup = shift;
|
|
||||||
my $strUserBackRest = shift;
|
|
||||||
my $strTestPath = shift;
|
|
||||||
my $strTest = shift;
|
my $strTest = shift;
|
||||||
|
my $iTestRun = shift;
|
||||||
|
|
||||||
# If no test was specified, then run them all
|
# If no test was specified, then run them all
|
||||||
if (!defined($strTest))
|
if (!defined($strTest))
|
||||||
@@ -84,33 +111,31 @@ sub BackRestBackupTest
|
|||||||
|
|
||||||
# Setup test variables
|
# Setup test variables
|
||||||
my $iRun;
|
my $iRun;
|
||||||
my $strTestPath = BackRestCommonTestPathGet();
|
$strTestPath = BackRestTestCommon_TestPathGet();
|
||||||
my $strStanza = BackRestCommonStanzaGet();
|
my $strStanza = BackRestTestCommon_StanzaGet();
|
||||||
# my $strHost = "127.0.0.1";
|
my $strUser = BackRestTestCommon_UserGet();
|
||||||
# my $strUser = getpwuid($<);
|
my $strGroup = BackRestTestCommon_GroupGet();
|
||||||
# my $strGroup = getgrgid($();
|
$strHost = BackRestTestCommon_HostGet();
|
||||||
# $strUserBackRest = 'backrest';
|
$strUserBackRest = BackRestTestCommon_UserBackRestGet();
|
||||||
|
|
||||||
# Print test parameters
|
# Print test banner
|
||||||
&log(INFO, "Testing with test_path = ${strTestPath}, host = ${strHost}, user = ${strUser}, group = ${strGroup}");
|
&log(INFO, "BACKUP MODULE ******************************************************************");
|
||||||
|
|
||||||
&log(INFO, "FILE MODULE ********************************************************************");
|
BackRestTestBackup_Setup();
|
||||||
|
|
||||||
system("ssh backrest\@${strHost} 'rm -rf ${strTestPath}/private'");
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------------------------------------------------------
|
||||||
# Create remote
|
# Create remote
|
||||||
#-------------------------------------------------------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------------------------------------------------------
|
||||||
my $oRemote = BackRest::Remote->new
|
# my $oRemote = BackRest::Remote->new
|
||||||
(
|
# (
|
||||||
strHost => BackRestCommonHostGet(),
|
# strHost => $strHost,
|
||||||
strUser => BackRestCommonUserGet(),
|
# strUser => $strUser,
|
||||||
strCommand => $strCommand,
|
# strCommand => BackRestTestCommon_CommandRemoteGet(),
|
||||||
);
|
# );
|
||||||
|
|
||||||
# #-------------------------------------------------------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------------------------------------------------------
|
||||||
# # Test path_create()
|
# Test path_create()
|
||||||
# #-------------------------------------------------------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------------------------------------------------------
|
||||||
# if ($strTest eq 'all' || $strTest eq 'path_create')
|
# if ($strTest eq 'all' || $strTest eq 'path_create')
|
||||||
# {
|
# {
|
||||||
# $iRun = 0;
|
# $iRun = 0;
|
||||||
@@ -123,8 +148,8 @@ sub BackRestBackupTest
|
|||||||
# # Create the file object
|
# # Create the file object
|
||||||
# my $oFile = (BackRest::File->new
|
# my $oFile = (BackRest::File->new
|
||||||
# (
|
# (
|
||||||
# strStanza => "db",
|
# strStanza => $strStanza,
|
||||||
# strBackupPath => ${strTestPath},
|
# strBackupPath => $strTestPath,
|
||||||
# strRemote => $bRemote ? 'backup' : undef,
|
# strRemote => $bRemote ? 'backup' : undef,
|
||||||
# oRemote => $bRemote ? $oRemote : undef
|
# oRemote => $bRemote ? $oRemote : undef
|
||||||
# ))->clone();
|
# ))->clone();
|
||||||
@@ -142,11 +167,16 @@ sub BackRestBackupTest
|
|||||||
#
|
#
|
||||||
# $iRun++;
|
# $iRun++;
|
||||||
#
|
#
|
||||||
|
# if (defined($iTestRun) && $iTestRun != $iRun)
|
||||||
|
# {
|
||||||
|
# next;
|
||||||
|
# }
|
||||||
|
#
|
||||||
# &log(INFO, "run ${iRun} - " .
|
# &log(INFO, "run ${iRun} - " .
|
||||||
# "remote ${bRemote}, exists ${bExists}, error ${bError}, permission ${bPermission}");
|
# "remote ${bRemote}, exists ${bExists}, error ${bError}, permission ${bPermission}");
|
||||||
#
|
#
|
||||||
# # Setup test directory
|
# # Setup test directory
|
||||||
# BackRestFileTestSetup($bError);
|
# BackRestTestFile_Setup($bError);
|
||||||
#
|
#
|
||||||
# mkdir("$strTestPath/backup") or confess "Unable to create test/backup directory";
|
# mkdir("$strTestPath/backup") or confess "Unable to create test/backup directory";
|
||||||
# mkdir("$strTestPath/backup/db") or confess "Unable to create test/backup/db directory";
|
# mkdir("$strTestPath/backup/db") or confess "Unable to create test/backup/db directory";
|
||||||
@@ -158,13 +188,6 @@ sub BackRestBackupTest
|
|||||||
# if ($bPermission)
|
# if ($bPermission)
|
||||||
# {
|
# {
|
||||||
# $strPermission = "0700";
|
# $strPermission = "0700";
|
||||||
#
|
|
||||||
# # # Make sure that we are not testing with the default permission
|
|
||||||
# # if ($strPermission eq $oFile->{strDefaultPathPermission})
|
|
||||||
# # {
|
|
||||||
# # confess 'cannot set test permission ${strPermission} equal to default permission' .
|
|
||||||
# # $oFile->{strDefaultPathPermission};
|
|
||||||
# # }
|
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
# # If not exists then set the path to something bogus
|
# # If not exists then set the path to something bogus
|
||||||
@@ -231,8 +254,8 @@ sub BackRestBackupTest
|
|||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
#
|
|
||||||
# BackRestFileTestSetup(false, true);
|
BackRestTestBackup_Setup(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|||||||
@@ -15,10 +15,14 @@ use Carp;
|
|||||||
use File::Basename;
|
use File::Basename;
|
||||||
use Cwd 'abs_path';
|
use Cwd 'abs_path';
|
||||||
|
|
||||||
|
use lib dirname($0) . "/../lib";
|
||||||
|
use BackRest::Utility;
|
||||||
|
|
||||||
use Exporter qw(import);
|
use Exporter qw(import);
|
||||||
our @EXPORT = qw(BackRestTestCommon_Setup BackRestTestCommon_StanzaGet BackRestTestCommon_CommandRemoteGet
|
our @EXPORT = qw(BackRestTestCommon_Setup BackRestTestCommon_Execute BackRestTestCommon_ExecuteBackRest
|
||||||
|
BackRestTestCommon_StanzaGet BackRestTestCommon_CommandRemoteGet
|
||||||
BackRestTestCommon_HostGet BackRestTestCommon_UserGet BackRestTestCommon_GroupGet
|
BackRestTestCommon_HostGet BackRestTestCommon_UserGet BackRestTestCommon_GroupGet
|
||||||
BackRestTestCommon_UserBackRestGet BackRestTestCommon_TestPathGet);
|
BackRestTestCommon_UserBackRestGet BackRestTestCommon_TestPathGet BackRestTestCommon_DbPortGet);
|
||||||
|
|
||||||
my $strCommonStanza;
|
my $strCommonStanza;
|
||||||
my $strCommonCommandRemote;
|
my $strCommonCommandRemote;
|
||||||
@@ -27,6 +31,37 @@ my $strCommonUser;
|
|||||||
my $strCommonGroup;
|
my $strCommonGroup;
|
||||||
my $strCommonUserBackRest;
|
my $strCommonUserBackRest;
|
||||||
my $strCommonTestPath;
|
my $strCommonTestPath;
|
||||||
|
my $iCommonDbPort;
|
||||||
|
|
||||||
|
####################################################################################################################################
|
||||||
|
# BackRestTestBackup_Execute
|
||||||
|
####################################################################################################################################
|
||||||
|
sub BackRestTestCommon_Execute
|
||||||
|
{
|
||||||
|
my $strCommand = shift;
|
||||||
|
my $bSuppressError = shift;
|
||||||
|
|
||||||
|
if (system($strCommand) != 0)
|
||||||
|
{
|
||||||
|
if (!defined($bSuppressError) || !$bSuppressError)
|
||||||
|
{
|
||||||
|
confess &log(ERROR, "unable to execute command: ${strCommand}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
####################################################################################################################################
|
||||||
|
# BackRestTestBackup_ExecuteBackRest
|
||||||
|
####################################################################################################################################
|
||||||
|
sub BackRestTestCommon_ExecuteBackRest
|
||||||
|
{
|
||||||
|
my $strCommand = shift;
|
||||||
|
my $bSuppressError = shift;
|
||||||
|
|
||||||
|
$strCommand = "ssh ${strCommonUserBackRest}\@${strCommonHost} '${strCommand}'";
|
||||||
|
|
||||||
|
BackRestTestCommon_Execute($strCommand, $bSuppressError);
|
||||||
|
}
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# BackRestTestCommon_Setup
|
# BackRestTestCommon_Setup
|
||||||
@@ -40,6 +75,7 @@ sub BackRestTestCommon_Setup
|
|||||||
$strCommonGroup = getgrgid($();
|
$strCommonGroup = getgrgid($();
|
||||||
$strCommonUserBackRest = 'backrest';
|
$strCommonUserBackRest = 'backrest';
|
||||||
$strCommonTestPath = dirname(abs_path($0)) . "/test";
|
$strCommonTestPath = dirname(abs_path($0)) . "/test";
|
||||||
|
$iCommonDbPort = 6543;
|
||||||
}
|
}
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
@@ -80,4 +116,9 @@ sub BackRestTestCommon_TestPathGet
|
|||||||
return $strCommonTestPath;
|
return $strCommonTestPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub BackRestTestCommon_DbPortGet
|
||||||
|
{
|
||||||
|
return $iCommonDbPort;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ sub BackRestTestFile_Test
|
|||||||
$strHost = BackRestTestCommon_HostGet();
|
$strHost = BackRestTestCommon_HostGet();
|
||||||
$strUserBackRest = BackRestTestCommon_UserBackRestGet();
|
$strUserBackRest = BackRestTestCommon_UserBackRestGet();
|
||||||
|
|
||||||
# Print test parameters
|
# Print test banner
|
||||||
&log(INFO, "FILE MODULE ********************************************************************");
|
&log(INFO, "FILE MODULE ********************************************************************");
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ use BackRest::Utility;
|
|||||||
use lib dirname($0) . "/lib";
|
use lib dirname($0) . "/lib";
|
||||||
use BackRestTest::CommonTest;
|
use BackRestTest::CommonTest;
|
||||||
use BackRestTest::FileTest;
|
use BackRestTest::FileTest;
|
||||||
|
use BackRestTest::BackupTest;
|
||||||
|
|
||||||
####################################################################################################################################
|
####################################################################################################################################
|
||||||
# Command line parameters
|
# Command line parameters
|
||||||
@@ -99,4 +100,9 @@ if ($strModule eq 'all' || $strModule eq "file")
|
|||||||
BackRestTestFile_Test($strModuleTest, $iModuleTestRun);
|
BackRestTestFile_Test($strModuleTest, $iModuleTestRun);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($strModule eq 'all' || $strModule eq "backup")
|
||||||
|
{
|
||||||
|
BackRestTestBackup_Test($strModuleTest, $iModuleTestRun);
|
||||||
|
}
|
||||||
|
|
||||||
&log(ASSERT, "TESTS COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)");
|
&log(ASSERT, "TESTS COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)");
|
||||||
|
|||||||
Reference in New Issue
Block a user