[libvirt PATCH 0/4] tests: Improve fakerootdir handling

I'd be open to squash patches 2 and 3 together, if that's considered preferable by the reviewer. Andrea Bolognani (4): tests: Introduce helpers for fakerootdir handling tests: Adopt fakerootdir helpers tests: Move fakerootdir handling to common logic tests: Print fakerootdir when it's preserved tests/qemuhotplugtest.c | 15 --------------- tests/qemumemlocktest.c | 15 --------------- tests/qemustatusxml2xmltest.c | 15 --------------- tests/qemuxml2argvtest.c | 15 --------------- tests/qemuxml2xmltest.c | 16 ---------------- tests/scsihosttest.c | 14 +++----------- tests/testutils.c | 34 ++++++++++++++++++++++++++++++++++ tests/testutils.h | 3 +++ tests/vircgrouptest.c | 18 ++++-------------- tests/virhostdevtest.c | 16 ---------------- tests/virpcitest.c | 15 --------------- 11 files changed, 44 insertions(+), 132 deletions(-) -- 2.39.2

We have this logic open-coded all over the test suite. Provide proper helpers implementing it. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/testutils.c | 26 ++++++++++++++++++++++++++ tests/testutils.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/tests/testutils.c b/tests/testutils.c index 8fe624f029..fe7677ebfb 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -766,6 +766,32 @@ virTestSetEnvPath(void) return 0; } +#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" + +char* +virTestFakeRootDirInit(void) +{ + g_autofree char *fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); + + if (!g_mkdtemp(fakerootdir)) { + fprintf(stderr, "Cannot create fakerootdir"); + return NULL; + } + + g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + + return g_steal_pointer(&fakerootdir); +} + +void +virTestFakeRootDirCleanup(char *fakerootdir) +{ + g_unsetenv("LIBVIRT_FAKE_ROOT_DIR"); + + if (!g_getenv("LIBVIRT_SKIP_CLEANUP")) + virFileDeleteTree(fakerootdir); +} + int virTestMain(int argc, char **argv, int (*func)(void), diff --git a/tests/testutils.h b/tests/testutils.h index 869b58bd1e..cf8a346dff 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -101,6 +101,9 @@ void virTestQuiesceLibvirtErrors(bool always); void virTestCounterReset(const char *prefix); const char *virTestCounterNext(void); +char *virTestFakeRootDirInit(void); +void virTestFakeRootDirCleanup(char *fakerootdir); + /** * The @func shall return EXIT_FAILURE or EXIT_SUCCESS or * EXIT_AM_SKIP or EXIT_AM_HARDFAIL. -- 2.39.2

