1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "libqtest.h"
15
16static void drive_add(void)
17{
18 char *resp = hmp("drive_add 0 if=none,id=drive0");
19
20 g_assert_cmpstr(resp, ==, "OK\r\n");
21 g_free(resp);
22}
23
24static void drive_del(void)
25{
26 char *resp = hmp("drive_del drive0");
27
28 g_assert_cmpstr(resp, ==, "");
29 g_free(resp);
30}
31
32static void device_del(void)
33{
34 QDict *response;
35
36
37 qmp_discard_response("{'execute': 'device_del',"
38 " 'arguments': { 'id': 'dev0' } }");
39 response = qmp_receive();
40 g_assert(response);
41 g_assert(qdict_haskey(response, "return"));
42 QDECREF(response);
43}
44
45static void test_drive_without_dev(void)
46{
47
48 qtest_start("-drive if=none,id=drive0");
49
50
51 drive_del();
52
53
54
55
56 drive_add();
57
58 qtest_end();
59}
60
61static void test_after_failed_device_add(void)
62{
63 QDict *response;
64 QDict *error;
65
66 qtest_start("-drive if=none,id=drive0");
67
68
69
70
71 response = qmp("{'execute': 'device_add',"
72 " 'arguments': {"
73 " 'driver': 'virtio-blk-pci',"
74 " 'drive': 'drive0'"
75 "}}");
76 g_assert(response);
77 error = qdict_get_qdict(response, "error");
78 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
79 QDECREF(response);
80
81
82 drive_del();
83
84
85
86
87 drive_add();
88
89 qtest_end();
90}
91
92static void test_drive_del_device_del(void)
93{
94
95 qtest_start("-drive if=none,id=drive0,file=/dev/null,format=raw"
96 " -device virtio-scsi-pci"
97 " -device scsi-hd,drive=drive0,id=dev0");
98
99
100
101
102
103 drive_del();
104 device_del();
105
106 qtest_end();
107}
108
109int main(int argc, char **argv)
110{
111 const char *arch = qtest_get_arch();
112
113 g_test_init(&argc, &argv, NULL);
114
115 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
116
117
118 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64") ||
119 !strcmp(arch, "ppc") || !strcmp(arch, "ppc64")) {
120 qtest_add_func("/drive_del/after_failed_device_add",
121 test_after_failed_device_add);
122 qtest_add_func("/blockdev/drive_del_device_del",
123 test_drive_del_device_del);
124 }
125
126 return g_test_run();
127}
128