OSDN Git Service

Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-3.0-20180703' into staging
[qmiga/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 static void drive_add(void)
19 {
20     char *resp = hmp("drive_add 0 if=none,id=drive0");
21
22     g_assert_cmpstr(resp, ==, "OK\r\n");
23     g_free(resp);
24 }
25
26 static void drive_del(void)
27 {
28     char *resp = hmp("drive_del drive0");
29
30     g_assert_cmpstr(resp, ==, "");
31     g_free(resp);
32 }
33
34 static void device_del(void)
35 {
36     QDict *response;
37
38     /* Complication: ignore DEVICE_DELETED event */
39     qmp_discard_response("{'execute': 'device_del',"
40                          " 'arguments': { 'id': 'dev0' } }");
41     response = qmp_receive();
42     g_assert(response);
43     g_assert(qdict_haskey(response, "return"));
44     qobject_unref(response);
45 }
46
47 static void test_drive_without_dev(void)
48 {
49     /* Start with an empty drive */
50     qtest_start("-drive if=none,id=drive0");
51
52     /* Delete the drive */
53     drive_del();
54
55     /* Ensure re-adding the drive works - there should be no duplicate ID error
56      * because the old drive must be gone.
57      */
58     drive_add();
59
60     qtest_end();
61 }
62
63 static void test_after_failed_device_add(void)
64 {
65     QDict *response;
66     QDict *error;
67
68     qtest_start("-drive if=none,id=drive0");
69
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).
72      */
73     response = qmp("{'execute': 'device_add',"
74                    " 'arguments': {"
75                    "   'driver': 'virtio-blk-%s',"
76                    "   'drive': 'drive0'"
77                    "}}", qvirtio_get_dev_type());
78     g_assert(response);
79     error = qdict_get_qdict(response, "error");
80     g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
81     qobject_unref(response);
82
83     /* Delete the drive */
84     drive_del();
85
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.
88      */
89     drive_add();
90
91     qtest_end();
92 }
93
94 static void test_drive_del_device_del(void)
95 {
96     char *args;
97
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());
103     qtest_start(args);
104
105     /*
106      * Delete the drive, and then the device
107      * Doing it in this order takes notoriously tricky special paths
108      */
109     drive_del();
110     device_del();
111
112     qtest_end();
113     g_free(args);
114 }
115
116 int main(int argc, char **argv)
117 {
118     const char *arch = qtest_get_arch();
119
120     g_test_init(&argc, &argv, NULL);
121
122     qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
123
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);
132     }
133
134     return g_test_run();
135 }