]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: guarantee abs_srcdir in all C tests
authorEric Blake <eblake@redhat.com>
Wed, 27 Nov 2013 21:31:53 +0000 (14:31 -0700)
committerEric Blake <eblake@redhat.com>
Wed, 27 Nov 2013 21:39:56 +0000 (14:39 -0700)
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>
tests/Makefile.am
tests/cputest.c
tests/qemuxml2argvtest.c
tests/qemuxmlnstest.c
tests/testutils.c
tests/testutils.h
tests/virpcimock.c

index e46d5f76320ccca59ba13baf278ee139c7245b3c..520fd2ac764db8af919bcc40dad1280cf1ab945b 100644 (file)
@@ -29,6 +29,7 @@ INCLUDES = \
 
 AM_CFLAGS = \
        -Dabs_builddir="\"`pwd`\"" \
+       -Dabs_srcdir="\"`cd '$(srcdir)'; pwd`\"" \
        $(LIBXML_CFLAGS) \
        $(GNUTLS_CFLAGS) \
        $(SASL_CFLAGS) \
index b80fac7b3c6c213cd598a23805c8d2494b39b7a6..20bc544c8d0ed5a3e6aaa58942a91a55d6fdb103 100644 (file)
@@ -503,7 +503,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) {
index a290062dd921c990bbd5c5c1bd2640788335d434..dad597394edee0d6b6d3abe3df13b143d49f4bd5 100644 (file)
@@ -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);
index 2cc15d150035f6a093116b3e554d0f05ef865465..2093e1e1b21f2d791b4191715c56e6df71a8210e 100644 (file)
@@ -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)
index 5d634b402435f31c6e211bd3616d6023d5ca1a3c..32fe3745c3c95bebedd8191cf6cd582406392579 100644 (file)
@@ -69,7 +69,6 @@ static size_t testStart = 0;
 static size_t testEnd = 0;
 
 char *progname;
-char *abs_srcdir;
 
 void virtTestResult(const char *name, int ret, const char *msg, ...)
 {
@@ -535,15 +534,9 @@ int virtTestMain(int argc,
                  int (*func)(void))
 {
     int ret;
-    bool abs_srcdir_cleanup = false;
     char *testRange = NULL;
 
-    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]);
@@ -599,8 +592,6 @@ int virtTestMain(int argc,
 
     ret = (func)();
 
-    if (abs_srcdir_cleanup)
-        VIR_FREE(abs_srcdir);
     virResetLastError();
     if (!virTestGetVerbose() && ret != EXIT_AM_SKIP) {
         if (testCounter == 0 || testCounter % 40)
index 478b53cfe462aa4b1820221a0fd13ff60af63e65..674d3df98dcd6879dcf0f462567d570959ae938b 100644 (file)
 # 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
 
 void virtTestResult(const char *name, int ret, const char *msg, ...)
     ATTRIBUTE_FMT_PRINTF(3,4);
index 19062c3705128b32c36327a827d6aa655f3f61d0..48d5f163dc7bc8eb54ff54fd0dffb1e5e21e2e82 100644 (file)
@@ -305,14 +305,10 @@ pci_device_new_from_stub(const struct pciDevice *data)
     char *configSrc, *configDst;
     char tmp[32];
     struct stat sb;
-    char *abs_srcdir;
-
-    abs_srcdir = getenv("abs_srcdir");
-    if (!abs_srcdir)
-        abs_srcdir = getcwd(NULL, 0);
 
     if (VIR_ALLOC_QUIET(dev) < 0 ||
-        virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config", abs_srcdir, data->id) < 0 ||
+        virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config",
+                         abs_srcdir, data->id) < 0 ||
         virAsprintfQuiet(&devpath, "%s/devices/%s", fakesysfsdir, data->id) < 0)
         ABORT_OOM();
 
@@ -480,7 +476,7 @@ pci_driver_bind(struct pciDriver *driver,
     char *devpath = NULL, *driverpath = NULL;
 
     if (dev->driver) {
-        /* Device already binded */
+        /* Device already bound */
         errno = ENODEV;
         return ret;
     }
@@ -527,7 +523,7 @@ pci_driver_unbind(struct pciDriver *driver,
     char *devpath = NULL, *driverpath = NULL;
 
     if (dev->driver != driver) {
-        /* Device not binded to the @driver */
+        /* Device not bound to the @driver */
         errno = ENODEV;
         return ret;
     }