1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-02-21 19:48:29 +02:00

Add log shim.

This allows DEBUG_UNIT and DEBUG_UNIT_EXTERN to be removed since static log variables can now be exposed by functions in the harness.
This commit is contained in:
David Steele 2021-05-21 12:51:32 -04:00
parent ef63750e0b
commit 15b8b9207d
13 changed files with 93 additions and 72 deletions

View File

@ -181,6 +181,7 @@
<commit subject="Factor local process exec out of protocolLocalGet()."/>
<commit subject="Add shim feature for unit tests."/>
<commit subject="Add local process shim."/>
<commit subject="Add log shim."/>
<p>Add local processs shim.</p>
</release-item>

View File

@ -9,16 +9,6 @@ Debug Routines
#include "common/type/convert.h"
#include "common/type/stringz.h"
/***********************************************************************************************************************************
Extern variables that are needed for unit testing
***********************************************************************************************************************************/
#ifdef DEBUG_UNIT
#define DEBUG_UNIT_EXTERN
#else
#define DEBUG_UNIT_EXTERN \
static
#endif
/***********************************************************************************************************************************
Base function debugging macros

View File

@ -29,16 +29,16 @@ static LogLevel logLevelAny = logLevelError;
// Log file descriptors
static int logFdStdOut = STDOUT_FILENO;
static int logFdStdErr = STDERR_FILENO;
DEBUG_UNIT_EXTERN int logFdFile = -1;
static int logFdFile = -1;
// Has the log file banner been written yet?
DEBUG_UNIT_EXTERN bool logFileBanner = false;
static bool logFileBanner = false;
// Is the timestamp printed in the log?
static bool logTimestamp = false;
// Default process id if none is specified
DEBUG_UNIT_EXTERN unsigned int logProcessId = 0;
static unsigned int logProcessId = 0;
// Size of the process id field
static int logProcessSize = 2;
@ -114,7 +114,7 @@ logLevelStr(LogLevel logLevel)
}
/**********************************************************************************************************************************/
DEBUG_UNIT_EXTERN void
static void
logAnySet(void)
{
FUNCTION_TEST_VOID();

View File

@ -10,7 +10,6 @@
# Most options can be set for modules and tests (test option will override module option if both are set):
# * db - determines if the test will be run against multiple db versions
# * define - defines for C code (will also be applied to the test harness)
# * debugUnitSuppress - don't define DEBUG_UNIT for unit tests -- this is used to test unit test debugging macros
# * binReq - is the pgbackrest binary required for this test?
# * containerReq - is this test required to run in a container?
#
@ -79,16 +78,14 @@ unit:
- name: assert-off
total: 1
define: -DNDEBUG
debugUnitSuppress: true
coverage:
- common/assert: noCode
# ----------------------------------------------------------------------------------------------------------------------------
- name: debug-off
total: 2
total: 1
define: -DNDEBUG
debugUnitSuppress: true
coverage:
- common/debug
@ -182,14 +179,17 @@ unit:
- name: log
total: 5
feature: log
harness: log
harness:
name: log
shim:
common/log: ~
coverage:
- common/log
# ----------------------------------------------------------------------------------------------------------------------------
- name: debug-on
total: 4
total: 3
feature: debug
coverage:
@ -456,9 +456,6 @@ unit:
- config/parse
- config/parse.auto: noCode
include:
- common/log
# ----------------------------------------------------------------------------------------------------------------------------
- name: load
total: 4
@ -467,7 +464,6 @@ unit:
- config/load
include:
- common/log
- common/io/socket/common
# ----------------------------------------------------------------------------------------------------------------------------

View File

@ -56,8 +56,6 @@ use constant TESTDEF_DEFINE => 'define';
push @EXPORT, qw(TESTDEF_DEFINE);
use constant TESTDEF_FEATURE => 'feature';
push @EXPORT, qw(TESTDEF_FEATURE);
use constant TESTDEF_DEBUG_UNIT_SUPPRESS => 'debugUnitSuppress';
push @EXPORT, qw(TESTDEF_DEBUG_UNIT_SUPPRESS);
use constant TESTDEF_HARNESS => 'harness';
push @EXPORT, qw(TESTDEF_HARNESS);
# Harness name which must match the harness implementation file name
@ -152,8 +150,7 @@ sub testDefLoad
# Resolve variables that can be set in the module or the test
foreach my $strVar (
TESTDEF_DEFINE, TESTDEF_DEBUG_UNIT_SUPPRESS, TESTDEF_DB, TESTDEF_BIN_REQ, TESTDEF_VM,
TESTDEF_CONTAINER_REQUIRED)
TESTDEF_DEFINE, TESTDEF_DB, TESTDEF_BIN_REQ, TESTDEF_VM, TESTDEF_CONTAINER_REQUIRED)
{
$hTestDefHash->{$strModule}{$strTest}{$strVar} = coalesce(
$hModuleTest->{$strVar}, $hModule->{$strVar}, $strVar eq TESTDEF_VM ? undef : false);

View File

@ -295,7 +295,6 @@ sub run
($self->{bProfile} ? " \\\n\t-pg" : '') .
(vmArchBits($self->{oTest}->{&TEST_VM}) == 32 ? " \\\n\t-D_FILE_OFFSET_BITS=64" : '') .
($self->{bDebug} ? '' : " \\\n\t-DNDEBUG") .
($self->{oTest}->{&TEST_DEBUG_UNIT_SUPPRESS} ? '' : " \\\n\t-DDEBUG_UNIT") .
($self->{oTest}->{&TEST_VM} eq VM_CO7 ? " \\\n\t-DDEBUG_EXEC_TIME" : '') .
($bCoverage ? " \\\n\t-DDEBUG_COVERAGE" : '') .
($self->{bDebugTestTrace} && $self->{bDebug} ? " \\\n\t-DDEBUG_TEST_TRACE" : '') .

View File

@ -32,8 +32,6 @@ use constant TEST_CTESTDEF => 'ctestdef
push @EXPORT, qw(TEST_CTESTDEF);
use constant TEST_CONTAINER => 'container';
push @EXPORT, qw(TEST_CONTAINER);
use constant TEST_DEBUG_UNIT_SUPPRESS => TESTDEF_DEBUG_UNIT_SUPPRESS;
push @EXPORT, qw(TEST_DEBUG_UNIT_SUPPRESS);
use constant TEST_MODULE => 'module';
push @EXPORT, qw(TEST_MODULE);
use constant TEST_NAME => 'test';
@ -172,7 +170,6 @@ sub testListGet
&TEST_C => coalesce($hTest->{&TESTDEF_C}, $hModule->{&TESTDEF_C}, false),
&TEST_CDEF => $hTest->{&TESTDEF_DEFINE},
&TEST_CTESTDEF => $hTest->{&TESTDEF_FEATURE},
&TEST_DEBUG_UNIT_SUPPRESS => $hTest->{&TEST_DEBUG_UNIT_SUPPRESS},
&TEST_CONTAINER => defined($hTest->{&TESTDEF_CONTAINER}) ?
$hTest->{&TESTDEF_CONTAINER} : $hModule->{&TESTDEF_CONTAINER},
&TEST_PGSQL_BIN => $strPgSqlBin,

View File

@ -18,13 +18,9 @@ Log Test Harness
#include "common/harnessTest.h"
/***********************************************************************************************************************************
Expose log internal data for unit testing/debugging
Include shimmed C modules
***********************************************************************************************************************************/
extern LogLevel logLevelFile;
extern int logFdFile;
extern bool logFileBanner;
extern unsigned int logProcessId;
extern void logAnySet(void);
{[SHIM_MODULE]}
/***********************************************************************************************************************************
Log settings for testing
@ -92,6 +88,53 @@ harnessLogDryRunSet(bool dryRun)
logInit(logLevelTestDefault, logLevelOff, logLevelTest, false, logProcessId, 99, logDryRunTest);
}
/**********************************************************************************************************************************/
unsigned int
hrnLogLevelFile(void)
{
return logLevelFile;
}
void hrnLogLevelFileSet(unsigned int logLevel)
{
logLevelFile = logLevel;
}
unsigned int
hrnLogLevelStdOut(void)
{
return logLevelStdOut;
}
void hrnLogLevelStdOutSet(unsigned int logLevel)
{
logLevelStdOut = logLevel;
}
unsigned int
hrnLogLevelStdErr(void)
{
return logLevelStdErr;
}
void hrnLogLevelStdErrSet(unsigned int logLevel)
{
logLevelStdErr = logLevel;
}
/**********************************************************************************************************************************/
bool
hrnLogTimestamp(void)
{
return logTimestamp;
}
void
hrnLogTimestampSet(bool log)
{
logTimestamp = log;
}
/***********************************************************************************************************************************
Change test log level

View File

@ -23,6 +23,18 @@ void harnessLogResultRegExp(const char *expression);
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
// Get/set log levels
unsigned int hrnLogLevelFile(void);
void hrnLogLevelFileSet(unsigned int logLevel);
unsigned int hrnLogLevelStdOut(void);
void hrnLogLevelStdOutSet(unsigned int logLevel);
unsigned int hrnLogLevelStdErr(void);
void hrnLogLevelStdErrSet(unsigned int logLevel);
// Get/set log timestamp
bool hrnLogTimestamp(void);
void hrnLogTimestampSet(bool log);
void harnessLogLevelReset(void);
void harnessLogLevelSet(LogLevel logLevel);

View File

@ -23,12 +23,5 @@ testRun(void)
TEST_RESULT_BOOL(debug, false, "DEBUG is not defined");
}
// *****************************************************************************************************************************
if (testBegin("DEBUG_UNIT_EXTERN"))
{
const char *debugUnitExtern = STRINGIFY(DEBUG_UNIT_EXTERN);
TEST_RESULT_Z(debugUnitExtern, "static", "DEBUG_UNIT_EXTERN is static");
}
FUNCTION_HARNESS_RETURN_VOID();
}

View File

@ -106,13 +106,6 @@ testRun(void)
TEST_RESULT_BOOL(debug, true, "DEBUG is defined");
}
// *****************************************************************************************************************************
if (testBegin("DEBUG_UNIT_EXTERN"))
{
const char *debugUnitExtern = STRINGIFY(DEBUG_UNIT_EXTERN);
TEST_RESULT_Z(debugUnitExtern, "", "DEBUG_UNIT_EXTERN is blank (extern)");
}
// *****************************************************************************************************************************
if (testBegin("FUNCTION_DEBUG() and FUNCTION_LOG_RETURN()"))
{

View File

@ -25,10 +25,10 @@ testRun(void)
TEST_RESULT_VOID(cfgLoadLogSetting(), "load log settings all defaults");
TEST_RESULT_INT(logLevelStdOut, logLevelOff, "console logging is off");
TEST_RESULT_INT(logLevelStdErr, logLevelOff, "stderr logging is off");
TEST_RESULT_INT(logLevelFile, logLevelOff, "file logging is off");
TEST_RESULT_BOOL(logTimestamp, true, "timestamp logging is on");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelOff, "console logging is off");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelOff, "stderr logging is off");
TEST_RESULT_INT(hrnLogLevelFile(), logLevelOff, "file logging is off");
TEST_RESULT_BOOL(hrnLogTimestamp(), true, "timestamp logging is on");
}
// *****************************************************************************************************************************

View File

@ -836,15 +836,15 @@ testRun(void)
strLstAdd(argList, strNew("--log-level-stderr=info"));
strLstAddZ(argList, CFGCMD_BACKUP ":" CONFIG_COMMAND_ROLE_LOCAL);
logLevelStdOut = logLevelError;
logLevelStdErr = logLevelError;
hrnLogLevelStdOutSet(logLevelError);
hrnLogLevelStdErrSet(logLevelError);
TEST_RESULT_VOID(configParse(storageTest, strLstSize(argList), strLstPtr(argList), true), "load local config");
TEST_RESULT_STR_Z(cfgOptionStr(cfgOptPgPath), "/path/to/2", "default pg-path");
TEST_RESULT_INT(cfgCommandRole(), cfgCmdRoleLocal, " command role is local");
TEST_RESULT_BOOL(cfgLockRequired(), false, " backup:local command does not require lock");
TEST_RESULT_STR_Z(cfgCommandRoleName(), "backup:local", " command/role name is backup:local");
TEST_RESULT_INT(logLevelStdOut, logLevelError, "console logging is error");
TEST_RESULT_INT(logLevelStdErr, logLevelError, "stderr logging is error");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelError, "console logging is error");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelError, "stderr logging is error");
argList = strLstNew();
strLstAdd(argList, strNew("pgbackrest"));
@ -855,13 +855,13 @@ testRun(void)
strLstAdd(argList, strNew("--log-level-stderr=info"));
strLstAddZ(argList, CFGCMD_BACKUP ":" CONFIG_COMMAND_ROLE_REMOTE);
logLevelStdOut = logLevelError;
logLevelStdErr = logLevelError;
hrnLogLevelStdOutSet(logLevelError);
hrnLogLevelStdErrSet(logLevelError);
TEST_RESULT_VOID(configParse(storageTest, strLstSize(argList), strLstPtr(argList), true), "load remote config");
TEST_RESULT_INT(cfgCommandRole(), cfgCmdRoleRemote, " command role is remote");
TEST_RESULT_STR_Z(cfgCommandRoleStr(cfgCmdRoleRemote), "remote", " remote role name");
TEST_RESULT_INT(logLevelStdOut, logLevelError, "console logging is error");
TEST_RESULT_INT(logLevelStdErr, logLevelError, "stderr logging is error");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelError, "console logging is error");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelError, "stderr logging is error");
argList = strLstNew();
strLstAdd(argList, strNew("pgbackrest"));
@ -870,12 +870,12 @@ testRun(void)
strLstAdd(argList, strNew("--log-level-stderr=info"));
strLstAddZ(argList, CFGCMD_ARCHIVE_GET ":" CONFIG_COMMAND_ROLE_ASYNC);
logLevelStdOut = logLevelError;
logLevelStdErr = logLevelError;
hrnLogLevelStdOutSet(logLevelError);
hrnLogLevelStdErrSet(logLevelError);
TEST_RESULT_VOID(configParse(storageTest, strLstSize(argList), strLstPtr(argList), true), "load async config");
TEST_RESULT_INT(cfgCommandRole(), cfgCmdRoleAsync, " command role is async");
TEST_RESULT_INT(logLevelStdOut, logLevelError, "console logging is error");
TEST_RESULT_INT(logLevelStdErr, logLevelError, "stderr logging is error");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelError, "console logging is error");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelError, "stderr logging is error");
harnessLogLevelReset();
@ -1158,13 +1158,13 @@ testRun(void)
argList = strLstNew();
strLstAdd(argList, strNew(TEST_BACKREST_EXE));
logLevelStdOut = logLevelOff;
logLevelStdErr = logLevelOff;
hrnLogLevelStdOutSet(logLevelOff);
hrnLogLevelStdErrSet(logLevelOff);
TEST_RESULT_VOID(configParse(storageTest, strLstSize(argList), strLstPtr(argList), true), "no command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, " help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, " command is none");
TEST_RESULT_INT(logLevelStdOut, logLevelWarn, "console logging is warn");
TEST_RESULT_INT(logLevelStdErr, logLevelWarn, "stderr logging is warn");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelWarn, "console logging is warn");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelWarn, "stderr logging is warn");
harnessLogLevelReset();
// -------------------------------------------------------------------------------------------------------------------------