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