2 * blockdev.c test cases
4 * Copyright (C) 2013-2014 Red Hat Inc.
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 "libqos/libqtest.h"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qlist.h"
19 static bool look_for_drive0(QTestState
*qts
, const char *command
, const char *key
)
26 response
= qtest_qmp(qts
, "{'execute': %s}", command
);
27 g_assert(response
&& qdict_haskey(response
, "return"));
28 ret
= qdict_get_qlist(response
, "return");
31 QLIST_FOREACH_ENTRY(ret
, entry
) {
32 QDict
*entry_dict
= qobject_to(QDict
, entry
->value
);
33 if (!strcmp(qdict_get_str(entry_dict
, key
), "drive0")) {
39 qobject_unref(response
);
43 static bool has_drive(QTestState
*qts
)
45 return look_for_drive0(qts
, "query-block", "device");
48 static bool has_blockdev(QTestState
*qts
)
50 return look_for_drive0(qts
, "query-named-block-nodes", "node-name");
53 static void blockdev_add_with_media(QTestState
*qts
)
57 response
= qtest_qmp(qts
,
58 "{ 'execute': 'blockdev-add',"
61 " 'node-name': 'drive0',"
63 " 'driver': 'null-co',"
64 " 'read-zeroes': true"
70 g_assert(qdict_haskey(response
, "return"));
71 qobject_unref(response
);
72 g_assert(has_blockdev(qts
));
75 static void drive_add(QTestState
*qts
)
77 char *resp
= qtest_hmp(qts
, "drive_add 0 if=none,id=drive0");
79 g_assert_cmpstr(resp
, ==, "OK\r\n");
80 g_assert(has_drive(qts
));
84 static void drive_add_with_media(QTestState
*qts
)
86 char *resp
= qtest_hmp(qts
,
87 "drive_add 0 if=none,id=drive0,file=null-co://,"
88 "file.read-zeroes=on,format=raw");
90 g_assert_cmpstr(resp
, ==, "OK\r\n");
91 g_assert(has_drive(qts
));
95 static void drive_del(QTestState
*qts
)
99 g_assert(has_drive(qts
));
100 resp
= qtest_hmp(qts
, "drive_del drive0");
101 g_assert_cmpstr(resp
, ==, "");
102 g_assert(!has_drive(qts
));
107 * qvirtio_get_dev_type:
108 * Returns: the preferred virtio bus/device type for the current architecture.
111 static const char *qvirtio_get_dev_type(void)
113 const char *arch
= qtest_get_arch();
115 if (g_str_equal(arch
, "arm") || g_str_equal(arch
, "aarch64")) {
116 return "device"; /* for virtio-mmio */
117 } else if (g_str_equal(arch
, "s390x")) {
124 static void device_add(QTestState
*qts
)
128 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
129 qvirtio_get_dev_type());
131 response
= qtest_qmp(qts
, "{'execute': 'device_add',"
134 " 'drive': 'drive0',"
138 g_assert(qdict_haskey(response
, "return"));
139 qobject_unref(response
);
142 static void device_del(QTestState
*qts
, bool and_reset
)
146 response
= qtest_qmp(qts
, "{'execute': 'device_del',"
147 " 'arguments': { 'id': 'dev0' } }");
149 g_assert(qdict_haskey(response
, "return"));
150 qobject_unref(response
);
153 response
= qtest_qmp(qts
, "{'execute': 'system_reset' }");
155 g_assert(qdict_haskey(response
, "return"));
156 qobject_unref(response
);
159 qtest_qmp_eventwait(qts
, "DEVICE_DELETED");
162 static void test_drive_without_dev(void)
166 /* Start with an empty drive */
167 qts
= qtest_init("-drive if=none,id=drive0");
169 /* Delete the drive */
172 /* Ensure re-adding the drive works - there should be no duplicate ID error
173 * because the old drive must be gone.
180 static void test_after_failed_device_add(void)
186 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
187 qvirtio_get_dev_type());
189 qts
= qtest_init("-drive if=none,id=drive0");
191 /* Make device_add fail. If this leaks the virtio-blk device then a
192 * reference to drive0 will also be held (via qdev properties).
194 response
= qtest_qmp(qts
, "{'execute': 'device_add',"
200 qmp_expect_error_and_unref(response
, "GenericError");
202 /* Delete the drive */
205 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
206 * virtio-blk device exists that holds a reference to the old drive0.
213 static void test_drive_del_device_del(void)
217 /* Start with a drive used by a device that unplugs instantaneously */
218 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
219 "file.read-zeroes=on,format=raw"
220 " -device virtio-scsi-%s"
221 " -device scsi-hd,drive=drive0,id=dev0",
222 qvirtio_get_dev_type());
225 * Delete the drive, and then the device
226 * Doing it in this order takes notoriously tricky special paths
229 device_del(qts
, false);
230 g_assert(!has_drive(qts
));
235 static void test_cli_device_del(void)
240 * -drive/-device and device_del. Start with a drive used by a
241 * device that unplugs after reset.
243 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
244 "file.read-zeroes=on,format=raw"
245 " -device virtio-blk-%s,drive=drive0,id=dev0",
246 qvirtio_get_dev_type());
248 device_del(qts
, true);
249 g_assert(!has_drive(qts
));
254 static void test_empty_device_del(void)
258 /* device_del with no drive plugged. */
259 qts
= qtest_initf("-device virtio-scsi-%s -device scsi-cd,id=dev0",
260 qvirtio_get_dev_type());
262 device_del(qts
, false);
266 static void test_device_add_and_del(void)
271 * -drive/device_add and device_del. Start with a drive used by a
272 * device that unplugs after reset.
274 qts
= qtest_init("-drive if=none,id=drive0,file=null-co://,"
275 "file.read-zeroes=on,format=raw");
278 device_del(qts
, true);
279 g_assert(!has_drive(qts
));
284 static void test_drive_add_device_add_and_del(void)
288 qts
= qtest_init("");
291 * drive_add/device_add and device_del. The drive is used by a
292 * device that unplugs after reset.
294 drive_add_with_media(qts
);
296 device_del(qts
, true);
297 g_assert(!has_drive(qts
));
302 static void test_blockdev_add_device_add_and_del(void)
306 qts
= qtest_init("");
309 * blockdev_add/device_add and device_del. The it drive is used by a
310 * device that unplugs after reset, but it doesn't go away.
312 blockdev_add_with_media(qts
);
314 device_del(qts
, true);
315 g_assert(has_blockdev(qts
));
320 int main(int argc
, char **argv
)
322 g_test_init(&argc
, &argv
, NULL
);
324 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
326 if (qvirtio_get_dev_type() != NULL
) {
327 qtest_add_func("/drive_del/after_failed_device_add",
328 test_after_failed_device_add
);
329 qtest_add_func("/drive_del/drive_del_device_del",
330 test_drive_del_device_del
);
331 qtest_add_func("/device_del/drive/cli_device",
332 test_cli_device_del
);
333 qtest_add_func("/device_del/drive/device_add",
334 test_device_add_and_del
);
335 qtest_add_func("/device_del/drive/drive_add_device_add",
336 test_drive_add_device_add_and_del
);
337 qtest_add_func("/device_del/empty",
338 test_empty_device_del
);
339 qtest_add_func("/device_del/blockdev",
340 test_blockdev_add_device_add_and_del
);