Re: [PATCH v3 11/49] python, tests: switch usernet queries from HMP to QMP
Thomas Huth <[email protected]>
| Newsgroups | gmane.comp.emulators.qemu |
|---|---|
| Message-ID | <[email protected]> |
On 16/08/2026 21.12, Marc-André Lureau wrote:
> Replace human-monitor-command "info usernet" calls with the structured
> x-query-usernet QMP command in get_usernet_hostfwd_port() and the VM
> test runner boot path.
>
> Since x-query-usernet returns a list of UsernetInfo entries, callers
> now iterate over the result and extract the port from each entry's
> "info" field. Also switch get_info_usernet_hostfwd_port() from
> split('\r\n') to splitlines(), since QMP strings use \n rather than
> the HMP monitor's \r\n line endings.
>
> Reviewed-by: Daniel P. Berrangé <[email protected]>
> Signed-off-by: Marc-Andre Lureau <[email protected]>
> ---
> python/qemu/utils/__init__.py | 5 +++--
> tests/functional/qemu_test/utils.py | 8 ++++++--
> tests/vm/basevm.py | 13 ++++++++-----
> 3 files changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/python/qemu/utils/__init__.py b/python/qemu/utils/__init__.py
> index be5daa836340..e32911d4c6df 100644
> --- a/python/qemu/utils/__init__.py
> +++ b/python/qemu/utils/__init__.py
> @@ -46,10 +46,11 @@ def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]:
> """
> Returns the port given to the hostfwd parameter via info usernet
>
> - :param info_usernet_output: output generated by hmp command "info usernet"
> + :param info_usernet_output: output generated by "info usernet" or
> + the "info" field from x-query-usernet
> :return: the port number allocated by the hostfwd option
> """
> - for line in info_usernet_output.split('\r\n'):
> + for line in info_usernet_output.splitlines():
> regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.'
> match = re.search(regex, line)
> if match is not None:
> diff --git a/tests/functional/qemu_test/utils.py b/tests/functional/qemu_test/utils.py
> index 826c267785bd..0992db49ab28 100644
> --- a/tests/functional/qemu_test/utils.py
> +++ b/tests/functional/qemu_test/utils.py
> @@ -14,8 +14,12 @@
>
>
> def get_usernet_hostfwd_port(vm):
> - res = vm.cmd('human-monitor-command', command_line='info usernet')
> - return get_info_usernet_hostfwd_port(res)
> + res = vm.cmd('x-query-usernet')
> + for entry in res:
> + port = get_info_usernet_hostfwd_port(entry['info'])
> + if port is not None:
> + return port
> + return None
>
> def pow2ceil(x):
> """
> diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
> index 9e879e966a36..198b04e8c375 100644
> --- a/tests/vm/basevm.py
> +++ b/tests/vm/basevm.py
> @@ -312,12 +312,15 @@ def boot(self, img, extra_args=[]):
> self._guest = guest
> # Init console so we can start consuming the chars.
> self.console_init()
> - usernet_info = guest.cmd("human-monitor-command",
> - command_line="info usernet")
> - self.ssh_port = get_info_usernet_hostfwd_port(usernet_info)
> + res = guest.cmd("x-query-usernet")
> + for entry in res:
> + port = get_info_usernet_hostfwd_port(entry['info'])
> + if port is not None:
> + self.ssh_port = port
> + break
> if not self.ssh_port:
> - raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \
> - usernet_info)
> + raise Exception("Cannot find ssh port from"
> + " 'x-query-usernet': %s" % res)
>
> def console_init(self, timeout = None):
> if timeout == None:
While you're at it, may I suggest to move the cmd('x-query-usernet') into
the get_info_usernet_hostfwd_port() function in
python/qemu/utils/__init__.py, too, so that the callers don't have to take
care about this anymore? Also just return the port number from there, so
that the callers don't have to take care about the parsing. WDYT?
Thomas