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 static void drive_add(void)
20 char *resp
= hmp("drive_add 0 if=none,id=drive0");
22 g_assert_cmpstr(resp
, ==, "OK\r\n");
26 static void drive_del(void)
28 char *resp
= hmp("drive_del drive0");
30 g_assert_cmpstr(resp
, ==, "");
34 static void device_del(void)
38 /* Complication: ignore DEVICE_DELETED event */
39 qmp_discard_response("{'execute': 'device_del',"
40 " 'arguments': { 'id': 'dev0' } }");
41 response
= qmp_receive();
43 g_assert(qdict_haskey(response
, "return"));
47 static void test_drive_without_dev(void)
49 /* Start with an empty drive */
50 qtest_start("-drive if=none,id=drive0");
52 /* Delete the drive */
55 /* Ensure re-adding the drive works - there should be no duplicate ID error
56 * because the old drive must be gone.
63 static void test_after_failed_device_add(void)
68 qtest_start("-drive if=none,id=drive0");
70 /* Make device_add fail. If this leaks the virtio-blk device then a
71 * reference to drive0 will also be held (via qdev properties).
73 response
= qmp("{'execute': 'device_add',"
75 " 'driver': 'virtio-blk-%s',"
77 "}}", qvirtio_get_dev_type());
79 error
= qdict_get_qdict(response
, "error");
80 g_assert_cmpstr(qdict_get_try_str(error
, "class"), ==, "GenericError");
83 /* Delete the drive */
86 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
87 * virtio-blk device exists that holds a reference to the old drive0.
94 static void test_drive_del_device_del(void)
98 /* Start with a drive used by a device that unplugs instantaneously */
99 args
= g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
100 " -device virtio-scsi-%s"
101 " -device scsi-hd,drive=drive0,id=dev0",
102 qvirtio_get_dev_type());
106 * Delete the drive, and then the device
107 * Doing it in this order takes notoriously tricky special paths
116 int main(int argc
, char **argv
)
118 const char *arch
= qtest_get_arch();
120 g_test_init(&argc
, &argv
, NULL
);
122 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
124 /* TODO I guess any arch with a hot-pluggable virtio bus would do */
125 if (!strcmp(arch
, "i386") || !strcmp(arch
, "x86_64") ||
126 !strcmp(arch
, "ppc") || !strcmp(arch
, "ppc64") ||
127 !strcmp(arch
, "s390x")) {
128 qtest_add_func("/drive_del/after_failed_device_add",
129 test_after_failed_device_add
);
130 qtest_add_func("/blockdev/drive_del_device_del",
131 test_drive_del_device_del
);