]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: Introduce helpers for fakerootdir handling
authorAndrea Bolognani <abologna@redhat.com>
Wed, 1 Mar 2023 09:39:44 +0000 (10:39 +0100)
committerAndrea Bolognani <abologna@redhat.com>
Fri, 3 Mar 2023 12:52:37 +0000 (13:52 +0100)
We have this logic open-coded all over the test suite. Provide
proper helpers implementing it.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
tests/testutils.c
tests/testutils.h

index 8fe624f029d72f49a7e54bf232909f52b208c77a..fe7677ebfb128dcd18b7dee8a4b3a475f05cfaea 100644 (file)
@@ -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),
index 869b58bd1e5c1dbb75c7008ce8081ce9daefcd88..cf8a346dffcc37d1e0cbc2f735f7526901154cfb 100644 (file)
@@ -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.