]>
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"
7 #include "qapi/qmp/qdict.h"
9 /*** Test Setup & Teardown ***/
12 * Launch QEMU with the given command line,
13 * and then set up interrupts and our guest malloc interface.
15 * Terminates the application in case an error is encountered.
17 QOSState
*qtest_vboot(QOSOps
*ops
, const char *cmdline_fmt
, va_list ap
)
21 QOSState
*qs
= g_new0(QOSState
, 1);
23 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
24 qs
->qts
= qtest_start(cmdline
);
27 qs
->alloc
= ops
->init_allocator(ALLOC_NO_FLAGS
);
28 qs
->pcibus
= ops
->qpci_init(qs
->qts
, qs
->alloc
);
36 * Launch QEMU with the given command line,
37 * and then set up interrupts and our guest malloc interface.
39 QOSState
*qtest_boot(QOSOps
*ops
, const char *cmdline_fmt
, ...)
44 va_start(ap
, cmdline_fmt
);
45 qs
= qtest_vboot(ops
, cmdline_fmt
, ap
);
52 * Tear down the QEMU instance.
54 void qtest_common_shutdown(QOSState
*qs
)
57 if (qs
->pcibus
&& qs
->ops
->qpci_free
) {
58 qs
->ops
->qpci_free(qs
->pcibus
);
61 if (qs
->alloc
&& qs
->ops
->uninit_allocator
) {
62 qs
->ops
->uninit_allocator(qs
->alloc
);
70 void qtest_shutdown(QOSState
*qs
)
72 if (qs
->ops
&& qs
->ops
->shutdown
) {
73 qs
->ops
->shutdown(qs
);
75 qtest_common_shutdown(qs
);
79 void set_context(QOSState
*s
)
81 global_qtest
= s
->qts
;
84 static QDict
*qmp_execute(const char *command
)
89 fmt
= g_strdup_printf("{ 'execute': '%s' }", command
);
96 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
105 /* Is the machine currently running? */
106 rsp
= qmp_execute("query-status");
107 g_assert(qdict_haskey(rsp
, "return"));
108 sub
= qdict_get_qdict(rsp
, "return");
109 g_assert(qdict_haskey(sub
, "running"));
110 running
= qdict_get_bool(sub
, "running");
113 /* Issue the migrate command. */
114 s
= g_strdup_printf("{ 'execute': 'migrate',"
115 "'arguments': { 'uri': '%s' } }",
119 g_assert(qdict_haskey(rsp
, "return"));
122 /* Wait for STOP event, but only if we were running: */
124 qmp_eventwait("STOP");
127 /* If we were running, we can wait for an event. */
129 migrate_allocator(from
->alloc
, to
->alloc
);
131 qmp_eventwait("RESUME");
135 /* Otherwise, we need to wait: poll until migration is completed. */
137 rsp
= qmp_execute("query-migrate");
138 g_assert(qdict_haskey(rsp
, "return"));
139 sub
= qdict_get_qdict(rsp
, "return");
140 g_assert(qdict_haskey(sub
, "status"));
141 st
= qdict_get_str(sub
, "status");
143 /* "setup", "active", "completed", "failed", "cancelled" */
144 if (strcmp(st
, "completed") == 0) {
149 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)) {
155 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
156 g_assert_not_reached();
159 migrate_allocator(from
->alloc
, to
->alloc
);
163 bool have_qemu_img(void)
166 const char *path
= getenv("QTEST_QEMU_IMG");
171 rpath
= realpath(path
, NULL
);
180 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
188 char *qemu_img_abs_path
;
190 qemu_img_path
= getenv("QTEST_QEMU_IMG");
191 g_assert(qemu_img_path
);
192 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
193 g_assert(qemu_img_abs_path
);
195 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
197 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
199 fprintf(stderr
, "%s\n", err
->message
);
202 g_assert(ret
&& !err
);
204 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
205 * glib 2.43.91 implementation assumes that any non-zero is an error for
206 * windows, but uses extra precautions for Linux. However,
207 * 0 is only possible if the program exited normally, so that should be
208 * sufficient for our purposes on all platforms, here. */
210 fprintf(stderr
, "qemu-img returned status code %d\n", rc
);
217 free(qemu_img_abs_path
);
220 void mkqcow2(const char *file
, unsigned size_mb
)
222 return mkimg(file
, "qcow2", size_mb
);
225 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
227 FILE *debug_file
= fopen(debug_fn
, "w");
230 fprintf(debug_file
, "[inject-error]\n");
231 fprintf(debug_file
, "event = \"%s\"\n", event
);
232 fprintf(debug_file
, "errno = \"5\"\n");
233 fprintf(debug_file
, "state = \"1\"\n");
234 fprintf(debug_file
, "immediately = \"off\"\n");
235 fprintf(debug_file
, "once = \"on\"\n");
237 fprintf(debug_file
, "[set-state]\n");
238 fprintf(debug_file
, "event = \"%s\"\n", event
);
239 fprintf(debug_file
, "new_state = \"2\"\n");
241 g_assert(!ferror(debug_file
));
243 ret
= fclose(debug_file
);
247 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
250 unsigned char *tx
= (unsigned char *)buffer
;
254 /* Write an indicative pattern that varies and is unique per-cycle */
256 for (i
= 0; i
< len
; i
++) {
258 if (i
% cycle_len
== 0) {
263 /* force uniqueness by writing an id per-cycle */
264 for (i
= 0; i
< len
/ cycle_len
; i
++) {
266 if (j
+ sizeof(*sx
) <= len
) {
267 sx
= (size_t *)&tx
[j
];