1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "libqtest.h"
15#include "libqos/virtio.h"
16
17static void drive_add(void)
18{
19 char *resp = hmp("drive_add 0 if=none,id=drive0");
20
21 g_assert_cmpstr(resp, ==, "OK\r\n");
22 g_free(resp);
23}
24
25static void drive_del(void)
26{
27 char *resp = hmp("drive_del drive0");
28
29 g_assert_cmpstr(resp, ==, "");
30 g_free(resp);
31}
32
33static void device_del(void)
34{
35 QDict *response;
36
37
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);
44}
45
46static void test_drive_without_dev(void)
47{
48
49 qtest_start("-drive if=none,id=drive0");
50
51
52 drive_del();
53
54
55
56
57 drive_add();
58
59 qtest_end();
60}
61
62static void test_after_failed_device_add(void)
63{
64 QDict *response;
65 QDict *error;
66
67 qtest_start("-drive if=none,id=drive0");
68
69
70
71
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);
81
82
83 drive_del();
84
85
86
87
88 drive_add();
89
90 qtest_end();
91}
92
93static void test_drive_del_device_del(void)
94{
95 char *args;
96
97
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);
103
104
105
106
107
108 drive_del();
109 device_del();
110
111 qtest_end();
112 g_free(args);
113}
114
115int main(int argc, char **argv)
116{
117 const char *arch = qtest_get_arch();
118
119 g_test_init(&argc, &argv, NULL);
120
121 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
122
123
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);
131 }
132
133 return g_test_run();
134}
135