]>
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_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 set_context(QOSState
*s
)
73 global_qtest
= s
->qts
;
76 static QDict
*qmp_execute(const char *command
)
81 fmt
= g_strdup_printf("{ 'execute': '%s' }", command
);
88 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
97 /* Is the machine currently running? */
98 rsp
= qmp_execute("query-status");
99 g_assert(qdict_haskey(rsp
, "return"));
100 sub
= qdict_get_qdict(rsp
, "return");
101 g_assert(qdict_haskey(sub
, "running"));
102 running
= qdict_get_bool(sub
, "running");
105 /* Issue the migrate command. */
106 s
= g_strdup_printf("{ 'execute': 'migrate',"
107 "'arguments': { 'uri': '%s' } }",
111 g_assert(qdict_haskey(rsp
, "return"));
114 /* Wait for STOP event, but only if we were running: */
116 qmp_eventwait("STOP");
119 /* If we were running, we can wait for an event. */
121 migrate_allocator(from
->alloc
, to
->alloc
);
123 qmp_eventwait("RESUME");
127 /* Otherwise, we need to wait: poll until migration is completed. */
129 rsp
= qmp_execute("query-migrate");
130 g_assert(qdict_haskey(rsp
, "return"));
131 sub
= qdict_get_qdict(rsp
, "return");
132 g_assert(qdict_haskey(sub
, "status"));
133 st
= qdict_get_str(sub
, "status");
135 /* "setup", "active", "completed", "failed", "cancelled" */
136 if (strcmp(st
, "completed") == 0) {
141 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)) {
147 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
148 g_assert_not_reached();
151 migrate_allocator(from
->alloc
, to
->alloc
);
155 bool have_qemu_img(void)
158 const char *path
= getenv("QTEST_QEMU_IMG");
163 rpath
= realpath(path
, NULL
);
172 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
180 char *qemu_img_abs_path
;
182 qemu_img_path
= getenv("QTEST_QEMU_IMG");
183 g_assert(qemu_img_path
);
184 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
185 g_assert(qemu_img_abs_path
);
187 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
189 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
191 fprintf(stderr
, "%s\n", err
->message
);
194 g_assert(ret
&& !err
);
196 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
197 * glib 2.43.91 implementation assumes that any non-zero is an error for
198 * windows, but uses extra precautions for Linux. However,
199 * 0 is only possible if the program exited normally, so that should be
200 * sufficient for our purposes on all platforms, here. */
202 fprintf(stderr
, "qemu-img returned status code %d\n", rc
);
209 free(qemu_img_abs_path
);
212 void mkqcow2(const char *file
, unsigned size_mb
)
214 return mkimg(file
, "qcow2", size_mb
);
217 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
219 FILE *debug_file
= fopen(debug_fn
, "w");
222 fprintf(debug_file
, "[inject-error]\n");
223 fprintf(debug_file
, "event = \"%s\"\n", event
);
224 fprintf(debug_file
, "errno = \"5\"\n");
225 fprintf(debug_file
, "state = \"1\"\n");
226 fprintf(debug_file
, "immediately = \"off\"\n");
227 fprintf(debug_file
, "once = \"on\"\n");
229 fprintf(debug_file
, "[set-state]\n");
230 fprintf(debug_file
, "event = \"%s\"\n", event
);
231 fprintf(debug_file
, "new_state = \"2\"\n");
233 g_assert(!ferror(debug_file
));
235 ret
= fclose(debug_file
);
239 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
242 unsigned char *tx
= (unsigned char *)buffer
;
246 /* Write an indicative pattern that varies and is unique per-cycle */
248 for (i
= 0; i
< len
; i
++) {
250 if (i
% cycle_len
== 0) {
255 /* force uniqueness by writing an id per-cycle */
256 for (i
= 0; i
< len
/ cycle_len
; i
++) {
258 if (j
+ sizeof(*sx
) <= len
) {
259 sx
= (size_t *)&tx
[j
];