tests/libqtest: Make qtest_qmp_device_add/del independent from global_qtest
authorThomas Huth <thuth@redhat.com>
Mon, 22 Jul 2019 15:10:55 +0000 (17:10 +0200)
committerThomas Huth <thuth@redhat.com>
Thu, 15 Aug 2019 17:24:10 +0000 (19:24 +0200)
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>
16 files changed:
tests/cpu-plug-test.c
tests/e1000e-test.c
tests/ivshmem-test.c
tests/libqos/usb.c
tests/libqos/usb.h
tests/libqtest.c
tests/libqtest.h
tests/usb-hcd-ohci-test.c
tests/usb-hcd-uhci-test.c
tests/usb-hcd-xhci-test.c
tests/virtio-blk-test.c
tests/virtio-ccw-test.c
tests/virtio-net-test.c
tests/virtio-rng-test.c
tests/virtio-scsi-test.c
tests/virtio-serial-test.c

index 668f00144e7eafcb07558eedb38e3c1df158c1c1..3049620854ac42bd8f38ba518b1728855c03d471 100644 (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);
 }
 
index 445787a7e45f4254fd811fc43c7d8a22f9ca167b..93628c588d1024cf8a2072bdc10c0c78a631494a 100644 (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);
 }
 
index a467b8c03d0b69c3abee6b86dbe6c1200970e618..b76457948bc372eb620cc1ffe09aaabcb54a7df5 100644 (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) {
index 49e2f4bc0a9f2161940f113d84d7e3a207867914..d7a9cb3c72a4febd05d03e705f4b9f7da62d5b14 100644 (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);
index c506418a13968be5d9914e7a24eb2256bf276b01..eeced39a2f9f06a4e523548e43f2f18c3fe9d90d 100644 (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
index d1aead30ed3ca2ea7a2501e4e802d0200256b460..19c1fc2e977a5a539f6df52a956c679ffe39298a 100644 (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);
index 0009b657911ea0c06e15ba4def65acac58602429..d93144fb74a8c7151f8f52286dbace8702c3798b 100644 (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:
index c12b892085c824d1c0b034bf34c6c31c74fb59b7..0cd73b73639c33428168a74916370920e816149a 100644 (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)
index 6d355c29da6b34b19a6abf34b6d6a884d0256332..2eef8e3d1c402671867a731307463c07c595fdaf 100644 (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)
index b15a51123fc9418a454a72f155ffeb8791669743..01845371f97f3cab950a54465c51da84c34efa65 100644 (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)
index 882b08ae2948050d613f6989b7dc4d91b2413f72..982ff1538c0ae63abc5233d57469ae4d1930cc73 100644 (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");
 
index 6be4e6aaf6d10094115f76d7d734484b9988e0d6..9f445ef4adf458c624026b9c1fabd8e178dc5dd7 100644 (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)
index 1071dd82c9e5f74862d06fa0efb6fe90553cbd01..840875aaae004d2e55c7312fed793e2e1bf29395 100644 (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) {
index a38a0d175bb156251f577fe7d10e15212cd72613..092ba130684dfdc378265f17ba1d779aec75fa35 100644 (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) {
index 6393272149a4cd102370da1282bc72f2471df084..16481491e30a8223dee855625386c6c1adf7c00e 100644 (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 */
index 066ca61280fcfa79b22b6d8a9022e31476b55c9c..e584ad76e847a56ee51e4054aae372912b43d2c9 100644 (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)