]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: Set PATH in each test
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 17 Mar 2016 12:30:42 +0000 (13:30 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 18 Mar 2016 10:38:37 +0000 (11:38 +0100)
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>
tests/Makefile.am
tests/testutils.c

index 90981dcf312ca06ddd42402c7d5f9cc19f8c8193..74f7f5a73ee69271ba92c5157ed31eeca37f1cef 100644 (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                          \
index 88c4d4b253a7870548ea6c9f03ea64a9fdaaa0be..2df4250bb6f5561761f1e3f03349e297535db630 100644 (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;