]> git.proxmox.com Git - mirror_qemu.git/blame - tests/drive_del-test.c
json: Clean up headers
[mirror_qemu.git] / tests / drive_del-test.c
CommitLineData
43cd2098 1/*
e2f3f221 2 * blockdev.c test cases
43cd2098 3 *
e2f3f221 4 * Copyright (C) 2013-2014 Red Hat Inc.
43cd2098
SH
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
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.
11 */
12
681c28a3 13#include "qemu/osdep.h"
43cd2098 14#include "libqtest.h"
2f84a92e 15#include "libqos/virtio.h"
452fcdbc 16#include "qapi/qmp/qdict.h"
43cd2098 17
055a1efc
MA
18/* TODO actually test the results and get rid of this */
19#define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
20
2eea5cd4 21static void drive_add(void)
e2f3f221 22{
5fb48d96 23 char *resp = hmp("drive_add 0 if=none,id=drive0");
e2f3f221 24
5fb48d96
MA
25 g_assert_cmpstr(resp, ==, "OK\r\n");
26 g_free(resp);
2eea5cd4
MA
27}
28
29static void drive_del(void)
30{
5fb48d96 31 char *resp = hmp("drive_del drive0");
e2f3f221 32
5fb48d96
MA
33 g_assert_cmpstr(resp, ==, "");
34 g_free(resp);
2eea5cd4
MA
35}
36
767c86d3
MA
37static void device_del(void)
38{
39 QDict *response;
40
41 /* Complication: ignore DEVICE_DELETED event */
42 qmp_discard_response("{'execute': 'device_del',"
43 " 'arguments': { 'id': 'dev0' } }");
44 response = qmp_receive();
45 g_assert(response);
46 g_assert(qdict_haskey(response, "return"));
cb3e7f08 47 qobject_unref(response);
767c86d3
MA
48}
49
2eea5cd4
MA
50static void test_drive_without_dev(void)
51{
52 /* Start with an empty drive */
53 qtest_start("-drive if=none,id=drive0");
54
55 /* Delete the drive */
56 drive_del();
e2f3f221
MA
57
58 /* Ensure re-adding the drive works - there should be no duplicate ID error
59 * because the old drive must be gone.
60 */
2eea5cd4 61 drive_add();
e2f3f221
MA
62
63 qtest_end();
64}
65
66static void test_after_failed_device_add(void)
43cd2098
SH
67{
68 QDict *response;
69 QDict *error;
70
71 qtest_start("-drive if=none,id=drive0");
72
2f84a92e 73 /* Make device_add fail. If this leaks the virtio-blk device then a
43cd2098
SH
74 * reference to drive0 will also be held (via qdev properties).
75 */
d0e38668
MA
76 response = qmp("{'execute': 'device_add',"
77 " 'arguments': {"
2f84a92e 78 " 'driver': 'virtio-blk-%s',"
d0e38668 79 " 'drive': 'drive0'"
2f84a92e 80 "}}", qvirtio_get_dev_type());
43cd2098
SH
81 g_assert(response);
82 error = qdict_get_qdict(response, "error");
49649f23 83 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
cb3e7f08 84 qobject_unref(response);
43cd2098
SH
85
86 /* Delete the drive */
2eea5cd4 87 drive_del();
43cd2098
SH
88
89 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
2f84a92e 90 * virtio-blk device exists that holds a reference to the old drive0.
43cd2098 91 */
2eea5cd4 92 drive_add();
43cd2098
SH
93
94 qtest_end();
95}
96
767c86d3
MA
97static void test_drive_del_device_del(void)
98{
2f84a92e
TH
99 char *args;
100
767c86d3 101 /* Start with a drive used by a device that unplugs instantaneously */
2f84a92e
TH
102 args = g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
103 " -device virtio-scsi-%s"
104 " -device scsi-hd,drive=drive0,id=dev0",
105 qvirtio_get_dev_type());
106 qtest_start(args);
767c86d3
MA
107
108 /*
109 * Delete the drive, and then the device
110 * Doing it in this order takes notoriously tricky special paths
111 */
112 drive_del();
113 device_del();
114
115 qtest_end();
2f84a92e 116 g_free(args);
767c86d3
MA
117}
118
43cd2098
SH
119int main(int argc, char **argv)
120{
121 const char *arch = qtest_get_arch();
122
43cd2098
SH
123 g_test_init(&argc, &argv, NULL);
124
e2f3f221
MA
125 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
126
2f84a92e 127 /* TODO I guess any arch with a hot-pluggable virtio bus would do */
059ce0f0 128 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64") ||
2f84a92e
TH
129 !strcmp(arch, "ppc") || !strcmp(arch, "ppc64") ||
130 !strcmp(arch, "s390x")) {
e2f3f221
MA
131 qtest_add_func("/drive_del/after_failed_device_add",
132 test_after_failed_device_add);
767c86d3
MA
133 qtest_add_func("/blockdev/drive_del_device_del",
134 test_drive_del_device_del);
e2f3f221 135 }
43cd2098
SH
136
137 return g_test_run();
138}