From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=54717 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PyBPQ-0004Gh-Gn for qemu-devel@nongnu.org; Fri, 11 Mar 2011 18:06:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PyBPO-0006z2-UF for qemu-devel@nongnu.org; Fri, 11 Mar 2011 18:06:12 -0500 Received: from e6.ny.us.ibm.com ([32.97.182.146]:47239) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PyBPO-0006yx-PO for qemu-devel@nongnu.org; Fri, 11 Mar 2011 18:06:10 -0500 Received: from d01dlp02.pok.ibm.com (d01dlp02.pok.ibm.com [9.56.224.85]) by e6.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p2BMfrib004881 for ; Fri, 11 Mar 2011 17:41:53 -0500 Received: from d01relay06.pok.ibm.com (d01relay06.pok.ibm.com [9.56.227.116]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 88E776E8039 for ; Fri, 11 Mar 2011 18:06:10 -0500 (EST) Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay06.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p2BN69YA1642578 for ; Fri, 11 Mar 2011 18:06:10 -0500 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p2BN69sJ028464 for ; Fri, 11 Mar 2011 18:06:09 -0500 From: Anthony Liguori Date: Fri, 11 Mar 2011 17:05:44 -0600 Message-Id: <1299884745-521-15-git-send-email-aliguori@us.ibm.com> In-Reply-To: <1299884745-521-1-git-send-email-aliguori@us.ibm.com> References: <1299884745-521-1-git-send-email-aliguori@us.ibm.com> Subject: [Qemu-devel] [PATCH 14/15] qapi: add test-libqmp List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Adam Litke , Anthony Liguori , Luiz Capitulino , Avi Kivity , Markus Armbruster This provides a glib-test based testing framework for QMP Signed-off-by: Anthony Liguori diff --git a/Makefile b/Makefile index 5170675..1d363d7 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,8 @@ defconfig: -include config-all-devices.mak +TOOLS += test-libqmp + build-all: $(DOCS) $(TOOLS) recurse-all config-host.h: config-host.h-timestamp @@ -205,6 +207,15 @@ check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS) check-qfloat: check-qfloat.o qfloat.o $(CHECK_PROG_DEPS) check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o $(CHECK_PROG_DEPS) +LIBQMP_OBJS := qmp-types.o libqmp.o error.o libqmp-core.o +LIBQMP_OBJS += qmp-marshal-types-core.o qmp-marshal-types.o +LIBQMP_OBJS += qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o +LIBQMP_OBJS += qerror.o +LIBQMP_OBJS += json-streamer.o json-lexer.o json-parser.o +LIBQMP_OBJS += $(oslib-obj-y) $(trace-obj-y) qemu-malloc.o + +test-libqmp: test-libqmp.o $(LIBQMP_OBJS) qemu-timer-common.o + clean: # avoid old build problems by removing potentially incorrect old files rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h diff --git a/test-libqmp.c b/test-libqmp.c new file mode 100644 index 0000000..9b73987 --- /dev/null +++ b/test-libqmp.c @@ -0,0 +1,170 @@ +/* + * QAPI + * + * Copyright IBM, Corp. 2011 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU LGPL, version 2. See + * the COPYING.LIB file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config-host.h" +#include "libqmp.h" +#include "qerror.h" + +#define g_assert_noerr(err) g_assert(err == NULL); +#define g_assert_anyerr(err) g_assert(err != NULL); +#define g_assert_cmperr(err, op, type) do { \ + g_assert_anyerr(err); \ + g_assert_cmpstr(error_get_field(err, "class"), op, type); \ +} while (0) + +static pid_t last_qemu_pid = -1; + +static QmpSession *qemu(const char *fmt, ...) +{ + char buffer0[4096]; + char buffer1[4096]; + const char *pid_filename = "/tmp/test-libqmp-qemu.pid"; + const char *path = "/tmp/test-libqmp-qemu.sock"; + struct sockaddr_un addr; + va_list ap; + int ret; + int fd; + + va_start(ap, fmt); + vsnprintf(buffer0, sizeof(buffer0), fmt, ap); + va_end(ap); + + snprintf(buffer1, sizeof(buffer1), + "i386-softmmu/qemu " + "-enable-kvm " + "-name test-libqmp " + "-qmp2 qmp " + "-chardev socket,id=qmp,path=%s,server=on,wait=off " + "-vnc none " + "-daemonize " + "-pidfile %s " + "%s", path, pid_filename, buffer0); + g_test_message("Executing %s\n", buffer1); + ret = system(buffer1); + g_assert(ret != -1); + + { + FILE *f; + char buffer[1024]; + char *ptr; + + f = fopen(pid_filename, "r"); + g_assert(f != NULL); + + ptr = fgets(buffer, sizeof(buffer), f); + g_assert(ptr != NULL); + + fclose(f); + + last_qemu_pid = atoi(buffer); + } + + fd = socket(PF_UNIX, SOCK_STREAM, 0); + g_assert(fd != -1); + + addr.sun_family = AF_UNIX; + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path); + ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); + g_assert(ret != -1); + + return qmp_session_new(fd); +} + +static void wait_for_pid_exit(pid_t pid) +{ + FILE *f = NULL; + + /* This is ugly but I don't know of a better way */ + do { + char buffer[1024]; + + if (f) { + fclose(f); + usleep(10000); + } + + snprintf(buffer, sizeof(buffer), "/proc/%d/stat", pid); + f = fopen(buffer, "r"); + } while (f); +} + +static void qemu_destroy(QmpSession *sess) +{ + wait_for_pid_exit(last_qemu_pid); + last_qemu_pid = -1; + qmp_session_destroy(sess); +} + +static void test_version(void) +{ + QmpSession *sess = NULL; + VersionInfo *info; + char version[1024]; + char *ptr, *end; + int major, minor, micro; + + /* Even though we use the same string as the source input, we do parse it + * a little bit different for no other reason that to make sure we catch + * potential bugs. + */ + snprintf(version, sizeof(version), "%s", QEMU_VERSION); + ptr = version; + + end = strchr(ptr, '.'); + g_assert(end != NULL); + *end = 0; + major = atoi(ptr); + ptr = end + 1; + + end = strchr(ptr, '.'); + g_assert(end != NULL); + *end = 0; + minor = atoi(ptr); + ptr = end + 1; + + micro = atoi(ptr); + while (g_ascii_isdigit(*ptr)) ptr++; + + sess = qemu("-S"); + + info = libqmp_query_version(sess, NULL); + + g_assert_cmpint(major, ==, info->qemu.major); + g_assert_cmpint(minor, ==, info->qemu.minor); + g_assert_cmpint(micro, ==, info->qemu.micro); + g_assert_cmpstr(ptr, ==, info->package); + + qmp_free_version_info(info); + + libqmp_quit(sess, NULL); + + qemu_destroy(sess); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/0.14/misc/version", test_version); + + g_test_run(); + + return 0; +} -- 1.7.0.4