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);
|
2015-06-14 00:25:49 +02:00
|
|
|
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';
|
2015-01-11 18:52:16 +02:00
|
|
|
use Pod::Usage;
|
2015-06-21 18:06:13 +02:00
|
|
|
use Scalar::Util qw(blessed);
|
2015-03-08 19:26:09 +02:00
|
|
|
#use Test::More;
|
2014-04-03 00:25:37 +03:00
|
|
|
|
2014-09-16 18:22:55 +03:00
|
|
|
use lib dirname($0) . '/../lib';
|
2015-04-03 04:07:23 +02:00
|
|
|
use BackRest::Db;
|
2015-06-22 19:11:07 +02:00
|
|
|
use BackRest::Ini;
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRest::Utility;
|
|
|
|
|
2014-09-16 18:22:55 +03:00
|
|
|
use lib dirname($0) . '/lib';
|
2014-06-22 17:30:17 +03:00
|
|
|
use BackRestTest::CommonTest;
|
2014-07-17 06:38:38 +03:00
|
|
|
use BackRestTest::UtilityTest;
|
2015-03-08 19:26:09 +02:00
|
|
|
use BackRestTest::ConfigTest;
|
2014-05-14 22:07:37 +03:00
|
|
|
use BackRestTest::FileTest;
|
2014-06-22 18:56:01 +03:00
|
|
|
use BackRestTest::BackupTest;
|
2015-04-08 00:36:59 +02:00
|
|
|
use BackRestTest::CompareTest;
|
2014-04-03 00:25:37 +03:00
|
|
|
|
2015-01-11 18:52:16 +02: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
|
2015-02-28 17:21:36 +02:00
|
|
|
--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
|
2015-02-28 06:31:39 +02:00
|
|
|
--infinite repeat selected tests forever
|
2015-04-03 04:07:23 +02:00
|
|
|
--db-version version of postgres to test (or all)
|
2015-05-05 19:08:48 +02:00
|
|
|
--log-force force overwrite of current test log files
|
2015-01-11 18:52:16 +02:00
|
|
|
|
|
|
|
Configuration Options:
|
2015-07-11 23:16:35 +02:00
|
|
|
--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
|
2015-01-11 18:52:16 +02:00
|
|
|
|
|
|
|
General Options:
|
2015-01-12 16:05:26 +02:00
|
|
|
--version display version and exit
|
|
|
|
--help display usage and exit
|
2015-01-11 18:52:16 +02:00
|
|
|
=cut
|
|
|
|
|
2014-04-03 00:25:37 +03:00
|
|
|
####################################################################################################################################
|
2014-06-04 04:22:07 +03:00
|
|
|
# Command line parameters
|
|
|
|
####################################################################################################################################
|
2015-04-03 04:07:23 +02:00
|
|
|
my $strLogLevel = 'info';
|
2014-06-05 17:20:03 +03:00
|
|
|
my $strModule = 'all';
|
|
|
|
my $strModuleTest = 'all';
|
2014-06-22 17:54:31 +03:00
|
|
|
my $iModuleTestRun = undef;
|
2015-03-12 18:15:19 +02:00
|
|
|
my $iThreadMax = 1;
|
2014-07-16 05:32:41 +03:00
|
|
|
my $bDryRun = false;
|
|
|
|
my $bNoCleanup = false;
|
2014-08-11 04:22:17 +03:00
|
|
|
my $strPgSqlBin;
|
2015-07-11 23:16:35 +02:00
|
|
|
my $strExe;
|
2014-08-11 04:22:17 +03:00
|
|
|
my $strTestPath;
|
2015-01-11 18:52:16 +02:00
|
|
|
my $bVersion = false;
|
|
|
|
my $bHelp = false;
|
|
|
|
my $bQuiet = false;
|
2015-02-28 06:31:39 +02:00
|
|
|
my $bInfinite = false;
|
2015-04-03 04:07:23 +02:00
|
|
|
my $strDbVersion = 'max';
|
2015-05-05 19:08:48 +02:00
|
|
|
my $bLogForce = false;
|
2015-01-11 18:52:16 +02:00
|
|
|
|
|
|
|
GetOptions ('q|quiet' => \$bQuiet,
|
|
|
|
'version' => \$bVersion,
|
|
|
|
'help' => \$bHelp,
|
|
|
|
'pgsql-bin=s' => \$strPgSqlBin,
|
2015-07-11 23:16:35 +02:00
|
|
|
'exes=s' => \$strExe,
|
2014-09-16 18:22:55 +03:00
|
|
|
'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,
|
2015-02-28 17:21:36 +02:00
|
|
|
'thread-max=s' => \$iThreadMax,
|
2014-09-16 18:22:55 +03:00
|
|
|
'dry-run' => \$bDryRun,
|
2015-02-28 06:31:39 +02:00
|
|
|
'no-cleanup' => \$bNoCleanup,
|
2015-04-03 04:07:23 +02:00
|
|
|
'infinite' => \$bInfinite,
|
2015-05-05 19:08:48 +02:00
|
|
|
'db-version=s' => \$strDbVersion,
|
|
|
|
'log-force' => \$bLogForce)
|
2015-01-11 18:52:16 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-06-04 04:22:07 +03:00
|
|
|
|
2015-04-03 04:07:23 +02:00
|
|
|
if (@ARGV > 0)
|
|
|
|
{
|
|
|
|
print "invalid parameter\n\n";
|
|
|
|
pod2usage();
|
|
|
|
}
|
2015-03-08 19:26:09 +02:00
|
|
|
|
2015-05-08 00:37:25 +02:00
|
|
|
# Must be run from the test path so relative paths to bin can be tested
|
2015-07-07 03:00:48 +02:00
|
|
|
# if ($0 ne './test.pl')
|
|
|
|
# {
|
|
|
|
# confess 'test.pl must be run from the test path';
|
|
|
|
# }
|
2015-05-08 00:37:25 +02:00
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# Setup
|
|
|
|
####################################################################################################################################
|
|
|
|
# Set a neutral umask so tests work as expected
|
|
|
|
umask(0);
|
|
|
|
|
2015-01-11 18:52:16 +02:00
|
|
|
# Set console log level
|
|
|
|
if ($bQuiet)
|
|
|
|
{
|
|
|
|
$strLogLevel = 'off';
|
|
|
|
}
|
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
log_level_set(undef, uc($strLogLevel));
|
|
|
|
|
2014-06-05 17:20:03 +03:00
|
|
|
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}\"";
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:52:16 +02:00
|
|
|
# Search for psql bin
|
2015-04-03 04:07:23 +02:00
|
|
|
my @stryTestVersion;
|
|
|
|
my $strVersionSupport = versionSupport();
|
|
|
|
|
2014-08-11 04:22:17 +03:00
|
|
|
if (!defined($strPgSqlBin))
|
|
|
|
{
|
2015-06-22 20:35:25 +02:00
|
|
|
# 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
|
|
|
|
);
|
2015-01-11 18:52:16 +02:00
|
|
|
|
|
|
|
foreach my $strSearchPath (@strySearchPath)
|
|
|
|
{
|
2015-04-03 04:07:23 +02:00
|
|
|
for (my $iVersionIdx = @{$strVersionSupport} - 1; $iVersionIdx >= 0; $iVersionIdx--)
|
2015-01-11 18:52:16 +02:00
|
|
|
{
|
2015-04-03 04:07:23 +02:00
|
|
|
if ($strDbVersion eq 'all' || $strDbVersion eq 'max' && @stryTestVersion == 0 ||
|
|
|
|
$strDbVersion eq ${$strVersionSupport}[$iVersionIdx])
|
2015-01-11 18:52:16 +02:00
|
|
|
{
|
2015-04-03 04:07:23 +02:00
|
|
|
my $strVersionPath = $strSearchPath;
|
|
|
|
$strVersionPath =~ s/VERSION/${$strVersionSupport}[$iVersionIdx]/g;
|
|
|
|
|
|
|
|
if (-e "${strVersionPath}/initdb")
|
|
|
|
{
|
|
|
|
&log(INFO, "FOUND pgsql-bin at ${strVersionPath}");
|
|
|
|
push @stryTestVersion, $strVersionPath;
|
|
|
|
}
|
2015-01-11 18:52:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 04:07:23 +02:00
|
|
|
# Make sure at least one version of postgres was found
|
2015-06-21 18:06:13 +02:00
|
|
|
@stryTestVersion > 0
|
2015-04-03 04:07:23 +02:00
|
|
|
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
|
|
|
|
2015-02-28 17:21:36 +02: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;
|
|
|
|
|
2014-09-16 18:22:55 +03:00
|
|
|
if (!open($hReadMe, '<', dirname($0) . '/../README.md'))
|
2014-06-08 00:29:11 +03:00
|
|
|
{
|
2014-09-16 18:22:55 +03:00
|
|
|
confess 'unable to open README.md';
|
2014-06-08 00:29:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while ($strLine = readline($hReadMe))
|
|
|
|
{
|
|
|
|
if ($strLine =~ /^\#\#\# v/)
|
|
|
|
{
|
2015-06-22 19:11:07 +02:00
|
|
|
$bMatch = substr($strLine, 5, length(BACKREST_VERSION)) eq BACKREST_VERSION;
|
2014-06-08 00:29:11 +03:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bMatch)
|
|
|
|
{
|
2015-06-22 19:11:07 +02:00
|
|
|
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
|
|
|
|
####################################################################################################################################
|
2015-06-22 19:11:07 +02:00
|
|
|
if (-e './test.pl' && -e '../bin/pg_backrest')
|
2014-08-11 04:22:17 +03:00
|
|
|
{
|
2015-06-22 19:11:07 +02: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
|
|
|
}
|
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# Runs tests
|
2014-04-03 00:25:37 +03:00
|
|
|
####################################################################################################################################
|
2014-07-16 05:32:41 +03:00
|
|
|
# &log(INFO, "Testing with test_path = " . BackRestTestCommon_TestPathGet() . ", host = {strHost}, user = {strUser}, " .
|
|
|
|
# "group = {strGroup}");
|
2014-06-22 17:30:17 +03:00
|
|
|
|
2015-02-28 06:31:39 +02:00
|
|
|
my $iRun = 0;
|
2014-07-17 06:38:38 +03:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
eval
|
2014-06-05 17:20:03 +03:00
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
do
|
2015-02-28 06:31:39 +02:00
|
|
|
{
|
2015-07-11 23:16:35 +02:00
|
|
|
if (BackRestTestCommon_Setup($strExe, $strTestPath, $stryTestVersion[0], $iModuleTestRun,
|
|
|
|
$bDryRun, $bNoCleanup, $bLogForce))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2015-06-14 16:12:36 +02:00
|
|
|
&log(INFO, "TESTING psql-bin = $stryTestVersion[0]\n");
|
2015-03-08 19:26:09 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($bInfinite)
|
|
|
|
{
|
|
|
|
$iRun++;
|
|
|
|
&log(INFO, "INFINITE - RUN ${iRun}\n");
|
|
|
|
}
|
2015-02-28 06:31:39 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($strModule eq 'all' || $strModule eq 'utility')
|
|
|
|
{
|
|
|
|
BackRestTestUtility_Test($strModuleTest);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($strModule eq 'all' || $strModule eq 'config')
|
|
|
|
{
|
|
|
|
BackRestTestConfig_Test($strModuleTest);
|
|
|
|
}
|
2015-04-03 04:07:23 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($strModule eq 'all' || $strModule eq 'file')
|
|
|
|
{
|
|
|
|
BackRestTestFile_Test($strModuleTest);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($strModule eq 'all' || $strModule eq 'backup')
|
2015-04-03 04:07:23 +02:00
|
|
|
{
|
2015-06-14 16:12:36 +02:00
|
|
|
BackRestTestBackup_Test($strModuleTest, $iThreadMax);
|
|
|
|
|
|
|
|
if (@stryTestVersion > 1 && ($strModuleTest eq 'all' || $strModuleTest eq 'full'))
|
2015-06-14 00:25:49 +02:00
|
|
|
{
|
2015-06-14 16:12:36 +02:00
|
|
|
for (my $iVersionIdx = 1; $iVersionIdx < @stryTestVersion; $iVersionIdx++)
|
|
|
|
{
|
2015-07-12 04:47:22 +02:00
|
|
|
BackRestTestCommon_Setup($strExe, $strTestPath, $stryTestVersion[$iVersionIdx],
|
2015-06-14 16:12:36 +02:00
|
|
|
$iModuleTestRun, $bDryRun, $bNoCleanup);
|
|
|
|
&log(INFO, "TESTING psql-bin = $stryTestVersion[$iVersionIdx] for backup/full\n");
|
|
|
|
BackRestTestBackup_Test('full', $iThreadMax);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2015-04-03 04:07:23 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2015-06-14 16:12:36 +02:00
|
|
|
if ($strModule eq 'compare')
|
|
|
|
{
|
|
|
|
BackRestTestCompare_Test($strModuleTest);
|
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
}
|
2015-02-28 06:31:39 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
while ($bInfinite);
|
|
|
|
};
|
2015-04-08 00:36:59 +02:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
if ($@)
|
|
|
|
{
|
|
|
|
my $oMessage = $@;
|
|
|
|
|
|
|
|
# If a backrest exception then return the code - don't confess
|
2015-06-21 18:06:13 +02:00
|
|
|
if (blessed($oMessage) && $oMessage->isa('BackRest::Exception'))
|
2015-04-08 00:36:59 +02:00
|
|
|
{
|
2015-06-14 00:25:49 +02:00
|
|
|
syswrite(*STDOUT, $oMessage->trace());
|
|
|
|
exit $oMessage->code();
|
2015-04-08 00:36:59 +02:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
|
|
|
syswrite(*STDOUT, $oMessage);
|
|
|
|
exit 255;;
|
2014-06-22 18:56:01 +03:00
|
|
|
}
|
2015-06-14 00:25:49 +02:00
|
|
|
|
2014-06-22 18:56:01 +03:00
|
|
|
|
2014-07-16 05:32:41 +03:00
|
|
|
if (!$bDryRun)
|
|
|
|
{
|
2015-01-11 18:52:16 +02:00
|
|
|
&log(INFO, 'TESTS COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)');
|
2014-07-16 05:32:41 +03:00
|
|
|
}
|