From: Laine Stump <laine@redhat.com> A long time ago we added some lines to "poison" the environment of test programs (specifically $HOME and $XDG_*) with nonexisting unusable paths so that any test program attempting to use the normal settings of those variables (which point into the filesystem of the system running the test) would fail (rather than silently messing up the test system). At some later time, someone wrote tests for hostdev devices that required that virGetUserRuntimeDirectory() (which normally uses either $XDG_RUNTIME_DIR or $HOME) return a directory that could actually be used as a part of the test; this was solved by mocking virGetUserRuntimeDirectory() to return a path underneath $LIBVIRT_FAKE_ROOT_DIR (which is created each time a test starts). Much much later, I wanted to add validation of the directory returned by virGetUserRuntimeDirectory(), but when this validation was added, the poisoned values that had been set (back in paragraph one "a long time ago") caused this validation to fail. My first attempt to fix this was to make the mocked virGetUserRuntimeDirectory() more generally available, and turn it on for all the tests that failed. But then I realized that a better solution would be to instead "nourish" (rather than "poison" - get it?) $HOME and $XDG_* with directories created under $LIBVIRT_FAKE_ROOT_DIR. This way we are actually testing the real virGetUserRuntimeDirectory() and any future validation, and also make some other tests cover more actual code in the future. In this patch the poisoning of the environment is removed, the call to the function creating the fake root dir is moved up to that location, and as a part of creating the fake root dir, we also set the aforementioned environment variables and create the directories associated with them (since the tests assume that they already exist). The now-redundant original mock of virGetUserRuntimeDirectory() will be removed in another patch. Signed-off-by: Laine Stump <laine@redhat.com> --- tests/testutils.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/tests/testutils.c b/tests/testutils.c index 814391b804..c984f66d0e 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -779,6 +779,20 @@ virTestSetEnvPath(void) #define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX" +static int +virTestFakeEnvSubDirInit(const char *envName, const char *fakeRootName, const char *fakeSubDirName) +{ + g_autofree char *envVal = g_build_filename(fakeRootName, fakeSubDirName, NULL); + + if (g_mkdir(envVal, 0777) < 0) { + fprintf(stderr, "Cannot create fake %s directory at %s", envName, envVal); + return -1; + } + + g_setenv(envName, envVal, TRUE); + return 0; +} + char* virTestFakeRootDirInit(void) { @@ -791,6 +805,22 @@ virTestFakeRootDirInit(void) g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE); + /* the glib g_get_user_*_dir() functions use these environment variables to + * determine locations for various data/log/config files. Setting them here + * will assure that code under test that is using those directories won't pollute + * the system under test. + */ + if (virTestFakeEnvSubDirInit("HOME", fakerootdir, "home") < 0) + return NULL; + if (virTestFakeEnvSubDirInit("XDG_RUNTIME_DIR", fakerootdir, "user-runtime-dir") < 0) + return NULL; + if (virTestFakeEnvSubDirInit("XDG_DATA_HOME", fakerootdir, "user-data-home") < 0) + return NULL; + if (virTestFakeEnvSubDirInit("XDG_CACHE_HOME", fakerootdir, "user-cache-home") < 0) + return NULL; + if (virTestFakeEnvSubDirInit("XDG_CONFIG_HOME", fakerootdir, "user-config-home") < 0) + return NULL; + return g_steal_pointer(&fakerootdir); } @@ -829,11 +859,8 @@ int virTestMain(int argc, preloads[npreloads] = NULL; } - g_setenv("HOME", "/bad-test-used-env-home", TRUE); - g_setenv("XDG_RUNTIME_DIR", "/bad-test-used-env-xdg-runtime-dir", TRUE); - g_setenv("XDG_DATA_HOME", "/bad-test-used-env-xdg-data-home", TRUE); - g_setenv("XDG_CACHE_HOME", "/bad-test-used-env-xdg-cache-home", TRUE); - g_setenv("XDG_CONFIG_HOME", "/bad-test-used-env-xdg-config-home", TRUE); + if (!(fakerootdir = virTestFakeRootDirInit())) + return EXIT_FAILURE; va_start(ap, func); while ((lib = va_arg(ap, const char *))) { @@ -907,9 +934,6 @@ int virTestMain(int argc, failedTests = virBitmapNew(1); - if (!(fakerootdir = virTestFakeRootDirInit())) - return EXIT_FAILURE; - ret = (func)(); virResetLastError(); -- 2.52.0