@@ -483,6 +483,22 @@ QDict *qmp_raw(const char *msg);
void qmp_async(const char *fmt, ...);
/**
+ * qmp_cmd:
+ * @cmd: QMP command, with no arguments.
+ *
+ * Sends a QMP message to QEMU and returns the response.
+ */
+QDict *qmp_cmd(const char *cmd);
+
+/**
+ * qmp_cmd_async:
+ * @cmd: QMP command, with no arguments.
+ *
+ * Sends a QMP message to QEMU and leaves the response in the stream.
+ */
+void qmp_cmd_async(const char *cmd);
+
+/**
* qmp_discard_response:
*
* Read and discard a QMP response, typically after qmp_async().
@@ -858,6 +858,17 @@ void qmp_async(const char *fmt, ...)
va_end(ap);
}
+QDict *qmp_cmd(const char *cmd)
+{
+ qmp_cmd_async(cmd);
+ return qtest_qmp_receive(global_qtest);
+}
+
+void qmp_cmd_async(const char *cmd)
+{
+ qtest_qmp_send(global_qtest, "{'execute':%s}", cmd);
+}
+
void qmp_discard_response(void)
{
QDict *response = qtest_qmp_receive(global_qtest);
@@ -890,7 +901,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
const char *mname;
qtest_start("-machine none");
- response = qmp("{ 'execute': 'query-machines' }");
+ response = qmp_cmd("query-machines");
g_assert(response);
list = qdict_get_qlist(response, "return");
g_assert(list);
@@ -1350,7 +1350,6 @@ static void test_flush_migrate(void)
AHCIQState *src, *dst;
AHCICommand *cmd;
uint8_t px;
- const char *s;
char *uri = g_strdup_printf("unix:%s", mig_socket);
prepare_blkdebug_script(debug_path, "flush_to_disk");
@@ -1386,8 +1385,7 @@ static void test_flush_migrate(void)
ahci_migrate(src, dst, uri);
/* Complete the command */
- s = "{'execute':'cont' }";
- qmp_async(s);
+ qmp_cmd_async("cont");
qmp_eventwait("RESUME");
ahci_command_wait(dst, cmd);
ahci_command_verify(dst, cmd);
@@ -38,7 +38,7 @@ static void test_a_boot_order(const char *machine,
qtest_start(args);
actual = read_boot_order();
g_assert_cmphex(actual, ==, expected_boot);
- qmp_async("{ 'execute': 'system_reset' }");
+ qmp_cmd_async("system_reset");
/*
* system_reset only requests reset. We get a RESET event after
* the actual reset completes. Need to wait for that.
@@ -651,7 +651,7 @@ static void test_retry_flush(const char *machine)
qmp_eventwait("STOP");
/* Complete the command */
- qmp_async("{'execute':'cont' }");
+ qmp_cmd_async("cont");
qmp_discard_response();
/* Check registers */
@@ -668,7 +668,7 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,
g_assert_cmpint(rc, ==, 0);
}
if (opts->error) {
- qmp_async("{'execute':'cont' }");
+ qmp_cmd_async("cont");
qmp_eventwait("RESUME");
}
@@ -706,7 +706,7 @@ AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port,
void ahci_guest_io_resume(AHCIQState *ahci, AHCICommand *cmd)
{
/* Complete the command */
- qmp_async("{'execute':'cont' }");
+ qmp_cmd_async("cont");
qmp_eventwait("RESUME");
ahci_command_wait(ahci, cmd);
ahci_command_verify(ahci, cmd);
@@ -84,18 +84,6 @@ void set_context(QOSState *s)
global_qtest = s->qts;
}
-static QDict *qmp_execute(const char *command)
-{
- char *fmt;
- QDict *rsp;
-
- fmt = g_strdup_printf("{ 'execute': '%s' }", command);
- rsp = qmp(fmt);
- g_free(fmt);
-
- return rsp;
-}
-
void migrate(QOSState *from, QOSState *to, const char *uri)
{
const char *st;
@@ -106,7 +94,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
set_context(from);
/* Is the machine currently running? */
- rsp = qmp_execute("query-status");
+ rsp = qmp_cmd("query-status");
g_assert(qdict_haskey(rsp, "return"));
sub = qdict_get_qdict(rsp, "return");
g_assert(qdict_haskey(sub, "running"));
@@ -137,7 +125,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
/* Otherwise, we need to wait: poll until migration is completed. */
while (1) {
- rsp = qmp_execute("query-migrate");
+ rsp = qmp_cmd("query-migrate");
g_assert(qdict_haskey(rsp, "return"));
sub = qdict_get_qdict(rsp, "return");
g_assert(qdict_haskey(sub, "status"));
@@ -74,7 +74,7 @@ static void test_mon_partial(const void *data)
static QList *get_cpus(QDict **resp)
{
- *resp = qmp("{ 'execute': 'query-cpus' }");
+ *resp = qmp_cmd("query-cpus");
g_assert(*resp);
g_assert(qdict_haskey(*resp, "return"));
return qdict_get_qlist(*resp, "return");
@@ -252,7 +252,7 @@ static uint64_t get_migration_pass(void)
QDict *rsp, *rsp_return, *rsp_ram;
uint64_t result;
- rsp = return_or_event(qmp("{ 'execute': 'query-migrate' }"));
+ rsp = return_or_event(qmp_cmd("query-migrate"));
rsp_return = qdict_get_qdict(rsp, "return");
if (!qdict_haskey(rsp_return, "ram")) {
/* Still in setup */
@@ -273,7 +273,7 @@ static void wait_for_migration_complete(void)
do {
const char *status;
- rsp = return_or_event(qmp("{ 'execute': 'query-migrate' }"));
+ rsp = return_or_event(qmp_cmd("query-migrate"));
rsp_return = qdict_get_qdict(rsp, "return");
status = qdict_get_str(rsp_return, "status");
completed = strcmp(status, "completed") == 0;
@@ -455,7 +455,7 @@ static void test_migrate(void)
wait_for_migration_pass();
- rsp = return_or_event(qmp("{ 'execute': 'migrate-start-postcopy' }"));
+ rsp = return_or_event(qmp_cmd("migrate-start-postcopy"));
g_assert(qdict_haskey(rsp, "return"));
QDECREF(rsp);
@@ -482,7 +482,7 @@ static void test_migrate(void)
usleep(10 * 1000);
} while (dest_byte_a == dest_byte_b);
- qmp_async("{ 'execute' : 'stop'}");
+ qmp_cmd_async("stop");
qmp_discard_response();
/* With it stopped, check nothing changes */
qtest_memread(to, start_address, &dest_byte_c, 1);
@@ -105,7 +105,7 @@ static void test_smram_lock(void)
g_assert(smram_test_bit(pcidev, MCH_HOST_BRIDGE_SMRAM_D_OPEN) == false);
/* reset */
- response = qmp("{'execute': 'system_reset', 'arguments': {} }");
+ response = qmp_cmd("system_reset");
g_assert(response);
g_assert(!qdict_haskey(response, "error"));
QDECREF(response);
@@ -92,7 +92,7 @@ static void test_qmp_protocol(void)
QDECREF(resp);
/* Test valid command before handshake */
- resp = qmp("{ 'execute': 'query-version' }");
+ resp = qmp_cmd("query-version");
g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
QDECREF(resp);
@@ -100,18 +100,18 @@ static void test_qmp_protocol(void)
test_malformed();
/* Test handshake */
- resp = qmp("{ 'execute': 'qmp_capabilities' }");
+ resp = qmp_cmd("qmp_capabilities");
ret = qdict_get_qdict(resp, "return");
g_assert(ret && !qdict_size(ret));
QDECREF(resp);
/* Test repeated handshake */
- resp = qmp("{ 'execute': 'qmp_capabilities' }");
+ resp = qmp_cmd("qmp_capabilities");
g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
QDECREF(resp);
/* Test valid command */
- resp = qmp("{ 'execute': 'query-version' }");
+ resp = qmp_cmd("query-version");
test_version(qdict_get(resp, "return"));
QDECREF(resp);
@@ -98,7 +98,7 @@ static void test_machine(gconstpointer data)
test_properties("/machine", true);
- response = qmp("{ 'execute': 'quit' }");
+ response = qmp_cmd("quit");
g_assert(qdict_haskey(response, "return"));
QDECREF(response);
@@ -57,7 +57,7 @@ static void test_mirror(void)
};
/* send a qmp command to guarantee that 'connected' is setting to true. */
- qmp_async("{ 'execute' : 'query-status'}");
+ qmp_cmd_async("query-status");
qmp_discard_response();
ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
@@ -99,7 +99,7 @@ static void test_redirector_tx(void)
g_assert_cmpint(recv_sock, !=, -1);
/* send a qmp command to guarantee that 'connected' is setting to true. */
- qmp_async("{ 'execute' : 'query-status'}");
+ qmp_cmd_async("query-status");
qmp_discard_response();
struct iovec iov[] = {
@@ -185,7 +185,7 @@ static void test_redirector_rx(void)
send_sock = unix_connect(sock_path1, NULL);
g_assert_cmpint(send_sock, !=, -1);
/* send a qmp command to guarantee that 'connected' is setting to true. */
- qmp_async("{ 'execute' : 'query-status'}");
+ qmp_cmd_async("query-status");
qmp_discard_response();
ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
@@ -13,7 +13,7 @@ static char *get_cpu0_qom_path(void)
QDict *cpu0;
char *path;
- resp = qmp("{'execute': 'query-cpus', 'arguments': {}}");
+ resp = qmp_cmd("query-cpus");
g_assert(qdict_haskey(resp, "return"));
ret = qdict_get_qlist(resp, "return");
@@ -151,7 +151,6 @@ static void rx_stop_cont_test(QVirtioDevice *dev,
char test[] = "TEST";
char buffer[64];
int len = htonl(sizeof(test));
- QDict *rsp;
struct iovec iov[] = {
{
.iov_base = &len,
@@ -168,8 +167,8 @@ static void rx_stop_cont_test(QVirtioDevice *dev,
free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
qvirtqueue_kick(dev, vq, free_head);
- rsp = qmp("{ 'execute' : 'stop'}");
- QDECREF(rsp);
+ qmp_cmd_async("stop");
+ qmp_discard_response();
ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
@@ -177,10 +176,10 @@ static void rx_stop_cont_test(QVirtioDevice *dev,
/* We could check the status, but this command is more importantly to
* ensure the packet data gets queued in QEMU, before we do 'cont'.
*/
- rsp = qmp("{ 'execute' : 'query-status'}");
- QDECREF(rsp);
- rsp = qmp("{ 'execute' : 'cont'}");
- QDECREF(rsp);
+ qmp_cmd_async("query-status");
+ qmp_discard_response();
+ qmp_cmd_async("cont");
+ qmp_discard_response();
qvirtio_wait_used_elem(dev, vq, free_head, QVIRTIO_NET_TIMEOUT_US);
memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
@@ -118,7 +118,7 @@ static void read_guid_from_monitor(QemuUUID *guid)
QDict *rsp, *rsp_ret;
const char *guid_str;
- rsp = qmp("{ 'execute': 'query-vm-generation-id' }");
+ rsp = qmp_cmd("query-vm-generation-id");
if (qdict_haskey(rsp, "return")) {
rsp_ret = qdict_get_qdict(rsp, "return");
g_assert(qdict_haskey(rsp_ret, "guid"));
@@ -13,7 +13,7 @@
static void qmp_check_no_event(void)
{
- QDict *resp = qmp("{'execute':'query-status'}");
+ QDict *resp = qmp_cmd("query-status");
g_assert(qdict_haskey(resp, "return"));
QDECREF(resp);
}
Now that we've asserted that all of our interpolated QMP commands include 'execute', we can reduce some of the caller boilerplate by providing a helpr function to wrap commands with no arguments (later patches will cover commands with arguments). Adjust all callers that can use the new helpers; in the process, fixing a couple of places where we would have failed -Wformat-nonliteral. Likewise, libqos.c no longer needs qmp_execute(), which in turn fixes the fact that it is better to interpolate JSON strings through qobject_from_json() than through sprintf(). The current name is long, but temporary: later patches will remove all other uses of qmp(), and then make the mass rename of qmp_cmd() down to qmp(). Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/libqtest.h | 16 ++++++++++++++++ tests/libqtest.c | 13 ++++++++++++- tests/ahci-test.c | 4 +--- tests/boot-order-test.c | 2 +- tests/ide-test.c | 2 +- tests/libqos/ahci.c | 4 ++-- tests/libqos/libqos.c | 16 ++-------------- tests/numa-test.c | 2 +- tests/postcopy-test.c | 8 ++++---- tests/q35-test.c | 2 +- tests/qmp-test.c | 8 ++++---- tests/qom-test.c | 2 +- tests/test-filter-mirror.c | 2 +- tests/test-filter-redirector.c | 4 ++-- tests/test-x86-cpuid-compat.c | 2 +- tests/virtio-net-test.c | 13 ++++++------- tests/vmgenid-test.c | 2 +- tests/wdt_ib700-test.c | 2 +- 18 files changed, 58 insertions(+), 46 deletions(-)