From 99dd19c013e27e99d9a561d878e7b81f56a45210 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Tue, 10 Jun 2025 15:11:55 +0200 Subject: [PATCH] virpcimock: Automatically invent fakerootdir, if not provided MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- tests/virpcimock.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index 5b923c63ce..34128d5516 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -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 -- 2.47.3