root / tests / qdev-monitor-test.c @ 5e954943
History | View | Annotate | Download (2.3 kB)
1 |
/*
|
---|---|
2 |
* qdev-monitor.c test cases
|
3 |
*
|
4 |
* Copyright (C) 2013 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 <string.h> |
14 |
#include <glib.h> |
15 |
#include "libqtest.h" |
16 |
#include "qapi/qmp/qjson.h" |
17 |
|
18 |
static void test_device_add(void) |
19 |
{ |
20 |
QDict *response; |
21 |
QDict *error; |
22 |
|
23 |
qtest_start("-drive if=none,id=drive0");
|
24 |
|
25 |
/* Make device_add fail. If this leaks the virtio-blk-pci device then a
|
26 |
* reference to drive0 will also be held (via qdev properties).
|
27 |
*/
|
28 |
response = qmp("{\"execute\": \"device_add\","
|
29 |
" \"arguments\": {"
|
30 |
" \"driver\": \"virtio-blk-pci\","
|
31 |
" \"drive\": \"drive0\""
|
32 |
"}}");
|
33 |
g_assert(response); |
34 |
error = qdict_get_qdict(response, "error");
|
35 |
g_assert(!strcmp(qdict_get_try_str(error, "desc") ?: "", |
36 |
"Device needs media, but drive is empty"));
|
37 |
QDECREF(response); |
38 |
|
39 |
/* Delete the drive */
|
40 |
response = qmp("{\"execute\": \"human-monitor-command\","
|
41 |
" \"arguments\": {"
|
42 |
" \"command-line\": \"drive_del drive0\""
|
43 |
"}}");
|
44 |
g_assert(response); |
45 |
g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "(null)", "")); |
46 |
QDECREF(response); |
47 |
|
48 |
/* Try to re-add the drive. This fails with duplicate IDs if a leaked
|
49 |
* virtio-blk-pci exists that holds a reference to the old drive0.
|
50 |
*/
|
51 |
response = qmp("{\"execute\": \"human-monitor-command\","
|
52 |
" \"arguments\": {"
|
53 |
" \"command-line\": \"drive_add pci-addr=auto if=none,id=drive0\""
|
54 |
"}}");
|
55 |
g_assert(response); |
56 |
g_assert(!strcmp(qdict_get_try_str(response, "return") ?: "", |
57 |
"OK\r\n"));
|
58 |
QDECREF(response); |
59 |
|
60 |
qtest_end(); |
61 |
} |
62 |
|
63 |
int main(int argc, char **argv) |
64 |
{ |
65 |
const char *arch = qtest_get_arch(); |
66 |
|
67 |
/* Check architecture */
|
68 |
if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { |
69 |
g_test_message("Skipping test for non-x86\n");
|
70 |
return 0; |
71 |
} |
72 |
|
73 |
/* Run the tests */
|
74 |
g_test_init(&argc, &argv, NULL);
|
75 |
|
76 |
qtest_add_func("/qmp/device_add", test_device_add);
|
77 |
|
78 |
return g_test_run();
|
79 |
} |