2014-06-22 17:30:17 +03:00
|
|
|
#!/usr/bin/perl
|
|
|
|
####################################################################################################################################
|
2014-06-22 18:56:01 +03:00
|
|
|
# BackupTest.pl - Unit Tests for BackRest::File
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 18:56:01 +03:00
|
|
|
package BackRestTest::BackupTest;
|
2014-06-22 17:30:17 +03:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Perl includes
|
|
|
|
####################################################################################################################################
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use english;
|
|
|
|
use Carp;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
use lib dirname($0) . "/../lib";
|
|
|
|
use BackRest::Utility;
|
|
|
|
use BackRest::File;
|
|
|
|
use BackRest::Remote;
|
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
use BackRestTest::CommonTest;
|
|
|
|
|
2014-06-22 17:30:17 +03:00
|
|
|
use Exporter qw(import);
|
2014-06-22 18:56:01 +03:00
|
|
|
our @EXPORT = qw(BackRestTestBackup_Test);
|
|
|
|
|
|
|
|
my $strTestPath;
|
|
|
|
my $strHost;
|
|
|
|
my $strUserBackRest;
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-06-29 17:53:39 +03:00
|
|
|
# BackRestTestBackup_ClusterStop
|
2014-06-22 18:56:01 +03:00
|
|
|
####################################################################################################################################
|
2014-06-29 17:53:39 +03:00
|
|
|
sub BackRestTestBackup_ClusterStop
|
2014-06-22 18:56:01 +03:00
|
|
|
{
|
|
|
|
my $strPath = shift;
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
# If the db directory already exists, stop the cluster and remove the directory
|
|
|
|
if (-e $strPath . "/postmaster.pid")
|
|
|
|
{
|
2014-07-03 02:15:21 +03:00
|
|
|
BackRestTestCommon_Execute("pg_ctl stop -D $strPath -w -s -m fast");
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 21:32:34 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BackRestTestBackup_ClusterRestart
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BackRestTestBackup_ClusterRestart
|
|
|
|
{
|
|
|
|
my $strPath = BackRestTestCommon_DbCommonPathGet();
|
|
|
|
|
|
|
|
# If the db directory already exists, stop the cluster and remove the directory
|
|
|
|
if (-e $strPath . "/postmaster.pid")
|
|
|
|
{
|
2014-07-03 02:15:21 +03:00
|
|
|
BackRestTestCommon_Execute("pg_ctl restart -D $strPath -w -s");
|
2014-06-28 21:32:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BackRestTestBackup_ClusterCreate
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BackRestTestBackup_ClusterCreate
|
|
|
|
{
|
|
|
|
my $strPath = shift;
|
|
|
|
my $iPort = shift;
|
2014-06-24 01:54:00 +03:00
|
|
|
|
|
|
|
my $strArchive = BackRestTestCommon_CommandMainGet() . " --stanza=" . BackRestTestCommon_StanzaGet() .
|
2014-06-23 03:19:13 +03:00
|
|
|
" --config=" . BackRestTestCommon_DbPathGet() . "/pg_backrest.conf archive-push %p";
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-07-03 02:15:21 +03:00
|
|
|
BackRestTestCommon_Execute("initdb -D $strPath -A trust");
|
2014-07-12 17:26:51 +03:00
|
|
|
BackRestTestCommon_Execute("/Library/PostgreSQL/9.3/bin/pg_ctl start -o \"-c port=$iPort -c checkpoint_segments=1 " .
|
|
|
|
"-c wal_level=archive -c archive_mode=on -c archive_command='$strArchive'\" " .
|
2014-07-03 02:15:21 +03:00
|
|
|
"-D $strPath -l $strPath/postgresql.log -w -s");
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
2014-06-22 17:30:17 +03:00
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-06-29 17:53:39 +03:00
|
|
|
# BackRestTestBackup_Drop
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
2014-06-29 17:53:39 +03:00
|
|
|
sub BackRestTestBackup_Drop
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
2014-06-29 17:53:39 +03:00
|
|
|
# Stop the cluster if one is running
|
|
|
|
BackRestTestBackup_ClusterStop(BackRestTestCommon_DbCommonPathGet());
|
2014-06-22 17:30:17 +03:00
|
|
|
|
|
|
|
# Remove the backrest private directory
|
2014-06-29 17:53:39 +03:00
|
|
|
if (-e BackRestTestCommon_BackupPathGet())
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
2014-07-03 02:15:21 +03:00
|
|
|
BackRestTestCommon_Execute('rm -rf ' . BackRestTestCommon_BackupPathGet(), true, true);
|
2014-06-22 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Remove the test directory
|
2014-06-29 17:53:39 +03:00
|
|
|
system('rm -rf ' . BackRestTestCommon_TestPathGet()) == 0
|
|
|
|
or die 'unable to remove ' . BackRestTestCommon_TestPathGet() . 'path';
|
|
|
|
}
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# BackRestTestBackup_Create
|
|
|
|
####################################################################################################################################
|
|
|
|
sub BackRestTestBackup_Create
|
|
|
|
{
|
|
|
|
my $bRemote = shift;
|
2014-06-22 18:56:01 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Set defaults
|
|
|
|
$bRemote = defined($bRemote) ? $bRemote : false;
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Drop the old test directory
|
|
|
|
BackRestTestBackup_Drop();
|
2014-06-24 01:54:00 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Create the test directory
|
|
|
|
mkdir(BackRestTestCommon_TestPathGet(), oct('0770'))
|
|
|
|
or confess 'Unable to create ' . BackRestTestCommon_TestPathGet() . ' path';
|
2014-06-22 18:56:01 +03:00
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
# Create the db directory
|
|
|
|
mkdir(BackRestTestCommon_DbPathGet(), oct('0700'))
|
|
|
|
or confess 'Unable to create ' . BackRestTestCommon_DbPathGet() . ' path';
|
|
|
|
|
|
|
|
# Create the db/common directory
|
|
|
|
mkdir(BackRestTestCommon_DbCommonPathGet())
|
|
|
|
or confess 'Unable to create ' . BackRestTestCommon_DbCommonPathGet() . ' path';
|
|
|
|
|
|
|
|
# Create the archive directory
|
|
|
|
mkdir(BackRestTestCommon_ArchivePathGet(), oct('0700'))
|
|
|
|
or confess 'Unable to create ' . BackRestTestCommon_ArchivePathGet() . ' path';
|
|
|
|
|
|
|
|
# Create the backup directory
|
|
|
|
if ($bRemote)
|
|
|
|
{
|
2014-07-03 02:15:21 +03:00
|
|
|
BackRestTestCommon_Execute("mkdir -m 700 " . BackRestTestCommon_BackupPathGet(), true);
|
2014-06-29 17:53:39 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mkdir(BackRestTestCommon_BackupPathGet(), oct('0700'))
|
|
|
|
or confess 'Unable to create ' . BackRestTestCommon_BackupPathGet() . ' path';
|
2014-06-22 17:30:17 +03:00
|
|
|
}
|
2014-06-29 17:53:39 +03:00
|
|
|
|
|
|
|
# Create the cluster
|
|
|
|
BackRestTestBackup_ClusterCreate(BackRestTestCommon_DbCommonPathGet(), BackRestTestCommon_DbPortGet());
|
2014-06-22 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-06-22 18:56:01 +03:00
|
|
|
# BackRestTestBackup_Test
|
2014-06-22 17:30:17 +03:00
|
|
|
####################################################################################################################################
|
2014-06-22 18:56:01 +03:00
|
|
|
sub BackRestTestBackup_Test
|
2014-06-22 17:30:17 +03:00
|
|
|
{
|
|
|
|
my $strTest = shift;
|
2014-06-22 18:56:01 +03:00
|
|
|
my $iTestRun = shift;
|
2014-06-22 17:30:17 +03:00
|
|
|
|
|
|
|
# If no test was specified, then run them all
|
|
|
|
if (!defined($strTest))
|
|
|
|
{
|
|
|
|
$strTest = 'all';
|
|
|
|
}
|
|
|
|
|
|
|
|
# Setup test variables
|
|
|
|
my $iRun;
|
2014-06-22 18:56:01 +03:00
|
|
|
$strTestPath = BackRestTestCommon_TestPathGet();
|
|
|
|
my $strStanza = BackRestTestCommon_StanzaGet();
|
|
|
|
my $strUser = BackRestTestCommon_UserGet();
|
|
|
|
my $strGroup = BackRestTestCommon_GroupGet();
|
|
|
|
$strHost = BackRestTestCommon_HostGet();
|
|
|
|
$strUserBackRest = BackRestTestCommon_UserBackRestGet();
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
# Print test banner
|
|
|
|
&log(INFO, "BACKUP MODULE ******************************************************************");
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2014-06-28 21:32:34 +03:00
|
|
|
if ($strTest eq 'all' || $strTest eq 'full')
|
|
|
|
{
|
|
|
|
$iRun = 0;
|
|
|
|
|
|
|
|
&log(INFO, "Test Full Backup\n");
|
|
|
|
|
2014-06-29 17:53:39 +03:00
|
|
|
for (my $bRemote = false; $bRemote <= true; $bRemote++)
|
2014-07-13 02:03:39 +03:00
|
|
|
{
|
|
|
|
for (my $bLarge = false; $bLarge <= false; $bLarge++)
|
2014-06-28 21:32:34 +03:00
|
|
|
{
|
2014-06-29 17:53:39 +03:00
|
|
|
BackRestTestBackup_Create($bRemote);
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
for (my $bArchiveLocal = false; $bArchiveLocal <= $bRemote; $bArchiveLocal++)
|
|
|
|
{
|
2014-06-29 17:53:39 +03:00
|
|
|
for (my $bHardlink = false; $bHardlink <= true; $bHardlink++)
|
2014-06-28 21:32:34 +03:00
|
|
|
{
|
|
|
|
my %oDbConfigHash;
|
|
|
|
my %oBackupConfigHash;
|
|
|
|
|
|
|
|
# Confgure hard-linking
|
|
|
|
if ($bHardlink)
|
|
|
|
{
|
|
|
|
$oBackupConfigHash{'global:backup'}{hardlink} = 'y';
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
BackRestTestCommon_ConfigCreate('db',
|
|
|
|
($bRemote ? REMOTE_BACKUP : undef), $bArchiveLocal, \%oDbConfigHash);
|
|
|
|
BackRestTestCommon_ConfigCreate('backup',
|
|
|
|
($bRemote ? REMOTE_DB : undef), $bArchiveLocal, \%oBackupConfigHash);
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
for (my $iFull = 1; $iFull <= 1; $iFull++)
|
|
|
|
{
|
|
|
|
$iRun++;
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
&log(INFO, "run ${iRun} - " .
|
|
|
|
"remote ${bRemote}, large ${bLarge}, archive_local ${bArchiveLocal}, full ${iFull}");
|
2014-06-28 21:32:34 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
my $strCommand = BackRestTestCommon_CommandMainGet() . ' --config=' . BackRestTestCommon_BackupPathGet() .
|
|
|
|
"/pg_backrest.conf --type=incr --stanza=${strStanza} backup";
|
2014-06-29 17:53:39 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
BackRestTestCommon_Execute($strCommand, $bRemote);
|
2014-06-29 17:53:39 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
for (my $iIncr = 1; $iIncr <= 1; $iIncr++)
|
|
|
|
{
|
|
|
|
$iRun++;
|
2014-06-29 17:53:39 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
&log(INFO, "run ${iRun} - " .
|
|
|
|
"remote ${bRemote}, large ${bLarge}, archive_local ${bArchiveLocal}, hardlink ${bHardlink}, " .
|
|
|
|
"full ${iFull}, incr ${iIncr}");
|
2014-06-29 17:53:39 +03:00
|
|
|
|
2014-07-13 02:03:39 +03:00
|
|
|
BackRestTestCommon_Execute($strCommand, $bRemote);
|
2014-06-28 21:32:34 +03:00
|
|
|
}
|
2014-07-13 02:03:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-28 21:32:34 +03:00
|
|
|
}
|
2014-06-29 17:53:39 +03:00
|
|
|
|
|
|
|
BackRestTestBackup_Drop();
|
2014-06-28 21:32:34 +03:00
|
|
|
}
|
2014-07-13 02:03:39 +03:00
|
|
|
}
|
2014-06-28 21:32:34 +03:00
|
|
|
}
|
2014-06-22 18:56:01 +03:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
# Test path_create()
|
|
|
|
#-------------------------------------------------------------------------------------------------------------------------------
|
2014-06-22 17:30:17 +03:00
|
|
|
# if ($strTest eq 'all' || $strTest eq 'path_create')
|
|
|
|
# {
|
|
|
|
# $iRun = 0;
|
|
|
|
#
|
|
|
|
# &log(INFO, "Test File->path_create()\n");
|
|
|
|
#
|
|
|
|
# # Loop through local/remote
|
|
|
|
# for (my $bRemote = 0; $bRemote <= 1; $bRemote++)
|
|
|
|
# {
|
|
|
|
# # Create the file object
|
|
|
|
# my $oFile = (BackRest::File->new
|
|
|
|
# (
|
2014-06-22 18:56:01 +03:00
|
|
|
# strStanza => $strStanza,
|
|
|
|
# strBackupPath => $strTestPath,
|
2014-06-22 17:30:17 +03:00
|
|
|
# strRemote => $bRemote ? 'backup' : undef,
|
|
|
|
# oRemote => $bRemote ? $oRemote : undef
|
|
|
|
# ))->clone();
|
|
|
|
#
|
|
|
|
# # Loop through exists (does the paren path exist?)
|
|
|
|
# for (my $bExists = 0; $bExists <= 1; $bExists++)
|
|
|
|
# {
|
|
|
|
# # Loop through exists (does the paren path exist?)
|
|
|
|
# for (my $bError = 0; $bError <= 1; $bError++)
|
|
|
|
# {
|
|
|
|
# # Loop through permission (permission will be set on true)
|
|
|
|
# for (my $bPermission = 0; $bPermission <= $bExists; $bPermission++)
|
|
|
|
# {
|
|
|
|
# my $strPathType = PATH_BACKUP_CLUSTER;
|
|
|
|
#
|
|
|
|
# $iRun++;
|
|
|
|
#
|
2014-06-22 18:56:01 +03:00
|
|
|
# if (defined($iTestRun) && $iTestRun != $iRun)
|
|
|
|
# {
|
|
|
|
# next;
|
|
|
|
# }
|
|
|
|
#
|
2014-06-22 17:30:17 +03:00
|
|
|
# &log(INFO, "run ${iRun} - " .
|
|
|
|
# "remote ${bRemote}, exists ${bExists}, error ${bError}, permission ${bPermission}");
|
|
|
|
#
|
|
|
|
# # Setup test directory
|
2014-06-22 18:56:01 +03:00
|
|
|
# BackRestTestFile_Setup($bError);
|
2014-06-22 17:30:17 +03:00
|
|
|
#
|
|
|
|
# mkdir("$strTestPath/backup") or confess "Unable to create test/backup directory";
|
|
|
|
# mkdir("$strTestPath/backup/db") or confess "Unable to create test/backup/db directory";
|
|
|
|
#
|
|
|
|
# my $strPath = "path";
|
|
|
|
# my $strPermission;
|
|
|
|
#
|
|
|
|
# # If permission then set one (other than the default)
|
|
|
|
# if ($bPermission)
|
|
|
|
# {
|
|
|
|
# $strPermission = "0700";
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # If not exists then set the path to something bogus
|
|
|
|
# if ($bError)
|
|
|
|
# {
|
|
|
|
# $strPath = "${strTestPath}/private/path";
|
|
|
|
# $strPathType = PATH_BACKUP_ABSOLUTE;
|
|
|
|
# }
|
|
|
|
# elsif (!$bExists)
|
|
|
|
# {
|
|
|
|
# $strPath = "error/path";
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # Execute in eval to catch errors
|
|
|
|
# my $bErrorExpected = !$bExists || $bError;
|
|
|
|
#
|
|
|
|
# eval
|
|
|
|
# {
|
|
|
|
# $oFile->path_create($strPathType, $strPath, $strPermission);
|
|
|
|
# };
|
|
|
|
#
|
|
|
|
# # Check for errors
|
|
|
|
# if ($@)
|
|
|
|
# {
|
|
|
|
# # Ignore errors if the path did not exist
|
|
|
|
# if ($bErrorExpected)
|
|
|
|
# {
|
|
|
|
# next;
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# confess "error raised: " . $@ . "\n";
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# if ($bErrorExpected)
|
|
|
|
# {
|
|
|
|
# confess 'error was expected';
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # Make sure the path was actually created
|
|
|
|
# my $strPathCheck = $oFile->path_get($strPathType, $strPath);
|
|
|
|
#
|
|
|
|
# unless (-e $strPathCheck)
|
|
|
|
# {
|
|
|
|
# confess "path was not created";
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# # Check that the permissions were set correctly
|
|
|
|
# my $oStat = lstat($strPathCheck);
|
|
|
|
#
|
|
|
|
# if (!defined($oStat))
|
|
|
|
# {
|
|
|
|
# confess "unable to stat ${strPathCheck}";
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# if ($bPermission)
|
|
|
|
# {
|
|
|
|
# if ($strPermission ne sprintf("%04o", S_IMODE($oStat->mode)))
|
|
|
|
# {
|
|
|
|
# confess "permissions were not set to {$strPermission}";
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
2014-06-22 18:56:01 +03:00
|
|
|
|
2014-06-22 21:51:28 +03:00
|
|
|
# BackRestTestBackup_Setup(true);
|
2014-06-22 17:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|