master
c 363 lines 11.2 KB
Raw
1 /*
2 * QMP command test cases
3 *
4 * Copyright (c) 2017 Red Hat Inc.
5 *
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include "libqtest.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-visit-introspect.h"
17 #include "qobject/qdict.h"
18 #include "qapi/qobject-input-visitor.h"
19
20 const char common_args[] = "-nodefaults -machine none";
21
22 /* Query smoke tests */
23
24 static int query_error_class(const char *cmd)
25 {
26 static struct {
27 const char *cmd;
28 int err_class;
29 } fails[] = {
30 /* Success depends on build configuration: */
31 #ifndef CONFIG_SPICE
32 { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
33 #endif
34 #ifndef CONFIG_TCG
35 { "query-replay", ERROR_CLASS_COMMAND_NOT_FOUND },
36 #endif
37 #ifndef CONFIG_VNC
38 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
39 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
40 #endif
41 #ifndef CONFIG_REPLICATION
42 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
43 #endif
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-hv-balloon-status-report", ERROR_CLASS_GENERIC_ERROR },
49 { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
50 /* Only valid with a USB bus added */
51 { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
52 /* Only valid with accel=tcg */
53 { "x-query-jit", ERROR_CLASS_GENERIC_ERROR },
54 { "xen-event-list", ERROR_CLASS_GENERIC_ERROR },
55 /* requires firmware with memory buffer logging support */
56 { "query-firmware-log", ERROR_CLASS_GENERIC_ERROR },
57 { NULL, -1 }
58 };
59 int i;
60
61 for (i = 0; fails[i].cmd; i++) {
62 if (!strcmp(cmd, fails[i].cmd)) {
63 return fails[i].err_class;
64 }
65 }
66 return -1;
67 }
68
69 static void test_query(const void *data)
70 {
71 const char *cmd = data;
72 int expected_error_class = query_error_class(cmd);
73 QDict *resp, *error;
74 const char *error_class;
75 QTestState *qts;
76
77 qts = qtest_init(common_args);
78
79 resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
80 error = qdict_get_qdict(resp, "error");
81 error_class = error ? qdict_get_str(error, "class") : NULL;
82
83 if (expected_error_class < 0) {
84 g_assert(qdict_haskey(resp, "return"));
85 } else {
86 g_assert(error);
87 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
88 -1, &error_abort),
89 ==, expected_error_class);
90 }
91 qobject_unref(resp);
92
93 qtest_quit(qts);
94 }
95
96 static bool query_is_ignored(const char *cmd)
97 {
98 const char *ignored[] = {
99 /* Not actually queries: */
100 "add-fd",
101 /* Success depends on target arch: */
102 "query-cpu-definitions", /* arm, i386, ppc, s390x */
103 "query-gic-capabilities", /* arm */
104 "query-s390x-cpu-polarization", /* s390x */
105 /* Success depends on target-specific build configuration: */
106 "query-pci", /* CONFIG_PCI */
107 "x-query-virtio", /* CONFIG_VIRTIO */
108 /* Success depends on launching SEV guest */
109 "query-sev-launch-measure",
110 /* Success depends on Host or Hypervisor SEV support */
111 "query-sev",
112 "query-sev-capabilities",
113 "query-sgx",
114 "query-sgx-capabilities",
115 /* Success depends on enabling dirty page rate limit */
116 "query-vcpu-dirty-limit",
117 NULL
118 };
119 int i;
120
121 for (i = 0; ignored[i]; i++) {
122 if (!strcmp(cmd, ignored[i])) {
123 return true;
124 }
125 }
126 return false;
127 }
128
129 typedef struct {
130 SchemaInfoList *list;
131 GHashTable *hash;
132 } QmpSchema;
133
134 static void qmp_schema_init(QmpSchema *schema)
135 {
136 QDict *resp;
137 Visitor *qiv;
138 SchemaInfoList *tail;
139 QTestState *qts;
140
141 qts = qtest_init(common_args);
142
143 resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
144
145 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
146 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
147 visit_free(qiv);
148
149 qobject_unref(resp);
150 qtest_quit(qts);
151
152 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
153
154 /* Build @schema: hash table mapping entity name to SchemaInfo */
155 for (tail = schema->list; tail; tail = tail->next) {
156 g_hash_table_insert(schema->hash, tail->value->name, tail->value);
157 }
158 }
159
160 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
161 {
162 return g_hash_table_lookup(schema->hash, name);
163 }
164
165 static void qmp_schema_cleanup(QmpSchema *schema)
166 {
167 qapi_free_SchemaInfoList(schema->list);
168 g_hash_table_destroy(schema->hash);
169 }
170
171 static bool object_type_has_mandatory_members(SchemaInfo *type)
172 {
173 SchemaInfoObjectMemberList *tail;
174
175 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
176
177 for (tail = type->u.object.members; tail; tail = tail->next) {
178 if (!tail->value->q_default) {
179 return true;
180 }
181 }
182
183 return false;
184 }
185
186 static void add_query_tests(QmpSchema *schema)
187 {
188 SchemaInfoList *tail;
189 SchemaInfo *si, *arg_type, *ret_type;
190 char *test_name;
191
192 /* Test the query-like commands */
193 for (tail = schema->list; tail; tail = tail->next) {
194 si = tail->value;
195 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
196 continue;
197 }
198
199 if (query_is_ignored(si->name)) {
200 continue;
201 }
202
203 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
204 if (object_type_has_mandatory_members(arg_type)) {
205 continue;
206 }
207
208 ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
209 if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
210 && !ret_type->u.object.members) {
211 continue;
212 }
213
214 test_name = g_strdup_printf("qmp/%s", si->name);
215 qtest_add_data_func(test_name, si->name, test_query);
216 g_free(test_name);
217 }
218 }
219
220 static void test_object_add_failure_modes(void)
221 {
222 QTestState *qts;
223 QDict *resp;
224
225 /* attempt to create an object without props */
226 qts = qtest_init(common_args);
227 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
228 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
229 g_assert_nonnull(resp);
230 qmp_expect_error_and_unref(resp, "GenericError");
231
232 /* attempt to create an object without qom-type */
233 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
234 " {'id': 'ram1' } }");
235 g_assert_nonnull(resp);
236 qmp_expect_error_and_unref(resp, "GenericError");
237
238 /* attempt to delete an object that does not exist */
239 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
240 " {'id': 'ram1' } }");
241 g_assert_nonnull(resp);
242 qmp_expect_error_and_unref(resp, "GenericError");
243
244 /* attempt to create 2 objects with duplicate id */
245 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
246 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
247 " 'size': 1048576 } }");
248 g_assert_nonnull(resp);
249 g_assert(qdict_haskey(resp, "return"));
250 qobject_unref(resp);
251
252 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
253 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
254 " 'size': 1048576 } }");
255 g_assert_nonnull(resp);
256 qmp_expect_error_and_unref(resp, "GenericError");
257
258 /* delete ram1 object */
259 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
260 " {'id': 'ram1' } }");
261 g_assert_nonnull(resp);
262 g_assert(qdict_haskey(resp, "return"));
263 qobject_unref(resp);
264
265 /* attempt to create an object with a property of a wrong type */
266 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
267 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
268 " 'size': '1048576' } }");
269 g_assert_nonnull(resp);
270 /* now do it right */
271 qmp_expect_error_and_unref(resp, "GenericError");
272
273 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
274 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
275 " 'size': 1048576 } }");
276 g_assert_nonnull(resp);
277 g_assert(qdict_haskey(resp, "return"));
278 qobject_unref(resp);
279
280 /* delete ram1 object */
281 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
282 " {'id': 'ram1' } }");
283 g_assert_nonnull(resp);
284 g_assert(qdict_haskey(resp, "return"));
285 qobject_unref(resp);
286
287 /* attempt to create an object without the id */
288 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
289 " {'qom-type': 'memory-backend-ram',"
290 " 'size': 1048576 } }");
291 g_assert_nonnull(resp);
292 qmp_expect_error_and_unref(resp, "GenericError");
293
294 /* now do it right */
295 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
296 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
297 " 'size': 1048576 } }");
298 g_assert_nonnull(resp);
299 g_assert(qdict_haskey(resp, "return"));
300 qobject_unref(resp);
301
302 /* delete ram1 object */
303 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
304 " {'id': 'ram1' } }");
305 g_assert_nonnull(resp);
306 g_assert(qdict_haskey(resp, "return"));
307 qobject_unref(resp);
308
309 /* attempt to set a non existing property */
310 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
311 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
312 " 'sized': 1048576 } }");
313 g_assert_nonnull(resp);
314 qmp_expect_error_and_unref(resp, "GenericError");
315
316 /* now do it right */
317 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
318 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
319 " 'size': 1048576 } }");
320 g_assert_nonnull(resp);
321 g_assert(qdict_haskey(resp, "return"));
322 qobject_unref(resp);
323
324 /* delete ram1 object without id */
325 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
326 " {'ida': 'ram1' } }");
327 g_assert_nonnull(resp);
328 qobject_unref(resp);
329
330 /* delete ram1 object */
331 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
332 " {'id': 'ram1' } }");
333 g_assert_nonnull(resp);
334 g_assert(qdict_haskey(resp, "return"));
335 qobject_unref(resp);
336
337 /* delete ram1 object that does not exist anymore*/
338 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
339 " {'id': 'ram1' } }");
340 g_assert_nonnull(resp);
341 qmp_expect_error_and_unref(resp, "GenericError");
342
343 qtest_quit(qts);
344 }
345
346 int main(int argc, char *argv[])
347 {
348 QmpSchema schema;
349 int ret;
350
351 g_test_init(&argc, &argv, NULL);
352
353 qmp_schema_init(&schema);
354 add_query_tests(&schema);
355
356 qtest_add_func("qmp/object-add-failure-modes",
357 test_object_add_failure_modes);
358
359 ret = g_test_run();
360
361 qmp_schema_cleanup(&schema);
362 return ret;
363 }