oslib: strip trailing '\n' from error_setg() string argument
[qemu/kevin.git] / tests / libqos / libqos.c
blob6226546c28874bd963c7966e04e1362d3b6522a0
1 #include "qemu/osdep.h"
2 #include <sys/wait.h>
4 #include "libqtest.h"
5 #include "libqos/libqos.h"
6 #include "libqos/pci.h"
8 /*** Test Setup & Teardown ***/
10 /**
11 * Launch QEMU with the given command line,
12 * and then set up interrupts and our guest malloc interface.
13 * Never returns NULL:
14 * Terminates the application in case an error is encountered.
16 QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
18 char *cmdline;
20 struct QOSState *qs = g_malloc(sizeof(QOSState));
22 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
23 qs->qts = qtest_start(cmdline);
24 qs->ops = ops;
25 if (ops) {
26 if (ops->init_allocator) {
27 qs->alloc = ops->init_allocator(ALLOC_NO_FLAGS);
29 if (ops->qpci_init && qs->alloc) {
30 qs->pcibus = ops->qpci_init(qs->alloc);
34 g_free(cmdline);
35 return qs;
38 /**
39 * Launch QEMU with the given command line,
40 * and then set up interrupts and our guest malloc interface.
42 QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
44 QOSState *qs;
45 va_list ap;
47 va_start(ap, cmdline_fmt);
48 qs = qtest_vboot(ops, cmdline_fmt, ap);
49 va_end(ap);
51 return qs;
54 /**
55 * Tear down the QEMU instance.
57 void qtest_common_shutdown(QOSState *qs)
59 if (qs->ops) {
60 if (qs->pcibus && qs->ops->qpci_free) {
61 qs->ops->qpci_free(qs->pcibus);
62 qs->pcibus = NULL;
64 if (qs->alloc && qs->ops->uninit_allocator) {
65 qs->ops->uninit_allocator(qs->alloc);
66 qs->alloc = NULL;
69 qtest_quit(qs->qts);
70 g_free(qs);
73 void qtest_shutdown(QOSState *qs)
75 if (qs->ops && qs->ops->shutdown) {
76 qs->ops->shutdown(qs);
77 } else {
78 qtest_common_shutdown(qs);
82 void set_context(QOSState *s)
84 global_qtest = s->qts;
87 static QDict *qmp_execute(const char *command)
89 char *fmt;
90 QDict *rsp;
92 fmt = g_strdup_printf("{ 'execute': '%s' }", command);
93 rsp = qmp(fmt);
94 g_free(fmt);
96 return rsp;
99 void migrate(QOSState *from, QOSState *to, const char *uri)
101 const char *st;
102 char *s;
103 QDict *rsp, *sub;
104 bool running;
106 set_context(from);
108 /* Is the machine currently running? */
109 rsp = qmp_execute("query-status");
110 g_assert(qdict_haskey(rsp, "return"));
111 sub = qdict_get_qdict(rsp, "return");
112 g_assert(qdict_haskey(sub, "running"));
113 running = qdict_get_bool(sub, "running");
114 QDECREF(rsp);
116 /* Issue the migrate command. */
117 s = g_strdup_printf("{ 'execute': 'migrate',"
118 "'arguments': { 'uri': '%s' } }",
119 uri);
120 rsp = qmp(s);
121 g_free(s);
122 g_assert(qdict_haskey(rsp, "return"));
123 QDECREF(rsp);
125 /* Wait for STOP event, but only if we were running: */
126 if (running) {
127 qmp_eventwait("STOP");
130 /* If we were running, we can wait for an event. */
131 if (running) {
132 migrate_allocator(from->alloc, to->alloc);
133 set_context(to);
134 qmp_eventwait("RESUME");
135 return;
138 /* Otherwise, we need to wait: poll until migration is completed. */
139 while (1) {
140 rsp = qmp_execute("query-migrate");
141 g_assert(qdict_haskey(rsp, "return"));
142 sub = qdict_get_qdict(rsp, "return");
143 g_assert(qdict_haskey(sub, "status"));
144 st = qdict_get_str(sub, "status");
146 /* "setup", "active", "completed", "failed", "cancelled" */
147 if (strcmp(st, "completed") == 0) {
148 QDECREF(rsp);
149 break;
152 if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
153 QDECREF(rsp);
154 g_usleep(5000);
155 continue;
158 fprintf(stderr, "Migration did not complete, status: %s\n", st);
159 g_assert_not_reached();
162 migrate_allocator(from->alloc, to->alloc);
163 set_context(to);
166 bool have_qemu_img(void)
168 char *rpath;
169 const char *path = getenv("QTEST_QEMU_IMG");
170 if (!path) {
171 return false;
174 rpath = realpath(path, NULL);
175 if (!rpath) {
176 return false;
177 } else {
178 free(rpath);
179 return true;
183 void mkimg(const char *file, const char *fmt, unsigned size_mb)
185 gchar *cli;
186 bool ret;
187 int rc;
188 GError *err = NULL;
189 char *qemu_img_path;
190 gchar *out, *out2;
191 char *qemu_img_abs_path;
193 qemu_img_path = getenv("QTEST_QEMU_IMG");
194 g_assert(qemu_img_path);
195 qemu_img_abs_path = realpath(qemu_img_path, NULL);
196 g_assert(qemu_img_abs_path);
198 cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
199 fmt, file, size_mb);
200 ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
201 if (err) {
202 fprintf(stderr, "%s\n", err->message);
203 g_error_free(err);
205 g_assert(ret && !err);
207 /* In glib 2.34, we have g_spawn_check_exit_status. in 2.12, we don't.
208 * glib 2.43.91 implementation assumes that any non-zero is an error for
209 * windows, but uses extra precautions for Linux. However,
210 * 0 is only possible if the program exited normally, so that should be
211 * sufficient for our purposes on all platforms, here. */
212 if (rc) {
213 fprintf(stderr, "qemu-img returned status code %d\n", rc);
215 g_assert(!rc);
217 g_free(out);
218 g_free(out2);
219 g_free(cli);
220 free(qemu_img_abs_path);
223 void mkqcow2(const char *file, unsigned size_mb)
225 return mkimg(file, "qcow2", size_mb);
228 void prepare_blkdebug_script(const char *debug_fn, const char *event)
230 FILE *debug_file = fopen(debug_fn, "w");
231 int ret;
233 fprintf(debug_file, "[inject-error]\n");
234 fprintf(debug_file, "event = \"%s\"\n", event);
235 fprintf(debug_file, "errno = \"5\"\n");
236 fprintf(debug_file, "state = \"1\"\n");
237 fprintf(debug_file, "immediately = \"off\"\n");
238 fprintf(debug_file, "once = \"on\"\n");
240 fprintf(debug_file, "[set-state]\n");
241 fprintf(debug_file, "event = \"%s\"\n", event);
242 fprintf(debug_file, "new_state = \"2\"\n");
243 fflush(debug_file);
244 g_assert(!ferror(debug_file));
246 ret = fclose(debug_file);
247 g_assert(ret == 0);
250 void generate_pattern(void *buffer, size_t len, size_t cycle_len)
252 int i, j;
253 unsigned char *tx = (unsigned char *)buffer;
254 unsigned char p;
255 size_t *sx;
257 /* Write an indicative pattern that varies and is unique per-cycle */
258 p = rand() % 256;
259 for (i = 0; i < len; i++) {
260 tx[i] = p++ % 256;
261 if (i % cycle_len == 0) {
262 p = rand() % 256;
266 /* force uniqueness by writing an id per-cycle */
267 for (i = 0; i < len / cycle_len; i++) {
268 j = i * cycle_len;
269 if (j + sizeof(*sx) <= len) {
270 sx = (size_t *)&tx[j];
271 *sx = i;