aboutsummaryrefslogtreecommitdiff
path: root/tests/libqtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/libqtest.c')
-rw-r--r--tests/libqtest.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/libqtest.c b/tests/libqtest.c
index b9a1f180e1..0c12b38906 100644
--- a/tests/libqtest.c
+++ b/tests/libqtest.c
@@ -987,3 +987,78 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
qtest_end();
QDECREF(response);
}
+
+/*
+ * Generic hot-plugging test via the device_add QMP command.
+ */
+void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
+ ...)
+{
+ QDict *response;
+ char *cmd, *opts = NULL;
+ va_list va;
+
+ if (fmt) {
+ va_start(va, fmt);
+ opts = g_strdup_vprintf(fmt, va);
+ va_end(va);
+ }
+
+ cmd = g_strdup_printf("{'execute': 'device_add',"
+ " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
+ driver, id, opts ? ", " : "", opts ? opts : "");
+ g_free(opts);
+
+ response = qmp(cmd);
+ g_free(cmd);
+ g_assert(response);
+ g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
+ g_assert(!qdict_haskey(response, "error"));
+ QDECREF(response);
+}
+
+/*
+ * Generic hot-unplugging test via the device_del QMP command.
+ * Device deletion will get one response and one event. For example:
+ *
+ * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
+ *
+ * will get this one:
+ *
+ * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
+ * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
+ * "path": "/machine/peripheral/scsi-hd"}}
+ *
+ * and this one:
+ *
+ * {"return": {}}
+ *
+ * But the order of arrival may vary - so we've got to detect both.
+ */
+void qtest_qmp_device_del(const char *id)
+{
+ QDict *response1, *response2, *event = NULL;
+ char *cmd;
+
+ cmd = g_strdup_printf("{'execute': 'device_del',"
+ " 'arguments': { 'id': '%s' }}", id);
+ response1 = qmp(cmd);
+ g_free(cmd);
+ g_assert(response1);
+ g_assert(!qdict_haskey(response1, "error"));
+
+ response2 = qmp("");
+ g_assert(response2);
+ g_assert(!qdict_haskey(response2, "error"));
+
+ if (qdict_haskey(response1, "event")) {
+ event = response1;
+ } else if (qdict_haskey(response2, "event")) {
+ event = response2;
+ }
+ g_assert(event);
+ g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
+
+ QDECREF(response1);
+ QDECREF(response2);
+}