tests/functional: use QMP to query available machines

Replace parsing of "qemu -M help" in set_machine() with
QMP "query-machines".

The previous approach relied on parsing human-readable CLI
output and substring matching, which is fragile and prone to
incorrect matches. It is also sensitive to output format changes.

Use QMP instead to retrieve structured machine information,
ensuring accurate matching and better maintainability.

Cache the result at the class level to avoid repeated QEMU
startup overhead.

Signed-off-by: Ganesh Harshan <ganeshredcobra@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Message-ID: <20260625165310.54113-1-ganeshredcobra@gmail.com>
[thuth: Drop problematic self.vm.set_machine() statement]
Signed-off-by: Thomas Huth <th.huth@posteo.eu>
This commit is contained in:
Ganesh Harshan
2026-06-25 12:53:10 -04:00
committed by Thomas Huth
parent d1edce955d
commit 0e7aa78b0b

View File

@@ -314,13 +314,32 @@ class QemuSystemTest(QemuBaseTest):
console_log.addHandler(self._console_log_fh)
def set_machine(self, machinename):
# TODO: We should use QMP to get the list of available machines
if not self._machinehelp:
self._machinehelp = run(
[self.qemu_bin, '-M', 'help'],
capture_output=True, check=True, encoding='utf8').stdout
if self._machinehelp.find(machinename) < 0:
cls = type(self)
if not hasattr(cls, "_machines"):
tmp_vm = QEMUMachine(self.qemu_bin)
tmp_vm.set_machine('none')
try:
tmp_vm.launch()
resp = tmp_vm.qmp('query-machines')
machines = resp.get('return', [])
cls._machines = [
m.get('name') for m in machines if 'name' in m
]
finally:
try:
tmp_vm.shutdown()
except Exception:
pass
self._machines = cls._machines
if machinename not in self._machines:
self.skipTest('no support for machine ' + machinename)
self.machine = machinename
def require_accelerator(self, accelerator):