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_init(cmdline
);
27 ops
->alloc_init(&qs
->alloc
, qs
->qts
, ALLOC_NO_FLAGS
);
28 qs
->pcibus
= ops
->qpci_new(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
);
62 alloc_destroy(&qs
->alloc
);
67 void qtest_shutdown(QOSState
*qs
)
69 if (qs
->ops
&& qs
->ops
->shutdown
) {
70 qs
->ops
->shutdown(qs
);
72 qtest_common_shutdown(qs
);
76 static QDict
*qmp_execute(QTestState
*qts
, const char *command
)
78 return qtest_qmp(qts
, "{ 'execute': %s }", command
);
81 void migrate(QOSState
*from
, QOSState
*to
, const char *uri
)
87 /* Is the machine currently running? */
88 rsp
= qmp_execute(from
->qts
, "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 rsp
= qtest_qmp(from
->qts
,
97 "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
99 g_assert(qdict_haskey(rsp
, "return"));
102 /* Wait for STOP event, but only if we were running: */
104 qtest_qmp_eventwait(from
->qts
, "STOP");
107 /* If we were running, we can wait for an event. */
109 migrate_allocator(&from
->alloc
, &to
->alloc
);
110 qtest_qmp_eventwait(to
->qts
, "RESUME");
114 /* Otherwise, we need to wait: poll until migration is completed. */
116 rsp
= qmp_execute(from
->qts
, "query-migrate");
117 g_assert(qdict_haskey(rsp
, "return"));
118 sub
= qdict_get_qdict(rsp
, "return");
119 g_assert(qdict_haskey(sub
, "status"));
120 st
= qdict_get_str(sub
, "status");
122 /* "setup", "active", "completed", "failed", "cancelled" */
123 if (strcmp(st
, "completed") == 0) {
128 if ((strcmp(st
, "setup") == 0) || (strcmp(st
, "active") == 0)
129 || (strcmp(st
, "wait-unplug") == 0)) {
135 fprintf(stderr
, "Migration did not complete, status: %s\n", st
);
136 g_assert_not_reached();
139 migrate_allocator(&from
->alloc
, &to
->alloc
);
142 bool have_qemu_img(void)
145 const char *path
= getenv("QTEST_QEMU_IMG");
150 rpath
= realpath(path
, NULL
);
159 void mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
167 char *qemu_img_abs_path
;
169 qemu_img_path
= getenv("QTEST_QEMU_IMG");
170 g_assert(qemu_img_path
);
171 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
172 g_assert(qemu_img_abs_path
);
174 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
176 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
177 if (err
|| !g_spawn_check_exit_status(rc
, &err
)) {
178 fprintf(stderr
, "%s\n", err
->message
);
181 g_assert(ret
&& !err
);
186 free(qemu_img_abs_path
);
189 void mkqcow2(const char *file
, unsigned size_mb
)
191 return mkimg(file
, "qcow2", size_mb
);
194 void prepare_blkdebug_script(const char *debug_fn
, const char *event
)
196 FILE *debug_file
= fopen(debug_fn
, "w");
199 fprintf(debug_file
, "[inject-error]\n");
200 fprintf(debug_file
, "event = \"%s\"\n", event
);
201 fprintf(debug_file
, "errno = \"5\"\n");
202 fprintf(debug_file
, "state = \"1\"\n");
203 fprintf(debug_file
, "immediately = \"off\"\n");
204 fprintf(debug_file
, "once = \"on\"\n");
206 fprintf(debug_file
, "[set-state]\n");
207 fprintf(debug_file
, "event = \"%s\"\n", event
);
208 fprintf(debug_file
, "new_state = \"2\"\n");
210 g_assert(!ferror(debug_file
));
212 ret
= fclose(debug_file
);
216 void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
219 unsigned char *tx
= (unsigned char *)buffer
;
223 /* Write an indicative pattern that varies and is unique per-cycle */
225 for (i
= 0; i
< len
; i
++) {
227 if (i
% cycle_len
== 0) {
232 /* force uniqueness by writing an id per-cycle */
233 for (i
= 0; i
< len
/ cycle_len
; i
++) {
235 if (j
+ sizeof(*sx
) <= len
) {
236 sx
= (size_t *)&tx
[j
];