test/functional: improve functional test debugging & fix tuxrun
cover test/functional: improve functional test debugging & fix tuxrun
This started out as a series to get rid of the many GBs of temp files the functional tests leave behind. Then it expanded into improving the functional test debugging by ensuring we preserve the QEMU stdout/stderr log file created by the QEMUMachine class. In the course of doing that I encountered some other minor points worth fixing, and then got side tracked into looking at the tuxrun hangs with aarch64be. Investigating the latter exposed some further holes in the debugging story prompting yet more patches, as well as a final solution for tuxrun. So this series does:
* Purge all scratch files created by tests
* Preserve the stdout/stderr log file
* Capture debug log messages on QEMUMachine
* Provide a QMP backdoor for debugging stuck QEMUs
* Enhance console handling for partial line matches
* Fix the tuxrun tests by eliminating sleeps
There's quite alot of code here, but at the same time it feels like the kind of stuff that'll be valuable either in the 9.2 release, or in the soon to exist 9.2 stable branch.
NB, with this series applied Thomas' tuxrun conversion to functional testing survives 200 iterations on my machine, whereas it would reliably hang in < 20, and often in < 10, before.
Daniel P. Berrangé (15):
tests/functional: fix mips64el test to honour workdir tests/functional: automatically clean up scratch files after tests tests/functional: remove "AVOCADO" from env variable name tests/functional: remove todo wrt avocado.utils.wait_for tests/functional: remove leftover :avocado: tags tests/functional: remove obsolete reference to avocado bug tests/functional: remove comments talking about avocado tests/functional: honour self.workdir in ACPI bits tests tests/functional: put QEMUMachine logs in testcase log directory tests/functional: honour requested test VM name in QEMUMachine tests/functional: enable debug logging for QEMUMachine tests/functional: logs details of console interaction operations tests/functional: rewrite console handling to be bytewise tests/functional: remove time.sleep usage from tuxrun tests tests/functional: add a QMP backdoor for debugging stalled tests
docs/devel/testing/functional.rst | 16 ++++++ tests/functional/qemu_test/cmd.py | 65 ++++++++++++++++++------ tests/functional/qemu_test/testcase.py | 33 +++++++++--- tests/functional/qemu_test/tuxruntest.py | 17 +++---- tests/functional/test_acpi_bits.py | 56 +++++++------------- tests/functional/test_arm_bpim2u.py | 20 -------- tests/functional/test_arm_orangepi.py | 27 ---------- tests/functional/test_m68k_nextcube.py | 3 +- tests/functional/test_mips64el_malta.py | 4 +- 9 files changed, 119 insertions(+), 122 deletions(-)
Signature · 1 line
2.46.0
PATCH 1/15 tests/functional: fix mips64el test to honour workdir
The missing directory separator resulted in the kernel file being created 1 level higher than expected.
--- tests/functional/test_mips64el_malta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/functional/test_mips64el_malta.py +1 -1
@@ -129,7 +129,7 @@ def do_test_i6400_framebuffer_logo(self, cpu_cores_count): screendump_path = os.path.join(self.workdir, 'screendump.pbm') kernel_path_gz = self.ASSET_KERNEL_4_7_0.fetch() - kernel_path = self.workdir + "vmlinux" + kernel_path = self.workdir + "/vmlinux" gzip_uncompress(kernel_path_gz, kernel_path) tuxlogo_path = self.ASSET_TUXLOGO.fetch()
Signature · 1 line
2.46.0
Re: [PATCH 01/15] tests/functional: fix mips64el test to honour workdir
Quoted 4 lines · Daniel P. Berrangé
The missing directory separator resulted in the kernel file being created 1 level higher than expected.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH 01/15] tests/functional: fix mips64el test to honour workdir
In reply to Daniel P. BerrangéQuoted 13 lines · Daniel P. Berrangé
The missing directory separator resulted in the kernel file being created 1 level higher than expected.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/test_mips64el_malta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/functional/test_mips64el_malta.py +0
-0
tests/functional/test_mips64el_malta.py def do_test_i6400_framebuffer_logo(self, cpu_cores_count): · line 132
Quoted 5 lines
tests/functional/test_mips64el_malta.py +1
-1@@ -129,7 +129,7 @@ def do_test_i6400_framebuffer_logo(self, cpu_cores_count): screendump_path = os.path.join(self.workdir, 'screendump.pbm') kernel_path_gz = self.ASSET_KERNEL_4_7_0.fetch() - kernel_path = self.workdir + "vmlinux" + kernel_path = self.workdir + "/vmlinux"
Safer is to use os.path.join(), anyway:
Quoted 3 lines
tests/functional/test_mips64el_malta.py +0
-0gzip_uncompress(kernel_path_gz, kernel_path) tuxlogo_path = self.ASSET_TUXLOGO.fetch()
PATCH 2/15 tests/functional: automatically clean up scratch files after tests
In reply to Daniel P. BerrangéThe build/tests/functional subdirectories are consuming huge amounts of disk space.
Split the location for scratch files into a 'scratch' sub-directory, separate from log files, and delete it upon completion of each test. The new env variable QEMU_TEST_KEEP_SCRATCH can be set to preserve this scratch dir for debugging access if required.
--- docs/devel/testing/functional.rst | 6 ++++++ tests/functional/qemu_test/testcase.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-)
docs/devel/testing/functional.rst +6 -0
@@ -65,6 +65,12 @@ to the QEMU binary that should be used for the test, for example:: $ export QEMU_TEST_QEMU_BINARY=$PWD/qemu-system-x86_64 $ python3 ../tests/functional/test_file.py +The test framework will automatically purge any scratch files created during +the tests. If needing to debug a failed test, it is possible to keep these +files around on disk by setting ```QEMU_TEST_KEEP_SCRATCH=1``` as an env +variable. Any preserved files will be deleted the next time the test is run +without this variable set. + Overview --------
tests/functional/qemu_test/testcase.py +9 -5
@@ -13,8 +13,9 @@ import logging import os -import subprocess import pycotap +import shutil +import subprocess import sys import unittest import uuid
@@ -40,11 +41,12 @@ def setUp(self, bin_prefix): self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set') self.arch = self.qemu_bin.split('-')[-1] - self.workdir = os.path.join(BUILD_DIR, 'tests/functional', self.arch, - self.id()) + self.outputdir = os.path.join(BUILD_DIR, 'tests', 'functional', + self.arch, self.id()) + self.workdir = os.path.join(self.outputdir, 'scratch') os.makedirs(self.workdir, exist_ok=True) - self.logdir = self.workdir + self.logdir = self.outputdir self.log_filename = os.path.join(self.logdir, 'base.log') self.log = logging.getLogger('qemu-test') self.log.setLevel(logging.DEBUG)
@@ -56,6 +58,8 @@ def setUp(self, bin_prefix): self.log.addHandler(self._log_fh) def tearDown(self): + if "QEMU_TEST_KEEP_SCRATCH" not in os.environ: + shutil.rmtree(self.workdir) self.log.removeHandler(self._log_fh) def main():
@@ -108,7 +112,7 @@ def setUp(self): console_log = logging.getLogger('console') console_log.setLevel(logging.DEBUG) - self.console_log_name = os.path.join(self.workdir, 'console.log') + self.console_log_name = os.path.join(self.logdir, 'console.log') self._console_log_fh = logging.FileHandler(self.console_log_name, mode='w') self._console_log_fh.setLevel(logging.DEBUG)
Signature · 1 line
2.46.0
Re: [PATCH 02/15] tests/functional: automatically clean up scratch files after tests
Quoted 47 lines · Daniel P. Berrangé
The build/tests/functional subdirectories are consuming huge amounts of disk space.
Split the location for scratch files into a 'scratch' sub-directory, separate from log files, and delete it upon completion of each test. The new env variable
QEMU_TEST_KEEP_SCRATCHcan be set to preserve this scratch dir for debugging access if required.Signed-off-by Daniel P. Berrangé <[email protected]>--- docs/devel/testing/functional.rst | 6 ++++++ tests/functional/qemu_test/testcase.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-)docs/devel/testing/functional.rst +6
-0@@ -65,6 +65,12 @@ to the QEMU binary that should be used for the test, for example:: $ export QEMU_TEST_QEMU_BINARY=$PWD/qemu-system-x86_64 $ python3 ../tests/functional/test_file.py +The test framework will automatically purge any scratch files created during +the tests. If needing to debug a failed test, it is possible to keep these +files around on disk by setting ```QEMU_TEST_KEEP_SCRATCH=1``` as an env +variable. Any preserved files will be deleted the next time the test is run +without this variable set. + Overview --------tests/functional/qemu_test/testcase.py +2
-1@@ -13,8 +13,9 @@ import logging import os -import subprocess import pycotap +import shutil +import subprocess import sys import unittest import uuid
tests/functional/qemu_test/testcase.py def setUp(self, bin_prefix): · line 45
Quoted 7 lines
tests/functional/qemu_test/testcase.py +2
-2@@ -40,11 +41,12 @@ def setUp(self, bin_prefix): self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set') self.arch = self.qemu_bin.split('-')[-1] - self.workdir = os.path.join(BUILD_DIR, 'tests/functional', self.arch, - self.id()) + self.outputdir = os.path.join(BUILD_DIR, 'tests', 'functional', + self.arch, self.id())
I don't think you need save self.outputdir as only self.logdir is used by the other functions.
Quoted 18 lines
tests/functional/qemu_test/testcase.py +4
-1+ self.workdir = os.path.join(self.outputdir, 'scratch') os.makedirs(self.workdir, exist_ok=True) - self.logdir = self.workdir + self.logdir = self.outputdir self.log_filename = os.path.join(self.logdir, 'base.log') self.log = logging.getLogger('qemu-test') self.log.setLevel(logging.DEBUG)@@ -56,6 +58,8 @@ def setUp(self, bin_prefix): self.log.addHandler(self._log_fh) def tearDown(self): + if "QEMU_TEST_KEEP_SCRATCH" not in os.environ: + shutil.rmtree(self.workdir) self.log.removeHandler(self._log_fh) def main():
tests/functional/qemu_test/testcase.py def setUp(self): · line 118
Quoted 8 lines
tests/functional/qemu_test/testcase.py +1
-1@@ -108,7 +112,7 @@ def setUp(self): console_log = logging.getLogger('console') console_log.setLevel(logging.DEBUG) - self.console_log_name = os.path.join(self.workdir, 'console.log') + self.console_log_name = os.path.join(self.logdir, 'console.log') self._console_log_fh = logging.FileHandler(self.console_log_name, mode='w') self._console_log_fh.setLevel(logging.DEBUG)
Otherwise:
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Thomas Huth
Reviewed-byRe: [PATCH 02/15] tests/functional: automatically clean up scratch files after tests
Quoted 52 lines · Alex Bennée
Quoted 50 lines · Daniel P. Berrangé
The build/tests/functional subdirectories are consuming huge amounts of disk space.
Split the location for scratch files into a 'scratch' sub-directory, separate from log files, and delete it upon completion of each test. The new env variable
QEMU_TEST_KEEP_SCRATCHcan be set to preserve this scratch dir for debugging access if required.Signed-off-by Daniel P. Berrangé <[email protected]>--- docs/devel/testing/functional.rst | 6 ++++++ tests/functional/qemu_test/testcase.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-)docs/devel/testing/functional.rst +6
-0@@ -65,6 +65,12 @@ to the QEMU binary that should be used for the test, for example:: $ export QEMU_TEST_QEMU_BINARY=$PWD/qemu-system-x86_64 $ python3 ../tests/functional/test_file.py +The test framework will automatically purge any scratch files created during +the tests. If needing to debug a failed test, it is possible to keep these +files around on disk by setting ```QEMU_TEST_KEEP_SCRATCH=1``` as an env +variable. Any preserved files will be deleted the next time the test is run +without this variable set. + Overview --------tests/functional/qemu_test/testcase.py +2
-1@@ -13,8 +13,9 @@ import logging import os -import subprocess import pycotap +import shutil +import subprocess import sys import unittest import uuid@@ -40,11 +41,12 @@ def setUp(self, bin_prefix): self.assertIsNotNone(self.qemu_bin, 'QEMU_TEST_QEMU_BINARY must be set') self.arch = self.qemu_bin.split('-')[-1]
Quoted 7 lines
tests/functional/qemu_test/testcase.py def setUp(self, bin_prefix): · line 45
Quoted 4 lines
tests/functional/qemu_test/testcase.py +2
-2- self.workdir = os.path.join(BUILD_DIR, 'tests/functional', self.arch, - self.id()) + self.outputdir = os.path.join(BUILD_DIR, 'tests', 'functional', + self.arch, self.id())I don't think you need save self.outputdir as only self.logdir is used by the other functions.
I guess it might be useful to have this variable as a reference point in future patches, so I'm in favor of keeping it.
PATCH 3/15 tests/functional: remove "AVOCADO" from env variable name
In reply to Daniel P. BerrangéThis env variable is a debugging flag to save screendumps in the mips64el malta tests.
--- tests/functional/test_mips64el_malta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/functional/test_mips64el_malta.py +1 -1
@@ -159,7 +159,7 @@ def do_test_i6400_framebuffer_logo(self, cpu_cores_count): loc = np.where(result >= match_threshold) tuxlogo_count = 0 h, w = tuxlogo_bgr.shape[:2] - debug_png = os.getenv('AVOCADO_CV2_SCREENDUMP_PNG_PATH') + debug_png = os.getenv('QEMU_TEST_CV2_SCREENDUMP_PNG_PATH') for tuxlogo_count, pt in enumerate(zip(*loc[::-1]), start=1): logger.debug('found Tux at position (x, y) = %s', pt) cv2.rectangle(screendump_bgr, pt,
Signature · 1 line
2.46.0
Re: [PATCH 03/15] tests/functional: remove "AVOCADO" from env variable name
Quoted 4 lines · Daniel P. Berrangé
This env variable is a debugging flag to save screendumps in the mips64el malta tests.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH 03/15] tests/functional: remove "AVOCADO" from env variable name
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
This env variable is a debugging flag to save screendumps in the mips64el malta tests.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/test_mips64el_malta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH 4/15 tests/functional: remove todo wrt avocado.utils.wait_for
In reply to Daniel P. BerrangéWe're not using avocado anymore, so while the TODO item is still relevant, suggesting use of avocado.utils is not.
--- tests/functional/test_m68k_nextcube.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tests/functional/test_m68k_nextcube.py +1 -2
@@ -37,8 +37,7 @@ def check_bootrom_framebuffer(self, screenshot_path): self.vm.launch() self.log.info('VM launched, waiting for display') - # TODO: Use avocado.utils.wait.wait_for to catch the - # 'displaysurface_create 1120x832' trace-event. + # TODO: wait for the 'displaysurface_create 1120x832' trace-event. time.sleep(2) self.vm.cmd('human-monitor-command',
Signature · 1 line
2.46.0
Re: [PATCH 04/15] tests/functional: remove todo wrt avocado.utils.wait_for
Quoted 4 lines · Daniel P. Berrangé
We're not using avocado anymore, so while the TODO item is still relevant, suggesting use of avocado.utils is not.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 5/15 tests/functional: remove leftover :avocado: tags
In reply to Daniel P. BerrangéThese tags are not honoured under the new functional test harness.
--- tests/functional/test_arm_bpim2u.py | 20 -------------------- tests/functional/test_arm_orangepi.py | 27 --------------------------- 2 files changed, 47 deletions(-)
tests/functional/test_arm_bpim2u.py +0 -20
@@ -37,11 +37,6 @@ class BananaPiMachine(LinuxKernelTest): '5b41b4e11423e562c6011640f9a7cd3bdd0a3d42b83430f7caa70a432e6cd82c') def test_arm_bpim2u(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:bpim2u - :avocado: tags=accel:tcg - """ self.set_machine('bpim2u') deb_path = self.ASSET_DEB.fetch() kernel_path = self.extract_from_deb(deb_path,
@@ -64,11 +59,6 @@ def test_arm_bpim2u(self): os.remove(dtb_path) def test_arm_bpim2u_initrd(self): - """ - :avocado: tags=arch:arm - :avocado: tags=accel:tcg - :avocado: tags=machine:bpim2u - """ self.set_machine('bpim2u') deb_path = self.ASSET_DEB.fetch() kernel_path = self.extract_from_deb(deb_path,
@@ -105,11 +95,6 @@ def test_arm_bpim2u_initrd(self): os.remove(initrd_path) def test_arm_bpim2u_gmac(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:bpim2u - :avocado: tags=device:sd - """ self.set_machine('bpim2u') self.require_netdev('user')
@@ -160,11 +145,6 @@ def test_arm_bpim2u_gmac(self): @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') def test_arm_bpim2u_openwrt_22_03_3(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:bpim2u - :avocado: tags=device:sd - """ self.set_machine('bpim2u') # This test download a 8.9 MiB compressed image and expand it # to 127 MiB.
tests/functional/test_arm_orangepi.py +0 -27
@@ -49,11 +49,6 @@ class BananaPiMachine(LinuxKernelTest): '20d3e07dc057e15c12452620e90ecab2047f0f7940d9cba8182ebc795927177f') def test_arm_orangepi(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:orangepi-pc - :avocado: tags=accel:tcg - """ self.set_machine('orangepi-pc') deb_path = self.ASSET_DEB.fetch() kernel_path = self.extract_from_deb(deb_path,
@@ -75,11 +70,6 @@ def test_arm_orangepi(self): os.remove(dtb_path) def test_arm_orangepi_initrd(self): - """ - :avocado: tags=arch:arm - :avocado: tags=accel:tcg - :avocado: tags=machine:orangepi-pc - """ self.set_machine('orangepi-pc') deb_path = self.ASSET_DEB.fetch() kernel_path = self.extract_from_deb(deb_path,
@@ -115,12 +105,6 @@ def test_arm_orangepi_initrd(self): os.remove(initrd_path) def test_arm_orangepi_sd(self): - """ - :avocado: tags=arch:arm - :avocado: tags=accel:tcg - :avocado: tags=machine:orangepi-pc - :avocado: tags=device:sd - """ self.set_machine('orangepi-pc') self.require_netdev('user') deb_path = self.ASSET_DEB.fetch()
@@ -167,11 +151,6 @@ def test_arm_orangepi_sd(self): @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') def test_arm_orangepi_armbian(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:orangepi-pc - :avocado: tags=device:sd - """ self.set_machine('orangepi-pc') # This test download a 275 MiB compressed image and expand it # to 1036 MiB, but the underlying filesystem is 1552 MiB...
@@ -208,12 +187,6 @@ def test_arm_orangepi_armbian(self): @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') def test_arm_orangepi_uboot_netbsd9(self): - """ - :avocado: tags=arch:arm - :avocado: tags=machine:orangepi-pc - :avocado: tags=device:sd - :avocado: tags=os:netbsd - """ self.set_machine('orangepi-pc') # This test download a 304MB compressed image and expand it to 2GB deb_path = self.ASSET_UBOOT.fetch()
Signature · 1 line
2.46.0
Re: [PATCH 05/15] tests/functional: remove leftover :avocado: tags
Quoted 3 lines · Daniel P. Berrangé
These tags are not honoured under the new functional test harness.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 6/15 tests/functional: remove obsolete reference to avocado bug
In reply to Daniel P. BerrangéHistorical bugs in avocado related to zstd support are not relevant to the code now that it uses QEMU's native test harness.
--- tests/functional/qemu_test/tuxruntest.py | 1 - 1 file changed, 1 deletion(-)
tests/functional/qemu_test/tuxruntest.py +0 -1
@@ -39,7 +39,6 @@ def setUp(self): super().setUp() # We need zstd for all the tuxrun tests - # See https://github.com/avocado-framework/avocado/issues/5609 (has_zstd, msg) = has_cmd('zstd') if has_zstd is False: self.skipTest(msg)
Signature · 1 line
2.46.0
Re: [PATCH 06/15] tests/functional: remove obsolete reference to avocado bug
Quoted 4 lines · Daniel P. Berrangé
Historical bugs in avocado related to zstd support are not relevant to the code now that it uses QEMU's native test harness.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 7/15 tests/functional: remove comments talking about avocado
In reply to Daniel P. BerrangéThe first comment is still relevant but should talk about our own test harness instead. The second comment adds no value over reading the code and can be removed.
--- tests/functional/test_acpi_bits.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
tests/functional/test_acpi_bits.py +5 -7
@@ -196,11 +196,11 @@ def copy_test_scripts(self): for filename in os.listdir(bits_test_dir): if os.path.isfile(os.path.join(bits_test_dir, filename)) and \ filename.endswith('.py2'): - # all test scripts are named with extension .py2 so that - # avocado does not try to load them. These scripts are - # written for python 2.7 not python 3 and hence if avocado - # loaded them, it would complain about python 3 specific - # syntaxes. + # all test scripts are named with extension .py2 so that our + # test harness does not try to load them. These scripts are + # written for python 2.7 not python 3 and hence if our test + # harness loaded them, it would complain about python 3 + # specific syntaxes. newfilename = os.path.splitext(filename)[0] + '.py' shutil.copy2(os.path.join(bits_test_dir, filename), os.path.join(target_test_dir, newfilename))
@@ -399,8 +399,6 @@ def test_acpi_smbios_bits(self): # biosbits has been configured to run all the specified test suites # in batch mode and then automatically initiate a vm shutdown. - # Set timeout to BITS_TIMEOUT for SHUTDOWN event from bits VM at par - # with the avocado test timeout. self._vm.event_wait('SHUTDOWN', timeout=BITS_TIMEOUT) self._vm.wait(timeout=None) self.logger.debug("Checking console output ...")
Signature · 1 line
2.46.0
Re: [PATCH 07/15] tests/functional: remove comments talking about avocado
Quoted 14 lines · Daniel P. Berrangé
The first comment is still relevant but should talk about our own test harness instead. The second comment adds no value over reading the code and can be removed.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/test_acpi_bits.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)tests/functional/test_acpi_bits.py +0
-0
tests/functional/test_acpi_bits.py def copy_test_scripts(self): · line 200
Quoted 10 lines
tests/functional/test_acpi_bits.py +2
-5@@ -196,11 +196,11 @@ def copy_test_scripts(self): for filename in os.listdir(bits_test_dir): if os.path.isfile(os.path.join(bits_test_dir, filename)) and \ filename.endswith('.py2'): - # all test scripts are named with extension .py2 so that - # avocado does not try to load them. These scripts are - # written for python 2.7 not python 3 and hence if avocado - # loaded them, it would complain about python 3 specific - # syntaxes. + # all test scripts are named with extension .py2 so that our + # test harness does not try to load them. These scripts are
Since our test framework does not try to scan for *.py files automatically (but the files have to be added via meson.build instead), I'd maybe rather say:
"all test scripts are named with extension .py2 so that they are not run by accident"
or something similar?
Quoted 15 lines
tests/functional/test_acpi_bits.py +3
-2+ # written for python 2.7 not python 3 and hence if our test + # harness loaded them, it would complain about python 3 + # specific syntaxes. newfilename = os.path.splitext(filename)[0] + '.py' shutil.copy2(os.path.join(bits_test_dir, filename), os.path.join(target_test_dir, newfilename))@@ -399,8 +399,6 @@ def test_acpi_smbios_bits(self): # biosbits has been configured to run all the specified test suites # in batch mode and then automatically initiate a vm shutdown. - # Set timeout to BITS_TIMEOUT for SHUTDOWN event from bits VM at par - # with the avocado test timeout. self._vm.event_wait('SHUTDOWN', timeout=BITS_TIMEOUT) self._vm.wait(timeout=None) self.logger.debug("Checking console output ...")
PATCH 8/15 tests/functional: honour self.workdir in ACPI bits tests
In reply to Daniel P. BerrangéThe ACPI bits test sets up its own private temporary directory into it creates scratch files. This is justified by a suggestion that we need to be able to preserve the scratch files. We have the ability to preserve the scratch dir with our functional harness, so there's no reason to diverge from standard practice in file placement.
--- tests/functional/test_acpi_bits.py | 44 +++++++++--------------------- 1 file changed, 13 insertions(+), 31 deletions(-)
tests/functional/test_acpi_bits.py +13 -31
@@ -150,7 +150,6 @@ class AcpiBitsTest(QemuBaseTest): #pylint: disable=too-many-instance-attributes def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._vm = None - self._workDir = None self._baseDir = None self._debugcon_addr = '0x403'
@@ -169,7 +168,7 @@ def copy_bits_config(self): config_file = 'bits-cfg.txt' bits_config_dir = os.path.join(self._baseDir, 'acpi-bits', 'bits-config') - target_config_dir = os.path.join(self._workDir, + target_config_dir = os.path.join(self.workdir, 'bits-%d' %self.BITS_INTERNAL_VER, 'boot') self.assertTrue(os.path.exists(bits_config_dir))
@@ -186,7 +185,7 @@ def copy_test_scripts(self): bits_test_dir = os.path.join(self._baseDir, 'acpi-bits', 'bits-tests') - target_test_dir = os.path.join(self._workDir, + target_test_dir = os.path.join(self.workdir, 'bits-%d' %self.BITS_INTERNAL_VER, 'boot', 'python')
@@ -224,8 +223,8 @@ def fix_mkrescue(self, mkrescue): the directory where we have extracted our pre-built bits grub tarball. """ - grub_x86_64_mods = os.path.join(self._workDir, 'grub-inst-x86_64-efi') - grub_i386_mods = os.path.join(self._workDir, 'grub-inst') + grub_x86_64_mods = os.path.join(self.workdir, 'grub-inst-x86_64-efi') + grub_i386_mods = os.path.join(self.workdir, 'grub-inst') self.assertTrue(os.path.exists(grub_x86_64_mods)) self.assertTrue(os.path.exists(grub_i386_mods))
@@ -246,11 +245,11 @@ def generate_bits_iso(self): """ Uses grub-mkrescue to generate a fresh bits iso with the python test scripts """ - bits_dir = os.path.join(self._workDir, + bits_dir = os.path.join(self.workdir, 'bits-%d' %self.BITS_INTERNAL_VER) - iso_file = os.path.join(self._workDir, + iso_file = os.path.join(self.workdir, 'bits-%d.iso' %self.BITS_INTERNAL_VER) - mkrescue_script = os.path.join(self._workDir, + mkrescue_script = os.path.join(self.workdir, 'grub-inst-x86_64-efi', 'bin', 'grub-mkrescue')
@@ -289,17 +288,7 @@ def setUp(self): # pylint: disable=arguments-differ self._baseDir = Path(__file__).parent - # workdir could also be avocado's own workdir in self.workdir. - # At present, I prefer to maintain my own temporary working - # directory. It gives us more control over the generated bits - # log files and also for debugging, we may chose not to remove - # this working directory so that the logs and iso can be - # inspected manually and archived if needed. - self._workDir = tempfile.mkdtemp(prefix='acpi-bits-', - suffix='.tmp') - self.logger.info('working dir: %s', self._workDir) - - prebuiltDir = os.path.join(self._workDir, 'prebuilt') + prebuiltDir = os.path.join(self.workdir, 'prebuilt') if not os.path.isdir(prebuiltDir): os.mkdir(prebuiltDir, mode=0o775)
@@ -320,10 +309,10 @@ def setUp(self): # pylint: disable=arguments-differ # extract the bits software in the temp working directory with zipfile.ZipFile(bits_zip_file, 'r') as zref: - zref.extractall(self._workDir) + zref.extractall(self.workdir) with tarfile.open(grub_tar_file, 'r', encoding='utf-8') as tarball: - tarball.extractall(self._workDir) + tarball.extractall(self.workdir) self.copy_test_scripts() self.copy_bits_config()
@@ -333,7 +322,7 @@ def parse_log(self): """parse the log generated by running bits tests and check for failures. """ - debugconf = os.path.join(self._workDir, self._debugcon_log) + debugconf = os.path.join(self.workdir, self._debugcon_log) log = "" with open(debugconf, 'r', encoding='utf-8') as filehandle: log = filehandle.read()
@@ -359,25 +348,18 @@ def tearDown(self): """ if self._vm: self.assertFalse(not self._vm.is_running) - if not os.getenv('BITS_DEBUG') and self._workDir: - self.logger.info('removing the work directory %s', self._workDir) - shutil.rmtree(self._workDir) - else: - self.logger.info('not removing the work directory %s ' \ - 'as BITS_DEBUG is ' \ - 'passed in the environment', self._workDir) super().tearDown() def test_acpi_smbios_bits(self): """The main test case implementation.""" - iso_file = os.path.join(self._workDir, + iso_file = os.path.join(self.workdir, 'bits-%d.iso' %self.BITS_INTERNAL_VER) self.assertTrue(os.access(iso_file, os.R_OK)) self._vm = QEMUBitsMachine(binary=self.qemu_bin, - base_temp_dir=self._workDir, + base_temp_dir=self.workdir, debugcon_log=self._debugcon_log, debugcon_addr=self._debugcon_addr)
Signature · 1 line
2.46.0
Re: [PATCH 08/15] tests/functional: honour self.workdir in ACPI bits tests
Quoted 7 lines · Daniel P. Berrangé
The ACPI bits test sets up its own private temporary directory into it creates scratch files. This is justified by a suggestion that we need to be able to preserve the scratch files. We have the ability to preserve the scratch dir with our functional harness, so there's no reason to diverge from standard practice in file placement.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 9/15 tests/functional: put QEMUMachine logs in testcase log directory
In reply to Daniel P. BerrangéWe are not passing the 'log_dir' parameter to QEMUMachine, so the QEMU stdout/err logs are being placed in a temp directory and thus deleted after execution. This makes them inaccessible as gitlab CI artifacts.
Pass the testcase log directory path into QEMUMachine to make the logs persistent.
--- tests/functional/qemu_test/testcase.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tests/functional/qemu_test/testcase.py +3 -2
@@ -163,10 +163,11 @@ def require_device(self, devicename): self.skipTest('no support for device ' + devicename) def _new_vm(self, name, *args): - vm = QEMUMachine(self.qemu_bin, base_temp_dir=self.workdir) + vm = QEMUMachine(self.qemu_bin, + base_temp_dir=self.workdir, + log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) - self.log.debug('QEMUMachine "%s" log_dir: %s', name, vm.log_dir) if args: vm.add_args(*args) return vm
Signature · 1 line
2.46.0
Re: [PATCH 09/15] tests/functional: put QEMUMachine logs in testcase log directory
Quoted 9 lines · Daniel P. Berrangé
We are not passing the '
log_dir' parameter to QEMUMachine, so the QEMU stdout/err logs are being placed in a temp directory and thus deleted after execution. This makes them inaccessible as gitlab CI artifacts.Pass the testcase log directory path into QEMUMachine to make the logs persistent.
Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 10/15 tests/functional: honour requested test VM name in QEMUMachine
In reply to Daniel P. BerrangéThe functional test case class is going to the trouble of passing around a machine name, but then fails to give this QEMUMachine. As a result, QEMUMachine will create a completely random name. Since log file names match the machine name, this results in log files accumulating over time.
--- tests/functional/qemu_test/testcase.py | 1 + 1 file changed, 1 insertion(+)
tests/functional/qemu_test/testcase.py +1 -0
@@ -164,6 +164,7 @@ def require_device(self, devicename): def _new_vm(self, name, *args): vm = QEMUMachine(self.qemu_bin, + name=name, base_temp_dir=self.workdir, log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name)
Signature · 1 line
2.46.0
Re: [PATCH 10/15] tests/functional: honour requested test VM name in QEMUMachine
Quoted 16 lines · Daniel P. Berrangé
The functional test case class is going to the trouble of passing around a machine name, but then fails to give this QEMUMachine. As a result, QEMUMachine will create a completely random name. Since log file names match the machine name, this results in log files accumulating over time.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/qemu_test/testcase.py | 1 + 1 file changed, 1 insertion(+)tests/functional/qemu_test/testcase.py +0
-0
tests/functional/qemu_test/testcase.py def require_device(self, devicename): · line 170
Quoted 7 lines
tests/functional/qemu_test/testcase.py +1
-0@@ -164,6 +164,7 @@ def require_device(self, devicename): def _new_vm(self, name, *args): vm = QEMUMachine(self.qemu_bin, + name=name, base_temp_dir=self.workdir, log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name)
PATCH 11/15 tests/functional: enable debug logging for QEMUMachine
In reply to Daniel P. BerrangéSet the 'qemu.machine' logger to 'DEBUG' level, to ensure we see log messages related to the QEMUMachine class. Most importantly this ensures we capture the full QEMU command line args for instances we spawn.
--- tests/functional/qemu_test/testcase.py | 6 ++++++ 1 file changed, 6 insertions(+)
tests/functional/qemu_test/testcase.py +6 -0
@@ -57,9 +57,15 @@ def setUp(self, bin_prefix): self._log_fh.setFormatter(fileFormatter) self.log.addHandler(self._log_fh) + # Capture QEMUMachine logging + self.machinelog = logging.getLogger('qemu.machine') + self.machinelog.setLevel(logging.DEBUG) + self.machinelog.addHandler(self._log_fh) + def tearDown(self): if "QEMU_TEST_KEEP_SCRATCH" not in os.environ: shutil.rmtree(self.workdir) + self.machinelog.removeHandler(self._log_fh) self.log.removeHandler(self._log_fh) def main():
Signature · 1 line
2.46.0
Re: [PATCH 11/15] tests/functional: enable debug logging for QEMUMachine
Quoted 20 lines · Daniel P. Berrangé
Set the 'qemu.machine' logger to 'DEBUG' level, to ensure we see log messages related to the QEMUMachine class. Most importantly this ensures we capture the full QEMU command line args for instances we spawn.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/qemu_test/testcase.py | 6 ++++++ 1 file changed, 6 insertions(+)tests/functional/qemu_test/testcase.py +2
-0@@ -57,9 +57,15 @@ def setUp(self, bin_prefix): self._log_fh.setFormatter(fileFormatter) self.log.addHandler(self._log_fh) + # Capture QEMUMachine logging + self.machinelog = logging.getLogger('qemu.machine')
tests/functional/qemu_test/testcase.py def setUp(self, bin_prefix): · line 71
Quoted 10 lines
tests/functional/qemu_test/testcase.py +4
-0+ self.machinelog.setLevel(logging.DEBUG) + self.machinelog.addHandler(self._log_fh) + def tearDown(self): if "QEMU_TEST_KEEP_SCRATCH" not in os.environ: shutil.rmtree(self.workdir) + self.machinelog.removeHandler(self._log_fh) self.log.removeHandler(self._log_fh) def main():
That looks better in the logs, indeed!
PATCH 12/15 tests/functional: logs details of console interaction operations
In reply to Daniel P. BerrangéWhen functional tests go wrong, it will often be related to the console interaction wait state. By logging the messages that we're looking for, and data we're about to be sending, it'll be easier to diagnose where tests are getting stuck.
--- tests/functional/qemu_test/cmd.py | 2 ++ 1 file changed, 2 insertions(+)
tests/functional/qemu_test/cmd.py +2 -0
@@ -85,6 +85,8 @@ def _console_interaction(test, success_message, failure_message, vm = test.vm console = vm.console_file console_logger = logging.getLogger('console') + test.log.debug(f"Console interaction success:'{success_message}' " + + f"failure:'{failure_message}' send:'{send_string}'") while True: if send_string: vm.console_socket.sendall(send_string.encode())
Signature · 1 line
2.46.0
Re: [PATCH 12/15] tests/functional: logs details of console interaction operations
Quoted 15 lines · Daniel P. Berrangé
When functional tests go wrong, it will often be related to the console interaction wait state. By logging the messages that we're looking for, and data we're about to be sending, it'll be easier to diagnose where tests are getting stuck.
Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/qemu_test/cmd.py | 2 ++ 1 file changed, 2 insertions(+)tests/functional/qemu_test/cmd.py +0
-0
tests/functional/qemu_test/cmd.py def _console_interaction(test, success_message, failure_message, · line 88
Quoted 4 lines
tests/functional/qemu_test/cmd.py +1
-0@@ -85,6 +85,8 @@ def _console_interaction(test, success_message, failure_message, vm = test.vm console = vm.console_file console_logger = logging.getLogger('console') + test.log.debug(f"Console interaction success:'{success_message}' " +
I have to admit that I had to read the message twice to really understand it. Since the message starts with "Console interaction success", I thought that this message means that we successfully matched a string already.
So maybe rather change the text to read:
Console interaction: success_msg=... failure_msg=...
?
Thomas
PATCH 13/15 tests/functional: rewrite console handling to be bytewise
In reply to Daniel P. BerrangéThe console interaction that waits for predicted strings uses readline(), and thus is only capable of waiting for strings that are followed by a newline.
This is inconvenient when needing to match on some things, particularly login prompts, or shell prompts, causing tests to use time.sleep(...) instead, which is unreliable.
Switch to reading the console 1 byte at a time, comparing against the success/failure messages until we see a match, regardless of whether a newline is encountered.
The success/failure comparisons are done with the python bytes type, rather than strings, to avoid the problem of needing to decode partially received multibyte utf8 characters.
Heavily inspired by a patch proposed by Cédric, but written again to work in bytes, rather than strings.
--- tests/functional/qemu_test/cmd.py | 63 +++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 15 deletions(-)
tests/functional/qemu_test/cmd.py +48 -15
@@ -78,15 +78,58 @@ def run_cmd(args): def is_readable_executable_file(path): return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK) +def _console_readline(test, vm, success, failure): + msg = bytes([]) + done = False + while True: + c = vm.console_socket.recv(1) + if c is None: + done = True + test.fail( + f"EOF in console, expected '{success}'") + break + msg += c + + if success is None or success in msg: + done = True + break + if failure and failure in msg: + done = True + vm.console_socket.close() + test.fail( + f"'{failure}' found in console, expected '{success}'") + + if c == b'\n': + break + + console_logger = logging.getLogger('console') + try: + console_logger.debug(msg.decode().strip()) + except: + console_logger.debug(msg) + + return done + def _console_interaction(test, success_message, failure_message, send_string, keep_sending=False, vm=None): assert not keep_sending or send_string if vm is None: vm = test.vm - console = vm.console_file - console_logger = logging.getLogger('console') + test.log.debug(f"Console interaction success:'{success_message}' " + f"failure:'{failure_message}' send:'{send_string}'") + + # We'll process console in bytes, to avoid having to + # deal with unicode decode errors from receiving + # partial utf8 byte sequences + success_message_b = None + if success_message is not None: + success_message_b = success_message.encode() + + failure_message_b = None + if failure_message is not None: + failure_message_b = failure_message.encode() + while True: if send_string: vm.console_socket.sendall(send_string.encode())
@@ -99,20 +142,10 @@ def _console_interaction(test, success_message, failure_message, break continue - try: - msg = console.readline().decode().strip() - except UnicodeDecodeError: - msg = None - if not msg: - continue - console_logger.debug(msg) - if success_message is None or success_message in msg: + if _console_readline(test, vm, + success_message_b, + failure_message_b): break - if failure_message and failure_message in msg: - console.close() - fail = 'Failure message found in console: "%s". Expected: "%s"' % \ - (failure_message, success_message) - test.fail(fail) def interrupt_interactive_console_until_pattern(test, success_message, failure_message=None,
Signature · 1 line
2.46.0
Re: [PATCH 13/15] tests/functional: rewrite console handling to be bytewise
Quoted 36 lines · Daniel P. Berrangé
The console interaction that waits for predicted strings uses
readline(), and thus is only capable of waiting for strings that are followed by a newline.This is inconvenient when needing to match on some things, particularly login prompts, or shell prompts, causing tests to use
time.sleep(...)instead, which is unreliable.Switch to reading the console 1 byte at a time, comparing against the success/failure messages until we see a match, regardless of whether a newline is encountered.
The success/failure comparisons are done with the python bytes type, rather than strings, to avoid the problem of needing to decode partially received multibyte utf8 characters.
Heavily inspired by a patch proposed by Cédric, but written again to work in bytes, rather than strings.
Co-developed-by Cédric Le Goater <[email protected]>Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/qemu_test/cmd.py | 63 +++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 15 deletions(-)tests/functional/qemu_test/cmd.py +3
-0@@ -78,15 +78,58 @@ def run_cmd(args): def is_readable_executable_file(path): return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK) +def _console_readline(test, vm, success, failure): + msg = bytes([]) + done = False
tests/functional/qemu_test/cmd.py def run_cmd(args): · line 93
Quoted 10 lines
tests/functional/qemu_test/cmd.py +10
-0+ while True: + c = vm.console_socket.recv(1) + if c is None: + done = True + test.fail( + f"EOF in console, expected '{success}'") + break + msg += c + + if success is None or success in msg:
As an optimization, you could use msg.endswith(success) and msg.endswith(failure), which would avoid the most blatant cases of O(n^2) behavior.
More important, I think "if success is None" should not be here, because it will exit after one char. Instead...
tests/functional/qemu_test/cmd.py def run_cmd(args): · line 102
Quoted 9 lines
tests/functional/qemu_test/cmd.py +9
-0+ done = True + break + if failure and failure in msg: + done = True + vm.console_socket.close() + test.fail( + f"'{failure}' found in console, expected '{success}'") + + if c == b'\n':
Here you can put
done = success is None
Paolo
Quoted 59 lines
tests/functional/qemu_test/cmd.py +26
-15+ break + + console_logger = logging.getLogger('console') + try: + console_logger.debug(msg.decode().strip()) + except: + console_logger.debug(msg) + + return done + def _console_interaction(test, success_message, failure_message, send_string, keep_sending=False, vm=None): assert not keep_sending or send_string if vm is None: vm = test.vm - console = vm.console_file - console_logger = logging.getLogger('console') + test.log.debug(f"Console interaction success:'{success_message}' " + f"failure:'{failure_message}' send:'{send_string}'") + + # We'll process console in bytes, to avoid having to + # deal with unicode decode errors from receiving + # partial utf8 byte sequences + success_message_b = None + if success_message is not None: + success_message_b = success_message.encode() + + failure_message_b = None + if failure_message is not None: + failure_message_b = failure_message.encode() + while True: if send_string: vm.console_socket.sendall(send_string.encode())@@ -99,20 +142,10 @@ def _console_interaction(test, success_message, failure_message, break continue - try: - msg = console.readline().decode().strip() - except UnicodeDecodeError: - msg = None - if not msg: - continue - console_logger.debug(msg) - if success_message is None or success_message in msg: + if _console_readline(test, vm, + success_message_b, + failure_message_b): break - if failure_message and failure_message in msg: - console.close() - fail = 'Failure message found in console: "%s". Expected: "%s"' % \ - (failure_message, success_message) - test.fail(fail) def interrupt_interactive_console_until_pattern(test, success_message, failure_message=None,
Re: [PATCH 13/15] tests/functional: rewrite console handling to be bytewise
Quoted 64 lines · Paolo Bonzini
Quoted 35 lines · Daniel P. Berrangé
The console interaction that waits for predicted strings uses
readline(), and thus is only capable of waiting for strings that are followed by a newline.This is inconvenient when needing to match on some things, particularly login prompts, or shell prompts, causing tests to use
time.sleep(...)instead, which is unreliable.Switch to reading the console 1 byte at a time, comparing against the success/failure messages until we see a match, regardless of whether a newline is encountered.
The success/failure comparisons are done with the python bytes type, rather than strings, to avoid the problem of needing to decode partially received multibyte utf8 characters.
Heavily inspired by a patch proposed by Cédric, but written again to work in bytes, rather than strings.
Co-developed-by Cédric Le Goater <[email protected]>Signed-off-by Daniel P. Berrangé <[email protected]>--- tests/functional/qemu_test/cmd.py | 63 +++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 15 deletions(-)tests/functional/qemu_test/cmd.py +3
-0@@ -78,15 +78,58 @@ def run_cmd(args): def is_readable_executable_file(path): return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK) +def _console_readline(test, vm, success, failure): + msg = bytes([]) + done = Falsetests/functional/qemu_test/cmd.py def run_cmd(args): · line 92
Quoted 10 lines
tests/functional/qemu_test/cmd.py +10
-0+ while True: + c = vm.console_socket.recv(1) + if c is None: + done = True + test.fail( + f"EOF in console, expected '{success}'") + break + msg += c + + if success is None or success in msg:As an optimization, you could use
msg.endswith(success)andmsg.endswith(failure), which would avoid the most blatant cases of O(n^2) behavior.More important, I think "if success is None" should not be here, because it will exit after one char. Instead...
tests/functional/qemu_test/cmd.py def run_cmd(args): · line 102
Quoted 9 lines
tests/functional/qemu_test/cmd.py +9
-0+ done = True + break + if failure and failure in msg: + done = True + vm.console_socket.close() + test.fail( + f"'{failure}' found in console, expected '{success}'") + + if c == b'\n':
Quoted 3 lines
Here you can put
done = success is None
Hmmm, this can only be a problem if "success" is None, and "failure" is not None, and although the old code would technically work in that case, I think it is actually an unknown/invalid usage scenario.
If BOTH "success" and "failure" are None, this method won't be called at all. It is valid for "failure" to be none, but I don't think it makes semantic sense for "success" to also be None, while have "failure" be non-None.
So I'm inclined to say we declare 'success' to be mandatory and validate that in the caller. eg
assert send_string is not None or success_message is not None
and then remove this "success is None" check from _console_readline.
Quoted 52 lines
Paolo
Quoted 49 lines
tests/functional/qemu_test/cmd.py +26
-10+ break + + console_logger = logging.getLogger('console') + try: + console_logger.debug(msg.decode().strip()) + except: + console_logger.debug(msg) + + return done + def _console_interaction(test, success_message, failure_message, send_string, keep_sending=False, vm=None): assert not keep_sending or send_string if vm is None: vm = test.vm - console = vm.console_file - console_logger = logging.getLogger('console') + test.log.debug(f"Console interaction success:'{success_message}' " + f"failure:'{failure_message}' send:'{send_string}'") + + # We'll process console in bytes, to avoid having to + # deal with unicode decode errors from receiving + # partial utf8 byte sequences + success_message_b = None + if success_message is not None: + success_message_b = success_message.encode() + + failure_message_b = None + if failure_message is not None: + failure_message_b = failure_message.encode() + while True: if send_string: vm.console_socket.sendall(send_string.encode())@@ -99,20 +142,10 @@ def _console_interaction(test, success_message, failure_message, break continue - try: - msg = console.readline().decode().strip() - except UnicodeDecodeError: - msg = None - if not msg: - continue - console_logger.debug(msg) - if success_message is None or success_message in msg: + if _console_readline(test, vm, + success_message_b, + failure_message_b):
Quoted 9 lines
tests/functional/qemu_test/cmd.py def _console_interaction(test, success_message, failure_message, · line 151
Quoted 8 lines
tests/functional/qemu_test/cmd.py +0
-5break - if failure_message and failure_message in msg: - console.close() - fail = 'Failure message found in console: "%s". Expected: "%s"' % \ - (failure_message, success_message) - test.fail(fail) def interrupt_interactive_console_until_pattern(test, success_message, failure_message=None,
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 13/15] tests/functional: rewrite console handling to be bytewise
Quoted 28 lines · Daniel P. Berrangé
Quoted 19 lines
Quoted 1 line
+ if success is None or success in msg:
As an optimization, you could use
msg.endswith(success)andmsg.endswith(failure), which would avoid the most blatant cases of O(n^2) behavior.More important, I think "if success is None" should not be here, because it will exit after one char. Instead...
tests/functional/qemu_test/cmd.py def run_cmd(args): · line 102
Quoted 9 lines
tests/functional/qemu_test/cmd.py +9
-0+ done = True + break + if failure and failure in msg: + done = True + vm.console_socket.close() + test.fail( + f"'{failure}' found in console, expected '{success}'") + + if c == b'\n':Quoted 3 lines
Here you can put
done = success is None
Hmmm, this can only be a problem if "success" is None, and "failure" is not None, and although the old code would technically work in that case, I think it is actually an unknown/invalid usage scenario.
Quoted 4 lines
If BOTH "success" and "failure" are None, this method won't be called at all. It is valid for "failure" to be none, but I don't think it makes semantic sense for "success" to also be None, while have "failure" be non-None.
"Read a line and check that it doesn't contain a substring" seemed like a plausible thing to test for, but you're right it doesn't make much sense in this context. It would make more sense if _console_readline returned the full line, at which point it would be a completely different function and probably not underscore-prefixed.
So yeah, this is okay:
Quoted 1 line
assert
send_stringis not None orsuccess_messageis not None
whether done in the caller or in _console_readline itself, as you prefer.
Paolo
PATCH 14/15 tests/functional: remove time.sleep usage from tuxrun tests
In reply to Daniel P. BerrangéThe tuxrun tests send a series of strings to the guest to login and then run commands. Since we have been unable to match on console output that isn't followed by a newline, the test used many time.sleep() statements to pretend to synchronize with the guest.
This has proved to be unreliable for the aarch64be instance of the tuxrun tests, with the test often hanging. The hang is a very subtle timing problem, and it is suspected that some (otherwise apparently harmless) I/O error messages could be resulting in full FIFO buffers, stalling interaction with the guest.
With the newly rewritten console interaction able to match strings that don't have a following newline, the tux run tests can now match directly on the login prompt, and/or shell PS1 prompt.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2689
--- tests/functional/qemu_test/tuxruntest.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
tests/functional/qemu_test/tuxruntest.py +6 -10
@@ -124,16 +124,12 @@ def run_tuxtest_tests(self, haltmsg): then do a few things on the console. Trigger a shutdown and wait to exit cleanly. """ - self.wait_for_console_pattern("Welcome to TuxTest") - time.sleep(0.2) - exec_command(self, 'root') - time.sleep(0.2) - exec_command(self, 'cat /proc/interrupts') - time.sleep(0.1) - exec_command(self, 'cat /proc/self/maps') - time.sleep(0.1) - exec_command(self, 'uname -a') - time.sleep(0.1) + ps1='root@tuxtest:~#' + self.wait_for_console_pattern('tuxtest login:') + exec_command_and_wait_for_pattern(self, 'root', ps1) + exec_command_and_wait_for_pattern(self, 'cat /proc/interrupts', ps1) + exec_command_and_wait_for_pattern(self, 'cat /proc/self/maps', ps1) + exec_command_and_wait_for_pattern(self, 'uname -a', ps1) exec_command_and_wait_for_pattern(self, 'halt', haltmsg) # Wait for VM to shut down gracefully if it can
Signature · 1 line
2.46.0
PATCH 15/15 tests/functional: add a QMP backdoor for debugging stalled tests
In reply to Daniel P. BerrangéSupport the QEMU_TEST_QMP_BACKDOOR=backdoor.sock env variable as a way to get a QMP backdoor for debugging a stalled QEMU test. Most typically this would be used if running the tests directly:
$ QEMU_TEST_QMP_BACKDOOR=backdoor.sock \ QEMU_TEST_QEMU_BINARY=./build/qemu-system-arm \ PYTHONPATH=./python \ ./tests/functional/test_arm_tuxrun.py
And then, when the test stalls, in a second shell run:
$ ./scripts/qmp/qmp-shell backdoor.sock
--- docs/devel/testing/functional.rst | 10 ++++++++++ tests/functional/qemu_test/testcase.py | 7 +++++++ 2 files changed, 17 insertions(+)
docs/devel/testing/functional.rst +10 -0
@@ -176,6 +176,16 @@ primarily depend on the value of the ``qemu_bin`` class attribute. If it is not explicitly set by the test code, its default value will be the result the QEMU_TEST_QEMU_BINARY environment variable. +Debugging hung QEMU +^^^^^^^^^^^^^^^^^^^ + +When test cases go wrong it may be helpful to debug a stalled QEMU +process. While the QEMUMachine class owns the primary QMP monitor +socket, it is possible to request a second QMP monitor be created +by setting the ``QEMU_TEST_QMP_BACKDOOR`` env variable to refer +to a UNIX socket name. The ``qmp-shell`` command can then be +attached to the stalled QEMU to examine its live state. + Attribute reference -------------------
tests/functional/qemu_test/testcase.py +7 -0
@@ -175,6 +175,13 @@ def _new_vm(self, name, *args): log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) + + if "QEMU_TEST_QMP_BACKDOOR" in os.environ: + path = os.environ["QEMU_TEST_QMP_BACKDOOR"] + vm.add_args("-chardev", + f"socket,id=backdoor,path={path},server=on,wait=off", + "-mon", "chardev=backdoor,mode=control") + if args: vm.add_args(*args) return vm
Signature · 1 line
2.46.0
Re: [PATCH 15/15] tests/functional: add a QMP backdoor for debugging stalled tests
Quoted 45 lines · Daniel P. Berrangé
Support the QEMU_TEST_QMP_BACKDOOR=backdoor.sock env variable as a way to get a QMP backdoor for debugging a stalled QEMU test. Most typically this would be used if running the tests directly:
$ QEMU_TEST_QMP_BACKDOOR=backdoor.sock \ QEMU_TEST_QEMU_BINARY=./build/qemu-system-arm \ PYTHONPATH=./python \ ./tests/functional/test_arm_tuxrun.pyAnd then, when the test stalls, in a second shell run:
$ ./scripts/qmp/qmp-shell backdoor.sockSigned-off-by Daniel P. Berrangé <[email protected]>--- docs/devel/testing/functional.rst | 10 ++++++++++ tests/functional/qemu_test/testcase.py | 7 +++++++ 2 files changed, 17 insertions(+)docs/devel/testing/functional.rst +10
-0@@ -176,6 +176,16 @@ primarily depend on the value of the ``qemu_bin`` class attribute. If it is not explicitly set by the test code, its default value will be the result the QEMU_TEST_QEMU_BINARY environment variable. +Debugging hung QEMU +^^^^^^^^^^^^^^^^^^^ + +When test cases go wrong it may be helpful to debug a stalled QEMU +process. While the QEMUMachine class owns the primary QMP monitor +socket, it is possible to request a second QMP monitor be created +by setting the ``QEMU_TEST_QMP_BACKDOOR`` env variable to refer +to a UNIX socket name. The ``qmp-shell`` command can then be +attached to the stalled QEMU to examine its live state. + Attribute reference -------------------tests/functional/qemu_test/testcase.py +0
-0
tests/functional/qemu_test/testcase.py def _new_vm(self, name, *args): · line 180
Quoted 6 lines
tests/functional/qemu_test/testcase.py +3
-0@@ -175,6 +175,13 @@ def _new_vm(self, name, *args): log_dir=self.logdir) self.log.debug('QEMUMachine "%s" created', name) self.log.debug('QEMUMachine "%s" temp_dir: %s', name, vm.temp_dir) + + if "QEMU_TEST_QMP_BACKDOOR" in os.environ: + path = os.environ["QEMU_TEST_QMP_BACKDOOR"]
Maybe slightly nicer to chech os.environ only once (and specify the environment variable name only once):
path = os.environ.get('QEMU_TEST_QMP_BACKDOOR') if path:
...
?
Thomas
Quoted 7 lines
tests/functional/qemu_test/testcase.py +4
-0+ vm.add_args("-chardev", + f"socket,id=backdoor,path={path},server=on,wait=off", + "-mon", "chardev=backdoor,mode=control") + if args: vm.add_args(*args) return vm
Quoted 17 lines · Daniel P. Berrangé
This started out as a series to get rid of the many GBs of temp files the functional tests leave behind. Then it expanded into improving the functional test debugging by ensuring we preserve the QEMU stdout/stderr log file created by the QEMUMachine class. In the course of doing that I encountered some other minor points worth fixing, and then got side tracked into looking at the tuxrun hangs with aarch64be. Investigating the latter exposed some further holes in the debugging story prompting yet more patches, as well as a final solution for tuxrun. So this series does:
* Purge all scratch files created by tests
* Preserve the stdout/stderr log file
* Capture debug log messages on QEMUMachine
* Provide a QMP backdoor for debugging stuck QEMUs
* Enhance console handling for partial line matches
* Fix the tuxrun tests by eliminating sleeps
Quoted 3 lines
There's quite alot of code here, but at the same time it feels like the kind of stuff that'll be valuable either in the 9.2 release, or in the soon to exist 9.2 stable branch.
I gave the series a try on a RHEL9 host. I opened one new issue [1] which seems related to QEMU modelling and not the test.
On top of that, I included the aspeed conversion patches from :
https://lore.kernel.org/all/[email protected]/
Thanks,
C.
[1] https://gitlab.com/qemu-project/qemu/-/issues/2691
Quoted 32 lines
NB, with this series applied Thomas' tuxrun conversion to functional testing survives 200 iterations on my machine, whereas it would reliably hang in < 20, and often in < 10, before.
Daniel P. Berrangé (15):
tests/functional: fix mips64el test to honour workdir tests/functional: automatically clean up scratch files after tests tests/functional: remove "AVOCADO" from env variable name tests/functional: remove todo wrt avocado.utils.wait_for tests/functional: remove leftover :avocado: tags tests/functional: remove obsolete reference to avocado bug tests/functional: remove comments talking about avocado tests/functional: honour self.workdir in ACPI bits tests tests/functional: put QEMUMachine logs in testcase log directory tests/functional: honour requested test VM name in QEMUMachine tests/functional: enable debug logging for QEMUMachine tests/functional: logs details of console interaction operations tests/functional: rewrite console handling to be bytewise tests/functional: remove time.sleep usage from tuxrun tests tests/functional: add a QMP backdoor for debugging stalled testsdocs/devel/testing/functional.rst | 16 ++++++ tests/functional/qemu_test/cmd.py | 65 ++++++++++++++++++------ tests/functional/qemu_test/testcase.py | 33 +++++++++--- tests/functional/qemu_test/tuxruntest.py | 17 +++---- tests/functional/test_acpi_bits.py | 56 +++++++------------- tests/functional/test_arm_bpim2u.py | 20 -------- tests/functional/test_arm_orangepi.py | 27 ---------- tests/functional/test_m68k_nextcube.py | 3 +- tests/functional/test_mips64el_malta.py | 4 +- 9 files changed, 119 insertions(+), 122 deletions(-)