]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virpcimock: Automatically invent fakerootdir, if not provided
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 10 Jun 2025 13:11:55 +0000 (15:11 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 2 Jul 2025 11:53:56 +0000 (13:53 +0200)
Currently, all users of virpcimock do set LIBVIRT_FAKE_ROOT_DIR
envvar. But soon, virt-aa-helper will be run with it and
basically right at the beginning of its main() it clears whole
environment. So even if the envvar is provided the mock won't see
that.

Anyway, the solution is to just create a tempdir and then 'rm
-rf' it in the desctructor.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/virpcimock.c

index 5b923c63cef655fe3f4ee9c0ef245bfa0d190faa..34128d5516bcf5e3cb8e4d077ed9645b4f01ea28 100644 (file)
@@ -44,6 +44,7 @@ static DIR * (*real_opendir)(const char *name);
 static char *(*real_virFileCanonicalizePath)(const char *path);
 
 static char *fakerootdir;
+static bool fakerootClean;
 
 /* To add a new mocked prefix in virpcimock:
  * - add the prefix here as a define to make it easier to track what we
@@ -976,8 +977,16 @@ init_env(void)
         .vpd_len = G_N_ELEMENTS(fullVPDExampleData),
     };
 
-    if (!(fakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR")))
-        ABORT("Missing LIBVIRT_FAKE_ROOT_DIR env variable\n");
+    if (!(fakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR"))) {
+        GError *err = NULL;
+
+        fakerootdir = g_dir_make_tmp(NULL, &err);
+        if (err != NULL) {
+            ABORT("Unable to create a temporary dir: %s\n", err->message);
+        }
+
+        fakerootClean = true;
+    }
 
     tmp = g_strdup_printf("%s%s", fakerootdir, SYSFS_PCI_PREFIX);
 
@@ -1046,6 +1055,18 @@ init_env(void)
 }
 
 
+static void __attribute__((destructor))
+deinit_env(void)
+{
+    if (!fakerootClean)
+        return;
+
+    virFileDeleteTree(fakerootdir);
+    g_clear_pointer(&fakerootdir, g_free);
+    fakerootClean = false;
+}
+
+
 /*
  *
  * Mocked functions