mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
600 lines
19 KiB
Python
600 lines
19 KiB
Python
8 years ago
|
"""Delegate test execution to another environment."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
|
import os
|
||
8 years ago
|
import re
|
||
8 years ago
|
import sys
|
||
8 years ago
|
import tempfile
|
||
8 years ago
|
|
||
5 years ago
|
from .executor import (
|
||
8 years ago
|
SUPPORTED_PYTHON_VERSIONS,
|
||
7 years ago
|
HTTPTESTER_HOSTS,
|
||
8 years ago
|
create_shell_command,
|
||
7 years ago
|
run_httptester,
|
||
|
start_httptester,
|
||
6 years ago
|
get_python_interpreter,
|
||
|
get_python_version,
|
||
|
get_docker_completion,
|
||
|
get_remote_completion,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .config import (
|
||
8 years ago
|
TestConfig,
|
||
8 years ago
|
EnvironmentConfig,
|
||
|
IntegrationConfig,
|
||
|
ShellConfig,
|
||
|
SanityConfig,
|
||
|
UnitsConfig,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .core_ci import (
|
||
8 years ago
|
AnsibleCoreCI,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .manage_ci import (
|
||
8 years ago
|
ManagePosixCI,
|
||
6 years ago
|
ManageWindowsCI,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util import (
|
||
8 years ago
|
ApplicationError,
|
||
8 years ago
|
common_environment,
|
||
|
pass_vars,
|
||
7 years ago
|
display,
|
||
5 years ago
|
ANSIBLE_ROOT,
|
||
|
ANSIBLE_TEST_DATA_ROOT,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util_common import (
|
||
5 years ago
|
run_command,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .docker_util import (
|
||
8 years ago
|
docker_exec,
|
||
|
docker_get,
|
||
|
docker_pull,
|
||
|
docker_put,
|
||
|
docker_rm,
|
||
|
docker_run,
|
||
7 years ago
|
docker_available,
|
||
6 years ago
|
docker_network_disconnect,
|
||
|
get_docker_networks,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .cloud import (
|
||
8 years ago
|
get_cloud_providers,
|
||
|
)
|
||
8 years ago
|
|
||
5 years ago
|
from .target import (
|
||
7 years ago
|
IntegrationTarget,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .data import (
|
||
5 years ago
|
data_context,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .payload import (
|
||
5 years ago
|
create_payload,
|
||
|
)
|
||
|
|
||
8 years ago
|
|
||
6 years ago
|
def check_delegation_args(args):
|
||
|
"""
|
||
|
:type args: CommonConfig
|
||
|
"""
|
||
|
if not isinstance(args, EnvironmentConfig):
|
||
|
return
|
||
|
|
||
|
if args.docker:
|
||
|
get_python_version(args, get_docker_completion(), args.docker_raw)
|
||
|
elif args.remote:
|
||
|
get_python_version(args, get_remote_completion(), args.remote)
|
||
|
|
||
|
|
||
7 years ago
|
def delegate(args, exclude, require, integration_targets):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget]
|
||
8 years ago
|
:rtype: bool
|
||
|
"""
|
||
|
if isinstance(args, TestConfig):
|
||
5 years ago
|
with tempfile.NamedTemporaryFile(prefix='metadata-', suffix='.json', dir=data_context().content.root) as metadata_fd:
|
||
8 years ago
|
args.metadata_path = os.path.basename(metadata_fd.name)
|
||
|
args.metadata.to_file(args.metadata_path)
|
||
|
|
||
|
try:
|
||
7 years ago
|
return delegate_command(args, exclude, require, integration_targets)
|
||
8 years ago
|
finally:
|
||
|
args.metadata_path = None
|
||
|
else:
|
||
7 years ago
|
return delegate_command(args, exclude, require, integration_targets)
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def delegate_command(args, exclude, require, integration_targets):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget]
|
||
8 years ago
|
:rtype: bool
|
||
8 years ago
|
"""
|
||
|
if args.tox:
|
||
7 years ago
|
delegate_tox(args, exclude, require, integration_targets)
|
||
8 years ago
|
return True
|
||
|
|
||
|
if args.docker:
|
||
7 years ago
|
delegate_docker(args, exclude, require, integration_targets)
|
||
8 years ago
|
return True
|
||
|
|
||
|
if args.remote:
|
||
7 years ago
|
delegate_remote(args, exclude, require, integration_targets)
|
||
8 years ago
|
return True
|
||
|
|
||
|
return False
|
||
|
|
||
|
|
||
7 years ago
|
def delegate_tox(args, exclude, require, integration_targets):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget]
|
||
8 years ago
|
"""
|
||
|
if args.python:
|
||
6 years ago
|
versions = (args.python_version,)
|
||
8 years ago
|
|
||
7 years ago
|
if args.python_version not in SUPPORTED_PYTHON_VERSIONS:
|
||
|
raise ApplicationError('tox does not support Python version %s' % args.python_version)
|
||
8 years ago
|
else:
|
||
|
versions = SUPPORTED_PYTHON_VERSIONS
|
||
|
|
||
7 years ago
|
if args.httptester:
|
||
|
needs_httptester = sorted(target.name for target in integration_targets if 'needs/httptester/' in target.aliases)
|
||
|
|
||
|
if needs_httptester:
|
||
|
display.warning('Use --docker or --remote to enable httptester for tests marked "needs/httptester": %s' % ', '.join(needs_httptester))
|
||
|
|
||
8 years ago
|
options = {
|
||
|
'--tox': args.tox_args,
|
||
8 years ago
|
'--tox-sitepackages': 0,
|
||
8 years ago
|
}
|
||
|
|
||
|
for version in versions:
|
||
5 years ago
|
tox = ['tox', '-c', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'tox.ini'), '-e', 'py' + version.replace('.', '')]
|
||
8 years ago
|
|
||
|
if args.tox_sitepackages:
|
||
|
tox.append('--sitepackages')
|
||
|
|
||
|
tox.append('--')
|
||
|
|
||
5 years ago
|
cmd = generate_command(args, None, ANSIBLE_ROOT, data_context().content.root, options, exclude, require)
|
||
8 years ago
|
|
||
|
if not args.python:
|
||
|
cmd += ['--python', version]
|
||
|
|
||
6 years ago
|
# 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]
|
||
|
|
||
8 years ago
|
if isinstance(args, TestConfig):
|
||
|
if args.coverage and not args.coverage_label:
|
||
|
cmd += ['--coverage-label', 'tox-%s' % version]
|
||
|
|
||
8 years ago
|
env = common_environment()
|
||
|
|
||
|
# temporary solution to permit ansible-test delegated to tox to provision remote resources
|
||
|
optional = (
|
||
|
'SHIPPABLE',
|
||
|
'SHIPPABLE_BUILD_ID',
|
||
|
'SHIPPABLE_JOB_NUMBER',
|
||
|
)
|
||
|
|
||
|
env.update(pass_vars(required=[], optional=optional))
|
||
|
|
||
|
run_command(args, tox + cmd, env=env)
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def delegate_docker(args, exclude, require, integration_targets):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget]
|
||
8 years ago
|
"""
|
||
|
test_image = args.docker
|
||
|
privileged = args.docker_privileged
|
||
|
|
||
7 years ago
|
if isinstance(args, ShellConfig):
|
||
|
use_httptester = args.httptester
|
||
|
else:
|
||
|
use_httptester = args.httptester and any('needs/httptester/' in target.aliases for target in integration_targets)
|
||
|
|
||
|
if use_httptester:
|
||
|
docker_pull(args, args.httptester)
|
||
8 years ago
|
|
||
|
docker_pull(args, test_image)
|
||
|
|
||
7 years ago
|
httptester_id = None
|
||
8 years ago
|
test_id = None
|
||
|
|
||
|
options = {
|
||
|
'--docker': 1,
|
||
|
'--docker-privileged': 0,
|
||
|
'--docker-util': 1,
|
||
|
}
|
||
|
|
||
6 years ago
|
python_interpreter = get_python_interpreter(args, get_docker_completion(), args.docker_raw)
|
||
5 years ago
|
|
||
|
install_root = '/root/ansible'
|
||
5 years ago
|
|
||
|
if data_context().content.collection:
|
||
|
content_root = os.path.join(install_root, data_context().content.collection.directory)
|
||
|
else:
|
||
|
content_root = install_root
|
||
5 years ago
|
|
||
|
cmd = generate_command(args, python_interpreter, install_root, content_root, options, exclude, require)
|
||
8 years ago
|
|
||
8 years ago
|
if isinstance(args, TestConfig):
|
||
|
if args.coverage and not args.coverage_label:
|
||
6 years ago
|
image_label = args.docker_raw
|
||
8 years ago
|
image_label = re.sub('[^a-zA-Z0-9]+', '-', image_label)
|
||
|
cmd += ['--coverage-label', 'docker-%s' % image_label]
|
||
|
|
||
8 years ago
|
if isinstance(args, IntegrationConfig):
|
||
|
if not args.allow_destructive:
|
||
|
cmd.append('--allow-destructive')
|
||
|
|
||
8 years ago
|
cmd_options = []
|
||
|
|
||
8 years ago
|
if isinstance(args, ShellConfig) or (isinstance(args, IntegrationConfig) and args.debug_strategy):
|
||
8 years ago
|
cmd_options.append('-it')
|
||
|
|
||
8 years ago
|
with tempfile.NamedTemporaryFile(prefix='ansible-source-', suffix='.tgz') as local_source_fd:
|
||
|
try:
|
||
5 years ago
|
create_payload(args, local_source_fd.name)
|
||
8 years ago
|
|
||
7 years ago
|
if use_httptester:
|
||
|
httptester_id = run_httptester(args)
|
||
8 years ago
|
else:
|
||
7 years ago
|
httptester_id = None
|
||
8 years ago
|
|
||
|
test_options = [
|
||
|
'--detach',
|
||
|
'--volume', '/sys/fs/cgroup:/sys/fs/cgroup:ro',
|
||
|
'--privileged=%s' % str(privileged).lower(),
|
||
8 years ago
|
]
|
||
|
|
||
7 years ago
|
if args.docker_memory:
|
||
|
test_options.extend([
|
||
|
'--memory=%d' % args.docker_memory,
|
||
7 years ago
|
'--memory-swap=%d' % args.docker_memory,
|
||
7 years ago
|
])
|
||
|
|
||
8 years ago
|
docker_socket = '/var/run/docker.sock'
|
||
|
|
||
6 years ago
|
if args.docker_seccomp != 'default':
|
||
|
test_options += ['--security-opt', 'seccomp=%s' % args.docker_seccomp]
|
||
|
|
||
8 years ago
|
if os.path.exists(docker_socket):
|
||
|
test_options += ['--volume', '%s:%s' % (docker_socket, docker_socket)]
|
||
|
|
||
7 years ago
|
if httptester_id:
|
||
|
test_options += ['--env', 'HTTPTESTER=1']
|
||
|
|
||
|
for host in HTTPTESTER_HOSTS:
|
||
|
test_options += ['--link', '%s:%s' % (httptester_id, host)]
|
||
8 years ago
|
|
||
7 years ago
|
if isinstance(args, IntegrationConfig):
|
||
8 years ago
|
cloud_platforms = get_cloud_providers(args)
|
||
8 years ago
|
|
||
8 years ago
|
for cloud_platform in cloud_platforms:
|
||
|
test_options += cloud_platform.get_docker_run_options()
|
||
8 years ago
|
|
||
5 years ago
|
test_id = docker_run(args, test_image, options=test_options)[0]
|
||
8 years ago
|
|
||
8 years ago
|
if args.explain:
|
||
|
test_id = 'test_id'
|
||
|
else:
|
||
|
test_id = test_id.strip()
|
||
8 years ago
|
|
||
8 years ago
|
# write temporary files to /root since /tmp isn't ready immediately on container start
|
||
5 years ago
|
docker_put(args, test_id, os.path.join(ANSIBLE_TEST_DATA_ROOT, 'setup', 'docker.sh'), '/root/docker.sh')
|
||
8 years ago
|
docker_exec(args, test_id, ['/bin/bash', '/root/docker.sh'])
|
||
|
docker_put(args, test_id, local_source_fd.name, '/root/ansible.tgz')
|
||
|
docker_exec(args, test_id, ['mkdir', '/root/ansible'])
|
||
|
docker_exec(args, test_id, ['tar', 'oxzf', '/root/ansible.tgz', '-C', '/root/ansible'])
|
||
8 years ago
|
|
||
8 years ago
|
# docker images are only expected to have a single python version available
|
||
|
if isinstance(args, UnitsConfig) and not args.python:
|
||
|
cmd += ['--python', 'default']
|
||
|
|
||
6 years ago
|
# run unit tests unprivileged to prevent stray writes to the source tree
|
||
6 years ago
|
# also disconnect from the network once requirements have been installed
|
||
6 years ago
|
if isinstance(args, UnitsConfig):
|
||
|
writable_dirs = [
|
||
5 years ago
|
os.path.join(install_root, '.pytest_cache'),
|
||
6 years ago
|
]
|
||
|
|
||
5 years ago
|
if content_root != install_root:
|
||
|
writable_dirs.append(os.path.join(content_root, 'test/results/junit'))
|
||
|
writable_dirs.append(os.path.join(content_root, 'test/results/coverage'))
|
||
|
|
||
6 years ago
|
docker_exec(args, test_id, ['mkdir', '-p'] + writable_dirs)
|
||
|
docker_exec(args, test_id, ['chmod', '777'] + writable_dirs)
|
||
|
|
||
5 years ago
|
if content_root == install_root:
|
||
|
docker_exec(args, test_id, ['find', os.path.join(content_root, 'test/results/'), '-type', 'd', '-exec', 'chmod', '777', '{}', '+'])
|
||
6 years ago
|
|
||
|
docker_exec(args, test_id, ['chmod', '755', '/root'])
|
||
5 years ago
|
docker_exec(args, test_id, ['chmod', '644', os.path.join(content_root, args.metadata_path)])
|
||
6 years ago
|
|
||
|
docker_exec(args, test_id, ['useradd', 'pytest', '--create-home'])
|
||
|
|
||
|
docker_exec(args, test_id, cmd + ['--requirements-mode', 'only'], options=cmd_options)
|
||
|
|
||
6 years ago
|
networks = get_docker_networks(args, test_id)
|
||
|
|
||
|
for network in networks:
|
||
|
docker_network_disconnect(args, test_id, network)
|
||
|
|
||
6 years ago
|
cmd += ['--requirements-mode', 'skip']
|
||
|
|
||
|
cmd_options += ['--user', 'pytest']
|
||
|
|
||
8 years ago
|
try:
|
||
|
docker_exec(args, test_id, cmd, options=cmd_options)
|
||
|
finally:
|
||
|
with tempfile.NamedTemporaryFile(prefix='ansible-result-', suffix='.tgz') as local_result_fd:
|
||
5 years ago
|
docker_exec(args, test_id, ['tar', 'czf', '/root/results.tgz', '-C', os.path.join(content_root, 'test'), 'results'])
|
||
8 years ago
|
docker_get(args, test_id, '/root/results.tgz', local_result_fd.name)
|
||
|
run_command(args, ['tar', 'oxzf', local_result_fd.name, '-C', 'test'])
|
||
8 years ago
|
finally:
|
||
7 years ago
|
if httptester_id:
|
||
|
docker_rm(args, httptester_id)
|
||
8 years ago
|
|
||
8 years ago
|
if test_id:
|
||
|
docker_rm(args, test_id)
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def delegate_remote(args, exclude, require, integration_targets):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget]
|
||
8 years ago
|
"""
|
||
|
parts = args.remote.split('/', 1)
|
||
|
|
||
|
platform = parts[0]
|
||
|
version = parts[1]
|
||
|
|
||
7 years ago
|
core_ci = AnsibleCoreCI(args, platform, version, stage=args.remote_stage, provider=args.remote_provider)
|
||
8 years ago
|
success = False
|
||
6 years ago
|
raw = False
|
||
8 years ago
|
|
||
7 years ago
|
if isinstance(args, ShellConfig):
|
||
|
use_httptester = args.httptester
|
||
6 years ago
|
raw = args.raw
|
||
7 years ago
|
else:
|
||
|
use_httptester = args.httptester and any('needs/httptester/' in target.aliases for target in integration_targets)
|
||
|
|
||
|
if use_httptester and not docker_available():
|
||
|
display.warning('Assuming --disable-httptester since `docker` is not available.')
|
||
|
use_httptester = False
|
||
|
|
||
|
httptester_id = None
|
||
|
ssh_options = []
|
||
5 years ago
|
content_root = None
|
||
7 years ago
|
|
||
8 years ago
|
try:
|
||
|
core_ci.start()
|
||
7 years ago
|
|
||
|
if use_httptester:
|
||
|
httptester_id, ssh_options = start_httptester(args)
|
||
|
|
||
8 years ago
|
core_ci.wait()
|
||
|
|
||
5 years ago
|
python_version = get_python_version(args, get_remote_completion(), args.remote)
|
||
|
|
||
6 years ago
|
if platform == 'windows':
|
||
|
# Windows doesn't need the ansible-test fluff, just run the SSH command
|
||
|
manage = ManageWindowsCI(core_ci)
|
||
5 years ago
|
manage.setup(python_version)
|
||
|
|
||
6 years ago
|
cmd = ['powershell.exe']
|
||
6 years ago
|
elif raw:
|
||
|
manage = ManagePosixCI(core_ci)
|
||
5 years ago
|
manage.setup(python_version)
|
||
|
|
||
6 years ago
|
cmd = create_shell_command(['bash'])
|
||
6 years ago
|
else:
|
||
5 years ago
|
manage = ManagePosixCI(core_ci)
|
||
|
pwd = manage.setup(python_version)
|
||
|
|
||
6 years ago
|
options = {
|
||
|
'--remote': 1,
|
||
|
}
|
||
8 years ago
|
|
||
6 years ago
|
python_interpreter = get_python_interpreter(args, get_remote_completion(), args.remote)
|
||
5 years ago
|
|
||
5 years ago
|
install_root = os.path.join(pwd, 'ansible')
|
||
|
|
||
|
if data_context().content.collection:
|
||
|
content_root = os.path.join(install_root, data_context().content.collection.directory)
|
||
|
else:
|
||
|
content_root = install_root
|
||
5 years ago
|
|
||
|
cmd = generate_command(args, python_interpreter, install_root, content_root, options, exclude, require)
|
||
8 years ago
|
|
||
6 years ago
|
if httptester_id:
|
||
|
cmd += ['--inject-httptester']
|
||
7 years ago
|
|
||
6 years ago
|
if isinstance(args, TestConfig):
|
||
|
if args.coverage and not args.coverage_label:
|
||
|
cmd += ['--coverage-label', 'remote-%s-%s' % (platform, version)]
|
||
8 years ago
|
|
||
6 years ago
|
if isinstance(args, IntegrationConfig):
|
||
|
if not args.allow_destructive:
|
||
|
cmd.append('--allow-destructive')
|
||
8 years ago
|
|
||
6 years ago
|
# remote instances are only expected to have a single python version available
|
||
|
if isinstance(args, UnitsConfig) and not args.python:
|
||
|
cmd += ['--python', 'default']
|
||
8 years ago
|
|
||
7 years ago
|
if isinstance(args, IntegrationConfig):
|
||
8 years ago
|
cloud_platforms = get_cloud_providers(args)
|
||
8 years ago
|
|
||
8 years ago
|
for cloud_platform in cloud_platforms:
|
||
|
ssh_options += cloud_platform.get_remote_ssh_options()
|
||
8 years ago
|
|
||
8 years ago
|
try:
|
||
8 years ago
|
manage.ssh(cmd, ssh_options)
|
||
8 years ago
|
success = True
|
||
8 years ago
|
finally:
|
||
6 years ago
|
download = False
|
||
|
|
||
6 years ago
|
if platform != 'windows':
|
||
6 years ago
|
download = True
|
||
|
|
||
|
if isinstance(args, ShellConfig):
|
||
|
if args.raw:
|
||
|
download = False
|
||
|
|
||
5 years ago
|
if download and content_root:
|
||
|
manage.ssh('rm -rf /tmp/results && cp -a %s/test/results /tmp/results && chmod -R a+r /tmp/results' % content_root)
|
||
6 years ago
|
manage.download('/tmp/results', 'test')
|
||
8 years ago
|
finally:
|
||
8 years ago
|
if args.remote_terminate == 'always' or (args.remote_terminate == 'success' and success):
|
||
|
core_ci.stop()
|
||
8 years ago
|
|
||
7 years ago
|
if httptester_id:
|
||
|
docker_rm(args, httptester_id)
|
||
|
|
||
8 years ago
|
|
||
5 years ago
|
def generate_command(args, python_interpreter, install_root, content_root, options, exclude, require):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
6 years ago
|
:type python_interpreter: str | None
|
||
5 years ago
|
:type install_root: str
|
||
|
:type content_root: str
|
||
8 years ago
|
:type options: dict[str, int]
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
8 years ago
|
:rtype: list[str]
|
||
8 years ago
|
"""
|
||
|
options['--color'] = 1
|
||
|
|
||
5 years ago
|
cmd = [os.path.join(install_root, 'bin/ansible-test')]
|
||
6 years ago
|
|
||
6 years ago
|
if python_interpreter:
|
||
|
cmd = [python_interpreter] + cmd
|
||
|
|
||
6 years ago
|
# Force the encoding used during delegation.
|
||
|
# This is only needed because ansible-test relies on Python's file system encoding.
|
||
|
# Environments that do not have the locale configured are thus unable to work with unicode file paths.
|
||
|
# Examples include FreeBSD and some Linux containers.
|
||
5 years ago
|
env_vars = dict(
|
||
|
LC_ALL='en_US.UTF-8',
|
||
|
ANSIBLE_TEST_CONTENT_ROOT=content_root,
|
||
|
)
|
||
|
|
||
|
env_args = ['%s=%s' % (key, env_vars[key]) for key in sorted(env_vars)]
|
||
|
|
||
|
cmd = ['/usr/bin/env'] + env_args + cmd
|
||
6 years ago
|
|
||
8 years ago
|
cmd += list(filter_options(args, sys.argv[1:], options, exclude, require))
|
||
|
cmd += ['--color', 'yes' if args.color else 'no']
|
||
|
|
||
|
if args.requirements:
|
||
|
cmd += ['--requirements']
|
||
|
|
||
|
if isinstance(args, ShellConfig):
|
||
|
cmd = create_shell_command(cmd)
|
||
8 years ago
|
elif isinstance(args, SanityConfig):
|
||
|
if args.base_branch:
|
||
|
cmd += ['--base-branch', args.base_branch]
|
||
8 years ago
|
|
||
|
return cmd
|
||
|
|
||
|
|
||
|
def filter_options(args, argv, options, exclude, require):
|
||
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type argv: list[str]
|
||
|
:type options: dict[str, int]
|
||
|
:type exclude: list[str]
|
||
|
:type require: list[str]
|
||
|
:rtype: collections.Iterable[str]
|
||
|
"""
|
||
|
options = options.copy()
|
||
|
|
||
|
options['--requirements'] = 0
|
||
7 years ago
|
options['--truncate'] = 1
|
||
7 years ago
|
options['--redact'] = 0
|
||
8 years ago
|
|
||
|
if isinstance(args, TestConfig):
|
||
|
options.update({
|
||
|
'--changed': 0,
|
||
|
'--tracked': 0,
|
||
|
'--untracked': 0,
|
||
|
'--ignore-committed': 0,
|
||
|
'--ignore-staged': 0,
|
||
|
'--ignore-unstaged': 0,
|
||
|
'--changed-from': 1,
|
||
|
'--changed-path': 1,
|
||
8 years ago
|
'--metadata': 1,
|
||
6 years ago
|
'--exclude': 1,
|
||
|
'--require': 1,
|
||
8 years ago
|
})
|
||
8 years ago
|
elif isinstance(args, SanityConfig):
|
||
|
options.update({
|
||
|
'--base-branch': 1,
|
||
|
})
|
||
8 years ago
|
|
||
|
remaining = 0
|
||
|
|
||
|
for arg in argv:
|
||
|
if not arg.startswith('-') and remaining:
|
||
|
remaining -= 1
|
||
|
continue
|
||
|
|
||
|
remaining = 0
|
||
|
|
||
|
parts = arg.split('=', 1)
|
||
|
key = parts[0]
|
||
|
|
||
|
if key in options:
|
||
|
remaining = options[key] - len(parts) + 1
|
||
|
continue
|
||
|
|
||
|
yield arg
|
||
|
|
||
6 years ago
|
for arg in args.delegate_args:
|
||
|
yield arg
|
||
|
|
||
8 years ago
|
for target in exclude:
|
||
|
yield '--exclude'
|
||
|
yield target
|
||
|
|
||
|
for target in require:
|
||
|
yield '--require'
|
||
|
yield target
|
||
8 years ago
|
|
||
8 years ago
|
if isinstance(args, TestConfig):
|
||
|
if args.metadata_path:
|
||
|
yield '--metadata'
|
||
|
yield args.metadata_path
|
||
7 years ago
|
|
||
|
yield '--truncate'
|
||
|
yield '%d' % args.truncate
|
||
7 years ago
|
|
||
|
if args.redact:
|
||
|
yield '--redact'
|