]> git.proxmox.com Git - mirror_qemu.git/blame - tests/drive_del-test.c
blockdev-test: Factor out some common code into helpers
[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
43cd2098 13#include <glib.h>
e2f3f221 14#include <string.h>
43cd2098 15#include "libqtest.h"
43cd2098 16
2eea5cd4 17static void drive_add(void)
e2f3f221
MA
18{
19 QDict *response;
e2f3f221 20
2eea5cd4
MA
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
30static void drive_del(void)
31{
32 QDict *response;
e2f3f221 33
d0e38668
MA
34 response = qmp("{'execute': 'human-monitor-command',"
35 " 'arguments': {"
36 " 'command-line': 'drive_del drive0'"
e2f3f221
MA
37 "}}");
38 g_assert(response);
37e153fe 39 g_assert_cmpstr(qdict_get_try_str(response, "return"), ==, "");
e2f3f221 40 QDECREF(response);
2eea5cd4
MA
41}
42
43static 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();
e2f3f221
MA
50
51 /* Ensure re-adding the drive works - there should be no duplicate ID error
52 * because the old drive must be gone.
53 */
2eea5cd4 54 drive_add();
e2f3f221
MA
55
56 qtest_end();
57}
58
59static void test_after_failed_device_add(void)
43cd2098
SH
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 */
d0e38668
MA
69 response = qmp("{'execute': 'device_add',"
70 " 'arguments': {"
71 " 'driver': 'virtio-blk-pci',"
72 " 'drive': 'drive0'"
43cd2098
SH
73 "}}");
74 g_assert(response);
75 error = qdict_get_qdict(response, "error");
49649f23 76 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
43cd2098
SH
77 QDECREF(response);
78
79 /* Delete the drive */
2eea5cd4 80 drive_del();
43cd2098
SH
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 */
2eea5cd4 85 drive_add();
43cd2098
SH
86
87 qtest_end();
88}
89
90int 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}