Skip to contentSkip to conversation

tests/functional: use QMP to query available machines

PATCH In review Ganesh Harshan 3 participants 4 messages Jun 25 – Jul 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 tests/functional/qemu_test/testcase.py | 32 +++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 6 deletions(-)
tests/functional/qemu_test/testcase.py +26 -6
index eaec1bea13..5920d6a784 100644
@@ -314,14 +314,34 @@ 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
+                ]
+
+            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
+        self.vm.set_machine(machinename)
 
     def require_accelerator(self, accelerator):
         """
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Ganesh Harshan

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.

Quoted 7 lines

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

---
 tests/functional/qemu_test/testcase.py | 32 +++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 6 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#2 lore raw
In reply to Ganesh Harshan

Am Thu, 25 Jun 2026 12:53:10 -0400
schrieb Ganesh Harshan <[email protected]>:

Quoted 10 lines

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.

Quoted 7 lines

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

---
 tests/functional/qemu_test/testcase.py | 32 +++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 6 deletions(-)

Hi Ganesh,

seems like a bunch of tests are failing when I include this patch in my source tree:

283/327 qemu:func-thorough+func-riscv64-thorough+thorough / func-riscv64-tuxrun               TIMEOUT         120.01s   killed by signal 15 SIGTERM
286/327 qemu:func-thorough+func-mipsel-thorough+thorough / func-mipsel-tuxrun                 TIMEOUT          90.01s   killed by signal 15 SIGTERM
289/327 qemu:func-thorough+func-mips-thorough+thorough / func-mips-tuxrun                     TIMEOUT          90.01s   killed by signal 15 SIGTERM
294/327 qemu:func-thorough+func-ppc64-thorough+thorough / func-ppc64-e500                     TIMEOUT          90.01s   killed by signal 15 SIGTERM
326/327 qemu:func-thorough+func-ppc-thorough+thorough / func-ppc-tuxrun                       TIMEOUT          90.01s   killed by signal 15 SIGTERM
327/327 qemu:func-thorough+func-xtensa-thorough+thorough / func-xtensa-lx60                   TIMEOUT          90.01s   killed by signal 15 SIGTERM

Are these working for you? Could you please have a look?

Quoted 5 lines
tests/functional/qemu_test/testcase.py +0 -0
index eaec1bea13..5920d6a784 100644
@@ -314,14 +314,34 @@ def setUp(self):

...

tests/functional/qemu_test/testcase.py def setUp(self): · line 344

Quoted 5 lines
tests/functional/qemu_test/testcase.py +3 -0
+        if machinename not in self._machines:
             self.skipTest('no support for machine ' + machinename)
+
         self.machine = machinename
+        self.vm.set_machine(machinename)

The last line was not there before, why is it required now?

Thomas

Quoted 2 lines

def require_accelerator(self, accelerator):
"""

#3 lore raw

Am Fri, 3 Jul 2026 21:21:11 +0200
schrieb Thomas Huth <[email protected]>:

Quoted 41 lines

Am Thu, 25 Jun 2026 12:53:10 -0400
schrieb Ganesh Harshan <[email protected]>:

Quoted 10 lines

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.

Quoted 7 lines

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

---
 tests/functional/qemu_test/testcase.py | 32 +++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 6 deletions(-)  

Hi Ganesh,

seems like a bunch of tests are failing when I include this patch in my source tree:

283/327 qemu:func-thorough+func-riscv64-thorough+thorough / func-riscv64-tuxrun               TIMEOUT         120.01s   killed by signal 15 SIGTERM
286/327 qemu:func-thorough+func-mipsel-thorough+thorough / func-mipsel-tuxrun                 TIMEOUT          90.01s   killed by signal 15 SIGTERM
289/327 qemu:func-thorough+func-mips-thorough+thorough / func-mips-tuxrun                     TIMEOUT          90.01s   killed by signal 15 SIGTERM
294/327 qemu:func-thorough+func-ppc64-thorough+thorough / func-ppc64-e500                     TIMEOUT          90.01s   killed by signal 15 SIGTERM
326/327 qemu:func-thorough+func-ppc-thorough+thorough / func-ppc-tuxrun                       TIMEOUT          90.01s   killed by signal 15 SIGTERM
327/327 qemu:func-thorough+func-xtensa-thorough+thorough / func-xtensa-lx60                   TIMEOUT          90.01s   killed by signal 15 SIGTERM

Are these working for you? Could you please have a look?

Quoted 5 lines
tests/functional/qemu_test/testcase.py +0 -0
index eaec1bea13..5920d6a784 100644
@@ -314,14 +314,34 @@ def setUp(self):  

...

Quoted 7 lines

tests/functional/qemu_test/testcase.py def setUp(self): · line 344

Quoted 5 lines
tests/functional/qemu_test/testcase.py +3 -0
+        if machinename not in self._machines:
             self.skipTest('no support for machine ' + machinename)
+
         self.machine = machinename
+        self.vm.set_machine(machinename)  

The last line was not there before, why is it required now?

I noticed that the tests work fine again when I drop that line. So I'll go ahead and pick up this patch without this line.

Thomas