1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-03 14:52:21 +02:00

Working on unit tests for file_copy. Still need to add specific error tests, timestamp, and permissions.

This commit is contained in:
David Steele 2014-05-14 15:07:37 -04:00
parent db40553434
commit 3e12f9230b
5 changed files with 194 additions and 86 deletions

View File

@ -202,7 +202,8 @@ sub path_get
my $bTemp = shift; # Return the temp file for this path type - only some types have temp files
# Only allow temp files for PATH_BACKUP_ARCHIVE and PATH_BACKUP_TMP
if (defined($bTemp) && $bTemp && !($strType eq PATH_BACKUP_ARCHIVE || $strType eq PATH_BACKUP_TMP || $strType eq PATH_DB_ABSOLUTE))
if (defined($bTemp) && $bTemp && !($strType eq PATH_BACKUP_ARCHIVE || $strType eq PATH_BACKUP_TMP ||
$strType eq PATH_DB_ABSOLUTE || $strType eq PATH_BACKUP_ABSOLUTE))
{
confess &log(ASSERT, "temp file not supported on path " . $strType);
}
@ -227,7 +228,10 @@ sub path_get
# Get absolute backup path
if ($strType eq PATH_BACKUP_ABSOLUTE)
{
# Need a check in here to make sure this is relative to the backup path
if (defined($bTemp) && $bTemp)
{
return "${strFile}.backrest.tmp";
}
return $strFile;
}
@ -610,7 +614,6 @@ sub file_copy
}
$strCommand =~ s/\%file\%/${strSource}/g;
$strCommand .= " 2> /dev/null";
# If this command is remote on only one side
if ($self->is_remote($strSourcePathType) && !$self->is_remote($strDestinationPathType) ||
@ -681,10 +684,11 @@ sub file_copy
&log(TRACE, "file_copy: remote ${strSourcePathType} '${strCommand}'");
my $oSSH = $self->remote_get($strSourcePathType);
$oSSH->system($strCommand);
unless($oSSH->system($strCommand))
if ($oSSH->error)
{
my $strResult = "unable to execute remote command ${strCommand}:" . oSSH->error;
my $strResult = "unable to execute copy ${strCommand}: " . $oSSH->error;
$bConfessCopyError ? confess &log(ERROR, $strResult) : return false;
}
}

View File

@ -17,7 +17,7 @@ use Exporter qw(import);
our @EXPORT = qw(data_hash_build trim common_prefix wait_for_file date_string_get file_size_format execute
log log_file_set log_level_set
lock_file_create lock_file_remove
TRACE DEBUG ERROR ASSERT WARN INFO true false);
TRACE DEBUG ERROR ASSERT WARN INFO OFF true false);
# Global constants
use constant

View File

@ -1,79 +0,0 @@
#!/usr/bin/perl
####################################################################################################################################
# File.pl - Unit Tests for BackRest::File
####################################################################################################################################
####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
use warnings;
use english;
use File::Basename;
use Carp;
use lib dirname($0) . "/..";
use pg_backrest_file;
use Exporter qw(import);
our @EXPORT = qw(BackRestTestFile);
sub BackRestTestFile
{
# Test copy()
for (my $bSourceCompressed = 0; $bSourceCompressed <= 1; $bSourceCompressed++)
{
# Loop through destination compression
for (my $bDestinationCompressed = 0; $bDestinationCompressed <= 1; $bDestinationCompressed++)
{
print "srccmp $bSourceCompressed, dstcmp $bDestinationCompressed\n";
# Drop the old test directory and create a new one
system("rm -rf test");
system("mkdir test") == 0 or confess "Unable to create the test directory";
# system("mkdir test/backup") == 0 or confess "Unable to create the test/backup directory";
# system("mkdir test/db") == 0 or confess "Unable to create the test/db directory";
my $strSourceFile = "test/test-source.txt";
my $strDestinationFile = "test/test-destination.txt";
# Create the compressed or uncompressed test file
if ($bSourceCompressed)
{
$strSourceFile .= ".gz";
system("echo 'TESTDATA' | gzip > ${strSourceFile}");
}
else
{
system("echo 'TESTDATA' > ${strSourceFile}");
}
# Create the file object based on current values
my $oFile = pg_backrest_file->new
(
strStanza => "db",
bNoCompression => !$bDestinationCompressed,
strBackupPath => "test/backup",
strBackupUser => undef,
strBackupHost => undef,
strBackupPath => "test/backup",
strBackupClusterPath => "test/db",
strCommandCompress => "gzip --stdout %file%",
strCommandDecompress => "gzip -dc %file%"
);
$oFile->file_copy(PATH_DB_ABSOLUTE, $strSourceFile, PATH_DB_ABSOLUTE, $strDestinationFile);
if ($bDestinationCompressed)
{
$strDestinationFile .= ".gz";
}
unless (-e $strDestinationFile)
{
confess "could not find destination file ${strDestinationFile}";
}
}
}
}

