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