tests: Introduce generic device hot-plug/hot-unplug functions

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 for device deletion in an incorrect way, for example, we've got
to deal with both, error code and DEVICE_DEL event here). So let's provide
some proper generic functions for adding and removing a device instead.

The code for correctly unplugging a device has been taken from a patch
from Peter Xu.

Reviewed-by: Peter Xu <peterx@redhat.com>
Tested-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
This commit is contained in:
Thomas Huth 2017-08-15 08:58:54 +02:00
parent 3dabde1128
commit acd80015fb
8 changed files with 112 additions and 155 deletions

View file

@ -40,34 +40,16 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
void usb_test_hotplug(const char *hcd_id, const int port,
void (*port_check)(void))
{
QDict *response;
char *cmd;
char *id = g_strdup_printf("usbdev%d", port);
cmd = g_strdup_printf("{'execute': 'device_add',"
" 'arguments': {"
" 'driver': 'usb-tablet',"
" 'port': '%d',"
" 'bus': '%s.0',"
" 'id': 'usbdev%d'"
"}}", port, hcd_id, port);
response = qmp(cmd);
g_free(cmd);
g_assert(response);
g_assert(!qdict_haskey(response, "error"));
QDECREF(response);
qtest_qmp_device_add("usb-tablet", id, "'port': '%d', 'bus': '%s.0'",
port, hcd_id);
if (port_check) {
port_check();
}
cmd = g_strdup_printf("{'execute': 'device_del',"
" 'arguments': {"
" 'id': 'usbdev%d'"
"}}", port);
response = qmp(cmd);
g_free(cmd);
g_assert(response);
g_assert(qdict_haskey(response, "event"));
g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
QDECREF(response);
qtest_qmp_device_del(id);
g_free(id);
}