@samitouri / QOSamiQemu / commits / 0e7aa78b0b

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>

Ganesh Harshan committed Jun 25, 2026 at 12:53 UTC 0e7aa78b0bee50b076785d4cf4a1b7546b3bed31
1 file changed +25 -6
tests/functional/qemu_test/testcase.py
+25 -6
@@ -314,13 +314,32 @@ class QemuSystemTest(QemuBaseTest):
314 console_log.addHandler(self._console_log_fh)
315
316 def set_machine(self, machinename):
317 - # TODO: We should use QMP to get the list of available machines
318 - if not self._machinehelp:
319 - self._machinehelp = run(
320 - [self.qemu_bin, '-M', 'help'],
321 - capture_output=True, check=True, encoding='utf8').stdout
322 - if self._machinehelp.find(machinename) < 0:
317 + cls = type(self)
318 +
319 + if not hasattr(cls, "_machines"):
320 + tmp_vm = QEMUMachine(self.qemu_bin)
321 + tmp_vm.set_machine('none')
322 +
323 + try:
324 + tmp_vm.launch()
325 + resp = tmp_vm.qmp('query-machines')
326 +
327 + machines = resp.get('return', [])
328 + cls._machines = [
329 + m.get('name') for m in machines if 'name' in m
330 + ]
331 +
332 + finally:
333 + try:
334 + tmp_vm.shutdown()
335 + except Exception:
336 + pass
337 +
338 + self._machines = cls._machines
339 +
340 + if machinename not in self._machines:
341 self.skipTest('no support for machine ' + machinename)
342 +
343 self.machine = machinename
344
345 def require_accelerator(self, accelerator):