Most replacements are completely straightforward but vircgrouptest requires slightly different handling because, instead of initializing a single fakerootdir at the start of the test program and cleaning it up at the end, it creates multiple different ones one after the other. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/qemuhotplugtest.c | 14 +++----------- tests/qemumemlocktest.c | 15 +++------------ tests/qemustatusxml2xmltest.c | 15 +++------------ tests/qemuxml2argvtest.c | 15 +++------------ tests/qemuxml2xmltest.c | 16 +++------------- tests/scsihosttest.c | 13 +++---------- tests/vircgrouptest.c | 18 ++++-------------- tests/virhostdevtest.c | 17 ++++------------- tests/virpcitest.c | 15 +++------------ 9 files changed, 29 insertions(+), 109 deletions(-) diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index 215837fc8f..429928aa7f 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -656,8 +656,6 @@ testQemuHotplugCpuIndividual(const void *opaque) return ret; } -#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) @@ -669,15 +667,10 @@ mymain(void) g_autofree char *fakerootdir = NULL; g_autoptr(virQEMUDriverConfig) cfg = NULL; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); + if (!(fakerootdir = virTestFakeRootDirInit())) { + return EXIT_FAILURE; } - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); - if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -1004,8 +997,7 @@ mymain(void) DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "16-22", true, true, true); DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "17", true, true, true); - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); qemuTestDriverFree(&driver); virObjectUnref(data.vm); diff --git a/tests/qemumemlocktest.c b/tests/qemumemlocktest.c index b303f70e9d..184d8ede19 100644 --- a/tests/qemumemlocktest.c +++ b/tests/qemumemlocktest.c @@ -42,8 +42,6 @@ testCompareMemLock(const void *data) return virTestCompareToULL(info->memlock, qemuDomainGetMemLockLimitBytes(def, false)); } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { @@ -51,14 +49,8 @@ mymain(void) g_autofree char *fakerootdir = NULL; g_autoptr(virQEMUCaps) qemuCaps = NULL; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -137,8 +129,7 @@ mymain(void) DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED); cleanup: - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); qemuTestDriverFree(&driver); diff --git a/tests/qemustatusxml2xmltest.c b/tests/qemustatusxml2xmltest.c index db82a1a980..a29aa723e6 100644 --- a/tests/qemustatusxml2xmltest.c +++ b/tests/qemustatusxml2xmltest.c @@ -70,8 +70,6 @@ testInfoSetStatusPaths(struct testQemuInfo *info) } -#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { @@ -87,14 +85,8 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -142,8 +134,7 @@ mymain(void) DO_TEST_STATUS("backup-pull"); cleanup: - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); qemuTestDriverFree(&driver); diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 5a33c336c8..39d1888427 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -796,8 +796,6 @@ testInfoSetPaths(struct testQemuInfo *info, abs_srcdir, info->name, suffix ? suffix : ""); } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { @@ -813,14 +811,8 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; /* Set the timezone because we are mocking the time() function. * If we don't do that, then localtime() may return unpredictable @@ -2992,8 +2984,7 @@ mymain(void) DO_TEST_CAPS_LATEST("crypto-builtin"); - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); VIR_FREE(driver.config->nbdTLSx509certdir); qemuTestDriverFree(&driver); diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 2d2d800523..3ae2e65a43 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -113,8 +113,6 @@ testInfoSetPaths(struct testQemuInfo *info, } -#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { @@ -131,15 +129,8 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; /* Required for tpm-emulator tests */ @@ -1308,8 +1299,7 @@ mymain(void) DO_TEST_CAPS_LATEST("crypto-builtin"); cleanup: - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); qemuTestDriverFree(&driver); virFileWrapperClearPrefixes(); diff --git a/tests/scsihosttest.c b/tests/scsihosttest.c index 28ad59f39d..37d2b86c0a 100644 --- a/tests/scsihosttest.c +++ b/tests/scsihosttest.c @@ -227,8 +227,6 @@ testVirFindSCSIHostByPCI(const void *data G_GNUC_UNUSED) return ret; } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { @@ -236,12 +234,8 @@ mymain(void) g_autofree char *fakerootdir = NULL; g_autofree char *fakesysfsdir = NULL; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - goto cleanup; - } + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; fakesysfsdir = g_strdup_printf("%s/sys", fakerootdir); @@ -268,8 +262,7 @@ mymain(void) ret = 0; cleanup: - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); VIR_FREE(scsihost_class_path); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c index 848a9c7bb0..0e7645af7a 100644 --- a/tests/vircgrouptest.c +++ b/tests/vircgrouptest.c @@ -956,22 +956,14 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) return 0; } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static char * initFakeFS(const char *mode, const char *filename) { char *fakerootdir; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); + if (!(fakerootdir = virTestFakeRootDirInit())) abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); if (mode) g_setenv("VIR_CGROUP_MOCK_MODE", mode, TRUE); @@ -985,13 +977,11 @@ initFakeFS(const char *mode, static void cleanupFakeFS(char *fakerootdir) { - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); - - VIR_FREE(fakerootdir); - g_unsetenv("LIBVIRT_FAKE_ROOT_DIR"); g_unsetenv("VIR_CGROUP_MOCK_MODE"); g_unsetenv("VIR_CGROUP_MOCK_FILENAME"); + + virTestFakeRootDirCleanup(fakerootdir); + VIR_FREE(fakerootdir); } static int diff --git a/tests/virhostdevtest.c b/tests/virhostdevtest.c index 1aed0d2b6d..c64940458f 100644 --- a/tests/virhostdevtest.c +++ b/tests/virhostdevtest.c @@ -584,22 +584,14 @@ testNVMeDiskRoundtrip(const void *opaque G_GNUC_UNUSED) } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { int ret = 0; g_autofree char *fakerootdir = NULL; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - fprintf(stderr, "Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; # define DO_TEST(fnc) \ do { \ @@ -609,7 +601,7 @@ mymain(void) if (myInit() < 0) { fprintf(stderr, "Init data structures failed."); - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); return EXIT_FAILURE; } @@ -622,8 +614,7 @@ mymain(void) myCleanup(); - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/virpcitest.c b/tests/virpcitest.c index 4c0f0b91c3..5ddf4de0bd 100644 --- a/tests/virpcitest.c +++ b/tests/virpcitest.c @@ -363,22 +363,14 @@ testVirPCIDeviceGetVPD(const void *opaque) return 0; } -# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" - static int mymain(void) { int ret = 0; g_autofree char *fakerootdir = NULL; - fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); - - if (!g_mkdtemp(fakerootdir)) { - VIR_TEST_DEBUG("Cannot create fakerootdir"); - abort(); - } - - g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; # define DO_TEST(fnc) \ do { \ @@ -446,8 +438,7 @@ mymain(void) DO_TEST_PCI(testVirPCIDeviceGetVPD, 0, 0x03, 0, 0); - if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) - virFileDeleteTree(fakerootdir); + virTestFakeRootDirCleanup(fakerootdir); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.39.2

