c401cc
From e3bed841655177bf41998f0ebfa4afed0a7821cf Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <e3bed841655177bf41998f0ebfa4afed0a7821cf.1386348946.git.jdenemar@redhat.com>
c401cc
From: Eric Blake <eblake@redhat.com>
c401cc
Date: Mon, 2 Dec 2013 13:39:08 +0000
c401cc
Subject: [PATCH] tests: guarantee abs_srcdir in all C tests
c401cc
c401cc
For
c401cc
c401cc
  https://bugzilla.redhat.com/show_bug.cgi?id=1035403
c401cc
c401cc
While trying to debug a failure of virpcitest during 'make distcheck',
c401cc
I noticed that with a VPATH build, 'cd tests; ./virpcitest' fails for
c401cc
an entirely different reason.  To reproduce the distcheck failure, I
c401cc
had to run 'cd tests; abs_srcdir=/path/to/src ./virpcitest'.  But we
c401cc
document in HACKING that all of our tests are supposed to be runnable
c401cc
without requiring extra environment variables.
c401cc
c401cc
The solution: hardcode the location of srcdir into the just-built
c401cc
binaries, rather than requiring make to prepopulate environment
c401cc
variables.  With this, './virpcitest' passes even in a VPATH build
c401cc
(provided that $(srcdir) is writable; a followup patch will fix the
c401cc
conditions required by 'make distcheck').  [Note: the makefile must
c401cc
still pass on directory variables to the test environment of shell
c401cc
scripts, since those aren't compiled.  So while this solves the case
c401cc
of a compiled test, it still requires environment variables to pass
c401cc
a VPATH build of any shell script test case that relies on srcdir.]
c401cc
c401cc
* tests/Makefile.am (AM_CFLAGS): Define abs_srcdir in all compiled
c401cc
tests.
c401cc
* tests/testutils.h (abs_srcdir): Quit declaring.
c401cc
* tests/testutils.c (virtTestMain): Rely on define rather than
c401cc
environment variable.
c401cc
* tests/virpcimock.c (pci_device_new_from_stub): Rely on define.
c401cc
* tests/cputest.c (mymain): Adjust abs_top_srcdir default.
c401cc
* tests/qemuxml2argvtest.c (mymain): Likewise.
c401cc
* tests/qemuxmlnstest.c (mymain): Likewise.
c401cc
c401cc
Signed-off-by: Eric Blake <eblake@redhat.com>
c401cc
(cherry picked from commit 0aa873d74118ea589f61fe46396335edeb1d955f)
c401cc
c401cc
Conflicts:
c401cc
	tests/virpcimock.c
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 tests/Makefile.am        |  1 +
c401cc
 tests/cputest.c          |  2 +-
c401cc
 tests/qemuxml2argvtest.c |  2 +-
c401cc
 tests/qemuxmlnstest.c    |  2 +-
c401cc
 tests/testutils.c        | 11 +----------
c401cc
 tests/testutils.h        |  6 +++++-
c401cc
 6 files changed, 10 insertions(+), 14 deletions(-)
c401cc
c401cc
diff --git a/tests/Makefile.am b/tests/Makefile.am
c401cc
index d095595..3d5ac72 100644
c401cc
--- a/tests/Makefile.am
c401cc
+++ b/tests/Makefile.am
c401cc
@@ -29,6 +29,7 @@ INCLUDES = \
c401cc
 
c401cc
 AM_CFLAGS = \
c401cc
 	-Dabs_builddir="\"`pwd`\"" \
c401cc
+	-Dabs_srcdir="\"`cd '$(srcdir)'; pwd`\"" \
c401cc
 	$(LIBXML_CFLAGS) \
c401cc
 	$(GNUTLS_CFLAGS) \
c401cc
 	$(SASL_CFLAGS) \
c401cc
diff --git a/tests/cputest.c b/tests/cputest.c
c401cc
index 2e5f0cd..6a1cfeb 100644
c401cc
--- a/tests/cputest.c
c401cc
+++ b/tests/cputest.c
c401cc
@@ -501,7 +501,7 @@ mymain(void)
c401cc
 
c401cc
     abs_top_srcdir = getenv("abs_top_srcdir");
c401cc
     if (!abs_top_srcdir)
c401cc
-        abs_top_srcdir = "..";
c401cc
+        abs_top_srcdir = abs_srcdir "/..";
c401cc
 
