2015-11-22 23:44:01 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# HostTest.pm - Encapsulate a docker host for testing
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRestTest::Common::HostTest;
|
2016-06-24 14:12:58 +02:00
|
|
|
use parent 'pgBackRestTest::Common::ParamTest';
|
2015-11-22 23:44:01 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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();
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
2015-11-22 23:44:01 +02:00
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRestTest::Common::ExecuteTest;
|
2015-11-22 23:44:01 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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},
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->{strContainer},
|
2015-11-22 23:44:01 +02:00
|
|
|
$self->{strImage},
|
|
|
|
$self->{strUser},
|
|
|
|
$self->{strOS},
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->{stryMount}
|
2015-11-22 23:44:01 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->new', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strName', trace => true},
|
2016-06-24 14:12:58 +02:00
|
|
|
{name => 'strContainer', trace => true},
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strImage', trace => true},
|
|
|
|
{name => 'strUser', trace => true},
|
|
|
|
{name => 'strOS', trace => true},
|
2016-11-07 17:06:35 +02:00
|
|
|
{name => 'stryMount', required => false, trace => true}
|
2015-11-22 23:44:01 +02:00
|
|
|
);
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
executeTest("docker rm -f $self->{strContainer}", {bSuppressError => true});
|
2015-11-22 23:44:01 +02:00
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
executeTest("docker run -itd -h $self->{strName} --name=$self->{strContainer}" .
|
|
|
|
(defined($self->{stryMount}) ? ' -v ' . join(' -v ', @{$self->{stryMount}}) : '') .
|
|
|
|
" $self->{strImage}");
|
2015-11-22 23:44:01 +02:00
|
|
|
|
2016-12-10 16:11:12 +02:00
|
|
|
# Get IP Address
|
2016-06-24 14:12:58 +02:00
|
|
|
$self->{strIP} = trim(executeTest("docker inspect --format '\{\{ .NetworkSettings.IPAddress \}\}' $self->{strContainer}"));
|
2015-11-22 23:44:01 +02:00
|
|
|
$self->{bActive} = true;
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self, trace => true}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# execute
|
|
|
|
####################################################################################################################################
|
|
|
|
sub execute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strCommand,
|
|
|
|
$oParam,
|
|
|
|
$strUser
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->execute', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strCommand'},
|
|
|
|
{name => 'oParam', required=> false},
|
|
|
|
{name => 'strUser', required => false}
|
|
|
|
);
|
|
|
|
|
|
|
|
# Set the user
|
|
|
|
if (!defined($strUser))
|
|
|
|
{
|
|
|
|
$strUser = $self->{strUser};
|
|
|
|
}
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
my $oExec = new pgBackRestTest::Common::ExecuteTest(
|
2016-06-24 14:12:58 +02:00
|
|
|
"docker exec -u ${strUser} $self->{strContainer} ${strCommand}" , $oParam);
|
2015-11-22 23:44:01 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->executeSimple', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{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();
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'strOutLog', value => $oExec->{strOutLog}, trace => true}
|
|
|
|
);
|
2015-11-22 23:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# copyTo
|
|
|
|
####################################################################################################################################
|
|
|
|
sub copyTo
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strSource,
|
|
|
|
$strDestination,
|
|
|
|
$strOwner,
|
|
|
|
$strMode
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->copyTo', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strSource'},
|
|
|
|
{name => 'strDestination'},
|
|
|
|
{name => 'strOwner', required => false},
|
|
|
|
{name => 'strMode', required => false}
|
|
|
|
);
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
executeTest("docker cp ${strSource} $self->{strContainer}:${strDestination}");
|
2015-11-22 23:44:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->copyFrom', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strSource'},
|
|
|
|
{name => 'strDestination'}
|
|
|
|
);
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
executeTest("docker cp $self->{strContainer}:${strSource} ${strDestination}");
|
2015-11-22 23:44:01 +02:00
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:12:58 +02:00
|
|
|
####################################################################################################################################
|
2016-08-24 18:39:27 +02:00
|
|
|
# ipGet
|
|
|
|
####################################################################################################################################
|
|
|
|
sub ipGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->{strIP};
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2016-06-24 14:12:58 +02:00
|
|
|
# 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};
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
1;
|