181
test/lib/BackRestTest/FileTest.pm Executable file
View File

@ -0,0 +1,181 @@
#!/usr/bin/perl
####################################################################################################################################
# File.pl - Unit Tests for BackRest::File
####################################################################################################################################
####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
use warnings;
use english;
use Carp;
use File::Basename;
use Cwd 'abs_path';
use lib dirname($0) . "/..";
use pg_backrest_file;
use pg_backrest_utility;
use Exporter qw(import);
our @EXPORT = qw(BackRestTestFile);
sub BackRestTestFile
{
my $strTestPath = dirname(abs_path($0)) . "/test";
my $iRun = 0;
log_level_set(OFF, OFF);
# Test copy()
for (my $bSourceRemote = 0; $bSourceRemote <= 1; $bSourceRemote++)
{
my $strSourceHost = $bSourceRemote ? "127.0.0.1" : undef;
my $strSourceUser = $bSourceRemote ? "dsteele" : undef;
# Loop through source compression
for (my $bDestinationRemote = 0; $bDestinationRemote <= 1; $bDestinationRemote++)
{
my $strDestinationHost = $bDestinationRemote ? "127.0.0.1" : undef;
my $strDestinationUser = $bDestinationRemote ? "dsteele" : undef;
# Loop through destination compression
for (my $bDestinationCompressed = 0; $bDestinationCompressed <= 1; $bDestinationCompressed++)
{
my $oFile = pg_backrest_file->new
(
strStanza => "db",
bNoCompression => !$bDestinationCompressed,
strBackupClusterPath => undef,
strBackupPath => ${strTestPath},
strBackupHost => $strSourceHost,
strBackupUser => $strSourceUser,
strDbHost => $strDestinationHost,
strDbUser => $strDestinationUser,
strCommandCompress => "gzip --stdout %file%",
strCommandDecompress => "gzip -dc %file%",
strLockPath => dirname($0) . "/test/lock"
);
for (my $bSourceCompressed = 0; $bSourceCompressed <= 1; $bSourceCompressed++)
{
for (my $bSourcePathType = 0; $bSourcePathType <= 1; $bSourcePathType++)
{
my $strSourcePath = $bSourcePathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE;
for (my $bDestinationPathType = 0; $bDestinationPathType <= 1; $bDestinationPathType++)
{
my $strDestinationPath = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE;
for (my $bError = 0; $bError <= 1; $bError++)
{
for (my $bConfessError = 0; $bConfessError <= 1; $bConfessError++)
{
$iRun++;
print "run ${iRun} - " .
"srcpth ${strSourcePath}, srcrmt $bSourceRemote, srccmp $bSourceCompressed, " .
"dstpth ${strDestinationPath}, dstrmt $bDestinationRemote, dstcmp $bDestinationCompressed, " .
"error $bError, confess_error $bConfessError\n";
# Drop the old test directory and create a new one
system("rm -rf test");
system("mkdir -p test/lock") == 0 or confess "Unable to create the test directory";
my $strSourceFile = "${strTestPath}/test-source.txt";
my $strDestinationFile = "${strTestPath}/test-destination.txt";
# Create the compressed or uncompressed test file
if ($bSourceCompressed)
{
$strSourceFile .= ".gz";
system("echo 'TESTDATA' | gzip > ${strSourceFile}");
}
else
{
system("echo 'TESTDATA' > ${strSourceFile}");
}
# Create the file object based on current values
if ($bError)
{
$strSourceFile .= ".error";
}
# Run file copy in an eval block because some errors are expected
my $bReturn;
eval
{
$bReturn = $oFile->file_copy($strSourcePath, $strSourceFile,
$strDestinationPath, $strDestinationFile,
undef, undef, undef, undef,
$bConfessError);
};
# Check for errors after copy
if ($@)
{
# Different remote and destination with different path types should error
if ($bSourceRemote && $bDestinationRemote)
{
next;
}
# If the error was intentional, then also continue
elsif ($bError)
{
next;
}
# Else this is an unexpected error
else
{
confess $@;
}
}
elsif ($bError)
{
if ($bConfessError)
{
confess "Value was returned instead of exception thrown when confess error is true";
}
else
{
if ($bReturn)
{
confess "true was returned when an error was generated";
}
else
{
next;
}
}
}
else
{
if (!$bReturn)
{
confess "error was returned when no error generated";
}
}
# Check for errors after copy
if ($bDestinationCompressed)
{
$strDestinationFile .= ".gz";
}
unless (-e $strDestinationFile)
{
confess "could not find destination file ${strDestinationFile}";
}
}
}
}
}
}
}
}
}
}

View File

@ -15,9 +15,11 @@ use Getopt::Long;
use Carp;
use lib dirname($0) . "/lib";
use BackRestTest::File;
use BackRestTest::FileTest;
####################################################################################################################################
# Perl includes
####################################################################################################################################
BackRestTestFile();
print "TEST COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)\n";