1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include <getopt.h>
21#include "libqtest-single.h"
22#include "qapi/error.h"
23#include "qapi/qmp/qdict.h"
24#include "qemu/module.h"
25#include "qapi/qobject-input-visitor.h"
26#include "qapi/qapi-visit-machine.h"
27#include "qapi/qapi-visit-qom.h"
28#include "libqos/libqos-malloc.h"
29#include "libqos/qgraph.h"
30#include "libqos/qgraph_internal.h"
31#include "libqos/qos_external.h"
32
33static char *old_path;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51static void qos_set_machines_devices_available(void)
52{
53 QDict *response;
54 QDict *args = qdict_new();
55 QObject *ret;
56 Visitor *v;
57 MachineInfoList *mach_info;
58 ObjectTypeInfoList *type_info;
59
60 qtest_start("-machine none");
61 response = qmp("{ 'execute': 'query-machines' }");
62 ret = qdict_get(response, "return");
63
64 v = qobject_input_visitor_new(ret);
65 visit_type_MachineInfoList(v, NULL, &mach_info, &error_abort);
66 visit_free(v);
67 machines_apply_to_node(mach_info);
68 qapi_free_MachineInfoList(mach_info);
69
70 qobject_unref(response);
71
72 qdict_put_bool(args, "abstract", true);
73 qdict_put_str(args, "implements", "device");
74
75 response = qmp("{'execute': 'qom-list-types',"
76 " 'arguments': %p }", args);
77 ret = qdict_get(response, "return");
78
79 v = qobject_input_visitor_new(ret);
80 visit_type_ObjectTypeInfoList(v, NULL, &type_info, &error_abort);
81 visit_free(v);
82 types_apply_to_node(type_info);
83 qapi_free_ObjectTypeInfoList(type_info);
84
85 qtest_end();
86 qobject_unref(response);
87}
88
89
90static void restart_qemu_or_continue(char *path)
91{
92 if (g_test_verbose()) {
93 qos_printf("Run QEMU with: '%s'\n", path);
94 }
95
96
97
98
99
100
101 if (g_strcmp0(old_path, path)) {
102 qtest_end();
103 qos_invalidate_command_line();
104 old_path = g_strdup(path);
105 qtest_start(path);
106 } else {
107 qobject_unref(qmp("{ 'execute': 'system_reset' }"));
108 qmp_eventwait("RESET");
109 }
110}
111
112void qos_invalidate_command_line(void)
113{
114 g_free(old_path);
115 old_path = NULL;
116}
117
118
119
120
121
122
123
124static char **current_path;
125
126const char *qos_get_current_command_line(void)
127{
128 return current_path[0];
129}
130
131void *qos_allocate_objects(QTestState *qts, QGuestAllocator **p_alloc)
132{
133 return allocate_objects(qts, current_path + 1, p_alloc);
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161static void run_one_test(const void *arg)
162{
163 QOSGraphNode *test_node;
164 QGuestAllocator *alloc = NULL;
165 void *obj;
166 char **path = (char **) arg;
167 GString *cmd_line = g_string_new(path[0]);
168 void *test_arg;
169
170
171 current_path = path;
172 test_node = qos_graph_get_node(path[(g_strv_length(path) - 1)]);
173 test_arg = test_node->u.test.arg;
174 if (test_node->u.test.before) {
175 test_arg = test_node->u.test.before(cmd_line, test_arg);
176 }
177
178 restart_qemu_or_continue(cmd_line->str);
179 g_string_free(cmd_line, true);
180
181 obj = qos_allocate_objects(global_qtest, &alloc);
182 test_node->u.test.function(obj, test_arg, alloc);
183}
184
185static void subprocess_run_one_test(const void *arg)
186{
187 const gchar *path = arg;
188 g_test_trap_subprocess(path, 180 * G_USEC_PER_SEC,
189 G_TEST_SUBPROCESS_INHERIT_STDOUT |
190 G_TEST_SUBPROCESS_INHERIT_STDERR);
191 g_test_trap_assert_passed();
192}
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218static void walk_path(QOSGraphNode *orig_path, int len)
219{
220 QOSGraphNode *path;
221 QOSGraphEdge *edge;
222
223
224 QOSEdgeType etype = QEDGE_CONSUMED_BY;
225
226
227 char **path_vec = g_new0(char *, (QOS_PATH_MAX_ELEMENT_SIZE * 2));
228 int path_vec_size = 0;
229
230 char *after_cmd, *before_cmd, *after_device;
231 GString *after_device_str = g_string_new("");
232 char *node_name = orig_path->name, *path_str;
233
234 GString *cmd_line = g_string_new("");
235 GString *cmd_line2 = g_string_new("");
236
237 path = qos_graph_get_node(node_name);
238 node_name = qos_graph_edge_get_dest(path->path_edge);
239
240 path_vec[path_vec_size++] = node_name;
241 path_vec[path_vec_size++] = qos_get_machine_type(node_name);
242
243 for (;;) {
244 path = qos_graph_get_node(node_name);
245 if (!path->path_edge) {
246 break;
247 }
248
249 node_name = qos_graph_edge_get_dest(path->path_edge);
250
251
252 if (path->command_line && etype == QEDGE_CONSUMED_BY) {
253 g_string_append(cmd_line, path->command_line);
254 g_string_append(cmd_line, after_device_str->str);
255 g_string_truncate(after_device_str, 0);
256 }
257
258 path_vec[path_vec_size++] = qos_graph_edge_get_name(path->path_edge);
259
260 after_cmd = qos_graph_edge_get_after_cmd_line(path->path_edge);
261 after_device = qos_graph_edge_get_extra_device_opts(path->path_edge);
262 before_cmd = qos_graph_edge_get_before_cmd_line(path->path_edge);
263 edge = qos_graph_get_edge(path->name, node_name);
264 etype = qos_graph_edge_get_type(edge);
265
266 if (before_cmd) {
267 g_string_append(cmd_line, before_cmd);
268 }
269 if (after_cmd) {
270 g_string_append(cmd_line2, after_cmd);
271 }
272 if (after_device) {
273 g_string_append(after_device_str, after_device);
274 }
275 }
276
277 path_vec[path_vec_size++] = NULL;
278 g_string_append(cmd_line, after_device_str->str);
279 g_string_free(after_device_str, true);
280
281 g_string_append(cmd_line, cmd_line2->str);
282 g_string_free(cmd_line2, true);
283
284
285
286
287 path_str = g_strjoinv("/", path_vec + 1);
288
289
290
291
292 path_vec[1] = path_vec[0];
293 path_vec[0] = g_string_free(cmd_line, false);
294
295 if (path->u.test.subprocess) {
296 gchar *subprocess_path = g_strdup_printf("/%s/%s/subprocess",
297 qtest_get_arch(), path_str);
298 qtest_add_data_func(path_str, subprocess_path, subprocess_run_one_test);
299 g_test_add_data_func(subprocess_path, path_vec, run_one_test);
300 } else {
301 qtest_add_data_func(path_str, path_vec, run_one_test);
302 }
303
304 g_free(path_str);
305}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321int main(int argc, char **argv, char** envp)
322{
323 g_test_init(&argc, &argv, NULL);
324
325 if (g_test_subprocess()) {
326 qos_printf("qos_test running single test in subprocess\n");
327 }
328
329 if (g_test_verbose()) {
330 qos_printf("ENVIRONMENT VARIABLES: {\n");
331 for (char **env = envp; *env != 0; env++) {
332 qos_printf("\t%s\n", *env);
333 }
334 qos_printf("}\n");
335 }
336 qos_graph_init();
337 module_call_init(MODULE_INIT_QOM);
338 module_call_init(MODULE_INIT_LIBQOS);
339 qos_set_machines_devices_available();
340
341 qos_graph_foreach_test_path(walk_path);
342 if (g_test_verbose()) {
343 qos_dump_graph();
344 }
345 g_test_run();
346 qtest_end();
347 qos_graph_destroy();
348 g_free(old_path);
349 return 0;
350}
351