]> git.proxmox.com Git - mirror_qemu.git/blame - tests/drive_del-test.c
tests/libqos: fix usage of bool in pci-spapr.c
[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
19e3d979
PB
66/*
67 * qvirtio_get_dev_type:
68 * Returns: the preferred virtio bus/device type for the current architecture.
69 * TODO: delete this
70 */
71static const char *qvirtio_get_dev_type(void)
72{
73 const char *arch = qtest_get_arch();
74
75 if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
76 return "device"; /* for virtio-mmio */
77 } else if (g_str_equal(arch, "s390x")) {
78 return "ccw";
79 } else {
80 return "pci";
81 }
82}
83
e2f3f221 84static void test_after_failed_device_add(void)
43cd2098 85{
83273e84 86 char driver[32];
43cd2098 87 QDict *response;
43cd2098 88
83273e84
MA
89 snprintf(driver, sizeof(driver), "virtio-blk-%s",
90 qvirtio_get_dev_type());
91
43cd2098
SH
92 qtest_start("-drive if=none,id=drive0");
93
2f84a92e 94 /* Make device_add fail. If this leaks the virtio-blk device then a
43cd2098
SH
95 * reference to drive0 will also be held (via qdev properties).
96 */
d0e38668
MA
97 response = qmp("{'execute': 'device_add',"
98 " 'arguments': {"
83273e84 99 " 'driver': %s,"
d0e38668 100 " 'drive': 'drive0'"
83273e84 101 "}}", driver);
43cd2098 102 g_assert(response);
ebb4d82d 103 qmp_assert_error_class(response, "GenericError");
43cd2098
SH
104
105 /* Delete the drive */
2eea5cd4 106 drive_del();
43cd2098
SH
107
108 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
2f84a92e 109 * virtio-blk device exists that holds a reference to the old drive0.
43cd2098 110 */
2eea5cd4 111 drive_add();
43cd2098
SH
112
113 qtest_end();
114}
115
767c86d3
MA
116static void test_drive_del_device_del(void)
117{
2f84a92e
TH
118 char *args;
119
767c86d3 120 /* Start with a drive used by a device that unplugs instantaneously */
2f84a92e
TH
121 args = g_strdup_printf("-drive if=none,id=drive0,file=null-co://,format=raw"
122 " -device virtio-scsi-%s"
123 " -device scsi-hd,drive=drive0,id=dev0",
124 qvirtio_get_dev_type());
125 qtest_start(args);
767c86d3
MA
126
127 /*
128 * Delete the drive, and then the device
129 * Doing it in this order takes notoriously tricky special paths
130 */
131 drive_del();
132 device_del();
133
134 qtest_end();
2f84a92e 135 g_free(args);
767c86d3
MA
136}
137
43cd2098
SH
138int main(int argc, char **argv)
139{
43cd2098
SH
140 g_test_init(&argc, &argv, NULL);
141
e2f3f221
MA
142 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
143
19e3d979 144 if (qvirtio_get_dev_type() != NULL) {
e2f3f221
MA
145 qtest_add_func("/drive_del/after_failed_device_add",
146 test_after_failed_device_add);
767c86d3
MA
147 qtest_add_func("/blockdev/drive_del_device_del",
148 test_drive_del_device_del);
e2f3f221 149 }
43cd2098
SH
150
151 return g_test_run();
152}