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-vm-generation-id", ERROR_CLASS_GENERIC_ERROR
},
49 #ifndef CONFIG_PROFILER
50 { "x-query-profile", ERROR_CLASS_GENERIC_ERROR
},
52 /* Only valid with a USB bus added */
53 { "x-query-usb", ERROR_CLASS_GENERIC_ERROR
},
54 /* Only valid with accel=tcg */
55 { "x-query-jit", ERROR_CLASS_GENERIC_ERROR
},
56 { "x-query-opcount", ERROR_CLASS_GENERIC_ERROR
},
57 { "xen-event-list", ERROR_CLASS_GENERIC_ERROR
},
62 for (i
= 0; fails
[i
].cmd
; i
++) {
63 if (!strcmp(cmd
, fails
[i
].cmd
)) {
64 return fails
[i
].err_class
;
70 static void test_query(const void *data
)
72 const char *cmd
= data
;
73 int expected_error_class
= query_error_class(cmd
);
75 const char *error_class
;
78 qts
= qtest_init(common_args
);
80 resp
= qtest_qmp(qts
, "{ 'execute': %s }", cmd
);
81 error
= qdict_get_qdict(resp
, "error");
82 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
84 if (expected_error_class
< 0) {
85 g_assert(qdict_haskey(resp
, "return"));
88 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
90 ==, expected_error_class
);
97 static bool query_is_ignored(const char *cmd
)
99 const char *ignored
[] = {
100 /* Not actually queries: */
102 /* Success depends on target arch: */
103 "query-cpu-definitions", /* arm, i386, ppc, s390x */
104 "query-gic-capabilities", /* arm */
105 /* Success depends on target-specific build configuration: */
106 "query-pci", /* CONFIG_PCI */
107 "x-query-virtio", /* CONFIG_VIRTIO */
108 /* Success depends on launching SEV guest */
109 "query-sev-launch-measure",
110 /* Success depends on Host or Hypervisor SEV support */
112 "query-sev-capabilities",
114 "query-sgx-capabilities",
115 /* Success depends on enabling dirty page rate limit */
116 "query-vcpu-dirty-limit",
121 for (i
= 0; ignored
[i
]; i
++) {
122 if (!strcmp(cmd
, ignored
[i
])) {
130 SchemaInfoList
*list
;
134 static void qmp_schema_init(QmpSchema
*schema
)
138 SchemaInfoList
*tail
;
141 qts
= qtest_init(common_args
);
143 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
145 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
146 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
152 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
154 /* Build @schema: hash table mapping entity name to SchemaInfo */
155 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
156 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
160 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
162 return g_hash_table_lookup(schema
->hash
, name
);
165 static void qmp_schema_cleanup(QmpSchema
*schema
)
167 qapi_free_SchemaInfoList(schema
->list
);
168 g_hash_table_destroy(schema
->hash
);
171 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
173 SchemaInfoObjectMemberList
*tail
;
175 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
177 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
178 if (!tail
->value
->q_default
) {
186 static void add_query_tests(QmpSchema
*schema
)
188 SchemaInfoList
*tail
;
189 SchemaInfo
*si
, *arg_type
, *ret_type
;
192 /* Test the query-like commands */
193 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
195 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
199 if (query_is_ignored(si
->name
)) {
203 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
204 if (object_type_has_mandatory_members(arg_type
)) {
208 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
209 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
210 && !ret_type
->u
.object
.members
) {
214 test_name
= g_strdup_printf("qmp/%s", si
->name
);
215 qtest_add_data_func(test_name
, si
->name
, test_query
);
220 static void test_object_add_failure_modes(void)
225 /* attempt to create an object without props */
226 qts
= qtest_init(common_args
);
227 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
228 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
229 g_assert_nonnull(resp
);
230 qmp_expect_error_and_unref(resp
, "GenericError");
232 /* attempt to create an object without qom-type */
233 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
234 " {'id': 'ram1' } }");
235 g_assert_nonnull(resp
);
236 qmp_expect_error_and_unref(resp
, "GenericError");
238 /* attempt to delete an object that does not exist */
239 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
240 " {'id': 'ram1' } }");
241 g_assert_nonnull(resp
);
242 qmp_expect_error_and_unref(resp
, "GenericError");
244 /* attempt to create 2 objects with duplicate id */
245 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
246 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
247 " 'size': 1048576 } }");
248 g_assert_nonnull(resp
);
249 g_assert(qdict_haskey(resp
, "return"));
252 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
253 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
254 " 'size': 1048576 } }");
255 g_assert_nonnull(resp
);
256 qmp_expect_error_and_unref(resp
, "GenericError");
258 /* delete ram1 object */
259 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
260 " {'id': 'ram1' } }");
261 g_assert_nonnull(resp
);
262 g_assert(qdict_haskey(resp
, "return"));
265 /* attempt to create an object with a property of a wrong type */
266 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
267 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
268 " 'size': '1048576' } }");
269 g_assert_nonnull(resp
);
270 /* now do it right */
271 qmp_expect_error_and_unref(resp
, "GenericError");
273 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
274 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
275 " 'size': 1048576 } }");
276 g_assert_nonnull(resp
);
277 g_assert(qdict_haskey(resp
, "return"));
280 /* delete ram1 object */
281 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
282 " {'id': 'ram1' } }");
283 g_assert_nonnull(resp
);
284 g_assert(qdict_haskey(resp
, "return"));
287 /* attempt to create an object without the id */
288 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
289 " {'qom-type': 'memory-backend-ram',"
290 " 'size': 1048576 } }");
291 g_assert_nonnull(resp
);
292 qmp_expect_error_and_unref(resp
, "GenericError");
294 /* now do it right */
295 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
296 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
297 " 'size': 1048576 } }");
298 g_assert_nonnull(resp
);
299 g_assert(qdict_haskey(resp
, "return"));
302 /* delete ram1 object */
303 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
304 " {'id': 'ram1' } }");
305 g_assert_nonnull(resp
);
306 g_assert(qdict_haskey(resp
, "return"));
309 /* attempt to set a non existing property */
310 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
311 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
312 " 'sized': 1048576 } }");
313 g_assert_nonnull(resp
);
314 qmp_expect_error_and_unref(resp
, "GenericError");
316 /* now do it right */
317 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
318 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
319 " 'size': 1048576 } }");
320 g_assert_nonnull(resp
);
321 g_assert(qdict_haskey(resp
, "return"));
324 /* delete ram1 object without id */
325 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
326 " {'ida': 'ram1' } }");
327 g_assert_nonnull(resp
);
330 /* delete ram1 object */
331 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
332 " {'id': 'ram1' } }");
333 g_assert_nonnull(resp
);
334 g_assert(qdict_haskey(resp
, "return"));
337 /* delete ram1 object that does not exist anymore*/
338 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
339 " {'id': 'ram1' } }");
340 g_assert_nonnull(resp
);
341 qmp_expect_error_and_unref(resp
, "GenericError");
346 int main(int argc
, char *argv
[])
351 g_test_init(&argc
, &argv
, NULL
);
353 qmp_schema_init(&schema
);
354 add_query_tests(&schema
);
356 qtest_add_func("qmp/object-add-failure-modes",
357 test_object_add_failure_modes
);
361 qmp_schema_cleanup(&schema
);