]> git.proxmox.com Git - mirror_qemu.git/blob - tests/drive_del-test.c
tests: add qmp_assert_error_class()
[mirror_qemu.git] / tests / drive_del-test.c
1 /*
2 * blockdev.c test cases
3 *
4 * Copyright (C) 2013-2014 Red Hat Inc.
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
17
18 /* TODO actually test the results and get rid of this */
19 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
20
21 static void drive_add(void)
22 {
23 char *resp = hmp("drive_add 0 if=none,id=drive0");
24
25 g_assert_cmpstr(resp, ==, "OK\r\n");
26 g_free(resp);
27 }
28
29 static void drive_del(void)
30 {
31 char *resp = hmp("drive_del drive0");
32
33 g_assert_cmpstr(resp, ==, "");
34 g_free(resp);
35 }
36
37 static void device_del(void)
38 {
39 QDict *response;
40
41 /* Complication: ignore DEVICE_DELETED event */
42 qmp_discard_response("{'execute': 'device_del',"
43 " 'arguments': { 'id': 'dev0' } }");
44 response = qmp_receive();
45 g_assert(response);
46 g_assert(qdict_haskey(response, "return"));
47 qobject_unref(response);
48 }
49
50 static void test_drive_without_dev(void)
51 {
52 /* Start with an empty drive */
53 qtest_start("-drive if=none,id=drive0");
54
55 /* Delete the drive */
56 drive_del();
57
58 /* Ensure re-adding the drive works - there should be no duplicate ID error
59 * because the old drive must be gone.
60 */
61 drive_add();
62
63 qtest_end();
64 }
65
66 static void test_after_failed_device_add(void)
67 {
68 char driver[32];
69 QDict *response;
70
71 snprintf(driver, sizeof(driver), "virtio-blk-%s",
72 qvirtio_get_dev_type());
73
74 qtest_start("-drive if=none,id=drive0");
75
76 /* Make device_add fail. If this leaks the virtio-blk device then a
77 * reference to drive0 will also be held (via qdev properties).
78 */
79 response = qmp("{'execute': 'device_add',"
80 " 'arguments': {"
81 " 'driver': %s,"
82 " 'drive': 'drive0'"
83 "}}", driver);
84 g_assert(response);
85 qmp_assert_error_class(response, "GenericError");
86
87 /* Delete the drive */
88 drive_del();
89
90 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
91 * virtio-blk device exists that holds a reference to the old drive0.
92 */
93 drive_add();
94
95 qtest_end();
96 }
97
98 static void test_drive_del_device_del(void)
99 {
100 char *args;
101
102 /* Start with a drive used by a device that unplugs instantaneously */
103 args = g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
104 " -device virtio-scsi-%s"
105 " -device scsi-hd,drive=drive0,id=dev0",
106 qvirtio_get_dev_type());
107 qtest_start(args);
108
109 /*
110 * Delete the drive, and then the device
111 * Doing it in this order takes notoriously tricky special paths
112 */
113 drive_del();
114 device_del();
115
116 qtest_end();
117 g_free(args);
118 }
119
120 int main(int argc, char **argv)
121 {
122 const char *arch = qtest_get_arch();
123
124 g_test_init(&argc, &argv, NULL);
125
126 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
127
128 /* TODO I guess any arch with a hot-pluggable virtio bus would do */
129 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64") ||
130 !strcmp(arch, "ppc") || !strcmp(arch, "ppc64") ||
131 !strcmp(arch, "s390x")) {
132 qtest_add_func("/drive_del/after_failed_device_add",
133 test_after_failed_device_add);
134 qtest_add_func("/blockdev/drive_del_device_del",
135 test_drive_del_device_del);
136 }
137
138 return g_test_run();
139 }