aboutsummaryrefslogtreecommitdiff
path: root/tests/libqtest.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2012-04-26 19:07:55 +0200
committerKevin Wolf <kwolf@redhat.com>2012-05-10 10:32:11 +0200
commita3ca163cb554e6a3d55644a1ccf8c86a1b02c6cc (patch)
tree32bce89af243d583fc21ae72d033f3f28b694af9 /tests/libqtest.c
parentc68b039aa950aea1e534c7abd5ba5ce602a8f4db (diff)
qtest: Add function to send QMP commands
Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests/libqtest.c')
-rw-r--r--tests/libqtest.c123
1 files changed, 96 insertions, 27 deletions
diff --git a/tests/libqtest.c b/tests/libqtest.c
index 295c6d49d0..6d333ef0ac 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -36,6 +36,7 @@ QTestState *global_qtest;
struct QTestState
{
int fd;
+ int qmp_fd;
bool irq_level[MAX_IRQ];
GString *rx;
gchar *pid_file;
@@ -45,48 +46,76 @@ struct QTestState
g_assert_cmpint(ret, !=, -1); \
} while (0)
+static int init_socket(const char *socket_path)
+{
+ struct sockaddr_un addr;
+ int sock;
+ int ret;
+
+ sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ g_assert_no_errno(sock);
+
+ addr.sun_family = AF_UNIX;
+ snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
+ qemu_set_cloexec(sock);
+
+ do {
+ ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
+ } while (ret == -1 && errno == EINTR);
+ g_assert_no_errno(ret);
+ listen(sock, 1);
+
+ return sock;
+}
+
+static int socket_accept(int sock)
+{
+ struct sockaddr_un addr;
+ socklen_t addrlen;
+ int ret;
+
+ do {
+ ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
+ } while (ret == -1 && errno == EINTR);
+ g_assert_no_errno(ret);
+ close(sock);
+
+ return ret;
+}
+
QTestState *qtest_init(const char *extra_args)
{
QTestState *s;
- struct sockaddr_un addr;
- int sock, ret, i;
+ int sock, qmpsock, ret, i;
gchar *socket_path;
+ gchar *qmp_socket_path;
gchar *pid_file;
gchar *command;
const char *qemu_binary;
pid_t pid;
- socklen_t addrlen;
qemu_binary = getenv("QTEST_QEMU_BINARY");
g_assert(qemu_binary != NULL);
socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
+ qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
s = g_malloc(sizeof(*s));
- sock = socket(PF_UNIX, SOCK_STREAM, 0);
- g_assert_no_errno(sock);
-
- addr.sun_family = AF_UNIX;
- snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
- qemu_set_cloexec(sock);
-
- do {
- ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
- } while (ret == -1 && errno == EINTR);
- g_assert_no_errno(ret);
- listen(sock, 1);
+ sock = init_socket(socket_path);
+ qmpsock = init_socket(qmp_socket_path);
pid = fork();
if (pid == 0) {
command = g_strdup_printf("%s "
"-qtest unix:%s,nowait "
"-qtest-log /dev/null "
+ "-qmp unix:%s,nowait "
"-pidfile %s "
"-machine accel=qtest "
"%s", qemu_binary, socket_path,
- pid_file,
+ qmp_socket_path, pid_file,
extra_args ?: "");
ret = system(command);
@@ -94,13 +123,9 @@ QTestState *qtest_init(const char *extra_args)
g_free(command);
}
- do {
- ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
- } while (ret == -1 && errno == EINTR);
- g_assert_no_errno(ret);
- close(sock);
+ s->fd = socket_accept(sock);
+ s->qmp_fd = socket_accept(qmpsock);
- s->fd = ret;
s->rx = g_string_new("");
s->pid_file = pid_file;
for (i = 0; i < MAX_IRQ; i++) {
@@ -108,6 +133,11 @@ QTestState *qtest_init(const char *extra_args)
}
g_free(socket_path);
+ g_free(qmp_socket_path);
+
+ /* Read the QMP greeting and then do the handshake */
+ qtest_qmp(s, "");
+ qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }");
return s;
}
@@ -131,22 +161,19 @@ void qtest_quit(QTestState *s)
}
}
-static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
+static void socket_sendf(int fd, const char *fmt, va_list ap)
{
- va_list ap;
gchar *str;
size_t size, offset;
- va_start(ap, fmt);
str = g_strdup_vprintf(fmt, ap);
- va_end(ap);
size = strlen(str);
offset = 0;
while (offset < size) {
ssize_t len;
- len = write(s->fd, str + offset, size - offset);
+ len = write(fd, str + offset, size - offset);
if (len == -1 && errno == EINTR) {
continue;
}
@@ -158,6 +185,15 @@ static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
}
}
+static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ socket_sendf(s->fd, fmt, ap);
+ va_end(ap);
+}
+
static GString *qtest_recv_line(QTestState *s)
{
GString *line;
@@ -233,6 +269,39 @@ redo:
return words;
}
+void qtest_qmp(QTestState *s, const char *fmt, ...)
+{
+ va_list ap;
+ bool has_reply = false;
+ int nesting = 0;
+
+ /* Send QMP request */
+ va_start(ap, fmt);
+ socket_sendf(s->qmp_fd, fmt, ap);
+ va_end(ap);
+
+ /* Receive reply */
+ while (!has_reply || nesting > 0) {
+ ssize_t len;
+ char c;
+
+ len = read(s->qmp_fd, &c, 1);
+ if (len == -1 && errno == EINTR) {
+ continue;
+ }
+
+ switch (c) {
+ case '{':
+ nesting++;
+ has_reply = true;
+ break;
+ case '}':
+ nesting--;
+ break;
+ }
+ }
+}
+
const char *qtest_get_arch(void)
{
const char *qemu = getenv("QTEST_QEMU_BINARY");