Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / tests / qtest / migration-helpers.c
blob4ee26014b78322df9a04ad7be2c368726b9f19d8
1 /*
2 * QTest migration helpers
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qjson.h"
16 #include "migration-helpers.h"
18 bool got_stop;
20 static void check_stop_event(QTestState *who)
22 QDict *event = qtest_qmp_event_ref(who, "STOP");
23 if (event) {
24 got_stop = true;
25 qobject_unref(event);
30 * Events can get in the way of responses we are actually waiting for.
32 QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
34 va_list ap;
35 QDict *resp, *ret;
37 va_start(ap, command);
38 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
39 va_end(ap);
41 resp = qtest_qmp_receive(who);
42 check_stop_event(who);
44 g_assert(!qdict_haskey(resp, "error"));
45 g_assert(qdict_haskey(resp, "return"));
47 ret = qdict_get_qdict(resp, "return");
48 qobject_ref(ret);
49 qobject_unref(resp);
51 return ret;
55 * Events can get in the way of responses we are actually waiting for.
57 QDict *wait_command(QTestState *who, const char *command, ...)
59 va_list ap;
60 QDict *resp, *ret;
62 va_start(ap, command);
63 resp = qtest_vqmp(who, command, ap);
64 va_end(ap);
66 check_stop_event(who);
68 g_assert(!qdict_haskey(resp, "error"));
69 g_assert(qdict_haskey(resp, "return"));
71 ret = qdict_get_qdict(resp, "return");
72 qobject_ref(ret);
73 qobject_unref(resp);
75 return ret;
79 * Send QMP command "migrate".
80 * Arguments are built from @fmt... (formatted like
81 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
83 void migrate_qmp(QTestState *who, const char *uri, const char *fmt, ...)
85 va_list ap;
86 QDict *args, *rsp;
88 va_start(ap, fmt);
89 args = qdict_from_vjsonf_nofail(fmt, ap);
90 va_end(ap);
92 g_assert(!qdict_haskey(args, "uri"));
93 qdict_put_str(args, "uri", uri);
95 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
97 g_assert(qdict_haskey(rsp, "return"));
98 qobject_unref(rsp);
102 * Note: caller is responsible to free the returned object via
103 * qobject_unref() after use
105 QDict *migrate_query(QTestState *who)
107 return wait_command(who, "{ 'execute': 'query-migrate' }");
111 * Note: caller is responsible to free the returned object via
112 * g_free() after use
114 static gchar *migrate_query_status(QTestState *who)
116 QDict *rsp_return = migrate_query(who);
117 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
119 g_assert(status);
120 qobject_unref(rsp_return);
122 return status;
125 static bool check_migration_status(QTestState *who, const char *goal,
126 const char **ungoals)
128 bool ready;
129 char *current_status;
130 const char **ungoal;
132 current_status = migrate_query_status(who);
133 ready = strcmp(current_status, goal) == 0;
134 if (!ungoals) {
135 g_assert_cmpstr(current_status, !=, "failed");
137 * If looking for a state other than completed,
138 * completion of migration would cause the test to
139 * hang.
141 if (strcmp(goal, "completed") != 0) {
142 g_assert_cmpstr(current_status, !=, "completed");
144 } else {
145 for (ungoal = ungoals; *ungoal; ungoal++) {
146 g_assert_cmpstr(current_status, !=, *ungoal);
149 g_free(current_status);
150 return ready;
153 void wait_for_migration_status(QTestState *who,
154 const char *goal, const char **ungoals)
156 while (!check_migration_status(who, goal, ungoals)) {
157 usleep(1000);
161 void wait_for_migration_complete(QTestState *who)
163 wait_for_migration_status(who, "completed", NULL);
166 void wait_for_migration_fail(QTestState *from, bool allow_active)
168 QDict *rsp_return;
169 char *status;
170 bool failed;
172 do {
173 status = migrate_query_status(from);
174 bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
175 (allow_active && !strcmp(status, "active"));
176 if (!result) {
177 fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
178 __func__, status, allow_active);
180 g_assert(result);
181 failed = !strcmp(status, "failed");
182 g_free(status);
183 } while (!failed);
185 /* Is the machine currently running? */
186 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
187 g_assert(qdict_haskey(rsp_return, "running"));
188 g_assert(qdict_get_bool(rsp_return, "running"));
189 qobject_unref(rsp_return);