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",
107 for (i
= 0; ignored
[i
]; i
++) {
108 if (!strcmp(cmd
, ignored
[i
])) {
116 SchemaInfoList
*list
;
120 static void qmp_schema_init(QmpSchema
*schema
)
124 SchemaInfoList
*tail
;
127 qts
= qtest_init(common_args
);
129 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
131 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
132 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
138 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
140 /* Build @schema: hash table mapping entity name to SchemaInfo */
141 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
142 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
146 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
148 return g_hash_table_lookup(schema
->hash
, name
);
151 static void qmp_schema_cleanup(QmpSchema
*schema
)
153 qapi_free_SchemaInfoList(schema
->list
);
154 g_hash_table_destroy(schema
->hash
);
157 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
159 SchemaInfoObjectMemberList
*tail
;
161 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
163 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
164 if (!tail
->value
->has_q_default
) {
172 static void add_query_tests(QmpSchema
*schema
)
174 SchemaInfoList
*tail
;
175 SchemaInfo
*si
, *arg_type
, *ret_type
;
178 /* Test the query-like commands */
179 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
181 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
185 if (query_is_ignored(si
->name
)) {
189 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
190 if (object_type_has_mandatory_members(arg_type
)) {
194 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
195 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
196 && !ret_type
->u
.object
.members
) {
200 test_name
= g_strdup_printf("qmp/%s", si
->name
);
201 qtest_add_data_func(test_name
, si
->name
, test_query
);
206 static void test_object_add_failure_modes(void)
211 /* attempt to create an object without props */
212 qts
= qtest_init(common_args
);
213 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
214 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
215 g_assert_nonnull(resp
);
216 qmp_expect_error_and_unref(resp
, "GenericError");
218 /* attempt to create an object without qom-type */
219 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
220 " {'id': 'ram1' } }");
221 g_assert_nonnull(resp
);
222 qmp_expect_error_and_unref(resp
, "GenericError");
224 /* attempt to delete an object that does not exist */
225 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
226 " {'id': 'ram1' } }");
227 g_assert_nonnull(resp
);
228 qmp_expect_error_and_unref(resp
, "GenericError");
230 /* attempt to create 2 objects with duplicate id */
231 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
232 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
233 " 'props': {'size': 1048576 } } }");
234 g_assert_nonnull(resp
);
235 g_assert(qdict_haskey(resp
, "return"));
238 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
239 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
240 " 'props': {'size': 1048576 } } }");
241 g_assert_nonnull(resp
);
242 qmp_expect_error_and_unref(resp
, "GenericError");
244 /* delete ram1 object */
245 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
246 " {'id': 'ram1' } }");
247 g_assert_nonnull(resp
);
248 g_assert(qdict_haskey(resp
, "return"));
251 /* attempt to create an object with a property of a wrong type */
252 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
253 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
254 " 'props': {'size': '1048576' } } }");
255 g_assert_nonnull(resp
);
256 /* now do it right */
257 qmp_expect_error_and_unref(resp
, "GenericError");
259 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
260 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
261 " 'props': {'size': 1048576 } } }");
262 g_assert_nonnull(resp
);
263 g_assert(qdict_haskey(resp
, "return"));
266 /* delete ram1 object */
267 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
268 " {'id': 'ram1' } }");
269 g_assert_nonnull(resp
);
270 g_assert(qdict_haskey(resp
, "return"));
273 /* attempt to create an object without the id */
274 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
275 " {'qom-type': 'memory-backend-ram',"
276 " 'props': {'size': 1048576 } } }");
277 g_assert_nonnull(resp
);
278 qmp_expect_error_and_unref(resp
, "GenericError");
280 /* now do it right */
281 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
282 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
283 " 'props': {'size': 1048576 } } }");
284 g_assert_nonnull(resp
);
285 g_assert(qdict_haskey(resp
, "return"));
288 /* delete ram1 object */
289 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
290 " {'id': 'ram1' } }");
291 g_assert_nonnull(resp
);
292 g_assert(qdict_haskey(resp
, "return"));
295 /* attempt to set a non existing property */
296 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
297 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
298 " 'props': {'sized': 1048576 } } }");
299 g_assert_nonnull(resp
);
300 qmp_expect_error_and_unref(resp
, "GenericError");
302 /* now do it right */
303 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
304 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
305 " 'props': {'size': 1048576 } } }");
306 g_assert_nonnull(resp
);
307 g_assert(qdict_haskey(resp
, "return"));
310 /* delete ram1 object without id */
311 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
312 " {'ida': 'ram1' } }");
313 g_assert_nonnull(resp
);
316 /* delete ram1 object */
317 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
318 " {'id': 'ram1' } }");
319 g_assert_nonnull(resp
);
320 g_assert(qdict_haskey(resp
, "return"));
323 /* delete ram1 object that does not exist anymore*/
324 resp
= qtest_qmp(qts
, "{'execute': 'object-del', 'arguments':"
325 " {'id': 'ram1' } }");
326 g_assert_nonnull(resp
);
327 qmp_expect_error_and_unref(resp
, "GenericError");
332 int main(int argc
, char *argv
[])
337 g_test_init(&argc
, &argv
, NULL
);
339 qmp_schema_init(&schema
);
340 add_query_tests(&schema
);
342 qtest_add_func("qmp/object-add-failure-modes",
343 test_object_add_failure_modes
);
347 qmp_schema_cleanup(&schema
);