pgbackrest.git / summary / log / commit / refs

commit    3d84f2ce5e02c5bd7f9bdcbb3b4579b146bf7ccf
Author:   David Steele <david@pgmasters.net>
Date:     Mon Apr 10 17:24:45 2017 +0000

    Improvements to Ini.pm.
    
    * Refactor Ini.pm to facilitate testing.
    * Complete statement/branch coverage for Ini.pm.
    * Improved functions used to test/munge manifest and info files.


doc/lib/BackRestDoc/Common/DocExecute.pm | 4 +- doc/xml/release.xml | 12 + lib/pgBackRest/Archive/ArchiveInfo.pm | 2 +- lib/pgBackRest/Backup.pm | 2 +- lib/pgBackRest/BackupInfo.pm | 2 +- lib/pgBackRest/Common/Ini.pm | 644 ++++++++++++--------- lib/pgBackRest/Config/Config.pm | 2 +- lib/pgBackRest/Manifest.pm | 2 +- lib/pgBackRest/Restore.pm | 5 +- lib/pgBackRest/Stanza.pm | 2 +- test/expect/full-synthetic-001.log | 16 - test/expect/full-synthetic-002.log | 16 - test/expect/full-synthetic-003.log | 16 - test/expect/full-synthetic-004.log | 16 - test/expect/full-synthetic-005.log | 16 - test/expect/full-synthetic-006.log | 16 - test/expect/full-synthetic-007.log | 16 - test/expect/full-synthetic-008.log | 16 - test/lib/pgBackRestTest/Common/DefineTest.pm | 15 + .../pgBackRestTest/Common/Host/HostBackupTest.pm | 176 +++--- .../pgBackRestTest/Common/Host/HostDbCommonTest.pm | 59 +- test/lib/pgBackRestTest/Config/ConfigConfigTest.pm | 41 +- test/lib/pgBackRestTest/Full/FullSyntheticTest.pm | 55 +- test/lib/pgBackRestTest/Info/InfoIniUnitTest.pm | 471 +++++++++++++++ 24 files changed, 1034 insertions(+), 588 deletions(-) diff --git a/doc/lib/BackRestDoc/Common/DocExecute.pm b/doc/lib/BackRestDoc/Common/DocExecute.pm index 6b85927bb..e73df71a4 100644 --- a/doc/lib/BackRestDoc/Common/DocExecute.pm +++ b/doc/lib/BackRestDoc/Common/DocExecute.pm @@ -562,7 +562,7 @@ sub backrestConfig my $strLocalFile = '/home/' . DOC_USER . '/data/pgbackrest.conf'; # Save the ini file - iniSave($strLocalFile, $self->{config}{$strHostName}{$$hCacheKey{file}}, true); + fileStringWrite($strLocalFile, iniRender($self->{config}{$strHostName}{$$hCacheKey{file}}, true)); $oHost->copyTo( $strLocalFile, $$hCacheKey{file}, @@ -579,7 +579,7 @@ sub backrestConfig delete($$oConfigClean{&CONFIG_SECTION_GLOBAL}); } - iniSave("${strLocalFile}.clean", $oConfigClean, true); + fileStringWrite("${strLocalFile}.clean", iniRender($oConfigClean, true)); # Push config file into the cache $strConfig = fileStringRead("${strLocalFile}.clean"); diff --git a/doc/xml/release.xml b/doc/xml/release.xml index cadeac32c..1ef50d968 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -188,6 +188,10 @@ <p>Simplify locking scheme. Now, only the master process will hold write locks (archive-push, backup) and not all the local and remote worker processes as before.</p> </release-item> + <release-item> + <p>Refactor <code>Ini.pm</code> to facilitate testing.</p> + </release-item> + <release-item> <p>Allow functions to accept optional parameters as a hash.</p> </release-item> @@ -212,6 +216,14 @@ <release-test-list> <release-refactor-list> + <release-item> + <p>Complete statement/branch coverage for <code>Ini.pm</code>.</p> + </release-item> + + <release-item> + <p>Improved functions used to test/munge manifest and info files.</p> + </release-item> + <release-item> <p>Coverage testing always enabled on Debian-based containers.</p> </release-item> diff --git a/lib/pgBackRest/Archive/ArchiveInfo.pm b/lib/pgBackRest/Archive/ArchiveInfo.pm index 664c12d42..2bafdc1e9 100644 --- a/lib/pgBackRest/Archive/ArchiveInfo.pm +++ b/lib/pgBackRest/Archive/ArchiveInfo.pm @@ -91,7 +91,7 @@ sub new } # Init object and store variables - my $self = $class->SUPER::new($strArchiveInfoFile, $bExists); + my $self = $class->SUPER::new($strArchiveInfoFile, {bLoad => $bExists}); $self->{bExists} = $bExists; $self->{strArchiveClusterPath} = $strArchiveClusterPath; diff --git a/lib/pgBackRest/Backup.pm b/lib/pgBackRest/Backup.pm index d6af6e862..8c1d42050 100644 --- a/lib/pgBackRest/Backup.pm +++ b/lib/pgBackRest/Backup.pm @@ -320,7 +320,7 @@ sub processManifest foreach my $strRepoFile ( sort {sprintf("%016d-${b}", $oBackupManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $b, MANIFEST_SUBKEY_SIZE)) cmp sprintf("%016d-${a}", $oBackupManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $a, MANIFEST_SUBKEY_SIZE))} - ($oBackupManifest->keys(MANIFEST_SECTION_TARGET_FILE, 'none'))) + ($oBackupManifest->keys(MANIFEST_SECTION_TARGET_FILE, INI_SORT_NONE))) { # If the file has a reference it does not need to be copied since it can be retrieved from the referenced backup. # However, if hard-linking is turned on the link will need to be created diff --git a/lib/pgBackRest/BackupInfo.pm b/lib/pgBackRest/BackupInfo.pm index 4e88be508..e5bc3d54c 100644 --- a/lib/pgBackRest/BackupInfo.pm +++ b/lib/pgBackRest/BackupInfo.pm @@ -136,7 +136,7 @@ sub new } # Init object and store variables - my $self = $class->SUPER::new($strBackupInfoFile, $bExists); + my $self = $class->SUPER::new($strBackupInfoFile, {bLoad => $bExists}); $self->{bExists} = $bExists; $self->{strBackupClusterPath} = $strBackupClusterPath; diff --git a/lib/pgBackRest/Common/Ini.pm b/lib/pgBackRest/Common/Ini.pm index cac669e54..8e3f27e6f 100644 --- a/lib/pgBackRest/Common/Ini.pm +++ b/lib/pgBackRest/Common/Ini.pm @@ -6,6 +6,7 @@ package pgBackRest::Common::Ini; use strict; use warnings FATAL => qw(all); use Carp qw(confess); +use English '-no_match_vars'; use Exporter qw(import); our @EXPORT = qw(); @@ -42,212 +43,331 @@ use constant INI_KEY_FORMAT => 'backrest use constant INI_KEY_VERSION => 'backrest-version'; push @EXPORT, qw(INI_KEY_VERSION); -use constant INI_COMMENT => '[comment]'; +#################################################################################################################################### +# Ini sort orders +#################################################################################################################################### +use constant INI_SORT_FORWARD => 'forward'; + push @EXPORT, qw(INI_SORT_FORWARD); +use constant INI_SORT_REVERSE => 'reverse'; + push @EXPORT, qw(INI_SORT_REVERSE); +use constant INI_SORT_NONE => 'none'; + push @EXPORT, qw(INI_SORT_NONE); #################################################################################################################################### -# CONSTRUCTOR +# new() #################################################################################################################################### sub new { my $class = shift; # Class name - my $strFileName = shift; # Manifest filename - my $bLoad = shift; # Load the ini? # Create the class hash my $self = {}; bless $self, $class; - # Filename must be specified - if (!defined($strFileName)) - { - confess &log(ASSERT, 'filename must be provided'); - } + # Assign function parameters, defaults, and log debug info + ( + my $strOperation, + $self->{strFileName}, + my $bLoad, + my $strContent, + $self->{iInitFormat}, + $self->{strInitVersion}, + ) = + logDebugParam + ( + __PACKAGE__ . '->new', \@_, + {name => 'strFileName', trace => true}, + {name => 'bLoad', optional => true, default => true, trace => true}, + {name => 'strContent', optional => true, trace => true}, + {name => 'iInitFormat', optional => true, default => BACKREST_FORMAT, trace => true}, + {name => 'strInitVersion', optional => true, default => BACKREST_VERSION, trace => true}, + ); # Set variables - my $oContent = {}; - $self->{oContent} = $oContent; - $self->{strFileName} = $strFileName; + $self->{oContent} = {}; + + # Set changed to false + $self->{bModified} = false; - # Load the ini if specified - if (!defined($bLoad) || $bLoad) + # Set exists to false + $self->{bExists} = false; + + # Load the file if requested + if ($bLoad) { $self->load(); + } + # Load from a string if provided + elsif (defined($strContent)) + { + $self->{oContent} = iniParse($strContent); + $self->headerCheck(); + } + # Else initialize + else + { + $self->numericSet(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, $self->{iInitFormat}); + $self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion}); + } + return $self; +} + +#################################################################################################################################### +# load() - load the ini. +#################################################################################################################################### +sub load +{ + my $self = shift; + + $self->{oContent} = iniParse(fileStringRead($self->{strFileName})); + $self->headerCheck(); + $self->{bExists} = true; +} + +#################################################################################################################################### +# headerCheck() - check that version and checksum in header are as expected +#################################################################################################################################### +sub headerCheck +{ + my $self = shift; + + # Assign function parameters, defaults, and log debug info + my + ( + $strOperation, + $bIgnoreInvalid, + ) = + logDebugParam + ( + __PACKAGE__ . '->headerCheck', \@_, + {name => 'bIgnoreInvalid', optional => true, default => false, trace => true}, + ); + + # Eval so exceptions can be ignored on bIgnoreInvalid + my $bValid = true; + + eval + { # Make sure the ini is valid by testing checksum - my $strChecksum = $self->get(INI_SECTION_BACKREST, INI_KEY_CHECKSUM); + my $strChecksum = $self->get(INI_SECTION_BACKREST, INI_KEY_CHECKSUM, undef, false); my $strTestChecksum = $self->hash(); - if ($strChecksum ne $strTestChecksum) + if (!defined($strChecksum) || $strChecksum ne $strTestChecksum) { - confess &log(ERROR, "${strFileName} checksum is invalid, should be ${strTestChecksum} but found ${strChecksum}", - ERROR_CHECKSUM); + confess &log(ERROR, + "invalid checksum in '$self->{strFileName}', expected '${strTestChecksum}' but found " . + (defined($strChecksum) ? "'${strChecksum}'" : '[undef]'), + ERROR_CHECKSUM); } # Make sure that the format is current, otherwise error my $iFormat = $self->get(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, false, 0); - if ($iFormat != BACKREST_FORMAT) + if ($iFormat != $self->{iInitFormat}) { - confess &log(ERROR, "format of ${strFileName} is ${iFormat} but " . BACKREST_FORMAT . ' is required', ERROR_FORMAT); + confess &log(ERROR, + "invalid format in '$self->{strFileName}', expected $self->{iInitFormat} but found ${iFormat}", ERROR_FORMAT); } # Check if the version has changed - if (!$self->test(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, BACKREST_VERSION)) + if (!$self->test(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion})) { - $self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, BACKREST_VERSION); + $self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, $self->{strInitVersion}); } + + return true; } - else + or do { - $self->numericSet(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, BACKREST_FORMAT); - $self->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, BACKREST_VERSION); - } + # Confess the error if it should not be ignored + if (!$bIgnoreInvalid) + { + confess $EVAL_ERROR; + } - return $self; -} + # Return false when errors are ignored + $bValid = false; + }; -#################################################################################################################################### -# load -# -# Load the ini. -#################################################################################################################################### -sub load -{ - my $self = shift; - - iniLoad($self->{strFileName}, $self->{oContent}); + # Return from function and log return values if any + return logDebugReturn + ( + $strOperation, + {name => 'bValid', value => $bValid, trace => true} + ); } #################################################################################################################################### -# iniLoad -# -# Load file from standard INI format to a hash. +# iniParse() - parse from standard INI format to a hash. #################################################################################################################################### -push @EXPORT, qw(iniLoad); +push @EXPORT, qw(iniParse); -sub iniLoad +sub iniParse { - my $strFileName = shift; - my $oContent = shift; - my $bRelaxed = shift; + # Assign function parameters, defaults, and log debug info + my + ( + $strOperation, + $strContent, + $bRelaxed, + $bIgnoreInvalid, + ) = + logDebugParam + ( + __PACKAGE__ . '::iniParse', \@_, + {name => 'strContent', trace => true}, + {name => 'bRelaxed', optional => true, default => false, trace => true}, + {name => 'bIgnoreInvalid', optional => true, default => false, trace => true}, + ); - # Open the ini file for reading - my $hFile; + # Ini content + my $oContent = undef; my $strSection; - open($hFile, '<', $strFileName) - or confess &log(ERROR, "unable to open ${strFileName}"); - # Create the JSON object my $oJSON = JSON::PP->new()->allow_nonref(); - # Read the INI file - while (my $strLine = readline($hFile)) + # Eval so exceptions can be ignored on bIgnoreInvalid + eval { - $strLine = trim($strLine); - - # Skip lines that are blank or comments - if ($strLine ne '' && $strLine !~ '^[ ]*#.*') + # Read the INI file + foreach my $strLine (split("\n", $strContent)) { - # Get the section - if (index($strLine, '[') == 0) - { - $strSection = substr($strLine, 1, length($strLine) - 2); - } - else - { - # Get key and value - my $iIndex = index($strLine, '='); + $strLine = trim($strLine); - if ($iIndex == -1) + # Skip lines that are blank or comments + if ($strLine ne '' && $strLine !~ '^[ ]*#.*') + { + # Get the section + if (index($strLine, '[') == 0) { - confess &log(ERROR, "unable to read from ${strFileName}: ${strLine}"); + $strSection = substr($strLine, 1, length($strLine) - 2); } + else + { + if (!defined($strSection)) + { + confess &log(ERROR, "key/value pair '${strLine}' found outside of a section", ERROR_CONFIG); + } - my $strKey = substr($strLine, 0, $iIndex); - my $strValue = substr($strLine, $iIndex + 1); + # Get key and value + my $iIndex = index($strLine, '='); - # If relaxed then read the value directly - if ($bRelaxed) - { - if (defined($$oContent{$strSection}{$strKey})) + if ($iIndex == -1) + { + confess &log(ERROR, "unable to find '=' in '${strLine}'", ERROR_CONFIG); + } + + my $strKey = substr($strLine, 0, $iIndex); + my $strValue = substr($strLine, $iIndex + 1); + + # If relaxed then read the value directly + if ($bRelaxed) { - if (ref($$oContent{$strSection}{$strKey}) ne 'ARRAY') + if (defined($oContent->{$strSection}{$strKey})) { - $$oContent{$strSection}{$strKey} = [$$oContent{$strSection}{$strKey}]; - } + if (ref($oContent->{$strSection}{$strKey}) ne 'ARRAY') + { + $oContent->{$strSection}{$strKey} = [$oContent->{$strSection}{$strKey}]; + } - push(@{$$oContent{$strSection}{$strKey}}, $strValue); + push(@{$oContent->{$strSection}{$strKey}}, $strValue); + } + else + { + $oContent->{$strSection}{$strKey} = $strValue; + } } + # Else read the value as stricter JSON else { - $$oContent{$strSection}{$strKey} = $strValue; + ${$oContent}{$strSection}{$strKey} = $oJSON->decode($strValue); } } - # Else read the value as stricter JSON - else - { - ${$oContent}{$strSection}{$strKey} = $oJSON->decode($strValue); - } } } + + # Error if the file is empty + if (!($bRelaxed || defined($oContent))) + { + confess &log(ERROR, 'no key/value pairs found', ERROR_CONFIG); + } + + return true; } + or do + { + # Confess the error if it should not be ignored + if (!$bIgnoreInvalid) + { + confess $EVAL_ERROR; + } + + # Undef content when errors are ignored + undef($oContent); + }; - close($hFile); - return($oContent); + # Return from function and log return values if any + return logDebugReturn + ( + $strOperation, + {name => 'oContent', value => $oContent, trace => true} + ); } #################################################################################################################################### -# save -# -# Save the file. +# save() - save the file. #################################################################################################################################### sub save { my $self = shift; - $self->hash(); - iniSave($self->{strFileName}, $self->{oContent}, false, true); + if ($self->{bModified}) + { + # Calculate the hash + $self->hash(); + + # Save the file + fileStringWrite($self->{strFileName}, iniRender($self->{oContent})); + $self->{bModified} = false; - # Indicate the file now exists - $self->{bExists} = true; + # Indicate the file now exists + $self->{bExists} = true; + + # File was saved + return true; + } + + # File was not saved + return false; } #################################################################################################################################### -# iniSave -# -# Save from a hash to standard INI format. +# iniRender() - render hash to standard INI format. #################################################################################################################################### -push @EXPORT, qw(iniSave); +push @EXPORT, qw(iniRender); -sub iniSave +sub iniRender { # Assign function parameters, defaults, and log debug info my ( $strOperation, - $strFileName, $oContent, $bRelaxed, - $bTemp ) = logDebugParam ( - __PACKAGE__ . '::iniSave', \@_, - {name => 'strFileName', trace => true}, + __PACKAGE__ . '::iniRender', \@_, {name => 'oContent', trace => true}, {name => 'bRelaxed', default => false, trace => true}, - {name => 'bTemp', default => false, trace => true} ); # Open the ini file for writing - my $strFileTemp = $bTemp ? "${strFileName}.new" : $strFileName; - my $hFile; + my $strContent = ''; my $bFirst = true; - sysopen($hFile, $strFileTemp, O_WRONLY | O_CREAT | O_TRUNC, 0640) - or confess &log(ERROR, "unable to open ${strFileTemp}"); - # Create the JSON object canonical so that fields are alpha ordered to pass unit tests my $oJSON = JSON::PP->new()->canonical()->allow_nonref(); @@ -257,35 +377,15 @@ sub iniSave # Add a linefeed between sections if (!$bFirst) { - syswrite($hFile, "\n") - or confess "unable to write lf: $!"; - } - - # Write the section comment if present - if (defined(${$oContent}{$strSection}{&INI_COMMENT})) - { - my $strComment = trim(${$oContent}{$strSection}{&INI_COMMENT}); - $strComment =~ s/\n/\n# /g; - - # syswrite($hFile, ('#' x 80) . "\n# ${strComment}\n" . ('#' x 80) . "\n") - # or confess "unable to comment for section ${strSection}: $!"; - syswrite($hFile, "# ${strComment}\n") - or confess "unable to comment for section ${strSection}: $!"; + $strContent .= "\n"; } # Write the section - syswrite($hFile, "[${strSection}]\n") - or confess "unable to write section ${strSection}: $!"; + $strContent .= "[${strSection}]\n"; # Iterate through all keys in the section - foreach my $strKey (sort(keys(%{${$oContent}{"${strSection}"}}))) + foreach my $strKey (sort(keys(%{$oContent->{$strSection}}))) { - # Skip comments - if ($strKey eq INI_COMMENT) - { - next; - } - # If the value is a hash then convert it to JSON, otherwise store as is my $strValue = ${$oContent}{$strSection}{$strKey}; @@ -297,181 +397,145 @@ sub iniSave { foreach my $strArrayValue (@{$strValue}) { - syswrite($hFile, "${strKey}=${strArrayValue}\n") - or confess "unable to write relaxed key array ${strKey}: $!"; + $strContent .= "${strKey}=${strArrayValue}\n"; } } # Else write a standard key/value pair else { - syswrite($hFile, "${strKey}=${strValue}\n") - or confess "unable to write relaxed key ${strKey}: $!"; + $strContent .= "${strKey}=${strValue}\n"; } } # Else write as stricter JSON else { - syswrite($hFile, "${strKey}=" . $oJSON->encode($strValue) . "\n") - or confess "unable to write json key ${strKey}: $!"; + $strContent .= "${strKey}=" . $oJSON->encode($strValue) . "\n"; } } $bFirst = false; } - # Sync and close temp file - $hFile->sync(); - close($hFile); - - # Rename temp file to ini file - if ($bTemp && !rename($strFileTemp, $strFileName)) - { - unlink($strFileTemp); - confess &log(ERROR, "unable to move ${strFileTemp} to ${strFileName}", ERROR_FILE_MOVE); - } - # Return from function and log return values if any - return logDebugReturn($strOperation); + return logDebugReturn + ( + $strOperation, + {name => 'strContent', value => $strContent, trace => true} + ); } #################################################################################################################################### -# hash -# -# Generate hash for the manifest. +# hash() - generate hash for the manifest. #################################################################################################################################### sub hash { my $self = shift; # Remove the old checksum - $self->remove(INI_SECTION_BACKREST, INI_KEY_CHECKSUM); + delete($self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM}); # Calculate the checksum my $oChecksumContent = dclone($self->{oContent}); - - foreach my $strSection (keys(%$oChecksumContent)) - { - delete(${$oChecksumContent}{$strSection}{&INI_COMMENT}); - } - my $oSHA = Digest::SHA->new('sha1'); my $oJSON = JSON::PP->new()->canonical()->allow_nonref(); - $oSHA->add($oJSON->encode($oChecksumContent)); + $oSHA->add($oJSON->encode($self->{oContent})); # Set the new checksum - my $strHash = $oSHA->hexdigest(); - - $self->set(INI_SECTION_BACKREST, INI_KEY_CHECKSUM, undef, $strHash); + $self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = $oSHA->hexdigest(); - return $strHash; + return $self->{oContent}{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM}; } #################################################################################################################################### -# get -# -# Get a value. +# get() - get a value. #################################################################################################################################### sub get { my $self = shift; my $strSection = shift; - my $strValue = shift; - my $strSubValue = shift; + my $strKey = shift; + my $strSubKey = shift; my $bRequired = shift; my $oDefault = shift; - my $oContent = $self->{oContent}; - - # Section must always be defined + # Parameter constraints if (!defined($strSection)) { - confess &log(ASSERT, 'section is not defined'); + confess &log(ASSERT, 'strSection is required'); } - # Set default for required - $bRequired = defined($bRequired) ? $bRequired : true; + if (defined($strSubKey) && !defined($strKey)) + { + confess &log(ASSERT, "strKey is required when strSubKey '${strSubKey}' is requested"); + } - # Store the result - my $oResult = undef; + # Get the result + my $oResult = $self->{oContent}->{$strSection}; - if (defined($strSubValue)) + if (defined($strKey) && defined($oResult)) { - if (!defined($strValue)) - { - confess &log(ASSERT, "subvalue '${strSubValue}' requested but value is not defined"); - } + $oResult = $oResult->{$strKey}; - if (defined(${$oContent}{$strSection}{$strValue})) + if (defined($strSubKey) && defined($oResult)) { - $oResult = ${$oContent}{$strSection}{$strValue}{$strSubValue}; + $oResult = $oResult->{$strSubKey}; } } - elsif (defined($strValue)) + + # When result is not defined + if (!defined($oResult)) { - if (defined(${$oContent}{$strSection})) + # Error if a result is required + if (!defined($bRequired) || $bRequired) { - $oResult = ${$oContent}{$strSection}{$strValue}; + confess &log(ASSERT, "strSection '$strSection'" . (defined($strKey) ? ", strKey '$strKey'" : '') . + (defined($strSubKey) ? ", strSubKey '$strSubKey'" : '') . ' is required but not defined'); } - } - else - { - $oResult = ${$oContent}{$strSection}; - } - if (!defined($oResult) && $bRequired) - { - confess &log(ASSERT, "manifest section '$strSection'" . (defined($strValue) ? ", value '$strValue'" : '') . - (defined($strSubValue) ? ", subvalue '$strSubValue'" : '') . ' is required but not defined'); - } - - if (!defined($oResult) && defined($oDefault)) - { - $oResult = $oDefault; + # Return default if specified + if (defined($oDefault)) + { + return $oDefault; + } } return $oResult } #################################################################################################################################### -# boolGet -# -# Get a numeric value. +# boolGet() - get a boolean value. #################################################################################################################################### sub boolGet { my $self = shift; my $strSection = shift; - my $strValue = shift; - my $strSubValue = shift; + my $strKey = shift; + my $strSubKey = shift; my $bRequired = shift; my $bDefault = shift; - return $self->get($strSection, $strValue, $strSubValue, $bRequired, - defined($bDefault) ? ($bDefault ? INI_TRUE : INI_FALSE) : undef) ? true : false; + return $self->get( + $strSection, $strKey, $strSubKey, $bRequired, + defined($bDefault) ? ($bDefault ? INI_TRUE : INI_FALSE) : undef) ? true : false; } #################################################################################################################################### -# numericGet -# -# Get a numeric value. +# numericGet() - get a numeric value. #################################################################################################################################### sub numericGet { my $self = shift; my $strSection = shift; - my $strValue = shift; - my $strSubValue = shift; + my $strKey = shift; + my $strSubKey = shift; my $bRequired = shift; my $nDefault = shift; - return $self->get($strSection, $strValue, $strSubValue, $bRequired, - defined($nDefault) ? $nDefault + 0 : undef) + 0; + return $self->get($strSection, $strKey, $strSubKey, $bRequired, defined($nDefault) ? $nDefault + 0 : undef) + 0; } #################################################################################################################################### -# set -# -# Set a value. +# set - set a value. #################################################################################################################################### sub set { @@ -479,24 +543,44 @@ sub set my $strSection = shift; my $strKey = shift; my $strSubKey = shift; - my $strValue = shift; + my $oValue = shift; + + # Parameter constraints + if (!(defined($strSection) && defined($strKey))) + { + confess &log(ASSERT, 'strSection and strKey are required'); + } - my $oContent = $self->{oContent}; + my $oCurrentValue; if (defined($strSubKey)) { - ${$oContent}{$strSection}{$strKey}{$strSubKey} = $strValue; + $oCurrentValue = \$self->{oContent}{$strSection}{$strKey}{$strSubKey}; } else { - ${$oContent}{$strSection}{$strKey} = $strValue; + $oCurrentValue = \$self->{oContent}{$strSection}{$strKey}; } + + if (!defined($$oCurrentValue) || + defined($oCurrentValue) != defined($oValue) || + ${dclone($oCurrentValue)} ne ${dclone(\$oValue)}) + { + $$oCurrentValue = $oValue; + + if (!$self->{bModified}) + { + $self->{bModified} = true; + } + + return true; + } + + return false; } #################################################################################################################################### -# boolSet -# -# Set a boolean value. +# boolSet - set a boolean value. #################################################################################################################################### sub boolSet { @@ -510,9 +594,7 @@ sub boolSet } #################################################################################################################################### -# numericSet -# -# Set a numeric value. +# numericSet - set a numeric value. #################################################################################################################################### sub numericSet { @@ -522,29 +604,11 @@ sub numericSet my $strSubKey = shift; my $nValue = shift; - $self->set($strSection, $strKey, $strSubKey, $nValue + 0); + $self->set($strSection, $strKey, $strSubKey, defined($nValue) ? $nValue + 0 : undef); } #################################################################################################################################### -# commentSet -# -# Set a section comment. -#################################################################################################################################### -# sub commentSet -# { -# my $self = shift; -# my $strSection = shift; -# my $strComment = shift; -# -# my $oContent = $self->{oContent}; -# -# ${$oContent}{$strSection}{&INI_COMMENT} = $strComment; -# } - -#################################################################################################################################### -# remove -# -# Remove a value. +# remove - remove a value. #################################################################################################################################### sub remove { @@ -552,28 +616,51 @@ sub remove my $strSection = shift; my $strKey = shift; my $strSubKey = shift; - my $strValue = shift; - my $oContent = $self->{oContent}; - - if (defined($strSubKey)) - { - delete(${$oContent}{$strSection}{$strKey}{$strSubKey}); - } - elsif (defined($strKey)) - { - delete(${$oContent}{$strSection}{$strKey}); - } - else + # Test if the value exists + if ($self->test($strSection, $strKey, $strSubKey)) { - delete(${$oContent}{$strSection}); + # Remove a subkey + if (defined($strSubKey)) + { + delete($self->{oContent}{$strSection}{$strKey}{$strSubKey}); + } + + # Remove a key + if (defined($strKey)) + { + if (!defined($strSubKey)) + { + delete($self->{oContent}{$strSection}{$strKey}); + } + + # Remove the section if it is now empty + if (keys(%{$self->{oContent}{$strSection}}) == 0) + { + delete($self->{oContent}{$strSection}); + } + } + + # Remove a section + if (!defined($strKey)) + { + delete($self->{oContent}{$strSection}); + } + + # Record changes + if (!$self->{bModified}) + { + $self->{bModified} = true; + } + + return true; } + + return false; } #################################################################################################################################### -# keys -# -# Get a list of keys. +# keys - get the list of keys in a section. #################################################################################################################################### sub keys { @@ -581,22 +668,17 @@ sub keys my $strSection = shift; my $strSortOrder = shift; - if (!defined($strSection)) - { - confess &log(ASSERT, 'strSection must be set'); - } - if ($self->test($strSection)) { - if (!defined($strSortOrder) || $strSortOrder eq 'forward') + if (!defined($strSortOrder) || $strSortOrder eq INI_SORT_FORWARD) { return (sort(keys(%{$self->get($strSection)}))); } - elsif ($strSortOrder eq 'reverse') + elsif ($strSortOrder eq INI_SORT_REVERSE) { return (sort {$b cmp $a} (keys(%{$self->get($strSection)}))); } - elsif ($strSortOrder eq 'none') + elsif ($strSortOrder eq INI_SORT_NONE) { return (keys(%{$self->get($strSection)})); } @@ -611,9 +693,10 @@ sub keys } #################################################################################################################################### -# test +# test - test a value. # -# Test a value to see if it equals the supplied test value. If no test value is given, tests that it is defined. +# Test a value to see if it equals the supplied test value. If no test value is given, tests that the section, key, or subkey +# is defined. #################################################################################################################################### sub test { @@ -623,10 +706,13 @@ sub test my $strSubValue = shift; my $strTest = shift; + # Get the value my $strResult = $self->get($strSection, $strValue, $strSubValue, false); + # Is there a result if (defined($strResult)) { + # Is there a value to test against? if (defined($strTest)) { return $strResult eq $strTest ? true : false; @@ -639,9 +725,7 @@ sub test } #################################################################################################################################### -# boolTest -# -# Test a value to see if it equals the supplied test boolean value. If no test value is given, tests that it is defined. +# boolTest - test a boolean value, see test(). #################################################################################################################################### sub boolTest { @@ -654,4 +738,10 @@ sub boolTest return $self->test($strSection, $strValue, $strSubValue, defined($bTest) ? ($bTest ? INI_TRUE : INI_FALSE) : undef); } +#################################################################################################################################### +# Properties. +#################################################################################################################################### +sub modified {shift->{bModified}} # Has the data been modified since last load/save? +sub exists {shift->{bExists}} # Is the data persisted to file? + 1; diff --git a/lib/pgBackRest/Config/Config.pm b/lib/pgBackRest/Config/Config.pm index 4c4597f57..5ab1aff34 100644 --- a/lib/pgBackRest/Config/Config.pm +++ b/lib/pgBackRest/Config/Config.pm @@ -2421,7 +2421,7 @@ sub optionValidate confess &log(ERROR, "'${strConfigFile}' is not a file", ERROR_FILE_INVALID); } - $oConfig = iniLoad($strConfigFile, undef, true); + $oConfig = iniParse(fileStringRead($strConfigFile), {bRelaxed => true}); } } diff --git a/lib/pgBackRest/Manifest.pm b/lib/pgBackRest/Manifest.pm index e9468df19..190197608 100644 --- a/lib/pgBackRest/Manifest.pm +++ b/lib/pgBackRest/Manifest.pm @@ -305,7 +305,7 @@ sub new $bLoad = defined($bLoad) ? $bLoad : true; # Init object and store variables - my $self = $class->SUPER::new($strFileName, $bLoad); + my $self = $class->SUPER::new($strFileName, {bLoad => $bLoad}); # Return from function and log return values if any return logDebugReturn diff --git a/lib/pgBackRest/Restore.pm b/lib/pgBackRest/Restore.pm index 39308fadc..36bea8df4 100644 --- a/lib/pgBackRest/Restore.pm +++ b/lib/pgBackRest/Restore.pm @@ -13,6 +13,7 @@ use File::stat qw(lstat); use pgBackRest::BackupInfo; use pgBackRest::Common::Exception; +use pgBackRest::Common::Ini; use pgBackRest::Common::Log; use pgBackRest::Config::Config; use pgBackRest::DbVersion; @@ -597,7 +598,7 @@ sub clean # Clean up each target starting from the most nested my %oFileChecked; - for my $strTarget (sort {$b cmp $a} ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET))) + for my $strTarget ($oManifest->keys(MANIFEST_SECTION_BACKUP_TARGET, INI_SORT_REVERSE)) { if ($oTargetFound{$strTarget}) { @@ -1207,7 +1208,7 @@ sub process foreach my $strRepoFile ( sort {sprintf("%016d-${b}", $oManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $b, MANIFEST_SUBKEY_SIZE)) cmp sprintf("%016d-${a}", $oManifest->numericGet(MANIFEST_SECTION_TARGET_FILE, $a, MANIFEST_SUBKEY_SIZE))} - ($oManifest->keys(MANIFEST_SECTION_TARGET_FILE, 'none'))) + ($oManifest->keys(MANIFEST_SECTION_TARGET_FILE, INI_SORT_NONE))) { # Skip the tablespace_map file in versions >= 9.5 so Postgres does not rewrite links in DB_PATH_PGTBLSPC. # The tablespace links have already been created by Restore::build(). diff --git a/lib/pgBackRest/Stanza.pm b/lib/pgBackRest/Stanza.pm index 0c3084549..6e4b8dc86 100644 --- a/lib/pgBackRest/Stanza.pm +++ b/lib/pgBackRest/Stanza.pm @@ -327,7 +327,7 @@ sub infoFileCreate { ($bReconstruct, $strWarningMsgArchive) = $self->reconstructCheck($oInfo, $strPathType, $oFile, $strParentPath); - if ($oInfo->{bExists} && $bReconstruct) + if ($oInfo->exists() && $bReconstruct) { # If force was not used and the hashes are different then error if (!optionGet(OPTION_FORCE)) diff --git a/test/expect/full-synthetic-001.log b/test/expect/full-synthetic-001.log index 3a9a306d2..e9f7356d5 100644 --- a/test/expect/full-synthetic-001.log +++ b/test/expect/full-synthetic-001.log @@ -2272,22 +2272,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-002.log b/test/expect/full-synthetic-002.log index 04f5eba05..feb21d956 100644 --- a/test/expect/full-synthetic-002.log +++ b/test/expect/full-synthetic-002.log @@ -1929,22 +1929,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-003.log b/test/expect/full-synthetic-003.log index 5928c7c97..51f8cb073 100644 --- a/test/expect/full-synthetic-003.log +++ b/test/expect/full-synthetic-003.log @@ -1832,22 +1832,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-004.log b/test/expect/full-synthetic-004.log index 17e3d506f..70f6e4558 100644 --- a/test/expect/full-synthetic-004.log +++ b/test/expect/full-synthetic-004.log @@ -1923,22 +1923,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/repo/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/repo/log --repo-path=[TEST_PATH]/db-master/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-005.log b/test/expect/full-synthetic-005.log index 6c3377eda..ccdae2224 100644 --- a/test/expect/full-synthetic-005.log +++ b/test/expect/full-synthetic-005.log @@ -2361,22 +2361,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-006.log b/test/expect/full-synthetic-006.log index 75f4fdb4d..de333e0f3 100644 --- a/test/expect/full-synthetic-006.log +++ b/test/expect/full-synthetic-006.log @@ -2081,22 +2081,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --no-compress --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-007.log b/test/expect/full-synthetic-007.log index 5d72bca0c..6c2adb22f 100644 --- a/test/expect/full-synthetic-007.log +++ b/test/expect/full-synthetic-007.log @@ -1978,22 +1978,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/expect/full-synthetic-008.log b/test/expect/full-synthetic-008.log index 60ceacdb9..4c1400807 100644 --- a/test/expect/full-synthetic-008.log +++ b/test/expect/full-synthetic-008.log @@ -2069,22 +2069,6 @@ P00 DETAIL: check [TEST_PATH]/db-master/db/base exists P00 ERROR: [115]: cannot restore to path '[TEST_PATH]/db-master/db/base' that contains files - try using --delta if this is what you intended P00 INFO: restore command end: aborted with exception [115] -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on undef format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - -restore, backup '[BACKUP-DIFF-2]', expect exit 104 - fail on mismatch format (db-master host) -> [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------- -P00 INFO: restore command begin [BACKREST-VERSION]: --backup-cmd=[BACKREST-BIN] --backup-config=[TEST_PATH]/backup/pgbackrest.conf --backup-host=backup --backup-user=[USER-2] --config=[TEST_PATH]/db-master/pgbackrest.conf --db-path=[TEST_PATH]/db-master/db/base --lock-path=[TEST_PATH]/db-master/spool/lock --log-level-console=detail --log-level-file=trace --log-level-stderr=off --log-path=[TEST_PATH]/db-master/spool/log --repo-path=[TEST_PATH]/backup/repo --set=[BACKUP-DIFF-2] --stanza=db -P00 INFO: restore backup set [BACKUP-DIFF-2] -P00 ERROR: [104]: format of [TEST_PATH]/db-master/db/base/backup.manifest is 0 but 5 is required -P00 INFO: restore command end: aborted with exception [104] - restore, backup '[BACKUP-DIFF-2]', remap - remap all paths (db-master host) > [CONTAINER-EXEC] db-master [BACKREST-BIN] --config=[TEST_PATH]/db-master/pgbackrest.conf --set=[BACKUP-DIFF-2] --log-level-console=detail --stanza=db restore ------------------------------------------------------------------------------------------------------------------------------------ diff --git a/test/lib/pgBackRestTest/Common/DefineTest.pm b/test/lib/pgBackRestTest/Common/DefineTest.pm index 65ea2ac55..4fbcaeca5 100644 --- a/test/lib/pgBackRestTest/Common/DefineTest.pm +++ b/test/lib/pgBackRestTest/Common/DefineTest.pm @@ -77,6 +77,11 @@ use constant TESTDEF_MODULE_ARCHIVE_PUSH_FILE => TESTDEF_M use constant TESTDEF_MODULE_BACKUP_COMMON => 'BackupCommon'; push @EXPORT, qw(TESTDEF_MODULE_BACKUP_COMMON); +use constant TESTDEF_MODULE_COMMON => 'Common'; + push @EXPORT, qw(TESTDEF_MODULE_COMMON); +use constant TESTDEF_MODULE_COMMON_INI => TESTDEF_MODULE_COMMON . '/Ini'; + push @EXPORT, qw(TESTDEF_MODULE_COMMON_INI); + use constant TESTDEF_MODULE_INFO => 'Info'; push @EXPORT, qw(TESTDEF_MODULE_INFO); @@ -206,6 +211,16 @@ my $oTestDef = &TESTDEF_TEST => [ + { + &TESTDEF_NAME => 'ini-unit', + &TESTDEF_CONTAINER => true, + &TESTDEF_TOTAL => 10, + + &TESTDEF_COVERAGE => + { + &TESTDEF_MODULE_COMMON_INI => TESTDEF_COVERAGE_FULL, + }, + }, { &TESTDEF_NAME => 'unit', &TESTDEF_TOTAL => 1, diff --git a/test/lib/pgBackRestTest/Common/Host/HostBackupTest.pm b/test/lib/pgBackRestTest/Common/Host/HostBackupTest.pm index 76cc51d88..6533ae259 100644 --- a/test/lib/pgBackRestTest/Common/Host/HostBackupTest.pm +++ b/test/lib/pgBackRestTest/Common/Host/HostBackupTest.pm @@ -13,6 +13,7 @@ use Carp qw(confess); use Exporter qw(import); our @EXPORT = qw(); +use File::Basename qw(dirname); use Storable qw(dclone); use pgBackRest::BackupCommon; @@ -240,8 +241,8 @@ sub backupEnd # because a running database is always in flux. Even so, it allows us test many things. if (!$self->synthetic()) { - $oExpectedManifest = - iniLoad($self->repoPath() . '/backup/' . $self->stanza() . "/${strBackup}/" . FILE_MANIFEST, $oExpectedManifest); + $oExpectedManifest = iniParse( + fileStringRead($self->repoPath() . '/backup/' . $self->stanza() . "/${strBackup}/" . FILE_MANIFEST)); } # Make sure tablespace links are correct @@ -494,8 +495,8 @@ sub backupCompare my $strTestPath = $self->testPath(); - iniSave("${strTestPath}/actual.manifest", $oActualManifest->{oContent}); - iniSave("${strTestPath}/expected.manifest", $oExpectedManifest); + fileStringWrite("${strTestPath}/actual.manifest", iniRender($oActualManifest->{oContent})); + fileStringWrite("${strTestPath}/expected.manifest", iniRender($oExpectedManifest)); executeTest("diff ${strTestPath}/expected.manifest ${strTestPath}/actual.manifest"); @@ -1032,7 +1033,10 @@ sub configCreate } # Write out the configuration file - iniSave($self->backrestConfig(), \%oParamHash, true); + fileStringWrite($self->backrestConfig(), iniRender(\%oParamHash, true)); + + # Modify the file permissions so it can be read/saved by all test users + executeTest('sudo chmod 660 ' . $self->backrestConfig()); } #################################################################################################################################### @@ -1050,59 +1054,48 @@ sub manifestMunge ( $strOperation, $strBackup, - $strSection, - $strKey, - $strSubKey, - $strValue, + $hParam, + $bCache, ) = logDebugParam ( __PACKAGE__ . '->manifestMunge', \@_, - {name => '$strBackup'}, - {name => '$strSection'}, - {name => '$strKey'}, - {name => '$strSubKey', required => false}, - {name => '$strValue', required => false}, + {name => 'strBackup'}, + {name => '$hParam'}, + {name => 'bCache', default => true}, ); - my $strManifestFile = "${strBackup}/" . FILE_MANIFEST; + $self->infoMunge($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, "${strBackup}/" . FILE_MANIFEST), $hParam, $bCache); - # Change mode on the backup path so it can be read/written - if ($self->nameTest(HOST_BACKUP)) - { - executeTest('sudo chmod g+w ' . $self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, $strManifestFile)); - } + # Return from function and log return values if any + return logDebugReturn($strOperation); +} - # Read the manifest - my %oManifest; - iniLoad($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, $strManifestFile), \%oManifest); +#################################################################################################################################### +# manifestRestore +#################################################################################################################################### +sub manifestRestore +{ + my $self = shift; - # Write in the munged value - if (defined($strSubKey)) - { - if (defined($strValue)) - { - $oManifest{$strSection}{$strKey}{$strSubKey} = $strValue; - } - else - { - delete($oManifest{$strSection}{$strKey}{$strSubKey}); - } - } - else - { - if (defined($strValue)) - { - $oManifest{$strSection}{$strKey} = $strValue; - } - else - { - delete($oManifest{$strSection}{$strKey}); - } - } + # Assign function parameters, defaults, and log debug info + my + ( + $strOperation, + $strBackup, + $bSave, + ) = + logDebugParam + ( + __PACKAGE__ . '->manifestRestore', \@_, + {name => 'strBackup'}, + {name => 'bSave', default => true}, + ); - # Resave the manifest - $self->iniSaveChecksum($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, $strManifestFile), \%oManifest, true); + $self->infoRestore($self->{oFile}->pathGet(PATH_BACKUP_CLUSTER, "${strBackup}/" . FILE_MANIFEST), $bSave); + + # Return from function and log return values if any + return logDebugReturn($strOperation); } #################################################################################################################################### @@ -1121,38 +1114,64 @@ sub infoMunge ( $strOperation, $strFileName, - $hParam + $hParam, + $bCache, ) = logDebugParam ( __PACKAGE__ . '->infoMunge', \@_, {name => 'strFileName'}, - {name => 'hParam'} + {name => 'hParam'}, + {name => 'bCache', default => true}, ); # If the original file content does not exist then load it if (!defined($self->{hInfoFile}{$strFileName})) { - $self->{hInfoFile}{$strFileName} = iniLoad($strFileName); - # Modify the file permissions so it can be saved - executeTest("sudo chmod 660 ${strFileName}"); + $self->{hInfoFile}{$strFileName} = new pgBackRest::Common::Ini($strFileName); } # Make a copy of the original file contents - my $hContent = dclone($self->{hInfoFile}{$strFileName}); + my $oMungeIni = new pgBackRest::Common::Ini( + $strFileName, {bLoad => false, strContent => iniRender($self->{hInfoFile}{$strFileName}->{oContent})}); # Load params - foreach my $strSection (sort(keys(%{$hParam}))) + foreach my $strSection (keys(%{$hParam})) { - foreach my $strKey (keys(%{$$hParam{$strSection}})) + foreach my $strKey (keys(%{$hParam->{$strSection}})) { - # Munge the copy with the new parameter values - $$hContent{$strSection}{$strKey} = $$hParam{$strSection}{$strKey}; + if (ref($hParam->{$strSection}{$strKey}) eq 'HASH') + { + foreach my $strSubKey (keys(%{$hParam->{$strSection}{$strKey}})) + { + # Munge the copy with the new parameter values + $oMungeIni->set($strSection, $strKey, $strSubKey, $hParam->{$strSection}{$strKey}{$strSubKey}); + } + } + else + { + # Munge the copy with the new parameter values + $oMungeIni->set($strSection, $strKey, undef, $hParam->{$strSection}{$strKey}); + } } } + # Modify the file/directory permissions so it can be saved + executeTest("sudo rm -f ${strFileName} && sudo chmod 770 " . dirname($strFileName)); + # Save the munged data to the file - $self->iniSaveChecksum($strFileName, \%{$hContent}, true); + $oMungeIni->save(); + + # Fix permissions + executeTest( + "sudo chmod 640 ${strFileName} && sudo chmod 750 " . dirname($strFileName) . + ' && sudo chown ' . $self->userGet() . " ${strFileName}"); + + # Clear the cache is requested + if (!$bCache) + { + delete($self->{hInfoFile}{$strFileName}); + } # Return from function and log return values if any return logDebugReturn($strOperation); @@ -1179,7 +1198,7 @@ sub infoRestore ( __PACKAGE__ . '->infoRestore', \@_, {name => 'strFileName'}, - {name => 'bSave', default => true, required => false}, + {name => 'bSave', default => true}, ); # If the original file content exists in the global hash, then save it to the file @@ -1187,7 +1206,15 @@ sub infoRestore { if ($bSave) { - iniSave($strFileName, $self->{hInfoFile}{$strFileName}); + # Modify the file/directory permissions so it can be saved + executeTest("sudo rm -f ${strFileName} && sudo chmod 770 " . dirname($strFileName)); + + # Save the munged data to the file + $self->{hInfoFile}{$strFileName}->{bModified} = true; + $self->{hInfoFile}{$strFileName}->save(); + + # Fix permissions + executeTest("sudo chmod 640 ${strFileName} && sudo chmod 750 " . dirname($strFileName)); } } else @@ -1202,33 +1229,6 @@ sub infoRestore return logDebugReturn($strOperation); } -#################################################################################################################################### -# iniSaveChecksum -# -# Save an ini file an optionall recalculate the checksum so it's valid. -#################################################################################################################################### -sub iniSaveChecksum -{ - my $self = shift; - my $strFileName = shift; - my $oIniRef = shift; - my $bChecksum = shift; - - # Calculate a new checksum if requested - if (defined($bChecksum) && $bChecksum) - { - delete($$oIniRef{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM}); - - my $oSHA = Digest::SHA->new('sha1'); - my $oJSON = JSON::PP->new()->canonical()->allow_nonref(); - $oSHA->add($oJSON->encode($oIniRef)); - - $$oIniRef{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = $oSHA->hexdigest(); - } - - iniSave($strFileName, $oIniRef); -} - #################################################################################################################################### # Getters #################################################################################################################################### diff --git a/test/lib/pgBackRestTest/Common/Host/HostDbCommonTest.pm b/test/lib/pgBackRestTest/Common/Host/HostDbCommonTest.pm index 5612f9f11..7e0f77614 100644 --- a/test/lib/pgBackRestTest/Common/Host/HostDbCommonTest.pm +++ b/test/lib/pgBackRestTest/Common/Host/HostDbCommonTest.pm @@ -15,6 +15,7 @@ use DBI; use Exporter qw(import); our @EXPORT = qw(); use Fcntl ':mode'; +use File::Basename qw(dirname); use Storable qw(dclone); use pgBackRest::Common::Exception; @@ -181,16 +182,7 @@ sub configRecovery my $strStanza = $self->stanza(); # Load db config file - my %oConfig; - iniLoad($self->backrestConfig(), \%oConfig, true); - - # Load backup config file - my %oRemoteConfig; - - if ($oHostBackup->nameTest(HOST_BACKUP)) - { - iniLoad($oHostBackup->backrestConfig(), \%oRemoteConfig, true); - } + my $oConfig = iniParse(fileStringRead($self->backrestConfig()), {bRelaxed => true}); # Rewrite recovery options my @stryRecoveryOption; @@ -202,17 +194,11 @@ sub configRecovery if (@stryRecoveryOption) { - $oConfig{$strStanza}{&OPTION_RESTORE_RECOVERY_OPTION} = \@stryRecoveryOption; + $oConfig->{$strStanza}{&OPTION_RESTORE_RECOVERY_OPTION} = \@stryRecoveryOption; } # Save db config file - iniSave($self->backrestConfig(), \%oConfig, true); - - # Save backup config file - if ($oHostBackup->nameTest(HOST_BACKUP)) - { - iniSave($oHostBackup->backrestConfig(), \%oRemoteConfig, true); - } + fileStringWrite($self->backrestConfig(), iniRender($oConfig, true)); } #################################################################################################################################### @@ -228,22 +214,21 @@ sub configRemap my $strStanza = $self->stanza(); # Load db config file - my %oConfig; - iniLoad($self->backrestConfig(), \%oConfig, true); + my $oConfig = iniParse(fileStringRead($self->backrestConfig()), {bRelaxed => true}); # Load backup config file - my %oRemoteConfig; + my $oRemoteConfig; my $oHostBackup = !$self->standby() && !$self->nameTest($self->backupDestination()) ? hostGroupGet()->hostGet($self->backupDestination()) : undef; if (defined($oHostBackup)) { - iniLoad($oHostBackup->backrestConfig(), \%oRemoteConfig, true); + $oRemoteConfig = iniParse(fileStringRead($oHostBackup->backrestConfig()), {bRelaxed => true}); } # Rewrite recovery section - delete($oConfig{"${strStanza}:restore"}{&OPTION_TABLESPACE_MAP}); + delete($oConfig->{"${strStanza}:restore"}{&OPTION_TABLESPACE_MAP}); my @stryTablespaceMap; foreach my $strRemap (sort(keys(%$oRemapHashRef))) @@ -252,13 +237,13 @@ sub configRemap if ($strRemap eq MANIFEST_TARGET_PGDATA) { - $oConfig{$strStanza}{&OPTION_DB_PATH} = $strRemapPath; + $oConfig->{$strStanza}{&OPTION_DB_PATH} = $strRemapPath; ${$oManifestRef}{&MANIFEST_SECTION_BACKUP_TARGET}{&MANIFEST_TARGET_PGDATA}{&MANIFEST_SUBKEY_PATH} = $strRemapPath; if (defined($oHostBackup)) { my $bForce = $oHostBackup->nameTest(HOST_BACKUP) && defined(hostGroupGet()->hostGet(HOST_DB_STANDBY, true)); - $oRemoteConfig{$strStanza}{optionIndex(OPTION_DB_PATH, 1, $bForce)} = $strRemapPath; + $oRemoteConfig->{$strStanza}{optionIndex(OPTION_DB_PATH, 1, $bForce)} = $strRemapPath; } } else @@ -273,16 +258,25 @@ sub configRemap if (@stryTablespaceMap) { - $oConfig{"${strStanza}:restore"}{&OPTION_TABLESPACE_MAP} = \@stryTablespaceMap; + $oConfig->{"${strStanza}:restore"}{&OPTION_TABLESPACE_MAP} = \@stryTablespaceMap; } # Save db config file - iniSave($self->backrestConfig(), \%oConfig, true); + fileStringWrite($self->backrestConfig(), iniRender($oConfig, true)); # Save backup config file (but not is this is the standby which is not the source of backups) if (defined($oHostBackup)) { - iniSave($oHostBackup->backrestConfig(), \%oRemoteConfig, true); + # Modify the file permissions so it can be read/saved by all test users + executeTest( + 'sudo chmod 660 ' . $oHostBackup->backrestConfig() . ' && sudo chmod 770 ' . dirname($oHostBackup->backrestConfig())); + + fileStringWrite($oHostBackup->backrestConfig(), iniRender($oRemoteConfig, true)); + + # Fix permissions + executeTest( + 'sudo chmod 660 ' . $oHostBackup->backrestConfig() . ' && sudo chmod 770 ' . dirname($oHostBackup->backrestConfig()) . + ' && sudo chown ' . $oHostBackup->userGet() . ' ' . $oHostBackup->backrestConfig()); } } @@ -676,9 +670,8 @@ sub restoreCompare if ($self->synthetic()) { - $oActualManifest->remove(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TIMESTAMP_COPY_START); - $oActualManifest->remove(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_LABEL); - $oActualManifest->remove(MANIFEST_SECTION_BACKUP, MANIFEST_KEY_TYPE); + $oActualManifest->remove(MANIFEST_SECTION_BACKUP); + delete($oExpectedManifestRef->{&MANIFEST_SECTION_BACKUP}); } else { @@ -730,8 +723,8 @@ sub restoreCompare $self->manifestDefault($oExpectedManifestRef); - iniSave("${strTestPath}/actual.manifest", $oActualManifest->{oContent}); - iniSave("${strTestPath}/expected.manifest", $oExpectedManifestRef); + fileStringWrite("${strTestPath}/actual.manifest", iniRender($oActualManifest->{oContent})); + fileStringWrite("${strTestPath}/expected.manifest", iniRender($oExpectedManifestRef)); executeTest("diff ${strTestPath}/expected.manifest ${strTestPath}/actual.manifest"); diff --git a/test/lib/pgBackRestTest/Config/ConfigConfigTest.pm b/test/lib/pgBackRestTest/Config/ConfigConfigTest.pm index 21628d3ba..e6cd65132 100644 --- a/test/lib/pgBackRestTest/Config/ConfigConfigTest.pm +++ b/test/lib/pgBackRestTest/Config/ConfigConfigTest.pm @@ -15,6 +15,7 @@ use pgBackRest::Common::Exception; use pgBackRest::Common::Ini; use pgBackRest::Common::Log; use pgBackRest::Config::Config; +use pgBackRest::FileCommon; use pgBackRestTest::Common::RunTest; @@ -71,7 +72,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza() . ':' . &CMD_BACKUP}{&OPTION_PROCESS_MAX} = 2; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -85,7 +86,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza()}{&OPTION_PROCESS_MAX} = 3; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -99,7 +100,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_BACKUP}{'thread-max'} = 2; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -113,7 +114,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_PROCESS_MAX} = 5; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -126,7 +127,7 @@ sub run if ($self->begin('default - option ' . OPTION_PROCESS_MAX)) { $oConfig = {}; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -140,7 +141,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_PROCESS_MAX} = 9; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -155,7 +156,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_BACKUP}{&OPTION_HARDLINK} = 'Y'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -168,7 +169,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_LOG_LEVEL_CONSOLE} = BOGUS; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -181,7 +182,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_LOG_LEVEL_CONSOLE} = lc(INFO); - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -202,7 +203,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza() . ':' . &CMD_EXPIRE}{&OPTION_RETENTION_FULL} = 2; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_CONFIG, $strConfigFile); @@ -215,7 +216,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_BACKUP}{&OPTION_COMPRESS} = 'n'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -229,7 +230,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_RESTORE}{&OPTION_RESTORE_RECOVERY_OPTION} = 'bogus='; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -242,7 +243,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_RESTORE}{&OPTION_RESTORE_RECOVERY_OPTION} = '=bogus'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -256,7 +257,7 @@ sub run $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL . ':' . &CMD_RESTORE}{&OPTION_RESTORE_RECOVERY_OPTION} = 'archive-command=/path/to/pgbackrest'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -270,7 +271,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza()}{&OPTION_RESTORE_RECOVERY_OPTION} = ['standby-mode=on', 'a=b']; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -285,7 +286,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza()}{&OPTION_DB_PATH} = '/path/to/db'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_CONFIG, $strConfigFile); @@ -298,7 +299,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza()}{&OPTION_DB_PATH} = '/path/to/db'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_CONFIG, $strConfigFile); @@ -313,7 +314,7 @@ sub run { $oConfig = {}; $$oConfig{$self->stanza()}{&OPTION_DB_PATH} = '/path/to/db'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_CONFIG, $strConfigFile); @@ -326,7 +327,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_REPO_PATH} = '/repo'; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); @@ -340,7 +341,7 @@ sub run { $oConfig = {}; $$oConfig{&CONFIG_SECTION_GLOBAL}{&OPTION_REPO_PATH} = ['/repo', '/repo2']; - iniSave($strConfigFile, $oConfig, true); + fileStringWrite($strConfigFile, iniRender($oConfig, true)); $self->optionSetTest($oOption, OPTION_STANZA, $self->stanza()); $self->optionSetTest($oOption, OPTION_DB_PATH, '/db'); diff --git a/test/lib/pgBackRestTest/Full/FullSyntheticTest.pm b/test/lib/pgBackRestTest/Full/FullSyntheticTest.pm index 214c42aa4..d8cab2b0a 100644 --- a/test/lib/pgBackRestTest/Full/FullSyntheticTest.pm +++ b/test/lib/pgBackRestTest/Full/FullSyntheticTest.pm @@ -11,9 +11,10 @@ use strict; use warnings FATAL => qw(all); use Carp qw(confess); -use File::Basename qw(dirname); +use File::Basename qw(basename dirname); use pgBackRest::Archive::ArchiveInfo; +use pgBackRest::BackupCommon; use pgBackRest::BackupInfo; use pgBackRest::DbVersion; use pgBackRest::Common::Exception; @@ -429,14 +430,16 @@ sub run testPathMove($oHostBackup->repoPath() . '/backup/' . $self->stanza() . "/${strFullBackup}", $strTmpPath); - my $oMungeManifest = new pgBackRest::Manifest("$strTmpPath/backup.manifest"); - $oMungeManifest->remove(MANIFEST_SECTION_TARGET_FILE, MANIFEST_TARGET_PGDATA . '/' . DB_FILE_PGVERSION, 'checksum'); - $oMungeManifest->save(); + $oHostBackup->infoMunge( + "$strTmpPath/" . FILE_MANIFEST, + {&MANIFEST_SECTION_TARGET_FILE => + {(&MANIFEST_TARGET_PGDATA . '/' . &DB_FILE_PGVERSION) => {&MANIFEST_SUBKEY_CHECKSUM => undef}}}, + false); # Create a temp file in backup temp root to be sure it's deleted correctly executeTest("touch ${strTmpPath}/file.tmp" . ($bCompress ? '.gz' : ''), {bRemote => $bRemote}); - executeTest("sudo chmod -R g+w " . dirname($strTmpPath)); + executeTest("sudo chown -R " . $oHostBackup->userGet() . ' ' . dirname($strTmpPath)); $strFullBackup = $oHostBackup->backup( $strType, 'resume', @@ -567,9 +570,10 @@ sub run # Munge the user to make sure it gets reset on the next run $oHostBackup->manifestMunge( - $strFullBackup, MANIFEST_SECTION_TARGET_FILE, MANIFEST_FILE_PGCONTROL, MANIFEST_SUBKEY_USER, 'bogus'); - $oHostBackup->manifestMunge( - $strFullBackup, MANIFEST_SECTION_TARGET_FILE, MANIFEST_FILE_PGCONTROL, MANIFEST_SUBKEY_GROUP, 'bogus'); + $strFullBackup, + {&MANIFEST_SECTION_TARGET_FILE => + {&MANIFEST_FILE_PGCONTROL => {&MANIFEST_SUBKEY_USER => 'bogus', &MANIFEST_SUBKEY_GROUP => 'bogus'}}}, + false); # Restore succeeds $oHostDbMaster->manifestLinkMap(\%oManifest, MANIFEST_TARGET_PGDATA . '/pg_stat'); @@ -760,11 +764,13 @@ sub run $strTmpPath = $oHostBackup->repoPath() . '/temp/' .$self->stanza() . '.tmp'; testPathMove($oHostBackup->repoPath() . '/backup/' . $self->stanza() . "/${strBackup}", $strTmpPath); - executeTest("sudo chmod -R g+w " . dirname($strTmpPath)); + executeTest("sudo chown -R " . $oHostBackup->userGet() . ' ' . dirname($strTmpPath)); - $oMungeManifest = new pgBackRest::Manifest("$strTmpPath/" . FILE_MANIFEST); - $oMungeManifest->set(MANIFEST_SECTION_TARGET_FILE, MANIFEST_TARGET_PGDATA . '/badchecksum.txt', 'checksum', 'bogus'); - $oMungeManifest->save(); + $oHostBackup->infoMunge( + "$strTmpPath/" . FILE_MANIFEST, + {&MANIFEST_SECTION_TARGET_FILE => + {(&MANIFEST_TARGET_PGDATA . '/badchecksum.txt') => {&MANIFEST_SUBKEY_CHECKSUM => BOGUS}}}, + false); # Add tablespace 2 $oHostDbMaster->manifestTablespaceCreate(\%oManifest, 2); @@ -800,7 +806,7 @@ sub run $strTmpPath = $oHostBackup->repoPath() . '/temp/' . $self->stanza() . '.tmp'; testPathMove($oHostBackup->repoPath() . '/backup/' . $self->stanza() . "/${strBackup}", $strTmpPath); - executeTest("sudo chmod -R g+w " . dirname($strTmpPath)); + executeTest("sudo chown -R " . $oHostBackup->userGet() . ' ' . dirname($strTmpPath)); $strBackup = $oHostBackup->backup( $strType, 'cannot resume - new diff', @@ -814,7 +820,7 @@ sub run $strTmpPath = $oHostBackup->repoPath() . '/temp/' . $self->stanza() . '.tmp'; testPathMove($oHostBackup->repoPath() . '/backup/' . $self->stanza() . "/${strBackup}", $strTmpPath); - executeTest("sudo chmod -R g+w " . dirname($strTmpPath)); + executeTest("sudo chown -R " . $oHostBackup->userGet() . ' ' . dirname($strTmpPath)); $strBackup = $oHostBackup->backup( $strType, 'cannot resume - disabled / no repo link', @@ -831,22 +837,6 @@ sub run $strBackup, \%oManifest, undef, $bDelta, $bForce, undef, undef, undef, undef, undef, undef, 'fail on used path', ERROR_PATH_NOT_EMPTY, '--log-level-console=detail'); - # Fail on undef format - $oHostBackup->manifestMunge($strBackup, INI_SECTION_BACKREST, INI_KEY_FORMAT); - - $oHostDbMaster->restore( - $strBackup, \%oManifest, undef, $bDelta, $bForce, undef, undef, undef, undef, undef, undef, - 'fail on undef format', ERROR_FORMAT, '--log-level-console=detail'); - - # Fail on mismatch format - $oHostBackup->manifestMunge($strBackup, INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, 0); - - $oHostDbMaster->restore( - $strBackup, \%oManifest, undef, $bDelta, $bForce, undef, undef, undef, undef, undef, undef, - 'fail on mismatch format', ERROR_FORMAT, '--log-level-console=detail'); - - $oHostBackup->manifestMunge($strBackup, INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, BACKREST_FORMAT); - # Remap the base and tablespace paths my %oRemapHash; $oRemapHash{&MANIFEST_TARGET_PGDATA} = $oHostDbMaster->dbBasePath(2); @@ -887,7 +877,7 @@ sub run 'e324463005236d83e6e54795dbddd20a74533bf3', $lTime, undef, undef, false); # Munge the version to make sure it gets corrected on the next run - $oHostBackup->manifestMunge($strBackup, INI_SECTION_BACKREST, INI_KEY_VERSION, undef, '0.00'); + $oHostBackup->manifestMunge($strBackup, {&INI_SECTION_BACKREST => {&INI_KEY_VERSION => '0.00'}}, false); $strBackup = $oHostBackup->backup( $strType, 'add files and remove tablespace 2', @@ -1035,7 +1025,8 @@ sub run # Munge the prior manifest so that option-checksum-page is missing to be sure the logic works for backups before page # checksums were introduced - $oHostBackup->manifestMunge($strFullBackup, MANIFEST_SECTION_BACKUP_OPTION, MANIFEST_KEY_CHECKSUM_PAGE, undef, undef); + $oHostBackup->manifestMunge( + $strFullBackup, {&MANIFEST_SECTION_BACKUP_OPTION => {&MANIFEST_KEY_CHECKSUM_PAGE => undef}}, false); $strBackup = $oHostBackup->backup( $strType, 'add file', {oExpectedManifest => \%oManifest, diff --git a/test/lib/pgBackRestTest/Info/InfoIniUnitTest.pm b/test/lib/pgBackRestTest/Info/InfoIniUnitTest.pm new file mode 100644 index 000000000..7421562a2 --- /dev/null +++ b/test/lib/pgBackRestTest/Info/InfoIniUnitTest.pm @@ -0,0 +1,471 @@ +#################################################################################################################################### +# InfoIniUnitTest.pm - Unit tests for Ini module +#################################################################################################################################### +package pgBackRestTest::Info::InfoIniUnitTest; +use parent 'pgBackRestTest::Common::RunTest'; + +#################################################################################################################################### +# Perl includes +#################################################################################################################################### +use strict; +use warnings FATAL => qw(all); +use Carp qw(confess); +use English '-no_match_vars'; + +use pgBackRest::Common::Exception; +use pgBackRest::Common::Ini; +use pgBackRest::Common::Log; +use pgBackRest::FileCommon; +use pgBackRest::Version; + +use pgBackRestTest::Common::ExecuteTest; +use pgBackRestTest::Common::RunTest; + +#################################################################################################################################### +# iniHeader +#################################################################################################################################### +sub iniHeader +{ + my $self = shift; + my $oIni = shift; + my $iFormat = shift; + my $iVersion = shift; + my $strChecksum = shift; + + return + "[backrest]" . + "\nbackrest-checksum=\"" . + (defined($strChecksum) ? $strChecksum : $oIni->get(INI_SECTION_BACKREST, INI_KEY_CHECKSUM)) . "\"" . + "\nbackrest-format=" . (defined($iFormat) ? $iFormat : $oIni->get(INI_SECTION_BACKREST, INI_KEY_FORMAT)) . + "\nbackrest-version=\"" . (defined($iVersion) ? $iVersion : $oIni->get(INI_SECTION_BACKREST, INI_KEY_VERSION)) . "\"" . + "\n"; +} + +#################################################################################################################################### +# run +#################################################################################################################################### +sub run +{ + my $self = shift; + + # Test ini file + my $strTestFile = $self->testPath() . '/test.ini'; + my $strTestFileCopy = "${strTestFile}.copy"; + + # Test keys, values + my $strSection = 'test-section'; + my $strKey = 'test-key'; + my $strSubKey = 'test-subkey'; + my $strValue = 'test-value'; + + ################################################################################################################################ + if ($self->begin("iniRender()")) + { + #--------------------------------------------------------------------------------------------------------------------------- + my $oIni = {section1 => {key => 'value'}, section2 => {key => ['value1', 'value2']}}; + + $self->testResult( + sub {iniRender($oIni, true)}, "[section1]\nkey=value\n\n[section2]\nkey=value1\nkey=value2\n", + 'relaxed formatting'); + $self->testResult( + sub {iniRender($oIni, false)}, "[section1]\nkey=\"value\"\n\n[section2]\nkey=[\"value1\",\"value2\"]\n", + 'strict formatting'); + } + + ################################################################################################################################ + if ($self->begin("iniParse()")) + { + #--------------------------------------------------------------------------------------------------------------------------- + my $strIni = "[section]\nkey"; + + $self->testException(sub {iniParse($strIni)}, ERROR_CONFIG, "unable to find '=' in 'key'"); + + #--------------------------------------------------------------------------------------------------------------------------- + $strIni = "[section]\n# comment\n\nkey=\"value\""; + + $self->testResult(sub {iniParse($strIni)}, '{section => {key => value}}', 'ignore blank lines and comments'); + + #--------------------------------------------------------------------------------------------------------------------------- + $strIni = "[section]\nkey=value"; + + $self->testResult(sub {iniParse($strIni, {bRelaxed => true})}, '{section => {key => value}}', 'relaxed value read'); + + #--------------------------------------------------------------------------------------------------------------------------- + $strIni = "[section]\nkey=value1\nkey=value2\nkey=value3"; + + $self->testResult( + sub {iniParse($strIni, {bRelaxed => true})}, '{section => {key => (value1, value2, value3)}}', 'relaxed array read'); + } + + ################################################################################################################################ + if ($self->begin("Ini->new()")) + { + #--------------------------------------------------------------------------------------------------------------------------- + my $oIni = new pgBackRest::Common::Ini( + $strTestFile, {bLoad => false, iInitFormat => 4, strInitVersion => '1.01'}); + + $self->testResult($oIni->exists(), false, 'file does not exist'); + + $oIni->save(); + + $self->testResult( + sub {fileStringRead($strTestFile)}, + $self->iniHeader(undef, 4, '1.01', '488e5ca1a018cd7cd6d4e15150548f39f493dacd'), + 'empty with synthetic format and version'); + + #--------------------------------------------------------------------------------------------------------------------------- + fileStringWrite($strTestFile); + $self->testException(sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_CONFIG, 'no key/value pairs found'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + $oIni->save(); + + $self->testResult( + sub {fileStringRead($strTestFile)}, + $self->iniHeader(undef, BACKREST_FORMAT, BACKREST_VERSION, $oIni->hash()), + 'empty with default format and version'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {new pgBackRest::Common::Ini($strTestFile)}, '[object]', 'normal load'); + + #--------------------------------------------------------------------------------------------------------------------------- + my $hIni = iniParse(fileStringRead($strTestFile)); + $hIni->{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = BOGUS; + fileStringWrite($strTestFile, iniRender($hIni)); + fileStringWrite($strTestFileCopy, iniRender($hIni)); + + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_CHECKSUM, + "invalid checksum in '${strTestFile}', expected '" . + $oIni->get(INI_SECTION_BACKREST, INI_KEY_CHECKSUM) . "' but found 'bogus'"); + + $hIni->{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = $oIni->hash(); + fileStringWrite($strTestFile, iniRender($hIni)); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->numericSet(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, BACKREST_FORMAT - 1); + $oIni->save(); + + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_FORMAT, + "invalid format in '${strTestFile}', expected " . BACKREST_FORMAT . ' but found ' . (BACKREST_FORMAT - 1)); + + $oIni->numericSet(INI_SECTION_BACKREST, INI_KEY_FORMAT, undef, BACKREST_FORMAT); + $oIni->save(); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->set(INI_SECTION_BACKREST, INI_KEY_VERSION, undef, '1.01'); + $oIni->save(); + + $self->testResult( + sub {fileStringRead($strTestFile)}, + $self->iniHeader($oIni, undef, '1.01'), + 'verify old version was written'); + + $oIni = new pgBackRest::Common::Ini($strTestFile); + + $self->testResult(sub {$oIni->get(INI_SECTION_BACKREST, INI_KEY_VERSION)}, BACKREST_VERSION, 'version is updated on load'); + $self->testResult(sub {$oIni->save()}, true, 'save changes'); + + $self->testResult( + sub {fileStringRead($strTestFile)}, + $self->iniHeader($oIni, undef, BACKREST_VERSION), + 'verify version is updated on load'); + + $self->testResult(sub {$oIni->save()}, false, 'save again with no changes'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult( + sub {new pgBackRest::Common::Ini($strTestFile, {bLoad => false, strContent => fileStringRead($strTestFile)})}, + '[object]', 'new() passing content as a string'); + + #--------------------------------------------------------------------------------------------------------------------------- + executeTest("rm -rf ${strTestFile}*"); + + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_UNKNOWN, + "unable to open ${strTestFile}"); + + #--------------------------------------------------------------------------------------------------------------------------- + fileStringWrite($strTestFileCopy, BOGUS); + + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFileCopy)}, ERROR_CONFIG, + "key/value pair 'bogus' found outside of a section"); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + fileStringWrite($strTestFileCopy, iniRender($oIni->{oContent})); + + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFileCopy)}, ERROR_CHECKSUM, + "invalid checksum in '${strTestFileCopy}', expected '" . + $oIni->hash() . "' but found [undef]"); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + $oIni->{oContent}->{&INI_SECTION_BACKREST}{&INI_KEY_FORMAT} = 0; + + $self->testResult( + sub {$oIni->headerCheck({bIgnoreInvalid => true})}, false, + 'ignore invalid header'); + + #--------------------------------------------------------------------------------------------------------------------------- + fileRemove($strTestFileCopy); + + fileStringWrite($strTestFile, "[section]\n" . BOGUS); + + $self->testException(sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_CONFIG, "unable to find '=' in 'bogus'"); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult( + sub {iniParse("[section]\n" . BOGUS, {bIgnoreInvalid => true})}, undef, 'ignore invalid content'); + + #--------------------------------------------------------------------------------------------------------------------------- + fileStringWrite($strTestFile, BOGUS); + + # main invalid, copy invalid + $self->testException( + sub {new pgBackRest::Common::Ini($strTestFile)}, ERROR_CONFIG, "key/value pair 'bogus' found outside of a section"); + + #--------------------------------------------------------------------------------------------------------------------------- + fileStringWrite($strTestFile, iniRender($hIni)); + $hIni->{&INI_SECTION_BACKREST}{&INI_KEY_CHECKSUM} = BOGUS; + fileStringWrite($strTestFileCopy, iniRender($hIni)); + + $self->testResult(sub {new pgBackRest::Common::Ini($strTestFile)}, '[object]', 'invalid header - load main'); + } + + ################################################################################################################################ + if ($self->begin("Ini->set()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testException(sub {$oIni->set()}, ERROR_ASSERT, 'strSection and strKey are required'); + $self->testException(sub {$oIni->set($strSection)}, ERROR_ASSERT, 'strSection and strKey are required'); + $self->testException(sub {$oIni->set(undef, $strKey)}, ERROR_ASSERT, 'strSection and strKey are required'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, true, 'set key value'); + $self->testResult($oIni->modified(), true, ' check changed flag = true'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, false, 'set same key value'); + $self->testResult($oIni->modified(), false, ' check changed flag remains false'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, "${strValue}2")}, true, 'set different key value'); + $self->testResult($oIni->modified(), true, ' check changed flag = true'); + + $self->testResult(sub {$oIni->get($strSection, $strKey)}, "${strValue}2", 'get last key value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, undef)}, true, 'set undef key value'); + $self->testResult($oIni->modified(), true, ' check changed flag = true'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->set($strSection, "${strKey}2", $strSubKey, $strValue)}, true, 'set subkey value'); + } + + ################################################################################################################################ + if ($self->begin("Ini->get()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testException(sub {$oIni->get()}, ERROR_ASSERT, 'strSection is required'); + + $self->testException(sub {$oIni->get($strSection)}, ERROR_ASSERT, "strSection '${strSection}' is required but not defined"); + + $self->testException( + sub {$oIni->get($strSection, $strKey, undef)}, ERROR_ASSERT, + "strSection '${strSection}', strKey '${strKey}' is required but not defined"); + + $self->testException( + sub {$oIni->get($strSection, undef, $strSubKey)}, ERROR_ASSERT, + "strKey is required when strSubKey '${strSubKey}' is requested"); + + $self->testException( + sub {$oIni->get($strSection, $strKey, $strSubKey, true)}, ERROR_ASSERT, + "strSection '${strSection}', strKey '${strKey}', strSubKey '${strSubKey}' is required but not defined"); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->get($strSection, undef, undef, false)}, '[undef]', 'section value is not required'); + + $self->testResult(sub {$oIni->get($strSection, undef, undef, false, $strValue)}, $strValue, 'section value is defaulted'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->set($strSection, $strKey, $strSubKey, $strValue); + + $self->testResult(sub {$oIni->get($strSection, "${strKey}2", "${strSubKey}2", false)}, undef, 'missing key value'); + + $self->testResult(sub {$oIni->get($strSection, $strKey, "${strSubKey}2", false)}, undef, 'missing subkey value'); + + $self->testResult(sub {$oIni->get($strSection, $strKey, $strSubKey)}, $strValue, 'get subkey value'); + + $self->testResult(sub {$oIni->get($strSection, $strKey)}, "{${strSubKey} => ${strValue}}", 'get key value'); + + $self->testResult(sub {$oIni->get($strSection)}, "{${strKey} => {${strSubKey} => ${strValue}}}", 'get section value'); + } + + ################################################################################################################################ + if ($self->begin("Ini->remove()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testException(sub {$oIni->remove()}, ERROR_ASSERT, 'strSection is required'); + + $self->testException( + sub {$oIni->remove($strSection, undef, $strSubKey)}, ERROR_ASSERT, + "strKey is required when strSubKey '${strSubKey}' is requested"); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->remove($strSection)}, false, 'undefined section is not removed'); + $self->testResult($oIni->modified(), '0', ' check changed flag remains false'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, true, 'set key'); + + $oIni->{bModified} = false; + $self->testResult(sub {$oIni->remove($strSection, $strKey)}, true, ' remove key'); + $self->testResult($oIni->modified(), '1', ' check changed flag = true'); + $self->testResult(sub {$oIni->test($strSection, $strKey)}, false, ' test key'); + $self->testResult(sub {$oIni->test($strSection)}, false, ' test section'); + + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, true, 'set key 1'); + $self->testResult(sub {$oIni->set($strSection, "${strKey}2", undef, $strValue)}, true, ' set key 2'); + $self->testResult(sub {$oIni->remove($strSection, $strKey)}, true, ' remove key 1'); + $self->testResult(sub {$oIni->test($strSection, $strKey)}, false, ' test key'); + $self->testResult(sub {$oIni->test($strSection)}, true, ' test section'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, true, 'set key'); + + $self->testResult(sub {$oIni->remove($strSection)}, true, ' remove section'); + $self->testResult(sub {$oIni->test($strSection)}, false, ' test section'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->set($strSection, $strKey, $strSubKey, $strValue)}, true, 'set subkey'); + + $self->testResult(sub {$oIni->remove($strSection, $strKey, $strSubKey)}, true, ' remove subkey'); + $self->testResult(sub {$oIni->test($strSection, $strKey, $strSubKey)}, false, ' test subkey'); + $self->testResult(sub {$oIni->test($strSection, $strKey)}, true, ' test key'); + $self->testResult(sub {$oIni->test($strSection)}, true, ' test section'); + + $self->testResult(sub {$oIni->set($strSection, $strKey, $strSubKey, $strValue)}, true, 'set subkey 1'); + $self->testResult(sub {$oIni->set($strSection, $strKey, "${strSubKey}2", $strValue)}, true, 'set subkey 2'); + + $self->testResult(sub {$oIni->remove($strSection, $strKey, $strSubKey)}, true, ' remove subkey'); + $self->testResult(sub {$oIni->test($strSection, $strKey, $strSubKey)}, false, ' test subkey'); + $self->testResult(sub {$oIni->test($strSection, $strKey)}, true, ' test key'); + $self->testResult(sub {$oIni->test($strSection)}, true, ' test section'); + } + + ################################################################################################################################ + if ($self->begin("Ini->test()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->test($strSection, $strKey)}, false, 'test undefined key'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->set($strSection, $strKey, undef, $strValue)}, true, 'define key'); + + $self->testResult(sub {$oIni->test($strSection, $strKey)}, true, 'test key exists'); + + $self->testResult(sub {$oIni->test($strSection, $strKey, undef, $strValue)}, true, 'test key value'); + + $self->testResult(sub {$oIni->test($strSection, $strKey, undef, BOGUS)}, false, 'test key invalid value'); + } + + ################################################################################################################################ + if ($self->begin("Ini->boolSet() & Ini->boolGet() & Ini->boolTest()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->boolTest($strSection, $strKey)}, false, 'test bool on undefined key'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->boolGet($strSection, $strKey, undef, false, false)}, false, 'get bool default false value'); + $self->testResult(sub {$oIni->boolGet($strSection, $strKey, undef, false, true)}, true, 'get bool default true value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->boolSet($strSection, $strKey, undef, undef)}, true, 'set bool false (undef) value'); + $self->testResult(sub {$oIni->boolGet($strSection, $strKey)}, false, ' check bool false value'); + + $self->testResult(sub {$oIni->boolTest($strSection, $strKey, undef, false)}, true, 'test bool on false key'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->boolSet($strSection, $strKey, undef, false)}, false, 'set bool false value'); + $self->testResult(sub {$oIni->boolGet($strSection, $strKey)}, false, ' check value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->boolSet($strSection, $strKey, undef, true)}, true, 'set bool false value'); + $self->testResult(sub {$oIni->boolGet($strSection, $strKey)}, true, ' check value'); + + $self->testResult(sub {$oIni->boolTest($strSection, $strKey, undef, true)}, true, 'test bool on true key'); + } + + ################################################################################################################################ + if ($self->begin("Ini->numericSet() & Ini->numericGet() & Ini->numericTest()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->numericSet($strSection, $strKey)}, true, 'set numeric undef value'); + $self->testResult(sub {$oIni->test($strSection, $strKey)}, false, 'test numeric undef value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->numericGet($strSection, $strKey, undef, false, 1000)}, 1000, 'get numeric default value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->numericSet($strSection, $strKey, undef, 0)}, true, 'set numeric 0 value'); + $self->testResult(sub {$oIni->numericGet($strSection, $strKey)}, 0, ' check value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->numericSet($strSection, $strKey, undef, 0)}, false, 'set numeric 0 value again'); + $self->testResult(sub {$oIni->numericGet($strSection, $strKey)}, 0, ' check value'); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->numericSet($strSection, $strKey, undef, -100)}, true, 'set numeric -100 value'); + $self->testResult(sub {$oIni->numericGet($strSection, $strKey)}, -100, ' check value'); + } + + ################################################################################################################################ + if ($self->begin("Ini->keys()")) + { + my $oIni = new pgBackRest::Common::Ini($strTestFile, {bLoad => false}); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->keys($strSection)}, '[undef]', 'section undefined'); + + #--------------------------------------------------------------------------------------------------------------------------- + $oIni->set($strSection, 'a', undef, $strValue); + $oIni->set($strSection, 'c', undef, $strValue); + $oIni->set($strSection, 'b', undef, $strValue); + + #--------------------------------------------------------------------------------------------------------------------------- + $self->testResult(sub {$oIni->keys($strSection)}, '(a, b, c)', 'sort forward (default)'); + + $self->testResult(sub {$oIni->keys($strSection, INI_SORT_FORWARD)}, '(a, b, c)', 'sort forward'); + + $self->testResult(sub {$oIni->keys($strSection, INI_SORT_REVERSE)}, '(c, b, a)', 'sort reverse'); + + $self->testResult(sub {sort($oIni->keys($strSection, INI_SORT_NONE))}, '(a, b, c)', 'sort none'); + + $self->testException(sub {sort($oIni->keys($strSection, BOGUS))}, ERROR_ASSERT, "invalid strSortOrder '" . BOGUS . "'"); + } +} + +1; [parent: 367e06f4be91]