]> git.proxmox.com Git - mirror_qemu.git/blob - tests/drive_del-test.c
blockdev-test: Factor out some common code into helpers
[mirror_qemu.git] / tests / drive_del-test.c
1 /*
2 * blockdev.c test cases
3 *
4 * Copyright (C) 2013-2014 Red Hat Inc.
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
13 #include <glib.h>
14 #include <string.h>
15 #include "libqtest.h"
16
17 static void drive_add(void)
18 {
19 QDict *response;
20
21 response = qmp("{'execute': 'human-monitor-command',"
22 " 'arguments': {"
23 " 'command-line': 'drive_add 0 if=none,id=drive0'"
24 "}}");
25 g_assert(response);
26 g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n");
27 QDECREF(response);
28 }
29
30 static void drive_del(void)
31 {
32 QDict *response;
33
34 response = qmp("{'execute': 'human-monitor-command',"
35 " 'arguments': {"
36 " 'command-line': 'drive_del drive0'"
37 "}}");
38 g_assert(response);
39 g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
40 QDECREF(response);
41 }
42
43 static void test_drive_without_dev(void)
44 {
45 /* Start with an empty drive */
46 qtest_start("-drive if=none,id=drive0");
47
48 /* Delete the drive */
49 drive_del();
50
51 /* Ensure re-adding the drive works - there should be no duplicate ID error
52 * because the old drive must be gone.
53 */
54 drive_add();
55
56 qtest_end();
57 }
58
59 static void test_after_failed_device_add(void)
60 {
61 QDict *response;
62 QDict *error;
63
64 qtest_start("-drive if=none,id=drive0");
65
66 /* Make device_add fail. If this leaks the virtio-blk-pci device then a
67 * reference to drive0 will also be held (via qdev properties).
68 */
69 response = qmp("{'execute': 'device_add',"
70 " 'arguments': {"
71 " 'driver': 'virtio-blk-pci',"
72 " 'drive': 'drive0'"
73 "}}");
74 g_assert(response);
75 error = qdict_get_qdict(response, "error");
76 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
77 QDECREF(response);
78
79 /* Delete the drive */
80 drive_del();
81
82 /* Try to re-add the drive. This fails with duplicate IDs if a leaked
83 * virtio-blk-pci exists that holds a reference to the old drive0.
84 */
85 drive_add();
86
87 qtest_end();
88 }
89
90 int main(int argc, char **argv)
91 {
92 const char *arch = qtest_get_arch();
93
94 g_test_init(&argc, &argv, NULL);
95
96 qtest_add_func("/drive_del/without-dev", test_drive_without_dev);
97
98 /* TODO I guess any arch with PCI would do */
99 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64")) {
100 qtest_add_func("/drive_del/after_failed_device_add",
101 test_after_failed_device_add);
102 }
103
104 return g_test_run();
105 }