iotests: update environment and linting configuration
cover iotests: update environment and linting configuration
GitLab: https://gitlab.com/jsnow/qemu/-/commits/python-package-iotest-pt1
CI: https://gitlab.com/jsnow/qemu/-/pipelines/375630185
This series partially supersedes:
[PATCH v3 00/16] python/iotests: Run iotest linters during Python CI'
Howdy, this is good stuff we want even if we aren't yet in agreement about the best way to run iotest 297 from CI.
- Update linting config to tolerate pylint 2.11.1
- Eliminate sys.path hacking in individual test files
- make mypy execution in test 297 faster
The rest of the actual "run at CI time" stuff can get handled separately and later pending some discussion on the other series.
--js
John Snow (6):
iotests: add 'qemu' package location to PYTHONPATH in testenv
iotests: add warning for rogue 'qemu' packages iotests/linters: check mypy files all at once iotests/mirror-top-perms: Adjust imports iotests/migrate-bitmaps-test: delint
iotests: Update for pylint 2.11.1
tests/qemu-iotests/235 | 2 - tests/qemu-iotests/297 | 50 ++++++++----------- tests/qemu-iotests/300 | 7 ++- tests/qemu-iotests/iotests.py | 2 - tests/qemu-iotests/pylintrc | 6 ++- tests/qemu-iotests/testenv.py | 35 ++++++++++--- tests/qemu-iotests/testrunner.py | 7 +-- tests/qemu-iotests/tests/migrate-bitmaps-test | 50 +++++++++++-------- tests/qemu-iotests/tests/mirror-top-perms | 12 ++--- 9 files changed, 95 insertions(+), 76 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/6 iotests: add 'qemu' package location to PYTHONPATH in testenv
We can drop the sys.path hacking in various places by doing this. Additionally, by doing it in one place right up top, we can print interesting warnings in case the environment does not look correct.
If we ever decide to change how the environment is crafted, all of the "help me find my python packages" goop is all in one place, right in one function.
--- tests/qemu-iotests/235 | 2 -- tests/qemu-iotests/297 | 6 ------ tests/qemu-iotests/300 | 7 +++---- tests/qemu-iotests/iotests.py | 2 -- tests/qemu-iotests/testenv.py | 14 +++++++++----- tests/qemu-iotests/tests/mirror-top-perms | 7 +++---- 6 files changed, 15 insertions(+), 23 deletions(-)
tests/qemu-iotests/235 +0 -2
@@ -24,8 +24,6 @@ import os import iotests from iotests import qemu_img_create, qemu_io, file_path, log -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python')) - from qemu.machine import QEMUMachine iotests.script_initialize(supported_fmts=['qcow2'])
tests/qemu-iotests/297 +0 -6
@@ -68,12 +68,6 @@ def run_linters(): # Todo notes are fine, but fixme's or xxx's should probably just be # fixed (in tests, at least) env = os.environ.copy() - qemu_module_path = os.path.join(os.path.dirname(__file__), - '..', '..', 'python') - try: - env['PYTHONPATH'] += os.pathsep + qemu_module_path - except KeyError: - env['PYTHONPATH'] = qemu_module_path subprocess.run(('pylint-3', '--score=n', '--notes=FIXME,XXX', *files), env=env, check=False)
tests/qemu-iotests/300 +3 -4
@@ -24,12 +24,11 @@ import random import re from typing import Dict, List, Optional -import iotests - -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order from qemu.machine import machine +import iotests + + BlockBitmapMapping = List[Dict[str, object]] mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')
tests/qemu-iotests/iotests.py +0 -2
@@ -36,8 +36,6 @@ from contextlib import contextmanager -# pylint: disable=import-error, wrong-import-position -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python')) from qemu.machine import qtest from qemu.qmp import QMPMessage
tests/qemu-iotests/testenv.py +9 -5
@@ -108,12 +108,16 @@ def init_directories(self) -> None: SAMPLE_IMG_DIR OUTPUT_DIR """ + + # Path where qemu goodies live in this source tree. + qemu_srctree_path = Path(__file__, '../../../python').resolve() + self.pythonpath = os.getenv('PYTHONPATH') - if self.pythonpath: - self.pythonpath = self.source_iotests + os.pathsep + \ - self.pythonpath - else: - self.pythonpath = self.source_iotests + self.pythonpath = os.pathsep.join(filter(None, ( + self.source_iotests, + str(qemu_srctree_path), + self.pythonpath, + ))) self.test_dir = os.getenv('TEST_DIR', os.path.join(os.getcwd(), 'scratch'))
tests/qemu-iotests/tests/mirror-top-perms +3 -4
@@ -20,13 +20,12 @@ # import os + +import qemu + import iotests from iotests import qemu_img -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order -import qemu - image_size = 1 * 1024 * 1024 source = os.path.join(iotests.test_dir, 'source.img')
Signature · 1 line
2.31.1
Re: [PATCH 1/6] iotests: add 'qemu' package location to PYTHONPATH in testenv
Quoted 9 lines · John Snow
We can drop the sys.path hacking in various places by doing this. Additionally, by doing it in one place right up top, we can print interesting warnings in case the environment does not look correct.
If we ever decide to change how the environment is crafted, all of the "help me find my python packages" goop is all in one place, right in one function.
Signed-off-by John Snow <[email protected]>
Quoted 8 lines
--- tests/qemu-iotests/235 | 2 -- tests/qemu-iotests/297 | 6 ------ tests/qemu-iotests/300 | 7 +++---- tests/qemu-iotests/iotests.py | 2 -- tests/qemu-iotests/testenv.py | 14 +++++++++----- tests/qemu-iotests/tests/mirror-top-perms | 7 +++---- 6 files changed, 15 insertions(+), 23 deletions(-)
Re: [PATCH 1/6] iotests: add 'qemu' package location to PYTHONPATH in testenv
In reply to John SnowQuoted 9 lines · John Snow
We can drop the sys.path hacking in various places by doing this. Additionally, by doing it in one place right up top, we can print interesting warnings in case the environment does not look correct.
If we ever decide to change how the environment is crafted, all of the "help me find my python packages" goop is all in one place, right in one function.
Signed-off-by John Snow <[email protected]>
Hurrah!!
Quoted 85 lines
--- tests/qemu-iotests/235 | 2 -- tests/qemu-iotests/297 | 6 ------ tests/qemu-iotests/300 | 7 +++---- tests/qemu-iotests/iotests.py | 2 -- tests/qemu-iotests/testenv.py | 14 +++++++++----- tests/qemu-iotests/tests/mirror-top-perms | 7 +++---- 6 files changed, 15 insertions(+), 23 deletions(-)tests/qemu-iotests/235 +0
-2@@ -24,8 +24,6 @@ import os import iotests from iotests import qemu_img_create, qemu_io, file_path, log -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python')) - from qemu.machine import QEMUMachine iotests.script_initialize(supported_fmts=['qcow2'])tests/qemu-iotests/297 +0
-6@@ -68,12 +68,6 @@ def run_linters(): # Todo notes are fine, but fixme's or xxx's should probably just be # fixed (in tests, at least) env = os.environ.copy() - qemu_module_path = os.path.join(os.path.dirname(__file__), - '..', '..', 'python') - try: - env['PYTHONPATH'] += os.pathsep + qemu_module_path - except KeyError: - env['PYTHONPATH'] = qemu_module_path subprocess.run(('pylint-3', '--score=n', '--notes=FIXME,XXX', *files), env=env, check=False)tests/qemu-iotests/300 +3
-4@@ -24,12 +24,11 @@ import random import re from typing import Dict, List, Optional -import iotests - -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order from qemu.machine import machine +import iotests + + BlockBitmapMapping = List[Dict[str, object]] mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')tests/qemu-iotests/iotests.py +0
-2@@ -36,8 +36,6 @@ from contextlib import contextmanager -# pylint: disable=import-error, wrong-import-position -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python')) from qemu.machine import qtest from qemu.qmp import QMPMessagetests/qemu-iotests/testenv.py +4
-0@@ -108,12 +108,16 @@ def init_directories(self) -> None: SAMPLE_IMG_DIR OUTPUT_DIR """ + + # Path where qemu goodies live in this source tree. + qemu_srctree_path = Path(__file__, '../../../python').resolve() + self.pythonpath = os.getenv('PYTHONPATH')
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 120
Quoted 10 lines
tests/qemu-iotests/testenv.py +5
-5- if self.pythonpath: - self.pythonpath = self.source_iotests + os.pathsep + \ - self.pythonpath - else: - self.pythonpath = self.source_iotests + self.pythonpath = os.pathsep.join(filter(None, ( + self.source_iotests, + str(qemu_srctree_path), + self.pythonpath, + )))
That was simple:)
Hmm, after you this you have
self.pythonpath = ...
self.pythonpath = something other
If have to resend, you may just use os.getenv('PYTHONPATH') inside filter(), it seems to be even nicer.
Quoted 25 lines
tests/qemu-iotests/testenv.py +0
-0self.test_dir = os.getenv('TEST_DIR', os.path.join(os.getcwd(), 'scratch'))tests/qemu-iotests/tests/mirror-top-perms +3
-4@@ -20,13 +20,12 @@ # import os + +import qemu + import iotests from iotests import qemu_img -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order -import qemu - image_size = 1 * 1024 * 1024 source = os.path.join(iotests.test_dir, 'source.img')
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 1/6] iotests: add 'qemu' package location to PYTHONPATH in testenv
Quoted 109 lines · Vladimir Sementsov-Ogievskiy
Quoted 9 lines · John Snow
We can drop the sys.path hacking in various places by doing this. Additionally, by doing it in one place right up top, we can print interesting warnings in case the environment does not look correct.
If we ever decide to change how the environment is crafted, all of the "help me find my python packages" goop is all in one place, right in one function.
Signed-off-by John Snow <[email protected]>Hurrah!!
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 14 lines
--- tests/qemu-iotests/235 | 2 -- tests/qemu-iotests/297 | 6 ------ tests/qemu-iotests/300 | 7 +++---- tests/qemu-iotests/iotests.py | 2 -- tests/qemu-iotests/testenv.py | 14 +++++++++----- tests/qemu-iotests/tests/mirror-top-perms | 7 +++---- 6 files changed, 15 insertions(+), 23 deletions(-)tests/qemu-iotests/235 +0
-0tests/qemu-iotests/235 import os · line 27
Quoted 4 lines
tests/qemu-iotests/235 +0
-1@@ -24,8 +24,6 @@ import os import iotests from iotests import qemu_img_create, qemu_io, file_path, log -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..','python'))
Quoted 9 lines
tests/qemu-iotests/235 +0
-1- from qemu.machine import QEMUMachine iotests.script_initialize(supported_fmts=['qcow2'])tests/qemu-iotests/297 +0
-0tests/qemu-iotests/297 def run_linters(): · line 71
Quoted 10 lines
tests/qemu-iotests/297 +0
-6@@ -68,12 +68,6 @@ def run_linters(): # Todo notes are fine, but fixme's or xxx's should probably just be # fixed (in tests, at least) env = os.environ.copy() - qemu_module_path = os.path.join(os.path.dirname(__file__), - '..', '..', 'python') - try: - env['PYTHONPATH'] += os.pathsep + qemu_module_path - except KeyError: - env['PYTHONPATH'] = qemu_module_path subprocess.run(('pylint-3', '--score=n', '--notes=FIXME,XXX',*files),
Review
Quoted 23 lines
tests/qemu-iotests/297 +0
-0env=env, check=False)tests/qemu-iotests/300 +3
-4@@ -24,12 +24,11 @@ import random import re from typing import Dict, List, Optional -import iotests - -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order from qemu.machine import machine +import iotests + + BlockBitmapMapping = List[Dict[str, object]] mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')(quoted diff) +0
-0
b/tests/qemu-iotests/iotests.pytests/qemu-iotests/iotests.py line 40
Quoted 9 lines
index
ce06cf56304..b06ad76e0c5100644tests/qemu-iotests/iotests.py +0
-2@@ -36,8 +36,6 @@ from contextlib import contextmanager -# pylint: disable=import-error, wrong-import-position -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..','python'))
Review
Quoted 4 lines
tests/qemu-iotests/iotests.py +0
-0from qemu.machine import qtest from qemu.qmp import QMPMessage(quoted diff) +0
-0
b/tests/qemu-iotests/testenv.pyQuoted 16 lines
index
70da0d60c80..88104dace90100644tests/qemu-iotests/testenv.py +4
-4@@ -108,12 +108,16 @@ def init_directories(self) -> None: SAMPLE_IMG_DIR OUTPUT_DIR """ + + # Path where qemu goodies live in this source tree. + qemu_srctree_path = Path(__file__, '../../../python').resolve() + self.pythonpath = os.getenv('PYTHONPATH') - if self.pythonpath: - self.pythonpath = self.source_iotests + os.pathsep + \ - self.pythonpath - else:
Quoted 10 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 120
Quoted 6 lines
tests/qemu-iotests/testenv.py +5
-1- self.pythonpath = self.source_iotests + self.pythonpath = os.pathsep.join(filter(None, ( + self.source_iotests, + str(qemu_srctree_path), + self.pythonpath, + )))That was simple:)
Only because a very dedicated engineer from Virtuozzo spent so much time writing a new iotest launcher ;)
Quoted 10 lines
Hmm, after you this you have
self.pythonpath = ...
self.pythonpath = something otherIf have to resend, you may just use
os.getenv('PYTHONPATH')insidefilter(), it seems to be even nicer.
Ah, yeah. I'll just fold that in. Thanks!
Quoted 33 lines
Review
Quoted 4 lines
tests/qemu-iotests/testenv.py +0
-0self.test_dir = os.getenv('TEST_DIR', os.path.join(os.getcwd(), 'scratch'))(quoted diff) +0
-0b/tests/qemu-iotests/tests/mirror-top-perms
Quoted 11 lines
index
2fc8dd66e0a..73138a0ef91100755tests/qemu-iotests/tests/mirror-top-perms +3
-0@@ -20,13 +20,12 @@ # import os + +import qemu +tests/qemu-iotests/tests/mirror-top-perms line 31
Quoted 10 lines
tests/qemu-iotests/tests/mirror-top-perms +0
-4import iotests from iotests import qemu_img -# Import qemu after iotests.py has amended sys.path -# pylint: disable=wrong-import-order -import qemu - image_size = 1 * 1024 * 1024 source = os.path.join(iotests.test_dir, 'source.img')--
Best regards,
Vladimir
PATCH 2/6 iotests: add warning for rogue 'qemu' packages
In reply to John SnowAdd a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
tests/qemu-iotests/testenv.py +20 -1
@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import importlib.util +import logging import os import sys import tempfile
@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'
@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__, '../../../python').resolve() + # warn if we happen to be able to find 'qemu' packages from an + # unexpected location (i.e. the package is already installed in + # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec: + spec_path = Path(cast(str, qemu_spec.origin)) + try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported from outside " + "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1]) + self.pythonpath = os.getenv('PYTHONPATH') self.pythonpath = os.pathsep.join(filter(None, ( self.source_iotests,
@@ -231,6 +249,7 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str, self.build_root = os.path.join(self.build_iotests, '..', '..') + self._logger = logging.getLogger('qemu.iotests') self.init_directories() self.init_binaries()
Signature · 1 line
2.31.1
Re: [PATCH 2/6] iotests: add warning for rogue 'qemu' packages
Am 23.09.2021 um 02:16 hat John Snow geschrieben:
Quoted 36 lines
Add a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)tests/qemu-iotests/testenv.py +3
-1@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import importlib.util +import logging import os import sys import tempfile@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 122
Quoted 9 lines
tests/qemu-iotests/testenv.py +6
-0@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__, '../../../python').resolve() + # warn if we happen to be able to find 'qemu' packages from an + # unexpected location (i.e. the package is already installed in + # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec: + spec_path = Path(cast(str, qemu_spec.origin))
You're casting Optional[str] to str here. The source type is not obvious from the local code, so unless you spend some time actively figuring it out, this could be casting anything to str. But even knowing this, just casting Optional away looks fishy anyway.
Looking up the ModuleSpec docs, it seems okay to assume that it's never None in our case, but wouldn't it be much cleaner to just 'assert qemu_spec.origin' first and then use it without the cast?
Quoted 14 lines
tests/qemu-iotests/testenv.py +10
-0+ try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported from outside " + "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1]) + self.pythonpath = os.getenv('PYTHONPATH') self.pythonpath = os.pathsep.join(filter(None, ( self.source_iotests,
tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 254
Quoted 6 lines
tests/qemu-iotests/testenv.py +1
-0@@ -231,6 +249,7 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str, self.build_root = os.path.join(self.build_iotests, '..', '..') + self._logger = logging.getLogger('qemu.iotests') self.init_directories() self.init_binaries()
Looks good otherwise.
Kevin
Re: [PATCH 2/6] iotests: add warning for rogue 'qemu' packages
Am 23.09.2021 um 12:57 hat Kevin Wolf geschrieben:
Quoted 71 lines
Am 23.09.2021 um 02:16 hat John Snow geschrieben:
Quoted 36 lines
Add a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)tests/qemu-iotests/testenv.py +3
-1@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import importlib.util +import logging import os import sys import tempfile@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 122
Quoted 9 lines
tests/qemu-iotests/testenv.py +6
-0@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__, '../../../python').resolve() + # warn if we happen to be able to find 'qemu' packages from an + # unexpected location (i.e. the package is already installed in + # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec: + spec_path = Path(cast(str, qemu_spec.origin))You're casting Optional[str] to str here. The source type is not obvious from the local code, so unless you spend some time actively figuring it out, this could be casting anything to str. But even knowing this, just casting Optional away looks fishy anyway.
Looking up the ModuleSpec docs, it seems okay to assume that it's never None in our case, but wouldn't it be much cleaner to just 'assert qemu_spec.origin' first and then use it without the cast?
Quoted 15 lines
tests/qemu-iotests/testenv.py +10
-0+ try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported from outside " + "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1]) + self.pythonpath = os.getenv('PYTHONPATH') self.pythonpath = os.pathsep.join(filter(None, ( self.source_iotests,@@ -231,6 +249,7 @@ def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
Quoted 7 lines
tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 254
Quoted 5 lines
tests/qemu-iotests/testenv.py +1
-0self.build_root = os.path.join(self.build_iotests, '..', '..') + self._logger = logging.getLogger('qemu.iotests') self.init_directories() self.init_binaries()Looks good otherwise.
Well, actually there is a major problem: We'll pass the right PYTHONPATH for the test scripts that we're calling, but this script itself doesn't use it yet. So what I get is:
Traceback (most recent call last):
File "/home/kwolf/source/qemu/tests/qemu-iotests/build/check", line 120, in <module>
env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
File "/home/kwolf/source/qemu/tests/qemu-iotests/testenv.py", line 253, in __init__
self.init_directories()
File "/home/kwolf/source/qemu/tests/qemu-iotests/testenv.py", line 120, in init_directories
qemu_spec = importlib.util.find_spec('qemu.qmp')
File "/usr/lib64/python3.9/importlib/util.py", line 94, in find_spec
parent = __import__(parent_name, fromlist=['__path__'])
ModuleNotFoundError: No module named 'qemu'
Kevin
Re: [PATCH 2/6] iotests: add warning for rogue 'qemu' packages
Quoted 39 lines · Kevin Wolf
Am 23.09.2021 um 12:57 hat Kevin Wolf geschrieben:
Quoted 9 lines
Am 23.09.2021 um 02:16 hat John Snow geschrieben:
Quoted 8 lines
Add a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
Quoted 6 lines
Review
Quoted 6 lines
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/tests/qemu-iotests/testenv.pyQuoted 22 lines
tests/qemu-iotests/testenv.py line 16
Quoted 5 lines
index
88104dace90..8a43b193af5100644tests/qemu-iotests/testenv.py +0
-0@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/.
Quoted 16 lines
tests/qemu-iotests/testenv.py +3
-1# +import importlib.util +import logging import os import sys import tempfile@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'
Quoted 23 lines
Quoted 3 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 115
Quoted 3 lines
tests/qemu-iotests/testenv.py +0
-0@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__,'../../../python').resolve()
Quoted 3 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 117
Quoted 3 lines
tests/qemu-iotests/testenv.py +2
-0+ # warn if we happen to be able to find 'qemu' packages from an + # unexpected location (i.e. the package is already installedin
Quoted 14 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 122
Quoted 4 lines
tests/qemu-iotests/testenv.py +4
-0+ # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec: + spec_path = Path(cast(str, qemu_spec.origin))You're casting Optional[str] to str here. The source type is not obvious from the local code, so unless you spend some time actively figuring it out, this could be casting anything to str. But even knowing this, just casting Optional away looks fishy anyway.
Looking up the ModuleSpec docs, it seems okay to assume that it's never None in our case, but wouldn't it be much cleaner to just 'assert qemu_spec.origin' first and then use it without the cast?
OK. I suppose I was thinking: "It's always going to be a string, and if it isn't, something else will explode below, surely, so the assertion is redundant", but I don't want code that makes people wonder, so principle of least surprise it is.
Quoted 40 lines
Quoted 5 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None:
Quoted 5 lines
tests/qemu-iotests/testenv.py +5
-0+ try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported fromoutside "
Quoted 9 lines
tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str,
Quoted 9 lines
tests/qemu-iotests/testenv.py +5
-0+ "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1]) + self.pythonpath = os.getenv('PYTHONPATH') self.pythonpath = os.pathsep.join(filter(None, ( self.source_iotests,@@ -231,6 +249,7 @@ def __init__(self, imgfmt: str, imgproto: str,aiomode: str,
Quoted 8 lines
tests/qemu-iotests/testenv.py def __init__(self, imgfmt: str, imgproto: str, aiomode: str, · line 254
Quoted 6 lines
tests/qemu-iotests/testenv.py +1
-0self.build_root = os.path.join(self.build_iotests, '..', '..') + self._logger = logging.getLogger('qemu.iotests') self.init_directories() self.init_binaries()Looks good otherwise.
Well, actually there is a major problem: We'll pass the right PYTHONPATH for the test scripts that we're calling, but this script itself doesn't use it yet. So what I get is:
Traceback (most recent call last):
File "/home/kwolf/source/qemu/tests/qemu-iotests/build/check", line 120,
in <module>
env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
File "/home/kwolf/source/qemu/tests/qemu-iotests/testenv.py", line 253,
in__init__
self.init_directories()
File "/home/kwolf/source/qemu/tests/qemu-iotests/testenv.py", line 120,
ininit_directories
qemu_spec=importlib.util.find_spec('qemu.qmp')
File "/usr/lib64/python3.9/importlib/util.py", line 94, infind_spec
Quoted 6 lines
parent = __import__(parent_name, fromlist=['__path__'])ModuleNotFoundError: No module named 'qemu'
Kevin
Tch. So, it turns out with namespace packages that once you remove the subpackages (pip uninstall qemu), it leaves the empty namespace folder behind. This is also the reason I directly use 'qemu.qmp' as a bellwether here, to make sure we're prodding a package and not just an empty namespace with nothing in it.
I'll fix these, thanks.
Re: [PATCH 2/6] iotests: add warning for rogue 'qemu' packages
In reply to John SnowQuoted 4 lines · John Snow
Add a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
IIUC, it is/was a valid use case to run the iotests on arbitrary QEMU outside the source tree ie a distro packaged binary set.
Are we not allowing the tests/qemu-iotests/* content to be run outside the context of the main source tree for this purpose ?
eg consider if Fedora/RHEL builds put tests/qemu-iotests/* into a 'qemu-iotests' RPM, which was installed and used with a distro package python-qemu ?
Quoted 38 lines
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)tests/qemu-iotests/testenv.py +8
-1@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import importlib.util +import logging import os import sys import tempfile@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__, '../../../python').resolve() + # warn if we happen to be able to find 'qemu' packages from an + # unexpected location (i.e. the package is already installed in + # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec:
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 131
Quoted 10 lines
tests/qemu-iotests/testenv.py +10
-0+ spec_path = Path(cast(str, qemu_spec.origin)) + try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported from outside " + "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1])
It feels to me like the scenario we're blocking here is actally the scenario we want to aim for.
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 2/6] iotests: add warning for rogue 'qemu' packages
Quoted 9 lines · Daniel P. Berrangé
Quoted 4 lines · John Snow
Add a warning for when 'iotests' runs against a qemu namespace that isn't the one in the source tree. This might occur if you have (accidentally) installed the Python namespace package to your local packages.
IIUC, it is/was a valid use case to run the iotests on arbitrary QEMU outside the source tree ie a distro packaged binary set.
Quoted 9 lines
Are we not allowing the tests/qemu-iotests/* content to be run outside the context of the main source tree for this purpose ?
eg consider if Fedora/RHEL builds put tests/qemu-iotests/* into a 'qemu-iotests' RPM, which was installed and used with a distro package python-qemu ?
(1) "isn't the one in the source tree" is imprecise language here. The real key is that it must be the QEMU namespace located at " testenv.py/../../../python/qemu". This usually means the source tree, but it'd work in any identically structured filesystem hierarchy.
(2) The preceding patch might help illustrate this. At present the iotests expect to be able to find the 'qemu' python packages by navigating directories starting from their own location (and not CWD). What patch 1 does is to centralize the logic that has to go "searching" for the python packages into the init_directories method in testenv.py so that each individual test doesn't have to repeat it.
i.e. before patch 1, iotests.py does this:sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
so we'll always crawl to ../../python from wherever iotests.py is.
After patch 1, we're going to crawl to the same location, but starting from testenv.py instead. testenv.py and iotests.py are in the same directory, so I expect whatever worked before (and in whatever environment) will continue working after. I can't say with full certainty in what circumstances we support running iotests, but I don't think I have introduced any new limitation with this.
Quoted 46 lines
Review
Quoted 8 lines
(I'm not going to say that this is because I bit myself with this, but. You can fill in the blanks.)
Signed-off-by John Snow <[email protected]>--- tests/qemu-iotests/testenv.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/tests/qemu-iotests/testenv.pyQuoted 26 lines
index
88104dace90..8a43b193af5100644tests/qemu-iotests/testenv.py +4
-1@@ -16,6 +16,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import importlib.util +import logging import os import sys import tempfile@@ -25,7 +27,7 @@ import random import subprocess import glob -from typing import List, Dict, Any, Optional, ContextManager +from typing import List, Dict, Any, Optional, ContextManager, cast DEF_GDB_OPTIONS = 'localhost:12345'@@ -112,6 +114,22 @@ def init_directories(self) -> None: # Path where qemu goodies live in this source tree. qemu_srctree_path = Path(__file__, '../../../python').resolve() + # warn if we happen to be able to find 'qemu' packages from antests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 127
Quoted 10 lines
tests/qemu-iotests/testenv.py +10
-0+ # unexpected location (i.e. the package is already installed in + # the user's environment) + qemu_spec = importlib.util.find_spec('qemu.qmp') + if qemu_spec: + spec_path = Path(cast(str, qemu_spec.origin)) + try: + _ = spec_path.relative_to(qemu_srctree_path) + except ValueError: + self._logger.warning( + "WARNING: 'qemu' package will be imported fromoutside "
Quoted 9 lines
tests/qemu-iotests/testenv.py def init_directories(self) -> None: · line 131
Quoted 4 lines
tests/qemu-iotests/testenv.py +4
-0+ "the source tree!") + self._logger.warning( + "Importing from: '%s'", + spec_path.parents[1])It feels to me like the scenario we're blocking here is actally the scenario we want to aim for.
It isn't blocking it, it's just a warning. At present, iotests expects that it's using the version of the python packages that are in the source tree / tarball / whatever. Since I converted those packages to be installable to your environment, I introduced an ambiguity about which version iotests is actually using: the version you installed, or the version in the source tree? This is just merely a warning to let you know that iotests is technically doing something new. ("Hey, you're running some other python code than what iotests has done for the past ten years. Are you cool with that?")
You're right, though: my actual end-goal (after a lot of pending cleanups I have in-flight, I am getting to it ...!) is to eventually pivot iotests to always using qemu as an installed package and wean it off of using directory-crawling to find its dependencies. We are close to doing that. When we do transition to that, the warning can be dropped as there will no longer be an ambiguity over which version it is using. There are some questions for me to ponder first over exactly how the environment setup should be accomplished, though.
Quoted 11 lines
Regards,
Daniel
--
|: 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 :|
PATCH 3/6 iotests/linters: check mypy files all at once
In reply to John SnowWe can circumvent the '__main__' redefinition problem by passing --scripts-are-modules. Take mypy out of the loop per-filename and check everything in one go: it's quite a bit faster.
--- tests/qemu-iotests/297 | 44 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-)
tests/qemu-iotests/297 +20 -24
@@ -74,32 +74,28 @@ def run_linters(): print('=== mypy ===') sys.stdout.flush() - # We have to call mypy separately for each file. Otherwise, it - # will interpret all given files as belonging together (i.e., they - # may not both define the same classes, etc.; most notably, they - # must not both define the __main__ module). env['MYPYPATH'] = env['PYTHONPATH'] - for filename in files: - p = subprocess.run(('mypy', - '--warn-unused-configs', - '--disallow-subclassing-any', - '--disallow-any-generics', - '--disallow-incomplete-defs', - '--disallow-untyped-decorators', - '--no-implicit-optional', - '--warn-redundant-casts', - '--warn-unused-ignores', - '--no-implicit-reexport', - '--namespace-packages', - filename), - env=env, - check=False, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - universal_newlines=True) + p = subprocess.run(('mypy', + '--warn-unused-configs', + '--disallow-subclassing-any', + '--disallow-any-generics', + '--disallow-incomplete-defs', + '--disallow-untyped-decorators', + '--no-implicit-optional', + '--warn-redundant-casts', + '--warn-unused-ignores', + '--no-implicit-reexport', + '--namespace-packages', + '--scripts-are-modules', + *files), + env=env, + check=False, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True) - if p.returncode != 0: - print(p.stdout) + if p.returncode != 0: + print(p.stdout) for linter in ('pylint-3', 'mypy'):
Signature · 1 line
2.31.1
Re: [PATCH 3/6] iotests/linters: check mypy files all at once
Quoted 9 lines · John Snow
We can circumvent the '
__main__' redefinition problem by passing --scripts-are-modules. Take mypy out of the loop per-filename and check everything in one go: it's quite a bit faster.Signed-off-by John Snow <[email protected]>Reviewed-by Hanna Reitz <[email protected]>--- tests/qemu-iotests/297 | 44 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-)
Re: [PATCH 3/6] iotests/linters: check mypy files all at once
In reply to John SnowQuoted 6 lines · John Snow
We can circumvent the '
__main__' redefinition problem by passing --scripts-are-modules. Take mypy out of the loop per-filename and check everything in one go: it's quite a bit faster.Signed-off-by John Snow<[email protected]>Reviewed-by Hanna Reitz<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH 4/6 iotests/mirror-top-perms: Adjust imports
In reply to John SnowWe need to import things from the qemu namespace; importing the namespace alone doesn't bring the submodules with it -- unless someone else (like iotests.py) imports them too.
Adjust the imports.
--- tests/qemu-iotests/tests/mirror-top-perms | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tests/qemu-iotests/tests/mirror-top-perms +4 -3
@@ -21,7 +21,8 @@ import os -import qemu +from qemu import qmp +from qemu.machine import machine import iotests from iotests import qemu_img
@@ -46,7 +47,7 @@ class TestMirrorTopPerms(iotests.QMPTestCase): def tearDown(self): try: self.vm.shutdown() - except qemu.machine.machine.AbnormalShutdown: + except machine.AbnormalShutdown: pass if self.vm_b is not None:
@@ -101,7 +102,7 @@ class TestMirrorTopPerms(iotests.QMPTestCase): self.vm_b.launch() print('ERROR: VM B launched successfully, this should not have ' 'happened') - except qemu.qmp.QMPConnectError: + except qmp.QMPConnectError: assert 'Is another process using the image' in self.vm_b.get_log() result = self.vm.qmp('block-job-cancel',
Signature · 1 line
2.31.1
Re: [PATCH 4/6] iotests/mirror-top-perms: Adjust imports
Quoted 9 lines · John Snow
We need to import things from the qemu namespace; importing the namespace alone doesn't bring the submodules with it -- unless someone else (like
iotests.py) imports them too.Adjust the imports.
Signed-off-by John Snow<[email protected]>Reviewed-by Philippe Mathieu-Daudé<[email protected]>Reviewed-by Hanna Reitz<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH 5/6 iotests/migrate-bitmaps-test: delint
In reply to John SnowMostly uninteresting stuff. Move the test injections under a function named main() so that the variables used during that process aren't in the global scope.
--- tests/qemu-iotests/tests/migrate-bitmaps-test | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-)
tests/qemu-iotests/tests/migrate-bitmaps-test +28 -22
@@ -19,10 +19,11 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -import os import itertools import operator +import os import re + import iotests from iotests import qemu_img, qemu_img_create, Timeout
@@ -224,25 +225,6 @@ def inject_test_case(klass, suffix, method, *args, **kwargs): setattr(klass, 'test_' + method + suffix, lambda self: mc(self)) -for cmb in list(itertools.product((True, False), repeat=5)): - name = ('_' if cmb[0] else '_not_') + 'persistent_' - name += ('_' if cmb[1] else '_not_') + 'migbitmap_' - name += '_online' if cmb[2] else '_offline' - name += '_shared' if cmb[3] else '_nonshared' - if cmb[4]: - name += '__pre_shutdown' - - inject_test_case(TestDirtyBitmapMigration, name, 'do_test_migration', - *list(cmb)) - -for cmb in list(itertools.product((True, False), repeat=2)): - name = ('_' if cmb[0] else '_not_') + 'persistent_' - name += ('_' if cmb[1] else '_not_') + 'migbitmap' - - inject_test_case(TestDirtyBitmapMigration, name, - 'do_test_migration_resume_source', *list(cmb)) - - class TestDirtyBitmapBackingMigration(iotests.QMPTestCase): def setUp(self): qemu_img_create('-f', iotests.imgfmt, base_a, size)
@@ -304,6 +286,30 @@ class TestDirtyBitmapBackingMigration(iotests.QMPTestCase): self.assert_qmp(result, 'return', {}) +def main() -> None: + for cmb in list(itertools.product((True, False), repeat=5)): + name = ('_' if cmb[0] else '_not_') + 'persistent_' + name += ('_' if cmb[1] else '_not_') + 'migbitmap_' + name += '_online' if cmb[2] else '_offline' + name += '_shared' if cmb[3] else '_nonshared' + if cmb[4]: + name += '__pre_shutdown' + + inject_test_case(TestDirtyBitmapMigration, name, 'do_test_migration', + *list(cmb)) + + for cmb in list(itertools.product((True, False), repeat=2)): + name = ('_' if cmb[0] else '_not_') + 'persistent_' + name += ('_' if cmb[1] else '_not_') + 'migbitmap' + + inject_test_case(TestDirtyBitmapMigration, name, + 'do_test_migration_resume_source', *list(cmb)) + + iotests.main( + supported_fmts=['qcow2'], + supported_protocols=['file'] + ) + + if __name__ == '__main__': - iotests.main(supported_fmts=['qcow2'], - supported_protocols=['file']) + main()
Signature · 1 line
2.31.1
Re: [PATCH 5/6] iotests/migrate-bitmaps-test: delint
Quoted 7 lines · John Snow
Mostly uninteresting stuff. Move the test injections under a function named
main()so that the variables used during that process aren't in the global scope.Signed-off-by John Snow<[email protected]>Reviewed-by Philippe Mathieu-Daudé<[email protected]>Reviewed-by Hanna Reitz<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH 6/6 iotests: Update for pylint 2.11.1
In reply to John Snow1. Ignore the new f-strings warning, we're not interested in doing a full conversion at this time.
2. Just mute the unbalanced-tuple-unpacking warning, it's not a real error in this case and muting the dozens of callsites is just not worth it.
3. Add encodings to read_text().
--- tests/qemu-iotests/pylintrc | 6 +++++- tests/qemu-iotests/testrunner.py | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-)
tests/qemu-iotests/pylintrc +5 -1
@@ -19,13 +19,17 @@ disable=invalid-name, too-many-public-methods, # pylint warns about Optional[] etc. as unsubscriptable in 3.9 unsubscriptable-object, + # pylint's static analysis causes false positivies for file_path(); + # If we really care to make it statically knowable, we'll use mypy. + unbalanced-tuple-unpacking, # Sometimes we need to disable a newly introduced pylint warning. # Doing so should not produce a warning in older versions of pylint. bad-option-value, # These are temporary, and should be removed: missing-docstring, too-many-return-statements, - too-many-statements + too-many-statements, + consider-using-f-string, [FORMAT]
tests/qemu-iotests/testrunner.py +4 -3
@@ -266,12 +266,13 @@ def do_run_test(self, test: str) -> TestResult: diff=file_diff(str(f_reference), str(f_bad))) if f_notrun.exists(): - return TestResult(status='not run', - description=f_notrun.read_text().strip()) + return TestResult( + status='not run', + description=f_notrun.read_text(encoding='utf-8').strip()) casenotrun = '' if f_casenotrun.exists(): - casenotrun = f_casenotrun.read_text() + casenotrun = f_casenotrun.read_text(encoding='utf-8') diff = file_diff(str(f_reference), str(f_bad)) if diff:
Signature · 1 line
2.31.1
Re: [PATCH 6/6] iotests: Update for pylint 2.11.1
Quoted 10 lines · John Snow
1. Ignore the new f-strings warning, we're not interested in doing a full conversion at this time.
2. Just mute the unbalanced-tuple-unpacking warning, it's not a real error in this case and muting the dozens of callsites is just not worth it.
3. Add encodings to
read_text().Signed-off-by John Snow<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Am 23.09.2021 um 02:16 hat John Snow geschrieben:
Quoted 9 lines
GitLab: https://gitlab.com/jsnow/qemu/-/commits/python-package-iotest-pt1
CI: https://gitlab.com/jsnow/qemu/-/pipelines/375630185This series partially supersedes:
[PATCH v3 00/16] python/iotests: Run iotest linters during Python CI'Howdy, this is good stuff we want even if we aren't yet in agreement about the best way to run iotest 297 from CI.
Quoted 6 lines
- Update linting config to tolerate pylint 2.11.1
- Eliminate sys.path hacking in individual test files
- make mypy execution in test 297 fasterThe rest of the actual "run at CI time" stuff can get handled separately and later pending some discussion on the other series.
Patch 2 seems to need some more work.
The rest is: Reviewed-by: Kevin Wolf <[email protected]>