OSDN Git Service

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