c401cc
     if (virAsprintf(&map, "%s/src/cpu/cpu_map.xml", abs_top_srcdir) < 0 ||
c401cc
         cpuMapOverride(map) < 0) {
c401cc
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
c401cc
index cfaee71..eb98966 100644
c401cc
--- a/tests/qemuxml2argvtest.c
c401cc
+++ b/tests/qemuxml2argvtest.c
c401cc
@@ -297,7 +297,7 @@ mymain(void)
c401cc
 
c401cc
     abs_top_srcdir = getenv("abs_top_srcdir");
c401cc
     if (!abs_top_srcdir)
c401cc
-        abs_top_srcdir = "..";
c401cc
+        abs_top_srcdir = abs_srcdir "/..";
c401cc
 
c401cc
     driver.config = virQEMUDriverConfigNew(false);
c401cc
     VIR_FREE(driver.config->spiceListen);
c401cc
diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c
c401cc
index 9f4f442..13dbf84 100644
c401cc
--- a/tests/qemuxmlnstest.c
c401cc
+++ b/tests/qemuxmlnstest.c
c401cc
@@ -203,7 +203,7 @@ mymain(void)
c401cc
 
c401cc
     abs_top_srcdir = getenv("abs_top_srcdir");
c401cc
     if (!abs_top_srcdir)
c401cc
-        abs_top_srcdir = "..";
c401cc
+        abs_top_srcdir = abs_srcdir "/..";
c401cc
 
c401cc
     driver.config = virQEMUDriverConfigNew(false);
c401cc
     if ((driver.caps = testQemuCapsInit()) == NULL)
c401cc
diff --git a/tests/testutils.c b/tests/testutils.c
c401cc
index 72aa5b3..964da41 100644
c401cc
--- a/tests/testutils.c
c401cc
+++ b/tests/testutils.c
c401cc
@@ -73,7 +73,6 @@ static size_t testStart = 0;
c401cc
 static size_t testEnd = 0;
c401cc
 
c401cc
 char *progname;
c401cc
-char *abs_srcdir;
c401cc
 
c401cc
 double
c401cc
 virtTestCountAverage(double *items, int nitems)
c401cc
@@ -586,7 +585,6 @@ int virtTestMain(int argc,
c401cc
                  int (*func)(void))
c401cc
 {
c401cc
     int ret;
c401cc
-    bool abs_srcdir_cleanup = false;
c401cc
     char *testRange = NULL;
c401cc
 #if TEST_OOM
c401cc
     int approxAlloc = 0;
c401cc
@@ -598,12 +596,7 @@ int virtTestMain(int argc,
c401cc
     int worker = 0;
c401cc
 #endif
c401cc
 
c401cc
-    abs_srcdir = getenv("abs_srcdir");
c401cc
-    if (!abs_srcdir) {
c401cc
-        abs_srcdir = getcwd(NULL, 0);
c401cc
-        abs_srcdir_cleanup = true;
c401cc
-    }
c401cc
-    if (!abs_srcdir)
c401cc
+    if (!virFileExists(abs_srcdir))
c401cc
         return EXIT_AM_HARDFAIL;
c401cc
 
c401cc
     progname = last_component(argv[0]);
c401cc
@@ -764,8 +757,6 @@ cleanup:
c401cc
     ret = (func)();
c401cc
 #endif
c401cc
 
c401cc
-    if (abs_srcdir_cleanup)
c401cc
-        VIR_FREE(abs_srcdir);
c401cc
     virResetLastError();
c401cc
     if (!virTestGetVerbose() && ret != EXIT_AM_SKIP) {
c401cc
         if (testCounter == 0 || testCounter % 40)
c401cc
diff --git a/tests/testutils.h b/tests/testutils.h
c401cc
index 27af5da..19efc5c 100644
c401cc
--- a/tests/testutils.h
c401cc
+++ b/tests/testutils.h
c401cc
@@ -38,7 +38,11 @@
c401cc
 # endif
c401cc
 
c401cc
 extern char *progname;
c401cc
-extern char *abs_srcdir;
c401cc
+
c401cc
+/* Makefile.am provides these two definitions */
c401cc
+# if !defined(abs_srcdir) || !defined(abs_builddir)
c401cc
+#  error Fix Makefile.am
c401cc
+# endif
c401cc
 
c401cc
 double virtTestCountAverage(double *items,
c401cc
                             int nitems);
c401cc
-- 
c401cc
1.8.4.5
c401cc