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);
23 if (ops && ops->init_allocator) {
24 qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
32 * Launch QEMU with the given command line,
33 * and then set up interrupts and our guest malloc interface.
35 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
40 va_start(ap, cmdline_fmt);
41 qs = qtest_vboot(ops, cmdline_fmt, ap);
48 * Tear down the QEMU instance.
50 void qtest_shutdown(QOSState *qs)
52 if (qs->alloc && qs->ops && qs->ops->uninit_allocator) {
53 qs->ops->uninit_allocator(qs->alloc);
60 void set_context(QOSState *s)
62 global_qtest = s->qts;
65 static QDict *qmp_execute(const char *command)
70 fmt = g_strdup_printf("{ 'execute': '%s' }", command);
77 void migrate(QOSState *from, QOSState *to, const char *uri)
86 /* Is the machine currently running? */
87 rsp = qmp_execute("query-status");
88 g_assert(qdict_haskey(rsp, "return"));
89 sub = qdict_get_qdict(rsp, "return");
90 g_assert(qdict_haskey(sub, "running"));
91 running = qdict_get_bool(sub, "running");
94 /* Issue the migrate command. */
95 s = g_strdup_printf("{ 'execute': 'migrate',"
96 "'arguments': { 'uri': '%s' } }",
100 g_assert(qdict_haskey(rsp, "return"));
103 /* Wait for STOP event, but only if we were running: */
105 qmp_eventwait("STOP");
108 /* If we were running, we can wait for an event. */
110 migrate_allocator(from->alloc, to->alloc);
112 qmp_eventwait("RESUME");
116 /* Otherwise, we need to wait: poll until migration is completed. */
118 rsp = qmp_execute("query-migrate");
119 g_assert(qdict_haskey(rsp, "return"));
120 sub = qdict_get_qdict(rsp, "return");
121 g_assert(qdict_haskey(sub, "status"));
122 st = qdict_get_str(sub, "status");
124 /* "setup", "active", "completed", "failed", "cancelled" */
125 if (strcmp(st, "completed") == 0) {
130 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
136 fprintf(stderr, "Migration did not complete, status: %s\n", st);
137 g_assert_not_reached();
140 migrate_allocator(from->alloc, to->alloc);
144 bool have_qemu_img(void)
147 const char *path = getenv("QTEST_QEMU_IMG");
152 rpath = realpath(path, NULL);
161 void mkimg(const char *file, const char *fmt, unsigned size_mb)
169 char *qemu_img_abs_path;
171 qemu_img_path = getenv("QTEST_QEMU_IMG");
172 g_assert(qemu_img_path);
173 qemu_img_abs_path = realpath(qemu_img_path, NULL);
174 g_assert(qemu_img_abs_path);
176 cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
178 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
180 fprintf(stderr, "%s\n", err->message);
183 g_assert(ret && !err);
185 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
186 * glib 2.43.91 implementation assumes that any non-zero is an error for
187 * windows, but uses extra precautions for Linux. However,
188 * 0 is only possible if the program exited normally, so that should be
189 * sufficient for our purposes on all platforms, here. */
191 fprintf(stderr, "qemu-img returned status code %d\n", rc);
198 free(qemu_img_abs_path);
201 void mkqcow2(const char *file, unsigned size_mb)
203 return mkimg(file, "qcow2", size_mb);
206 void prepare_blkdebug_script(const char *debug_fn, const char *event)
208 FILE *debug_file = fopen(debug_fn, "w");
211 fprintf(debug_file, "[inject-error]\n");
212 fprintf(debug_file, "event = \"%s\"\n", event);
213 fprintf(debug_file, "errno = \"5\"\n");
214 fprintf(debug_file, "state = \"1\"\n");
215 fprintf(debug_file, "immediately = \"off\"\n");
216 fprintf(debug_file, "once = \"on\"\n");
218 fprintf(debug_file, "[set-state]\n");
219 fprintf(debug_file, "event = \"%s\"\n", event);
220 fprintf(debug_file, "new_state = \"2\"\n");
222 g_assert(!ferror(debug_file));
224 ret = fclose(debug_file);
228 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
231 unsigned char *tx = (unsigned char *)buffer;
235 /* Write an indicative pattern that varies and is unique per-cycle */
237 for (i = 0; i < len; i++) {
239 if (i % cycle_len == 0) {
244 /* force uniqueness by writing an id per-cycle */
245 for (i = 0; i < len / cycle_len; i++) {
247 if (j + sizeof(*sx) <= len) {
248 sx = (size_t *)&tx[j];