Blob Blame History Raw
From e3bed841655177bf41998f0ebfa4afed0a7821cf Mon Sep 17 00:00:00 2001
Message-Id: <e3bed841655177bf41998f0ebfa4afed0a7821cf.1386348946.git.jdenemar@redhat.com>
From: Eric Blake <eblake@redhat.com>
Date: Mon, 2 Dec 2013 13:39:08 +0000
Subject: [PATCH] tests: guarantee abs_srcdir in all C tests

For

  https://bugzilla.redhat.com/show_bug.cgi?id=1035403

While trying to debug a failure of virpcitest during 'make distcheck',
I noticed that with a VPATH build, 'cd tests; ./virpcitest' fails for
an entirely different reason.  To reproduce the distcheck failure, I
had to run 'cd tests; abs_srcdir=/path/to/src ./virpcitest'.  But we
document in HACKING that all of our tests are supposed to be runnable
without requiring extra environment variables.

The solution: hardcode the location of srcdir into the just-built
binaries, rather than requiring make to prepopulate environment
variables.  With this, './virpcitest' passes even in a VPATH build
(provided that $(srcdir) is writable; a followup patch will fix the
conditions required by 'make distcheck').  [Note: the makefile must
still pass on directory variables to the test environment of shell
scripts, since those aren't compiled.  So while this solves the case
of a compiled test, it still requires environment variables to pass
a VPATH build of any shell script test case that relies on srcdir.]

* tests/Makefile.am (AM_CFLAGS): Define abs_srcdir in all compiled
tests.
* tests/testutils.h (abs_srcdir): Quit declaring.
* tests/testutils.c (virtTestMain): Rely on define rather than
environment variable.
* tests/virpcimock.c (pci_device_new_from_stub): Rely on define.
* tests/cputest.c (mymain): Adjust abs_top_srcdir default.
* tests/qemuxml2argvtest.c (mymain): Likewise.
* tests/qemuxmlnstest.c (mymain): Likewise.

Signed-off-by: Eric Blake <eblake@redhat.com>
(cherry picked from commit 0aa873d74118ea589f61fe46396335edeb1d955f)

Conflicts:
	tests/virpcimock.c
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 tests/Makefile.am        |  1 +
 tests/cputest.c          |  2 +-
 tests/qemuxml2argvtest.c |  2 +-
 tests/qemuxmlnstest.c    |  2 +-
 tests/testutils.c        | 11 +----------
 tests/testutils.h        |  6 +++++-
 6 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index d095595..3d5ac72 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,6 +29,7 @@ INCLUDES = \
 
 AM_CFLAGS = \
 	-Dabs_builddir="\"`pwd`\"" \
+	-Dabs_srcdir="\"`cd '$(srcdir)'; pwd`\"" \
 	$(LIBXML_CFLAGS) \
 	$(GNUTLS_CFLAGS) \
 	$(SASL_CFLAGS) \
diff --git a/tests/cputest.c b/tests/cputest.c
index 2e5f0cd..6a1cfeb 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -501,7 +501,7 @@ mymain(void)
 
     abs_top_srcdir = getenv("abs_top_srcdir");
     if (!abs_top_srcdir)
-        abs_top_srcdir = "..";
+        abs_top_srcdir = abs_srcdir "/..";
 
     if (virAsprintf(&map, "%s/src/cpu/cpu_map.xml", abs_top_srcdir) < 0 ||
         cpuMapOverride(map) < 0) {
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index cfaee71..eb98966 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -297,7 +297,7 @@ mymain(void)
 
     abs_top_srcdir = getenv("abs_top_srcdir");
     if (!abs_top_srcdir)
-        abs_top_srcdir = "..";
+        abs_top_srcdir = abs_srcdir "/..";
 
     driver.config = virQEMUDriverConfigNew(false);
     VIR_FREE(driver.config->spiceListen);
diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c
index 9f4f442..13dbf84 100644
--- a/tests/qemuxmlnstest.c
+++ b/tests/qemuxmlnstest.c
@@ -203,7 +203,7 @@ mymain(void)
 
     abs_top_srcdir = getenv("abs_top_srcdir");
     if (!abs_top_srcdir)
-        abs_top_srcdir = "..";
+        abs_top_srcdir = abs_srcdir "/..";
 
     driver.config = virQEMUDriverConfigNew(false);
     if ((driver.caps = testQemuCapsInit()) == NULL)
diff --git a/tests/testutils.c b/tests/testutils.c
index 72aa5b3..964da41 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -73,7 +73,6 @@ static size_t testStart = 0;
 static size_t testEnd = 0;
 
 char *progname;
-char *abs_srcdir;
 
 double
 virtTestCountAverage(double *items, int nitems)
@@ -586,7 +585,6 @@ int virtTestMain(int argc,
                  int (*func)(void))
 {
     int ret;
-    bool abs_srcdir_cleanup = false;
     char *testRange = NULL;
 #if TEST_OOM
     int approxAlloc = 0;
@@ -598,12 +596,7 @@ int virtTestMain(int argc,
     int worker = 0;
 #endif
 
-    abs_srcdir = getenv("abs_srcdir");
-    if (!abs_srcdir) {
-        abs_srcdir = getcwd(NULL, 0);
-        abs_srcdir_cleanup = true;
-    }
-    if (!abs_srcdir)
+    if (!virFileExists(abs_srcdir))
         return EXIT_AM_HARDFAIL;
 
     progname = last_component(argv[0]);
@@ -764,8 +757,6 @@ cleanup:
     ret = (func)();
 #endif
 
-    if (abs_srcdir_cleanup)
-        VIR_FREE(abs_srcdir);
     virResetLastError();
     if (!virTestGetVerbose() && ret != EXIT_AM_SKIP) {
         if (testCounter == 0 || testCounter % 40)
diff --git a/tests/testutils.h b/tests/testutils.h
index 27af5da..19efc5c 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -38,7 +38,11 @@
 # endif
 
 extern char *progname;
-extern char *abs_srcdir;
+
+/* Makefile.am provides these two definitions */
+# if !defined(abs_srcdir) || !defined(abs_builddir)
+#  error Fix Makefile.am
+# endif
 
 double virtTestCountAverage(double *items,
                             int nitems);
-- 
1.8.4.5