]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/libqos/libqos.c
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
);
24 if (ops
->init_allocator
) {
25 qs
->alloc
= ops
->init_allocator(ALLOC_NO_FLAGS
);
27 if (ops
->qpci_init
&& qs
->alloc
) {
28 qs
->pcibus
= ops
->qpci_init(qs
->alloc
);
37 * Launch QEMU with the given command line,
38 * and then set up interrupts and our guest malloc interface.
40 QOSState
*qtest_boot(QOSOps
*ops
, const char *cmdline_fmt
, ...)
45 va_start(ap
, cmdline_fmt
);
46 qs
= qtest_vboot(ops
, cmdline_fmt
, ap
);
53 * Tear down the QEMU instance.
55 void qtest_common_shutdown(QOSState
*qs
)
58 if (qs
->pcibus
&& qs
->ops
->qpci_free
) {
59 qs
->ops
->qpci_free(qs
->pcibus
);
62 if (qs
->alloc
&& qs
->ops
->uninit_allocator
) {
63 qs
->ops
->uninit_allocator(qs
->alloc
);
71 void qtest_shutdown(QOSState
*qs
)
73 if (qs
->ops
&& qs
->ops
->shutdown
) {
74 qs
->ops
->shutdown(qs
);
76 qtest_common_shutdown(qs
);
80 void set_context(QOSState
*s
)
82 global_qtest
= s
->qts
;
85 static QDict
*qmp_execute(const char *command
)
90 fmt
= g_strdup_printf("{ 'execute': '%s' }", command
);
97 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
106 /* Is the machine currently running? */
107 rsp
= qmp_execute("query-status");
108 g_assert(qdict_haskey(rsp
, "return"));
109 sub
= qdict_get_qdict(rsp
, "return");
110 g_assert(qdict_haskey(sub
, "running"));
111 running
= qdict_get_bool(sub
, "running");
114 /* Issue the migrate command. */
115 s
= g_strdup_printf("{ 'execute': 'migrate',"
116 "'arguments': { 'uri': '%s' } }",
120 g_assert(qdict_haskey(rsp
, "return"));
123 /* Wait for STOP event, but only if we were running: */
125 qmp_eventwait("STOP");
128 /* If we were running, we can wait for an event. */
130 migrate_allocator(from
->alloc
, to
->alloc
);
132 qmp_eventwait("RESUME");
136 /* Otherwise, we need to wait: poll until migration is completed. */
138 rsp
= qmp_execute("query-migrate");
139 g_assert(qdict_haskey(rsp
, "return"));
140 sub
= qdict_get_qdict(rsp
, "return");
141 g_assert(qdict_haskey(sub
, "status"));
142 st
= qdict_get_str(sub
, "status");
144 /* "setup", "active", "completed", "failed", "cancelled" */
145 if (strcmp(st
, "completed") == 0) {
150 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)) {
156 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
157 g_assert_not_reached();
160 migrate_allocator(from
->alloc
, to
->alloc
);
164 bool have_qemu_img(void)
167 const char *path
= getenv("QTEST_QEMU_IMG");
172 rpath
= realpath(path
, NULL
);
181 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
189 char *qemu_img_abs_path
;
191 qemu_img_path
= getenv("QTEST_QEMU_IMG");
192 g_assert(qemu_img_path
);
193 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
194 g_assert(qemu_img_abs_path
);
196 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
198 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
200 fprintf(stderr
, "%s\n", err
->message
);
203 g_assert(ret
&& !err
);
205 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
206 * glib 2.43.91 implementation assumes that any non-zero is an error for
207 * windows, but uses extra precautions for Linux. However,
208 * 0 is only possible if the program exited normally, so that should be
209 * sufficient for our purposes on all platforms, here. */
211 fprintf(stderr
, "qemu-img returned status code %d\n", rc
);
218 free(qemu_img_abs_path
);
221 void mkqcow2(const char *file
, unsigned size_mb
)
223 return mkimg(file
, "qcow2", size_mb
);
226 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
228 FILE *debug_file
= fopen(debug_fn
, "w");
231 fprintf(debug_file
, "[inject-error]\n");
232 fprintf(debug_file
, "event = \"%s\"\n", event
);
233 fprintf(debug_file
, "errno = \"5\"\n");
234 fprintf(debug_file
, "state = \"1\"\n");
235 fprintf(debug_file
, "immediately = \"off\"\n");
236 fprintf(debug_file
, "once = \"on\"\n");
238 fprintf(debug_file
, "[set-state]\n");
239 fprintf(debug_file
, "event = \"%s\"\n", event
);
240 fprintf(debug_file
, "new_state = \"2\"\n");
242 g_assert(!ferror(debug_file
));
244 ret
= fclose(debug_file
);
248 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
251 unsigned char *tx
= (unsigned char *)buffer
;
255 /* Write an indicative pattern that varies and is unique per-cycle */
257 for (i
= 0; i
< len
; i
++) {
259 if (i
% cycle_len
== 0) {
264 /* force uniqueness by writing an id per-cycle */
265 for (i
= 0; i
< len
/ cycle_len
; i
++) {
267 if (j
+ sizeof(*sx
) <= len
) {
268 sx
= (size_t *)&tx
[j
];