Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-2.12-20180410' into staging
[qemu/ar7.git] / tests / drive_del-test.c
blob313030a14cf44aca6b5721b688bcb50c82258b13
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 static void drive_add(void)
20 char *resp = hmp("drive_add 0 if=none,id=drive0");
22 g_assert_cmpstr(resp, ==, "OK\r\n");
23 g_free(resp);
26 static void drive_del(void)
28 char *resp = hmp("drive_del drive0");
30 g_assert_cmpstr(resp, ==, "");
31 g_free(resp);
34 static void device_del(void)
36 QDict *response;
38 /* Complication: ignore DEVICE_DELETED event */
39 qmp_discard_response("{'execute': 'device_del',"
40 " 'arguments': { 'id': 'dev0' } }");
41 response = qmp_receive();
42 g_assert(response);
43 g_assert(qdict_haskey(response, "return"));
44 QDECREF(response);
47 static void test_drive_without_dev(void)
49 /* Start with an empty drive */
50 qtest_start("-drive if=none,id=drive0");
52 /* Delete the drive */
53 drive_del();
55 /* Ensure re-adding the drive works - there should be no duplicate ID error
56 * because the old drive must be gone.
58 drive_add();
60 qtest_end();
63 static void test_after_failed_device_add(void)
65 QDict *response;
66 QDict *error;
68 qtest_start("-drive if=none,id=drive0");
70 /* Make device_add fail. If this leaks the virtio-blk device then a
71 * reference to drive0 will also be held (via qdev properties).
73 response = qmp("{'execute': 'device_add',"
74 " 'arguments': {"
75 " 'driver': 'virtio-blk-%s',"
76 " 'drive': 'drive0'"
77 "}}", qvirtio_get_dev_type());
78 g_assert(response);
79 error = qdict_get_qdict(response, "error");
80 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
81 QDECREF(response);
83 /* Delete the drive */
84 drive_del();
86 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
87 * virtio-blk device exists that holds a reference to the old drive0.
89 drive_add();
91 qtest_end();
94 static void test_drive_del_device_del(void)
96 char *args;
98 /* Start with a drive used by a device that unplugs instantaneously */
99 args = g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
100 " -device virtio-scsi-%s"
101 " -device scsi-hd,drive=drive0,id=dev0",
102 qvirtio_get_dev_type());
103 qtest_start(args);
106 * Delete the drive, and then the device
107 * Doing it in this order takes notoriously tricky special paths
109 drive_del();
110 device_del();
112 qtest_end();
113 g_free(args);
116 int main(int argc, char **argv)
118 const char *arch = qtest_get_arch();
120 g_test_init(&argc, &argv, NULL);
122 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
124 /* TODO I guess any arch with a hot-pluggable virtio bus would do */
125 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64") ||
126 !strcmp(arch, "ppc") || !strcmp(arch, "ppc64") ||
127 !strcmp(arch, "s390x")) {
128 qtest_add_func("/drive_del/after_failed_device_add",
129 test_after_failed_device_add);
130 qtest_add_func("/blockdev/drive_del_device_del",
131 test_drive_del_device_del);
134 return g_test_run();