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 * Terminates the application in case an error is encountered.
16 QOSState
*qtest_vboot(QOSOps
*ops
, const char *cmdline_fmt
, va_list ap
)
20 struct QOSState
*qs
= g_malloc(sizeof(QOSState
));
22 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
23 qs
->qts
= qtest_start(cmdline
);
26 if (ops
->init_allocator
) {
27 qs
->alloc
= ops
->init_allocator(ALLOC_NO_FLAGS
);
29 if (ops
->qpci_init
&& qs
->alloc
) {
30 qs
->pcibus
= ops
->qpci_init(qs
->alloc
);
39 * Launch QEMU with the given command line,
40 * and then set up interrupts and our guest malloc interface.
42 QOSState
*qtest_boot(QOSOps
*ops
, const char *cmdline_fmt
, ...)
47 va_start(ap
, cmdline_fmt
);
48 qs
= qtest_vboot(ops
, cmdline_fmt
, ap
);
55 * Tear down the QEMU instance.
57 void qtest_common_shutdown(QOSState
*qs
)
60 if (qs
->pcibus
&& qs
->ops
->qpci_free
) {
61 qs
->ops
->qpci_free(qs
->pcibus
);
64 if (qs
->alloc
&& qs
->ops
->uninit_allocator
) {
65 qs
->ops
->uninit_allocator(qs
->alloc
);
73 void qtest_shutdown(QOSState
*qs
)
75 if (qs
->ops
&& qs
->ops
->shutdown
) {
76 qs
->ops
->shutdown(qs
);
78 qtest_common_shutdown(qs
);
82 void set_context(QOSState
*s
)
84 global_qtest
= s
->qts
;
87 static QDict
*qmp_execute(const char *command
)
92 fmt
= g_strdup_printf("{ 'execute': '%s' }", command
);
99 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
108 /* Is the machine currently running? */
109 rsp
= qmp_execute("query-status");
110 g_assert(qdict_haskey(rsp
, "return"));
111 sub
= qdict_get_qdict(rsp
, "return");
112 g_assert(qdict_haskey(sub
, "running"));
113 running
= qdict_get_bool(sub
, "running");
116 /* Issue the migrate command. */
117 s
= g_strdup_printf("{ 'execute': 'migrate',"
118 "'arguments': { 'uri': '%s' } }",
122 g_assert(qdict_haskey(rsp
, "return"));
125 /* Wait for STOP event, but only if we were running: */
127 qmp_eventwait("STOP");
130 /* If we were running, we can wait for an event. */
132 migrate_allocator(from
->alloc
, to
->alloc
);
134 qmp_eventwait("RESUME");
138 /* Otherwise, we need to wait: poll until migration is completed. */
140 rsp
= qmp_execute("query-migrate");
141 g_assert(qdict_haskey(rsp
, "return"));
142 sub
= qdict_get_qdict(rsp
, "return");
143 g_assert(qdict_haskey(sub
, "status"));
144 st
= qdict_get_str(sub
, "status");
146 /* "setup", "active", "completed", "failed", "cancelled" */
147 if (strcmp(st
, "completed") == 0) {
152 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)) {
158 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
159 g_assert_not_reached();
162 migrate_allocator(from
->alloc
, to
->alloc
);
166 bool have_qemu_img(void)
169 const char *path
= getenv("QTEST_QEMU_IMG");
174 rpath
= realpath(path
, NULL
);
183 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
191 char *qemu_img_abs_path
;
193 qemu_img_path
= getenv("QTEST_QEMU_IMG");
194 g_assert(qemu_img_path
);
195 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
196 g_assert(qemu_img_abs_path
);
198 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
200 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
202 fprintf(stderr
, "%s\n", err
->message
);
205 g_assert(ret
&& !err
);
207 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
208 * glib 2.43.91 implementation assumes that any non-zero is an error for
209 * windows, but uses extra precautions for Linux. However,
210 * 0 is only possible if the program exited normally, so that should be
211 * sufficient for our purposes on all platforms, here. */
213 fprintf(stderr
, "qemu-img returned status code %d\n", rc
);
220 free(qemu_img_abs_path
);
223 void mkqcow2(const char *file
, unsigned size_mb
)
225 return mkimg(file
, "qcow2", size_mb
);
228 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
230 FILE *debug_file
= fopen(debug_fn
, "w");
233 fprintf(debug_file
, "[inject-error]\n");
234 fprintf(debug_file
, "event = \"%s\"\n", event
);
235 fprintf(debug_file
, "errno = \"5\"\n");
236 fprintf(debug_file
, "state = \"1\"\n");
237 fprintf(debug_file
, "immediately = \"off\"\n");
238 fprintf(debug_file
, "once = \"on\"\n");
240 fprintf(debug_file
, "[set-state]\n");
241 fprintf(debug_file
, "event = \"%s\"\n", event
);
242 fprintf(debug_file
, "new_state = \"2\"\n");
244 g_assert(!ferror(debug_file
));
246 ret
= fclose(debug_file
);
250 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
253 unsigned char *tx
= (unsigned char *)buffer
;
257 /* Write an indicative pattern that varies and is unique per-cycle */
259 for (i
= 0; i
< len
; i
++) {
261 if (i
% cycle_len
== 0) {
266 /* force uniqueness by writing an id per-cycle */
267 for (i
= 0; i
< len
/ cycle_len
; i
++) {
269 if (j
+ sizeof(*sx
) <= len
) {
270 sx
= (size_t *)&tx
[j
];