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-vnc", ERROR_CLASS_GENERIC_ERROR
},
36 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR
},
38 #ifndef CONFIG_REPLICATION
39 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND
},
41 /* Likewise, and require special QEMU command-line arguments: */
42 { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR
},
43 { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE
},
44 { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR
},
45 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR
},
50 for (i
= 0; fails
[i
].cmd
; i
++) {
51 if (!strcmp(cmd
, fails
[i
].cmd
)) {
52 return fails
[i
].err_class
;
58 static void test_query(const void *data
)
60 const char *cmd
= data
;
61 int expected_error_class
= query_error_class(cmd
);
63 const char *error_class
;
66 qts
= qtest_init(common_args
);
68 resp
= qtest_qmp(qts
, "{ 'execute': %s }", cmd
);
69 error
= qdict_get_qdict(resp
, "error");
70 error_class
= error
? qdict_get_str(error
, "class") : NULL
;
72 if (expected_error_class
< 0) {
73 g_assert(qdict_haskey(resp
, "return"));
76 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup
, error_class
,
78 ==, expected_error_class
);
85 static bool query_is_blacklisted(const char *cmd
)
87 const char *blacklist
[] = {
88 /* Not actually queries: */
90 /* Success depends on target arch: */
91 "query-cpu-definitions", /* arm, i386, ppc, s390x */
92 "query-gic-capabilities", /* arm */
93 /* Success depends on target-specific build configuration: */
94 "query-pci", /* CONFIG_PCI */
95 /* Success depends on launching SEV guest */
96 "query-sev-launch-measure",
97 /* Success depends on Host or Hypervisor SEV support */
99 "query-sev-capabilities",
104 for (i
= 0; blacklist
[i
]; i
++) {
105 if (!strcmp(cmd
, blacklist
[i
])) {
113 SchemaInfoList
*list
;
117 static void qmp_schema_init(QmpSchema
*schema
)
121 SchemaInfoList
*tail
;
124 qts
= qtest_init(common_args
);
126 resp
= qtest_qmp(qts
, "{ 'execute': 'query-qmp-schema' }");
128 qiv
= qobject_input_visitor_new(qdict_get(resp
, "return"));
129 visit_type_SchemaInfoList(qiv
, NULL
, &schema
->list
, &error_abort
);
135 schema
->hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
137 /* Build @schema: hash table mapping entity name to SchemaInfo */
138 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
139 g_hash_table_insert(schema
->hash
, tail
->value
->name
, tail
->value
);
143 static SchemaInfo
*qmp_schema_lookup(QmpSchema
*schema
, const char *name
)
145 return g_hash_table_lookup(schema
->hash
, name
);
148 static void qmp_schema_cleanup(QmpSchema
*schema
)
150 qapi_free_SchemaInfoList(schema
->list
);
151 g_hash_table_destroy(schema
->hash
);
154 static bool object_type_has_mandatory_members(SchemaInfo
*type
)
156 SchemaInfoObjectMemberList
*tail
;
158 g_assert(type
->meta_type
== SCHEMA_META_TYPE_OBJECT
);
160 for (tail
= type
->u
.object
.members
; tail
; tail
= tail
->next
) {
161 if (!tail
->value
->has_q_default
) {
169 static void add_query_tests(QmpSchema
*schema
)
171 SchemaInfoList
*tail
;
172 SchemaInfo
*si
, *arg_type
, *ret_type
;
175 /* Test the query-like commands */
176 for (tail
= schema
->list
; tail
; tail
= tail
->next
) {
178 if (si
->meta_type
!= SCHEMA_META_TYPE_COMMAND
) {
182 if (query_is_blacklisted(si
->name
)) {
186 arg_type
= qmp_schema_lookup(schema
, si
->u
.command
.arg_type
);
187 if (object_type_has_mandatory_members(arg_type
)) {
191 ret_type
= qmp_schema_lookup(schema
, si
->u
.command
.ret_type
);
192 if (ret_type
->meta_type
== SCHEMA_META_TYPE_OBJECT
193 && !ret_type
->u
.object
.members
) {
197 test_name
= g_strdup_printf("qmp/%s", si
->name
);
198 qtest_add_data_func(test_name
, si
->name
, test_query
);
203 static void test_object_add_without_props(void)
208 qts
= qtest_init(common_args
);
209 resp
= qtest_qmp(qts
, "{'execute': 'object-add', 'arguments':"
210 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
211 g_assert_nonnull(resp
);
212 qmp_assert_error_class(resp
, "GenericError");
216 int main(int argc
, char *argv
[])
221 g_test_init(&argc
, &argv
, NULL
);
223 qmp_schema_init(&schema
);
224 add_query_tests(&schema
);
226 qtest_add_func("qmp/object-add-without-props",
227 test_object_add_without_props
);
228 /* TODO: add coverage of generic object-add failure modes */
232 qmp_schema_cleanup(&schema
);