On Thu, 17 Aug 2017 08:25:11 +0200 Thomas Huth <th...@redhat.com> wrote:
> A lot of tests provide code for adding and removing a device via the > device_add and device_del QMP commands. Maintaining this code in so > many places is cumbersome and error-prone (some of the code parts > check the responses in an incorrect way, for example), so let's > provide some proper generic qtest functions for adding and removing a > device instead. This sounds like a good idea. > > Signed-off-by: Thomas Huth <th...@redhat.com> > --- > tests/libqos/pci.c | 19 ++------------- > tests/libqos/usb.c | 30 +++++------------------ > tests/libqtest.c | 60 > ++++++++++++++++++++++++++++++++++++++++++++++ > tests/libqtest.h | 19 +++++++++++++++ > tests/usb-hcd-uhci-test.c | 26 ++------------------ > tests/usb-hcd-xhci-test.c | 51 ++++----------------------------------- > tests/virtio-scsi-test.c | 24 ++----------------- > tests/virtio-serial-test.c | 25 +++---------------- > 8 files changed, 98 insertions(+), 156 deletions(-) > > diff --git a/tests/libqtest.c b/tests/libqtest.c > index b9a1f18..4339d97 100644 > --- a/tests/libqtest.c > +++ b/tests/libqtest.c > @@ -987,3 +987,63 @@ 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_hot_plug_device(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); > + while (qdict_haskey(response, "event")) { > + /* We can get DEVICE_DELETED events in case something went wrong */ > + g_assert_cmpstr(qdict_get_str(response, "event"), !=, > "DEVICE_DELETED"); Is there other stuff we should check for? > + QDECREF(response); > + response = qmp(""); > + g_assert(response); > + } > + g_assert(!qdict_haskey(response, "error")); > + QDECREF(response); > +} > + > +/** > + * Generic hot-unplugging test via the device_del QMP command > + */ > +void qtest_hot_unplug_device(const char *id) > +{ > + QDict *response; > + char *cmd; > + > + cmd = g_strdup_printf("{'execute': 'device_del'," > + " 'arguments': { 'id': '%s' }}", id); > + > + response = qmp(cmd); > + g_free(cmd); > + g_assert(response); > + while (qdict_haskey(response, "event")) { > + /* We should get DEVICE_DELETED event first */ > + g_assert_cmpstr(qdict_get_str(response, "event"), ==, > "DEVICE_DELETED"); 'should' does not sound assert-worthy to me :) Is this an expected event? > + QDECREF(response); > + response = qmp(""); > + g_assert(response); > + } > + g_assert(!qdict_haskey(response, "error")); > + QDECREF(response); > +}