tap: net_tap_fd_init() can't fail, drop dead error handling
[qemu.git] / tests / libqos / libqos.c
blobfce625b18adac97515c8ec5014a6a0b20e81a1e4
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <glib.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/wait.h>
9 #include "libqtest.h"
10 #include "libqos/libqos.h"
11 #include "libqos/pci.h"
13 /*** Test Setup & Teardown ***/
15 /**
16 * Launch QEMU with the given command line,
17 * and then set up interrupts and our guest malloc interface.
19 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
21 char *cmdline;
23 struct QOSState *qs = g_malloc(sizeof(QOSState));
25 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
26 qs->qts = qtest_start(cmdline);
27 qs->ops = ops;
28 qtest_irq_intercept_in(global_qtest, "ioapic");
29 if (ops && ops->init_allocator) {
30 qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
33 g_free(cmdline);
34 return qs;
37 /**
38 * Launch QEMU with the given command line,
39 * and then set up interrupts and our guest malloc interface.
41 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
43 QOSState *qs;
44 va_list ap;
46 va_start(ap, cmdline_fmt);
47 qs = qtest_vboot(ops, cmdline_fmt, ap);
48 va_end(ap);
50 return qs;
53 /**
54 * Tear down the QEMU instance.
56 void qtest_shutdown(QOSState *qs)
58 if (qs->alloc && qs->ops && qs->ops->uninit_allocator) {
59 qs->ops->uninit_allocator(qs->alloc);
60 qs->alloc = NULL;
62 qtest_quit(qs->qts);
63 g_free(qs);
66 void set_context(QOSState *s)
68 global_qtest = s->qts;
71 static QDict *qmp_execute(const char *command)
73 char *fmt;
74 QDict *rsp;
76 fmt = g_strdup_printf("{ 'execute': '%s' }", command);
77 rsp = qmp(fmt);
78 g_free(fmt);
80 return rsp;
83 void migrate(QOSState *from, QOSState *to, const char *uri)
85 const char *st;
86 char *s;
87 QDict *rsp, *sub;
88 bool running;
90 set_context(from);
92 /* Is the machine currently running? */
93 rsp = qmp_execute("query-status");
94 g_assert(qdict_haskey(rsp, "return"));
95 sub = qdict_get_qdict(rsp, "return");
96 g_assert(qdict_haskey(sub, "running"));
97 running = qdict_get_bool(sub, "running");
98 QDECREF(rsp);
100 /* Issue the migrate command. */
101 s = g_strdup_printf("{ 'execute': 'migrate',"
102 "'arguments': { 'uri': '%s' } }",
103 uri);
104 rsp = qmp(s);
105 g_free(s);
106 g_assert(qdict_haskey(rsp, "return"));
107 QDECREF(rsp);
109 /* Wait for STOP event, but only if we were running: */
110 if (running) {
111 qmp_eventwait("STOP");
114 /* If we were running, we can wait for an event. */
115 if (running) {
116 migrate_allocator(from->alloc, to->alloc);
117 set_context(to);
118 qmp_eventwait("RESUME");
119 return;
122 /* Otherwise, we need to wait: poll until migration is completed. */
123 while (1) {
124 rsp = qmp_execute("query-migrate");
125 g_assert(qdict_haskey(rsp, "return"));
126 sub = qdict_get_qdict(rsp, "return");
127 g_assert(qdict_haskey(sub, "status"));
128 st = qdict_get_str(sub, "status");
130 /* "setup", "active", "completed", "failed", "cancelled" */
131 if (strcmp(st, "completed") == 0) {
132 QDECREF(rsp);
133 break;
136 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
137 QDECREF(rsp);
138 g_usleep(5000);
139 continue;
142 fprintf(stderr, "Migration did not complete, status: %s\n", st);
143 g_assert_not_reached();
146 migrate_allocator(from->alloc, to->alloc);
147 set_context(to);
150 void mkimg(const char *file, const char *fmt, unsigned size_mb)
152 gchar *cli;
153 bool ret;
154 int rc;
155 GError *err = NULL;
156 char *qemu_img_path;
157 gchar *out, *out2;
158 char *abs_path;
160 qemu_img_path = getenv("QTEST_QEMU_IMG");
161 abs_path = realpath(qemu_img_path, NULL);
162 assert(qemu_img_path);
164 cli = g_strdup_printf("%s create -f %s %s %uM", abs_path,
165 fmt, file, size_mb);
166 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
167 if (err) {
168 fprintf(stderr, "%s\n", err->message);
169 g_error_free(err);
171 g_assert(ret && !err);
173 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
174 * glib 2.43.91 implementation assumes that any non-zero is an error for
175 * windows, but uses extra precautions for Linux. However,
176 * 0 is only possible if the program exited normally, so that should be
177 * sufficient for our purposes on all platforms, here. */
178 if (rc) {
179 fprintf(stderr, "qemu-img returned status code %d\n", rc);
181 g_assert(!rc);
183 g_free(out);
184 g_free(out2);
185 g_free(cli);
186 free(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");
197 int ret;
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");
209 fflush(debug_file);
210 g_assert(!ferror(debug_file));
212 ret = fclose(debug_file);
213 g_assert(ret == 0);