blob: fde04e7a197554d57895816c66aae137c0fe6ea1 [file] [log] [blame]
Andreas Färber7c41f212013-07-29 05:44:47 +02001/*
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ärber7c41f212013-07-29 05:44:47 +02009
10#include <glib.h>
11#include <string.h>
Stefan Hajnoczi91f32b02014-02-08 11:41:07 +010012
Andreas Färberdc06cbd2014-02-07 15:36:16 +010013#include "qemu-common.h"
Stefan Hajnoczi91f32b02014-02-08 11:41:07 +010014#include "libqtest.h"
Andreas Färber7c41f212013-07-29 05:44:47 +020015#include "qemu/osdep.h"
Markus Armbruster5c1904f2014-01-10 14:31:38 +010016#include "qapi/qmp/types.h"
17
18static const char *blacklist_x86[] = {
19 "xenfv", "xenpv", NULL
20};
21
22static const struct {
23 const char *arch;
24 const char **machine;
25} blacklists[] = {
26 { "i386", blacklist_x86 },
27 { "x86_64", blacklist_x86 },
28};
29
30static 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ärber7c41f212013-07-29 05:44:47 +020046
Cole Robinson0380aef2014-04-11 18:07:12 -040047static void test_properties(const char *path, bool recurse)
Andreas Färberdc06cbd2014-02-07 15:36:16 +010048{
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 Bonzini563890c2014-06-13 09:35:18 +020056 " 'arguments': { 'path': %s } }", path);
Andreas Färberdc06cbd2014-02-07 15:36:16 +010057 g_assert(response);
58
Cole Robinson0380aef2014-04-11 18:07:12 -040059 if (!recurse) {
60 return;
61 }
62
Andreas Färberdc06cbd2014-02-07 15:36:16 +010063 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 Robinson0380aef2014-04-11 18:07:12 -040067 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ärberdc06cbd2014-02-07 15:36:16 +010071 child_path = g_strdup_printf("%s/%s",
72 path, qdict_get_str(tuple, "name"));
Cole Robinson0380aef2014-04-11 18:07:12 -040073 test_properties(child_path, is_child);
Andreas Färberdc06cbd2014-02-07 15:36:16 +010074 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 Bonzini563890c2014-06-13 09:35:18 +020079 " 'arguments': { 'path': %s,"
80 " 'property': %s } }",
Andreas Färberdc06cbd2014-02-07 15:36:16 +010081 path, prop);
82 /* qom-get may fail but should not, e.g., segfault. */
83 g_assert(response);
84 }
85 }
86}
87
Markus Armbrusterbb6c5e32014-01-10 14:31:39 +010088static void test_machine(gconstpointer data)
Andreas Färber7c41f212013-07-29 05:44:47 +020089{
Andreas Färber7c41f212013-07-29 05:44:47 +020090 const char *machine = data;
91 char *args;
Markus Armbrusterbb6c5e32014-01-10 14:31:39 +010092 QDict *response;
Andreas Färber7c41f212013-07-29 05:44:47 +020093
Andreas Färber2ad645d2013-11-18 17:36:34 +010094 args = g_strdup_printf("-machine %s", machine);
Markus Armbrusterbb6c5e32014-01-10 14:31:39 +010095 qtest_start(args);
Andreas Färberdc06cbd2014-02-07 15:36:16 +010096
Cole Robinson0380aef2014-04-11 18:07:12 -040097 test_properties("/machine", true);
Andreas Färberdc06cbd2014-02-07 15:36:16 +010098
Markus Armbrusterbb6c5e32014-01-10 14:31:39 +010099 response = qmp("{ 'execute': 'quit' }");
100 g_assert(qdict_haskey(response, "return"));
Andreas Färberdc06cbd2014-02-07 15:36:16 +0100101
Markus Armbrusterbb6c5e32014-01-10 14:31:39 +0100102 qtest_end();
Andreas Färber7c41f212013-07-29 05:44:47 +0200103 g_free(args);
104}
105
Markus Armbruster5c1904f2014-01-10 14:31:38 +0100106static void add_machine_test_cases(void)
Andreas Färber7c41f212013-07-29 05:44:47 +0200107{
Markus Armbruster5c1904f2014-01-10 14:31:38 +0100108 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ärber7c41f212013-07-29 05:44:47 +0200115
Markus Armbruster5c1904f2014-01-10 14:31:38 +0100116 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ärber53f77e42015-03-25 18:40:15 +0100131 path = g_strdup_printf("qom/%s", mname);
132 qtest_add_data_func(path, mname, test_machine);
Markus Armbruster5c1904f2014-01-10 14:31:38 +0100133 }
134 }
135 qtest_end();
136}
Andreas Färber7c41f212013-07-29 05:44:47 +0200137
138int main(int argc, char **argv)
139{
Andreas Färber7c41f212013-07-29 05:44:47 +0200140 g_test_init(&argc, &argv, NULL);
141
Markus Armbruster5c1904f2014-01-10 14:31:38 +0100142 add_machine_test_cases();
Andreas Färber7c41f212013-07-29 05:44:47 +0200143
144 return g_test_run();
145}