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 <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2016-03-17 13:30:42 +01:00
parent 865764de06
commit 363b401f94
2 changed files with 21 additions and 3 deletions

View File

@ -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 \

View File

@ -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;