1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Cleanup output to stderr in unit tests.

The unit tests were ignoring stderr but nothing being output there was important. Now a test will fail if there is anything on stderr.

This makes it easier to work with -fsanitize, which outputs to stderr.
This commit is contained in:
David Steele 2022-03-24 18:43:43 -06:00
parent 50ee4b19fe
commit f60ec5055a
6 changed files with 21 additions and 5 deletions

View File

@ -722,12 +722,14 @@ sub run
'') .
$self->{strMakeCmd} . " -j $self->{iBuildMax} -s 2>&1 && \\\n" .
"rm ${strBuildProcessingFile} && \\\n" .
# Allow stderr to be copied to stderr and stdout
"exec 3>&1 && \\\n" .
# Test with valgrind when requested
($self->{bValgrindUnit} && $self->{oTest}->{&TEST_TYPE} ne TESTDEF_PERFORMANCE ?
'valgrind -q --gen-suppressions=all' .
($self->{oStorageTest}->exists($strValgrindSuppress) ? " --suppressions=${strValgrindSuppress}" : '') .
" --exit-on-first-error=yes --leak-check=full --leak-resolution=high --error-exitcode=25" . ' ' : '') .
"./test.bin 2>&1" .
"./test.bin 2>&1 1>&3 | tee /dev/stderr" .
($self->{oTest}->{&TEST_VM} ne VM_NONE ? "'" : '');
}
else
@ -833,20 +835,20 @@ sub end
my $fTestElapsedTime = ceil((gettimeofday() - $self->{oProcess}{start_time}) * 100) / 100;
# Output error
if ($iExitStatus != 0)
if ($iExitStatus != 0 || (defined($oExecDone->{strErrorLog}) && $oExecDone->{strErrorLog} ne ''))
{
# Get stdout
my $strOutput = trim($oExecDone->{strOutLog}) ? "STDOUT:\n" . trim($oExecDone->{strOutLog}) : '';
# Get stderr
if (trim($oExecDone->{strSuppressedErrorLog}) ne '')
if (defined($oExecDone->{strErrorLog}) && trim($oExecDone->{strErrorLog}) ne '')
{
if ($strOutput ne '')
{
$strOutput .= "\n";
}
$strOutput .= "STDERR:\n" . trim($oExecDone->{strSuppressedErrorLog});
$strOutput .= "STDERR:\n" . trim($oExecDone->{strErrorLog});
}
# If no stdout or stderr output something rather than a blank line

View File

@ -131,6 +131,14 @@ testBegin(const char *name)
// Make sure there is nothing untested left in the log
harnessLogFinal();
// It is possible the test left the cwd in a weird place
if (chdir(testPath()) != 0)
{
fprintf(stderr, "ERROR: unable to chdir to test path '%s'\n", testPath());
fflush(stderr);
exit(255);
}
// Clear out the test directory so the next test starts clean
char buffer[2048];
snprintf(

View File

@ -130,6 +130,7 @@ testRun(void)
StringList *argList = strLstNew();
hrnCfgArgRawZ(argList, cfgOptConfig, TEST_PATH "/pgbackrest.conf");
hrnCfgArgRawFmt(argList, cfgOptTlsServerPort, "%u", hrnServerPort(0));
hrnCfgArgRawZ(argList, cfgOptLogLevelStderr, CFGOPTVAL_ARCHIVE_MODE_OFF_Z);
HRN_CFG_LOAD(cfgCmdServer, argList);
// Init exit signal handlers

View File

@ -99,7 +99,7 @@ testSuite(CompressType type, const char *decompressCmd)
TEST_TITLE("compressed output can be decompressed with command-line tool");
storagePutP(storageNewWriteP(storageTest, STRDEF("test.cmp")), compressed);
HRN_SYSTEM_FMT("%s " TEST_PATH "/test.cmp > " TEST_PATH "/test.out", decompressCmd);
HRN_SYSTEM_FMT("%s " TEST_PATH "/test.cmp > " TEST_PATH "/test.out 2> /dev/null", decompressCmd);
TEST_RESULT_BOOL(bufEq(decompressed, storageGetP(storageNewReadP(storageTest, STRDEF("test.out")))), true, "check output");
TEST_RESULT_BOOL(

View File

@ -403,6 +403,9 @@ testRun(void)
{
HRN_FORK_CHILD_BEGIN(.expectedExitStatus = UnhandledError.code)
{
// Redirect stderr to stdout (we do not care about the output here since coverage will tell us we hit the code)
stderr = stdout;
THROW(TestChildError, "does not get caught!");
}
HRN_FORK_CHILD_END();

View File

@ -544,6 +544,7 @@ testRun(void)
hrnCfgArgRawZ(argList, cfgOptLockPath, HRN_PATH "/lock");
hrnCfgArgRawZ(argList, cfgOptLogPath, "/bogus");
hrnCfgArgRawZ(argList, cfgOptLogLevelFile, "info");
hrnCfgArgRawZ(argList, cfgOptLogLevelStderr, CFGOPTVAL_ARCHIVE_MODE_OFF_Z);
strLstAddZ(argList, CFGCMD_BACKUP);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "load config for backup");
lockRelease(true);
@ -563,6 +564,7 @@ testRun(void)
strLstAddZ(argList, PROJECT_BIN);
hrnCfgArgRawZ(argList, cfgOptStanza, "db");
hrnCfgArgRawZ(argList, cfgOptLockPath, HRN_PATH "/lock");
hrnCfgArgRawZ(argList, cfgOptLogLevelStderr, CFGOPTVAL_ARCHIVE_MODE_OFF_Z);
strLstAddZ(argList, CFGCMD_EXPIRE);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "load config");