]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qtest/drive_del-test.c
meson: convert tests/qtest to meson
[mirror_qemu.git] / tests / qtest / 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"
a2ce7dbd 14#include "libqos/libqtest.h"
2f84a92e 15#include "libqos/virtio.h"
452fcdbc 16#include "qapi/qmp/qdict.h"
43cd2098 17
055a1efc 18/* TODO actually test the results and get rid of this */
a771729c 19#define qmp_discard_response(q, ...) qobject_unref(qtest_qmp(q, __VA_ARGS__))
055a1efc 20
a771729c 21static void drive_add(QTestState *qts)
e2f3f221 22{
a771729c 23 char *resp = qtest_hmp(qts, "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
a771729c 29static void drive_del(QTestState *qts)
2eea5cd4 30{
a771729c 31 char *resp = qtest_hmp(qts, "drive_del drive0");
e2f3f221 32
5fb48d96
MA
33 g_assert_cmpstr(resp, ==, "");
34 g_free(resp);
2eea5cd4
MA
35}
36
a771729c 37static void device_del(QTestState *qts)
767c86d3
MA
38{
39 QDict *response;
40
41 /* Complication: ignore DEVICE_DELETED event */
a771729c 42 qmp_discard_response(qts, "{'execute': 'device_del',"
767c86d3 43 " 'arguments': { 'id': 'dev0' } }");
a771729c 44 response = qtest_qmp_receive(qts);
767c86d3
MA
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{
a771729c
TH
52 QTestState *qts;
53
2eea5cd4 54 /* Start with an empty drive */
a771729c 55 qts = qtest_init("-drive if=none,id=drive0");
2eea5cd4
MA
56
57 /* Delete the drive */
a771729c 58 drive_del(qts);
e2f3f221
MA
59
60 /* Ensure re-adding the drive works - there should be no duplicate ID error
61 * because the old drive must be gone.
62 */
a771729c 63 drive_add(qts);
e2f3f221 64
a771729c 65 qtest_quit(qts);
e2f3f221
MA
66}
67
19e3d979
PB
68/*
69 * qvirtio_get_dev_type:
70 * Returns: the preferred virtio bus/device type for the current architecture.
71 * TODO: delete this
72 */
73static const char *qvirtio_get_dev_type(void)
74{
75 const char *arch = qtest_get_arch();
76
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";
83 }
84}
85
e2f3f221 86static void test_after_failed_device_add(void)
43cd2098 87{
83273e84 88 char driver[32];
43cd2098 89 QDict *response;
a771729c 90 QTestState *qts;
43cd2098 91
83273e84
MA
92 snprintf(driver, sizeof(driver), "virtio-blk-%s",
93 qvirtio_get_dev_type());
94
a771729c 95 qts = qtest_init("-drive if=none,id=drive0");
43cd2098 96
2f84a92e 97 /* Make device_add fail. If this leaks the virtio-blk device then a
43cd2098
SH
98 * reference to drive0 will also be held (via qdev properties).
99 */
a771729c
TH
100 response = qtest_qmp(qts, "{'execute': 'device_add',"
101 " 'arguments': {"
102 " 'driver': %s,"
103 " 'drive': 'drive0'"
104 "}}", driver);
43cd2098 105 g_assert(response);
ebb4d82d 106 qmp_assert_error_class(response, "GenericError");
43cd2098
SH
107
108 /* Delete the drive */
a771729c 109 drive_del(qts);
43cd2098
SH
110
111 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
2f84a92e 112 * virtio-blk device exists that holds a reference to the old drive0.
43cd2098 113 */
a771729c 114 drive_add(qts);
43cd2098 115
a771729c 116 qtest_quit(qts);
43cd2098
SH
117}
118
767c86d3
MA
119static void test_drive_del_device_del(void)
120{
a771729c 121 QTestState *qts;
2f84a92e 122
767c86d3 123 /* Start with a drive used by a device that unplugs instantaneously */
ca1ef1e6
AS
124 qts = qtest_initf("-drive if=none,id=drive0,file=null-co://,"
125 "file.read-zeroes=on,format=raw"
a771729c
TH
126 " -device virtio-scsi-%s"
127 " -device scsi-hd,drive=drive0,id=dev0",
128 qvirtio_get_dev_type());
767c86d3
MA
129
130 /*
131 * Delete the drive, and then the device
132 * Doing it in this order takes notoriously tricky special paths
133 */
a771729c
TH
134 drive_del(qts);
135 device_del(qts);
767c86d3 136
a771729c 137 qtest_quit(qts);
767c86d3
MA
138}
139
43cd2098
SH
140int main(int argc, char **argv)
141{
43cd2098
SH
142 g_test_init(&argc, &argv, NULL);
143
e2f3f221
MA
144 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
145
19e3d979 146 if (qvirtio_get_dev_type() != NULL) {
e2f3f221
MA
147 qtest_add_func("/drive_del/after_failed_device_add",
148 test_after_failed_device_add);
767c86d3
MA
149 qtest_add_func("/blockdev/drive_del_device_del",
150 test_drive_del_device_del);
e2f3f221 151 }
43cd2098
SH
152
153 return g_test_run();
154}