2016-11-30 06:21:53 +01:00
|
|
|
"""Execute Ansible tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2021-04-06 01:48:59 +02:00
|
|
|
import atexit
|
2017-05-11 13:05:21 +02:00
|
|
|
import json
|
2016-11-30 06:21:53 +01:00
|
|
|
import os
|
2017-03-08 09:47:21 +01:00
|
|
|
import re
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from . import types as t
|
2019-07-11 22:03:49 +02:00
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
from .io import (
|
|
|
|
make_dirs,
|
|
|
|
read_text_file,
|
|
|
|
write_text_file,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2016-11-30 06:21:53 +01:00
|
|
|
ApplicationWarning,
|
|
|
|
ApplicationError,
|
|
|
|
SubprocessError,
|
|
|
|
display,
|
2017-05-11 13:05:21 +02:00
|
|
|
find_executable,
|
|
|
|
raw_command,
|
2018-09-18 17:37:14 +02:00
|
|
|
generate_pip_command,
|
|
|
|
find_python,
|
2019-05-24 22:10:33 +02:00
|
|
|
cmd_quote,
|
2019-08-06 02:40:00 +02:00
|
|
|
ANSIBLE_TEST_DATA_ROOT,
|
2020-03-29 06:33:13 +02:00
|
|
|
str_to_version,
|
2020-12-09 19:46:05 +01:00
|
|
|
version_to_str,
|
2018-05-09 18:24:39 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
intercept_command,
|
|
|
|
run_command,
|
2019-08-28 08:40:06 +02:00
|
|
|
ResultType,
|
2021-04-06 01:48:59 +02:00
|
|
|
CommonConfig,
|
2019-07-11 07:00:34 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .docker_util import (
|
2018-05-09 18:24:39 +02:00
|
|
|
docker_pull,
|
|
|
|
docker_run,
|
2021-02-02 20:47:38 +01:00
|
|
|
docker_inspect,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .ansible_util import (
|
2016-11-30 06:21:53 +01:00
|
|
|
ansible_environment,
|
2019-06-05 07:08:23 +02:00
|
|
|
check_pyyaml,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
2020-04-26 03:55:39 +02:00
|
|
|
from .ci import (
|
|
|
|
get_ci_provider,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .classification import (
|
2016-11-30 06:21:53 +01:00
|
|
|
categorize_changes,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .config import (
|
2017-07-07 01:14:44 +02:00
|
|
|
TestConfig,
|
|
|
|
EnvironmentConfig,
|
|
|
|
IntegrationConfig,
|
|
|
|
ShellConfig,
|
2021-04-06 01:48:59 +02:00
|
|
|
UnitsConfig,
|
|
|
|
SanityConfig,
|
2017-07-07 01:14:44 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .metadata import (
|
2018-04-13 01:15:28 +02:00
|
|
|
ChangeDescription,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2021-04-06 01:48:59 +02:00
|
|
|
from .http import (
|
|
|
|
urlparse,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def create_shell_command(command):
|
|
|
|
"""
|
|
|
|
:type command: list[str]
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
|
|
|
optional_vars = (
|
|
|
|
'TERM',
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd = ['/usr/bin/env']
|
|
|
|
cmd += ['%s=%s' % (var, os.environ[var]) for var in optional_vars if var in os.environ]
|
|
|
|
cmd += command
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2020-12-09 19:46:05 +01:00
|
|
|
def get_openssl_version(args, python, python_version): # type: (EnvironmentConfig, str, str) -> t.Optional[t.Tuple[int, ...]]
|
|
|
|
"""Return the openssl version."""
|
|
|
|
if not python_version.startswith('2.'):
|
|
|
|
# OpenSSL version checking only works on Python 3.x.
|
|
|
|
# This should be the most accurate, since it is the Python we will be using.
|
|
|
|
version = json.loads(run_command(args, [python, os.path.join(ANSIBLE_TEST_DATA_ROOT, 'sslcheck.py')], capture=True, always=True)[0])['version']
|
|
|
|
|
|
|
|
if version:
|
|
|
|
display.info('Detected OpenSSL version %s under Python %s.' % (version_to_str(version), python_version), verbosity=1)
|
|
|
|
|
|
|
|
return tuple(version)
|
|
|
|
|
|
|
|
# Fall back to detecting the OpenSSL version from the CLI.
|
|
|
|
# This should provide an adequate solution on Python 2.x.
|
|
|
|
openssl_path = find_executable('openssl', required=False)
|
|
|
|
|
|
|
|
if openssl_path:
|
|
|
|
try:
|
|
|
|
result = raw_command([openssl_path, 'version'], capture=True)[0]
|
|
|
|
except SubprocessError:
|
|
|
|
result = ''
|
|
|
|
|
|
|
|
match = re.search(r'^OpenSSL (?P<version>[0-9]+\.[0-9]+\.[0-9]+)', result)
|
|
|
|
|
|
|
|
if match:
|
|
|
|
version = str_to_version(match.group('version'))
|
|
|
|
|
|
|
|
display.info('Detected OpenSSL version %s using the openssl CLI.' % version_to_str(version), verbosity=1)
|
|
|
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
display.info('Unable to detect OpenSSL version.', verbosity=1)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-03-29 06:33:13 +02:00
|
|
|
def get_setuptools_version(args, python): # type: (EnvironmentConfig, str) -> t.Tuple[int]
|
|
|
|
"""Return the setuptools version for the given python."""
|
|
|
|
try:
|
|
|
|
return str_to_version(raw_command([python, '-c', 'import setuptools; print(setuptools.__version__)'], capture=True)[0])
|
|
|
|
except SubprocessError:
|
|
|
|
if args.explain:
|
|
|
|
return tuple() # ignore errors in explain mode in case setuptools is not aleady installed
|
|
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2020-12-17 23:59:53 +01:00
|
|
|
def install_cryptography(args, python, python_version, pip): # type: (EnvironmentConfig, str, str, t.List[str]) -> None
|
|
|
|
"""
|
|
|
|
Install cryptography for the specified environment.
|
|
|
|
"""
|
|
|
|
# make sure ansible-test's basic requirements are met before continuing
|
|
|
|
# this is primarily to ensure that pip is new enough to facilitate further requirements installation
|
|
|
|
install_ansible_test_requirements(args, pip)
|
|
|
|
|
|
|
|
# make sure setuptools is available before trying to install cryptography
|
|
|
|
# the installed version of setuptools affects the version of cryptography to install
|
|
|
|
run_command(args, generate_pip_install(pip, '', packages=['setuptools']))
|
|
|
|
|
|
|
|
# install the latest cryptography version that the current requirements can support
|
|
|
|
# use a custom constraints file to avoid the normal constraints file overriding the chosen version of cryptography
|
|
|
|
# if not installed here later install commands may try to install an unsupported version due to the presence of older setuptools
|
|
|
|
# this is done instead of upgrading setuptools to allow tests to function with older distribution provided versions of setuptools
|
|
|
|
run_command(args, generate_pip_install(pip, '',
|
|
|
|
packages=[get_cryptography_requirement(args, python, python_version)],
|
|
|
|
constraints=os.path.join(ANSIBLE_TEST_DATA_ROOT, 'cryptography-constraints.txt')))
|
|
|
|
|
|
|
|
|
|
|
|
def get_cryptography_requirement(args, python, python_version): # type: (EnvironmentConfig, str, str) -> str
|
2020-03-29 06:33:13 +02:00
|
|
|
"""
|
|
|
|
Return the correct cryptography requirement for the given python version.
|
2020-12-09 19:46:05 +01:00
|
|
|
The version of cryptography installed depends on the python version, setuptools version and openssl version.
|
2020-03-29 06:33:13 +02:00
|
|
|
"""
|
|
|
|
setuptools_version = get_setuptools_version(args, python)
|
2020-12-09 19:46:05 +01:00
|
|
|
openssl_version = get_openssl_version(args, python, python_version)
|
2020-03-29 06:33:13 +02:00
|
|
|
|
|
|
|
if setuptools_version >= (18, 5):
|
|
|
|
if python_version == '2.6':
|
|
|
|
# cryptography 2.2+ requires python 2.7+
|
|
|
|
# see https://github.com/pyca/cryptography/blob/master/CHANGELOG.rst#22---2018-03-19
|
|
|
|
cryptography = 'cryptography < 2.2'
|
2020-12-09 19:46:05 +01:00
|
|
|
elif openssl_version and openssl_version < (1, 1, 0):
|
|
|
|
# cryptography 3.2 requires openssl 1.1.x or later
|
|
|
|
# see https://cryptography.io/en/latest/changelog.html#v3-2
|
|
|
|
cryptography = 'cryptography < 3.2'
|
2020-03-29 06:33:13 +02:00
|
|
|
else:
|
2021-02-08 23:35:00 +01:00
|
|
|
# cryptography 3.4+ fails to install on many systems
|
|
|
|
# this is a temporary work-around until a more permanent solution is available
|
|
|
|
cryptography = 'cryptography < 3.4'
|
2020-03-29 06:33:13 +02:00
|
|
|
else:
|
|
|
|
# cryptography 2.1+ requires setuptools 18.5+
|
|
|
|
# see https://github.com/pyca/cryptography/blob/62287ae18383447585606b9d0765c0f1b8a9777c/setup.py#L26
|
|
|
|
cryptography = 'cryptography < 2.1'
|
|
|
|
|
|
|
|
return cryptography
|
|
|
|
|
|
|
|
|
2021-05-07 02:59:07 +02:00
|
|
|
def install_command_requirements(args, python_version=None, context=None, enable_pyyaml_check=False, extra_requirements=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
2018-09-18 17:37:14 +02:00
|
|
|
:type python_version: str | None
|
2020-06-09 22:38:36 +02:00
|
|
|
:type context: str | None
|
2020-06-10 18:39:25 +02:00
|
|
|
:type enable_pyyaml_check: bool
|
2021-05-07 02:59:07 +02:00
|
|
|
:type extra_requirements: list[str] | None
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2019-07-23 04:24:48 +02:00
|
|
|
if not args.explain:
|
2019-08-28 08:40:06 +02:00
|
|
|
make_dirs(ResultType.COVERAGE.path)
|
|
|
|
make_dirs(ResultType.DATA.path)
|
2019-07-23 04:24:48 +02:00
|
|
|
|
2018-11-16 23:50:01 +01:00
|
|
|
if isinstance(args, ShellConfig):
|
|
|
|
if args.raw:
|
|
|
|
return
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if not args.requirements:
|
|
|
|
return
|
|
|
|
|
2018-03-07 23:02:31 +01:00
|
|
|
if isinstance(args, ShellConfig):
|
|
|
|
return
|
|
|
|
|
2017-03-08 09:47:21 +01:00
|
|
|
packages = []
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if isinstance(args, TestConfig):
|
|
|
|
if args.coverage:
|
2017-03-08 09:47:21 +01:00
|
|
|
packages.append('coverage')
|
2017-03-02 21:36:46 +01:00
|
|
|
if args.junit:
|
2017-03-08 09:47:21 +01:00
|
|
|
packages.append('junit-xml')
|
|
|
|
|
2018-09-18 17:37:14 +02:00
|
|
|
if not python_version:
|
|
|
|
python_version = args.python_version
|
|
|
|
|
2020-12-17 23:59:53 +01:00
|
|
|
python = find_python(python_version)
|
|
|
|
pip = generate_pip_command(python)
|
2017-10-26 09:21:46 +02:00
|
|
|
|
2020-06-10 18:39:25 +02:00
|
|
|
# skip packages which have aleady been installed for python_version
|
|
|
|
|
|
|
|
try:
|
|
|
|
package_cache = install_command_requirements.package_cache
|
|
|
|
except AttributeError:
|
|
|
|
package_cache = install_command_requirements.package_cache = {}
|
|
|
|
|
|
|
|
installed_packages = package_cache.setdefault(python_version, set())
|
|
|
|
skip_packages = [package for package in packages if package in installed_packages]
|
|
|
|
|
|
|
|
for package in skip_packages:
|
|
|
|
packages.remove(package)
|
|
|
|
|
|
|
|
installed_packages.update(packages)
|
2020-03-29 06:33:13 +02:00
|
|
|
|
2020-06-09 22:38:36 +02:00
|
|
|
if args.command != 'sanity':
|
2020-12-17 23:59:53 +01:00
|
|
|
install_cryptography(args, python, python_version, pip)
|
2020-03-29 06:33:13 +02:00
|
|
|
|
2020-06-09 22:38:36 +02:00
|
|
|
commands = [generate_pip_install(pip, args.command, packages=packages, context=context)]
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2021-05-07 02:59:07 +02:00
|
|
|
if extra_requirements:
|
|
|
|
for extra_requirement in extra_requirements:
|
|
|
|
commands.append(generate_pip_install(pip, extra_requirement))
|
2017-10-26 09:21:46 +02:00
|
|
|
|
|
|
|
commands = [cmd for cmd in commands if cmd]
|
2017-05-05 10:23:00 +02:00
|
|
|
|
2020-06-10 18:39:25 +02:00
|
|
|
if not commands:
|
|
|
|
return # no need to detect changes or run pip check since we are not making any changes
|
|
|
|
|
2017-10-20 17:48:01 +02:00
|
|
|
# only look for changes when more than one requirements file is needed
|
|
|
|
detect_pip_changes = len(commands) > 1
|
2017-03-08 09:47:21 +01:00
|
|
|
|
2017-10-20 17:48:01 +02:00
|
|
|
# first pass to install requirements, changes expected unless environment is already set up
|
2020-06-10 18:39:25 +02:00
|
|
|
install_ansible_test_requirements(args, pip)
|
2017-10-26 09:21:46 +02:00
|
|
|
changes = run_pip_commands(args, pip, commands, detect_pip_changes)
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2019-04-06 07:34:38 +02:00
|
|
|
if changes:
|
|
|
|
# second pass to check for conflicts in requirements, changes are not expected here
|
|
|
|
changes = run_pip_commands(args, pip, commands, detect_pip_changes)
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2019-04-06 07:34:38 +02:00
|
|
|
if changes:
|
|
|
|
raise ApplicationError('Conflicts detected in requirements. The following commands reported changes during verification:\n%s' %
|
2019-05-24 22:10:33 +02:00
|
|
|
'\n'.join((' '.join(cmd_quote(c) for c in cmd) for cmd in changes)))
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2020-01-28 02:19:42 +01:00
|
|
|
if args.pip_check:
|
|
|
|
# ask pip to check for conflicts between installed packages
|
|
|
|
try:
|
|
|
|
run_command(args, pip + ['check', '--disable-pip-version-check'], capture=True)
|
|
|
|
except SubprocessError as ex:
|
|
|
|
if ex.stderr.strip() == 'ERROR: unknown command "check"':
|
|
|
|
display.warning('Cannot check pip requirements for conflicts because "pip check" is not supported.')
|
|
|
|
else:
|
|
|
|
raise
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2020-06-10 18:39:25 +02:00
|
|
|
if enable_pyyaml_check:
|
|
|
|
# pyyaml may have been one of the requirements that was installed, so perform an optional check for it
|
|
|
|
check_pyyaml(args, python_version, required=False)
|
|
|
|
|
|
|
|
|
|
|
|
def install_ansible_test_requirements(args, pip): # type: (EnvironmentConfig, t.List[str]) -> None
|
|
|
|
"""Install requirements for ansible-test for the given pip if not already installed."""
|
|
|
|
try:
|
|
|
|
installed = install_command_requirements.installed
|
|
|
|
except AttributeError:
|
|
|
|
installed = install_command_requirements.installed = set()
|
|
|
|
|
|
|
|
if tuple(pip) in installed:
|
|
|
|
return
|
|
|
|
|
|
|
|
# make sure basic ansible-test requirements are met, including making sure that pip is recent enough to support constraints
|
|
|
|
# virtualenvs created by older distributions may include very old pip versions, such as those created in the centos6 test container (pip 6.0.8)
|
|
|
|
run_command(args, generate_pip_install(pip, 'ansible-test', use_constraints=False))
|
|
|
|
|
|
|
|
installed.add(tuple(pip))
|
|
|
|
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
def run_pip_commands(args, pip, commands, detect_pip_changes=False):
|
2017-10-20 17:48:01 +02:00
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
2018-03-14 19:35:59 +01:00
|
|
|
:type pip: list[str]
|
2017-10-20 17:48:01 +02:00
|
|
|
:type commands: list[list[str]]
|
|
|
|
:type detect_pip_changes: bool
|
|
|
|
:rtype: list[list[str]]
|
|
|
|
"""
|
|
|
|
changes = []
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
after_list = pip_list(args, pip) if detect_pip_changes else None
|
2017-03-02 21:36:46 +01:00
|
|
|
|
2017-10-20 17:48:01 +02:00
|
|
|
for cmd in commands:
|
|
|
|
if not cmd:
|
|
|
|
continue
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-10-20 17:48:01 +02:00
|
|
|
before_list = after_list
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-04-06 07:34:38 +02:00
|
|
|
run_command(args, cmd)
|
2017-10-20 17:48:01 +02:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
after_list = pip_list(args, pip) if detect_pip_changes else None
|
2017-10-20 17:48:01 +02:00
|
|
|
|
|
|
|
if before_list != after_list:
|
|
|
|
changes.append(cmd)
|
|
|
|
|
|
|
|
return changes
|
|
|
|
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
def pip_list(args, pip):
|
2017-10-20 17:48:01 +02:00
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
2018-03-14 19:35:59 +01:00
|
|
|
:type pip: list[str]
|
2017-10-20 17:48:01 +02:00
|
|
|
:rtype: str
|
|
|
|
"""
|
2019-07-15 22:47:16 +02:00
|
|
|
stdout = run_command(args, pip + ['list'], capture=True)[0]
|
2017-10-20 17:48:01 +02:00
|
|
|
return stdout
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2020-06-09 22:38:36 +02:00
|
|
|
def generate_pip_install(pip, command, packages=None, constraints=None, use_constraints=True, context=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2018-03-14 19:35:59 +01:00
|
|
|
:type pip: list[str]
|
2016-11-30 06:21:53 +01:00
|
|
|
:type command: str
|
2017-03-08 09:47:21 +01:00
|
|
|
:type packages: list[str] | None
|
2020-03-29 06:33:13 +02:00
|
|
|
:type constraints: str | None
|
|
|
|
:type use_constraints: bool
|
2020-06-09 22:38:36 +02:00
|
|
|
:type context: str | None
|
2017-02-14 03:49:36 +01:00
|
|
|
:rtype: list[str] | None
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2020-03-29 06:33:13 +02:00
|
|
|
constraints = constraints or os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', 'constraints.txt')
|
2020-06-09 22:38:36 +02:00
|
|
|
requirements = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', '%s.txt' % ('%s.%s' % (command, context) if context else command))
|
2020-10-08 17:51:26 +02:00
|
|
|
content_constraints = None
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-03-08 09:47:21 +01:00
|
|
|
options = []
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
|
|
|
options += ['-r', requirements]
|
2017-03-08 09:47:21 +01:00
|
|
|
|
2020-06-09 22:38:36 +02:00
|
|
|
if command == 'sanity' and data_context().content.is_ansible:
|
|
|
|
requirements = os.path.join(data_context().content.sanity_path, 'code-smell', '%s.requirements.txt' % context)
|
|
|
|
|
|
|
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
|
|
|
options += ['-r', requirements]
|
2019-08-07 16:13:28 +02:00
|
|
|
|
2019-09-12 11:00:33 +02:00
|
|
|
if command == 'units':
|
|
|
|
requirements = os.path.join(data_context().content.unit_path, 'requirements.txt')
|
|
|
|
|
|
|
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
|
|
|
options += ['-r', requirements]
|
|
|
|
|
2020-10-08 17:51:26 +02:00
|
|
|
content_constraints = os.path.join(data_context().content.unit_path, 'constraints.txt')
|
|
|
|
|
2019-09-12 11:00:33 +02:00
|
|
|
if command in ('integration', 'windows-integration', 'network-integration'):
|
|
|
|
requirements = os.path.join(data_context().content.integration_path, 'requirements.txt')
|
|
|
|
|
|
|
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
|
|
|
options += ['-r', requirements]
|
|
|
|
|
|
|
|
requirements = os.path.join(data_context().content.integration_path, '%s.requirements.txt' % command)
|
|
|
|
|
|
|
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
|
|
|
options += ['-r', requirements]
|
|
|
|
|
2020-10-08 17:51:26 +02:00
|
|
|
content_constraints = os.path.join(data_context().content.integration_path, 'constraints.txt')
|
|
|
|
|
|
|
|
if command.startswith('integration.cloud.'):
|
|
|
|
content_constraints = os.path.join(data_context().content.integration_path, 'constraints.txt')
|
|
|
|
|
2017-03-08 09:47:21 +01:00
|
|
|
if packages:
|
|
|
|
options += packages
|
|
|
|
|
|
|
|
if not options:
|
2016-11-30 06:21:53 +01:00
|
|
|
return None
|
|
|
|
|
2020-03-29 06:33:13 +02:00
|
|
|
if use_constraints:
|
2020-10-08 17:51:26 +02:00
|
|
|
if content_constraints and os.path.exists(content_constraints) and os.path.getsize(content_constraints):
|
|
|
|
# listing content constraints first gives them priority over constraints provided by ansible-test
|
|
|
|
options.extend(['-c', content_constraints])
|
|
|
|
|
2020-03-29 06:33:13 +02:00
|
|
|
options.extend(['-c', constraints])
|
|
|
|
|
|
|
|
return pip + ['install', '--disable-pip-version-check'] + options
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
def parse_inventory(args, inventory_path): # type: (IntegrationConfig, str) -> t.Dict[str, t.Any]
|
|
|
|
"""Return a dict parsed from the given inventory file."""
|
|
|
|
cmd = ['ansible-inventory', '-i', inventory_path, '--list']
|
|
|
|
env = ansible_environment(args)
|
|
|
|
inventory = json.loads(intercept_command(args, cmd, '', env, capture=True, disable_coverage=True)[0])
|
|
|
|
return inventory
|
2018-05-09 18:24:39 +02:00
|
|
|
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
def get_hosts(inventory, group_name): # type: (t.Dict[str, t.Any], str) -> t.Dict[str, t.Dict[str, t.Any]]
|
|
|
|
"""Return a dict of hosts from the specified group in the given inventory."""
|
|
|
|
hostvars = inventory.get('_meta', {}).get('hostvars', {})
|
|
|
|
group = inventory.get(group_name, {})
|
|
|
|
host_names = group.get('hosts', [])
|
|
|
|
hosts = dict((name, hostvars[name]) for name in host_names)
|
|
|
|
return hosts
|
2018-05-09 18:24:39 +02:00
|
|
|
|
|
|
|
|
2021-04-06 01:48:59 +02:00
|
|
|
def run_pypi_proxy(args): # type: (EnvironmentConfig) -> t.Tuple[t.Optional[str], t.Optional[str]]
|
|
|
|
"""Run a PyPI proxy container, returning the container ID and proxy endpoint."""
|
|
|
|
use_proxy = False
|
|
|
|
|
|
|
|
if args.docker_raw == 'centos6':
|
|
|
|
use_proxy = True # python 2.6 is the only version available
|
|
|
|
|
|
|
|
if args.docker_raw == 'default':
|
|
|
|
if args.python == '2.6':
|
|
|
|
use_proxy = True # python 2.6 requested
|
|
|
|
elif not args.python and isinstance(args, (SanityConfig, UnitsConfig, ShellConfig)):
|
|
|
|
use_proxy = True # multiple versions (including python 2.6) can be used
|
|
|
|
|
|
|
|
if args.docker_raw and args.pypi_proxy:
|
|
|
|
use_proxy = True # manual override to force proxy usage
|
|
|
|
|
|
|
|
if not use_proxy:
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
proxy_image = 'quay.io/ansible/pypi-test-container:1.0.0'
|
|
|
|
port = 3141
|
|
|
|
|
|
|
|
options = [
|
|
|
|
'--detach',
|
|
|
|
]
|
|
|
|
|
|
|
|
docker_pull(args, proxy_image)
|
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
container_id = docker_run(args, proxy_image, options=options)
|
2021-04-06 01:48:59 +02:00
|
|
|
|
2021-02-02 20:47:38 +01:00
|
|
|
container = docker_inspect(args, container_id)
|
|
|
|
|
|
|
|
container_ip = container.get_ip_address()
|
|
|
|
|
|
|
|
if not container_ip:
|
|
|
|
raise Exception('PyPI container IP not available.')
|
2021-04-06 01:48:59 +02:00
|
|
|
|
|
|
|
endpoint = 'http://%s:%d/root/pypi/+simple/' % (container_ip, port)
|
|
|
|
|
|
|
|
return container_id, endpoint
|
|
|
|
|
|
|
|
|
|
|
|
def configure_pypi_proxy(args): # type: (CommonConfig) -> None
|
|
|
|
"""Configure the environment to use a PyPI proxy, if present."""
|
|
|
|
if not isinstance(args, EnvironmentConfig):
|
|
|
|
return
|
|
|
|
|
|
|
|
if args.pypi_endpoint:
|
|
|
|
configure_pypi_block_access()
|
|
|
|
configure_pypi_proxy_pip(args)
|
|
|
|
configure_pypi_proxy_easy_install(args)
|
|
|
|
|
|
|
|
|
|
|
|
def configure_pypi_block_access(): # type: () -> None
|
|
|
|
"""Block direct access to PyPI to ensure proxy configurations are always used."""
|
|
|
|
if os.getuid() != 0:
|
|
|
|
display.warning('Skipping custom hosts block for PyPI for non-root user.')
|
|
|
|
return
|
|
|
|
|
|
|
|
hosts_path = '/etc/hosts'
|
|
|
|
hosts_block = '''
|
|
|
|
127.0.0.1 pypi.org pypi.python.org files.pythonhosted.org
|
|
|
|
'''
|
|
|
|
|
|
|
|
def hosts_cleanup():
|
|
|
|
display.info('Removing custom PyPI hosts entries: %s' % hosts_path, verbosity=1)
|
|
|
|
|
|
|
|
with open(hosts_path) as hosts_file_read:
|
|
|
|
content = hosts_file_read.read()
|
|
|
|
|
|
|
|
content = content.replace(hosts_block, '')
|
|
|
|
|
|
|
|
with open(hosts_path, 'w') as hosts_file_write:
|
|
|
|
hosts_file_write.write(content)
|
|
|
|
|
|
|
|
display.info('Injecting custom PyPI hosts entries: %s' % hosts_path, verbosity=1)
|
|
|
|
display.info('Config: %s\n%s' % (hosts_path, hosts_block), verbosity=3)
|
|
|
|
|
|
|
|
with open(hosts_path, 'a') as hosts_file:
|
|
|
|
hosts_file.write(hosts_block)
|
|
|
|
|
|
|
|
atexit.register(hosts_cleanup)
|
|
|
|
|
|
|
|
|
|
|
|
def configure_pypi_proxy_pip(args): # type: (EnvironmentConfig) -> None
|
|
|
|
"""Configure a custom index for pip based installs."""
|
|
|
|
pypi_hostname = urlparse(args.pypi_endpoint)[1].split(':')[0]
|
|
|
|
|
|
|
|
pip_conf_path = os.path.expanduser('~/.pip/pip.conf')
|
|
|
|
pip_conf = '''
|
|
|
|
[global]
|
|
|
|
index-url = {0}
|
|
|
|
trusted-host = {1}
|
|
|
|
'''.format(args.pypi_endpoint, pypi_hostname).strip()
|
|
|
|
|
|
|
|
def pip_conf_cleanup():
|
|
|
|
display.info('Removing custom PyPI config: %s' % pip_conf_path, verbosity=1)
|
|
|
|
os.remove(pip_conf_path)
|
|
|
|
|
|
|
|
if os.path.exists(pip_conf_path):
|
|
|
|
raise ApplicationError('Refusing to overwrite existing file: %s' % pip_conf_path)
|
|
|
|
|
|
|
|
display.info('Injecting custom PyPI config: %s' % pip_conf_path, verbosity=1)
|
|
|
|
display.info('Config: %s\n%s' % (pip_conf_path, pip_conf), verbosity=3)
|
|
|
|
|
|
|
|
write_text_file(pip_conf_path, pip_conf, True)
|
|
|
|
atexit.register(pip_conf_cleanup)
|
|
|
|
|
|
|
|
|
|
|
|
def configure_pypi_proxy_easy_install(args): # type: (EnvironmentConfig) -> None
|
|
|
|
"""Configure a custom index for easy_install based installs."""
|
|
|
|
pydistutils_cfg_path = os.path.expanduser('~/.pydistutils.cfg')
|
|
|
|
pydistutils_cfg = '''
|
|
|
|
[easy_install]
|
|
|
|
index_url = {0}
|
|
|
|
'''.format(args.pypi_endpoint).strip()
|
|
|
|
|
|
|
|
if os.path.exists(pydistutils_cfg_path):
|
|
|
|
raise ApplicationError('Refusing to overwrite existing file: %s' % pydistutils_cfg_path)
|
|
|
|
|
|
|
|
def pydistutils_cfg_cleanup():
|
|
|
|
display.info('Removing custom PyPI config: %s' % pydistutils_cfg_path, verbosity=1)
|
|
|
|
os.remove(pydistutils_cfg_path)
|
|
|
|
|
|
|
|
display.info('Injecting custom PyPI config: %s' % pydistutils_cfg_path, verbosity=1)
|
|
|
|
display.info('Config: %s\n%s' % (pydistutils_cfg_path, pydistutils_cfg), verbosity=3)
|
|
|
|
|
|
|
|
write_text_file(pydistutils_cfg_path, pydistutils_cfg, True)
|
|
|
|
atexit.register(pydistutils_cfg_cleanup)
|
|
|
|
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
def get_changes_filter(args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
:rtype: list[str]
|
|
|
|
"""
|
|
|
|
paths = detect_changes(args)
|
|
|
|
|
2018-04-13 01:15:28 +02:00
|
|
|
if not args.metadata.change_description:
|
|
|
|
if paths:
|
|
|
|
changes = categorize_changes(args, paths, args.command)
|
|
|
|
else:
|
|
|
|
changes = ChangeDescription()
|
|
|
|
|
|
|
|
args.metadata.change_description = changes
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if paths is None:
|
|
|
|
return [] # change detection not enabled, do not filter targets
|
|
|
|
|
|
|
|
if not paths:
|
|
|
|
raise NoChangesDetected()
|
|
|
|
|
2018-04-13 01:15:28 +02:00
|
|
|
if args.metadata.change_description.targets is None:
|
2016-11-30 06:21:53 +01:00
|
|
|
raise NoTestsForChanges()
|
|
|
|
|
2018-04-13 01:15:28 +02:00
|
|
|
return args.metadata.change_description.targets
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def detect_changes(args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
:rtype: list[str] | None
|
|
|
|
"""
|
2020-04-26 03:55:39 +02:00
|
|
|
if args.changed:
|
|
|
|
paths = get_ci_provider().detect_changes(args)
|
2016-11-30 06:21:53 +01:00
|
|
|
elif args.changed_from or args.changed_path:
|
|
|
|
paths = args.changed_path or []
|
|
|
|
if args.changed_from:
|
2020-02-04 20:21:53 +01:00
|
|
|
paths += read_text_file(args.changed_from).splitlines()
|
2016-11-30 06:21:53 +01:00
|
|
|
else:
|
|
|
|
return None # change detection not enabled
|
|
|
|
|
2017-09-07 01:40:04 +02:00
|
|
|
if paths is None:
|
|
|
|
return None # act as though change detection not enabled, do not filter targets
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
display.info('Detected changes in %d file(s).' % len(paths))
|
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
display.info(path, verbosity=1)
|
|
|
|
|
|
|
|
return paths
|
|
|
|
|
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
def get_python_version(args, configs, name):
|
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
|
|
|
:type configs: dict[str, dict[str, str]]
|
|
|
|
:type name: str
|
|
|
|
"""
|
|
|
|
config = configs.get(name, {})
|
|
|
|
config_python = config.get('python')
|
|
|
|
|
|
|
|
if not config or not config_python:
|
|
|
|
if args.python:
|
|
|
|
return args.python
|
|
|
|
|
|
|
|
display.warning('No Python version specified. '
|
|
|
|
'Use completion config or the --python option to specify one.', unique=True)
|
|
|
|
|
|
|
|
return '' # failure to provide a version may result in failures or reduced functionality later
|
|
|
|
|
|
|
|
supported_python_versions = config_python.split(',')
|
|
|
|
default_python_version = supported_python_versions[0]
|
|
|
|
|
|
|
|
if args.python and args.python not in supported_python_versions:
|
|
|
|
raise ApplicationError('Python %s is not supported by %s. Supported Python version(s) are: %s' % (
|
|
|
|
args.python, name, ', '.join(sorted(supported_python_versions))))
|
|
|
|
|
|
|
|
python_version = args.python or default_python_version
|
|
|
|
|
|
|
|
return python_version
|
|
|
|
|
|
|
|
|
|
|
|
def get_python_interpreter(args, configs, name):
|
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
|
|
|
:type configs: dict[str, dict[str, str]]
|
|
|
|
:type name: str
|
|
|
|
"""
|
|
|
|
if args.python_interpreter:
|
|
|
|
return args.python_interpreter
|
|
|
|
|
|
|
|
config = configs.get(name, {})
|
|
|
|
|
|
|
|
if not config:
|
|
|
|
if args.python:
|
|
|
|
guess = 'python%s' % args.python
|
|
|
|
else:
|
|
|
|
guess = 'python'
|
|
|
|
|
|
|
|
display.warning('Using "%s" as the Python interpreter. '
|
|
|
|
'Use completion config or the --python-interpreter option to specify the path.' % guess, unique=True)
|
|
|
|
|
|
|
|
return guess
|
|
|
|
|
|
|
|
python_version = get_python_version(args, configs, name)
|
|
|
|
|
|
|
|
python_dir = config.get('python_dir', '/usr/bin')
|
|
|
|
python_interpreter = os.path.join(python_dir, 'python%s' % python_version)
|
|
|
|
python_interpreter = config.get('python%s' % python_version, python_interpreter)
|
|
|
|
|
|
|
|
return python_interpreter
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NoChangesDetected(ApplicationWarning):
|
|
|
|
"""Exception when change detection was performed, but no changes were found."""
|
|
|
|
def __init__(self):
|
|
|
|
super(NoChangesDetected, self).__init__('No changes detected.')
|
|
|
|
|
|
|
|
|
|
|
|
class NoTestsForChanges(ApplicationWarning):
|
|
|
|
"""Exception when changes detected, but no tests trigger as a result."""
|
|
|
|
def __init__(self):
|
|
|
|
super(NoTestsForChanges, self).__init__('No tests found for detected changes.')
|
|
|
|
|
|
|
|
|
|
|
|
class Delegate(Exception):
|
|
|
|
"""Trigger command delegation."""
|
2021-02-02 20:47:38 +01:00
|
|
|
def __init__(self, exclude=None, require=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
:type exclude: list[str] | None
|
|
|
|
:type require: list[str] | None
|
|
|
|
"""
|
|
|
|
super(Delegate, self).__init__()
|
|
|
|
|
|
|
|
self.exclude = exclude or []
|
|
|
|
self.require = require or []
|
|
|
|
|
|
|
|
|
|
|
|
class AllTargetsSkipped(ApplicationWarning):
|
|
|
|
"""All targets skipped."""
|
|
|
|
def __init__(self):
|
|
|
|
super(AllTargetsSkipped, self).__init__('All targets skipped.')
|