Merge remote-tracking branch 'remotes/kraxel/tags/ui-20210311-pull-request' into...
[qemu/ar7.git] / tests / qtest / drive_del-test.c
blob8d08ee9995d0de3c86fbfcbb29a7d79102e3a287
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 "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)
21 QDict *response;
22 QList *ret;
23 QListEntry *entry;
24 bool found;
26 response = qtest_qmp(qts, "{'execute': %s}", command);
27 g_assert(response && qdict_haskey(response, "return"));
28 ret = qdict_get_qlist(response, "return");
30 found = false;
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")) {
34 found = true;
35 break;
39 qobject_unref(response);
40 return found;
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)
55 QDict *response;
57 response = qtest_qmp(qts,
58 "{ 'execute': 'blockdev-add',"
59 " 'arguments': {"
60 " 'driver': 'raw',"
61 " 'node-name': 'drive0',"
62 " 'file': {"
63 " 'driver': 'null-co',"
64 " 'read-zeroes': true"
65 " }"
66 " }"
67 "}");
69 g_assert(response);
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));
81 g_free(resp);
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));
92 g_free(resp);
95 static void drive_del(QTestState *qts)
97 char *resp;
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));
103 g_free(resp);
107 * qvirtio_get_dev_type:
108 * Returns: the preferred virtio bus/device type for the current architecture.
109 * TODO: delete this
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")) {
118 return "ccw";
119 } else {
120 return "pci";
124 static void device_add(QTestState *qts)
126 QDict *response;
127 char driver[32];
128 snprintf(driver, sizeof(driver), "virtio-blk-%s",
129 qvirtio_get_dev_type());
131 response = qtest_qmp(qts, "{'execute': 'device_add',"
132 " 'arguments': {"
133 " 'driver': %s,"
134 " 'drive': 'drive0',"
135 " 'id': 'dev0'"
136 "}}", driver);
137 g_assert(response);
138 g_assert(qdict_haskey(response, "return"));
139 qobject_unref(response);
142 static void device_del(QTestState *qts, bool and_reset)
144 QDict *response;
146 response = qtest_qmp(qts, "{'execute': 'device_del',"
147 " 'arguments': { 'id': 'dev0' } }");
148 g_assert(response);
149 g_assert(qdict_haskey(response, "return"));
150 qobject_unref(response);
152 if (and_reset) {
153 response = qtest_qmp(qts, "{'execute': 'system_reset' }");
154 g_assert(response);
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)
164 QTestState *qts;
166 /* Start with an empty drive */
167 qts = qtest_init("-drive if=none,id=drive0");
169 /* Delete the drive */
170 drive_del(qts);
172 /* Ensure re-adding the drive works - there should be no duplicate ID error
173 * because the old drive must be gone.
175 drive_add(qts);
177 qtest_quit(qts);
180 static void test_after_failed_device_add(void)
182 char driver[32];
183 QDict *response;
184 QTestState *qts;
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',"
195 " 'arguments': {"
196 " 'driver': %s,"
197 " 'drive': 'drive0'"
198 "}}", driver);
199 g_assert(response);
200 qmp_expect_error_and_unref(response, "GenericError");
202 /* Delete the drive */
203 drive_del(qts);
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.
208 drive_add(qts);
210 qtest_quit(qts);
213 static void test_drive_del_device_del(void)
215 QTestState *qts;
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
228 drive_del(qts);
229 device_del(qts, false);
230 g_assert(!has_drive(qts));
232 qtest_quit(qts);
235 static void test_cli_device_del(void)
237 QTestState *qts;
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));
251 qtest_quit(qts);
254 static void test_empty_device_del(void)
256 QTestState *qts;
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);
263 qtest_quit(qts);
266 static void test_device_add_and_del(void)
268 QTestState *qts;
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");
277 device_add(qts);
278 device_del(qts, true);
279 g_assert(!has_drive(qts));
281 qtest_quit(qts);
284 static void test_drive_add_device_add_and_del(void)
286 QTestState *qts;
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);
295 device_add(qts);
296 device_del(qts, true);
297 g_assert(!has_drive(qts));
299 qtest_quit(qts);
302 static void test_blockdev_add_device_add_and_del(void)
304 QTestState *qts;
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);
313 device_add(qts);
314 device_del(qts, true);
315 g_assert(has_blockdev(qts));
317 qtest_quit(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);
343 return g_test_run();