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
},
61 for (i
= 0; fails
[i
].cmd
; i
++) {
62 if (!strcmp(cmd
, fails
[i
].cmd
)) {
63 return fails
[i
].err_class
;
69 static void test_query(const void *data
)
71 const char *cmd
= data
;
72 int expected_error_class
= query_error_class(cmd
);
74 const char *error_class
;
77 qts
= qtest_init(common_args
);
79 resp
= qtest_qmp(qts
, "{ 'execute': %s }", cmd
);
80 error
= qdict_get_qdict(resp
, "error");
81 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
83 if (expected_error_class
< 0) {
84 g_assert(qdict_haskey(resp
, "return"));
87 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
89 ==, expected_error_class
);
96 static bool query_is_ignored(const char *cmd
)
98 const char *ignored
[] = {
99 /* Not actually queries: */
101 /* Success depends on target arch: */
102 "query-cpu-definitions", /* arm, i386, ppc, s390x */
103 "query-gic-capabilities", /* arm */
104 /* Success depends on target-specific build configuration: */
105 "query-pci", /* CONFIG_PCI */
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",
117 for (i
= 0; ignored
[i
]; i
++) {
118 if (!strcmp(cmd
, ignored
[i
])) {
126 SchemaInfoList
*list
;
130 static void qmp_schema_init(QmpSchema
*schema
)
134 SchemaInfoList
*tail
;
137 qts
= qtest_init(common_args
);
139 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
141 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
142 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
148 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
150 /* Build @schema: hash table mapping entity name to SchemaInfo */
151 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
152 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
156 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
158 return g_hash_table_lookup(schema
->hash
, name
);
161 static void qmp_schema_cleanup(QmpSchema
*schema
)
163 qapi_free_SchemaInfoList(schema
->list
);
164 g_hash_table_destroy(schema
->hash
);
167 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
169 SchemaInfoObjectMemberList
*tail
;
171 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
173 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
174 if (!tail
->value
->has_q_default
) {
182 static void add_query_tests(QmpSchema
*schema
)
184 SchemaInfoList
*tail
;
185 SchemaInfo
*si
, *arg_type
, *ret_type
;
188 /* Test the query-like commands */
189 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
191 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
195 if (query_is_ignored(si
->name
)) {
199 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
200 if (object_type_has_mandatory_members(arg_type
)) {
204 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
205 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
206 && !ret_type
->u
.object
.members
) {
210 test_name
= g_strdup_printf("qmp/%s", si
->name
);
211 qtest_add_data_func(test_name
, si
->name
, test_query
);
216 static void test_object_add_failure_modes(void)
221 /* attempt to create an object without props */
222 qts
= qtest_init(common_args
);
223 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
224 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
225 g_assert_nonnull(resp
);
226 qmp_expect_error_and_unref(resp
, "GenericError");
228 /* attempt to create an object without qom-type */
229 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
230 " {'id': 'ram1' } }");
231 g_assert_nonnull(resp
);
232 qmp_expect_error_and_unref(resp
, "GenericError");
234 /* attempt to delete an object that does not exist */
235 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
236 " {'id': 'ram1' } }");
237 g_assert_nonnull(resp
);
238 qmp_expect_error_and_unref(resp
, "GenericError");
240 /* attempt to create 2 objects with duplicate id */
241 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
242 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
243 " 'size': 1048576 } }");
244 g_assert_nonnull(resp
);
245 g_assert(qdict_haskey(resp
, "return"));
248 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
249 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
250 " 'size': 1048576 } }");
251 g_assert_nonnull(resp
);
252 qmp_expect_error_and_unref(resp
, "GenericError");
254 /* delete ram1 object */
255 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
256 " {'id': 'ram1' } }");
257 g_assert_nonnull(resp
);
258 g_assert(qdict_haskey(resp
, "return"));
261 /* attempt to create an object with a property of a wrong type */
262 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
263 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
264 " 'size': '1048576' } }");
265 g_assert_nonnull(resp
);
266 /* now do it right */
267 qmp_expect_error_and_unref(resp
, "GenericError");
269 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
270 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
271 " 'size': 1048576 } }");
272 g_assert_nonnull(resp
);
273 g_assert(qdict_haskey(resp
, "return"));
276 /* delete ram1 object */
277 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
278 " {'id': 'ram1' } }");
279 g_assert_nonnull(resp
);
280 g_assert(qdict_haskey(resp
, "return"));
283 /* attempt to create an object without the id */
284 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
285 " {'qom-type': 'memory-backend-ram',"
286 " 'size': 1048576 } }");
287 g_assert_nonnull(resp
);
288 qmp_expect_error_and_unref(resp
, "GenericError");
290 /* now do it right */
291 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
292 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
293 " 'size': 1048576 } }");
294 g_assert_nonnull(resp
);
295 g_assert(qdict_haskey(resp
, "return"));
298 /* delete ram1 object */
299 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
300 " {'id': 'ram1' } }");
301 g_assert_nonnull(resp
);
302 g_assert(qdict_haskey(resp
, "return"));
305 /* attempt to set a non existing property */
306 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
307 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
308 " 'sized': 1048576 } }");
309 g_assert_nonnull(resp
);
310 qmp_expect_error_and_unref(resp
, "GenericError");
312 /* now do it right */
313 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
314 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
315 " 'size': 1048576 } }");
316 g_assert_nonnull(resp
);
317 g_assert(qdict_haskey(resp
, "return"));
320 /* delete ram1 object without id */
321 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
322 " {'ida': 'ram1' } }");
323 g_assert_nonnull(resp
);
326 /* delete ram1 object */
327 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
328 " {'id': 'ram1' } }");
329 g_assert_nonnull(resp
);
330 g_assert(qdict_haskey(resp
, "return"));
333 /* delete ram1 object that does not exist anymore*/
334 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
335 " {'id': 'ram1' } }");
336 g_assert_nonnull(resp
);
337 qmp_expect_error_and_unref(resp
, "GenericError");
342 int main(int argc
, char *argv
[])
347 g_test_init(&argc
, &argv
, NULL
);
349 qmp_schema_init(&schema
);
350 add_query_tests(&schema
);
352 qtest_add_func("qmp/object-add-failure-modes",
353 test_object_add_failure_modes
);
357 qmp_schema_cleanup(&schema
);