Instead of having each test manually initialize and cleanup its own fakerootdir, do that as part of the common test initialization logic in virTestMain(). In most cases we can simply drop the relevant code from the test program, but scsihosttest uses the value of fakerootdir as a starting point to build another path, so we need to do things slightly differently. In order to keep things working, we retrieve the value from the LIBVIRT_FAKE_ROOT_DIR environment variable, same as all the mock libraries are already doing. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/qemuhotplugtest.c | 7 ------- tests/qemumemlocktest.c | 6 ------ tests/qemustatusxml2xmltest.c | 6 ------ tests/qemuxml2argvtest.c | 6 ------ tests/qemuxml2xmltest.c | 6 ------ tests/scsihosttest.c | 5 ++--- tests/testutils.c | 6 ++++++ tests/virhostdevtest.c | 7 ------- tests/virpcitest.c | 6 ------ 9 files changed, 8 insertions(+), 47 deletions(-) diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index 429928aa7f..2e8ac05e5e 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -664,13 +664,8 @@ mymain(void) int ret = 0; struct qemuHotplugTestData data = {0}; struct testQemuHotplugCpuParams cpudata; - g_autofree char *fakerootdir = NULL; g_autoptr(virQEMUDriverConfig) cfg = NULL; - if (!(fakerootdir = virTestFakeRootDirInit())) { - return EXIT_FAILURE; - } - if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -997,8 +992,6 @@ mymain(void) DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "16-22", true, true, true); DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "17", true, true, true); - virTestFakeRootDirCleanup(fakerootdir); - qemuTestDriverFree(&driver); virObjectUnref(data.vm); return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/qemumemlocktest.c b/tests/qemumemlocktest.c index 184d8ede19..a03ed20a45 100644 --- a/tests/qemumemlocktest.c +++ b/tests/qemumemlocktest.c @@ -46,12 +46,8 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; g_autoptr(virQEMUCaps) qemuCaps = NULL; - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; - if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -129,8 +125,6 @@ mymain(void) DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED); cleanup: - virTestFakeRootDirCleanup(fakerootdir); - qemuTestDriverFree(&driver); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/qemustatusxml2xmltest.c b/tests/qemustatusxml2xmltest.c index a29aa723e6..7587c4c34f 100644 --- a/tests/qemustatusxml2xmltest.c +++ b/tests/qemustatusxml2xmltest.c @@ -74,7 +74,6 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; g_autoptr(GHashTable) capslatest = testQemuGetLatestCaps(); g_autoptr(GHashTable) capscache = virHashNew(virObjectUnref); g_autoptr(virConnect) conn = NULL; @@ -85,9 +84,6 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; - if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE; @@ -134,8 +130,6 @@ mymain(void) DO_TEST_STATUS("backup-pull"); cleanup: - virTestFakeRootDirCleanup(fakerootdir); - qemuTestDriverFree(&driver); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 39d1888427..ad4d6aa4d2 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -800,7 +800,6 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; g_autoptr(GHashTable) capslatest = testQemuGetLatestCaps(); g_autoptr(GHashTable) qapiSchemaCache = virHashNew((GDestroyNotify) g_hash_table_unref); g_autoptr(GHashTable) capscache = virHashNew(virObjectUnref); @@ -811,9 +810,6 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; - /* Set the timezone because we are mocking the time() function. * If we don't do that, then localtime() may return unpredictable * results. In order to detect things that just work by a blind @@ -2984,8 +2980,6 @@ mymain(void) DO_TEST_CAPS_LATEST("crypto-builtin"); - virTestFakeRootDirCleanup(fakerootdir); - VIR_FREE(driver.config->nbdTLSx509certdir); qemuTestDriverFree(&driver); virFileWrapperClearPrefixes(); diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 3ae2e65a43..88e152b65c 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -117,7 +117,6 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; g_autoptr(virQEMUDriverConfig) cfg = NULL; g_autoptr(GHashTable) capslatest = testQemuGetLatestCaps(); g_autoptr(GHashTable) capscache = virHashNew(virObjectUnref); @@ -129,9 +128,6 @@ mymain(void) if (!capslatest) return EXIT_FAILURE; - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; - /* Required for tpm-emulator tests */ virFileWrapperAddPrefix(SYSCONFDIR "/qemu/firmware", @@ -1299,8 +1295,6 @@ mymain(void) DO_TEST_CAPS_LATEST("crypto-builtin"); cleanup: - virTestFakeRootDirCleanup(fakerootdir); - qemuTestDriverFree(&driver); virFileWrapperClearPrefixes(); diff --git a/tests/scsihosttest.c b/tests/scsihosttest.c index 37d2b86c0a..7f2dcb68a2 100644 --- a/tests/scsihosttest.c +++ b/tests/scsihosttest.c @@ -231,10 +231,10 @@ static int mymain(void) { int ret = -1; - g_autofree char *fakerootdir = NULL; + const char *fakerootdir = NULL; g_autofree char *fakesysfsdir = NULL; - if (!(fakerootdir = virTestFakeRootDirInit())) + if (!(fakerootdir = g_getenv("LIBVIRT_FAKE_ROOT_DIR"))) return EXIT_FAILURE; fakesysfsdir = g_strdup_printf("%s/sys", fakerootdir); @@ -262,7 +262,6 @@ mymain(void) ret = 0; cleanup: - virTestFakeRootDirCleanup(fakerootdir); VIR_FREE(scsihost_class_path); return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/testutils.c b/tests/testutils.c index fe7677ebfb..e717895fbf 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -808,6 +808,7 @@ int virTestMain(int argc, g_autofree const char **preloads = NULL; size_t npreloads = 0; g_autofree char *mock = NULL; + g_autofree char *fakerootdir = NULL; if (getenv("VIR_TEST_FILE_ACCESS")) { preloads = g_renew(const char *, preloads, npreloads + 2); @@ -888,6 +889,9 @@ int virTestMain(int argc, failedTests = virBitmapNew(1); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; + ret = (func)(); virResetLastError(); @@ -897,6 +901,8 @@ int virTestMain(int argc, fprintf(stderr, " %-3zu %s\n", testCounter, ret == 0 ? "OK" : "FAIL"); } + virTestFakeRootDirCleanup(fakerootdir); + switch (ret) { case EXIT_FAILURE: case EXIT_SUCCESS: diff --git a/tests/virhostdevtest.c b/tests/virhostdevtest.c index c64940458f..ee0d1c1e6b 100644 --- a/tests/virhostdevtest.c +++ b/tests/virhostdevtest.c @@ -588,10 +588,6 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; - - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; # define DO_TEST(fnc) \ do { \ @@ -601,7 +597,6 @@ mymain(void) if (myInit() < 0) { fprintf(stderr, "Init data structures failed."); - virTestFakeRootDirCleanup(fakerootdir); return EXIT_FAILURE; } @@ -614,8 +609,6 @@ mymain(void) myCleanup(); - virTestFakeRootDirCleanup(fakerootdir); - return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/tests/virpcitest.c b/tests/virpcitest.c index 5ddf4de0bd..769175d7c4 100644 --- a/tests/virpcitest.c +++ b/tests/virpcitest.c @@ -367,10 +367,6 @@ static int mymain(void) { int ret = 0; - g_autofree char *fakerootdir = NULL; - - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; # define DO_TEST(fnc) \ do { \ @@ -438,8 +434,6 @@ mymain(void) DO_TEST_PCI(testVirPCIDeviceGetVPD, 0, 0x03, 0, 0); - virTestFakeRootDirCleanup(fakerootdir); - return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.39.2

Setting the LIBVIRT_SKIP_CLEANUP environment variable results in the contents of fakerootdir being preserved for inspection. Be more helpful towards the developer and print out the path in this case. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/testutils.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testutils.c b/tests/testutils.c index e717895fbf..e8cb8e6737 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -790,6 +790,8 @@ virTestFakeRootDirCleanup(char *fakerootdir) if (!g_getenv("LIBVIRT_SKIP_CLEANUP")) virFileDeleteTree(fakerootdir); + else + fprintf(stderr, "Test data ready for inspection: %s\n", fakerootdir); } int virTestMain(int argc, -- 2.39.2

On 3/1/23 11:54 AM, Andrea Bolognani wrote:
I'd be open to squash patches 2 and 3 together, if that's considered preferable by the reviewer.
Andrea Bolognani (4): tests: Introduce helpers for fakerootdir handling tests: Adopt fakerootdir helpers tests: Move fakerootdir handling to common logic tests: Print fakerootdir when it's preserved
Reviewed-by: Laine Stump <laine@redhat.com>
tests/qemuhotplugtest.c | 15 --------------- tests/qemumemlocktest.c | 15 --------------- tests/qemustatusxml2xmltest.c | 15 --------------- tests/qemuxml2argvtest.c | 15 --------------- tests/qemuxml2xmltest.c | 16 ---------------- tests/scsihosttest.c | 14 +++----------- tests/testutils.c | 34 ++++++++++++++++++++++++++++++++++ tests/testutils.h | 3 +++ tests/vircgrouptest.c | 18 ++++-------------- tests/virhostdevtest.c | 16 ---------------- tests/virpcitest.c | 15 --------------- 11 files changed, 44 insertions(+), 132 deletions(-)
participants (2)
-
Andrea Bolognani
-
Laine Stump