virtio/vhost: reset dev->log after syncing
[qemu.git] / tests / drive_del-test.c
blobc9ac997555f13b3a53c86d2053205ec7163ce573
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 "libqtest.h"
15 #include "libqos/virtio.h"
17 static void drive_add(void)
19 char *resp = hmp("drive_add 0 if=none,id=drive0");
21 g_assert_cmpstr(resp, ==, "OK\r\n");
22 g_free(resp);
25 static void drive_del(void)
27 char *resp = hmp("drive_del drive0");
29 g_assert_cmpstr(resp, ==, "");
30 g_free(resp);
33 static void device_del(void)
35 QDict *response;
37 /* Complication: ignore DEVICE_DELETED event */
38 qmp_discard_response("{'execute': 'device_del',"
39 " 'arguments': { 'id': 'dev0' } }");
40 response = qmp_receive();
41 g_assert(response);
42 g_assert(qdict_haskey(response, "return"));
43 QDECREF(response);
46 static void test_drive_without_dev(void)
48 /* Start with an empty drive */
49 qtest_start("-drive if=none,id=drive0");
51 /* Delete the drive */
52 drive_del();
54 /* Ensure re-adding the drive works - there should be no duplicate ID error
55 * because the old drive must be gone.
57 drive_add();
59 qtest_end();
62 static void test_after_failed_device_add(void)
64 QDict *response;
65 QDict *error;
67 qtest_start("-drive if=none,id=drive0");
69 /* Make device_add fail. If this leaks the virtio-blk device then a
70 * reference to drive0 will also be held (via qdev properties).
72 response = qmp("{'execute': 'device_add',"
73 " 'arguments': {"
74 " 'driver': 'virtio-blk-%s',"
75 " 'drive': 'drive0'"
76 "}}", qvirtio_get_dev_type());
77 g_assert(response);
78 error = qdict_get_qdict(response, "error");
79 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
80 QDECREF(response);
82 /* Delete the drive */
83 drive_del();
85 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
86 * virtio-blk device exists that holds a reference to the old drive0.
88 drive_add();
90 qtest_end();
93 static void test_drive_del_device_del(void)
95 char *args;
97 /* Start with a drive used by a device that unplugs instantaneously */
98 args = g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
99 " -device virtio-scsi-%s"
100 " -device scsi-hd,drive=drive0,id=dev0",
101 qvirtio_get_dev_type());
102 qtest_start(args);
105 * Delete the drive, and then the device
106 * Doing it in this order takes notoriously tricky special paths
108 drive_del();
109 device_del();
111 qtest_end();
112 g_free(args);
115 int main(int argc, char **argv)
117 const char *arch = qtest_get_arch();
119 g_test_init(&argc, &argv, NULL);
121 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
123 /* TODO I guess any arch with a hot-pluggable virtio bus would do */
124 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64") ||
125 !strcmp(arch, "ppc") || !strcmp(arch, "ppc64") ||
126 !strcmp(arch, "s390x")) {
127 qtest_add_func("/drive_del/after_failed_device_add",
128 test_after_failed_device_add);
129 qtest_add_func("/blockdev/drive_del_device_del",
130 test_drive_del_device_del);
133 return g_test_run();