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 -M none");
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 if (!has_device_builtin("virtio-blk")) {
196 g_test_skip("Device virtio-blk is not available");
200 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
201 qvirtio_get_dev_type());
203 qts
= qtest_init("-drive if=none,id=drive0");
205 /* Make device_add fail. If this leaks the virtio-blk device then a
206 * reference to drive0 will also be held (via qdev properties).
208 response
= qtest_qmp(qts
, "{'execute': 'device_add',"
214 qmp_expect_error_and_unref(response
, "GenericError");
216 /* Delete the drive */
219 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
220 * virtio-blk device exists that holds a reference to the old drive0.
227 static void test_drive_del_device_del(void)
231 if (!has_device_builtin("virtio-scsi")) {
232 g_test_skip("Device virtio-scsi is not available");
236 /* Start with a drive used by a device that unplugs instantaneously */
237 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
238 "file.read-zeroes=on,format=raw"
239 " -device virtio-scsi-%s"
240 " -device scsi-hd,drive=drive0,id=dev0",
241 qvirtio_get_dev_type());
244 * Delete the drive, and then the device
245 * Doing it in this order takes notoriously tricky special paths
248 device_del(qts
, false);
249 g_assert(!has_drive(qts
));
254 static void test_cli_device_del(void)
257 const char *arch
= qtest_get_arch();
258 const char *machine_addition
= "";
260 if (!has_device_builtin("virtio-blk")) {
261 g_test_skip("Device virtio-blk is not available");
265 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
266 machine_addition
= "-machine pc";
270 * -drive/-device and device_del. Start with a drive used by a
271 * device that unplugs after reset.
273 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
274 "file.read-zeroes=on,format=raw"
275 " -device virtio-blk-%s,drive=drive0,id=dev0",
277 qvirtio_get_dev_type());
279 device_del(qts
, true);
280 g_assert(!has_drive(qts
));
285 static void test_cli_device_del_q35(void)
289 if (!has_device_builtin("virtio-blk")) {
290 g_test_skip("Device virtio-blk is not available");
295 * -drive/-device and device_del. Start with a drive used by a
296 * device that unplugs after reset.
298 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
299 "file.read-zeroes=on,format=raw "
300 "-machine q35 -device pcie-root-port,id=p1 "
301 "-device pcie-pci-bridge,bus=p1,id=b1 "
302 "-device virtio-blk-%s,drive=drive0,bus=b1,id=dev0",
303 qvirtio_get_dev_type());
305 device_del(qts
, true);
306 g_assert(!has_drive(qts
));
311 static void test_empty_device_del(void)
315 if (!has_device_builtin("virtio-scsi")) {
316 g_test_skip("Device virtio-scsi is not available");
320 /* device_del with no drive plugged. */
321 qts
= qtest_initf("-device virtio-scsi-%s -device scsi-cd,id=dev0",
322 qvirtio_get_dev_type());
324 device_del(qts
, false);
328 static void test_device_add_and_del(void)
331 const char *arch
= qtest_get_arch();
332 const char *machine_addition
= "";
334 if (!has_device_builtin("virtio-blk")) {
335 g_test_skip("Device virtio-blk is not available");
339 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
340 machine_addition
= "-machine pc";
344 * -drive/device_add and device_del. Start with a drive used by a
345 * device that unplugs after reset.
347 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
348 "file.read-zeroes=on,format=raw", machine_addition
);
351 device_del(qts
, true);
352 g_assert(!has_drive(qts
));
357 static void device_add_q35(QTestState
*qts
)
359 g_autofree
char *driver
= g_strdup_printf("virtio-blk-%s",
360 qvirtio_get_dev_type());
362 qtest_qmp(qts
, "{'execute': 'device_add',"
365 " 'drive': 'drive0',"
370 g_assert(qdict_haskey(response
, "return"));
371 qobject_unref(response
);
374 static void test_device_add_and_del_q35(void)
378 if (!has_device_builtin("virtio-blk")) {
379 g_test_skip("Device virtio-blk is not available");
384 * -drive/device_add and device_del. Start with a drive used by a
385 * device that unplugs after reset.
387 qts
= qtest_initf("-machine q35 -device pcie-root-port,id=p1 "
388 "-device pcie-pci-bridge,bus=p1,id=b1 "
389 "-drive if=none,id=drive0,file=null-co://,"
390 "file.read-zeroes=on,format=raw");
393 device_del(qts
, true);
394 g_assert(!has_drive(qts
));
399 static void test_drive_add_device_add_and_del(void)
402 const char *arch
= qtest_get_arch();
403 const char *machine_addition
= "";
405 if (!has_device_builtin("virtio-blk")) {
406 g_test_skip("Device virtio-blk is not available");
410 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
411 machine_addition
= "-machine pc";
414 qts
= qtest_init(machine_addition
);
417 * drive_add/device_add and device_del. The drive is used by a
418 * device that unplugs after reset.
420 drive_add_with_media(qts
);
422 device_del(qts
, true);
423 g_assert(!has_drive(qts
));
428 static void test_drive_add_device_add_and_del_q35(void)
432 if (!has_device_builtin("virtio-blk")) {
433 g_test_skip("Device virtio-blk is not available");
437 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
438 "-device pcie-pci-bridge,bus=p1,id=b1");
441 * drive_add/device_add and device_del. The drive is used by a
442 * device that unplugs after reset.
444 drive_add_with_media(qts
);
446 device_del(qts
, true);
447 g_assert(!has_drive(qts
));
452 static void test_blockdev_add_device_add_and_del(void)
455 const char *arch
= qtest_get_arch();
456 const char *machine_addition
= "";
458 if (!has_device_builtin("virtio-blk")) {
459 g_test_skip("Device virtio-blk is not available");
463 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
464 machine_addition
= "-machine pc";
467 qts
= qtest_init(machine_addition
);
470 * blockdev_add/device_add and device_del. The drive is used by a
471 * device that unplugs after reset, but it doesn't go away.
473 blockdev_add_with_media(qts
);
475 device_del(qts
, true);
476 g_assert(has_blockdev(qts
));
481 static void test_blockdev_add_device_add_and_del_q35(void)
485 if (!has_device_builtin("virtio-blk")) {
486 g_test_skip("Device virtio-blk is not available");
490 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
491 "-device pcie-pci-bridge,bus=p1,id=b1");
494 * blockdev_add/device_add and device_del. The drive is used by a
495 * device that unplugs after reset, but it doesn't go away.
497 blockdev_add_with_media(qts
);
499 device_del(qts
, true);
500 g_assert(has_blockdev(qts
));
505 int main(int argc
, char **argv
)
507 g_test_init(&argc
, &argv
, NULL
);
509 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
511 if (qvirtio_get_dev_type() != NULL
) {
512 qtest_add_func("/drive_del/after_failed_device_add",
513 test_after_failed_device_add
);
514 qtest_add_func("/drive_del/drive_del_device_del",
515 test_drive_del_device_del
);
516 qtest_add_func("/device_del/drive/cli_device",
517 test_cli_device_del
);
518 qtest_add_func("/device_del/drive/device_add",
519 test_device_add_and_del
);
520 qtest_add_func("/device_del/drive/drive_add_device_add",
521 test_drive_add_device_add_and_del
);
522 qtest_add_func("/device_del/empty",
523 test_empty_device_del
);
524 qtest_add_func("/device_del/blockdev",
525 test_blockdev_add_device_add_and_del
);
527 if (qtest_has_machine("q35")) {
528 qtest_add_func("/device_del/drive/cli_device_q35",
529 test_cli_device_del_q35
);
530 qtest_add_func("/device_del/drive/device_add_q35",
531 test_device_add_and_del_q35
);
532 qtest_add_func("/device_del/drive/drive_add_device_add_q35",
533 test_drive_add_device_add_and_del_q35
);
534 qtest_add_func("/device_del/blockdev_q35",
535 test_blockdev_add_device_add_and_del_q35
);