From 06b5ee3f753e87d15a0c7d34488346c0d3c225f4 Mon Sep 17 00:00:00 2001 From: David Steele Date: Wed, 16 Jul 2014 23:38:38 -0400 Subject: [PATCH] Working on new config load/save functions and unit tests. --- lib/BackRest/Backup.pm | 43 +++++++- test/lib/BackRestTest/UtilityTest.pm | 157 +++++++++++++++++++++++++++ test/test.pl | 6 + 3 files changed, 204 insertions(+), 2 deletions(-) create mode 100755 test/lib/BackRestTest/UtilityTest.pm diff --git a/lib/BackRest/Backup.pm b/lib/BackRest/Backup.pm index ac6f133bd..0d0283154 100644 --- a/lib/BackRest/Backup.pm +++ b/lib/BackRest/Backup.pm @@ -657,9 +657,48 @@ sub backup_manifest_load sub backup_manifest_save { my $strBackupManifestFile = shift; - my $oBackupManifestRef = shift; + my $oConfig = shift; - store($oBackupManifestRef, $strBackupManifestFile) or confess &log(ERROR, "unable to open ${strBackupManifestFile}"); + my $hFile; + my $bFirst = true; + + open($hFile, '>', $strBackupManifestFile) + or confess "unable to open ${$strBackupManifestFile}"; + + foreach my $strSection (sort(keys $oConfig)) + { + if (!$bFirst) + { + syswrite($hFile, "\n") + or confess "unable to write lf: $!"; + } + + syswrite($hFile, "[${strSection}]\n") + or confess "unable to write section ${strSection}: $!"; + + foreach my $strKey (sort(keys ${$oConfig}{"${strSection}"})) + { + my $strValue = ${$oConfig}{"${strSection}"}{"${strKey}"}; + + if (defined($strValue)) + { + if (ref($strValue) eq "HASH") + { + syswrite($hFile, "${strKey}=" . encode_json($strValue) . "\n") + or confess "unable to write key ${strKey}: $!"; + } + else + { + syswrite($hFile, "${strKey}=${strValue}\n") + or confess "unable to write key ${strKey}: $!"; + } + } + } + + $bFirst = false; + } + +# store($oBackupManifestRef, $strBackupManifestFile) or confess &log(ERROR, "unable to open ${strBackupManifestFile}"); } #################################################################################################################################### diff --git a/test/lib/BackRestTest/UtilityTest.pm b/test/lib/BackRestTest/UtilityTest.pm new file mode 100755 index 000000000..cb1c8176c --- /dev/null +++ b/test/lib/BackRestTest/UtilityTest.pm @@ -0,0 +1,157 @@ +#!/usr/bin/perl +#################################################################################################################################### +# BackupTest.pl - Unit Tests for BackRest::File +#################################################################################################################################### +package BackRestTest::UtilityTest; + +#################################################################################################################################### +# Perl includes +#################################################################################################################################### +use strict; +use warnings; +use english; +use Carp; + +use File::Basename; +use JSON; # remove when config functions go to utility + +use lib dirname($0) . "/../lib"; +use BackRest::Utility; + +use BackRestTest::CommonTest; + +use Exporter qw(import); +our @EXPORT = qw(BackRestTestUtility_Test); + +#################################################################################################################################### +# BackRestTestUtility_Drop +#################################################################################################################################### +sub BackRestTestUtility_Drop +{ + # Remove the test directory + system('rm -rf ' . BackRestTestCommon_TestPathGet()) == 0 + or die 'unable to remove ' . BackRestTestCommon_TestPathGet() . 'path'; +} + +#################################################################################################################################### +# BackRestTestUtility_Create +#################################################################################################################################### +sub BackRestTestUtility_Create +{ + # Drop the old test directory + BackRestTestUtility_Drop(); + + # Create the test directory + mkdir(BackRestTestCommon_TestPathGet(), oct('0770')) + or confess 'Unable to create ' . BackRestTestCommon_TestPathGet() . ' path'; +} + +#################################################################################################################################### +# BackRestTestUtility_Test +#################################################################################################################################### +sub BackRestTestUtility_Test +{ + my $strTest = shift; + + # Setup test variables + my $iRun; + my $bCreate; + my $strTestPath = BackRestTestCommon_TestPathGet(); + + # Print test banner + &log(INFO, "UTILITY MODULE ******************************************************************"); + + #------------------------------------------------------------------------------------------------------------------------------- + # Test config + #------------------------------------------------------------------------------------------------------------------------------- + if ($strTest eq 'all' || $strTest eq 'config') + { + $iRun = 0; + $bCreate = true; + my $oFile; + + &log(INFO, "Test config\n"); + + # Increment the run, log, and decide whether this unit test should be run + if (BackRestTestCommon_Run(++$iRun, "base")) + { + # Create the test directory + if ($bCreate) + { + # # Create the file object + # $oFile = (BackRest::File->new + # ( + # # strStanza => $strStanza, + # # strBackupPath => BackRestTestCommon_BackupPathGet(), + # # strRemote => $bRemote ? 'backup' : undef, + # # oRemote => $bRemote ? $oRemote : undef + # ))->clone(); + + BackRestTestUtility_Create(); + + $bCreate = false; + } + + my %oConfig; + + $oConfig{test1}{key1} = 'value'; + $oConfig{test1}{key2} = 'value'; + + $oConfig{test2}{key1} = 'value'; + + $oConfig{test3}{key1} = 'value'; + $oConfig{test3}{key2}{sub1} = 'value'; + $oConfig{test3}{key2}{sub2} = 'value'; + + my $strFile = "$strTestPath/test.cfg"; + my $hFile; + my $bFirst = true; + + open($hFile, '>', $strFile) + or confess "unable to open ${strFile}"; + + foreach my $strSection (sort(keys %oConfig)) + { + if (!$bFirst) + { + syswrite($hFile, "\n") + or confess "unable to write lf: $!"; + } + + syswrite($hFile, "[${strSection}]\n") + or confess "unable to write section ${strSection}: $!"; + + foreach my $strKey (sort(keys $oConfig{$strSection})) + { + my $strValue = $oConfig{"${strSection}"}{"${strKey}"}; + + if (defined($strValue)) + { + if (ref($strValue) eq "HASH") + { + syswrite($hFile, "${strKey}=" . encode_json($strValue) . "\n") + or confess "unable to write key ${strKey}: $!"; + } + else + { + syswrite($hFile, "${strKey}=${strValue}\n") + or confess "unable to write key ${strKey}: $!"; + } + } + } + + $bFirst = false; + } + + close($hFile); + + if (BackRestTestCommon_Cleanup()) + { + &log(INFO, 'cleanup'); + BackRestTestUtility_Drop(); + } + } + } +} + +1; diff --git a/test/test.pl b/test/test.pl index 33ffd8e6e..0e838894e 100755 --- a/test/test.pl +++ b/test/test.pl @@ -19,6 +19,7 @@ use BackRest::Utility; use lib dirname($0) . "/lib"; use BackRestTest::CommonTest; +use BackRestTest::UtilityTest; use BackRestTest::FileTest; use BackRestTest::BackupTest; @@ -102,6 +103,11 @@ BackRestTestCommon_Setup($iModuleTestRun, $bDryRun, $bNoCleanup); # &log(INFO, "Testing with test_path = " . BackRestTestCommon_TestPathGet() . ", host = {strHost}, user = {strUser}, " . # "group = {strGroup}"); +if ($strModule eq 'all' || $strModule eq "utility") +{ + BackRestTestUtility_Test($strModuleTest); +} + if ($strModule eq 'all' || $strModule eq "file") { BackRestTestFile_Test($strModuleTest);