tests: Introduce helpers for fakerootdir handling

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>
This commit is contained in:
Andrea Bolognani 2023-03-01 10:39:44 +01:00
parent 4b2799fdaa
commit ef3b22e589
2 changed files with 29 additions and 0 deletions

View 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),

View 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.