From e07040c2e4a7fd290fa8c26f4297dc7a5b2b9ab1 Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 12 Mar 2021 12:54:34 -0500 Subject: [PATCH] Add HRN_STORAGE_TIME() harness macro. Makes updating the time of a path/file more streamlined in tests. Also update all tests where utime() was being used directly. --- test/src/common/harnessStorage.c | 20 ++++++++++++++++++++ test/src/common/harnessStorage.h | 8 ++++++++ test/src/module/command/backupTest.c | 8 +------- test/src/module/storage/posixTest.c | 9 ++------- test/src/module/storage/remoteTest.c | 12 +++--------- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/test/src/common/harnessStorage.c b/test/src/common/harnessStorage.c index 3c7322ff4..460b3c667 100644 --- a/test/src/common/harnessStorage.c +++ b/test/src/common/harnessStorage.c @@ -5,6 +5,7 @@ Storage Test Harness #include #include #include +#include #include "common/crypto/cipherBlock.h" #include "common/debug.h" @@ -310,3 +311,22 @@ hrnStoragePutLog(const Storage *storage, const char *file, const Buffer *buffer, return strZ(log); } + +/**********************************************************************************************************************************/ +void +hrnStorageTime(const int line, const Storage *const storage, const char *const path, const time_t modified) +{ + hrnTestLogPrefix(line, true); + hrnTestResultBegin(__func__, line, false); + + const char *const pathFull = strZ(storagePathP(storage, path == NULL ? NULL : STR(path))); + + printf("time '%" PRId64 "' on '%s'\n", (int64_t)modified, pathFull); + fflush(stdout); + + THROW_ON_SYS_ERROR_FMT( + utime(pathFull, &((struct utimbuf){.actime = modified, .modtime = modified})) == -1, FileInfoError, + "unable to set time for '%s'", pathFull); + + hrnTestResultEnd(); +} diff --git a/test/src/common/harnessStorage.h b/test/src/common/harnessStorage.h index 487445820..feac02fbd 100644 --- a/test/src/common/harnessStorage.h +++ b/test/src/common/harnessStorage.h @@ -99,6 +99,14 @@ Remove a file and error if it does not exist TEST_RESULT_VOID(storageRemoveP(storage, STR(path), .errorOnMissing = true), "remove file '%s'", \ strZ(storagePathP(storage, STR(path)))) +/*********************************************************************************************************************************** +Change the time of a path/file +***********************************************************************************************************************************/ +#define HRN_STORAGE_TIME(storage, path, time) \ + hrnStorageTime(__LINE__, storage, path, time) + +void hrnStorageTime(const int line, const Storage *const storage, const char *const path, const time_t modified); + /*********************************************************************************************************************************** Dummy interface for constructing test storage drivers. All required functions are stubbed out so this interface can be copied and specific functions replaced for testing. diff --git a/test/src/module/command/backupTest.c b/test/src/module/command/backupTest.c index eba2156c9..03cfd596c 100644 --- a/test/src/module/command/backupTest.c +++ b/test/src/module/command/backupTest.c @@ -1,8 +1,6 @@ /*********************************************************************************************************************************** Test Backup Command ***********************************************************************************************************************************/ -#include - #include "command/stanza/create.h" #include "command/stanza/upgrade.h" #include "common/crypto/hash.h" @@ -2571,11 +2569,7 @@ testRun(void) harnessCfgLoad(cfgCmdBackup, argList); // Update pg_control timestamp - THROW_ON_SYS_ERROR( - utime( - strZ(storagePathP(storagePg(), STRDEF("global/pg_control"))), - &(struct utimbuf){.actime = backupTimeStart, .modtime = backupTimeStart}) != 0, FileWriteError, - "unable to set time"); + HRN_STORAGE_TIME(storagePg(), "global/pg_control", backupTimeStart); // Run backup. Make sure that the timeline selected converts to hexdecimal that can't be interpreted as decimal. testBackupPqScriptP(PG_VERSION_11, backupTimeStart, .timeline = 0x2C); diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c index c8935f6e0..e49519cec 100644 --- a/test/src/module/storage/posixTest.c +++ b/test/src/module/storage/posixTest.c @@ -1,9 +1,6 @@ /*********************************************************************************************************************************** Test Posix Storage ***********************************************************************************************************************************/ -#include -#include - #include "common/io/io.h" #include "common/time.h" #include "storage/read.h" @@ -204,8 +201,7 @@ testRun(void) "path not enforced"); // ------------------------------------------------------------------------------------------------------------------------- - struct utimbuf utimeTest = {.actime = 1000000000, .modtime = 1555160000}; - THROW_ON_SYS_ERROR_FMT(utime(testPath(), &utimeTest) != 0, FileWriteError, "unable to set time for '%s'", testPath()); + HRN_STORAGE_TIME(storageTest, testPath(), 1555160000); TEST_ASSIGN(info, storageInfoP(storageTest, strNew(testPath())), "get path info"); TEST_RESULT_STR(info.name, NULL, " name is not set"); @@ -224,8 +220,7 @@ testRun(void) const Buffer *buffer = BUFSTRDEF("TESTFILE"); TEST_RESULT_VOID(storagePutP(storageNewWriteP(storageTest, fileName), buffer), "put test file"); - utimeTest.modtime = 1555155555; - THROW_ON_SYS_ERROR_FMT(utime(strZ(fileName), &utimeTest) != 0, FileWriteError, "unable to set time for '%s'", testPath()); + HRN_STORAGE_TIME(storageTest, strZ(fileName), 1555155555); #ifdef TEST_CONTAINER_REQUIRED TEST_RESULT_INT(system(strZ(strNewFmt("sudo chown 99999:99999 %s", strZ(fileName)))), 0, "set invalid user/group"); diff --git a/test/src/module/storage/remoteTest.c b/test/src/module/storage/remoteTest.c index de7e466c7..19282d7f7 100644 --- a/test/src/module/storage/remoteTest.c +++ b/test/src/module/storage/remoteTest.c @@ -1,8 +1,6 @@ /*********************************************************************************************************************************** Test Remote Storage ***********************************************************************************************************************************/ -#include - #include "command/backup/pageChecksum.h" #include "common/crypto/cipherBlock.h" #include "common/io/bufferRead.h" @@ -98,9 +96,7 @@ testRun(void) TEST_TITLE("path info"); storagePathCreateP(storageTest, strNew("repo")); - struct utimbuf utimeTest = {.actime = 1000000000, .modtime = 1555160000}; - THROW_ON_SYS_ERROR( - utime(strZ(storagePathP(storageTest, strNew("repo"))), &utimeTest) != 0, FileWriteError, "unable to set time"); + HRN_STORAGE_TIME(storageTest, "repo", 1555160000); StorageInfo info = {.exists = false}; TEST_ASSIGN(info, storageInfoP(storageRemote, NULL), "valid path"); @@ -278,8 +274,7 @@ testRun(void) storagePutP(storageNewWriteP(storageRemote, strNew("test"), .timeModified = 1555160001), BUFSTRDEF("TESTME")); // Path timestamp must be set after file is created since file creation updates it - struct utimbuf utimeTest = {.actime = 1000000000, .modtime = 1555160000}; - THROW_ON_SYS_ERROR(utime(strZ(storagePathP(storageRemote, NULL)), &utimeTest) != 0, FileWriteError, "unable to set time"); + HRN_STORAGE_TIME(storageRemote, NULL, 1555160000); TEST_RESULT_BOOL( storageInfoListP(storageRemote, NULL, hrnStorageInfoListCallback, &callbackData, .sortOrder = sortOrderAsc), @@ -298,8 +293,7 @@ testRun(void) storageRemoveP(storageRemote, STRDEF("test"), .errorOnMissing = true); // Path timestamp must be set after file is removed since file removal updates it - utimeTest = (struct utimbuf){.actime = 1000000000, .modtime = 1555160000}; - THROW_ON_SYS_ERROR(utime(strZ(storagePathP(storageRemote, NULL)), &utimeTest) != 0, FileWriteError, "unable to set time"); + HRN_STORAGE_TIME(storageRemote, NULL, 1555160000); VariantList *paramList = varLstNew(); varLstAdd(paramList, varNewStrZ(hrnReplaceKey("{[path]}/repo")));