tests/libqtest: Make qtest_qmp_device_add/del independent from global_qtest

Generic library functions like qtest_qmp_device_add() and _del()
should not depend on the global_qtest variable. Pass the test
state via parameter instead.

Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20190813093047.27948-6-thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
This commit is contained in:
Thomas Huth 2019-07-22 17:10:55 +02:00
parent 17de474129
commit e5758de4e8
16 changed files with 66 additions and 52 deletions

View File

@ -77,18 +77,19 @@ static void test_plug_with_device_add_x86(gconstpointer data)
const PlugTestData *td = data;
char *args;
unsigned int s, c, t;
QTestState *qts;
args = g_strdup_printf("-machine %s -cpu %s "
"-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
td->machine, td->cpu_model,
td->sockets, td->cores, td->threads, td->maxcpus);
qtest_start(args);
qts = qtest_init(args);
for (s = 1; s < td->sockets; s++) {
for (c = 0; c < td->cores; c++) {
for (t = 0; t < td->threads; t++) {
char *id = g_strdup_printf("id-%i-%i-%i", s, c, t);
qtest_qmp_device_add(td->device_model, id,
qtest_qmp_device_add(qts, td->device_model, id,
"{'socket-id':%u, 'core-id':%u,"
" 'thread-id':%u}",
s, c, t);
@ -97,7 +98,7 @@ static void test_plug_with_device_add_x86(gconstpointer data)
}
}
qtest_end();
qtest_quit(qts);
g_free(args);
}
@ -106,20 +107,22 @@ static void test_plug_with_device_add_coreid(gconstpointer data)
const PlugTestData *td = data;
char *args;
unsigned int c;
QTestState *qts;
args = g_strdup_printf("-machine %s -cpu %s "
"-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
td->machine, td->cpu_model,
td->sockets, td->cores, td->threads, td->maxcpus);
qtest_start(args);
qts = qtest_init(args);
for (c = 1; c < td->cores; c++) {
char *id = g_strdup_printf("id-%i", c);
qtest_qmp_device_add(td->device_model, id, "{'core-id':%u}", c);
qtest_qmp_device_add(qts, td->device_model, id,
"{'core-id':%u}", c);
g_free(id);
}
qtest_end();
qtest_quit(qts);
g_free(args);
}

View File

@ -235,7 +235,7 @@ static void test_e1000e_hotplug(void *obj, void *data, QGuestAllocator * alloc)
{
QTestState *qts = global_qtest; /* TODO: get rid of global_qtest here */
qtest_qmp_device_add("e1000e", "e1000e_net", "{'addr': '0x06'}");
qtest_qmp_device_add(qts, "e1000e", "e1000e_net", "{'addr': '0x06'}");
qpci_unplug_acpi_device_test(qts, "e1000e_net", 0x06);
}

View File

