2 * QMP command test cases
4 * Copyright (c) 2017 Red Hat Inc.
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;
65 qtest_start(common_args);
67 resp = qmp("{ 'execute': %s }", cmd);
68 error = qdict_get_qdict(resp, "error");
69 error_class = error ? qdict_get_str(error, "class") : NULL;
71 if (expected_error_class < 0) {
72 g_assert(qdict_haskey(resp, "return"));
75 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
77 ==, expected_error_class);
84 static bool query_is_blacklisted(const char *cmd)
86 const char *blacklist[] = {
87 /* Not actually queries: */
89 /* Success depends on target arch: */
90 "query-cpu-definitions", /* arm, i386, ppc, s390x */
91 "query-gic-capabilities", /* arm */
92 /* Success depends on target-specific build configuration: */
93 "query-pci", /* CONFIG_PCI */
94 /* Success depends on launching SEV guest */
95 "query-sev-launch-measure",
96 /* Success depends on Host or Hypervisor SEV support */
98 "query-sev-capabilities",
103 for (i = 0; blacklist[i]; i++) {
104 if (!strcmp(cmd, blacklist[i])) {
112 SchemaInfoList *list;
116 static void qmp_schema_init(QmpSchema *schema)
120 SchemaInfoList *tail;
122 qtest_start(common_args);
123 resp = qmp("{ 'execute': 'query-qmp-schema' }");
125 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
126 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
132 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
134 /* Build @schema: hash table mapping entity name to SchemaInfo */
135 for (tail = schema->list; tail; tail = tail->next) {
136 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
140 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
142 return g_hash_table_lookup(schema->hash, name);
145 static void qmp_schema_cleanup(QmpSchema *schema)
147 qapi_free_SchemaInfoList(schema->list);
148 g_hash_table_destroy(schema->hash);
151 static bool object_type_has_mandatory_members(SchemaInfo *type)
153 SchemaInfoObjectMemberList *tail;
155 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
157 for (tail = type->u.object.members; tail; tail = tail->next) {
158 if (!tail->value->has_q_default) {
166 static void add_query_tests(QmpSchema *schema)
168 SchemaInfoList *tail;
169 SchemaInfo *si, *arg_type, *ret_type;
172 /* Test the query-like commands */
173 for (tail = schema->list; tail; tail = tail->next) {
175 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
179 if (query_is_blacklisted(si->name)) {
183 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
184 if (object_type_has_mandatory_members(arg_type)) {
188 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
189 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
190 && !ret_type->u.object.members) {
194 test_name = g_strdup_printf("qmp/%s", si->name);
195 qtest_add_data_func(test_name, si->name, test_query);
200 int main(int argc, char *argv[])
205 g_test_init(&argc, &argv, NULL);
207 qmp_schema_init(&schema);
208 add_query_tests(&schema);
211 qmp_schema_cleanup(&schema);