Skip to contentSkip to conversation

tests/functional: misc fixes for reliability / debuggability

PATCH In review Daniel P. Berrangé 3 participants 10 messages Sep 8, 2025 – Sep 9, 2025

lore
Share & tools
lore
#0 lore raw

cover tests/functional: misc fixes for reliability / debuggability

Daniel P. Berrangé (4):
tests/functional: fix infinite loop on console EOF tests/functional: avoid duplicate messages on failures tests/functional: avoid tearDown failure when QEMU dies tests/functional: purge scratch dir on test startup

 tests/functional/qemu_test/cmd.py      |  2 +-
 tests/functional/qemu_test/testcase.py | 14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/4 tests/functional: fix infinite loop on console EOF

The 'recv' method will return an empty byte array, not None, when the socket has EOF.

---
 tests/functional/qemu_test/cmd.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/functional/qemu_test/cmd.py +1 -1
index dc5f422b77..2fb731ca24 100644
@@ -51,7 +51,7 @@ def _console_read_line_until_match(test, vm, success, failure):
     done = False
     while True:
         c = vm.console_socket.recv(1)
-        if c is None:
+        if not c:
             done = True
             test.fail(
                 f"EOF in console, expected '{success}'")
Signature · 1 line
2.50.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] tests/functional: fix infinite loop on console EOF

Quoted 13 lines · Daniel P. Berrangé

The 'recv' method will return an empty byte array, not None, when the socket has EOF.

---
  tests/functional/qemu_test/cmd.py | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/functional/qemu_test/cmd.py +0 -0
index dc5f422b77..2fb731ca24 100644

tests/functional/qemu_test/cmd.py def _console_read_line_until_match(test, vm, success, failure): · line 57

Quoted 8 lines
tests/functional/qemu_test/cmd.py +1 -1
@@ -51,7 +51,7 @@ def _console_read_line_until_match(test, vm, success, failure):
      done = False
      while True:
          c = vm.console_socket.recv(1)
-        if c is None:
+        if not c:
              done = True
              test.fail(
                  f"EOF in console, expected '{success}'")
#4 lore raw

PATCH 2/4 tests/functional: avoid duplicate messages on failures

In reply to Daniel P. Berrangé

In some scenarios the same tests is mentioned in both the 'res.results.errors' and 'res.results.failures' array returned by unittest.main(). This was seen when the 'tearDown' method raised an exception.

In such a case, we printed out the same information about where to find a log file twice for each test. Track which tests we have already reported on, to avoid the duplication.

---
 tests/functional/qemu_test/testcase.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tests/functional/qemu_test/testcase.py +3 -2
index fbeb171058..82a7724404 100644
@@ -251,13 +251,14 @@ def main():
                                    test_output_log = pycotap.LogMode.LogToError)
         res = unittest.main(module = None, testRunner = tr, exit = False,
                             argv=[sys.argv[0], path] + sys.argv[1:])
+        failed = {}
         for (test, message) in res.result.errors + res.result.failures:
-
-            if hasattr(test, "log_filename"):
+            if hasattr(test, "log_filename") and not test.id() in failed:
                 print('More information on ' + test.id() + ' could be found here:'
                       '\n %s' % test.log_filename, file=sys.stderr)
                 if hasattr(test, 'console_log_name'):
                     print(' %s' % test.console_log_name, file=sys.stderr)
+                failed[test.id()] = True
         sys.exit(not res.result.wasSuccessful())
 
 
Signature · 1 line
2.50.1
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] tests/functional: avoid duplicate messages on failures

Quoted 23 lines · Daniel P. Berrangé

In some scenarios the same tests is mentioned in both the 'res.results.errors' and 'res.results.failures' array returned by unittest.main(). This was seen when the 'tearDown' method raised an exception.

In such a case, we printed out the same information about where to find a log file twice for each test. Track which tests we have already reported on, to avoid the duplication.

---
  tests/functional/qemu_test/testcase.py | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
tests/functional/qemu_test/testcase.py +1 -0
index fbeb171058..82a7724404 100644
@@ -251,13 +251,14 @@ def main():
                                     test_output_log = pycotap.LogMode.LogToError)
          res = unittest.main(module = None, testRunner = tr, exit = False,
                              argv=[sys.argv[0], path] + sys.argv[1:])
+        failed = {}

tests/functional/qemu_test/testcase.py def main(): · line 262

Quoted 10 lines
tests/functional/qemu_test/testcase.py +2 -2
          for (test, message) in res.result.errors + res.result.failures:
-
-            if hasattr(test, "log_filename"):
+            if hasattr(test, "log_filename") and not test.id() in failed:
                  print('More information on ' + test.id() + ' could be found here:'
                        '\n %s' % test.log_filename, file=sys.stderr)
                  if hasattr(test, 'console_log_name'):
                      print(' %s' % test.console_log_name, file=sys.stderr)
