1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-05 15:05:48 +02:00

Add hrnTzSet() to set timezone in tests.

3c8819e1 replaced gmtime/localtime with gmtime_r/localtime_r but did not take into account a subtle difference in how they operate. While gmtime/localtime operate as if tzset() has been called, i.e. they operate on the TZ env variable directly, gmtime_r/localtime_r require tzset() to be called after changing TZ for consistent results.

Rather than call tzset() every time TZ is changed, add hrnTzSet() to encapsulate both operations.
This commit is contained in:
David Steele 2024-05-20 08:44:37 +10:00
parent 28ad1badd9
commit 9910a4eddb
8 changed files with 39 additions and 22 deletions

View File

@ -669,7 +669,7 @@ testBldUnit(TestBuild *const this)
strReplace(
testC, STRDEF("{[C_TEST_TZ]}"),
testBldTimeZone(this) == NULL ?
STRDEF("// No timezone specified") : strNewFmt("setenv(\"TZ\", \"%s\", true);", strZ(testBldTimeZone(this))));
STRDEF("// No timezone specified") : strNewFmt("hrnTzSet(\"%s\");", strZ(testBldTimeZone(this))));
// Scale performance test
strReplace(testC, STRDEF("{[C_TEST_SCALE]}"), strNewFmt("%" PRIu64, testBldScale(this)));

View File

@ -8,6 +8,7 @@ C Test Harness
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "common/harnessDebug.h"
@ -326,6 +327,14 @@ hrnDiff(const char *expected, const char *actual)
FUNCTION_HARNESS_RETURN(STRINGZ, harnessDiffBuffer);
}
/**********************************************************************************************************************************/
void
hrnTzSet(const char *const tz)
{
setenv("TZ", tz, true);
tzset();
}
/**********************************************************************************************************************************/
void
hrnTestLogTitle(int lineNo)

View File

@ -48,6 +48,9 @@ void hrnFileWrite(const char *fileName, const unsigned char *buffer, size_t buff
// Diff two strings using command-line diff tool
const char *hrnDiff(const char *actual, const char *expected);
// Set timezone
void hrnTzSet(const char *tz);
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/

View File

@ -561,7 +561,7 @@ testRun(void)
hrnProtocolLocalShimInstall(testLocalHandlerList, LENGTH_OF(testLocalHandlerList));
// The tests expect the timezone to be UTC
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
Storage *storageTest = storagePosixNewP(TEST_PATH_STR, .write = true);

View File

@ -22,7 +22,7 @@ testRun(void)
Storage *storageTest = storagePosixNewP(TEST_PATH_STR, .write = true);
// The tests expect the timezone to be UTC
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
// *****************************************************************************************************************************
if (testBegin("infoRender()"))
@ -1658,7 +1658,7 @@ testRun(void)
HRN_CFG_LOAD(cfgCmdInfo, argList2);
// Switch to America/New_York to test + timezone offset without minutes
setenv("TZ", "America/New_York", true);
hrnTzSet("America/New_York");
TEST_RESULT_STR_Z(
infoRender(),
@ -1687,7 +1687,7 @@ testRun(void)
"text - backup set requested");
// Reset timezone
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
hrnCfgArgRawZ(argList2, cfgOptOutput, "json");
HRN_CFG_LOAD(cfgCmdInfo, argList2);
@ -1851,7 +1851,7 @@ testRun(void)
HRN_CFG_LOAD(cfgCmdInfo, argList2);
// Switch to Asia/Kolkata to test - timezone offset with 30 minutes
setenv("TZ", "Asia/Kolkata", true);
hrnTzSet("Asia/Kolkata");
TEST_RESULT_STR_Z(
infoRender(),
@ -1880,7 +1880,7 @@ testRun(void)
"text - multi-repo, backup set requested, found on repo2, report stanza and db over all repos");
// Reset timezone
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
hrnCfgArgRawZ(argList2, cfgOptOutput, "json");
HRN_CFG_LOAD(cfgCmdInfo, argList2);
@ -2062,7 +2062,7 @@ testRun(void)
.comment = "write manifest with checksum errors and no links");
// Switch to Pacific/Chatham to test + timezone offset with 45 minutes
setenv("TZ", "Pacific/Chatham", true);
hrnTzSet("Pacific/Chatham");
TEST_RESULT_STR_Z(
infoRender(),
@ -2085,7 +2085,7 @@ testRun(void)
"text - backup set requested, no links");
// Reset timezone
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
hrnCfgArgRawZ(argList2, cfgOptOutput, "json");
HRN_CFG_LOAD(cfgCmdInfo, argList2);
@ -2240,7 +2240,7 @@ testRun(void)
.comment = " rewrite same manifest without checksum errors");
// Switch to America/St_Johns to test - timezone offset with 30 minutes
setenv("TZ", "America/St_Johns", true);
hrnTzSet("America/St_Johns");
TEST_RESULT_STR_Z(
infoRender(),
@ -2262,7 +2262,7 @@ testRun(void)
"text - backup set requested, no db and no checksum error");
// Reset timezone
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
hrnCfgArgRawZ(argList2, cfgOptOutput, "json");
HRN_CFG_LOAD(cfgCmdInfo, argList2);
@ -2757,7 +2757,7 @@ testRun(void)
"/9.5-2/0000000100000000/000000010000000000000006-47dff2b7552a9d66e4bae1a762488a6885e7082c.gz");
// Switch to America/New_York to test + timezone offset without minutes
setenv("TZ", "America/New_York", true);
hrnTzSet("America/New_York");
TEST_RESULT_STR_Z(
infoRender(),
@ -2795,7 +2795,7 @@ testRun(void)
"text - multi-repo, database mismatch, repo2 stanza-upgrade needed");
// Reset timezone
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
hrnCfgArgRawZ(argList2, cfgOptOutput, "json");
HRN_CFG_LOAD(cfgCmdInfo, argList2);