@ -389,7 +389,7 @@ static void test_ivshmem_hotplug(void)
qts = qtest_init("-object memory-backend-ram,size=1M,id=mb1");
global_qtest = qts; /* TODO: Get rid of global_qtest here */
qtest_qmp_device_add("ivshmem-plain", "iv1",
qtest_qmp_device_add(qts, "ivshmem-plain", "iv1",
"{'addr': %s, 'memdev': 'mb1'}",
stringify(PCI_SLOT_HP));
if (strcmp(arch, "ppc64") != 0) {

View File

@ -37,20 +37,20 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
g_assert((value & mask) == (expect & mask));
}
void usb_test_hotplug(const char *hcd_id, const char *port,
void usb_test_hotplug(QTestState *qts, const char *hcd_id, const char *port,
void (*port_check)(void))
{
char *id = g_strdup_printf("usbdev%s", port);
char *bus = g_strdup_printf("%s.0", hcd_id);
qtest_qmp_device_add("usb-tablet", id, "{'port': %s, 'bus': %s}",
qtest_qmp_device_add(qts, "usb-tablet", id, "{'port': %s, 'bus': %s}",
port, bus);
if (port_check) {
port_check();
}
qtest_qmp_device_del(id);
qtest_qmp_device_del(qts, id);
g_free(bus);
g_free(id);

View File

@ -13,6 +13,6 @@ void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc,
void uhci_port_test(struct qhc *hc, int port, uint16_t expect);
void uhci_deinit(struct qhc *hc);
void usb_test_hotplug(const char *bus_name, const char *port,
void usb_test_hotplug(QTestState *qts, const char *bus_name, const char *port,
void (*port_check)(void));
#endif

View File

@ -1246,7 +1246,7 @@ QDict *qtest_qmp_receive_success(QTestState *s,
/*
* Generic hot-plugging test via the device_add QMP command.
*/
void qtest_qmp_device_add(const char *driver, const char *id,
void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
const char *fmt, ...)
{
QDict *args, *response;
@ -1260,7 +1260,8 @@ void qtest_qmp_device_add(const char *driver, const char *id,
qdict_put_str(args, "driver", driver);
qdict_put_str(args, "id", id);
response = qmp("{'execute': 'device_add', 'arguments': %p}", args);
response = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}",
args);
g_assert(response);
g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
g_assert(!qdict_haskey(response, "error"));
@ -1293,19 +1294,17 @@ static void device_deleted_cb(void *opaque, const char *name, QDict *data)
*
* But the order of arrival may vary - so we've got to detect both.
*/
void qtest_qmp_device_del(const char *id)
void qtest_qmp_device_del(QTestState *qts, const char *id)
{
bool got_event = false;
QDict *rsp;
qtest_qmp_send(global_qtest,
"{'execute': 'device_del', 'arguments': {'id': %s}}",
qtest_qmp_send(qts, "{'execute': 'device_del', 'arguments': {'id': %s}}",
id);
rsp = qtest_qmp_receive_success(global_qtest, device_deleted_cb,
&got_event);
rsp = qtest_qmp_receive_success(qts, device_deleted_cb, &got_event);
qobject_unref(rsp);
if (!got_event) {
rsp = qtest_qmp_receive(global_qtest);
rsp = qtest_qmp_receive(qts);
g_assert_cmpstr(qdict_get_try_str(rsp, "event"),
==, "DEVICE_DELETED");
qobject_unref(rsp);

View File

@ -946,6 +946,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
/**
* qtest_qmp_device_add:
* @qts: QTestState instance to operate on
* @driver: Name of the device that should be added
* @id: Identification string
* @fmt...: QMP message to send to qemu, formatted like
@ -954,16 +955,17 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
*
* Generic hot-plugging test via the device_add QMP command.
*/
void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
...) GCC_FMT_ATTR(3, 4);
void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
const char *fmt, ...) GCC_FMT_ATTR(4, 5);
/**
* qtest_qmp_device_del:
* @qts: QTestState instance to operate on
* @id: Identification string
*
* Generic hot-unplugging test via the device_del QMP command.
*/
void qtest_qmp_device_del(const char *id);
void qtest_qmp_device_del(QTestState *qts, const char *id);
/**
* qmp_rsp_is_err:

View File

@ -23,7 +23,7 @@ struct QOHCI_PCI {
static void test_ohci_hotplug(void *obj, void *data, QGuestAllocator *alloc)
{
usb_test_hotplug("ohci", "1", NULL);
usb_test_hotplug(global_qtest, "ohci", "1", NULL);
}
static void *ohci_pci_get_driver(void *obj, const char *interface)

View File

@ -43,14 +43,16 @@ static void test_port_2(void)
static void test_uhci_hotplug(void)
{
usb_test_hotplug("uhci", "2", test_port_2);
usb_test_hotplug(global_qtest, "uhci", "2", test_port_2);
}
static void test_usb_storage_hotplug(void)
{
qtest_qmp_device_add("usb-storage", "usbdev0", "{'drive': 'drive0'}");
QTestState *qts = global_qtest;
qtest_qmp_device_del("usbdev0");
qtest_qmp_device_add(qts, "usb-storage", "usbdev0", "{'drive': 'drive0'}");
qtest_qmp_device_del(qts, "usbdev0");
}
int main(int argc, char **argv)

View File

@ -18,30 +18,34 @@ static void test_xhci_init(void)
static void test_xhci_hotplug(void)
{
usb_test_hotplug("xhci", "1", NULL);
usb_test_hotplug(global_qtest, "xhci", "1", NULL);
}
static void test_usb_uas_hotplug(void)
{
qtest_qmp_device_add("usb-uas", "uas", "{}");
qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drive0'}");
QTestState *qts = global_qtest;
qtest_qmp_device_add(qts, "usb-uas", "uas", "{}");
qtest_qmp_device_add(qts, "scsi-hd", "scsihd", "{'drive': 'drive0'}");
/* TODO:
UAS HBA driver in libqos, to check that
added disk is visible after BUS rescan
*/
qtest_qmp_device_del("scsihd");
qtest_qmp_device_del("uas");
qtest_qmp_device_del(qts, "scsihd");
qtest_qmp_device_del(qts, "uas");
}
static void test_usb_ccid_hotplug(void)
{
qtest_qmp_device_add("usb-ccid", "ccid", "{}");
qtest_qmp_device_del("ccid");
QTestState *qts = global_qtest;
qtest_qmp_device_add(qts, "usb-ccid", "ccid", "{}");
qtest_qmp_device_del(qts, "ccid");
/* check the device can be added again */
qtest_qmp_device_add("usb-ccid", "ccid", "{}");
qtest_qmp_device_del("ccid");
qtest_qmp_device_add(qts, "usb-ccid", "ccid", "{}");
qtest_qmp_device_del(qts, "ccid");
}
int main(int argc, char **argv)

View File

@ -690,7 +690,7 @@ static void pci_hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
QTestState *qts = dev1->pdev->bus->qts;
/* plug secondary disk */
qtest_qmp_device_add("virtio-blk-pci", "drv1",
qtest_qmp_device_add(qts, "virtio-blk-pci", "drv1",
"{'addr': %s, 'drive': 'drive1'}",
stringify(PCI_SLOT_HP) ".0");

View File

@ -45,10 +45,12 @@ static void virtio_serial_nop(void)
static void virtio_serial_hotplug(void)
{
global_qtest = qtest_initf("-device virtio-serial-ccw");
qtest_qmp_device_add("virtserialport", "hp-port", "{}");
qtest_qmp_device_del("hp-port");
qtest_end();
QTestState *qts = qtest_initf("-device virtio-serial-ccw");
qtest_qmp_device_add(qts, "virtserialport", "hp-port", "{}");
qtest_qmp_device_del(qts, "hp-port");
qtest_quit(qts);
}
static void virtio_blk_nop(void)
@ -79,16 +81,16 @@ static void virtio_scsi_nop(void)
static void virtio_scsi_hotplug(void)
{
global_qtest = qtest_initf("-drive if=none,id=drv0,file=null-co://,"
"file.read-zeroes=on,format=raw "
QTestState *s = qtest_initf("-drive if=none,id=drv0,file=null-co://,"
"file.read-zeroes=on,format=raw "
"-drive if=none,id=drv1,file=null-co://,"
"file.read-zeroes=on,format=raw "
"-device virtio-scsi-ccw "
"-device scsi-hd,drive=drv0");
qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
qtest_qmp_device_del("scsihd");
qtest_qmp_device_add(s, "scsi-hd", "scsihd", "{'drive': 'drv1'}");
qtest_qmp_device_del(s, "scsihd");
qtest_end();
qtest_quit(s);
}
int main(int argc, char **argv)

View File

@ -174,7 +174,7 @@ static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
QTestState *qts = dev->pdev->bus->qts;
const char *arch = qtest_get_arch();
qtest_qmp_device_add("virtio-net-pci", "net1",
qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
"{'addr': %s}", stringify(PCI_SLOT_HP));
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {

View File

@ -22,7 +22,7 @@ static void rng_hotplug(void *obj, void *data, QGuestAllocator *alloc)
const char *arch = qtest_get_arch();
qtest_qmp_device_add("virtio-rng-pci", "rng1",
qtest_qmp_device_add(qts, "virtio-rng-pci", "rng1",
"{'addr': %s}", stringify(PCI_SLOT_HP));
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {

View File

@ -150,8 +150,10 @@ static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev)
static void hotplug(void *obj, void *data, QGuestAllocator *alloc)
{
qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
qtest_qmp_device_del("scsihd");
QTestState *qts = global_qtest;
qtest_qmp_device_add(qts, "scsi-hd", "scsihd", "{'drive': 'drv1'}");
qtest_qmp_device_del(qts, "scsihd");
}
/* Test WRITE SAME with the lba not aligned */

View File

@ -20,8 +20,8 @@ static void virtio_serial_nop(void *obj, void *data, QGuestAllocator *alloc)
static void serial_hotplug(void *obj, void *data, QGuestAllocator *alloc)
{
qtest_qmp_device_add("virtserialport", "hp-port", "{}");
qtest_qmp_device_del("hp-port");
qtest_qmp_device_add(global_qtest, "virtserialport", "hp-port", "{}");
qtest_qmp_device_del(global_qtest, "hp-port");
}
static void register_virtio_serial_test(void)