From 490edbdc486c595cbbcd18e4138662701ba72b7b Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 4 Jun 2019 21:59:56 -0700 Subject: [PATCH] Add python version checking when using --tox. --- test/runner/lib/cli.py | 4 ++++ test/runner/lib/config.py | 8 +++++++- test/runner/lib/delegation.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/runner/lib/cli.py b/test/runner/lib/cli.py index eb0ef7d8c9b..1ffc7201dd5 100644 --- a/test/runner/lib/cli.py +++ b/test/runner/lib/cli.py @@ -182,6 +182,10 @@ def parse_args(): action='store_true', help='redact sensitive values in output') + common.add_argument('--check-python', + choices=SUPPORTED_PYTHON_VERSIONS, + help=argparse.SUPPRESS) + test = argparse.ArgumentParser(add_help=False, parents=[common]) test.add_argument('include', diff --git a/test/runner/lib/config.py b/test/runner/lib/config.py index 4c276333e72..e3cffe7fb3e 100644 --- a/test/runner/lib/config.py +++ b/test/runner/lib/config.py @@ -12,6 +12,7 @@ from lib.util import ( find_python, generate_pip_command, get_docker_completion, + ApplicationError, ) from lib.metadata import ( @@ -67,7 +68,9 @@ class EnvironmentConfig(CommonConfig): if self.python == 'default': self.python = None - self.python_version = self.python or '.'.join(str(i) for i in sys.version_info[:2]) + actual_major_minor = '.'.join(str(i) for i in sys.version_info[:2]) + + self.python_version = self.python or actual_major_minor self.python_interpreter = args.python_interpreter self.delegate = self.tox or self.docker or self.remote @@ -79,6 +82,9 @@ class EnvironmentConfig(CommonConfig): self.inject_httptester = args.inject_httptester if 'inject_httptester' in args else False # type: bool self.httptester = docker_qualify_image(args.httptester if 'httptester' in args else '') # type: str + if args.check_python and args.check_python != actual_major_minor: + raise ApplicationError('Running under Python %s instead of Python %s as expected.' % (actual_major_minor, args.check_python)) + @property def python_executable(self): """ diff --git a/test/runner/lib/delegation.py b/test/runner/lib/delegation.py index fa81d569b19..6c4517c918a 100644 --- a/test/runner/lib/delegation.py +++ b/test/runner/lib/delegation.py @@ -165,6 +165,14 @@ def delegate_tox(args, exclude, require, integration_targets): if not args.python: cmd += ['--python', version] + # newer versions of tox do not support older python versions and will silently fall back to a different version + # passing this option will allow the delegated ansible-test to verify it is running under the expected python version + # tox 3.0.0 dropped official python 2.6 support: https://tox.readthedocs.io/en/latest/changelog.html#v3-0-0-2018-04-02 + # tox 3.1.3 is the first version to support python 3.8 and later: https://tox.readthedocs.io/en/latest/changelog.html#v3-1-3-2018-08-03 + # tox 3.1.3 appears to still work with python 2.6, making it a good version to use when supporting all python versions we use + # virtualenv 16.0.0 dropped python 2.6 support: https://virtualenv.pypa.io/en/latest/changes/#v16-0-0-2018-05-16 + cmd += ['--check-python', version] + if isinstance(args, TestConfig): if args.coverage and not args.coverage_label: cmd += ['--coverage-label', 'tox-%s' % version]