1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00
pgbackrest/test/lib/pgBackRestTest/Common/HostTest.pm

279 lines
8.8 KiB
Perl
Raw Normal View History

####################################################################################################################################
# HostTest.pm - Encapsulate a docker host for testing
####################################################################################################################################
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
package pgBackRestTest::Common::HostTest;
use parent 'pgBackRestTest::Common::ParamTest';
####################################################################################################################################
# Perl includes
####################################################################################################################################
use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use Cwd qw(abs_path);
use Exporter qw(import);
our @EXPORT = qw();
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
use pgBackRest::Common::Log;
use pgBackRest::Common::String;
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
use pgBackRestTest::Common::ExecuteTest;
####################################################################################################################################
# new
####################################################################################################################################
sub new
{
my $class = shift; # Class name
# Create the class hash
my $self = {};
bless $self, $class;
# Assign function parameters, defaults, and log debug info
(
my $strOperation,
$self->{strName},
$self->{strContainer},
$self->{strImage},
$self->{strUser},
$self->{strOS},
$self->{stryMount}
) =
logDebugParam
(
__PACKAGE__ . '->new', \@_,
{name => 'strName', trace => true},
{name => 'strContainer', trace => true},
{name => 'strImage', trace => true},
{name => 'strUser', trace => true},
{name => 'strOS', trace => true},
{name => 'stryMount', required => false, trace => true}
);
executeTest("docker rm -f $self->{strContainer}", {bSuppressError => true});
executeTest("docker run -itd -h $self->{strName} --name=$self->{strContainer}" .
(defined($self->{stryMount}) ? ' -v ' . join(' -v ', @{$self->{stryMount}}) : '') .
" $self->{strImage}");
# Get IP Address
$self->{strIP} = trim(executeTest("docker inspect --format '\{\{ .NetworkSettings.IPAddress \}\}' $self->{strContainer}"));
$self->{bActive} = true;
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'self', value => $self, trace => true}
);
}
####################################################################################################################################
# remove
####################################################################################################################################
sub remove
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my ($strOperation) = logDebugParam(__PACKAGE__ . '->remove');
if ($self->{bActive})
{
executeTest("docker rm -f $self->{strContainer}", {bSuppressError => true});
$self->{bActive} = false;
}
# Return from function and log return values if any
return logDebugReturn($strOperation);
}
####################################################################################################################################
# execute
####################################################################################################################################
sub execute
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strCommand,
$oParam,
$strUser
) =
logDebugParam
(
__PACKAGE__ . '->execute', \@_,
{name => 'strCommand'},
{name => 'oParam', required=> false},
{name => 'strUser', required => false}
);
# Set the user
if (!defined($strUser))
{
$strUser = $self->{strUser};
}
New simpler configuration and consistent project/exe/path naming. * The repo-path option now always refers to the repository where backups and archive are stored, whether local or remote, so the repo-remote-path option has been removed. The new spool-path option can be used to define a location for queueing WAL segments when archiving asynchronously. Otherwise, a local repository is no longer required. * Implemented a new config format which should be far simpler to use. See the User Guide and Configuration Reference for details but for a simple configuration all options can now be placed in the stanza section. Options that are shared between stanzas can be placed in the [global] section. More complex configurations can still make use of command sections though this should be a rare use case. * The default configuration filename is now pgbackrest.conf instead of pg_backrest.conf. This was done for consistency with other naming changes but also to prevent old config files from being loaded accidentally. * The default repository name was changed from /var/lib/backup to /var/lib/pgbackrest. * Lock files are now stored in /tmp/pgbackrest by default. These days /run/pgbackrest would be the preferred location but that would require init scripts which are not part of this release. The lock-path option can be used to configure the lock directory. * Log files are now stored in /var/log/pgbackrest by default and no longer have the date appended so they can be managed with logrotate. The log-path option can be used to configure the lock directory. * Executable filename changed from pg_backrest to pgbackrest.
2016-04-14 15:30:54 +02:00
my $oExec = new pgBackRestTest::Common::ExecuteTest(
"docker exec -u ${strUser} $self->{strContainer} ${strCommand}" , $oParam);
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'oExec', value => $oExec, trace => true}
);
}
####################################################################################################################################
# executeSimple
####################################################################################################################################
sub executeSimple
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strCommand,
$oParam,
$strUser
) =
logDebugParam
(
__PACKAGE__ . '->executeSimple', \@_,
{name => 'strCommand', trace => true},
{name => 'oParam', required=> false, trace => true},
{name => 'strUser', required => false, trace => true}
);
my $oExec = $self->execute($strCommand, $oParam, $strUser);
$oExec->begin();
$oExec->end();
# Return from function and log return values if any
return logDebugReturn
(
$strOperation,
{name => 'strOutLog', value => $oExec->{strOutLog}, trace => true}
);
}
####################################################################################################################################
# copyTo
####################################################################################################################################
sub copyTo
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strSource,
$strDestination,
$strOwner,
$strMode
) =
logDebugParam
(
__PACKAGE__ . '->copyTo', \@_,
{name => 'strSource'},
{name => 'strDestination'},
{name => 'strOwner', required => false},
{name => 'strMode', required => false}
);
executeTest("docker cp ${strSource} $self->{strContainer}:${strDestination}");
if (defined($strOwner))
{
$self->executeSimple("chown ${strOwner} ${strDestination}", undef, 'root');
}
if (defined($strMode))
{
$self->executeSimple("chmod ${strMode} ${strDestination}", undef, 'root');
}
# Return from function and log return values if any
return logDebugReturn($strOperation);
}
####################################################################################################################################
# copyFrom
####################################################################################################################################
sub copyFrom
{
my $self = shift;
# Assign function parameters, defaults, and log debug info
my
(
$strOperation,
$strSource,
$strDestination
) =
logDebugParam
(
__PACKAGE__ . '->copyFrom', \@_,
{name => 'strSource'},
{name => 'strDestination'}
);
executeTest("docker cp $self->{strContainer}:${strSource} ${strDestination}");
# Return from function and log return values if any
return logDebugReturn($strOperation);
}
####################################################################################################################################
# ipGet
####################################################################################################################################
sub ipGet
{
my $self = shift;
return $self->{strIP};
}
####################################################################################################################################
# nameGet
####################################################################################################################################
sub nameGet
{
my $self = shift;
return $self->{strName};
}
####################################################################################################################################
# nameTest
####################################################################################################################################
sub nameTest
{
my $self = shift;
my $strName = shift;
return $self->{strName} eq $strName;
}
####################################################################################################################################
# userGet
####################################################################################################################################
sub userGet
{
my $self = shift;
return $self->{strUser};
}
1;