[libvirt PATCH 1/4] tests: Introduce helpers for fakerootdir handling

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


We have this logic open-coded all over the test suite. Provide
proper helpers implementing it.

Signed-off-by: Andrea Bolognani <abologna at 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



More information about the libvir-list mailing list