tests: Adopt fakerootdir helpers

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>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Andrea Bolognani 2023-03-01 10:52:14 +01:00
parent ef3b22e589
commit 457a0183d6
9 changed files with 29 additions and 109 deletions

View File

@ -656,8 +656,6 @@ testQemuHotplugCpuIndividual(const void *opaque)
return ret; return ret;
} }
#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
@ -669,15 +667,10 @@ mymain(void)
g_autofree char *fakerootdir = NULL; g_autofree char *fakerootdir = NULL;
g_autoptr(virQEMUDriverConfig) cfg = NULL; g_autoptr(virQEMUDriverConfig) cfg = NULL;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit())) {
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
} }
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
if (qemuTestDriverInit(&driver) < 0) if (qemuTestDriverInit(&driver) < 0)
return EXIT_FAILURE; 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", "16-22", true, true, true);
DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "17", true, true, true); DO_TEST_CPU_INDIVIDUAL("ppc64-modern-individual", "17", true, true, true);
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
qemuTestDriverFree(&driver); qemuTestDriverFree(&driver);
virObjectUnref(data.vm); virObjectUnref(data.vm);

View File

@ -42,8 +42,6 @@ testCompareMemLock(const void *data)
return virTestCompareToULL(info->memlock, qemuDomainGetMemLockLimitBytes(def, false)); return virTestCompareToULL(info->memlock, qemuDomainGetMemLockLimitBytes(def, false));
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
@ -51,14 +49,8 @@ mymain(void)
g_autofree char *fakerootdir = NULL; g_autofree char *fakerootdir = NULL;
g_autoptr(virQEMUCaps) qemuCaps = NULL; g_autoptr(virQEMUCaps) qemuCaps = NULL;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
if (qemuTestDriverInit(&driver) < 0) if (qemuTestDriverInit(&driver) < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
@ -137,8 +129,7 @@ mymain(void)
DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED); DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED);
cleanup: cleanup:
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
qemuTestDriverFree(&driver); qemuTestDriverFree(&driver);

View File

@ -70,8 +70,6 @@ testInfoSetStatusPaths(struct testQemuInfo *info)
} }
#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
@ -87,14 +85,8 @@ mymain(void)
if (!capslatest) if (!capslatest)
return EXIT_FAILURE; return EXIT_FAILURE;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
if (qemuTestDriverInit(&driver) < 0) if (qemuTestDriverInit(&driver) < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
@ -142,8 +134,7 @@ mymain(void)
DO_TEST_STATUS("backup-pull"); DO_TEST_STATUS("backup-pull");
cleanup: cleanup:
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
qemuTestDriverFree(&driver); qemuTestDriverFree(&driver);

View File

@ -796,8 +796,6 @@ testInfoSetPaths(struct testQemuInfo *info,
abs_srcdir, info->name, suffix ? suffix : ""); abs_srcdir, info->name, suffix ? suffix : "");
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
@ -813,14 +811,8 @@ mymain(void)
if (!capslatest) if (!capslatest)
return EXIT_FAILURE; return EXIT_FAILURE;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
/* Set the timezone because we are mocking the time() function. /* Set the timezone because we are mocking the time() function.
* If we don't do that, then localtime() may return unpredictable * If we don't do that, then localtime() may return unpredictable
@ -2995,8 +2987,7 @@ mymain(void)
DO_TEST_CAPS_LATEST("crypto-builtin"); DO_TEST_CAPS_LATEST("crypto-builtin");
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
VIR_FREE(driver.config->nbdTLSx509certdir); VIR_FREE(driver.config->nbdTLSx509certdir);
qemuTestDriverFree(&driver); qemuTestDriverFree(&driver);

View File

@ -113,8 +113,6 @@ testInfoSetPaths(struct testQemuInfo *info,
} }
#define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
@ -131,15 +129,8 @@ mymain(void)
if (!capslatest) if (!capslatest)
return EXIT_FAILURE; return EXIT_FAILURE;
if (!(fakerootdir = virTestFakeRootDirInit()))
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
/* Required for tpm-emulator tests /* Required for tpm-emulator tests
*/ */
@ -1334,8 +1325,7 @@ mymain(void)
DO_TEST_CAPS_LATEST("crypto-builtin"); DO_TEST_CAPS_LATEST("crypto-builtin");
cleanup: cleanup:
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
qemuTestDriverFree(&driver); qemuTestDriverFree(&driver);
virFileWrapperClearPrefixes(); virFileWrapperClearPrefixes();

View File

@ -227,8 +227,6 @@ testVirFindSCSIHostByPCI(const void *data G_GNUC_UNUSED)
return ret; return ret;
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
@ -236,12 +234,8 @@ mymain(void)
g_autofree char *fakerootdir = NULL; g_autofree char *fakerootdir = NULL;
g_autofree char *fakesysfsdir = NULL; g_autofree char *fakesysfsdir = NULL;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
goto cleanup;
}
fakesysfsdir = g_strdup_printf("%s/sys", fakerootdir); fakesysfsdir = g_strdup_printf("%s/sys", fakerootdir);
@ -268,8 +262,7 @@ mymain(void)
ret = 0; ret = 0;
cleanup: cleanup:
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
VIR_FREE(scsihost_class_path); VIR_FREE(scsihost_class_path);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }

View File

@ -956,22 +956,14 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED)
return 0; return 0;
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static char * static char *
initFakeFS(const char *mode, initFakeFS(const char *mode,
const char *filename) const char *filename)
{ {
char *fakerootdir; char *fakerootdir;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort(); abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
if (mode) if (mode)
g_setenv("VIR_CGROUP_MOCK_MODE", mode, TRUE); g_setenv("VIR_CGROUP_MOCK_MODE", mode, TRUE);
@ -985,13 +977,11 @@ initFakeFS(const char *mode,
static void static void
cleanupFakeFS(char *fakerootdir) 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_MODE");
g_unsetenv("VIR_CGROUP_MOCK_FILENAME"); g_unsetenv("VIR_CGROUP_MOCK_FILENAME");
virTestFakeRootDirCleanup(fakerootdir);
VIR_FREE(fakerootdir);
} }
static int static int

View File

@ -584,22 +584,14 @@ testNVMeDiskRoundtrip(const void *opaque G_GNUC_UNUSED)
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
int ret = 0; int ret = 0;
g_autofree char *fakerootdir = NULL; g_autofree char *fakerootdir = NULL;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
fprintf(stderr, "Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
# define DO_TEST(fnc) \ # define DO_TEST(fnc) \
do { \ do { \
@ -609,7 +601,7 @@ mymain(void)
if (myInit() < 0) { if (myInit() < 0) {
fprintf(stderr, "Init data structures failed."); fprintf(stderr, "Init data structures failed.");
virFileDeleteTree(fakerootdir); virTestFakeRootDirCleanup(fakerootdir);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -622,8 +614,7 @@ mymain(void)
myCleanup(); myCleanup();
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }

View File

@ -363,22 +363,14 @@ testVirPCIDeviceGetVPD(const void *opaque)
return 0; return 0;
} }
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
static int static int
mymain(void) mymain(void)
{ {
int ret = 0; int ret = 0;
g_autofree char *fakerootdir = NULL; g_autofree char *fakerootdir = NULL;
fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE); if (!(fakerootdir = virTestFakeRootDirInit()))
return EXIT_FAILURE;
if (!g_mkdtemp(fakerootdir)) {
VIR_TEST_DEBUG("Cannot create fakerootdir");
abort();
}
g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
# define DO_TEST(fnc) \ # define DO_TEST(fnc) \
do { \ do { \
@ -446,8 +438,7 @@ mymain(void)
DO_TEST_PCI(testVirPCIDeviceGetVPD, 0, 0x03, 0, 0); DO_TEST_PCI(testVirPCIDeviceGetVPD, 0, 0x03, 0, 0);
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL) virTestFakeRootDirCleanup(fakerootdir);
virFileDeleteTree(fakerootdir);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }