From: Michal Privoznik Date: Thu, 17 Mar 2016 12:30:42 +0000 (+0100) Subject: tests: Set PATH in each test X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=363b401f9473dee21c152866fc158d231ddcb6dd;p=libvirt.git tests: Set PATH in each test Currently we spawn couple of binaries in our test suite. Moreover, we provide some spoofed versions of system binaries hoping that those will be executed instead of the system ones. For instance, for testing SSH socket we have written our own ssh binary for producing predictable results. We certainly don't want to execute the system ssh binary. However, in order to prefer our binaries over system ones, we need to set PATH environment variable. But this is done only at the Makefile level. So if anybody runs a test by hand that expects our spoofed binary, the test ends up executing real system binaries. This is not good. In fact, it's terribly wrong. The fix lies in a small trick - putting our build directory at the beginning of the PATH environment variable in each test. Hopefully, since every test has this VIRT_TEST_MAIN* wrapper, we can fix this at a single place. Moreover, while this removes setting PATH for our tests written in bash, it's safe as we are not calling anything ours that would require PATH change there. Signed-off-by: Michal Privoznik --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 90981dcf31..74f7f5a73e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -462,8 +462,6 @@ TESTS = $(test_programs) \ # intermediate shell variable, but must do all the expansion in make lv_abs_top_builddir=$(shell cd '$(top_builddir)' && pwd) -path_add = $(subst :,$(PATH_SEPARATOR),\ - $(subst !,$(lv_abs_top_builddir)/,!daemon:!tools:!tests)) VIR_TEST_EXPENSIVE ?= $(VIR_TEST_EXPENSIVE_DEFAULT) TESTS_ENVIRONMENT = \ @@ -472,7 +470,6 @@ TESTS_ENVIRONMENT = \ abs_builddir=$(abs_builddir) \ abs_srcdir=$(abs_srcdir) \ CONFIG_HEADER="$(lv_abs_top_builddir)/config.h" \ - PATH="$(path_add)$(PATH_SEPARATOR)$$PATH" \ SHELL="$(SHELL)" \ LIBVIRT_DRIVER_DIR="$(lv_abs_top_builddir)/src/.libs" \ LIBVIRT_AUTOSTART=0 \ diff --git a/tests/testutils.c b/tests/testutils.c index 88c4d4b253..2df4250bb6 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -806,6 +806,24 @@ virTestGetRegenerate(void) return testRegenerate; } +static int +virTestSetEnvPath(void) +{ + int ret = -1; + const char *path = getenv("PATH"); + char *new_path = NULL; + + if (strstr(path, abs_builddir) != path && + (virAsprintf(&new_path, "%s:%s", abs_builddir, path) < 0 || + setenv("PATH", new_path, 1) < 0)) + goto cleanup; + + ret = 0; + cleanup: + VIR_FREE(new_path); + return ret; +} + int virtTestMain(int argc, char **argv, int (*func)(void)) @@ -818,6 +836,9 @@ int virtTestMain(int argc, virFileActivateDirOverride(argv[0]); + if (virTestSetEnvPath() < 0) + return EXIT_AM_HARDFAIL; + if (!virFileExists(abs_srcdir)) return EXIT_AM_HARDFAIL;