libqtest: Rename qmp_assert_error_class() to qmp_expect_error_and_unref()
[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 "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"
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_VNC
35 { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
36 { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
37 #endif
38 #ifndef CONFIG_REPLICATION
39 { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
40 #endif
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 },
46 { NULL, -1 }
47 };
48 int i;
49
50 for (i = 0; fails[i].cmd; i++) {
51 if (!strcmp(cmd, fails[i].cmd)) {
52 return fails[i].err_class;
53 }
54 }
55 return -1;
56 }
57
58 static void test_query(const void *data)
59 {
60 const char *cmd = data;
61 int expected_error_class = query_error_class(cmd);
62 QDict *resp, *error;
63 const char *error_class;
64 QTestState *qts;
65
66 qts = qtest_init(common_args);
67
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;
71
72 if (expected_error_class < 0) {
73 g_assert(qdict_haskey(resp, "return"));
74 } else {
75 g_assert(error);
76 g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
77 -1, &error_abort),
78 ==, expected_error_class);
79 }
80 qobject_unref(resp);
81
82 qtest_quit(qts);
83 }
84
85 static bool query_is_blacklisted(const char *cmd)
86 {
87 const char *blacklist[] = {
88 /* Not actually queries: */
89 "add-fd",
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 */
98 "query-sev",
99 "query-sev-capabilities",
100 NULL
101 };
102 int i;
103
104 for (i = 0; blacklist[i]; i++) {
105 if (!strcmp(cmd, blacklist[i])) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 typedef struct {
113 SchemaInfoList *list;
114 GHashTable *hash;
115 } QmpSchema;
116
117 static void qmp_schema_init(QmpSchema *schema)
118 {
119 QDict *resp;
120 Visitor *qiv;
121 SchemaInfoList *tail;
122 QTestState *qts;
123
124 qts = qtest_init(common_args);
125
126 resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
127
128 qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
129 visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
130 visit_free(qiv);
131
132 qobject_unref(resp);
133 qtest_quit(qts);
134
135 schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
136
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);
140 }
141 }
142
143 static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
144 {
145 return g_hash_table_lookup(schema->hash, name);
146 }
147
148 static void qmp_schema_cleanup(QmpSchema *schema)
149 {
150 qapi_free_SchemaInfoList(schema->list);
151 g_hash_table_destroy(schema->hash);
152 }
153
154 static bool object_type_has_mandatory_members(SchemaInfo *type)
155 {
156 SchemaInfoObjectMemberList *tail;
157
158 g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
159
160 for (tail = type->u.object.members; tail; tail = tail->next) {
161 if (!tail->value->has_q_default) {
162 return true;
163 }
164 }
165
166 return false;
167 }
168
169 static void add_query_tests(QmpSchema *schema)
170 {
171 SchemaInfoList *tail;
172 SchemaInfo *si, *arg_type, *ret_type;
173 char *test_name;
174
175 /* Test the query-like commands */
176 for (tail = schema->list; tail; tail = tail->next) {
177 si = tail->value;
178 if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
179 continue;
180 }
181
182 if (query_is_blacklisted(si->name)) {
183 continue;
184 }
185
186 arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
187 if (object_type_has_mandatory_members(arg_type)) {
188 continue;
189 }
190
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) {
194 continue;
195 }
196
197 test_name = g_strdup_printf("qmp/%s", si->name);
198 qtest_add_data_func(test_name, si->name, test_query);
199 g_free(test_name);
200 }
201 }
202
203 static void test_object_add_failure_modes(void)
204 {
205 QTestState *qts;
206 QDict *resp;
207
208 /* attempt to create an object without props */
209 qts = qtest_init(common_args);
210 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
211 " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
212 g_assert_nonnull(resp);
213 qmp_expect_error_and_unref(resp, "GenericError");
214
215 /* attempt to create an object without qom-type */
216 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
217 " {'id': 'ram1' } }");
218 g_assert_nonnull(resp);
219 qmp_expect_error_and_unref(resp, "GenericError");
220
221 /* attempt to delete an object that does not exist */
222 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
223 " {'id': 'ram1' } }");
224 g_assert_nonnull(resp);
225 qmp_expect_error_and_unref(resp, "GenericError");
226
227 /* attempt to create 2 objects with duplicate id */
228 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
229 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
230 " 'props': {'size': 1048576 } } }");
231 g_assert_nonnull(resp);
232 g_assert(qdict_haskey(resp, "return"));
233 qobject_unref(resp);
234
235 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
236 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
237 " 'props': {'size': 1048576 } } }");
238 g_assert_nonnull(resp);
239 qmp_expect_error_and_unref(resp, "GenericError");
240
241 /* delete ram1 object */
242 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
243 " {'id': 'ram1' } }");
244 g_assert_nonnull(resp);
245 g_assert(qdict_haskey(resp, "return"));
246 qobject_unref(resp);
247
248 /* attempt to create an object with a property of a wrong type */
249 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
250 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
251 " 'props': {'size': '1048576' } } }");
252 g_assert_nonnull(resp);
253 /* now do it right */
254 qmp_expect_error_and_unref(resp, "GenericError");
255
256 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
257 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
258 " 'props': {'size': 1048576 } } }");
259 g_assert_nonnull(resp);
260 g_assert(qdict_haskey(resp, "return"));
261 qobject_unref(resp);
262
263 /* delete ram1 object */
264 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
265 " {'id': 'ram1' } }");
266 g_assert_nonnull(resp);
267 g_assert(qdict_haskey(resp, "return"));
268 qobject_unref(resp);
269
270 /* attempt to create an object without the id */
271 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
272 " {'qom-type': 'memory-backend-ram',"
273 " 'props': {'size': 1048576 } } }");
274 g_assert_nonnull(resp);
275 qmp_expect_error_and_unref(resp, "GenericError");
276
277 /* now do it right */
278 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
279 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
280 " 'props': {'size': 1048576 } } }");
281 g_assert_nonnull(resp);
282 g_assert(qdict_haskey(resp, "return"));
283 qobject_unref(resp);
284
285 /* delete ram1 object */
286 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
287 " {'id': 'ram1' } }");
288 g_assert_nonnull(resp);
289 g_assert(qdict_haskey(resp, "return"));
290 qobject_unref(resp);
291
292 /* attempt to set a non existing property */
293 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
294 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
295 " 'props': {'sized': 1048576 } } }");
296 g_assert_nonnull(resp);
297 qmp_expect_error_and_unref(resp, "GenericError");
298
299 /* now do it right */
300 resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
301 " {'qom-type': 'memory-backend-ram', 'id': 'ram1',"
302 " 'props': {'size': 1048576 } } }");
303 g_assert_nonnull(resp);
304 g_assert(qdict_haskey(resp, "return"));
305 qobject_unref(resp);
306
307 /* delete ram1 object without id */
308 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
309 " {'ida': 'ram1' } }");
310 g_assert_nonnull(resp);
311 qobject_unref(resp);
312
313 /* delete ram1 object */
314 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
315 " {'id': 'ram1' } }");
316 g_assert_nonnull(resp);
317 g_assert(qdict_haskey(resp, "return"));
318 qobject_unref(resp);
319
320 /* delete ram1 object that does not exist anymore*/
321 resp = qtest_qmp(qts, "{'execute': 'object-del', 'arguments':"
322 " {'id': 'ram1' } }");
323 g_assert_nonnull(resp);
324 qmp_expect_error_and_unref(resp, "GenericError");
325
326 qtest_quit(qts);
327 }
328
329 int main(int argc, char *argv[])
330 {
331 QmpSchema schema;
332 int ret;
333
334 g_test_init(&argc, &argv, NULL);
335
336 qmp_schema_init(&schema);
337 add_query_tests(&schema);
338
339 qtest_add_func("qmp/object-add-failure-modes",
340 test_object_add_failure_modes);
341
342 ret = g_test_run();
343
344 qmp_schema_cleanup(&schema);
345 return ret;
346 }