1 #include "qemu/osdep.h"
5 #include "libqos/libqos.h"
6 #include "libqos/pci.h"
8 /*** Test Setup & Teardown ***/
11 * Launch QEMU with the given command line,
12 * and then set up interrupts and our guest malloc interface.
14 QOSState
*qtest_vboot(QOSOps
*ops
, const char *cmdline_fmt
, va_list ap
)
18 struct QOSState
*qs
= g_malloc(sizeof(QOSState
));
20 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
21 qs
->qts
= qtest_start(cmdline
);
23 qtest_irq_intercept_in(global_qtest
, "ioapic");
24 if (ops
&& ops
->init_allocator
) {
25 qs
->alloc
= ops
->init_allocator(ALLOC_NO_FLAGS
);
33 * Launch QEMU with the given command line,
34 * and then set up interrupts and our guest malloc interface.
36 QOSState
*qtest_boot(QOSOps
*ops
, const char *cmdline_fmt
, ...)
41 va_start(ap
, cmdline_fmt
);
42 qs
= qtest_vboot(ops
, cmdline_fmt
, ap
);
49 * Tear down the QEMU instance.
51 void qtest_shutdown(QOSState
*qs
)
53 if (qs
->alloc
&& qs
->ops
&& qs
->ops
->uninit_allocator
) {
54 qs
->ops
->uninit_allocator(qs
->alloc
);
61 void set_context(QOSState
*s
)
63 global_qtest
= s
->qts
;
66 static QDict
*qmp_execute(const char *command
)
71 fmt
= g_strdup_printf("{ 'execute': '%s' }", command
);
78 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
87 /* Is the machine currently running? */
88 rsp
= qmp_execute("query-status");
89 g_assert(qdict_haskey(rsp
, "return"));
90 sub
= qdict_get_qdict(rsp
, "return");
91 g_assert(qdict_haskey(sub
, "running"));
92 running
= qdict_get_bool(sub
, "running");
95 /* Issue the migrate command. */
96 s
= g_strdup_printf("{ 'execute': 'migrate',"
97 "'arguments': { 'uri': '%s' } }",
101 g_assert(qdict_haskey(rsp
, "return"));
104 /* Wait for STOP event, but only if we were running: */
106 qmp_eventwait("STOP");
109 /* If we were running, we can wait for an event. */
111 migrate_allocator(from
->alloc
, to
->alloc
);
113 qmp_eventwait("RESUME");
117 /* Otherwise, we need to wait: poll until migration is completed. */
119 rsp
= qmp_execute("query-migrate");
120 g_assert(qdict_haskey(rsp
, "return"));
121 sub
= qdict_get_qdict(rsp
, "return");
122 g_assert(qdict_haskey(sub
, "status"));
123 st
= qdict_get_str(sub
, "status");
125 /* "setup", "active", "completed", "failed", "cancelled" */
126 if (strcmp(st
, "completed") == 0) {
131 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)) {
137 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
138 g_assert_not_reached();
141 migrate_allocator(from
->alloc
, to
->alloc
);
145 bool have_qemu_img(void)
148 const char *path
= getenv("QTEST_QEMU_IMG");
153 rpath
= realpath(path
, NULL
);
162 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
170 char *qemu_img_abs_path
;
172 qemu_img_path
= getenv("QTEST_QEMU_IMG");
173 g_assert(qemu_img_path
);
174 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
175 g_assert(qemu_img_abs_path
);
177 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
179 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
181 fprintf(stderr
, "%s\n", err
->message
);
184 g_assert(ret
&& !err
);
186 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
187 * glib 2.43.91 implementation assumes that any non-zero is an error for
188 * windows, but uses extra precautions for Linux. However,
189 * 0 is only possible if the program exited normally, so that should be
190 * sufficient for our purposes on all platforms, here. */
192 fprintf(stderr
, "qemu-img returned status code %d\n", rc
);
199 free(qemu_img_abs_path
);
202 void mkqcow2(const char *file
, unsigned size_mb
)
204 return mkimg(file
, "qcow2", size_mb
);
207 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
209 FILE *debug_file
= fopen(debug_fn
, "w");
212 fprintf(debug_file
, "[inject-error]\n");
213 fprintf(debug_file
, "event = \"%s\"\n", event
);
214 fprintf(debug_file
, "errno = \"5\"\n");
215 fprintf(debug_file
, "state = \"1\"\n");
216 fprintf(debug_file
, "immediately = \"off\"\n");
217 fprintf(debug_file
, "once = \"on\"\n");
219 fprintf(debug_file
, "[set-state]\n");
220 fprintf(debug_file
, "event = \"%s\"\n", event
);
221 fprintf(debug_file
, "new_state = \"2\"\n");
223 g_assert(!ferror(debug_file
));
225 ret
= fclose(debug_file
);
229 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
232 unsigned char *tx
= (unsigned char *)buffer
;
236 /* Write an indicative pattern that varies and is unique per-cycle */
238 for (i
= 0; i
< len
; i
++) {
240 if (i
% cycle_len
== 0) {
245 /* force uniqueness by writing an id per-cycle */
246 for (i
= 0; i
< len
/ cycle_len
; i
++) {
248 if (j
+ sizeof(*sx
) <= len
) {
249 sx
= (size_t *)&tx
[j
];