1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-21 01:21:57 +02:00
pgbackrest/test/test.pl

328 lines
11 KiB
Perl
Raw Normal View History

2013-11-17 21:58:21 +03:00
#!/usr/bin/perl
2014-04-03 00:25:37 +03:00
####################################################################################################################################
2014-06-08 00:29:11 +03:00
# test.pl - BackRest Unit Tests
2014-04-03 00:25:37 +03:00
####################################################################################################################################
####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
2015-04-19 23:27:40 +02:00
use warnings FATAL => qw(all);
use Carp qw(confess longmess);
$SIG{__DIE__} = sub { Carp::confess @_ };
2014-04-03 00:25:37 +03:00
use File::Basename;
use Getopt::Long;
2014-08-11 04:22:17 +03:00
use Cwd 'abs_path';
use Pod::Usage;
use Scalar::Util qw(blessed);
#use Test::More;
2014-04-03 00:25:37 +03:00
use lib dirname($0) . '/../lib';
use BackRest::Db;
use BackRest::Ini;
use BackRest::Utility;
use lib dirname($0) . '/lib';
use BackRestTest::CommonTest;
use BackRestTest::UtilityTest;
use BackRestTest::ConfigTest;
use BackRestTest::FileTest;
use BackRestTest::BackupTest;
use BackRestTest::CompareTest;
2014-04-03 00:25:37 +03:00
####################################################################################################################################
# Usage
####################################################################################################################################
=head1 NAME
test.pl - Simple Postgres Backup and Restore Unit Tests
=head1 SYNOPSIS
test.pl [options]
Test Options:
2015-01-12 16:05:26 +02:00
--module test module to execute:
2015-06-27 17:12:44 +02:00
--test execute the specified test in a module
--run execute only the specified test run
--thread-max max threads to run for backup/restore (default 4)
2015-01-12 16:05:26 +02:00
--dry-run show only the tests that would be executed but don't execute them
--no-cleanup don't cleaup after the last test is complete - useful for debugging
--infinite repeat selected tests forever
--db-version version of postgres to test (or all)
--log-force force overwrite of current test log files
Configuration Options:
--exe backup executable
2015-01-12 16:05:26 +02:00
--psql-bin path to the psql executables (e.g. /usr/lib/postgresql/9.3/bin/)
--test-path path where tests are executed (defaults to ./test)
--log-level log level to use for tests (defaults to INFO)
--quiet, -q equivalent to --log-level=off
General Options:
2015-01-12 16:05:26 +02:00
--version display version and exit
--help display usage and exit
=cut
2014-04-03 00:25:37 +03:00
####################################################################################################################################
# Command line parameters
####################################################################################################################################
my $strLogLevel = 'info';
my $strModule = 'all';
my $strModuleTest = 'all';
2014-06-22 17:54:31 +03:00
my $iModuleTestRun = undef;
my $iThreadMax = 1;
my $bDryRun = false;
my $bNoCleanup = false;
2014-08-11 04:22:17 +03:00
my $strPgSqlBin;
my $strExe;
2014-08-11 04:22:17 +03:00
my $strTestPath;
my $bVersion = false;
my $bHelp = false;
my $bQuiet = false;
my $bInfinite = false;
my $strDbVersion = 'max';
my $bLogForce = false;
GetOptions ('q|quiet' => \$bQuiet,
'version' => \$bVersion,
'help' => \$bHelp,
'pgsql-bin=s' => \$strPgSqlBin,
'exes=s' => \$strExe,
'test-path=s' => \$strTestPath,
'log-level=s' => \$strLogLevel,
'module=s' => \$strModule,
2015-06-27 17:12:44 +02:00
'test=s' => \$strModuleTest,
'run=s' => \$iModuleTestRun,
'thread-max=s' => \$iThreadMax,
'dry-run' => \$bDryRun,
'no-cleanup' => \$bNoCleanup,
'infinite' => \$bInfinite,
'db-version=s' => \$strDbVersion,
'log-force' => \$bLogForce)
or pod2usage(2);
# Display version and exit if requested
if ($bVersion || $bHelp)
{
print 'pg_backrest ' . version_get() . " unit test\n";
if ($bHelp)
{
print "\n";
pod2usage();
}
exit 0;
}
if (@ARGV > 0)
{
print "invalid parameter\n\n";
pod2usage();
}
# Must be run from the test path so relative paths to bin can be tested
# if ($0 ne './test.pl')
# {
# confess 'test.pl must be run from the test path';
# }
####################################################################################################################################
# Setup
####################################################################################################################################
# Set a neutral umask so tests work as expected
umask(0);
# Set console log level
if ($bQuiet)
{
$strLogLevel = 'off';
}
log_level_set(undef, uc($strLogLevel));
if ($strModuleTest ne 'all' && $strModule eq 'all')
{
confess "--module must be provided for test \"${strModuleTest}\"";
}
2014-06-22 17:54:31 +03:00
if (defined($iModuleTestRun) && $strModuleTest eq 'all')
{
confess "--module-test must be provided for run \"${iModuleTestRun}\"";
}
# Search for psql bin
my @stryTestVersion;
my $strVersionSupport = versionSupport();
2014-08-11 04:22:17 +03:00
if (!defined($strPgSqlBin))
{
# Distribution-specific paths where the PostgreSQL binaries may be located
my @strySearchPath =
(
'/usr/lib/postgresql/VERSION/bin', # Debian/Ubuntu
'/usr/pgsql-VERSION/bin', # CentOS/RHEL/Fedora
'/Library/PostgreSQL/VERSION/bin', # OSX
'/usr/local/bin' # BSD
);
foreach my $strSearchPath (@strySearchPath)
{
for (my $iVersionIdx = @{$strVersionSupport} - 1; $iVersionIdx >= 0; $iVersionIdx--)
{
if ($strDbVersion eq 'all' || $strDbVersion eq 'max' && @stryTestVersion == 0 ||
$strDbVersion eq ${$strVersionSupport}[$iVersionIdx])
{
my $strVersionPath = $strSearchPath;
$strVersionPath =~ s/VERSION/${$strVersionSupport}[$iVersionIdx]/g;
if (-e "${strVersionPath}/initdb")
{
&log(INFO, "FOUND pgsql-bin at ${strVersionPath}");
push @stryTestVersion, $strVersionPath;
}
}
}
}
# Make sure at least one version of postgres was found
@stryTestVersion > 0
or confess 'pgsql-bin was not defined and postgres could not be located automatically';
}
else
{
push @stryTestVersion, $strPgSqlBin;
2014-08-11 04:22:17 +03:00
}
2014-06-08 00:29:11 +03:00
# Check thread total
if ($iThreadMax < 1 || $iThreadMax > 32)
{
confess 'thread-max must be between 1 and 32';
}
2014-06-08 00:29:11 +03:00
####################################################################################################################################
# Make sure version number matches in README.md and VERSION
####################################################################################################################################
my $hReadMe;
my $strLine;
my $bMatch = false;
if (!open($hReadMe, '<', dirname($0) . '/../README.md'))
2014-06-08 00:29:11 +03:00
{
confess 'unable to open README.md';
2014-06-08 00:29:11 +03:00
}
while ($strLine = readline($hReadMe))
{
if ($strLine =~ /^\#\#\# v/)
{
$bMatch = substr($strLine, 5, length(BACKREST_VERSION)) eq BACKREST_VERSION;
2014-06-08 00:29:11 +03:00
last;
}
}
if (!$bMatch)
{
confess 'unable to find version ' . BACKREST_VERSION . ' as last revision in README.md';
2014-06-08 00:29:11 +03:00
}
2014-08-11 04:22:17 +03:00
####################################################################################################################################
# Clean whitespace only if test.pl is being run from the test directory in the backrest repo
####################################################################################################################################
if (-e './test.pl' && -e '../bin/pg_backrest')
2014-08-11 04:22:17 +03:00
{
BackRestTestCommon_Execute(
"find .. -type f -not -path \"../.git/*\" -not -path \"*.DS_Store\" -not -path \"../test/test/*\" " .
"-not -path \"../test/data/*\" " .
"-exec sh -c 'for i;do echo \"\$i\" && sed 's/[[:space:]]*\$//' \"\$i\">/tmp/.\$\$ && cat /tmp/.\$\$ " .
"> \"\$i\";done' arg0 {} + > /dev/null", false, true);
2014-08-11 04:22:17 +03:00
}
####################################################################################################################################
# Runs tests
2014-04-03 00:25:37 +03:00
####################################################################################################################################
# &log(INFO, "Testing with test_path = " . BackRestTestCommon_TestPathGet() . ", host = {strHost}, user = {strUser}, " .
# "group = {strGroup}");
my $iRun = 0;
eval
{
do
{
if (BackRestTestCommon_Setup($strExe, $strTestPath, $stryTestVersion[0], $iModuleTestRun,
$bDryRun, $bNoCleanup, $bLogForce))
{
&log(INFO, "TESTING psql-bin = $stryTestVersion[0]\n");
if ($bInfinite)
{
$iRun++;
&log(INFO, "INFINITE - RUN ${iRun}\n");
}
if ($strModule eq 'all' || $strModule eq 'utility')
{
BackRestTestUtility_Test($strModuleTest);
}
if ($strModule eq 'all' || $strModule eq 'config')
{
BackRestTestConfig_Test($strModuleTest);
}
if ($strModule eq 'all' || $strModule eq 'file')
{
BackRestTestFile_Test($strModuleTest);
}
if ($strModule eq 'all' || $strModule eq 'backup')
{
BackRestTestBackup_Test($strModuleTest, $iThreadMax);
if (@stryTestVersion > 1 && ($strModuleTest eq 'all' || $strModuleTest eq 'full'))
{
for (my $iVersionIdx = 1; $iVersionIdx < @stryTestVersion; $iVersionIdx++)
{
BackRestTestCommon_Setup($strExe, $strTestPath, $stryTestVersion[$iVersionIdx],
$iModuleTestRun, $bDryRun, $bNoCleanup);
&log(INFO, "TESTING psql-bin = $stryTestVersion[$iVersionIdx] for backup/full\n");
BackRestTestBackup_Test('full', $iThreadMax);
}
}
}
if ($strModule eq 'compare')
{
BackRestTestCompare_Test($strModuleTest);
}
}
}
while ($bInfinite);
};
if ($@)
{
my $oMessage = $@;
# If a backrest exception then return the code - don't confess
if (blessed($oMessage) && $oMessage->isa('BackRest::Exception'))
{
syswrite(*STDOUT, $oMessage->trace());
exit $oMessage->code();
}
syswrite(*STDOUT, $oMessage);
exit 255;;
}
if (!$bDryRun)
{
&log(INFO, 'TESTS COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)');
}