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"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qlist.h"
19 static const char *qvirtio_get_dev_type(void);
21 static bool look_for_drive0(QTestState
*qts
, const char *command
, const char *key
)
28 response
= qtest_qmp(qts
, "{'execute': %s}", command
);
29 g_assert(response
&& qdict_haskey(response
, "return"));
30 ret
= qdict_get_qlist(response
, "return");
33 QLIST_FOREACH_ENTRY(ret
, entry
) {
34 QDict
*entry_dict
= qobject_to(QDict
, entry
->value
);
35 if (!strcmp(qdict_get_str(entry_dict
, key
), "drive0")) {
41 qobject_unref(response
);
46 * This covers the possible absence of a device due to QEMU build
49 static bool has_device_builtin(const char *dev
)
51 gchar
*device
= g_strdup_printf("%s-%s", dev
, qvirtio_get_dev_type());
52 bool rc
= qtest_has_device(device
);
58 static bool has_drive(QTestState
*qts
)
60 return look_for_drive0(qts
, "query-block", "device");
63 static bool has_blockdev(QTestState
*qts
)
65 return look_for_drive0(qts
, "query-named-block-nodes", "node-name");
68 static void blockdev_add_with_media(QTestState
*qts
)
72 response
= qtest_qmp(qts
,
73 "{ 'execute': 'blockdev-add',"
76 " 'node-name': 'drive0',"
78 " 'driver': 'null-co',"
79 " 'read-zeroes': true"
85 g_assert(qdict_haskey(response
, "return"));
86 qobject_unref(response
);
87 g_assert(has_blockdev(qts
));
90 static void drive_add(QTestState
*qts
)
92 char *resp
= qtest_hmp(qts
, "drive_add 0 if=none,id=drive0");
94 g_assert_cmpstr(resp
, ==, "OK\r\n");
95 g_assert(has_drive(qts
));
99 static void drive_add_with_media(QTestState
*qts
)
101 char *resp
= qtest_hmp(qts
,
102 "drive_add 0 if=none,id=drive0,file=null-co://,"
103 "file.read-zeroes=on,format=raw");
105 g_assert_cmpstr(resp
, ==, "OK\r\n");
106 g_assert(has_drive(qts
));
110 static void drive_del(QTestState
*qts
)
114 g_assert(has_drive(qts
));
115 resp
= qtest_hmp(qts
, "drive_del drive0");
116 g_assert_cmpstr(resp
, ==, "");
117 g_assert(!has_drive(qts
));
122 * qvirtio_get_dev_type:
123 * Returns: the preferred virtio bus/device type for the current architecture.
126 static const char *qvirtio_get_dev_type(void)
128 const char *arch
= qtest_get_arch();
130 if (g_str_equal(arch
, "arm") || g_str_equal(arch
, "aarch64")) {
131 return "device"; /* for virtio-mmio */
132 } else if (g_str_equal(arch
, "s390x")) {
139 static void device_add(QTestState
*qts
)
141 g_autofree
char *driver
= g_strdup_printf("virtio-blk-%s",
142 qvirtio_get_dev_type());
144 qtest_qmp(qts
, "{'execute': 'device_add',"
147 " 'drive': 'drive0',"
151 g_assert(qdict_haskey(response
, "return"));
152 qobject_unref(response
);
155 static void device_del(QTestState
*qts
, bool and_reset
)
159 qtest_qmp_device_del_send(qts
, "dev0");
162 response
= qtest_qmp(qts
, "{'execute': 'system_reset' }");
164 g_assert(qdict_haskey(response
, "return"));
165 qobject_unref(response
);
168 qtest_qmp_eventwait(qts
, "DEVICE_DELETED");
171 static void test_drive_without_dev(void)
175 /* Start with an empty drive */
176 qts
= qtest_init("-drive if=none,id=drive0");
178 /* Delete the drive */
181 /* Ensure re-adding the drive works - there should be no duplicate ID error
182 * because the old drive must be gone.
189 static void test_after_failed_device_add(void)
195 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
196 qvirtio_get_dev_type());
198 qts
= qtest_init("-drive if=none,id=drive0");
200 /* Make device_add fail. If this leaks the virtio-blk device then a
201 * reference to drive0 will also be held (via qdev properties).
203 response
= qtest_qmp(qts
, "{'execute': 'device_add',"
209 qmp_expect_error_and_unref(response
, "GenericError");
211 /* Delete the drive */
214 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
215 * virtio-blk device exists that holds a reference to the old drive0.
222 static void test_drive_del_device_del(void)
226 if (!has_device_builtin("virtio-scsi")) {
227 g_test_skip("Device virtio-scsi is not available");
231 /* Start with a drive used by a device that unplugs instantaneously */
232 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
233 "file.read-zeroes=on,format=raw"
234 " -device virtio-scsi-%s"
235 " -device scsi-hd,drive=drive0,id=dev0",
236 qvirtio_get_dev_type());
239 * Delete the drive, and then the device
240 * Doing it in this order takes notoriously tricky special paths
243 device_del(qts
, false);
244 g_assert(!has_drive(qts
));
249 static void test_cli_device_del(void)
252 const char *arch
= qtest_get_arch();
253 const char *machine_addition
= "";
255 if (!has_device_builtin("virtio-blk")) {
256 g_test_skip("Device virtio-blk is not available");
260 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
261 machine_addition
= "-machine pc";
265 * -drive/-device and device_del. Start with a drive used by a
266 * device that unplugs after reset.
268 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
269 "file.read-zeroes=on,format=raw"
270 " -device virtio-blk-%s,drive=drive0,id=dev0",
272 qvirtio_get_dev_type());
274 device_del(qts
, true);
275 g_assert(!has_drive(qts
));
280 static void test_cli_device_del_q35(void)
284 if (!has_device_builtin("virtio-blk")) {
285 g_test_skip("Device virtio-blk is not available");
290 * -drive/-device and device_del. Start with a drive used by a
291 * device that unplugs after reset.
293 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
294 "file.read-zeroes=on,format=raw "
295 "-machine q35 -device pcie-root-port,id=p1 "
296 "-device pcie-pci-bridge,bus=p1,id=b1 "
297 "-device virtio-blk-%s,drive=drive0,bus=b1,id=dev0",
298 qvirtio_get_dev_type());
300 device_del(qts
, true);
301 g_assert(!has_drive(qts
));
306 static void test_empty_device_del(void)
310 if (!has_device_builtin("virtio-scsi")) {
311 g_test_skip("Device virtio-scsi is not available");
315 /* device_del with no drive plugged. */
316 qts
= qtest_initf("-device virtio-scsi-%s -device scsi-cd,id=dev0",
317 qvirtio_get_dev_type());
319 device_del(qts
, false);
323 static void test_device_add_and_del(void)
326 const char *arch
= qtest_get_arch();
327 const char *machine_addition
= "";
329 if (!has_device_builtin("virtio-blk")) {
330 g_test_skip("Device virtio-blk is not available");
334 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
335 machine_addition
= "-machine pc";
339 * -drive/device_add and device_del. Start with a drive used by a
340 * device that unplugs after reset.
342 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
343 "file.read-zeroes=on,format=raw", machine_addition
);
346 device_del(qts
, true);
347 g_assert(!has_drive(qts
));
352 static void device_add_q35(QTestState
*qts
)
354 g_autofree
char *driver
= g_strdup_printf("virtio-blk-%s",
355 qvirtio_get_dev_type());
357 qtest_qmp(qts
, "{'execute': 'device_add',"
360 " 'drive': 'drive0',"
365 g_assert(qdict_haskey(response
, "return"));
366 qobject_unref(response
);
369 static void test_device_add_and_del_q35(void)
373 if (!has_device_builtin("virtio-blk")) {
374 g_test_skip("Device virtio-blk is not available");
379 * -drive/device_add and device_del. Start with a drive used by a
380 * device that unplugs after reset.
382 qts
= qtest_initf("-machine q35 -device pcie-root-port,id=p1 "
383 "-device pcie-pci-bridge,bus=p1,id=b1 "
384 "-drive if=none,id=drive0,file=null-co://,"
385 "file.read-zeroes=on,format=raw");
388 device_del(qts
, true);
389 g_assert(!has_drive(qts
));
394 static void test_drive_add_device_add_and_del(void)
397 const char *arch
= qtest_get_arch();
398 const char *machine_addition
= "";
400 if (!has_device_builtin("virtio-blk")) {
401 g_test_skip("Device virtio-blk is not available");
405 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
406 machine_addition
= "-machine pc";
409 qts
= qtest_init(machine_addition
);
412 * drive_add/device_add and device_del. The drive is used by a
413 * device that unplugs after reset.
415 drive_add_with_media(qts
);
417 device_del(qts
, true);
418 g_assert(!has_drive(qts
));
423 static void test_drive_add_device_add_and_del_q35(void)
427 if (!has_device_builtin("virtio-blk")) {
428 g_test_skip("Device virtio-blk is not available");
432 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
433 "-device pcie-pci-bridge,bus=p1,id=b1");
436 * drive_add/device_add and device_del. The drive is used by a
437 * device that unplugs after reset.
439 drive_add_with_media(qts
);
441 device_del(qts
, true);
442 g_assert(!has_drive(qts
));
447 static void test_blockdev_add_device_add_and_del(void)
450 const char *arch
= qtest_get_arch();
451 const char *machine_addition
= "";
453 if (!has_device_builtin("virtio-blk")) {
454 g_test_skip("Device virtio-blk is not available");
458 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
459 machine_addition
= "-machine pc";
462 qts
= qtest_init(machine_addition
);
465 * blockdev_add/device_add and device_del. The drive is used by a
466 * device that unplugs after reset, but it doesn't go away.
468 blockdev_add_with_media(qts
);
470 device_del(qts
, true);
471 g_assert(has_blockdev(qts
));
476 static void test_blockdev_add_device_add_and_del_q35(void)
480 if (!has_device_builtin("virtio-blk")) {
481 g_test_skip("Device virtio-blk is not available");
485 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
486 "-device pcie-pci-bridge,bus=p1,id=b1");
489 * blockdev_add/device_add and device_del. The drive is used by a
490 * device that unplugs after reset, but it doesn't go away.
492 blockdev_add_with_media(qts
);
494 device_del(qts
, true);
495 g_assert(has_blockdev(qts
));
500 int main(int argc
, char **argv
)
502 g_test_init(&argc
, &argv
, NULL
);
504 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
506 if (qvirtio_get_dev_type() != NULL
) {
507 qtest_add_func("/drive_del/after_failed_device_add",
508 test_after_failed_device_add
);
509 qtest_add_func("/drive_del/drive_del_device_del",
510 test_drive_del_device_del
);
511 qtest_add_func("/device_del/drive/cli_device",
512 test_cli_device_del
);
513 qtest_add_func("/device_del/drive/device_add",
514 test_device_add_and_del
);
515 qtest_add_func("/device_del/drive/drive_add_device_add",
516 test_drive_add_device_add_and_del
);
517 qtest_add_func("/device_del/empty",
518 test_empty_device_del
);
519 qtest_add_func("/device_del/blockdev",
520 test_blockdev_add_device_add_and_del
);
522 if (qtest_has_machine("q35")) {
523 qtest_add_func("/device_del/drive/cli_device_q35",
524 test_cli_device_del_q35
);
525 qtest_add_func("/device_del/drive/device_add_q35",
526 test_device_add_and_del_q35
);
527 qtest_add_func("/device_del/drive/drive_add_device_add_q35",
528 test_drive_add_device_add_and_del_q35
);
529 qtest_add_func("/device_del/blockdev_q35",
530 test_blockdev_add_device_add_and_del_q35
);