2 * blockdev.c test cases
4 * Copyright (C) 2013-2014 Red Hat Inc.
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"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
18 /* TODO actually test the results and get rid of this */
19 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
21 static void drive_add(void)
23 char *resp
= hmp("drive_add 0 if=none,id=drive0");
25 g_assert_cmpstr(resp
, ==, "OK\r\n");
29 static void drive_del(void)
31 char *resp
= hmp("drive_del drive0");
33 g_assert_cmpstr(resp
, ==, "");
37 static void device_del(void)
41 /* Complication: ignore DEVICE_DELETED event */
42 qmp_discard_response("{'execute': 'device_del',"
43 " 'arguments': { 'id': 'dev0' } }");
44 response
= qmp_receive();
46 g_assert(qdict_haskey(response
, "return"));
47 qobject_unref(response
);
50 static void test_drive_without_dev(void)
52 /* Start with an empty drive */
53 qtest_start("-drive if=none,id=drive0");
55 /* Delete the drive */
58 /* Ensure re-adding the drive works - there should be no duplicate ID error
59 * because the old drive must be gone.
67 * qvirtio_get_dev_type:
68 * Returns: the preferred virtio bus/device type for the current architecture.
71 static const char *qvirtio_get_dev_type(void)
73 const char *arch
= qtest_get_arch();
75 if (g_str_equal(arch
, "arm") || g_str_equal(arch
, "aarch64")) {
76 return "device"; /* for virtio-mmio */
77 } else if (g_str_equal(arch
, "s390x")) {
84 static void test_after_failed_device_add(void)
89 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
90 qvirtio_get_dev_type());
92 qtest_start("-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
= qmp("{'execute': 'device_add',"
103 qmp_assert_error_class(response
, "GenericError");
105 /* Delete the drive */
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.
116 static void test_drive_del_device_del(void)
120 /* Start with a drive used by a device that unplugs instantaneously */
121 args
= g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
122 " -device virtio-scsi-%s"
123 " -device scsi-hd,drive=drive0,id=dev0",
124 qvirtio_get_dev_type());
128 * Delete the drive, and then the device
129 * Doing it in this order takes notoriously tricky special paths
138 int main(int argc
, char **argv
)
140 g_test_init(&argc
, &argv
, NULL
);
142 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
144 if (qvirtio_get_dev_type() != NULL
) {
145 qtest_add_func("/drive_del/after_failed_device_add",
146 test_after_failed_device_add
);
147 qtest_add_func("/blockdev/drive_del_device_del",
148 test_drive_del_device_del
);