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"
14 #include "libqos/libqtest.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
},
53 for (i
= 0; fails
[i
].cmd
; i
++) {
54 if (!strcmp(cmd
, fails
[i
].cmd
)) {
55 return fails
[i
].err_class
;
61 static void test_query(const void *data
)
63 const char *cmd
= data
;
64 int expected_error_class
= query_error_class(cmd
);
66 const char *error_class
;
69 qts
= qtest_init(common_args
);
71 resp
= qtest_qmp(qts
, "{ 'execute': %s }", cmd
);
72 error
= qdict_get_qdict(resp
, "error");
73 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
75 if (expected_error_class
< 0) {
76 g_assert(qdict_haskey(resp
, "return"));
79 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
81 ==, expected_error_class
);
88 static bool query_is_ignored(const char *cmd
)
90 const char *ignored
[] = {
91 /* Not actually queries: */
93 /* Success depends on target arch: */
94 "query-cpu-definitions", /* arm, i386, ppc, s390x */
95 "query-gic-capabilities", /* arm */
96 /* Success depends on target-specific build configuration: */
97 "query-pci", /* CONFIG_PCI */
98 /* Success depends on launching SEV guest */
99 "query-sev-launch-measure",
100 /* Success depends on Host or Hypervisor SEV support */
102 "query-sev-capabilities",
104 "query-sgx-capabilities",
109 for (i
= 0; ignored
[i
]; i
++) {
110 if (!strcmp(cmd
, ignored
[i
])) {
118 SchemaInfoList
*list
;
122 static void qmp_schema_init(QmpSchema
*schema
)
126 SchemaInfoList
*tail
;
129 qts
= qtest_init(common_args
);
131 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
133 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
134 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
140 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
142 /* Build @schema: hash table mapping entity name to SchemaInfo */
143 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
144 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
148 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
150 return g_hash_table_lookup(schema
->hash
, name
);
153 static void qmp_schema_cleanup(QmpSchema
*schema
)
155 qapi_free_SchemaInfoList(schema
->list
);
156 g_hash_table_destroy(schema
->hash
);
159 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
161 SchemaInfoObjectMemberList
*tail
;
163 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
165 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
166 if (!tail
->value
->has_q_default
) {
174 static void add_query_tests(QmpSchema
*schema
)
176 SchemaInfoList
*tail
;
177 SchemaInfo
*si
, *arg_type
, *ret_type
;
180 /* Test the query-like commands */
181 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
183 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
187 if (query_is_ignored(si
->name
)) {
191 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
192 if (object_type_has_mandatory_members(arg_type
)) {
196 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
197 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
198 && !ret_type
->u
.object
.members
) {
202 test_name
= g_strdup_printf("qmp/%s", si
->name
);
203 qtest_add_data_func(test_name
, si
->name
, test_query
);
208 static void test_object_add_failure_modes(void)
213 /* attempt to create an object without props */
214 qts
= qtest_init(common_args
);
215 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
216 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
217 g_assert_nonnull(resp
);
218 qmp_expect_error_and_unref(resp
, "GenericError");
220 /* attempt to create an object without qom-type */
221 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
222 " {'id': 'ram1' } }");
223 g_assert_nonnull(resp
);
224 qmp_expect_error_and_unref(resp
, "GenericError");
226 /* attempt to delete an object that does not exist */
227 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
228 " {'id': 'ram1' } }");
229 g_assert_nonnull(resp
);
230 qmp_expect_error_and_unref(resp
, "GenericError");
232 /* attempt to create 2 objects with duplicate id */
233 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
234 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
235 " 'size': 1048576 } }");
236 g_assert_nonnull(resp
);
237 g_assert(qdict_haskey(resp
, "return"));
240 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
241 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
242 " 'size': 1048576 } }");
243 g_assert_nonnull(resp
);
244 qmp_expect_error_and_unref(resp
, "GenericError");
246 /* delete ram1 object */
247 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
248 " {'id': 'ram1' } }");
249 g_assert_nonnull(resp
);
250 g_assert(qdict_haskey(resp
, "return"));
253 /* attempt to create an object with a property of a wrong type */
254 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
255 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
256 " 'size': '1048576' } }");
257 g_assert_nonnull(resp
);
258 /* now do it right */
259 qmp_expect_error_and_unref(resp
, "GenericError");
261 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
262 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
263 " 'size': 1048576 } }");
264 g_assert_nonnull(resp
);
265 g_assert(qdict_haskey(resp
, "return"));
268 /* delete ram1 object */
269 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
270 " {'id': 'ram1' } }");
271 g_assert_nonnull(resp
);
272 g_assert(qdict_haskey(resp
, "return"));
275 /* attempt to create an object without the id */
276 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
277 " {'qom-type': 'memory-backend-ram',"
278 " 'size': 1048576 } }");
279 g_assert_nonnull(resp
);
280 qmp_expect_error_and_unref(resp
, "GenericError");
282 /* now do it right */
283 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
284 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
285 " 'size': 1048576 } }");
286 g_assert_nonnull(resp
);
287 g_assert(qdict_haskey(resp
, "return"));
290 /* delete ram1 object */
291 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
292 " {'id': 'ram1' } }");
293 g_assert_nonnull(resp
);
294 g_assert(qdict_haskey(resp
, "return"));
297 /* attempt to set a non existing property */
298 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
299 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
300 " 'sized': 1048576 } }");
301 g_assert_nonnull(resp
);
302 qmp_expect_error_and_unref(resp
, "GenericError");
304 /* now do it right */
305 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
306 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
307 " 'size': 1048576 } }");
308 g_assert_nonnull(resp
);
309 g_assert(qdict_haskey(resp
, "return"));
312 /* delete ram1 object without id */
313 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
314 " {'ida': 'ram1' } }");
315 g_assert_nonnull(resp
);
318 /* delete ram1 object */
319 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
320 " {'id': 'ram1' } }");
321 g_assert_nonnull(resp
);
322 g_assert(qdict_haskey(resp
, "return"));
325 /* delete ram1 object that does not exist anymore*/
326 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
327 " {'id': 'ram1' } }");
328 g_assert_nonnull(resp
);
329 qmp_expect_error_and_unref(resp
, "GenericError");
334 int main(int argc
, char *argv
[])
339 g_test_init(&argc
, &argv
, NULL
);
341 qmp_schema_init(&schema
);
342 add_query_tests(&schema
);
344 qtest_add_func("qmp/object-add-failure-modes",
345 test_object_add_failure_modes
);
349 qmp_schema_cleanup(&schema
);