Merge tag 'v3.1.0-rc0'
[qemu/ar7.git] / tests / libqos / libqos.c
blob7ce0f415183bc562f7857e1fffccb73923f979eb
1 #include "qemu/osdep.h"
2 #include <sys/wait.h>
4 #include "libqtest.h"
5 #include "libqos/libqos.h"
6 #include "libqos/pci.h"
7 #include "qapi/qmp/qdict.h"
9 /*** Test Setup & Teardown ***/
11 /**
12 * Launch QEMU with the given command line,
13 * and then set up interrupts and our guest malloc interface.
14 * Never returns NULL:
15 * Terminates the application in case an error is encountered.
17 GCC_FMT_ATTR(2, 0)
18 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
20 char *cmdline;
22 QOSState *qs = g_new0(QOSState, 1);
24 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
25 qs->qts = qtest_init(cmdline);
26 qs->ops = ops;
27 if (ops) {
28 qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
29 qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
32 g_free(cmdline);
33 return qs;
36 /**
37 * Launch QEMU with the given command line,
38 * and then set up interrupts and our guest malloc interface.
40 GCC_FMT_ATTR(2, 3)
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_common_shutdown(QOSState *qs)
58 if (qs->ops) {
59 if (qs->pcibus && qs->ops->qpci_free) {
60 qs->ops->qpci_free(qs->pcibus);
61 qs->pcibus = NULL;
63 if (qs->alloc && qs->ops->uninit_allocator) {
64 qs->ops->uninit_allocator(qs->alloc);
65 qs->alloc = NULL;
68 qtest_quit(qs->qts);
69 g_free(qs);
72 void qtest_shutdown(QOSState *qs)
74 if (qs->ops && qs->ops->shutdown) {
75 qs->ops->shutdown(qs);
76 } else {
77 qtest_common_shutdown(qs);
81 void set_context(QOSState *s)
83 global_qtest = s->qts;
86 static QDict *qmp_execute(QTestState *qts, const char *command)
88 return qtest_qmp(qts, "{ 'execute': %s }", command);
91 void migrate(QOSState *from, QOSState *to, const char *uri)
93 const char *st;
94 QDict *rsp, *sub;
95 bool running;
97 set_context(from);
99 /* Is the machine currently running? */
100 rsp = qmp_execute(from->qts, "query-status");
101 g_assert(qdict_haskey(rsp, "return"));
102 sub = qdict_get_qdict(rsp, "return");
103 g_assert(qdict_haskey(sub, "running"));
104 running = qdict_get_bool(sub, "running");
105 qobject_unref(rsp);
107 /* Issue the migrate command. */
108 rsp = qtest_qmp(from->qts,
109 "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
110 uri);
111 g_assert(qdict_haskey(rsp, "return"));
112 qobject_unref(rsp);
114 /* Wait for STOP event, but only if we were running: */
115 if (running) {
116 qtest_qmp_eventwait(from->qts, "STOP");
119 /* If we were running, we can wait for an event. */
120 if (running) {
121 migrate_allocator(from->alloc, to->alloc);
122 set_context(to);
123 qtest_qmp_eventwait(to->qts, "RESUME");
124 return;
127 /* Otherwise, we need to wait: poll until migration is completed. */
128 while (1) {
129 rsp = qmp_execute(from->qts, "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) {
137 qobject_unref(rsp);
138 break;
141 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
142 qobject_unref(rsp);
143 g_usleep(5000);
144 continue;
147 fprintf(stderr, "Migration did not complete, status: %s\n", st);
148 g_assert_not_reached();
151 migrate_allocator(from->alloc, to->alloc);
152 set_context(to);
155 bool have_qemu_img(void)
157 char *rpath;
158 const char *path = getenv("QTEST_QEMU_IMG");
159 if (!path) {
160 return false;
163 rpath = realpath(path, NULL);
164 if (!rpath) {
165 return false;
166 } else {
167 free(rpath);
168 return true;
172 void mkimg(const char *file, const char *fmt, unsigned size_mb)
174 gchar *cli;
175 bool ret;
176 int rc;
177 GError *err = NULL;
178 char *qemu_img_path;
179 gchar *out, *out2;
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,
188 fmt, file, size_mb);
189 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
190 if (err || !g_spawn_check_exit_status(rc, &err)) {
191 fprintf(stderr, "%s\n", err->message);
192 g_error_free(err);
194 g_assert(ret && !err);
196 g_free(out);
197 g_free(out2);
198 g_free(cli);
199 free(qemu_img_abs_path);
202 void mkqcow2(const char *file, unsigned size_mb)
204 return mkimg(file, "qcow2", size_mb);
207 void prepare_blkdebug_script(const char *debug_fn, const char *event)
209 FILE *debug_file = fopen(debug_fn, "w");
210 int ret;
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");
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));
225 ret = fclose(debug_file);
226 g_assert(ret == 0);
229 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
231 int i, j;
232 unsigned char *tx = (unsigned char *)buffer;
233 unsigned char p;
234 size_t *sx;
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;
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;