[libvirt PATCH 3/4] tests: Move fakerootdir handling to common logic

Andrea Bolognani abologna at redhat.com
Wed Mar 1 16:54:45 UTC 2023


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 at 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



More information about the libvir-list mailing list