2
0

qmp-cmd-test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "qemu/osdep.h"
  13. #include "libqtest.h"
  14. #include "qapi/error.h"
  15. #include "qapi/qapi-visit-introspect.h"
  16. #include "qapi/qmp/qdict.h"
  17. #include "qapi/qobject-input-visitor.h"
  18. const char common_args[] = "-nodefaults -machine none";
  19. /* Query smoke tests */
  20. static int query_error_class(const char *cmd)
  21. {
  22. static struct {
  23. const char *cmd;
  24. int err_class;
  25. } fails[] = {
  26. /* Success depends on build configuration: */
  27. #ifndef CONFIG_SPICE
  28. { "query-spice", ERROR_CLASS_COMMAND_NOT_FOUND },
  29. #endif
  30. #ifndef CONFIG_VNC
  31. { "query-vnc", ERROR_CLASS_GENERIC_ERROR },
  32. { "query-vnc-servers", ERROR_CLASS_GENERIC_ERROR },
  33. #endif
  34. #ifndef CONFIG_REPLICATION
  35. { "query-xen-replication-status", ERROR_CLASS_COMMAND_NOT_FOUND },
  36. #endif
  37. /* Likewise, and require special QEMU command-line arguments: */
  38. { "query-acpi-ospm-status", ERROR_CLASS_GENERIC_ERROR },
  39. { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
  40. { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
  41. { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
  42. { NULL, -1 }
  43. };
  44. int i;
  45. for (i = 0; fails[i].cmd; i++) {
  46. if (!strcmp(cmd, fails[i].cmd)) {
  47. return fails[i].err_class;
  48. }
  49. }
  50. return -1;
  51. }
  52. static void test_query(const void *data)
  53. {
  54. const char *cmd = data;
  55. int expected_error_class = query_error_class(cmd);
  56. QDict *resp, *error;
  57. const char *error_class;
  58. QTestState *qts;
  59. qts = qtest_init(common_args);
  60. resp = qtest_qmp(qts, "{ 'execute': %s }", cmd);
  61. error = qdict_get_qdict(resp, "error");
  62. error_class = error ? qdict_get_str(error, "class") : NULL;
  63. if (expected_error_class < 0) {
  64. g_assert(qdict_haskey(resp, "return"));
  65. } else {
  66. g_assert(error);
  67. g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
  68. -1, &error_abort),
  69. ==, expected_error_class);
  70. }
  71. qobject_unref(resp);
  72. qtest_quit(qts);
  73. }
  74. static bool query_is_blacklisted(const char *cmd)
  75. {
  76. const char *blacklist[] = {
  77. /* Not actually queries: */
  78. "add-fd",
  79. /* Success depends on target arch: */
  80. "query-cpu-definitions", /* arm, i386, ppc, s390x */
  81. "query-gic-capabilities", /* arm */
  82. /* Success depends on target-specific build configuration: */
  83. "query-pci", /* CONFIG_PCI */
  84. /* Success depends on launching SEV guest */
  85. "query-sev-launch-measure",
  86. /* Success depends on Host or Hypervisor SEV support */
  87. "query-sev",
  88. "query-sev-capabilities",
  89. NULL
  90. };
  91. int i;
  92. for (i = 0; blacklist[i]; i++) {
  93. if (!strcmp(cmd, blacklist[i])) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. typedef struct {
  100. SchemaInfoList *list;
  101. GHashTable *hash;
  102. } QmpSchema;
  103. static void qmp_schema_init(QmpSchema *schema)
  104. {
  105. QDict *resp;
  106. Visitor *qiv;
  107. SchemaInfoList *tail;
  108. QTestState *qts;
  109. qts = qtest_init(common_args);
  110. resp = qtest_qmp(qts, "{ 'execute': 'query-qmp-schema' }");
  111. qiv = qobject_input_visitor_new(qdict_get(resp, "return"));
  112. visit_type_SchemaInfoList(qiv, NULL, &schema->list, &error_abort);
  113. visit_free(qiv);
  114. qobject_unref(resp);
  115. qtest_quit(qts);
  116. schema->hash = g_hash_table_new(g_str_hash, g_str_equal);
  117. /* Build @schema: hash table mapping entity name to SchemaInfo */
  118. for (tail = schema->list; tail; tail = tail->next) {
  119. g_hash_table_insert(schema->hash, tail->value->name, tail->value);
  120. }
  121. }
  122. static SchemaInfo *qmp_schema_lookup(QmpSchema *schema, const char *name)
  123. {
  124. return g_hash_table_lookup(schema->hash, name);
  125. }
  126. static void qmp_schema_cleanup(QmpSchema *schema)
  127. {
  128. qapi_free_SchemaInfoList(schema->list);
  129. g_hash_table_destroy(schema->hash);
  130. }
  131. static bool object_type_has_mandatory_members(SchemaInfo *type)
  132. {
  133. SchemaInfoObjectMemberList *tail;
  134. g_assert(type->meta_type == SCHEMA_META_TYPE_OBJECT);
  135. for (tail = type->u.object.members; tail; tail = tail->next) {
  136. if (!tail->value->has_q_default) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. static void add_query_tests(QmpSchema *schema)
  143. {
  144. SchemaInfoList *tail;
  145. SchemaInfo *si, *arg_type, *ret_type;
  146. char *test_name;
  147. /* Test the query-like commands */
  148. for (tail = schema->list; tail; tail = tail->next) {
  149. si = tail->value;
  150. if (si->meta_type != SCHEMA_META_TYPE_COMMAND) {
  151. continue;
  152. }
  153. if (query_is_blacklisted(si->name)) {
  154. continue;
  155. }
  156. arg_type = qmp_schema_lookup(schema, si->u.command.arg_type);
  157. if (object_type_has_mandatory_members(arg_type)) {
  158. continue;
  159. }
  160. ret_type = qmp_schema_lookup(schema, si->u.command.ret_type);
  161. if (ret_type->meta_type == SCHEMA_META_TYPE_OBJECT
  162. && !ret_type->u.object.members) {
  163. continue;
  164. }
  165. test_name = g_strdup_printf("qmp/%s", si->name);
  166. qtest_add_data_func(test_name, si->name, test_query);
  167. g_free(test_name);
  168. }
  169. }
  170. static void test_object_add_without_props(void)
  171. {
  172. QTestState *qts;
  173. QDict *resp;
  174. qts = qtest_init(common_args);
  175. resp = qtest_qmp(qts, "{'execute': 'object-add', 'arguments':"
  176. " {'qom-type': 'memory-backend-ram', 'id': 'ram1' } }");
  177. g_assert_nonnull(resp);
  178. qmp_assert_error_class(resp, "GenericError");
  179. qtest_quit(qts);
  180. }
  181. int main(int argc, char *argv[])
  182. {
  183. QmpSchema schema;
  184. int ret;
  185. g_test_init(&argc, &argv, NULL);
  186. qmp_schema_init(&schema);
  187. add_query_tests(&schema);
  188. qtest_add_func("qmp/object-add-without-props",
  189. test_object_add_without_props);
  190. /* TODO: add coverage of generic object-add failure modes */
  191. ret = g_test_run();
  192. qmp_schema_cleanup(&schema);
  193. return ret;
  194. }