On 07.07.26 09:19, Thomas Huth wrote:
From: Ganesh Harshan <[email protected]>

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 <[email protected]>
Reviewed-by: Daniel P. BerrangĂ© <[email protected]>
Message-ID: <[email protected]>
[thuth: Drop problematic self.vm.set_machine() statement]
Signed-off-by: Thomas Huth <[email protected]>
---
  tests/functional/qemu_test/testcase.py | 31 +++++++++++++++++++++-----
  1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/tests/functional/qemu_test/testcase.py 
b/tests/functional/qemu_test/testcase.py
index eaec1bea13..4912a47a46 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -314,13 +314,32 @@ def setUp(self):
          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

This doesn't cover aliases, now tests using "q35" are skipped. I'm sending a 
patch..

+                ]
+
+            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):


--
Best regards,
Vladimir

Reply via email to