diff --git a/test/lib/BackRestTest/FileTest.pm b/test/lib/BackRestTest/FileTest.pm index fde190f83..a273d9333 100755 --- a/test/lib/BackRestTest/FileTest.pm +++ b/test/lib/BackRestTest/FileTest.pm @@ -25,6 +25,15 @@ our @EXPORT = qw(BackRestFileTest); sub BackRestFileTest { + my $strTest = shift; + + # If no test was specified, then run them all + if (!defined($strTest)) + { + $strTest = 'all'; + } + + # Setup test paths my $strLockPath = dirname(abs_path($0)) . "/lock"; my $strTestPath = dirname(abs_path($0)) . "/test"; my $iRun; @@ -43,99 +52,102 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test path_create() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "Test File->path_create()\n"); - - # Loop through local/remote - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'path_create') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through exists (does the paren path exist?) - for (my $bExists = 0; $bExists <= 1; $bExists++) + &log(INFO, "Test File->path_create()\n"); + + # Loop through local/remote + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - # Loop through permission (permission will be set on true) - for (my $bPermission = 0; $bPermission <= $bExists; $bPermission++) + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); + + # Loop through exists (does the paren path exist?) + for (my $bExists = 0; $bExists <= 1; $bExists++) { - $iRun++; - - &log(INFO, "run ${iRun} - " . - "remote $bRemote, exists $bExists, permission $bPermission"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strPath = "${strTestPath}/path"; - my $strPermission; - - # If permission then set one (other than the default) - if ($bPermission) + # Loop through permission (permission will be set on true) + for (my $bPermission = 0; $bPermission <= $bExists; $bPermission++) { - $strPermission = "0700"; + $iRun++; - # Make sure that we are not testing with the default permission - if ($strPermission eq $oFile->{strDefaultPathPermission}) + &log(INFO, "run ${iRun} - " . + "remote $bRemote, exists $bExists, permission $bPermission"); + + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; + + my $strPath = "${strTestPath}/path"; + my $strPermission; + + # If permission then set one (other than the default) + if ($bPermission) { - confess 'cannot set test permission ${strPermission} equal to default permission' . - $oFile->{strDefaultPathPermission}; + $strPermission = "0700"; + + # Make sure that we are not testing with the default permission + if ($strPermission eq $oFile->{strDefaultPathPermission}) + { + confess 'cannot set test permission ${strPermission} equal to default permission' . + $oFile->{strDefaultPathPermission}; + } } - } - # If not exists then set the path to something bogus - if (!$bExists) - { - $strPath = "${strTestPath}/error/path"; - } - - # Execute in eval to catch errors - eval - { - $oFile->path_create(PATH_BACKUP_ABSOLUTE, $strPath, $strPermission); - }; - - # Check for errors - if ($@) - { - # Ignore errors if the path did not exist + # If not exists then set the path to something bogus if (!$bExists) { - next; + $strPath = "${strTestPath}/error/path"; } - confess "error raised: " . $@ . "\n"; - } - else - { - # Make sure the path was actually created - unless (-e $strPath) + # Execute in eval to catch errors + eval { - confess "path was not created"; + $oFile->path_create(PATH_BACKUP_ABSOLUTE, $strPath, $strPermission); + }; + + # Check for errors + if ($@) + { + # Ignore errors if the path did not exist + if (!$bExists) + { + next; + } + + confess "error raised: " . $@ . "\n"; } - - # Check that the permissions were set correctly - my $oStat = lstat($strPath); - - if (!defined($oStat)) + else { - confess "unable to stat ${strPath}"; - } + # Make sure the path was actually created + unless (-e $strPath) + { + confess "path was not created"; + } - my $strPermissionCompare = defined($strPermission) ? $strPermission : $oFile->{strDefaultPathPermission}; + # Check that the permissions were set correctly + my $oStat = lstat($strPath); - if ($strPermissionCompare ne sprintf("%04o", S_IMODE($oStat->mode))) - { - confess "permissions were not set to {$strPermissionCompare}"; + if (!defined($oStat)) + { + confess "unable to stat ${strPath}"; + } + + my $strPermissionCompare = defined($strPermission) ? $strPermission : $oFile->{strDefaultPathPermission}; + + if ($strPermissionCompare ne sprintf("%04o", S_IMODE($oStat->mode))) + { + confess "permissions were not set to {$strPermissionCompare}"; + } } } } @@ -145,84 +157,87 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test move() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "Test File->move()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'move') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through source exists - for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "Test File->move()\n"); + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - # Loop through destination exists - for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++) + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); + + # Loop through source exists + for (my $bSourceExists = 0; $bSourceExists <= 1; $bSourceExists++) { - # Loop through create - for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++) + # Loop through destination exists + for (my $bDestinationExists = 0; $bDestinationExists <= 1; $bDestinationExists++) { - $iRun++; - - &log(INFO, "run ${iRun} - " . - "remote $bRemote, src_exists $bSourceExists, dst_exists $bDestinationExists, create $bCreate"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strSourceFile = "${strTestPath}/test.txt"; - my $strDestinationFile = "${strTestPath}/test-dest.txt"; - - if ($bCreate) + # Loop through create + for (my $bCreate = 0; $bCreate <= $bDestinationExists; $bCreate++) { - $strDestinationFile = "${strTestPath}/sub/test-dest.txt" - } + $iRun++; - if ($bSourceExists) - { - system("echo 'TESTDATA' > ${strSourceFile}"); - } + &log(INFO, "run ${iRun} - " . + "remote $bRemote, src_exists $bSourceExists, dst_exists $bDestinationExists, create $bCreate"); - if (!$bDestinationExists) - { - $strDestinationFile = "error" . $strDestinationFile; - } + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; - # Execute in eval in case of error - eval - { - $oFile->move(PATH_BACKUP_ABSOLUTE, $strSourceFile, PATH_BACKUP_ABSOLUTE, $strDestinationFile, $bCreate); - }; + my $strSourceFile = "${strTestPath}/test.txt"; + my $strDestinationFile = "${strTestPath}/test-dest.txt"; - if ($@) - { - if (!$bSourceExists || !$bDestinationExists) + if ($bCreate) { - next; + $strDestinationFile = "${strTestPath}/sub/test-dest.txt" } - confess "error raised: " . $@ . "\n"; - } + if ($bSourceExists) + { + system("echo 'TESTDATA' > ${strSourceFile}"); + } - if (!$bSourceExists || !$bDestinationExists) - { - confess "error should have been raised"; - } + if (!$bDestinationExists) + { + $strDestinationFile = "error" . $strDestinationFile; + } - unless (-e $strDestinationFile) - { - confess "file was not moved"; + # Execute in eval in case of error + eval + { + $oFile->move(PATH_BACKUP_ABSOLUTE, $strSourceFile, PATH_BACKUP_ABSOLUTE, $strDestinationFile, $bCreate); + }; + + if ($@) + { + if (!$bSourceExists || !$bDestinationExists) + { + next; + } + + confess "error raised: " . $@ . "\n"; + } + + if (!$bSourceExists || !$bDestinationExists) + { + confess "error should have been raised"; + } + + unless (-e $strDestinationFile) + { + confess "file was not moved"; + } } } } @@ -232,52 +247,55 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test compress() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "Test File->compress()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'compress') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through exists - for (my $bExists = 0; $bExists <= 1; $bExists++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "Test File->compress()\n"); + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - $iRun++; + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); - &log(INFO, "run ${iRun} - " . - "remote $bRemote, exists $bExists"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strFile = "${strTestPath}/test.txt"; - - if ($bExists) + # Loop through exists + for (my $bExists = 0; $bExists <= 1; $bExists++) { - system("echo 'TESTDATA' > ${strFile}"); - } + $iRun++; - # Execute in eval in case of error - eval - { - $oFile->compress(PATH_BACKUP_ABSOLUTE, $strFile); - }; + &log(INFO, "run ${iRun} - " . + "remote $bRemote, exists $bExists"); - if ($@ && $bExists) - { - confess "error raised: " . $@ . "\n"; + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; + + my $strFile = "${strTestPath}/test.txt"; + + if ($bExists) + { + system("echo 'TESTDATA' > ${strFile}"); + } + + # Execute in eval in case of error + eval + { + $oFile->compress(PATH_BACKUP_ABSOLUTE, $strFile); + }; + + if ($@ && $bExists) + { + confess "error raised: " . $@ . "\n"; + } } } } @@ -285,129 +303,132 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test manifest() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "Test File->manifest()\n"); - - # Create the test data - system("rm -rf test"); - - system("mkdir -m 750 ${strTestPath}") == 0 or confess "Unable to create test directory"; - system("mkdir -m 750 ${strTestPath}/sub1") == 0 or confess "Unable to create test directory"; - system("mkdir -m 750 ${strTestPath}/sub1/sub2") == 0 or confess "Unable to create test directory"; - - system("echo 'TESTDATA' > ${strTestPath}/test.txt"); - utime(1111111111, 1111111111, "${strTestPath}/test.txt"); - system("chmod 1640 ${strTestPath}/test.txt"); - - system("echo 'TESTDATA_' > ${strTestPath}/sub1/test-sub1.txt"); - utime(1111111112, 1111111112, "${strTestPath}/sub1/test-sub1.txt"); - system("chmod 0640 ${strTestPath}/sub1/test-sub1.txt"); - - system("echo 'TESTDATA__' > ${strTestPath}/sub1/sub2/test-sub2.txt"); - utime(1111111113, 1111111113, "${strTestPath}/sub1/sub2/test-sub2.txt"); - system("chmod 0646 ${strTestPath}/sub1/test-sub1.txt"); - - system("ln ${strTestPath}/test.txt ${strTestPath}/sub1/test-hardlink.txt"); - system("ln ${strTestPath}/test.txt ${strTestPath}/sub1/sub2/test-hardlink.txt"); - - system("ln -s .. ${strTestPath}/sub1/test"); - system("chmod 0700 ${strTestPath}/sub1/test"); - system("ln -s ../.. ${strTestPath}/sub1/sub2/test"); - system("chmod 0750 ${strTestPath}/sub1/sub2/test"); - - my $strManifestCompare = - ".,d,${strUser},${strGroup},0750,,,,\n" . - "sub1,d,${strUser},${strGroup},0750,,,,\n" . - "sub1/sub2,d,${strUser},${strGroup},0750,,,,\n" . - "sub1/sub2/test,l,${strUser},${strGroup},,,,,../..\n" . - "sub1/sub2/test-hardlink.txt,f,${strUser},${strGroup},1640,1111111111,0,9,\n" . - "sub1/sub2/test-sub2.txt,f,${strUser},${strGroup},0666,1111111113,0,11,\n" . - "sub1/test,l,${strUser},${strGroup},,,,,..\n" . - "sub1/test-hardlink.txt,f,${strUser},${strGroup},1640,1111111111,0,9,\n" . - "sub1/test-sub1.txt,f,${strUser},${strGroup},0646,1111111112,0,10,\n" . - "test.txt,f,${strUser},${strGroup},1640,1111111111,0,9,"; - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'manifest') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - for (my $bError = 0; $bError <= 1; $bError++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "Test File->manifest()\n"); + + # Create the test data + system("rm -rf test"); + + system("mkdir -m 750 ${strTestPath}") == 0 or confess "Unable to create test directory"; + system("mkdir -m 750 ${strTestPath}/sub1") == 0 or confess "Unable to create test directory"; + system("mkdir -m 750 ${strTestPath}/sub1/sub2") == 0 or confess "Unable to create test directory"; + + system("echo 'TESTDATA' > ${strTestPath}/test.txt"); + utime(1111111111, 1111111111, "${strTestPath}/test.txt"); + system("chmod 1640 ${strTestPath}/test.txt"); + + system("echo 'TESTDATA_' > ${strTestPath}/sub1/test-sub1.txt"); + utime(1111111112, 1111111112, "${strTestPath}/sub1/test-sub1.txt"); + system("chmod 0640 ${strTestPath}/sub1/test-sub1.txt"); + + system("echo 'TESTDATA__' > ${strTestPath}/sub1/sub2/test-sub2.txt"); + utime(1111111113, 1111111113, "${strTestPath}/sub1/sub2/test-sub2.txt"); + system("chmod 0646 ${strTestPath}/sub1/test-sub1.txt"); + + system("ln ${strTestPath}/test.txt ${strTestPath}/sub1/test-hardlink.txt"); + system("ln ${strTestPath}/test.txt ${strTestPath}/sub1/sub2/test-hardlink.txt"); + + system("ln -s .. ${strTestPath}/sub1/test"); + system("chmod 0700 ${strTestPath}/sub1/test"); + system("ln -s ../.. ${strTestPath}/sub1/sub2/test"); + system("chmod 0750 ${strTestPath}/sub1/sub2/test"); + + my $strManifestCompare = + ".,d,${strUser},${strGroup},0750,,,,\n" . + "sub1,d,${strUser},${strGroup},0750,,,,\n" . + "sub1/sub2,d,${strUser},${strGroup},0750,,,,\n" . + "sub1/sub2/test,l,${strUser},${strGroup},,,,,../..\n" . + "sub1/sub2/test-hardlink.txt,f,${strUser},${strGroup},1640,1111111111,0,9,\n" . + "sub1/sub2/test-sub2.txt,f,${strUser},${strGroup},0666,1111111113,0,11,\n" . + "sub1/test,l,${strUser},${strGroup},,,,,..\n" . + "sub1/test-hardlink.txt,f,${strUser},${strGroup},1640,1111111111,0,9,\n" . + "sub1/test-sub1.txt,f,${strUser},${strGroup},0646,1111111112,0,10,\n" . + "test.txt,f,${strUser},${strGroup},1640,1111111111,0,9,"; + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - $iRun++; + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); - &log(INFO, "run ${iRun} - " . - "remote $bRemote, error $bError"); - - my $strPath = $strTestPath; - - if ($bError) + for (my $bError = 0; $bError <= 1; $bError++) { - $strPath .= "-error"; - } + $iRun++; - # Execute in eval in case of error - eval - { - my %oManifestHash; - $oFile->manifest(PATH_BACKUP_ABSOLUTE, $strPath, \%oManifestHash); + &log(INFO, "run ${iRun} - " . + "remote $bRemote, error $bError"); - my $strManifest; + my $strPath = $strTestPath; - foreach my $strName (sort(keys $oManifestHash{name})) + if ($bError) { - if (!defined($strManifest)) - { - $strManifest = ""; - } - else - { - $strManifest .= "\n"; - } - - if (defined($oManifestHash{name}{"${strName}"}{inode})) - { - $oManifestHash{name}{"${strName}"}{inode} = 0; - } - - $strManifest .= - "${strName}," . - $oManifestHash{name}{"${strName}"}{type} . "," . - (defined($oManifestHash{name}{"${strName}"}{user}) ? - $oManifestHash{name}{"${strName}"}{user} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{group}) ? - $oManifestHash{name}{"${strName}"}{group} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{permission}) ? - $oManifestHash{name}{"${strName}"}{permission} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{modification_time}) ? - $oManifestHash{name}{"${strName}"}{modification_time} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{inode}) ? - $oManifestHash{name}{"${strName}"}{inode} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{size}) ? - $oManifestHash{name}{"${strName}"}{size} : "") . "," . - (defined($oManifestHash{name}{"${strName}"}{link_destination}) ? - $oManifestHash{name}{"${strName}"}{link_destination} : ""); + $strPath .= "-error"; } - if ($strManifest ne $strManifestCompare) + # Execute in eval in case of error + eval { - confess "manifest is not equal:\n\n${strManifest}\n\ncompare:\n\n${strManifestCompare}\n\n"; - } - }; + my %oManifestHash; + $oFile->manifest(PATH_BACKUP_ABSOLUTE, $strPath, \%oManifestHash); - if ($@ && !$bError) - { - confess "error raised: " . $@ . "\n"; + my $strManifest; + + foreach my $strName (sort(keys $oManifestHash{name})) + { + if (!defined($strManifest)) + { + $strManifest = ""; + } + else + { + $strManifest .= "\n"; + } + + if (defined($oManifestHash{name}{"${strName}"}{inode})) + { + $oManifestHash{name}{"${strName}"}{inode} = 0; + } + + $strManifest .= + "${strName}," . + $oManifestHash{name}{"${strName}"}{type} . "," . + (defined($oManifestHash{name}{"${strName}"}{user}) ? + $oManifestHash{name}{"${strName}"}{user} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{group}) ? + $oManifestHash{name}{"${strName}"}{group} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{permission}) ? + $oManifestHash{name}{"${strName}"}{permission} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{modification_time}) ? + $oManifestHash{name}{"${strName}"}{modification_time} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{inode}) ? + $oManifestHash{name}{"${strName}"}{inode} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{size}) ? + $oManifestHash{name}{"${strName}"}{size} : "") . "," . + (defined($oManifestHash{name}{"${strName}"}{link_destination}) ? + $oManifestHash{name}{"${strName}"}{link_destination} : ""); + } + + if ($strManifest ne $strManifestCompare) + { + confess "manifest is not equal:\n\n${strManifest}\n\ncompare:\n\n${strManifestCompare}\n\n"; + } + }; + + if ($@ && !$bError) + { + confess "error raised: " . $@ . "\n"; + } } } } @@ -415,97 +436,100 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test list() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "Test File->list()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'list') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through exists - for (my $bSort = 0; $bSort <= 1; $bSort++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "Test File->list()\n"); + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - my $strSort = $bSort ? undef : "reverse"; + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); - # Loop through expression - for (my $iExpression = 0; $iExpression <= 2; $iExpression++) + # Loop through exists + for (my $bSort = 0; $bSort <= 1; $bSort++) { - my $strExpression; + my $strSort = $bSort ? undef : "reverse"; - # Expression tha returns results - if ($iExpression == 1) + # Loop through expression + for (my $iExpression = 0; $iExpression <= 2; $iExpression++) { - $strExpression = "^test2\\..*\$"; - } - # Expression that does not return results - elsif ($iExpression == 2) - { - $strExpression = "^du\$"; - } + my $strExpression; - # Loop through exists - for (my $bExists = 0; $bExists <= 1; $bExists++) - { - $iRun++; - - &log(INFO, "run ${iRun} - " . - "remote $bRemote, exists $bExists, " . - "expression " . (defined($strExpression) ? $strExpression : "[undef]") . ", " . - "sort " . (defined($strSort) ? $strSort : "[undef]")); - - my $strPath = "${strTestPath}"; - - # Drop the old test directory and create a new one - system("rm -rf test"); - - if ($bExists) + # Expression tha returns results + if ($iExpression == 1) { - system("mkdir test") == 0 or confess "Unable to create test directory"; - system("echo 'TESTDATA' > ${strPath}/test.txt"); - system("echo 'TESTDATA2' > ${strPath}/test2.txt"); + $strExpression = "^test2\\..*\$"; + } + # Expression that does not return results + elsif ($iExpression == 2) + { + $strExpression = "^du\$"; } - my @stryFileCompare = split(/\n/, "test.txt\ntest2.txt"); - - # Execute in eval in case of error - eval + # Loop through exists + for (my $bExists = 0; $bExists <= 1; $bExists++) { - my @stryFileList = $oFile->list(PATH_BACKUP_ABSOLUTE, $strPath, $strExpression, $strSort); + $iRun++; - if (defined($strExpression)) + &log(INFO, "run ${iRun} - " . + "remote $bRemote, exists $bExists, " . + "expression " . (defined($strExpression) ? $strExpression : "[undef]") . ", " . + "sort " . (defined($strSort) ? $strSort : "[undef]")); + + my $strPath = "${strTestPath}"; + + # Drop the old test directory and create a new one + system("rm -rf test"); + + if ($bExists) { - @stryFileCompare = grep(/$strExpression/i, @stryFileCompare); + system("mkdir test") == 0 or confess "Unable to create test directory"; + system("echo 'TESTDATA' > ${strPath}/test.txt"); + system("echo 'TESTDATA2' > ${strPath}/test2.txt"); } - if (defined($strSort)) + my @stryFileCompare = split(/\n/, "test.txt\ntest2.txt"); + + # Execute in eval in case of error + eval { - @stryFileCompare = sort {$b cmp $a} @stryFileCompare; - } + my @stryFileList = $oFile->list(PATH_BACKUP_ABSOLUTE, $strPath, $strExpression, $strSort); - my $strFileList = sprintf("@stryFileList"); - my $strFileCompare = sprintf("@stryFileCompare"); + if (defined($strExpression)) + { + @stryFileCompare = grep(/$strExpression/i, @stryFileCompare); + } - if ($strFileList ne $strFileCompare) + if (defined($strSort)) + { + @stryFileCompare = sort {$b cmp $a} @stryFileCompare; + } + + my $strFileList = sprintf("@stryFileList"); + my $strFileCompare = sprintf("@stryFileCompare"); + + if ($strFileList ne $strFileCompare) + { + confess "list (${strFileList})[" . @stryFileList . + "] does not match compare (${strFileCompare})[" . @stryFileCompare . "]"; + } + }; + + if ($@ && $bExists) { - confess "list (${strFileList})[" . @stryFileList . - "] does not match compare (${strFileCompare})[" . @stryFileCompare . "]"; + confess "error raised: " . $@ . "\n"; } - }; - - if ($@ && $bExists) - { - confess "error raised: " . $@ . "\n"; } } } @@ -515,69 +539,72 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test remove() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "Test File->remove()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'remove') { - my $strHost = $bRemote ? "127.0.0.1" : undef; - my $strUser = $bRemote ? "dsteele" : undef; + $iRun = 0; - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => ${strCommand}, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "Test File->remove()\n"); - # Loop through exists - for (my $bExists = 0; $bExists <= 1; $bExists++) + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - # Loop through temp - for (my $bTemp = 0; $bTemp <= 1; $bTemp++) + my $strHost = $bRemote ? "127.0.0.1" : undef; + my $strUser = $bRemote ? "dsteele" : undef; + + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => ${strCommand}, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); + + # Loop through exists + for (my $bExists = 0; $bExists <= 1; $bExists++) { - # Loop through ignore missing - for (my $bIgnoreMissing = 0; $bIgnoreMissing <= 1; $bIgnoreMissing++) + # Loop through temp + for (my $bTemp = 0; $bTemp <= 1; $bTemp++) { - $iRun++; - - &log(INFO, "run ${iRun} - " . - "remote ${bRemote}, exists ${bExists}, temp ${bTemp}, ignore missing ${bIgnoreMissing}"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strFile = "${strTestPath}/test.txt"; - - if ($bExists) + # Loop through ignore missing + for (my $bIgnoreMissing = 0; $bIgnoreMissing <= 1; $bIgnoreMissing++) { - system("echo 'TESTDATA' > ${strFile}" . ($bTemp ? ".backrest.tmp" : "")); - } + $iRun++; - # Execute in eval in case of error - eval - { - if ($oFile->remove(PATH_BACKUP_ABSOLUTE, $strFile, $bTemp, $bIgnoreMissing) != $bExists) + &log(INFO, "run ${iRun} - " . + "remote ${bRemote}, exists ${bExists}, temp ${bTemp}, ignore missing ${bIgnoreMissing}"); + + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; + + my $strFile = "${strTestPath}/test.txt"; + + if ($bExists) { - confess "hash did not match expected"; + system("echo 'TESTDATA' > ${strFile}" . ($bTemp ? ".backrest.tmp" : "")); } - }; - if ($@ && $bExists) - { - confess "error raised: " . $@ . "\n"; - } + # Execute in eval in case of error + eval + { + if ($oFile->remove(PATH_BACKUP_ABSOLUTE, $strFile, $bTemp, $bIgnoreMissing) != $bExists) + { + confess "hash did not match expected"; + } + }; - if (-e ($strFile . ($bTemp ? ".backrest.tmp" : ""))) - { - confess "file still exists"; + if ($@ && $bExists) + { + confess "error raised: " . $@ . "\n"; + } + + if (-e ($strFile . ($bTemp ? ".backrest.tmp" : ""))) + { + confess "file still exists"; + } } } } @@ -587,57 +614,60 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test hash() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "test File->hash()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'hash') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through exists - for (my $bExists = 0; $bExists <= 1; $bExists++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "test File->hash()\n"); + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - $iRun++; + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); - &log(INFO, "run ${iRun} - " . - "remote $bRemote, exists $bExists"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strFile = "${strTestPath}/test.txt"; - - if ($bExists) + # Loop through exists + for (my $bExists = 0; $bExists <= 1; $bExists++) { - system("echo 'TESTDATA' > ${strFile}"); - } + $iRun++; - # Execute in eval in case of error - eval - { - if ($oFile->hash(PATH_BACKUP_ABSOLUTE, $strFile) ne '06364afe79d801433188262478a76d19777ef351') - { - confess "bExists is set to ${bExists}, but exists() returned " . !$bExists; - } - }; + &log(INFO, "run ${iRun} - " . + "remote $bRemote, exists $bExists"); + + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; + + my $strFile = "${strTestPath}/test.txt"; - if ($@) - { if ($bExists) { - confess "error raised: " . $@ . "\n"; + system("echo 'TESTDATA' > ${strFile}"); + } + + # Execute in eval in case of error + eval + { + if ($oFile->hash(PATH_BACKUP_ABSOLUTE, $strFile) ne '06364afe79d801433188262478a76d19777ef351') + { + confess "bExists is set to ${bExists}, but exists() returned " . !$bExists; + } + }; + + if ($@) + { + if ($bExists) + { + confess "error raised: " . $@ . "\n"; + } } } } @@ -646,195 +676,168 @@ sub BackRestFileTest #------------------------------------------------------------------------------------------------------------------------------- # Test exists() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - &log(INFO, "--------------------------------------------------------------------------------"); - &log(INFO, "test File->exists()\n"); - - for (my $bRemote = 0; $bRemote <= 1; $bRemote++) + if ($strTest eq 'all' || $strTest eq 'exists') { - my $oFile = pg_backrest_file->new - ( - strStanza => $strStanza, - bNoCompression => true, - strCommand => $strCommand, - strBackupClusterPath => ${strTestPath}, - strBackupPath => ${strTestPath}, - strBackupHost => $bRemote ? $strHost : undef, - strBackupUser => $bRemote ? $strUser : undef - ); + $iRun = 0; - # Loop through exists - for (my $bExists = 0; $bExists <= 1; $bExists++) + &log(INFO, "--------------------------------------------------------------------------------"); + &log(INFO, "test File->exists()\n"); + + for (my $bRemote = 0; $bRemote <= 1; $bRemote++) { - $iRun++; + my $oFile = pg_backrest_file->new + ( + strStanza => $strStanza, + bNoCompression => true, + strCommand => $strCommand, + strBackupClusterPath => ${strTestPath}, + strBackupPath => ${strTestPath}, + strBackupHost => $bRemote ? $strHost : undef, + strBackupUser => $bRemote ? $strUser : undef + ); - &log(INFO, "run ${iRun} - " . - "remote $bRemote, exists $bExists"); - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir test") == 0 or confess "Unable to create test directory"; - - my $strFile = "${strTestPath}/test.txt"; - - if ($bExists) + # Loop through exists + for (my $bExists = 0; $bExists <= 1; $bExists++) { - system("echo 'TESTDATA' > ${strFile}"); - } + $iRun++; - # Execute in eval in case of error - eval - { - if ($oFile->exists(PATH_BACKUP_ABSOLUTE, $strFile) != $bExists) + &log(INFO, "run ${iRun} - " . + "remote $bRemote, exists $bExists"); + + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir test") == 0 or confess "Unable to create test directory"; + + my $strFile = "${strTestPath}/test.txt"; + + if ($bExists) { - confess "bExists is set to ${bExists}, but exists() returned " . !$bExists; + system("echo 'TESTDATA' > ${strFile}"); } - }; - if ($@) - { - confess "error raised: " . $@ . "\n"; + # Execute in eval in case of error + eval + { + if ($oFile->exists(PATH_BACKUP_ABSOLUTE, $strFile) != $bExists) + { + confess "bExists is set to ${bExists}, but exists() returned " . !$bExists; + } + }; + + if ($@) + { + confess "error raised: " . $@ . "\n"; + } } } } - return; - #------------------------------------------------------------------------------------------------------------------------------- # Test copy() #------------------------------------------------------------------------------------------------------------------------------- - $iRun = 0; - - system("rm -rf lock"); - system("mkdir -p lock") == 0 or confess "Unable to create lock directory"; - - for (my $bBackupRemote = 0; $bBackupRemote <= 1; $bBackupRemote++) + if ($strTest eq 'all' || $strTest eq 'copy') { - my $strBackupHost = $bBackupRemote ? "127.0.0.1" : undef; - my $strBackupUser = $bBackupRemote ? "dsteele" : undef; + $iRun = 0; - # Loop through source compression - for (my $bDbRemote = 0; $bDbRemote <= 1; $bDbRemote++) + system("rm -rf lock"); + system("mkdir -p lock") == 0 or confess "Unable to create lock directory"; + + for (my $bBackupRemote = 0; $bBackupRemote <= 1; $bBackupRemote++) { - my $strDbHost = $bDbRemote ? "127.0.0.1" : undef; - my $strDbUser = $bDbRemote ? "dsteele" : undef; + my $strBackupHost = $bBackupRemote ? "127.0.0.1" : undef; + my $strBackupUser = $bBackupRemote ? "dsteele" : undef; - # Loop through destination compression - for (my $bDestinationCompressed = 0; $bDestinationCompressed <= 1; $bDestinationCompressed++) + # Loop through source compression + for (my $bDbRemote = 0; $bDbRemote <= 1; $bDbRemote++) { - my $oFile = pg_backrest_file->new - ( - strStanza => "db", - bNoCompression => !$bDestinationCompressed, - strBackupClusterPath => undef, - strBackupPath => ${strTestPath}, - strBackupHost => $strBackupHost, - strBackupUser => $strBackupUser, - strDbHost => $strDbHost, - strDbUser => $strDbUser, - strCommandCompress => "gzip --stdout %file%", - strCommandDecompress => "gzip -dc %file%", - strLockPath => dirname($0) . "/test/lock" - ); + my $strDbHost = $bDbRemote ? "127.0.0.1" : undef; + my $strDbUser = $bDbRemote ? "dsteele" : undef; - for (my $bSourceCompressed = 0; $bSourceCompressed <= 1; $bSourceCompressed++) + # Loop through destination compression + for (my $bDestinationCompressed = 0; $bDestinationCompressed <= 1; $bDestinationCompressed++) { - for (my $bSourcePathType = 0; $bSourcePathType <= 1; $bSourcePathType++) + my $oFile = pg_backrest_file->new + ( + strStanza => "db", + bNoCompression => !$bDestinationCompressed, + strBackupClusterPath => undef, + strBackupPath => ${strTestPath}, + strBackupHost => $strBackupHost, + strBackupUser => $strBackupUser, + strDbHost => $strDbHost, + strDbUser => $strDbUser, + strCommandCompress => "gzip --stdout %file%", + strCommandDecompress => "gzip -dc %file%", + strLockPath => dirname($0) . "/test/lock" + ); + + for (my $bSourceCompressed = 0; $bSourceCompressed <= 1; $bSourceCompressed++) { - my $strSourcePath = $bSourcePathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; - - for (my $bDestinationPathType = 0; $bDestinationPathType <= 1; $bDestinationPathType++) + for (my $bSourcePathType = 0; $bSourcePathType <= 1; $bSourcePathType++) { - my $strDestinationPath = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; + my $strSourcePath = $bSourcePathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; - for (my $bError = 0; $bError <= 1; $bError++) + for (my $bDestinationPathType = 0; $bDestinationPathType <= 1; $bDestinationPathType++) { - for (my $bConfessError = 0; $bConfessError <= 1; $bConfessError++) + my $strDestinationPath = $bDestinationPathType ? PATH_DB_ABSOLUTE : PATH_BACKUP_ABSOLUTE; + + for (my $bError = 0; $bError <= 1; $bError++) { - $iRun++; - - print "run ${iRun} - " . - "srcpth ${strSourcePath}, bkprmt $bBackupRemote, srccmp $bSourceCompressed, " . - "dstpth ${strDestinationPath}, dbrmt $bDbRemote, dstcmp $bDestinationCompressed, " . - "error $bError, confess_error $bConfessError\n"; - - # Drop the old test directory and create a new one - system("rm -rf test"); - system("mkdir -p test/lock") == 0 or confess "Unable to create the test directory"; - - my $strSourceFile = "${strTestPath}/test-source.txt"; - my $strDestinationFile = "${strTestPath}/test-destination.txt"; - - # Create the compressed or uncompressed test file - if ($bSourceCompressed) + for (my $bConfessError = 0; $bConfessError <= 1; $bConfessError++) { - $strSourceFile .= ".gz"; - system("echo 'TESTDATA' | gzip > ${strSourceFile}"); - } - else - { - system("echo 'TESTDATA' > ${strSourceFile}"); - } - # Create the file object based on current values + $iRun++; - if ($bError) - { - $strSourceFile .= ".error"; - } + print "run ${iRun} - " . + "srcpth ${strSourcePath}, bkprmt $bBackupRemote, srccmp $bSourceCompressed, " . + "dstpth ${strDestinationPath}, dbrmt $bDbRemote, dstcmp $bDestinationCompressed, " . + "error $bError, confess_error $bConfessError\n"; - # Run file copy in an eval block because some errors are expected - my $bReturn; + # Drop the old test directory and create a new one + system("rm -rf test"); + system("mkdir -p test/lock") == 0 or confess "Unable to create the test directory"; - eval - { - $bReturn = $oFile->file_copy($strSourcePath, $strSourceFile, - $strDestinationPath, $strDestinationFile, - undef, undef, undef, undef, - $bConfessError); - }; + my $strSourceFile = "${strTestPath}/test-source.txt"; + my $strDestinationFile = "${strTestPath}/test-destination.txt"; - # Check for errors after copy - if ($@) - { - # Different remote and destination with different path types should error - if ($bBackupRemote && $bDbRemote && ($strSourcePath ne $strDestinationPath)) + # Create the compressed or uncompressed test file + if ($bSourceCompressed) { - print " different source and remote for same path not supported\n"; - next; - } - # If the error was intentional, then also continue - elsif ($bError) - { - my $strError = $oFile->error_get(); - - if (!defined($strError) || ($strError eq '')) - { - confess 'no error message returned'; - } - - print " error raised: ${strError}\n"; - next; - } - # Else this is an unexpected error - else - { - confess $@; - } - } - elsif ($bError) - { - if ($bConfessError) - { - confess "Value was returned instead of exception thrown when confess error is true"; + $strSourceFile .= ".gz"; + system("echo 'TESTDATA' | gzip > ${strSourceFile}"); } else { - if ($bReturn) + system("echo 'TESTDATA' > ${strSourceFile}"); + } + # Create the file object based on current values + + if ($bError) + { + $strSourceFile .= ".error"; + } + + # Run file copy in an eval block because some errors are expected + my $bReturn; + + eval + { + $bReturn = $oFile->file_copy($strSourcePath, $strSourceFile, + $strDestinationPath, $strDestinationFile, + undef, undef, undef, undef, + $bConfessError); + }; + + # Check for errors after copy + if ($@) + { + # Different remote and destination with different path types should error + if ($bBackupRemote && $bDbRemote && ($strSourcePath ne $strDestinationPath)) { - confess "true was returned when an error was generated"; + print " different source and remote for same path not supported\n"; + next; } - else + # If the error was intentional, then also continue + elsif ($bError) { my $strError = $oFile->error_get(); @@ -843,30 +846,61 @@ sub BackRestFileTest confess 'no error message returned'; } - print " error returned: ${strError}\n"; + print " error raised: ${strError}\n"; next; } + # Else this is an unexpected error + else + { + confess $@; + } } - } - else - { - if (!$bReturn) + elsif ($bError) { - confess "error was returned when no error generated"; + if ($bConfessError) + { + confess "Value was returned instead of exception thrown when confess error is true"; + } + else + { + if ($bReturn) + { + confess "true was returned when an error was generated"; + } + else + { + my $strError = $oFile->error_get(); + + if (!defined($strError) || ($strError eq '')) + { + confess 'no error message returned'; + } + + print " error returned: ${strError}\n"; + next; + } + } + } + else + { + if (!$bReturn) + { + confess "error was returned when no error generated"; + } + + print " true was returned\n"; } - print " true was returned\n"; - } + # Check for errors after copy + if ($bDestinationCompressed) + { + $strDestinationFile .= ".gz"; + } - # Check for errors after copy - if ($bDestinationCompressed) - { - $strDestinationFile .= ".gz"; - } - - unless (-e $strDestinationFile) - { - confess "could not find destination file ${strDestinationFile}"; + unless (-e $strDestinationFile) + { + confess "could not find destination file ${strDestinationFile}"; + } } } } diff --git a/test/test.pl b/test/test.pl index 755e9aa92..0b0e8498e 100755 --- a/test/test.pl +++ b/test/test.pl @@ -20,9 +20,13 @@ use BackRestTest::FileTest; #################################################################################################################################### # Command line parameters #################################################################################################################################### -my $strLogLevel = "OFF"; # Log level for tests +my $strLogLevel = 'off'; # Log level for tests +my $strModule = 'all'; +my $strModuleTest = 'all'; -GetOptions ("log-level=s" => \$strLogLevel) +GetOptions ("log-level=s" => \$strLogLevel, + "module=s" => \$strModule, + "module-test=s" => \$strModuleTest) or die("Error in command line arguments\n"); #################################################################################################################################### @@ -34,9 +38,17 @@ umask(0); # Set console log level to trace for testing log_level_set(undef, uc($strLogLevel)); +if ($strModuleTest ne 'all' && $strModule eq 'all') +{ + confess "--module must be provided for test \"${strModuleTest}\""; +} + #################################################################################################################################### # Runs tests #################################################################################################################################### -BackRestFileTest(); +if ($strModule eq 'all' || $strModule eq "file") +{ + BackRestFileTest($strModuleTest); +} print "\nTEST COMPLETED SUCCESSFULLY (DESPITE ANY ERROR MESSAGES YOU SAW)\n";