2 * QMP command test cases
4 * Copyright (c) 2017 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-visit-introspect.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qobject-input-visitor.h"
20 const char common_args
[] = "-nodefaults -machine none";
22 /* Query smoke tests */
24 static int query_error_class(const char *cmd
)
30 /* Success depends on build configuration: */
32 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND
},
35 { "query-replay", ERROR_CLASS_COMMAND_NOT_FOUND
},
38 { "query-vnc", ERROR_CLASS_GENERIC_ERROR
},
39 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR
},
41 #ifndef CONFIG_REPLICATION
42 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND
},
44 /* Likewise, and require special QEMU command-line arguments: */
45 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR
},
46 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE
},
47 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR
},
48 { "query-hv-balloon-status-report", ERROR_CLASS_GENERIC_ERROR
},
49 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR
},
50 /* Only valid with a USB bus added */
51 { "x-query-usb", ERROR_CLASS_GENERIC_ERROR
},
52 /* Only valid with accel=tcg */
53 { "x-query-jit", ERROR_CLASS_GENERIC_ERROR
},
54 { "x-query-opcount", ERROR_CLASS_GENERIC_ERROR
},
55 { "xen-event-list", ERROR_CLASS_GENERIC_ERROR
},
60 for (i
= 0; fails
[i
].cmd
; i
++) {
61 if (!strcmp(cmd
, fails
[i
].cmd
)) {
62 return fails
[i
].err_class
;
68 static void test_query(const void *data
)
70 const char *cmd
= data
;
71 int expected_error_class
= query_error_class(cmd
);
73 const char *error_class
;
76 qts
= qtest_init(common_args
);
78 resp
= qtest_qmp(qts
, "{ 'execute': %s }", cmd
);
79 error
= qdict_get_qdict(resp
, "error");
80 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
82 if (expected_error_class
< 0) {
83 g_assert(qdict_haskey(resp
, "return"));
86 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
88 ==, expected_error_class
);
95 static bool query_is_ignored(const char *cmd
)
97 const char *ignored
[] = {
98 /* Not actually queries: */
100 /* Success depends on target arch: */
101 "query-cpu-definitions", /* arm, i386, ppc, s390x */
102 "query-gic-capabilities", /* arm */
103 /* Success depends on target-specific build configuration: */
104 "query-pci", /* CONFIG_PCI */
105 "x-query-virtio", /* CONFIG_VIRTIO */
106 /* Success depends on launching SEV guest */
107 "query-sev-launch-measure",
108 /* Success depends on Host or Hypervisor SEV support */
110 "query-sev-capabilities",
112 "query-sgx-capabilities",
113 /* Success depends on enabling dirty page rate limit */
114 "query-vcpu-dirty-limit",
119 for (i
= 0; ignored
[i
]; i
++) {
120 if (!strcmp(cmd
, ignored
[i
])) {
128 SchemaInfoList
*list
;
132 static void qmp_schema_init(QmpSchema
*schema
)
136 SchemaInfoList
*tail
;
139 qts
= qtest_init(common_args
);
141 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
143 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
144 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
150 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
152 /* Build @schema: hash table mapping entity name to SchemaInfo */
153 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
154 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
158 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
160 return g_hash_table_lookup(schema
->hash
, name
);
163 static void qmp_schema_cleanup(QmpSchema
*schema
)
165 qapi_free_SchemaInfoList(schema
->list
);
166 g_hash_table_destroy(schema
->hash
);
169 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
171 SchemaInfoObjectMemberList
*tail
;
173 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
175 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
176 if (!tail
->value
->q_default
) {
184 static void add_query_tests(QmpSchema
*schema
)
186 SchemaInfoList
*tail
;
187 SchemaInfo
*si
, *arg_type
, *ret_type
;
190 /* Test the query-like commands */
191 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
193 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
197 if (query_is_ignored(si
->name
)) {
201 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
202 if (object_type_has_mandatory_members(arg_type
)) {
206 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
207 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
208 && !ret_type
->u
.object
.members
) {
212 test_name
= g_strdup_printf("qmp/%s", si
->name
);
213 qtest_add_data_func(test_name
, si
->name
, test_query
);
218 static void test_object_add_failure_modes(void)
223 /* attempt to create an object without props */
224 qts
= qtest_init(common_args
);
225 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
226 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
227 g_assert_nonnull(resp
);
228 qmp_expect_error_and_unref(resp
, "GenericError");
230 /* attempt to create an object without qom-type */
231 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
232 " {'id': 'ram1' } }");
233 g_assert_nonnull(resp
);
234 qmp_expect_error_and_unref(resp
, "GenericError");
236 /* attempt to delete an object that does not exist */
237 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
238 " {'id': 'ram1' } }");
239 g_assert_nonnull(resp
);
240 qmp_expect_error_and_unref(resp
, "GenericError");
242 /* attempt to create 2 objects with duplicate id */
243 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
244 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
245 " 'size': 1048576 } }");
246 g_assert_nonnull(resp
);
247 g_assert(qdict_haskey(resp
, "return"));
250 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
251 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
252 " 'size': 1048576 } }");
253 g_assert_nonnull(resp
);
254 qmp_expect_error_and_unref(resp
, "GenericError");
256 /* delete ram1 object */
257 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
258 " {'id': 'ram1' } }");
259 g_assert_nonnull(resp
);
260 g_assert(qdict_haskey(resp
, "return"));
263 /* attempt to create an object with a property of a wrong type */
264 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
265 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
266 " 'size': '1048576' } }");
267 g_assert_nonnull(resp
);
268 /* now do it right */
269 qmp_expect_error_and_unref(resp
, "GenericError");
271 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
272 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
273 " 'size': 1048576 } }");
274 g_assert_nonnull(resp
);
275 g_assert(qdict_haskey(resp
, "return"));
278 /* delete ram1 object */
279 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
280 " {'id': 'ram1' } }");
281 g_assert_nonnull(resp
);
282 g_assert(qdict_haskey(resp
, "return"));
285 /* attempt to create an object without the id */
286 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
287 " {'qom-type': 'memory-backend-ram',"
288 " 'size': 1048576 } }");
289 g_assert_nonnull(resp
);
290 qmp_expect_error_and_unref(resp
, "GenericError");
292 /* now do it right */
293 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
294 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
295 " 'size': 1048576 } }");
296 g_assert_nonnull(resp
);
297 g_assert(qdict_haskey(resp
, "return"));
300 /* delete ram1 object */
301 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
302 " {'id': 'ram1' } }");
303 g_assert_nonnull(resp
);
304 g_assert(qdict_haskey(resp
, "return"));
307 /* attempt to set a non existing property */
308 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
309 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
310 " 'sized': 1048576 } }");
311 g_assert_nonnull(resp
);
312 qmp_expect_error_and_unref(resp
, "GenericError");
314 /* now do it right */
315 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
316 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
317 " 'size': 1048576 } }");
318 g_assert_nonnull(resp
);
319 g_assert(qdict_haskey(resp
, "return"));
322 /* delete ram1 object without id */
323 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
324 " {'ida': 'ram1' } }");
325 g_assert_nonnull(resp
);
328 /* delete ram1 object */
329 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
330 " {'id': 'ram1' } }");
331 g_assert_nonnull(resp
);
332 g_assert(qdict_haskey(resp
, "return"));
335 /* delete ram1 object that does not exist anymore*/
336 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
337 " {'id': 'ram1' } }");
338 g_assert_nonnull(resp
);
339 qmp_expect_error_and_unref(resp
, "GenericError");
344 int main(int argc
, char *argv
[])
349 g_test_init(&argc
, &argv
, NULL
);
351 qmp_schema_init(&schema
);
352 add_query_tests(&schema
);
354 qtest_add_func("qmp/object-add-failure-modes",
355 test_object_add_failure_modes
);
359 qmp_schema_cleanup(&schema
);