You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-06-16 23:47:38 +02:00
Having a copy per version worked well until it was time to add new features or modify existing functions. Then it was necessary to modify every version and try to keep them all in sync. Consolidate all the PostgreSQL types into a single file using #if for type versions. Many types do not change or change infrequently so this cuts down on duplication. In addition, it is far easier to see what has changed when a new version is added. Use macros to write the interface functions. There is still duplication here since some changes require a new copy of the macro, but it is far less than before.
174 lines
9.3 KiB
C
174 lines
9.3 KiB
C
/***********************************************************************************************************************************
|
|
Test PostgreSQL Interface
|
|
***********************************************************************************************************************************/
|
|
#include "storage/driver/posix/storage.h"
|
|
|
|
/***********************************************************************************************************************************
|
|
Test Run
|
|
***********************************************************************************************************************************/
|
|
void
|
|
testRun(void)
|
|
{
|
|
FUNCTION_HARNESS_VOID();
|
|
|
|
Storage *storageTest = storageDriverPosixInterface(
|
|
storageDriverPosixNew(strNew(testPath()), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL));
|
|
|
|
// *****************************************************************************************************************************
|
|
if (testBegin("pgVersionFromStr() and pgVersionToStr()"))
|
|
{
|
|
TEST_ERROR(pgVersionFromStr(strNew("9.3.4")), AssertError, "version 9.3.4 format is invalid");
|
|
TEST_ERROR(pgVersionFromStr(strNew("abc")), AssertError, "version abc format is invalid");
|
|
TEST_ERROR(pgVersionFromStr(NULL), AssertError, "assertion 'version != NULL' failed");
|
|
|
|
TEST_RESULT_INT(pgVersionFromStr(strNew("10")), PG_VERSION_10, "valid pg version 10");
|
|
TEST_RESULT_INT(pgVersionFromStr(strNew("9.6")), 90600, "valid pg version 9.6");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
TEST_RESULT_STR(strPtr(pgVersionToStr(PG_VERSION_11)), "11", "infoPgVersionToString 11");
|
|
TEST_RESULT_STR(strPtr(pgVersionToStr(PG_VERSION_96)), "9.6", "infoPgVersionToString 9.6");
|
|
TEST_RESULT_STR(strPtr(pgVersionToStr(83456)), "8.34", "infoPgVersionToString 83456");
|
|
}
|
|
|
|
// *****************************************************************************************************************************
|
|
if (testBegin("pgControlFromBuffer() and pgControlFromFile()"))
|
|
{
|
|
// Sanity test to ensure PG_VERSION_MAX has been updated
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
TEST_RESULT_UINT(pgInterface[0].version, PG_VERSION_MAX, "check max version");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
String *controlFile = strNew(PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL);
|
|
|
|
// Create a bogus control file
|
|
Buffer *result = bufNew(PG_CONTROL_SIZE);
|
|
memset(bufPtr(result), 0, bufSize(result));
|
|
bufUsedSet(result, bufSize(result));
|
|
((PgControlCommon *)bufPtr(result))->controlVersion = 501;
|
|
((PgControlCommon *)bufPtr(result))->catalogVersion = 19780101;
|
|
|
|
TEST_ERROR(
|
|
pgControlFromBuffer(result), VersionNotSupportedError,
|
|
"unexpected control version = 501 and catalog version = 19780101\nHINT: is this version of PostgreSQL supported?");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
TEST_ERROR(pgControlTestToBuffer((PgControl){.version = 0}), AssertError, "invalid version 0");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
storagePutNP(
|
|
storageNewWriteNP(storageTest, controlFile),
|
|
pgControlTestToBuffer((PgControl){.version = PG_VERSION_11, .systemId = 0xFACEFACE, .walSegmentSize = 1024 * 1024}));
|
|
|
|
PgControl info = {0};
|
|
TEST_ASSIGN(info, pgControlFromFile(strNew(testPath())), "get control info v11");
|
|
TEST_RESULT_INT(info.systemId, 0xFACEFACE, " check system id");
|
|
TEST_RESULT_INT(info.controlVersion, 1100, " check control version");
|
|
TEST_RESULT_INT(info.catalogVersion, 201809051, " check catalog version");
|
|
TEST_RESULT_INT(info.version, PG_VERSION_11, " check version");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
storagePutNP(
|
|
storageNewWriteNP(storageTest, controlFile),
|
|
pgControlTestToBuffer((PgControl){.version = PG_VERSION_93, .walSegmentSize = 1024 * 1024}));
|
|
|
|
TEST_ERROR(
|
|
pgControlFromFile(strNew(testPath())), FormatError,
|
|
"wal segment size is 1048576 but must be 16777216 for PostgreSQL <= 10");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
storagePutNP(
|
|
storageNewWriteNP(storageTest, controlFile),
|
|
pgControlTestToBuffer((PgControl){.version = PG_VERSION_95, .pageSize = 32 * 1024}));
|
|
|
|
TEST_ERROR(pgControlFromFile(strNew(testPath())), FormatError, "page size is 32768 but must be 8192");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
storagePutNP(
|
|
storageNewWriteNP(storageTest, controlFile),
|
|
pgControlTestToBuffer((PgControl){.version = PG_VERSION_83, .systemId = 0xEFEFEFEFEF}));
|
|
|
|
TEST_ASSIGN(info, pgControlFromFile(strNew(testPath())), "get control info v83");
|
|
TEST_RESULT_INT(info.systemId, 0xEFEFEFEFEF, " check system id");
|
|
TEST_RESULT_INT(info.controlVersion, 833, " check control version");
|
|
TEST_RESULT_INT(info.catalogVersion, 200711281, " check catalog version");
|
|
TEST_RESULT_INT(info.version, PG_VERSION_83, " check version");
|
|
}
|
|
|
|
// *****************************************************************************************************************************
|
|
if (testBegin("pgWalFromBuffer() and pgWalFromFile()"))
|
|
{
|
|
String *walFile = strNewFmt("%s/0000000F0000000F0000000F", testPath());
|
|
|
|
// Create a bogus control file, initially not in long format)
|
|
// --------------------------------------------------------------------------------------------------------------------------
|
|
Buffer *result = bufNew((size_t)16 * 1024 * 1024);
|
|
memset(bufPtr(result), 0, bufSize(result));
|
|
bufUsedSet(result, bufSize(result));
|
|
((PgWalCommon *)bufPtr(result))->magic = 777;
|
|
|
|
TEST_ERROR(pgWalFromBuffer(result), FormatError, "first page header in WAL file is expected to be in long format");
|
|
|
|
// Add the long flag so that the version will now error
|
|
// --------------------------------------------------------------------------------------------------------------------------
|
|
((PgWalCommon *)bufPtr(result))->flag = PG_WAL_LONG_HEADER;
|
|
|
|
TEST_ERROR(
|
|
pgWalFromBuffer(result), VersionNotSupportedError,
|
|
"unexpected WAL magic 777\n"
|
|
"HINT: is this version of PostgreSQL supported?");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
TEST_ERROR(pgWalTestToBuffer((PgWal){.version = 0}, result), AssertError, "invalid version 0");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
pgWalTestToBuffer((PgWal){.version = PG_VERSION_11, .systemId = 0xECAFECAF}, result);
|
|
storagePutNP(storageNewWriteNP(storageTest, walFile), result);
|
|
|
|
PgWal info = {0};
|
|
TEST_ASSIGN(info, pgWalFromFile(walFile), "get wal info v11");
|
|
TEST_RESULT_INT(info.systemId, 0xECAFECAF, " check system id");
|
|
TEST_RESULT_INT(info.version, PG_VERSION_11, " check version");
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
pgWalTestToBuffer((PgWal){.version = PG_VERSION_83, .systemId = 0xEAEAEAEA}, result);
|
|
storagePutNP(storageNewWriteNP(storageTest, walFile), result);
|
|
|
|
TEST_ASSIGN(info, pgWalFromFile(walFile), "get wal info v8.3");
|
|
TEST_RESULT_INT(info.systemId, 0xEAEAEAEA, " check system id");
|
|
TEST_RESULT_INT(info.version, PG_VERSION_83, " check version");
|
|
}
|
|
|
|
// *****************************************************************************************************************************
|
|
if (testBegin("pgControlToLog()"))
|
|
{
|
|
PgControl pgControl =
|
|
{
|
|
.version = PG_VERSION_11,
|
|
.systemId = 0xEFEFEFEFEF,
|
|
.walSegmentSize= 16 * 1024 * 1024,
|
|
.pageChecksum = true
|
|
};
|
|
|
|
TEST_RESULT_STR(
|
|
strPtr(pgControlToLog(&pgControl)),
|
|
"{version: 110000, systemId: 1030522662895, walSegmentSize: 16777216, pageChecksum: true}", "check log");
|
|
}
|
|
|
|
|
|
// *****************************************************************************************************************************
|
|
if (testBegin("pgWalToLog()"))
|
|
{
|
|
PgWal pgWal =
|
|
{
|
|
.version = PG_VERSION_10,
|
|
.systemId = 0xFEFEFEFEFE
|
|
};
|
|
|
|
TEST_RESULT_STR(
|
|
strPtr(pgWalToLog(&pgWal)),
|
|
"{version: 100000, systemId: 1095199817470}", "check log");
|
|
}
|
|
|
|
FUNCTION_HARNESS_RESULT_VOID();
|
|
}
|