qtest: switch users back to qtest_qmp_receive
[qemu/ar7.git] / tests / qtest / drive_del-test.c
blob9d20a1ed8b48f86abc594b1aa60db1b8fc72e05e
1 /*
2 * blockdev.c test cases
4 * Copyright (C) 2013-2014 Red Hat Inc.
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "libqos/libqtest.h"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
18 static void drive_add(QTestState *qts)
20 char *resp = qtest_hmp(qts, "drive_add 0 if=none,id=drive0");
22 g_assert_cmpstr(resp, ==, "OK\r\n");
23 g_free(resp);
26 static void drive_del(QTestState *qts)
28 char *resp = qtest_hmp(qts, "drive_del drive0");
30 g_assert_cmpstr(resp, ==, "");
31 g_free(resp);
34 static void device_del(QTestState *qts)
36 QDict *response;
38 response = qtest_qmp(qts, "{'execute': 'device_del',"
39 " 'arguments': { 'id': 'dev0' } }");
40 g_assert(response);
41 g_assert(qdict_haskey(response, "return"));
42 qobject_unref(response);
44 qtest_qmp_eventwait(qts, "DEVICE_DELETED");
47 static void test_drive_without_dev(void)
49 QTestState *qts;
51 /* Start with an empty drive */
52 qts = qtest_init("-drive if=none,id=drive0");
54 /* Delete the drive */
55 drive_del(qts);
57 /* Ensure re-adding the drive works - there should be no duplicate ID error
58 * because the old drive must be gone.
60 drive_add(qts);
62 qtest_quit(qts);
66 * qvirtio_get_dev_type:
67 * Returns: the preferred virtio bus/device type for the current architecture.
68 * TODO: delete this
70 static const char *qvirtio_get_dev_type(void)
72 const char *arch = qtest_get_arch();
74 if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
75 return "device"; /* for virtio-mmio */
76 } else if (g_str_equal(arch, "s390x")) {
77 return "ccw";
78 } else {
79 return "pci";
83 static void test_after_failed_device_add(void)
85 char driver[32];
86 QDict *response;
87 QTestState *qts;
89 snprintf(driver, sizeof(driver), "virtio-blk-%s",
90 qvirtio_get_dev_type());
92 qts = qtest_init("-drive if=none,id=drive0");
94 /* Make device_add fail. If this leaks the virtio-blk device then a
95 * reference to drive0 will also be held (via qdev properties).
97 response = qtest_qmp(qts, "{'execute': 'device_add',"
98 " 'arguments': {"
99 " 'driver': %s,"
100 " 'drive': 'drive0'"
101 "}}", driver);
102 g_assert(response);
103 qmp_expect_error_and_unref(response, "GenericError");
105 /* Delete the drive */
106 drive_del(qts);
108 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
109 * virtio-blk device exists that holds a reference to the old drive0.
111 drive_add(qts);
113 qtest_quit(qts);
116 static void test_drive_del_device_del(void)
118 QTestState *qts;
120 /* Start with a drive used by a device that unplugs instantaneously */
121 qts = qtest_initf("-drive if=none,id=drive0,file=null-co://,"
122 "file.read-zeroes=on,format=raw"
123 " -device virtio-scsi-%s"
124 " -device scsi-hd,drive=drive0,id=dev0",
125 qvirtio_get_dev_type());
128 * Delete the drive, and then the device
129 * Doing it in this order takes notoriously tricky special paths
131 drive_del(qts);
132 device_del(qts);
134 qtest_quit(qts);
137 int main(int argc, char **argv)
139 g_test_init(&argc, &argv, NULL);
141 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
143 if (qvirtio_get_dev_type() != NULL) {
144 qtest_add_func("/drive_del/after_failed_device_add",
145 test_after_failed_device_add);
146 qtest_add_func("/blockdev/drive_del_device_del",
147 test_drive_del_device_del);
150 return g_test_run();