]>
Commit | Line | Data |
---|---|---|
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 | ||
43cd2098 | 13 | #include <glib.h> |
e2f3f221 | 14 | #include <string.h> |
43cd2098 | 15 | #include "libqtest.h" |
43cd2098 | 16 | |
e2f3f221 MA |
17 | static void test_drive_without_dev(void) |
18 | { | |
19 | QDict *response; | |
e2f3f221 MA |
20 | |
21 | /* Start with an empty drive */ | |
22 | qtest_start("-drive if=none,id=drive0"); | |
23 | ||
24 | /* Delete the drive */ | |
d0e38668 MA |
25 | response = qmp("{'execute': 'human-monitor-command'," |
26 | " 'arguments': {" | |
27 | " 'command-line': 'drive_del drive0'" | |
e2f3f221 MA |
28 | "}}"); |
29 | g_assert(response); | |
37e153fe | 30 | g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, ""); |
e2f3f221 MA |
31 | QDECREF(response); |
32 | ||
33 | /* Ensure re-adding the drive works - there should be no duplicate ID error | |
34 | * because the old drive must be gone. | |
35 | */ | |
d0e38668 MA |
36 | response = qmp("{'execute': 'human-monitor-command'," |
37 | " 'arguments': {" | |
38 | " 'command-line': 'drive_add 0 if=none,id=drive0'" | |
e2f3f221 MA |
39 | "}}"); |
40 | g_assert(response); | |
37e153fe | 41 | g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n"); |
e2f3f221 MA |
42 | QDECREF(response); |
43 | ||
44 | qtest_end(); | |
45 | } | |
46 | ||
47 | static void test_after_failed_device_add(void) | |
43cd2098 SH |
48 | { |
49 | QDict *response; | |
50 | QDict *error; | |
51 | ||
52 | qtest_start("-drive if=none,id=drive0"); | |
53 | ||
54 | /* Make device_add fail. If this leaks the virtio-blk-pci device then a | |
55 | * reference to drive0 will also be held (via qdev properties). | |
56 | */ | |
d0e38668 MA |
57 | response = qmp("{'execute': 'device_add'," |
58 | " 'arguments': {" | |
59 | " 'driver': 'virtio-blk-pci'," | |
60 | " 'drive': 'drive0'" | |
43cd2098 SH |
61 | "}}"); |
62 | g_assert(response); | |
63 | error = qdict_get_qdict(response, "error"); | |
49649f23 | 64 | g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError"); |
43cd2098 SH |
65 | QDECREF(response); |
66 | ||
67 | /* Delete the drive */ | |
d0e38668 MA |
68 | response = qmp("{'execute': 'human-monitor-command'," |
69 | " 'arguments': {" | |
70 | " 'command-line': 'drive_del drive0'" | |
43cd2098 SH |
71 | "}}"); |
72 | g_assert(response); | |
a3d7cbc1 | 73 | g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, ""); |
43cd2098 SH |
74 | QDECREF(response); |
75 | ||
76 | /* Try to re-add the drive. This fails with duplicate IDs if a leaked | |
77 | * virtio-blk-pci exists that holds a reference to the old drive0. | |
78 | */ | |
d0e38668 MA |
79 | response = qmp("{'execute': 'human-monitor-command'," |
80 | " 'arguments': {" | |
e319df66 | 81 | " 'command-line': 'drive_add 0 if=none,id=drive0'" |
43cd2098 SH |
82 | "}}"); |
83 | g_assert(response); | |
a3d7cbc1 | 84 | g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "OK\r\n"); |
43cd2098 SH |
85 | QDECREF(response); |
86 | ||
87 | qtest_end(); | |
88 | } | |
89 | ||
90 | int main(int argc, char **argv) | |
91 | { | |
92 | const char *arch = qtest_get_arch(); | |
93 | ||
43cd2098 SH |
94 | g_test_init(&argc, &argv, NULL); |
95 | ||
e2f3f221 MA |
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 | } | |
43cd2098 SH |
103 | |
104 | return g_test_run(); | |
105 | } |