View File

@ -23,7 +23,7 @@ testRun(void)
FUNCTION_HARNESS_VOID();
// The tests expect the timezone to be UTC
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
// Install local command handler shim
static const ProtocolServerHandler testLocalHandlerList[] = {PROTOCOL_SERVER_HANDLER_BACKUP_LIST};

View File

@ -388,7 +388,8 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("system time UTC");
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
TEST_RESULT_INT(getEpoch(STRDEF("2020-01-08 09:18:15-0700")), 1578500295, "epoch with timezone");
TEST_RESULT_INT(getEpoch(STRDEF("2020-01-08 16:18:15.0000")), 1578500295, "same epoch no timezone");
TEST_RESULT_INT(getEpoch(STRDEF("2020-01-08 16:18:15.0000+00")), 1578500295, "same epoch timezone 0");
@ -398,10 +399,13 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("system time America/New_York");
setenv("TZ", "America/New_York", true);
hrnTzSet("America/New_York");
time_t testTime = 1573754569;
char timeBuffer[20];
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", localtime(&testTime));
struct tm timePart;
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", localtime_r(&testTime, &timePart));
TEST_RESULT_Z(timeBuffer, "2019-11-14 13:02:49", "check timezone set");
TEST_RESULT_INT(getEpoch(STRDEF("2019-11-14 13:02:49-0500")), 1573754569, "offset same as local");
TEST_RESULT_INT(getEpoch(STRDEF("2019-11-14 13:02:49")), 1573754569, "GMT-0500 (EST)");
@ -417,7 +421,7 @@ testRun(void)
"HINT: time format must be YYYY-MM-DD HH:MM:SS with optional msec and optional timezone (+/- HH or HHMM or HH:MM) - if"
" timezone is omitted, local time is assumed (for UTC use +00)");
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
}
// *****************************************************************************************************************************
@ -491,7 +495,8 @@ testRun(void)
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("target time");
setenv("TZ", "UTC", true);
hrnTzSet("UTC");
const String *repoPath2 = STRDEF(TEST_PATH "/repo2");

View File

@ -848,7 +848,7 @@ testRun(void)
strReplace(testCDup, STRDEF("{[C_TEST_PATH_BUILD]}"), STRDEF(TEST_PATH "/test/unit-3/none/build"));
strReplace(testCDup, STRDEF("{[C_TEST_PROFILE]}"), STRDEF("true"));
strReplace(testCDup, STRDEF("{[C_TEST_PROJECT_EXE]}"), STRDEF(TEST_PATH "/test/build/uXX/src/pgbackrest"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("setenv(\"TZ\", \"America/New_York\", true);"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("hrnTzSet(\"America/New_York\");"));
strReplace(testCDup, STRDEF("{[C_INCLUDE]}"), STRDEF(""));
strReplace(
@ -1006,7 +1006,7 @@ testRun(void)
strReplace(testCDup, STRDEF("{[C_TEST_PATH_BUILD]}"), STRDEF(TEST_PATH "/test/unit-3/uXX/build"));
strReplace(testCDup, STRDEF("{[C_TEST_PROFILE]}"), STRDEF("true"));
strReplace(testCDup, STRDEF("{[C_TEST_PROJECT_EXE]}"), STRDEF(TEST_PATH "/test/build/uXX/src/pgbackrest"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("setenv(\"TZ\", \"America/New_York\", true);"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("hrnTzSet(\"America/New_York\");"));
strReplace(testCDup, STRDEF("{[C_INCLUDE]}"), STRDEF(""));
strReplace(
@ -1048,7 +1048,7 @@ testRun(void)
strReplace(testCDup, STRDEF("{[C_TEST_PATH_BUILD]}"), STRDEF(TEST_PATH "/test/unit-3/uXX/build"));
strReplace(testCDup, STRDEF("{[C_TEST_PROFILE]}"), STRDEF("false"));
strReplace(testCDup, STRDEF("{[C_TEST_PROJECT_EXE]}"), STRDEF(TEST_PATH "/test/build/uXX/src/pgbackrest"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("setenv(\"TZ\", \"America/New_York\", true);"));
strReplace(testCDup, STRDEF("{[C_TEST_TZ]}"), STRDEF("hrnTzSet(\"America/New_York\");"));
strReplace(testCDup, STRDEF("{[C_INCLUDE]}"), STRDEF(""));
strReplace(