]> git.proxmox.com Git - mirror_qemu.git/blob - tests/libqos/libqos.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream-qgraph' into staging
[mirror_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 #include "qapi/qmp/qdict.h"
8
9 /*** Test Setup & Teardown ***/
10
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.
16 */
17 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
18 {
19 char *cmdline;
20
21 QOSState *qs = g_new0(QOSState, 1);
22
23 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
24 qs->qts = qtest_init(cmdline);
25 qs->ops = ops;
26 if (ops) {
27 ops->alloc_init(&qs->alloc, qs->qts, ALLOC_NO_FLAGS);
28 qs->pcibus = ops->qpci_new(qs->qts, &qs->alloc);
29 }
30
31 g_free(cmdline);
32 return qs;
33 }
34
35 /**
36 * Launch QEMU with the given command line,
37 * and then set up interrupts and our guest malloc interface.
38 */
39 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
40 {
41 QOSState *qs;
42 va_list ap;
43
44 va_start(ap, cmdline_fmt);
45 qs = qtest_vboot(ops, cmdline_fmt, ap);
46 va_end(ap);
47
48 return qs;
49 }
50
51 /**
52 * Tear down the QEMU instance.
53 */
54 void qtest_common_shutdown(QOSState *qs)
55 {
56 if (qs->ops) {
57 if (qs->pcibus && qs->ops->qpci_free) {
58 qs->ops->qpci_free(qs->pcibus);
59 qs->pcibus = NULL;
60 }
61 }
62 alloc_destroy(&qs->alloc);
63 qtest_quit(qs->qts);
64 g_free(qs);
65 }
66
67 void qtest_shutdown(QOSState *qs)
68 {
69 if (qs->ops && qs->ops->shutdown) {
70 qs->ops->shutdown(qs);
71 } else {
72 qtest_common_shutdown(qs);
73 }
74 }
75
76 void set_context(QOSState *s)
77 {
78 global_qtest = s->qts;
79 }
80
81 static QDict *qmp_execute(QTestState *qts, const char *command)
82 {
83 return qtest_qmp(qts, "{ 'execute': %s }", command);
84 }
85
86 void migrate(QOSState *from, QOSState *to, const char *uri)
87 {
88 const char *st;
89 QDict *rsp, *sub;
90 bool running;
91
92 set_context(from);
93
94 /* Is the machine currently running? */
95 rsp = qmp_execute(from->qts, "query-status");
96 g_assert(qdict_haskey(rsp, "return"));
97 sub = qdict_get_qdict(rsp, "return");
98 g_assert(qdict_haskey(sub, "running"));
99 running = qdict_get_bool(sub, "running");
100 qobject_unref(rsp);
101
102 /* Issue the migrate command. */
103 rsp = qtest_qmp(from->qts,
104 "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
105 uri);
106 g_assert(qdict_haskey(rsp, "return"));
107 qobject_unref(rsp);
108
109 /* Wait for STOP event, but only if we were running: */
110 if (running) {
111 qtest_qmp_eventwait(from->qts, "STOP");
112 }
113
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 qtest_qmp_eventwait(to->qts, "RESUME");
119 return;
120 }
121
122 /* Otherwise, we need to wait: poll until migration is completed. */
123 while (1) {
124 rsp = qmp_execute(from->qts, "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");
129
130 /* "setup", "active", "completed", "failed", "cancelled" */
131 if (strcmp(st, "completed") == 0) {
132 qobject_unref(rsp);
133 break;
134 }
135
136 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
137 qobject_unref(rsp);
138 g_usleep(5000);
139 continue;
140 }
141
142 fprintf(stderr, "Migration did not complete, status: %s\n", st);
143 g_assert_not_reached();
144 }
145
146 migrate_allocator(&from->alloc, &to->alloc);
147 set_context(to);
148 }
149
150 bool have_qemu_img(void)
151 {
152 char *rpath;
153 const char *path = getenv("QTEST_QEMU_IMG");
154 if (!path) {
155 return false;
156 }
157
158 rpath = realpath(path, NULL);
159 if (!rpath) {
160 return false;
161 } else {
162 free(rpath);
163 return true;
164 }
165 }
166
167 void mkimg(const char *file, const char *fmt, unsigned size_mb)
168 {
169 gchar *cli;
170 bool ret;
171 int rc;
172 GError *err = NULL;
173 char *qemu_img_path;
174 gchar *out, *out2;
175 char *qemu_img_abs_path;
176
177 qemu_img_path = getenv("QTEST_QEMU_IMG");
178 g_assert(qemu_img_path);
179 qemu_img_abs_path = realpath(qemu_img_path, NULL);
180 g_assert(qemu_img_abs_path);
181
182 cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
183 fmt, file, size_mb);
184 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
185 if (err || !g_spawn_check_exit_status(rc, &err)) {
186 fprintf(stderr, "%s\n", err->message);
187 g_error_free(err);
188 }
189 g_assert(ret && !err);
190
191 g_free(out);
192 g_free(out2);
193 g_free(cli);
194 free(qemu_img_abs_path);
195 }
196
197 void mkqcow2(const char *file, unsigned size_mb)
198 {
199 return mkimg(file, "qcow2", size_mb);
200 }
201
202 void prepare_blkdebug_script(const char *debug_fn, const char *event)
203 {
204 FILE *debug_file = fopen(debug_fn, "w");
205 int ret;
206
207 fprintf(debug_file, "[inject-error]\n");
208 fprintf(debug_file, "event = \"%s\"\n", event);
209 fprintf(debug_file, "errno = \"5\"\n");
210 fprintf(debug_file, "state = \"1\"\n");
211 fprintf(debug_file, "immediately = \"off\"\n");
212 fprintf(debug_file, "once = \"on\"\n");
213
214 fprintf(debug_file, "[set-state]\n");
215 fprintf(debug_file, "event = \"%s\"\n", event);
216 fprintf(debug_file, "new_state = \"2\"\n");
217 fflush(debug_file);
218 g_assert(!ferror(debug_file));
219
220 ret = fclose(debug_file);
221 g_assert(ret == 0);
222 }
223
224 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
225 {
226 int i, j;
227 unsigned char *tx = (unsigned char *)buffer;
228 unsigned char p;
229 size_t *sx;
230
231 /* Write an indicative pattern that varies and is unique per-cycle */
232 p = rand() % 256;
233 for (i = 0; i < len; i++) {
234 tx[i] = p++ % 256;
235 if (i % cycle_len == 0) {
236 p = rand() % 256;
237 }
238 }
239
240 /* force uniqueness by writing an id per-cycle */
241 for (i = 0; i < len / cycle_len; i++) {
242 j = i * cycle_len;
243 if (j + sizeof(*sx) <= len) {
244 sx = (size_t *)&tx[j];
245 *sx = i;
246 }
247 }
248 }