hw/input/lm832x: Convert reset handler to DeviceReset
[qemu/ar7.git] / tests / drive_del-test.c
blob5f8839b2320f42d2659e2a58e15f9e9b19d6907f
1 /*
2 * blockdev.c test cases
4 * Copyright (C) 2013-2014 Red Hat Inc.
6 * Authors:
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 "libqtest.h"
15 #include "libqos/virtio.h"
16 #include "qapi/qmp/qdict.h"
18 /* TODO actually test the results and get rid of this */
19 #define qmp_discard_response(q, ...) qobject_unref(qtest_qmp(q, __VA_ARGS__))
21 static void drive_add(QTestState *qts)
23 char *resp = qtest_hmp(qts, "drive_add 0 if=none,id=drive0");
25 g_assert_cmpstr(resp, ==, "OK\r\n");
26 g_free(resp);
29 static void drive_del(QTestState *qts)
31 char *resp = qtest_hmp(qts, "drive_del drive0");
33 g_assert_cmpstr(resp, ==, "");
34 g_free(resp);
37 static void device_del(QTestState *qts)
39 QDict *response;
41 /* Complication: ignore DEVICE_DELETED event */
42 qmp_discard_response(qts, "{'execute': 'device_del',"
43 " 'arguments': { 'id': 'dev0' } }");
44 response = qtest_qmp_receive(qts);
45 g_assert(response);
46 g_assert(qdict_haskey(response, "return"));
47 qobject_unref(response);
50 static void test_drive_without_dev(void)
52 QTestState *qts;
54 /* Start with an empty drive */
55 qts = qtest_init("-drive if=none,id=drive0");
57 /* Delete the drive */
58 drive_del(qts);
60 /* Ensure re-adding the drive works - there should be no duplicate ID error
61 * because the old drive must be gone.
63 drive_add(qts);
65 qtest_quit(qts);
69 * qvirtio_get_dev_type:
70 * Returns: the preferred virtio bus/device type for the current architecture.
71 * TODO: delete this
73 static const char *qvirtio_get_dev_type(void)
75 const char *arch = qtest_get_arch();
77 if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
78 return "device"; /* for virtio-mmio */
79 } else if (g_str_equal(arch, "s390x")) {
80 return "ccw";
81 } else {
82 return "pci";
86 static void test_after_failed_device_add(void)
88 char driver[32];
89 QDict *response;
90 QTestState *qts;
92 snprintf(driver, sizeof(driver), "virtio-blk-%s",
93 qvirtio_get_dev_type());
95 qts = qtest_init("-drive if=none,id=drive0");
97 /* Make device_add fail. If this leaks the virtio-blk device then a
98 * reference to drive0 will also be held (via qdev properties).
100 response = qtest_qmp(qts, "{'execute': 'device_add',"
101 " 'arguments': {"
102 " 'driver': %s,"
103 " 'drive': 'drive0'"
104 "}}", driver);
105 g_assert(response);
106 qmp_assert_error_class(response, "GenericError");
108 /* Delete the drive */
109 drive_del(qts);
111 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
112 * virtio-blk device exists that holds a reference to the old drive0.
114 drive_add(qts);
116 qtest_quit(qts);
119 static void test_drive_del_device_del(void)
121 QTestState *qts;
123 /* Start with a drive used by a device that unplugs instantaneously */
124 qts = qtest_initf("-drive if=none,id=drive0,file=null-co://,"
125 "file.read-zeroes=on,format=raw"
126 " -device virtio-scsi-%s"
127 " -device scsi-hd,drive=drive0,id=dev0",
128 qvirtio_get_dev_type());
131 * Delete the drive, and then the device
132 * Doing it in this order takes notoriously tricky special paths
134 drive_del(qts);
135 device_del(qts);
137 qtest_quit(qts);
140 int main(int argc, char **argv)
142 g_test_init(&argc, &argv, NULL);
144 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
146 if (qvirtio_get_dev_type() != NULL) {
147 qtest_add_func("/drive_del/after_failed_device_add",
148 test_after_failed_device_add);
149 qtest_add_func("/blockdev/drive_del_device_del",
150 test_drive_del_device_del);
153 return g_test_run();