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 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
)
126 g_autofree
char *driver
= g_strdup_printf("virtio-blk-%s",
127 qvirtio_get_dev_type());
129 qtest_qmp(qts
, "{'execute': 'device_add',"
132 " 'drive': 'drive0',"
136 g_assert(qdict_haskey(response
, "return"));
137 qobject_unref(response
);
140 static void device_del(QTestState
*qts
, bool and_reset
)
144 qtest_qmp_device_del_send(qts
, "dev0");
147 response
= qtest_qmp(qts
, "{'execute': 'system_reset' }");
149 g_assert(qdict_haskey(response
, "return"));
150 qobject_unref(response
);
153 qtest_qmp_eventwait(qts
, "DEVICE_DELETED");
156 static void test_drive_without_dev(void)
160 /* Start with an empty drive */
161 qts
= qtest_init("-drive if=none,id=drive0");
163 /* Delete the drive */
166 /* Ensure re-adding the drive works - there should be no duplicate ID error
167 * because the old drive must be gone.
174 static void test_after_failed_device_add(void)
180 snprintf(driver
, sizeof(driver
), "virtio-blk-%s",
181 qvirtio_get_dev_type());
183 qts
= qtest_init("-drive if=none,id=drive0");
185 /* Make device_add fail. If this leaks the virtio-blk device then a
186 * reference to drive0 will also be held (via qdev properties).
188 response
= qtest_qmp(qts
, "{'execute': 'device_add',"
194 qmp_expect_error_and_unref(response
, "GenericError");
196 /* Delete the drive */
199 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
200 * virtio-blk device exists that holds a reference to the old drive0.
207 static void test_drive_del_device_del(void)
211 /* Start with a drive used by a device that unplugs instantaneously */
212 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
213 "file.read-zeroes=on,format=raw"
214 " -device virtio-scsi-%s"
215 " -device scsi-hd,drive=drive0,id=dev0",
216 qvirtio_get_dev_type());
219 * Delete the drive, and then the device
220 * Doing it in this order takes notoriously tricky special paths
223 device_del(qts
, false);
224 g_assert(!has_drive(qts
));
229 static void test_cli_device_del(void)
232 const char *arch
= qtest_get_arch();
233 const char *machine_addition
= "";
235 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
236 machine_addition
= "-machine pc";
240 * -drive/-device and device_del. Start with a drive used by a
241 * device that unplugs after reset.
243 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
244 "file.read-zeroes=on,format=raw"
245 " -device virtio-blk-%s,drive=drive0,id=dev0",
247 qvirtio_get_dev_type());
249 device_del(qts
, true);
250 g_assert(!has_drive(qts
));
255 static void test_cli_device_del_q35(void)
260 * -drive/-device and device_del. Start with a drive used by a
261 * device that unplugs after reset.
263 qts
= qtest_initf("-drive if=none,id=drive0,file=null-co://,"
264 "file.read-zeroes=on,format=raw "
265 "-machine q35 -device pcie-root-port,id=p1 "
266 "-device pcie-pci-bridge,bus=p1,id=b1 "
267 "-device virtio-blk-%s,drive=drive0,bus=b1,id=dev0",
268 qvirtio_get_dev_type());
270 device_del(qts
, true);
271 g_assert(!has_drive(qts
));
276 static void test_empty_device_del(void)
280 /* device_del with no drive plugged. */
281 qts
= qtest_initf("-device virtio-scsi-%s -device scsi-cd,id=dev0",
282 qvirtio_get_dev_type());
284 device_del(qts
, false);
288 static void test_device_add_and_del(void)
291 const char *arch
= qtest_get_arch();
292 const char *machine_addition
= "";
294 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
295 machine_addition
= "-machine pc";
299 * -drive/device_add and device_del. Start with a drive used by a
300 * device that unplugs after reset.
302 qts
= qtest_initf("%s -drive if=none,id=drive0,file=null-co://,"
303 "file.read-zeroes=on,format=raw", machine_addition
);
306 device_del(qts
, true);
307 g_assert(!has_drive(qts
));
312 static void device_add_q35(QTestState
*qts
)
314 g_autofree
char *driver
= g_strdup_printf("virtio-blk-%s",
315 qvirtio_get_dev_type());
317 qtest_qmp(qts
, "{'execute': 'device_add',"
320 " 'drive': 'drive0',"
325 g_assert(qdict_haskey(response
, "return"));
326 qobject_unref(response
);
329 static void test_device_add_and_del_q35(void)
334 * -drive/device_add and device_del. Start with a drive used by a
335 * device that unplugs after reset.
337 qts
= qtest_initf("-machine q35 -device pcie-root-port,id=p1 "
338 "-device pcie-pci-bridge,bus=p1,id=b1 "
339 "-drive if=none,id=drive0,file=null-co://,"
340 "file.read-zeroes=on,format=raw");
343 device_del(qts
, true);
344 g_assert(!has_drive(qts
));
349 static void test_drive_add_device_add_and_del(void)
352 const char *arch
= qtest_get_arch();
353 const char *machine_addition
= "";
355 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
356 machine_addition
= "-machine pc";
359 qts
= qtest_init(machine_addition
);
362 * drive_add/device_add and device_del. The drive is used by a
363 * device that unplugs after reset.
365 drive_add_with_media(qts
);
367 device_del(qts
, true);
368 g_assert(!has_drive(qts
));
373 static void test_drive_add_device_add_and_del_q35(void)
377 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
378 "-device pcie-pci-bridge,bus=p1,id=b1");
381 * drive_add/device_add and device_del. The drive is used by a
382 * device that unplugs after reset.
384 drive_add_with_media(qts
);
386 device_del(qts
, true);
387 g_assert(!has_drive(qts
));
392 static void test_blockdev_add_device_add_and_del(void)
395 const char *arch
= qtest_get_arch();
396 const char *machine_addition
= "";
398 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
399 machine_addition
= "-machine pc";
402 qts
= qtest_init(machine_addition
);
405 * blockdev_add/device_add and device_del. The drive is used by a
406 * device that unplugs after reset, but it doesn't go away.
408 blockdev_add_with_media(qts
);
410 device_del(qts
, true);
411 g_assert(has_blockdev(qts
));
416 static void test_blockdev_add_device_add_and_del_q35(void)
420 qts
= qtest_init("-machine q35 -device pcie-root-port,id=p1 "
421 "-device pcie-pci-bridge,bus=p1,id=b1");
424 * blockdev_add/device_add and device_del. The drive is used by a
425 * device that unplugs after reset, but it doesn't go away.
427 blockdev_add_with_media(qts
);
429 device_del(qts
, true);
430 g_assert(has_blockdev(qts
));
435 int main(int argc
, char **argv
)
437 g_test_init(&argc
, &argv
, NULL
);
439 qtest_add_func("/drive_del/without-dev", test_drive_without_dev
);
441 if (qvirtio_get_dev_type() != NULL
) {
442 qtest_add_func("/drive_del/after_failed_device_add",
443 test_after_failed_device_add
);
444 qtest_add_func("/drive_del/drive_del_device_del",
445 test_drive_del_device_del
);
446 qtest_add_func("/device_del/drive/cli_device",
447 test_cli_device_del
);
448 qtest_add_func("/device_del/drive/device_add",
449 test_device_add_and_del
);
450 qtest_add_func("/device_del/drive/drive_add_device_add",
451 test_drive_add_device_add_and_del
);
452 qtest_add_func("/device_del/empty",
453 test_empty_device_del
);
454 qtest_add_func("/device_del/blockdev",
455 test_blockdev_add_device_add_and_del
);
457 if (qtest_has_machine("q35")) {
458 qtest_add_func("/device_del/drive/cli_device_q35",
459 test_cli_device_del_q35
);
460 qtest_add_func("/device_del/drive/device_add_q35",
461 test_device_add_and_del_q35
);
462 qtest_add_func("/device_del/drive/drive_add_device_add_q35",
463 test_drive_add_device_add_and_del_q35
);
464 qtest_add_func("/device_del/blockdev_q35",
465 test_blockdev_add_device_add_and_del_q35
);