From: Fabiano Rosas <farosas@suse.de>
To: qemu-devel@nongnu.org
Cc: Peter Xu <peterx@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
"Dr . David Alan Gilbert" <dave@treblig.org>,
Laurent Vivier <lvivier@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: [PATCH v2 10/18] tests/qtest/migration: Add a test for HMP completion
Date: Wed, 9 Sep 2026 18:45:00 -0300 [thread overview]
Message-ID: <20260909214509.237309-11-farosas@suse.de> (raw)
In-Reply-To: <20260909214509.237309-1-farosas@suse.de>
The following patches will touch HMP readline completion for migration
parameters, add a test case.
The test puts the monitor on a chardev via socket and bypasses qtest
facilities because it needs to emit raw codes to readline. It
therefore requires a couple of new helpers to read/write to the
monitor socket.
Usage:
QTEST_QEMU_BINARY=./qemu-system-x86_64 \
./tests/qtest/migration-test --full -p /x86_64/migration/hmp/completion
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
tests/qtest/migration/misc-tests.c | 192 +++++++++++++++++++++++++++++
1 file changed, 192 insertions(+)
diff --git a/tests/qtest/migration/misc-tests.c b/tests/qtest/migration/misc-tests.c
index 533cf3d55d..04f46d5e81 100644
--- a/tests/qtest/migration/misc-tests.c
+++ b/tests/qtest/migration/misc-tests.c
@@ -11,6 +11,7 @@
*/
#include "qemu/osdep.h"
+#include "qemu/sockets.h"
#include "qapi/error.h"
#include "qobject/qjson.h"
#include "libqtest.h"
@@ -99,6 +100,46 @@ HMPTestData test_cases[] = {
"can only be set through QMP"),
};
+/*
+ * .input1: partial string with an ending TAB (as if pressed by
+ * the user).
+ * .input2: common root of the completions, i.e. what the partial
+ * part of .input1 string completes to.
+ * .output1: full list of completion suggestions for the string
+ * in .input2.
+ * E.g:
+ * (qemu) .input1
+ * <after TAB>
+ * (qemu) .input2
+ * .output1
+ */
+HMPTestData completion_cases[] = {
+ TEST("migra\t",
+ "migrate",
+ "migrate migrate_cancel migrate_continue migrate_incoming "
+ "migrate_pause migrate_recover migrate_set_capability "
+ "migrate_set_parameter migrate_start_postcopy"),
+
+ /*
+ * Note QEMU doesn't keep 'info' when offering the completions
+ * suggestions.
+ */
+ TEST("info migra\t",
+ "migrate",
+ "migrate migrate_capabilities migrate_parameters"),
+
+ TEST("migrate_se\t",
+ "migrate_set_",
+ "migrate_set_capability migrate_set_parameter"),
+
+ /*
+ * parameters and capabilities are not listed here to avoid having
+ * to enumerate them all, see test_hmp_completion().
+ */
+ TEST("migrate_set_parameter \t", "migrate_set_parameter ", "@params@"),
+ TEST("migrate_set_capability \t", "migrate_set_capability ", "@caps@"),
+};
+
/*
* Find a contiguous run of tokens in @larger that match the sequence
* of tokens in @smaller, ignoring mismatches due to sequences of
@@ -141,6 +182,22 @@ static bool token_list_is_substr(char **smaller, char **larger, int *last)
return match;
}
+static void assert_hmp_match_text(const char *str, const char *text)
+{
+ g_auto(GStrv) tok_str = g_strsplit_set(str, " ", -1);
+ g_auto(GStrv) tok_txt = g_strsplit_set(text, " \r\n", -1);
+ int idx;
+
+ if (token_list_is_substr(tok_str, tok_txt, &idx)) {
+ return;
+ }
+
+ g_test_message("HMP output mismatch for entry at line %d:", test_case_line);
+ g_test_message("expected vs. found (whitespace ignored):\n\n%s\n---\n%s",
+ str, text);
+ g_assert_not_reached();
+}
+
static void assert_hmp_match_line(const char *str, const char *text)
{
g_auto(GStrv) tok_str = g_strsplit_set(str, " ", -1);
@@ -217,6 +274,139 @@ static void test_hmp_migration_parameters(char *name, MigrateCommon *args)
qtest_quit(qts);
}
+
+static void hmp_sock_write(int fd, const char *buf)
+{
+ size_t sz = strlen(buf);
+
+ assert(fd > 0);
+ assert(write(fd, buf, sz) == sz);
+}
+
+static void hmp_sock_read(int fd, char *buf, size_t buf_sz)
+{
+ char *p = buf;
+ size_t sz = buf_sz - 1;
+
+ assert(fd >= 0);
+ memset(buf, 0, buf_sz);
+
+ while (sz > 0) {
+ ssize_t r = read(fd, p, sz);
+ char *prompt;
+
+ if (!r) {
+ break;
+ } else if (r < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ g_assert_not_reached();
+ }
+
+ p += r;
+ sz -= r;
+
+ prompt = strstr(buf, "(qemu) ");
+ if (prompt) {
+ *prompt = '\0';
+ break;
+ }
+ }
+}
+
+static int comp(const void *a, const void *b)
+{
+ return strcmp(*(const char **) a, *(const char **) b);
+}
+
+static void get_migration_opts_sorted(GString *exp, const char * const *lookup, int n)
+{
+ g_autofree char **opts_array = g_new0(char *, n);
+ uint8_t i;
+
+ for (i = 0; i < n; i++) {
+ opts_array[i] = g_strdup(lookup[i]);
+ }
+
+ qsort(opts_array, n, sizeof(char *), comp);
+
+ for (i = 0; i < n; i++) {
+ g_string_append(exp, opts_array[i]);
+ if (i + 1 != n) {
+ g_string_append(exp, " ");
+ }
+ }
+
+ for (i = 0; i < n; i++) {
+ g_free(opts_array[i]);
+ }
+}
+
+static void hmp_completion_single(int fd, const struct HMPTestData *t)
+{
+ g_autoptr(GString) exp = g_string_new("");
+ char buf[8192];
+ char *output;
+
+ test_case_line = t->line;
+
+ if (g_str_equal(t->output1, "@caps@")) {
+ g_string_append(exp, "migrate_set_capability ");
+ get_migration_opts_sorted(exp, MigrationCapability_lookup.array,
+ MIGRATION_CAPABILITY__MAX);
+ } else if (g_str_equal(t->output1, "@params@")) {
+ g_string_append(exp, "migrate_set_parameter ");
+ get_migration_opts_sorted(exp, MigrationParameter_lookup.array,
+ MIGRATION_PARAMETER__MAX);
+ } else {
+ g_string_append(exp, t->output1);
+ }
+
+ hmp_sock_write(fd, t->input1);
+ hmp_sock_read(fd, buf, sizeof(buf));
+
+ /*
+ * readline first rewrites the input to the common root of the
+ * completions, then outputs the completion suggestions:
+ *
+ * (qemu) info migr<TAB>
+ * (qemu) migrate migrate_parameters
+ * migrate_capabilities ...
+ */
+ output = strstr(buf, t->input2);
+ assert_hmp_match_text(exp->str, output);
+
+ /* ^U backward kill line */
+ hmp_sock_write(fd, "\x15");
+}
+
+static void test_hmp_completion(char *name, MigrateCommon *args)
+{
+ g_autofree char *cmdline;
+ char buf[1024];
+ QTestState *qts;
+ int sockfds[2];
+
+ assert(!qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds));
+ qemu_clear_cloexec(sockfds[1]);
+
+ cmdline = g_strdup_printf("-chardev socket,id=mon0,fd=%d "
+ "-mon chardev=mon0,mode=readline -S",
+ sockfds[1]);
+ qts = qtest_init(cmdline);
+ close(sockfds[1]);
+
+ /* read HMP banner */
+ hmp_sock_read(sockfds[0], buf, sizeof(buf));
+
+ for (int i = 0; i < G_N_ELEMENTS(completion_cases); i++) {
+ hmp_completion_single(sockfds[0], &completion_cases[i]);
+ }
+
+ close(sockfds[0]);
+ qtest_quit(qts);
+}
#endif /* CONFIG_HMP */
static void test_baddest(char *name, MigrateCommon *args)
@@ -460,5 +650,7 @@ void migration_test_add_misc(MigrationTestEnv *env)
#ifdef CONFIG_HMP
migration_test_add("/migration/hmp/parameters",
test_hmp_migration_parameters);
+ migration_test_add("/migration/hmp/completion",
+ test_hmp_completion);
#endif
}
--
2.53.0
next prev parent reply other threads:[~2026-09-09 21:46 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-09 21:44 [PATCH v2 00/18] migration: MigrationParameters changes Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 01/18] checkpatch: Fix checking of newlines in error messages Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 02/18] migration/options.c: Don't export migrate_tls_opts_free Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 03/18] migration: Rename variables in qmp_migrate_set_parameters Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 04/18] migration: Use QAPI_CLONE_MEMBERS in migrate_params_apply Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 05/18] migration: Merge parameter structs instead of assigning one by one Fabiano Rosas
2026-09-10 12:28 ` Peter Xu
2026-09-09 21:44 ` [PATCH v2 06/18] migration: Open code migrate_params_apply Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 07/18] migration: Stop freeing s->parameters members individually Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 08/18] migration: Use migrate_params_free during finalize Fabiano Rosas
2026-09-09 21:44 ` [PATCH v2 09/18] tests/qtest/migration: Add a test for HMP Fabiano Rosas
2026-09-10 14:02 ` Peter Xu
2026-09-13 20:30 ` Dr. David Alan Gilbert
2026-09-09 21:45 ` Fabiano Rosas [this message]
2026-09-10 17:36 ` [PATCH v2 10/18] tests/qtest/migration: Add a test for HMP completion Peter Xu
2026-09-13 20:58 ` Dr. David Alan Gilbert
2026-09-09 21:45 ` [PATCH v2 11/18] migration: HMP: Fix bandwidth parameters Fabiano Rosas
2026-09-10 6:05 ` Markus Armbruster
2026-09-10 12:37 ` Fabiano Rosas
2026-09-11 6:19 ` Markus Armbruster
2026-09-10 13:20 ` Dr. David Alan Gilbert
2026-09-10 13:26 ` Dr. David Alan Gilbert
2026-09-10 17:38 ` Peter Xu
2026-09-09 21:45 ` [PATCH v2 12/18] migration: Change HMP 'info migrate_parameters' output Fabiano Rosas
2026-09-10 7:32 ` Markus Armbruster
2026-09-10 13:02 ` Fabiano Rosas
2026-09-11 6:46 ` Markus Armbruster
2026-09-09 21:45 ` [PATCH v2 13/18] migration: Use keyval input visitor in HMP set command Fabiano Rosas
2026-09-10 11:07 ` Markus Armbruster
2026-09-10 14:15 ` Fabiano Rosas
2026-09-10 22:10 ` Fabiano Rosas
2026-09-11 8:22 ` Markus Armbruster
2026-09-11 12:54 ` Fabiano Rosas
2026-09-11 8:12 ` Markus Armbruster
2026-09-09 21:45 ` [PATCH v2 14/18] migration: Use output visitor in info command Fabiano Rosas
2026-09-09 21:45 ` [PATCH v2 15/18] migration: Rewrite migrate_set_parameter_completion using QDict Fabiano Rosas
2026-09-10 11:17 ` Markus Armbruster
2026-09-10 13:09 ` Fabiano Rosas
2026-09-11 7:15 ` Markus Armbruster
2026-09-09 21:45 ` [PATCH v2 16/18] migration: Add capabilities into MigrationParameters Fabiano Rosas
2026-09-10 11:22 ` Markus Armbruster
2026-09-09 21:45 ` [PATCH v2 17/18] migration: Remove s->capabilities Fabiano Rosas
2026-09-09 21:45 ` [PATCH v2 18/18] qapi/migration: Deprecate capabilities commands Fabiano Rosas
2026-09-10 17:35 ` [PATCH v2 00/18] migration: MigrationParameters changes Peter Xu
2026-09-10 19:27 ` Fabiano Rosas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260909214509.237309-11-farosas@suse.de \
--to=farosas@suse.de \
--cc=armbru@redhat.com \
--cc=dave@treblig.org \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.