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