+                failed[test.id()] = True
          sys.exit(not res.result.wasSuccessful())
#6 lore raw

PATCH 3/4 tests/functional: avoid tearDown failure when QEMU dies

In reply to Daniel P. Berrangé

In a QEMU process under test dies unexpectedly, the 'shutdown' method may well raise an exeception. This causes the tearDown method to fail, which means any later cleanup code fails to get run. Most notably the log handlers don't get removed so the base.log file from an earlier test will get polluted with messages from any subsequent tests. The tearDown failure also results in pages of exceptions printed on the console, which obscures the real failure message / trace printed by the test.

Ignore any shutdown failures in the tearDown method, since any test which cares about clean shutdown should have already cleaned up any running VMs. The tearDown method is just there as a safety net to cleanup resources. The base.log file will still containing log messages from the failed 'vm.shutdown' call too.

---
 tests/functional/qemu_test/testcase.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tests/functional/qemu_test/testcase.py +4 -1
index 82a7724404..faa0a4f0db 100644
@@ -404,7 +404,10 @@ def set_vm_arg(self, arg, value):
 
     def tearDown(self):
         for vm in self._vms.values():
-            vm.shutdown()
+            try:
+                vm.shutdown()
+            except Exception as ex:
+                self.log.error("Failed to teardown VM: %s" % ex)
         logging.getLogger('console').removeHandler(self._console_log_fh)
         self._console_log_fh.close()
         super().tearDown()
Signature · 1 line
2.50.1
Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] tests/functional: avoid tearDown failure when QEMU dies

Quoted 27 lines · Daniel P. Berrangé

In a QEMU process under test dies unexpectedly, the 'shutdown' method may well raise an exeception. This causes the tearDown method to fail, which means any later cleanup code fails to get run. Most notably the log handlers don't get removed so the base.log file from an earlier test will get polluted with messages from any subsequent tests. The tearDown failure also results in pages of exceptions printed on the console, which obscures the real failure message / trace printed by the test.

Ignore any shutdown failures in the tearDown method, since any test which cares about clean shutdown should have already cleaned up any running VMs. The tearDown method is just there as a safety net to cleanup resources. The base.log file will still containing log messages from the failed 'vm.shutdown' call too.

---
  tests/functional/qemu_test/testcase.py | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
tests/functional/qemu_test/testcase.py +0 -0
index 82a7724404..faa0a4f0db 100644
@@ -404,7 +404,10 @@ def set_vm_arg(self, arg, value):
 

tests/functional/qemu_test/testcase.py def set_vm_arg(self, arg, value): · line 413

Quoted 10 lines
tests/functional/qemu_test/testcase.py +4 -1
      def tearDown(self):
          for vm in self._vms.values():
-            vm.shutdown()
+            try:
+                vm.shutdown()
+            except Exception as ex:
+                self.log.error("Failed to teardown VM: %s" % ex)
          logging.getLogger('console').removeHandler(self._console_log_fh)
          self._console_log_fh.close()
          super().tearDown()
#8 lore raw

PATCH 4/4 tests/functional: purge scratch dir on test startup

In reply to Daniel P. Berrangé

The test suite purges the scratch dir in the tearDown method, but if python crashes (or is non-gracefully killed) this won't get run. Also the user can set QEMU_TEST_KEEP_SCRATCH to disable cleanup.

Purging the scratch dir on startup ensures that tests always run from a clean state.

---
 tests/functional/qemu_test/testcase.py | 4 ++++
 1 file changed, 4 insertions(+)
tests/functional/qemu_test/testcase.py +4 -0
index faa0a4f0db..2c0abde395 100644
@@ -205,6 +205,10 @@ def setUp(self):
         self.outputdir = self.build_file('tests', 'functional',
                                          self.arch, self.id())
         self.workdir = os.path.join(self.outputdir, 'scratch')
+        if os.path.exists(self.workdir):
+            # Purge as safety net in case of unclean termination of
+            # previous test, or use of QEMU_TEST_KEEP_SCRATCH
+            shutil.rmtree(self.workdir)
         os.makedirs(self.workdir, exist_ok=True)
 
         self.log_filename = self.log_file('base.log')
Signature · 1 line
2.50.1
Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] tests/functional: purge scratch dir on test startup

Quoted 9 lines · Daniel P. Berrangé

The test suite purges the scratch dir in the tearDown method, but if python crashes (or is non-gracefully killed) this won't get run. Also the user can set QEMU_TEST_KEEP_SCRATCH to disable cleanup.

Purging the scratch dir on startup ensures that tests always run from a clean state.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro