tests: Move fakerootdir handling to common logic

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>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Andrea Bolognani 2023-03-01 11:06:14 +01:00
parent 457a0183d6
commit 87b30e6965
9 changed files with 8 additions and 47 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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
@ -2987,8 +2983,6 @@ mymain(void)
DO_TEST_CAPS_LATEST("crypto-builtin");
virTestFakeRootDirCleanup(fakerootdir);
VIR_FREE(driver.config->nbdTLSx509certdir);
qemuTestDriverFree(&driver);
virFileWrapperClearPrefixes();

View File

@ -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",
@ -1325,8 +1321,6 @@ mymain(void)
DO_TEST_CAPS_LATEST("crypto-builtin");
cleanup:
virTestFakeRootDirCleanup(fakerootdir);
qemuTestDriverFree(&driver);
virFileWrapperClearPrefixes();

View File

@ -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;
}

View File

@ -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:

View File

@ -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;
}

View File

@ -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;
}