Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QTest testcase for QOM |
| 3 | * |
| 4 | * Copyright (c) 2013 SUSE LINUX Products GmbH |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 9 | |
| 10 | #include <glib.h> |
| 11 | #include <string.h> |
Stefan Hajnoczi | 91f32b0 | 2014-02-08 11:41:07 +0100 | [diff] [blame] | 12 | |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 13 | #include "qemu-common.h" |
Stefan Hajnoczi | 91f32b0 | 2014-02-08 11:41:07 +0100 | [diff] [blame] | 14 | #include "libqtest.h" |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/types.h" |
| 17 | |
| 18 | static const char *blacklist_x86[] = { |
| 19 | "xenfv", "xenpv", NULL |
| 20 | }; |
| 21 | |
| 22 | static const struct { |
| 23 | const char *arch; |
| 24 | const char **machine; |
| 25 | } blacklists[] = { |
| 26 | { "i386", blacklist_x86 }, |
| 27 | { "x86_64", blacklist_x86 }, |
| 28 | }; |
| 29 | |
| 30 | static bool is_blacklisted(const char *arch, const char *mach) |
| 31 | { |
| 32 | int i; |
| 33 | const char **p; |
| 34 | |
| 35 | for (i = 0; i < ARRAY_SIZE(blacklists); i++) { |
| 36 | if (!strcmp(blacklists[i].arch, arch)) { |
| 37 | for (p = blacklists[i].machine; *p; p++) { |
| 38 | if (!strcmp(*p, mach)) { |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return false; |
| 45 | } |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 46 | |
Cole Robinson | 0380aef | 2014-04-11 18:07:12 -0400 | [diff] [blame] | 47 | static void test_properties(const char *path, bool recurse) |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 48 | { |
| 49 | char *child_path; |
| 50 | QDict *response, *tuple; |
| 51 | QList *list; |
| 52 | QListEntry *entry; |
| 53 | |
| 54 | g_test_message("Obtaining properties of %s", path); |
| 55 | response = qmp("{ 'execute': 'qom-list'," |
Paolo Bonzini | 563890c | 2014-06-13 09:35:18 +0200 | [diff] [blame] | 56 | " 'arguments': { 'path': %s } }", path); |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 57 | g_assert(response); |
| 58 | |
Cole Robinson | 0380aef | 2014-04-11 18:07:12 -0400 | [diff] [blame] | 59 | if (!recurse) { |
| 60 | return; |
| 61 | } |
| 62 | |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 63 | g_assert(qdict_haskey(response, "return")); |
| 64 | list = qobject_to_qlist(qdict_get(response, "return")); |
| 65 | QLIST_FOREACH_ENTRY(list, entry) { |
| 66 | tuple = qobject_to_qdict(qlist_entry_obj(entry)); |
Cole Robinson | 0380aef | 2014-04-11 18:07:12 -0400 | [diff] [blame] | 67 | bool is_child = strstart(qdict_get_str(tuple, "type"), "child<", NULL); |
| 68 | bool is_link = strstart(qdict_get_str(tuple, "type"), "link<", NULL); |
| 69 | |
| 70 | if (is_child || is_link) { |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 71 | child_path = g_strdup_printf("%s/%s", |
| 72 | path, qdict_get_str(tuple, "name")); |
Cole Robinson | 0380aef | 2014-04-11 18:07:12 -0400 | [diff] [blame] | 73 | test_properties(child_path, is_child); |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 74 | g_free(child_path); |
| 75 | } else { |
| 76 | const char *prop = qdict_get_str(tuple, "name"); |
| 77 | g_test_message("Testing property %s.%s", path, prop); |
| 78 | response = qmp("{ 'execute': 'qom-get'," |
Paolo Bonzini | 563890c | 2014-06-13 09:35:18 +0200 | [diff] [blame] | 79 | " 'arguments': { 'path': %s," |
| 80 | " 'property': %s } }", |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 81 | path, prop); |
| 82 | /* qom-get may fail but should not, e.g., segfault. */ |
| 83 | g_assert(response); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Markus Armbruster | bb6c5e3 | 2014-01-10 14:31:39 +0100 | [diff] [blame] | 88 | static void test_machine(gconstpointer data) |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 89 | { |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 90 | const char *machine = data; |
| 91 | char *args; |
Markus Armbruster | bb6c5e3 | 2014-01-10 14:31:39 +0100 | [diff] [blame] | 92 | QDict *response; |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 93 | |
Andreas Färber | 2ad645d | 2013-11-18 17:36:34 +0100 | [diff] [blame] | 94 | args = g_strdup_printf("-machine %s", machine); |
Markus Armbruster | bb6c5e3 | 2014-01-10 14:31:39 +0100 | [diff] [blame] | 95 | qtest_start(args); |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 96 | |
Cole Robinson | 0380aef | 2014-04-11 18:07:12 -0400 | [diff] [blame] | 97 | test_properties("/machine", true); |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 98 | |
Markus Armbruster | bb6c5e3 | 2014-01-10 14:31:39 +0100 | [diff] [blame] | 99 | response = qmp("{ 'execute': 'quit' }"); |
| 100 | g_assert(qdict_haskey(response, "return")); |
Andreas Färber | dc06cbd | 2014-02-07 15:36:16 +0100 | [diff] [blame] | 101 | |
Markus Armbruster | bb6c5e3 | 2014-01-10 14:31:39 +0100 | [diff] [blame] | 102 | qtest_end(); |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 103 | g_free(args); |
| 104 | } |
| 105 | |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 106 | static void add_machine_test_cases(void) |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 107 | { |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 108 | const char *arch = qtest_get_arch(); |
| 109 | QDict *response, *minfo; |
| 110 | QList *list; |
| 111 | const QListEntry *p; |
| 112 | QObject *qobj; |
| 113 | QString *qstr; |
| 114 | const char *mname, *path; |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 115 | |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 116 | qtest_start("-machine none"); |
| 117 | response = qmp("{ 'execute': 'query-machines' }"); |
| 118 | g_assert(response); |
| 119 | list = qdict_get_qlist(response, "return"); |
| 120 | g_assert(list); |
| 121 | |
| 122 | for (p = qlist_first(list); p; p = qlist_next(p)) { |
| 123 | minfo = qobject_to_qdict(qlist_entry_obj(p)); |
| 124 | g_assert(minfo); |
| 125 | qobj = qdict_get(minfo, "name"); |
| 126 | g_assert(qobj); |
| 127 | qstr = qobject_to_qstring(qobj); |
| 128 | g_assert(qstr); |
| 129 | mname = qstring_get_str(qstr); |
| 130 | if (!is_blacklisted(arch, mname)) { |
Andreas Färber | 53f77e4 | 2015-03-25 18:40:15 +0100 | [diff] [blame] | 131 | path = g_strdup_printf("qom/%s", mname); |
| 132 | qtest_add_data_func(path, mname, test_machine); |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | qtest_end(); |
| 136 | } |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 137 | |
| 138 | int main(int argc, char **argv) |
| 139 | { |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 140 | g_test_init(&argc, &argv, NULL); |
| 141 | |
Markus Armbruster | 5c1904f | 2014-01-10 14:31:38 +0100 | [diff] [blame] | 142 | add_machine_test_cases(); |
Andreas Färber | 7c41f21 | 2013-07-29 05:44:47 +0200 | [diff] [blame] | 143 | |
| 144 | return g_test_run(); |
| 145 | } |