]> Git Repo - qemu.git/blob - tests/libqos/libqos.c
qapi: Plumb in 'boxed' to qapi generator lower levels
[qemu.git] / tests / libqos / libqos.c
1 #include "qemu/osdep.h"
2 #include <sys/wait.h>
3
4 #include "libqtest.h"
5 #include "libqos/libqos.h"
6 #include "libqos/pci.h"
7
8 /*** Test Setup & Teardown ***/
9
10 /**
11  * Launch QEMU with the given command line,
12  * and then set up interrupts and our guest malloc interface.
13  */
14 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
15 {
16     char *cmdline;
17
18     struct QOSState *qs = g_malloc(sizeof(QOSState));
19
20     cmdline = g_strdup_vprintf(cmdline_fmt, ap);
21     qs->qts = qtest_start(cmdline);
22     qs->ops = ops;
23     qtest_irq_intercept_in(global_qtest, "ioapic");
24     if (ops && ops->init_allocator) {
25         qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
26     }
27
28     g_free(cmdline);
29     return qs;
30 }
31
32 /**
33  * Launch QEMU with the given command line,
34  * and then set up interrupts and our guest malloc interface.
35  */
36 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
37 {
38     QOSState *qs;
39     va_list ap;
40
41     va_start(ap, cmdline_fmt);
42     qs = qtest_vboot(ops, cmdline_fmt, ap);
43     va_end(ap);
44
45     return qs;
46 }
47
48 /**
49  * Tear down the QEMU instance.
50  */
51 void qtest_shutdown(QOSState *qs)
52 {
53     if (qs->alloc && qs->ops && qs->ops->uninit_allocator) {
54         qs->ops->uninit_allocator(qs->alloc);
55         qs->alloc = NULL;
56     }
57     qtest_quit(qs->qts);
58     g_free(qs);
59 }
60
61 void set_context(QOSState *s)
62 {
63     global_qtest = s->qts;
64 }
65
66 static QDict *qmp_execute(const char *command)
67 {
68     char *fmt;
69     QDict *rsp;
70
71     fmt = g_strdup_printf("{ 'execute': '%s' }", command);
72     rsp = qmp(fmt);
73     g_free(fmt);
74
75     return rsp;
76 }
77
78 void migrate(QOSState *from, QOSState *to, const char *uri)
79 {
80     const char *st;
81     char *s;
82     QDict *rsp, *sub;
83     bool running;
84
85     set_context(from);
86
87     /* Is the machine currently running? */
88     rsp = qmp_execute("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");
93     QDECREF(rsp);
94
95     /* Issue the migrate command. */
96     s = g_strdup_printf("{ 'execute': 'migrate',"
97                         "'arguments': { 'uri': '%s' } }",
98                         uri);
99     rsp = qmp(s);
100     g_free(s);
101     g_assert(qdict_haskey(rsp, "return"));
102     QDECREF(rsp);
103
104     /* Wait for STOP event, but only if we were running: */
105     if (running) {
106         qmp_eventwait("STOP");
107     }
108
109     /* If we were running, we can wait for an event. */
110     if (running) {
111         migrate_allocator(from->alloc, to->alloc);
112         set_context(to);
113         qmp_eventwait("RESUME");
114         return;
115     }
116
117     /* Otherwise, we need to wait: poll until migration is completed. */
118     while (1) {
119         rsp = qmp_execute("query-migrate");
120         g_assert(qdict_haskey(rsp, "return"));
121         sub = qdict_get_qdict(rsp, "return");
122         g_assert(qdict_haskey(sub, "status"));
123         st = qdict_get_str(sub, "status");
124
125         /* "setup", "active", "completed", "failed", "cancelled" */
126         if (strcmp(st, "completed") == 0) {
127             QDECREF(rsp);
128             break;
129         }
130
131         if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
132             QDECREF(rsp);
133             g_usleep(5000);
134             continue;
135         }
136
137         fprintf(stderr, "Migration did not complete, status: %s\n", st);
138         g_assert_not_reached();
139     }
140
141     migrate_allocator(from->alloc, to->alloc);
142     set_context(to);
143 }
144
145 bool have_qemu_img(void)
146 {
147     char *rpath;
148     const char *path = getenv("QTEST_QEMU_IMG");
149     if (!path) {
150         return false;
151     }
152
153     rpath = realpath(path, NULL);
154     if (!rpath) {
155         return false;
156     } else {
157         free(rpath);
158         return true;
159     }
160 }
161
162 void mkimg(const char *file, const char *fmt, unsigned size_mb)
163 {
164     gchar *cli;
165     bool ret;
166     int rc;
167     GError *err = NULL;
168     char *qemu_img_path;
169     gchar *out, *out2;
170     char *qemu_img_abs_path;
171
172     qemu_img_path = getenv("QTEST_QEMU_IMG");
173     g_assert(qemu_img_path);
174     qemu_img_abs_path = realpath(qemu_img_path, NULL);
175     g_assert(qemu_img_abs_path);
176
177     cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
178                           fmt, file, size_mb);
179     ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
180     if (err) {
181         fprintf(stderr, "%s\n", err->message);
182         g_error_free(err);
183     }
184     g_assert(ret && !err);
185
186     /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
187      * glib 2.43.91 implementation assumes that any non-zero is an error for
188      * windows, but uses extra precautions for Linux. However,
189      * 0 is only possible if the program exited normally, so that should be
190      * sufficient for our purposes on all platforms, here. */
191     if (rc) {
192         fprintf(stderr, "qemu-img returned status code %d\n", rc);
193     }
194     g_assert(!rc);
195
196     g_free(out);
197     g_free(out2);
198     g_free(cli);
199     free(qemu_img_abs_path);
200 }
201
202 void mkqcow2(const char *file, unsigned size_mb)
203 {
204     return mkimg(file, "qcow2", size_mb);
205 }
206
207 void prepare_blkdebug_script(const char *debug_fn, const char *event)
208 {
209     FILE *debug_file = fopen(debug_fn, "w");
210     int ret;
211
212     fprintf(debug_file, "[inject-error]\n");
213     fprintf(debug_file, "event = \"%s\"\n", event);
214     fprintf(debug_file, "errno = \"5\"\n");
215     fprintf(debug_file, "state = \"1\"\n");
216     fprintf(debug_file, "immediately = \"off\"\n");
217     fprintf(debug_file, "once = \"on\"\n");
218
219     fprintf(debug_file, "[set-state]\n");
220     fprintf(debug_file, "event = \"%s\"\n", event);
221     fprintf(debug_file, "new_state = \"2\"\n");
222     fflush(debug_file);
223     g_assert(!ferror(debug_file));
224
225     ret = fclose(debug_file);
226     g_assert(ret == 0);
227 }
228
229 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
230 {
231     int i, j;
232     unsigned char *tx = (unsigned char *)buffer;
233     unsigned char p;
234     size_t *sx;
235
236     /* Write an indicative pattern that varies and is unique per-cycle */
237     p = rand() % 256;
238     for (i = 0; i < len; i++) {
239         tx[i] = p++ % 256;
240         if (i % cycle_len == 0) {
241             p = rand() % 256;
242         }
243     }
244
245     /* force uniqueness by writing an id per-cycle */
246     for (i = 0; i < len / cycle_len; i++) {
247         j = i * cycle_len;
248         if (j + sizeof(*sx) <= len) {
249             sx = (size_t *)&tx[j];
250             *sx = i;
251         }
252     }
253 }
This page took 0.034929 seconds and 4 git commands to generate.