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 /* Only valid with a USB bus added */
50 { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
51 /* Only valid with accel=tcg */
52 { "x-query-jit", ERROR_CLASS_GENERIC_ERROR },
53 { "x-query-opcount", ERROR_CLASS_GENERIC_ERROR },
54 { "xen-event-list", ERROR_CLASS_GENERIC_ERROR },
59 for (i = 0; fails[i].cmd; i++) {
60 if (!strcmp(cmd, fails[i].cmd)) {
61 return fails[i].err_class;
67 static void test_query(const void *data)
69 const char *cmd = data;
70 int expected_error_class = query_error_class(cmd);
72 const char *error_class;
75 qts = qtest_init(common_args);
77 resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
78 error = qdict_get_qdict(resp, "error");
79 error_class = error ? qdict_get_str(error, "class") : NULL;
81 if (expected_error_class < 0) {
82 g_assert(qdict_haskey(resp, "return"));
85 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
87 ==, expected_error_class);
94 static bool query_is_ignored(const char *cmd)
96 const char *ignored[] = {
97 /* Not actually queries: */
99 /* Success depends on target arch: */
100 "query-cpu-definitions", /* arm, i386, ppc, s390x */
101 "query-gic-capabilities", /* arm */
102 /* Success depends on target-specific build configuration: */
103 "query-pci", /* CONFIG_PCI */
104 "x-query-virtio", /* CONFIG_VIRTIO */
105 /* Success depends on launching SEV guest */
106 "query-sev-launch-measure",
107 /* Success depends on Host or Hypervisor SEV support */
109 "query-sev-capabilities",
111 "query-sgx-capabilities",
112 /* Success depends on enabling dirty page rate limit */
113 "query-vcpu-dirty-limit",
118 for (i = 0; ignored[i]; i++) {
119 if (!strcmp(cmd, ignored[i])) {
127 SchemaInfoList *list;
131 static void qmp_schema_init(QmpSchema *schema)
135 SchemaInfoList *tail;
138 qts = qtest_init(common_args);
140 resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
142 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
143 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
149 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
151 /* Build @schema: hash table mapping entity name to SchemaInfo */
152 for (tail = schema->list; tail; tail = tail->next) {
153 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
157 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
159 return g_hash_table_lookup(schema->hash, name);
162 static void qmp_schema_cleanup(QmpSchema *schema)
164 qapi_free_SchemaInfoList(schema->list);
165 g_hash_table_destroy(schema->hash);
168 static bool object_type_has_mandatory_members(SchemaInfo *type)
170 SchemaInfoObjectMemberList *tail;
172 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
174 for (tail = type->u.object.members; tail; tail = tail->next) {
175 if (!tail->value->q_default) {
183 static void add_query_tests(QmpSchema *schema)
185 SchemaInfoList *tail;
186 SchemaInfo *si, *arg_type, *ret_type;
189 /* Test the query-like commands */
190 for (tail = schema->list; tail; tail = tail->next) {
192 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
196 if (query_is_ignored(si->name)) {
200 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
201 if (object_type_has_mandatory_members(arg_type)) {
205 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
206 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
207 && !ret_type->u.object.members) {
211 test_name = g_strdup_printf("qmp/%s", si->name);
212 qtest_add_data_func(test_name, si->name, test_query);
217 static void test_object_add_failure_modes(void)
222 /* attempt to create an object without props */
223 qts = qtest_init(common_args);
224 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
225 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
226 g_assert_nonnull(resp);
227 qmp_expect_error_and_unref(resp, "GenericError");
229 /* attempt to create an object without qom-type */
230 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
231 " {'id': 'ram1' } }");
232 g_assert_nonnull(resp);
233 qmp_expect_error_and_unref(resp, "GenericError");
235 /* attempt to delete an object that does not exist */
236 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
237 " {'id': 'ram1' } }");
238 g_assert_nonnull(resp);
239 qmp_expect_error_and_unref(resp, "GenericError");
241 /* attempt to create 2 objects with duplicate id */
242 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
243 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
244 " 'size': 1048576 } }");
245 g_assert_nonnull(resp);
246 g_assert(qdict_haskey(resp, "return"));
249 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
250 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
251 " 'size': 1048576 } }");
252 g_assert_nonnull(resp);
253 qmp_expect_error_and_unref(resp, "GenericError");
255 /* delete ram1 object */
256 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
257 " {'id': 'ram1' } }");
258 g_assert_nonnull(resp);
259 g_assert(qdict_haskey(resp, "return"));
262 /* attempt to create an object with a property of a wrong type */
263 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
264 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
265 " 'size': '1048576' } }");
266 g_assert_nonnull(resp);
267 /* now do it right */
268 qmp_expect_error_and_unref(resp, "GenericError");
270 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
271 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
272 " 'size': 1048576 } }");
273 g_assert_nonnull(resp);
274 g_assert(qdict_haskey(resp, "return"));
277 /* delete ram1 object */
278 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
279 " {'id': 'ram1' } }");
280 g_assert_nonnull(resp);
281 g_assert(qdict_haskey(resp, "return"));
284 /* attempt to create an object without the id */
285 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
286 " {'qom-type': 'memory-backend-ram',"
287 " 'size': 1048576 } }");
288 g_assert_nonnull(resp);
289 qmp_expect_error_and_unref(resp, "GenericError");
291 /* now do it right */
292 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
293 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
294 " 'size': 1048576 } }");
295 g_assert_nonnull(resp);
296 g_assert(qdict_haskey(resp, "return"));
299 /* delete ram1 object */
300 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
301 " {'id': 'ram1' } }");
302 g_assert_nonnull(resp);
303 g_assert(qdict_haskey(resp, "return"));
306 /* attempt to set a non existing property */
307 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
308 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
309 " 'sized': 1048576 } }");
310 g_assert_nonnull(resp);
311 qmp_expect_error_and_unref(resp, "GenericError");
313 /* now do it right */
314 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
315 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
316 " 'size': 1048576 } }");
317 g_assert_nonnull(resp);
318 g_assert(qdict_haskey(resp, "return"));
321 /* delete ram1 object without id */
322 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
323 " {'ida': 'ram1' } }");
324 g_assert_nonnull(resp);
327 /* delete ram1 object */
328 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
329 " {'id': 'ram1' } }");
330 g_assert_nonnull(resp);
331 g_assert(qdict_haskey(resp, "return"));
334 /* delete ram1 object that does not exist anymore*/
335 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
336 " {'id': 'ram1' } }");
337 g_assert_nonnull(resp);
338 qmp_expect_error_and_unref(resp, "GenericError");
343 int main(int argc, char *argv[])
348 g_test_init(&argc, &argv, NULL);
350 qmp_schema_init(&schema);
351 add_query_tests(&schema);
353 qtest_add_func("qmp/object-add-failure-modes",
354 test_object_add_failure_modes);
358 qmp_schema_cleanup(&schema);