[libvirt] PATCH: Don't rely on host binaries in QEMU tests

Daniel P. Berrange berrange at redhat.com
Fri May 16 15:19:16 UTC 2008


The QEMU test suites rely on the QEMU/KVM/Xenner binaries being present
in /usr/bin. This has obvious problems and is unneccessary. The solution
is to not use the qemudCapsInit() function which initializes capabilities
based on binaries present. Instead I add a custom impl just for the test
cases which adds a pre-defined stable set of capabilities. I also had to
move a stat() check out of qemudBuildCommandLine() and into its caller.
It probably should have been there in the first place anyway

 src/qemu_conf.c          |   13 --------
 src/qemu_driver.c        |   14 +++++++++
 tests/Makefile.am        |    4 +-
 tests/qemuxml2argvtest.c |    4 +-
 tests/qemuxml2xmltest.c  |    3 +
 tests/testutilsqemu.c    |   71 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/testutilsqemu.h    |    5 +++
 7 files changed, 97 insertions(+), 17 deletions(-)

Dan.

Index: src/qemu_conf.c
===================================================================
RCS file: /data/cvs/libvirt/src/qemu_conf.c,v
retrieving revision 1.66
diff -u -p -u -p -r1.66 qemu_conf.c
--- src/qemu_conf.c	16 May 2008 09:37:44 -0000	1.66
+++ src/qemu_conf.c	16 May 2008 15:15:05 -0000
@@ -2326,7 +2326,6 @@ int qemudBuildCommandLine(virConnectPtr 
     char memory[50];
     char vcpus[50];
     char boot[QEMUD_MAX_BOOT_DEVS+1];
-    struct stat sb;
     struct qemud_vm_disk_def *disk = vm->def->disks;
     struct qemud_vm_net_def *net = vm->def->nets;
     struct qemud_vm_input_def *input = vm->def->inputs;
@@ -2336,18 +2335,6 @@ int qemudBuildCommandLine(virConnectPtr 
     struct utsname ut;
     int disableKQEMU = 0;
 
-    /* Make sure the binary we are about to try exec'ing exists.
-     * Technically we could catch the exec() failure, but that's
-     * in a sub-process so its hard to feed back a useful error
-     */
-    if (stat(vm->def->os.binary, &sb) < 0) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
-                         _("Cannot find QEMU binary %s: %s"),
-                         vm->def->os.binary,
-                         strerror(errno));
-        return -1;
-    }
-
     if (vm->qemuVersion == 0) {
         if (qemudExtractVersionInfo(vm->def->os.binary,
                                     &(vm->qemuVersion),
Index: src/qemu_driver.c
===================================================================
RCS file: /data/cvs/libvirt/src/qemu_driver.c,v
retrieving revision 1.75
diff -u -p -u -p -r1.75 qemu_driver.c
--- src/qemu_driver.c	16 May 2008 09:37:44 -0000	1.75
+++ src/qemu_driver.c	16 May 2008 15:15:05 -0000
@@ -646,6 +646,7 @@ static int qemudStartVMDaemon(virConnect
     char **argv = NULL, **tmp;
     int i, ret;
     char logfile[PATH_MAX];
+    struct stat sb;
 
     if (qemudIsActiveVM(vm)) {
         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
@@ -702,6 +703,19 @@ static int qemudStartVMDaemon(virConnect
         return -1;
     }
 
+    /* Make sure the binary we are about to try exec'ing exists.
+     * Technically we could catch the exec() failure, but that's
+     * in a sub-process so its hard to feed back a useful error
+     */
+    if (stat(vm->def->os.binary, &sb) < 0) {
+        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+                         _("Cannot find QEMU binary %s: %s"),
+                         vm->def->os.binary,
+                         strerror(errno));
+        return -1;
+    }
+
+
     if (qemudBuildCommandLine(conn, driver, vm, &argv) < 0) {
         close(vm->logfile);
         vm->logfile = -1;
Index: tests/Makefile.am
===================================================================
RCS file: /data/cvs/libvirt/tests/Makefile.am,v
retrieving revision 1.42
diff -u -p -u -p -r1.42 Makefile.am
--- tests/Makefile.am	28 Apr 2008 13:36:48 -0000	1.42
+++ tests/Makefile.am	16 May 2008 15:15:05 -0000
@@ -100,12 +100,12 @@ xmconfigtest_SOURCES = \
 xmconfigtest_LDADD = $(LDADDS)
 
 qemuxml2argvtest_SOURCES = \
-	qemuxml2argvtest.c \
+	qemuxml2argvtest.c testutilsqemu.c testutilsqemu.h \
 	testutils.c testutils.h
 qemuxml2argvtest_LDADD = $(LDADDS)
 
 qemuxml2xmltest_SOURCES = \
-	qemuxml2xmltest.c \
+	qemuxml2xmltest.c testutilsqemu.c testutilsqemu.h \
 	testutils.c testutils.h
 qemuxml2xmltest_LDADD = $(LDADDS)
 
Index: tests/qemuxml2argvtest.c
===================================================================
RCS file: /data/cvs/libvirt/tests/qemuxml2argvtest.c,v
retrieving revision 1.22
diff -u -p -u -p -r1.22 qemuxml2argvtest.c
--- tests/qemuxml2argvtest.c	15 May 2008 16:21:11 -0000	1.22
+++ tests/qemuxml2argvtest.c	16 May 2008 15:15:05 -0000
@@ -14,6 +14,8 @@
 #include "testutils.h"
 #include "qemu_conf.h"
 
+#include "testutilsqemu.h"
+
 static char *progname;
 static char *abs_srcdir;
 static struct qemud_driver driver;
@@ -130,7 +132,7 @@ main(int argc, char **argv)
     if (!abs_srcdir)
         abs_srcdir = getcwd(cwd, sizeof(cwd));
 
-    driver.caps = qemudCapsInit();
+    driver.caps = testQemuCapsInit();
 
 #define DO_TEST(name, extraFlags)                                       \
     do {                                                                \
Index: tests/qemuxml2xmltest.c
===================================================================
RCS file: /data/cvs/libvirt/tests/qemuxml2xmltest.c,v
retrieving revision 1.17
diff -u -p -u -p -r1.17 qemuxml2xmltest.c
--- tests/qemuxml2xmltest.c	15 May 2008 16:21:11 -0000	1.17
+++ tests/qemuxml2xmltest.c	16 May 2008 15:15:05 -0000
@@ -13,6 +13,7 @@
 #include "internal.h"
 #include "testutils.h"
 #include "qemu_conf.h"
+#include "testutilsqemu.h"
 
 static char *progname;
 static char *abs_srcdir;
@@ -86,7 +87,7 @@ main(int argc, char **argv)
     if (!abs_srcdir)
         abs_srcdir = getcwd(cwd, sizeof(cwd));
 
-    driver.caps = qemudCapsInit();
+    driver.caps = testQemuCapsInit();
 
 #define DO_TEST(name) \
     if (virtTestRun("QEMU XML-2-XML " name, \
Index: tests/testutilsqemu.c
===================================================================
RCS file: tests/testutilsqemu.c
diff -N tests/testutilsqemu.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/testutilsqemu.c	16 May 2008 15:15:05 -0000
@@ -0,0 +1,71 @@
+
+#include <sys/utsname.h>
+#include <stdlib.h>
+
+#include "testutilsqemu.h"
+
+virCapsPtr testQemuCapsInit(void) {
+    struct utsname utsname;
+    virCapsPtr caps;
+    virCapsGuestPtr guest;
+    static const char *const x86_machines[] = {
+        "pc", "isapc"
+    };
+    static const char *const xen_machines[] = {
+        "xenner"
+    };
+
+    uname (&utsname);
+    if ((caps = virCapabilitiesNew(utsname.machine,
+                                   0, 0)) == NULL)
+        return NULL;
+
+    if ((guest = virCapabilitiesAddGuest(caps, "hvm", "i686", 32,
+                                         "/usr/bin/qemu", NULL,
+                                         2, x86_machines)) == NULL)
+        goto cleanup;
+    if (virCapabilitiesAddGuestDomain(guest,
+                                      "qemu",
+                                      NULL,
+                                      NULL,
+                                      0,
+                                      NULL) == NULL)
+        goto cleanup;
+
+    if ((guest = virCapabilitiesAddGuest(caps, "hvm", "x86_64", 64,
+                                         "/usr/bin/qemu-system-x86_64", NULL,
+                                         2, x86_machines)) == NULL)
+        goto cleanup;
+    if (virCapabilitiesAddGuestDomain(guest,
+                                      "qemu",
+                                      NULL,
+                                      NULL,
+                                      0,
+                                      NULL) == NULL)
+        goto cleanup;
+    if (virCapabilitiesAddGuestDomain(guest,
+                                      "kvm",
+                                      "/usr/bin/kvm",
+                                      NULL,
+                                      0,
+                                      NULL) == NULL)
+        goto cleanup;
+
+    if ((guest = virCapabilitiesAddGuest(caps, "xen", "x86_64", 64,
+                                         "/usr/bin/xenner", NULL,
+                                         1, xen_machines)) == NULL)
+        goto cleanup;
+    if (virCapabilitiesAddGuestDomain(guest,
+                                      "kvm",
+                                      "/usr/bin/kvm",
+                                      NULL,
+                                      0,
+                                      NULL) == NULL)
+        goto cleanup;
+
+    return caps;
+
+cleanup:
+    virCapabilitiesFree(caps);
+    return NULL;
+}
Index: tests/testutilsqemu.h
===================================================================
RCS file: tests/testutilsqemu.h
diff -N tests/testutilsqemu.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/testutilsqemu.h	16 May 2008 15:15:05 -0000
@@ -0,0 +1,5 @@
+
+#include "capabilities.h"
+
+virCapsPtr testQemuCapsInit(void);
+


-- 
|: Red Hat, Engineering, Boston   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list