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;
|
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();
|
|
|
|
|
2020-03-10 21:41:56 +02:00
|
|
|
use pgBackRestDoc::Common::Log;
|
|
|
|
use pgBackRestDoc::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},
|
2017-02-10 17:22:05 +02:00
|
|
|
$self->{stryMount},
|
|
|
|
$self->{strOption},
|
2018-05-25 19:42:09 +02:00
|
|
|
$self->{strParam},
|
2019-05-27 13:37:20 +02:00
|
|
|
$self->{bHostUpdate},
|
2021-10-18 20:32:41 +02:00
|
|
|
$self->{strEntryPoint},
|
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},
|
2017-02-10 17:22:05 +02:00
|
|
|
{name => 'stryMount', required => false, trace => true},
|
|
|
|
{name => 'strOption', required => false, trace => true},
|
2018-05-25 19:42:09 +02:00
|
|
|
{name => 'strParam', required => false, trace => true},
|
2019-05-27 13:37:20 +02:00
|
|
|
{name => 'bHostUpdate', required => false, trace => true, default => true},
|
2021-10-18 20:32:41 +02:00
|
|
|
{name => 'strEntryPoint', 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}" .
|
2017-02-10 17:22:05 +02:00
|
|
|
(defined($self->{strOption}) ? ' ' . $self->{strOption} : '') .
|
2016-06-24 14:12:58 +02:00
|
|
|
(defined($self->{stryMount}) ? ' -v ' . join(' -v ', @{$self->{stryMount}}) : '') .
|
2021-10-18 20:32:41 +02:00
|
|
|
(defined($self->{strEntryPoint}) ? " --entrypoint=$self->{strEntryPoint} --user=$self->{strUser}" : '') .
|
2018-05-25 19:42:09 +02:00
|
|
|
" $self->{strImage} " . (defined($self->{strParam}) ? ' ' . $self->{strParam} : ''),
|
2017-02-21 15:59:23 +02:00
|
|
|
{bSuppressStdErr => true});
|
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,
|
2018-06-10 20:13:56 +02:00
|
|
|
$strUser,
|
|
|
|
$bLoadEnv,
|
|
|
|
$bBashWrap,
|
2015-11-22 23:44:01 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-06-24 14:12:58 +02:00
|
|
|
__PACKAGE__ . '->execute', \@_,
|
2015-11-22 23:44:01 +02:00
|
|
|
{name => 'strCommand'},
|
2018-06-10 20:13:56 +02:00
|
|
|
{name => 'oParam', required => false},
|
|
|
|
{name => 'strUser', required => false},
|
|
|
|
{name => 'bLoadEnv', optional => true, default => true},
|
|
|
|
{name => 'bBashWrap', optional => true, default => true},
|
2015-11-22 23:44:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
# Set the user
|
|
|
|
if (!defined($strUser))
|
|
|
|
{
|
|
|
|
$strUser = $self->{strUser};
|
|
|
|
}
|
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
$strCommand =~ s/'/'\\''/g;
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
my $oExec = new pgBackRestTest::Common::ExecuteTest(
|
2018-06-10 20:13:56 +02:00
|
|
|
"docker exec -u ${strUser} $self->{strContainer}" .
|
|
|
|
($bBashWrap ? " bash" . ($bLoadEnv ? ' -l' : '') . " -c '${strCommand}'" : " ${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,
|
2018-06-10 20:13:56 +02:00
|
|
|
$strUser,
|
|
|
|
$bLoadEnv,
|
|
|
|
$bBashWrap,
|
2015-11-22 23:44:01 +02:00
|
|
|
) =
|
|
|
|
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},
|
2018-06-10 20:13:56 +02:00
|
|
|
{name => 'strUser', required => false, trace => true},
|
|
|
|
{name => 'bLoadEnv', optional => true, default => true, trace => true},
|
|
|
|
{name => 'bBashWrap', optional => true, default => true},
|
2015-11-22 23:44:01 +02:00
|
|
|
);
|
|
|
|
|
2018-06-10 20:13:56 +02:00
|
|
|
my $oExec = $self->execute($strCommand, $oParam, $strUser, {bLoadEnv => $bLoadEnv});
|
2015-11-22 23:44:01 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:37:20 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# hostUpdateGet
|
|
|
|
####################################################################################################################################
|
|
|
|
sub hostUpdateGet
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return $self->{bHostUpdate};
|
|
|
|
}
|
|
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Getters
|
|
|
|
####################################################################################################################################
|
|
|
|
sub container {shift->{strContainer}}
|
|
|
|
|
2015-11-22 23:44:01 +02:00
|
|
|
1;
|