2016-12-23 15:22:59 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# ConfigCommonTest.pm - Common code for Config unit tests
|
|
|
|
####################################################################################################################################
|
2017-05-12 22:43:04 +02:00
|
|
|
package pgBackRestTest::Env::ConfigEnvTest;
|
2016-12-23 15:22:59 +02:00
|
|
|
use parent 'pgBackRestTest::Common::RunTest';
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Perl includes
|
|
|
|
####################################################################################################################################
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
use English '-no_match_vars';
|
|
|
|
|
2018-01-29 04:37:09 +02:00
|
|
|
use Getopt::Long qw(GetOptions);
|
|
|
|
|
2016-12-23 15:22:59 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Config::Config;
|
2018-02-05 19:32:30 +02:00
|
|
|
use pgBackRest::LibC qw(:test);
|
2018-01-29 04:37:09 +02:00
|
|
|
use pgBackRest::Version;
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
use pgBackRestTest::Common::RunTest;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
use constant CONFIGENVTEST => 'ConfigEnvTest';
|
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Is the option secure?
|
|
|
|
####################################################################################################################################
|
|
|
|
sub optionTestSecure
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strOption = shift;
|
|
|
|
|
|
|
|
return (cfgDefOptionSecure(cfgOptionId($strOption)) ? true : false);
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
sub optionTestSet
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2016-12-23 15:22:59 +02:00
|
|
|
my $strValue = shift;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$self->{&CONFIGENVTEST}{option}{cfgOptionName($iOptionId)} = $strValue;
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
sub optionTestSetBool
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
2016-12-23 15:22:59 +02:00
|
|
|
my $bValue = shift;
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$self->{&CONFIGENVTEST}{boolean}{cfgOptionName($iOptionId)} = defined($bValue) ? $bValue : true;
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
sub optionTestClear
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $iOptionId = shift;
|
|
|
|
|
|
|
|
delete($self->{&CONFIGENVTEST}{option}{cfgOptionName($iOptionId)});
|
|
|
|
delete($self->{&CONFIGENVTEST}{boolean}{cfgOptionName($iOptionId)});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub configTestClear
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $rhConfig = $self->{&CONFIGENVTEST};
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
delete($self->{&CONFIGENVTEST});
|
|
|
|
|
|
|
|
return $rhConfig;
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
sub configTestSet
|
2017-01-27 16:42:30 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $rhConfig = shift;
|
2017-01-27 16:42:30 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
$self->{&CONFIGENVTEST} = $rhConfig;
|
2017-01-27 16:42:30 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Write all secure options to a config file
|
|
|
|
####################################################################################################################################
|
|
|
|
sub configFileWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strConfigFile = shift;
|
|
|
|
my $rhConfig = shift;
|
|
|
|
|
|
|
|
my $strConfig = "[global]\n";
|
|
|
|
|
|
|
|
if (defined($rhConfig->{boolean}))
|
|
|
|
{
|
|
|
|
foreach my $strOption (sort(keys(%{$rhConfig->{boolean}})))
|
|
|
|
{
|
|
|
|
if ($self->optionTestSecure($strOption))
|
|
|
|
{
|
|
|
|
$strConfig .= "${strOption}=" . ($rhConfig->{boolean}{$strOption} ? 'y' : 'n') . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($rhConfig->{option}))
|
|
|
|
{
|
|
|
|
foreach my $strOption (sort(keys(%{$rhConfig->{option}})))
|
|
|
|
{
|
|
|
|
if ($self->optionTestSecure($strOption))
|
|
|
|
{
|
|
|
|
$strConfig .= "${strOption}=$rhConfig->{option}{$strOption}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storageTest()->put($strConfigFile, $strConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Write all non-secure options to the command line
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub commandTestWrite
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $strCommand = shift;
|
2018-03-15 17:09:46 +02:00
|
|
|
my $strConfigFile = shift;
|
2017-08-25 22:47:47 +02:00
|
|
|
my $rhConfig = shift;
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
my @szyParam = ();
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
# Add boolean options
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($rhConfig->{boolean}))
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
foreach my $strOption (sort(keys(%{$rhConfig->{boolean}})))
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
2018-03-15 17:09:46 +02:00
|
|
|
if (!$self->optionTestSecure($strOption))
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
2018-03-15 17:09:46 +02:00
|
|
|
if ($rhConfig->{boolean}{$strOption})
|
|
|
|
{
|
|
|
|
push(@szyParam, "--${strOption}");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
push(@szyParam, "--no-${strOption}");
|
|
|
|
}
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
# Add non-boolean options
|
2017-08-25 22:47:47 +02:00
|
|
|
if (defined($rhConfig->{option}))
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
2017-08-25 22:47:47 +02:00
|
|
|
foreach my $strOption (sort(keys(%{$rhConfig->{option}})))
|
2016-12-23 15:22:59 +02:00
|
|
|
{
|
2018-03-15 17:09:46 +02:00
|
|
|
if (!$self->optionTestSecure($strOption))
|
|
|
|
{
|
|
|
|
push(@szyParam, "--${strOption}=$rhConfig->{option}{$strOption}");
|
|
|
|
}
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
# Add config file
|
|
|
|
push(@szyParam, '--' . cfgOptionName(CFGOPT_CONFIG) . "=${strConfigFile}");
|
|
|
|
|
|
|
|
# Add command
|
2017-08-25 22:47:47 +02:00
|
|
|
push(@szyParam, $strCommand);
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2017-08-25 22:47:47 +02:00
|
|
|
return @szyParam;
|
|
|
|
}
|
2016-12-23 15:22:59 +02:00
|
|
|
|
2018-01-29 04:37:09 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Load the configuration
|
|
|
|
####################################################################################################################################
|
2017-08-25 22:47:47 +02:00
|
|
|
sub configTestLoad
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $iCommandId = shift;
|
|
|
|
|
2018-03-15 17:09:46 +02:00
|
|
|
# A config file is required to store secure options before they can be parsed
|
|
|
|
my $strConfigFile = $self->testPath() . '/pgbackrest.test.conf';
|
|
|
|
$self->configFileWrite($strConfigFile, $self->{&CONFIGENVTEST});
|
|
|
|
|
|
|
|
my @stryArg = $self->commandTestWrite(cfgCommandName($iCommandId), $strConfigFile, $self->{&CONFIGENVTEST});
|
2018-02-05 19:32:30 +02:00
|
|
|
my $strConfigJson = cfgParseTest(backrestBin(), join('|', @stryArg));
|
2018-01-29 04:37:09 +02:00
|
|
|
$self->testResult(
|
2018-03-08 23:24:16 +02:00
|
|
|
sub {configLoad(false, backrestBin(), cfgCommandName($iCommandId), \$strConfigJson)},
|
2018-02-05 19:32:30 +02:00
|
|
|
true, 'config load: ' . join(" ", @stryArg));
|
2016-12-23 15:22:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|