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.
2068 lines
67 KiB
Python
2068 lines
67 KiB
Python
8 years ago
|
"""Execute Ansible tests."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
8 years ago
|
import json
|
||
8 years ago
|
import os
|
||
7 years ago
|
import collections
|
||
7 years ago
|
import datetime
|
||
8 years ago
|
import re
|
||
8 years ago
|
import time
|
||
|
import textwrap
|
||
|
import functools
|
||
6 years ago
|
import sys
|
||
7 years ago
|
import hashlib
|
||
6 years ago
|
import difflib
|
||
|
import filecmp
|
||
6 years ago
|
import random
|
||
|
import string
|
||
|
import shutil
|
||
8 years ago
|
|
||
5 years ago
|
from . import types as t
|
||
5 years ago
|
|
||
5 years ago
|
from .thread import (
|
||
5 years ago
|
WrappedThread,
|
||
|
)
|
||
8 years ago
|
|
||
5 years ago
|
from .core_ci import (
|
||
8 years ago
|
AnsibleCoreCI,
|
||
8 years ago
|
SshKey,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .manage_ci import (
|
||
8 years ago
|
ManageWindowsCI,
|
||
8 years ago
|
ManageNetworkCI,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .cloud import (
|
||
8 years ago
|
cloud_filter,
|
||
|
cloud_init,
|
||
|
get_cloud_environment,
|
||
|
get_cloud_platforms,
|
||
6 years ago
|
CloudEnvironmentConfig,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util import (
|
||
8 years ago
|
ApplicationWarning,
|
||
|
ApplicationError,
|
||
|
SubprocessError,
|
||
|
display,
|
||
|
remove_tree,
|
||
|
make_dirs,
|
||
|
is_shippable,
|
||
8 years ago
|
is_binary_file,
|
||
8 years ago
|
find_executable,
|
||
|
raw_command,
|
||
7 years ago
|
get_available_port,
|
||
6 years ago
|
generate_pip_command,
|
||
|
find_python,
|
||
6 years ago
|
get_docker_completion,
|
||
6 years ago
|
get_remote_completion,
|
||
6 years ago
|
COVERAGE_OUTPUT_PATH,
|
||
6 years ago
|
cmd_quote,
|
||
5 years ago
|
ANSIBLE_ROOT,
|
||
5 years ago
|
ANSIBLE_TEST_DATA_ROOT,
|
||
5 years ago
|
get_available_python_versions,
|
||
5 years ago
|
is_subdir,
|
||
7 years ago
|
)
|
||
|
|
||
5 years ago
|
from .util_common import (
|
||
5 years ago
|
get_python_path,
|
||
|
intercept_command,
|
||
|
named_temporary_file,
|
||
|
run_command,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .docker_util import (
|
||
7 years ago
|
docker_pull,
|
||
|
docker_run,
|
||
6 years ago
|
docker_available,
|
||
|
docker_rm,
|
||
7 years ago
|
get_docker_container_id,
|
||
|
get_docker_container_ip,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .ansible_util import (
|
||
8 years ago
|
ansible_environment,
|
||
6 years ago
|
check_pyyaml,
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
from .target import (
|
||
8 years ago
|
IntegrationTarget,
|
||
|
walk_internal_targets,
|
||
|
walk_posix_integration_targets,
|
||
|
walk_network_integration_targets,
|
||
|
walk_windows_integration_targets,
|
||
|
walk_units_targets,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .changes import (
|
||
8 years ago
|
ShippableChanges,
|
||
|
LocalChanges,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .git import (
|
||
8 years ago
|
Git,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .classification import (
|
||
8 years ago
|
categorize_changes,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .config import (
|
||
7 years ago
|
TestConfig,
|
||
|
EnvironmentConfig,
|
||
|
IntegrationConfig,
|
||
|
NetworkIntegrationConfig,
|
||
|
PosixIntegrationConfig,
|
||
|
ShellConfig,
|
||
|
UnitsConfig,
|
||
|
WindowsIntegrationConfig,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .metadata import (
|
||
7 years ago
|
ChangeDescription,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .integration import (
|
||
6 years ago
|
integration_test_environment,
|
||
6 years ago
|
integration_test_config_file,
|
||
6 years ago
|
setup_common_temp_dir,
|
||
6 years ago
|
)
|
||
|
|
||
5 years ago
|
from .coverage_util import (
|
||
5 years ago
|
coverage_context,
|
||
|
)
|
||
|
|
||
5 years ago
|
from .data import (
|
||
5 years ago
|
data_context,
|
||
|
)
|
||
|
|
||
5 years ago
|
REMOTE_ONLY_PYTHON_VERSIONS = (
|
||
|
'2.6',
|
||
|
)
|
||
|
|
||
8 years ago
|
SUPPORTED_PYTHON_VERSIONS = (
|
||
|
'2.6',
|
||
|
'2.7',
|
||
|
'3.5',
|
||
8 years ago
|
'3.6',
|
||
7 years ago
|
'3.7',
|
||
6 years ago
|
'3.8',
|
||
8 years ago
|
)
|
||
|
|
||
7 years ago
|
HTTPTESTER_HOSTS = (
|
||
|
'ansible.http.tests',
|
||
|
'sni1.ansible.http.tests',
|
||
|
'fail.ansible.http.tests',
|
||
|
)
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
def check_startup():
|
||
|
"""Checks to perform at startup before running commands."""
|
||
|
check_legacy_modules()
|
||
|
|
||
|
|
||
|
def check_legacy_modules():
|
||
|
"""Detect conflicts with legacy core/extras module directories to avoid problems later."""
|
||
|
for directory in 'core', 'extras':
|
||
|
path = 'lib/ansible/modules/%s' % directory
|
||
|
|
||
5 years ago
|
for root, _dir_names, file_names in os.walk(path):
|
||
8 years ago
|
if file_names:
|
||
|
# the directory shouldn't exist, but if it does, it must contain no files
|
||
|
raise ApplicationError('Files prohibited in "%s". '
|
||
|
'These are most likely legacy modules from version 2.2 or earlier.' % root)
|
||
|
|
||
|
|
||
8 years ago
|
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
|
||
|
|
||
|
|
||
6 years ago
|
def install_command_requirements(args, python_version=None):
|
||
8 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
6 years ago
|
:type python_version: str | None
|
||
8 years ago
|
"""
|
||
5 years ago
|
if not args.explain:
|
||
|
make_dirs('test/results/coverage')
|
||
|
make_dirs('test/results/data')
|
||
|
|
||
6 years ago
|
if isinstance(args, ShellConfig):
|
||
|
if args.raw:
|
||
|
return
|
||
|
|
||
8 years ago
|
generate_egg_info(args)
|
||
|
|
||
|
if not args.requirements:
|
||
|
return
|
||
|
|
||
7 years ago
|
if isinstance(args, ShellConfig):
|
||
|
return
|
||
|
|
||
8 years ago
|
packages = []
|
||
8 years ago
|
|
||
|
if isinstance(args, TestConfig):
|
||
|
if args.coverage:
|
||
8 years ago
|
packages.append('coverage')
|
||
8 years ago
|
if args.junit:
|
||
8 years ago
|
packages.append('junit-xml')
|
||
|
|
||
6 years ago
|
if not python_version:
|
||
|
python_version = args.python_version
|
||
|
|
||
|
pip = generate_pip_command(find_python(python_version))
|
||
7 years ago
|
|
||
|
commands = [generate_pip_install(pip, args.command, packages=packages)]
|
||
8 years ago
|
|
||
7 years ago
|
if isinstance(args, IntegrationConfig):
|
||
7 years ago
|
for cloud_platform in get_cloud_platforms(args):
|
||
7 years ago
|
commands.append(generate_pip_install(pip, '%s.cloud.%s' % (args.command, cloud_platform)))
|
||
|
|
||
|
commands = [cmd for cmd in commands if cmd]
|
||
8 years ago
|
|
||
7 years ago
|
# only look for changes when more than one requirements file is needed
|
||
|
detect_pip_changes = len(commands) > 1
|
||
8 years ago
|
|
||
7 years ago
|
# first pass to install requirements, changes expected unless environment is already set up
|
||
7 years ago
|
changes = run_pip_commands(args, pip, commands, detect_pip_changes)
|
||
7 years ago
|
|
||
6 years ago
|
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)
|
||
7 years ago
|
|
||
6 years ago
|
if changes:
|
||
|
raise ApplicationError('Conflicts detected in requirements. The following commands reported changes during verification:\n%s' %
|
||
6 years ago
|
'\n'.join((' '.join(cmd_quote(c) for c in cmd) for cmd in changes)))
|
||
7 years ago
|
|
||
6 years ago
|
# 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
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
def run_pip_commands(args, pip, commands, detect_pip_changes=False):
|
||
7 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
7 years ago
|
:type pip: list[str]
|
||
7 years ago
|
:type commands: list[list[str]]
|
||
|
:type detect_pip_changes: bool
|
||
|
:rtype: list[list[str]]
|
||
|
"""
|
||
|
changes = []
|
||
|
|
||
7 years ago
|
after_list = pip_list(args, pip) if detect_pip_changes else None
|
||
8 years ago
|
|
||
7 years ago
|
for cmd in commands:
|
||
|
if not cmd:
|
||
|
continue
|
||
8 years ago
|
|
||
7 years ago
|
before_list = after_list
|
||
8 years ago
|
|
||
6 years ago
|
run_command(args, cmd)
|
||
7 years ago
|
|
||
7 years ago
|
after_list = pip_list(args, pip) if detect_pip_changes else None
|
||
7 years ago
|
|
||
|
if before_list != after_list:
|
||
|
changes.append(cmd)
|
||
|
|
||
|
return changes
|
||
|
|
||
|
|
||
7 years ago
|
def pip_list(args, pip):
|
||
7 years ago
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
7 years ago
|
:type pip: list[str]
|
||
7 years ago
|
:rtype: str
|
||
|
"""
|
||
5 years ago
|
stdout = run_command(args, pip + ['list'], capture=True)[0]
|
||
7 years ago
|
return stdout
|
||
8 years ago
|
|
||
|
|
||
|
def generate_egg_info(args):
|
||
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
"""
|
||
5 years ago
|
if not os.path.exists(os.path.join(ANSIBLE_ROOT, 'setup.py')):
|
||
5 years ago
|
return
|
||
|
|
||
5 years ago
|
if os.path.isdir(os.path.join(ANSIBLE_ROOT, 'lib/ansible.egg-info')):
|
||
8 years ago
|
return
|
||
|
|
||
5 years ago
|
run_command(args, [args.python_executable, 'setup.py', 'egg_info'], cwd=ANSIBLE_ROOT, capture=args.verbosity < 3)
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def generate_pip_install(pip, command, packages=None):
|
||
8 years ago
|
"""
|
||
7 years ago
|
:type pip: list[str]
|
||
8 years ago
|
:type command: str
|
||
8 years ago
|
:type packages: list[str] | None
|
||
8 years ago
|
:rtype: list[str] | None
|
||
8 years ago
|
"""
|
||
5 years ago
|
constraints = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', 'constraints.txt')
|
||
|
requirements = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', '%s.txt' % command)
|
||
8 years ago
|
|
||
8 years ago
|
options = []
|
||
|
|
||
7 years ago
|
if os.path.exists(requirements) and os.path.getsize(requirements):
|
||
|
options += ['-r', requirements]
|
||
8 years ago
|
|
||
|
if packages:
|
||
|
options += packages
|
||
|
|
||
|
if not options:
|
||
8 years ago
|
return None
|
||
|
|
||
7 years ago
|
return pip + ['install', '--disable-pip-version-check', '-c', constraints] + options
|
||
8 years ago
|
|
||
|
|
||
|
def command_shell(args):
|
||
|
"""
|
||
|
:type args: ShellConfig
|
||
|
"""
|
||
|
if args.delegate:
|
||
|
raise Delegate()
|
||
|
|
||
|
install_command_requirements(args)
|
||
|
|
||
7 years ago
|
if args.inject_httptester:
|
||
|
inject_httptester(args)
|
||
|
|
||
8 years ago
|
cmd = create_shell_command(['bash', '-i'])
|
||
|
run_command(args, cmd)
|
||
|
|
||
|
|
||
|
def command_posix_integration(args):
|
||
|
"""
|
||
|
:type args: PosixIntegrationConfig
|
||
|
"""
|
||
6 years ago
|
filename = 'test/integration/inventory'
|
||
|
|
||
7 years ago
|
all_targets = tuple(walk_posix_integration_targets(include_hidden=True))
|
||
|
internal_targets = command_integration_filter(args, all_targets)
|
||
6 years ago
|
command_integration_filtered(args, internal_targets, all_targets, filename)
|
||
8 years ago
|
|
||
|
|
||
|
def command_network_integration(args):
|
||
|
"""
|
||
|
:type args: NetworkIntegrationConfig
|
||
|
"""
|
||
7 years ago
|
default_filename = 'test/integration/inventory.networking'
|
||
8 years ago
|
|
||
7 years ago
|
if args.inventory:
|
||
|
filename = os.path.join('test/integration', args.inventory)
|
||
|
else:
|
||
|
filename = default_filename
|
||
|
|
||
|
if not args.explain and not args.platform and not os.path.exists(filename):
|
||
|
if args.inventory:
|
||
|
filename = os.path.abspath(filename)
|
||
|
|
||
|
raise ApplicationError(
|
||
|
'Inventory not found: %s\n'
|
||
|
'Use --inventory to specify the inventory path.\n'
|
||
|
'Use --platform to provision resources and generate an inventory file.\n'
|
||
|
'See also inventory template: %s.template' % (filename, default_filename)
|
||
|
)
|
||
8 years ago
|
|
||
7 years ago
|
all_targets = tuple(walk_network_integration_targets(include_hidden=True))
|
||
7 years ago
|
internal_targets = command_integration_filter(args, all_targets, init_callback=network_init)
|
||
5 years ago
|
instances = [] # type: t.List[WrappedThread]
|
||
8 years ago
|
|
||
|
if args.platform:
|
||
6 years ago
|
get_python_path(args, args.python_executable) # initialize before starting threads
|
||
7 years ago
|
|
||
7 years ago
|
configs = dict((config['platform_version'], config) for config in args.metadata.instance_config)
|
||
8 years ago
|
|
||
|
for platform_version in args.platform:
|
||
|
platform, version = platform_version.split('/', 1)
|
||
7 years ago
|
config = configs.get(platform_version)
|
||
8 years ago
|
|
||
7 years ago
|
if not config:
|
||
8 years ago
|
continue
|
||
|
|
||
5 years ago
|
instance = WrappedThread(functools.partial(network_run, args, platform, version, config))
|
||
8 years ago
|
instance.daemon = True
|
||
|
instance.start()
|
||
|
instances.append(instance)
|
||
|
|
||
|
while any(instance.is_alive() for instance in instances):
|
||
|
time.sleep(1)
|
||
|
|
||
|
remotes = [instance.wait_for_result() for instance in instances]
|
||
|
inventory = network_inventory(remotes)
|
||
|
|
||
8 years ago
|
display.info('>>> Inventory: %s\n%s' % (filename, inventory.strip()), verbosity=3)
|
||
|
|
||
8 years ago
|
if not args.explain:
|
||
8 years ago
|
with open(filename, 'w') as inventory_fd:
|
||
8 years ago
|
inventory_fd.write(inventory)
|
||
|
|
||
7 years ago
|
success = False
|
||
|
|
||
|
try:
|
||
6 years ago
|
command_integration_filtered(args, internal_targets, all_targets, filename)
|
||
7 years ago
|
success = True
|
||
|
finally:
|
||
|
if args.remote_terminate == 'always' or (args.remote_terminate == 'success' and success):
|
||
|
for instance in instances:
|
||
|
instance.result.stop()
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
def network_init(args, internal_targets):
|
||
|
"""
|
||
|
:type args: NetworkIntegrationConfig
|
||
|
:type internal_targets: tuple[IntegrationTarget]
|
||
|
"""
|
||
|
if not args.platform:
|
||
|
return
|
||
|
|
||
|
if args.metadata.instance_config is not None:
|
||
|
return
|
||
|
|
||
5 years ago
|
platform_targets = set(a for target in internal_targets for a in target.aliases if a.startswith('network/'))
|
||
7 years ago
|
|
||
5 years ago
|
instances = [] # type: t.List[WrappedThread]
|
||
7 years ago
|
|
||
|
# generate an ssh key (if needed) up front once, instead of for each instance
|
||
|
SshKey(args)
|
||
|
|
||
|
for platform_version in args.platform:
|
||
|
platform, version = platform_version.split('/', 1)
|
||
|
platform_target = 'network/%s/' % platform
|
||
|
|
||
7 years ago
|
if platform_target not in platform_targets:
|
||
7 years ago
|
display.warning('Skipping "%s" because selected tests do not target the "%s" platform.' % (
|
||
|
platform_version, platform))
|
||
|
continue
|
||
|
|
||
5 years ago
|
instance = WrappedThread(functools.partial(network_start, args, platform, version))
|
||
7 years ago
|
instance.daemon = True
|
||
|
instance.start()
|
||
|
instances.append(instance)
|
||
|
|
||
|
while any(instance.is_alive() for instance in instances):
|
||
|
time.sleep(1)
|
||
|
|
||
|
args.metadata.instance_config = [instance.wait_for_result() for instance in instances]
|
||
|
|
||
|
|
||
|
def network_start(args, platform, version):
|
||
8 years ago
|
"""
|
||
|
:type args: NetworkIntegrationConfig
|
||
|
:type platform: str
|
||
|
:type version: str
|
||
|
:rtype: AnsibleCoreCI
|
||
|
"""
|
||
7 years ago
|
core_ci = AnsibleCoreCI(args, platform, version, stage=args.remote_stage, provider=args.remote_provider)
|
||
8 years ago
|
core_ci.start()
|
||
7 years ago
|
|
||
|
return core_ci.save()
|
||
|
|
||
|
|
||
|
def network_run(args, platform, version, config):
|
||
|
"""
|
||
|
:type args: NetworkIntegrationConfig
|
||
|
:type platform: str
|
||
|
:type version: str
|
||
|
:type config: dict[str, str]
|
||
|
:rtype: AnsibleCoreCI
|
||
|
"""
|
||
7 years ago
|
core_ci = AnsibleCoreCI(args, platform, version, stage=args.remote_stage, provider=args.remote_provider, load=False)
|
||
7 years ago
|
core_ci.load(config)
|
||
8 years ago
|
core_ci.wait()
|
||
|
|
||
|
manage = ManageNetworkCI(core_ci)
|
||
|
manage.wait()
|
||
|
|
||
|
return core_ci
|
||
|
|
||
|
|
||
|
def network_inventory(remotes):
|
||
|
"""
|
||
|
:type remotes: list[AnsibleCoreCI]
|
||
|
:rtype: str
|
||
|
"""
|
||
|
groups = dict([(remote.platform, []) for remote in remotes])
|
||
7 years ago
|
net = []
|
||
8 years ago
|
|
||
|
for remote in remotes:
|
||
8 years ago
|
options = dict(
|
||
|
ansible_host=remote.connection.hostname,
|
||
|
ansible_user=remote.connection.username,
|
||
7 years ago
|
ansible_ssh_private_key_file=os.path.abspath(remote.ssh_key.key),
|
||
8 years ago
|
ansible_network_os=remote.platform,
|
||
8 years ago
|
ansible_connection='local'
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
groups[remote.platform].append(
|
||
8 years ago
|
'%s %s' % (
|
||
7 years ago
|
remote.name.replace('.', '-'),
|
||
8 years ago
|
' '.join('%s="%s"' % (k, options[k]) for k in sorted(options)),
|
||
8 years ago
|
)
|
||
|
)
|
||
|
|
||
7 years ago
|
net.append(remote.platform)
|
||
|
|
||
|
groups['net:children'] = net
|
||
|
|
||
8 years ago
|
template = ''
|
||
|
|
||
|
for group in groups:
|
||
|
hosts = '\n'.join(groups[group])
|
||
|
|
||
8 years ago
|
template += textwrap.dedent("""
|
||
8 years ago
|
[%s]
|
||
|
%s
|
||
8 years ago
|
""") % (group, hosts)
|
||
8 years ago
|
|
||
8 years ago
|
inventory = template
|
||
8 years ago
|
|
||
|
return inventory
|
||
|
|
||
|
|
||
8 years ago
|
def command_windows_integration(args):
|
||
|
"""
|
||
|
:type args: WindowsIntegrationConfig
|
||
|
"""
|
||
8 years ago
|
filename = 'test/integration/inventory.winrm'
|
||
|
|
||
|
if not args.explain and not args.windows and not os.path.isfile(filename):
|
||
|
raise ApplicationError('Use the --windows option or provide an inventory file (see %s.template).' % filename)
|
||
|
|
||
7 years ago
|
all_targets = tuple(walk_windows_integration_targets(include_hidden=True))
|
||
7 years ago
|
internal_targets = command_integration_filter(args, all_targets, init_callback=windows_init)
|
||
5 years ago
|
instances = [] # type: t.List[WrappedThread]
|
||
6 years ago
|
pre_target = None
|
||
|
post_target = None
|
||
6 years ago
|
httptester_id = None
|
||
8 years ago
|
|
||
|
if args.windows:
|
||
6 years ago
|
get_python_path(args, args.python_executable) # initialize before starting threads
|
||
7 years ago
|
|
||
7 years ago
|
configs = dict((config['platform_version'], config) for config in args.metadata.instance_config)
|
||
8 years ago
|
|
||
|
for version in args.windows:
|
||
7 years ago
|
config = configs['windows/%s' % version]
|
||
|
|
||
5 years ago
|
instance = WrappedThread(functools.partial(windows_run, args, version, config))
|
||
8 years ago
|
instance.daemon = True
|
||
|
instance.start()
|
||
|
instances.append(instance)
|
||
|
|
||
|
while any(instance.is_alive() for instance in instances):
|
||
|
time.sleep(1)
|
||
|
|
||
|
remotes = [instance.wait_for_result() for instance in instances]
|
||
|
inventory = windows_inventory(remotes)
|
||
|
|
||
8 years ago
|
display.info('>>> Inventory: %s\n%s' % (filename, inventory.strip()), verbosity=3)
|
||
|
|
||
8 years ago
|
if not args.explain:
|
||
8 years ago
|
with open(filename, 'w') as inventory_fd:
|
||
8 years ago
|
inventory_fd.write(inventory)
|
||
|
|
||
5 years ago
|
use_httptester = args.httptester and any('needs/httptester/' in target.aliases for target in internal_targets)
|
||
6 years ago
|
# if running under Docker delegation, the httptester may have already been started
|
||
|
docker_httptester = bool(os.environ.get("HTTPTESTER", False))
|
||
|
|
||
|
if use_httptester and not docker_available() and not docker_httptester:
|
||
|
display.warning('Assuming --disable-httptester since `docker` is not available.')
|
||
6 years ago
|
elif use_httptester:
|
||
6 years ago
|
if docker_httptester:
|
||
|
# we are running in a Docker container that is linked to the httptester container, we just need to
|
||
|
# forward these requests to the linked hostname
|
||
|
first_host = HTTPTESTER_HOSTS[0]
|
||
|
ssh_options = ["-R", "8080:%s:80" % first_host, "-R", "8443:%s:443" % first_host]
|
||
|
else:
|
||
|
# we are running directly and need to start the httptester container ourselves and forward the port
|
||
|
# from there manually set so HTTPTESTER env var is set during the run
|
||
|
args.inject_httptester = True
|
||
|
httptester_id, ssh_options = start_httptester(args)
|
||
|
|
||
|
# to get this SSH command to run in the background we need to set to run in background (-f) and disable
|
||
|
# the pty allocation (-T)
|
||
|
ssh_options.insert(0, "-fT")
|
||
|
|
||
|
# create a script that will continue to run in the background until the script is deleted, this will
|
||
|
# cleanup and close the connection
|
||
6 years ago
|
def forward_ssh_ports(target):
|
||
|
"""
|
||
|
:type target: IntegrationTarget
|
||
|
"""
|
||
|
if 'needs/httptester/' not in target.aliases:
|
||
|
return
|
||
|
|
||
|
for remote in [r for r in remotes if r.version != '2008']:
|
||
|
manage = ManageWindowsCI(remote)
|
||
5 years ago
|
manage.upload(os.path.join(ANSIBLE_TEST_DATA_ROOT, 'setup', 'windows-httptester.ps1'), watcher_path)
|
||
6 years ago
|
|
||
6 years ago
|
# We cannot pass an array of string with -File so we just use a delimiter for multiple values
|
||
|
script = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\\%s -Hosts \"%s\"" \
|
||
|
% (watcher_path, "|".join(HTTPTESTER_HOSTS))
|
||
6 years ago
|
if args.verbosity > 3:
|
||
|
script += " -Verbose"
|
||
|
manage.ssh(script, options=ssh_options, force_pty=False)
|
||
|
|
||
|
def cleanup_ssh_ports(target):
|
||
|
"""
|
||
|
:type target: IntegrationTarget
|
||
|
"""
|
||
|
if 'needs/httptester/' not in target.aliases:
|
||
|
return
|
||
|
|
||
|
for remote in [r for r in remotes if r.version != '2008']:
|
||
|
# delete the tmp file that keeps the http-tester alive
|
||
|
manage = ManageWindowsCI(remote)
|
||
6 years ago
|
manage.ssh("cmd.exe /c \"del %s /F /Q\"" % watcher_path, force_pty=False)
|
||
6 years ago
|
|
||
6 years ago
|
watcher_path = "ansible-test-http-watcher-%s.ps1" % time.time()
|
||
|
pre_target = forward_ssh_ports
|
||
|
post_target = cleanup_ssh_ports
|
||
6 years ago
|
|
||
7 years ago
|
success = False
|
||
|
|
||
|
try:
|
||
6 years ago
|
command_integration_filtered(args, internal_targets, all_targets, filename, pre_target=pre_target,
|
||
6 years ago
|
post_target=post_target)
|
||
7 years ago
|
success = True
|
||
|
finally:
|
||
6 years ago
|
if httptester_id:
|
||
|
docker_rm(args, httptester_id)
|
||
6 years ago
|
|
||
7 years ago
|
if args.remote_terminate == 'always' or (args.remote_terminate == 'success' and success):
|
||
|
for instance in instances:
|
||
|
instance.result.stop()
|
||
7 years ago
|
|
||
|
|
||
6 years ago
|
# noinspection PyUnusedLocal
|
||
7 years ago
|
def windows_init(args, internal_targets): # pylint: disable=locally-disabled, unused-argument
|
||
|
"""
|
||
|
:type args: WindowsIntegrationConfig
|
||
|
:type internal_targets: tuple[IntegrationTarget]
|
||
|
"""
|
||
|
if not args.windows:
|
||
|
return
|
||
|
|
||
|
if args.metadata.instance_config is not None:
|
||
|
return
|
||
|
|
||
5 years ago
|
instances = [] # type: t.List[WrappedThread]
|
||
7 years ago
|
|
||
|
for version in args.windows:
|
||
5 years ago
|
instance = WrappedThread(functools.partial(windows_start, args, version))
|
||
7 years ago
|
instance.daemon = True
|
||
|
instance.start()
|
||
|
instances.append(instance)
|
||
8 years ago
|
|
||
7 years ago
|
while any(instance.is_alive() for instance in instances):
|
||
|
time.sleep(1)
|
||
8 years ago
|
|
||
7 years ago
|
args.metadata.instance_config = [instance.wait_for_result() for instance in instances]
|
||
|
|
||
|
|
||
|
def windows_start(args, version):
|
||
8 years ago
|
"""
|
||
|
:type args: WindowsIntegrationConfig
|
||
|
:type version: str
|
||
|
:rtype: AnsibleCoreCI
|
||
|
"""
|
||
7 years ago
|
core_ci = AnsibleCoreCI(args, 'windows', version, stage=args.remote_stage, provider=args.remote_provider)
|
||
8 years ago
|
core_ci.start()
|
||
7 years ago
|
|
||
|
return core_ci.save()
|
||
|
|
||
|
|
||
|
def windows_run(args, version, config):
|
||
|
"""
|
||
|
:type args: WindowsIntegrationConfig
|
||
|
:type version: str
|
||
|
:type config: dict[str, str]
|
||
|
:rtype: AnsibleCoreCI
|
||
|
"""
|
||
7 years ago
|
core_ci = AnsibleCoreCI(args, 'windows', version, stage=args.remote_stage, provider=args.remote_provider, load=False)
|
||
7 years ago
|
core_ci.load(config)
|
||
8 years ago
|
core_ci.wait()
|
||
|
|
||
|
manage = ManageWindowsCI(core_ci)
|
||
|
manage.wait()
|
||
|
|
||
|
return core_ci
|
||
|
|
||
|
|
||
|
def windows_inventory(remotes):
|
||
|
"""
|
||
|
:type remotes: list[AnsibleCoreCI]
|
||
|
:rtype: str
|
||
|
"""
|
||
8 years ago
|
hosts = []
|
||
|
|
||
|
for remote in remotes:
|
||
|
options = dict(
|
||
|
ansible_host=remote.connection.hostname,
|
||
|
ansible_user=remote.connection.username,
|
||
|
ansible_password=remote.connection.password,
|
||
|
ansible_port=remote.connection.port,
|
||
|
)
|
||
|
|
||
6 years ago
|
# used for the connection_windows_ssh test target
|
||
|
if remote.ssh_key:
|
||
|
options["ansible_ssh_private_key_file"] = os.path.abspath(remote.ssh_key.key)
|
||
|
|
||
8 years ago
|
hosts.append(
|
||
|
'%s %s' % (
|
||
|
remote.name.replace('/', '_'),
|
||
|
' '.join('%s="%s"' % (k, options[k]) for k in sorted(options)),
|
||
|
)
|
||
|
)
|
||
8 years ago
|
|
||
|
template = """
|
||
|
[windows]
|
||
|
%s
|
||
|
|
||
|
[windows:vars]
|
||
|
ansible_connection=winrm
|
||
|
ansible_winrm_server_cert_validation=ignore
|
||
|
|
||
|
# support winrm connection tests (temporary solution, does not support testing enable/disable of pipelining)
|
||
|
[winrm:children]
|
||
|
windows
|
||
|
|
||
|
# support winrm binary module tests (temporary solution)
|
||
|
[testhost_binary_modules:children]
|
||
|
windows
|
||
|
"""
|
||
|
|
||
|
template = textwrap.dedent(template)
|
||
|
inventory = template % ('\n'.join(hosts))
|
||
|
|
||
|
return inventory
|
||
|
|
||
|
|
||
7 years ago
|
def command_integration_filter(args, targets, init_callback=None):
|
||
8 years ago
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: collections.Iterable[IntegrationTarget]
|
||
7 years ago
|
:type init_callback: (IntegrationConfig, tuple[IntegrationTarget]) -> None
|
||
8 years ago
|
:rtype: tuple[IntegrationTarget]
|
||
|
"""
|
||
7 years ago
|
targets = tuple(target for target in targets if 'hidden/' not in target.aliases)
|
||
8 years ago
|
changes = get_changes_filter(args)
|
||
6 years ago
|
|
||
|
# special behavior when the --changed-all-target target is selected based on changes
|
||
|
if args.changed_all_target in changes:
|
||
|
# act as though the --changed-all-target target was in the include list
|
||
|
if args.changed_all_mode == 'include' and args.changed_all_target not in args.include:
|
||
|
args.include.append(args.changed_all_target)
|
||
|
args.delegate_args += ['--include', args.changed_all_target]
|
||
|
# act as though the --changed-all-target target was in the exclude list
|
||
|
elif args.changed_all_mode == 'exclude' and args.changed_all_target not in args.exclude:
|
||
|
args.exclude.append(args.changed_all_target)
|
||
|
|
||
|
require = args.require + changes
|
||
|
exclude = args.exclude
|
||
8 years ago
|
|
||
|
internal_targets = walk_internal_targets(targets, args.include, exclude, require)
|
||
|
environment_exclude = get_integration_filter(args, internal_targets)
|
||
|
|
||
8 years ago
|
environment_exclude += cloud_filter(args, internal_targets)
|
||
|
|
||
8 years ago
|
if environment_exclude:
|
||
|
exclude += environment_exclude
|
||
|
internal_targets = walk_internal_targets(targets, args.include, exclude, require)
|
||
|
|
||
|
if not internal_targets:
|
||
|
raise AllTargetsSkipped()
|
||
|
|
||
5 years ago
|
if args.start_at and not any(target.name == args.start_at for target in internal_targets):
|
||
8 years ago
|
raise ApplicationError('Start at target matches nothing: %s' % args.start_at)
|
||
|
|
||
7 years ago
|
if init_callback:
|
||
|
init_callback(args, internal_targets)
|
||
|
|
||
8 years ago
|
cloud_init(args, internal_targets)
|
||
|
|
||
8 years ago
|
if args.delegate:
|
||
6 years ago
|
raise Delegate(require=require, exclude=exclude, integration_targets=internal_targets)
|
||
8 years ago
|
|
||
|
install_command_requirements(args)
|
||
|
|
||
|
return internal_targets
|
||
|
|
||
|
|
||
6 years ago
|
def command_integration_filtered(args, targets, all_targets, inventory_path, pre_target=None, post_target=None):
|
||
8 years ago
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
7 years ago
|
:type all_targets: tuple[IntegrationTarget]
|
||
6 years ago
|
:type inventory_path: str
|
||
6 years ago
|
:type pre_target: (IntegrationTarget) -> None | None
|
||
|
:type post_target: (IntegrationTarget) -> None | None
|
||
8 years ago
|
"""
|
||
|
found = False
|
||
7 years ago
|
passed = []
|
||
|
failed = []
|
||
8 years ago
|
|
||
|
targets_iter = iter(targets)
|
||
7 years ago
|
all_targets_dict = dict((target.name, target) for target in all_targets)
|
||
|
|
||
|
setup_errors = []
|
||
|
setup_targets_executed = set()
|
||
|
|
||
|
for target in all_targets:
|
||
|
for setup_target in target.setup_once + target.setup_always:
|
||
|
if setup_target not in all_targets_dict:
|
||
|
setup_errors.append('Target "%s" contains invalid setup target: %s' % (target.name, setup_target))
|
||
|
|
||
|
if setup_errors:
|
||
|
raise ApplicationError('Found %d invalid setup aliases:\n%s' % (len(setup_errors), '\n'.join(setup_errors)))
|
||
8 years ago
|
|
||
6 years ago
|
check_pyyaml(args, args.python_version)
|
||
|
|
||
8 years ago
|
test_dir = os.path.expanduser('~/ansible_testing')
|
||
|
|
||
7 years ago
|
if not args.explain and any('needs/ssh/' in target.aliases for target in targets):
|
||
8 years ago
|
max_tries = 20
|
||
|
display.info('SSH service required for tests. Checking to make sure we can connect.')
|
||
|
for i in range(1, max_tries + 1):
|
||
|
try:
|
||
|
run_command(args, ['ssh', '-o', 'BatchMode=yes', 'localhost', 'id'], capture=True)
|
||
|
display.info('SSH service responded.')
|
||
|
break
|
||
8 years ago
|
except SubprocessError:
|
||
8 years ago
|
if i == max_tries:
|
||
8 years ago
|
raise
|
||
8 years ago
|
seconds = 3
|
||
|
display.warning('SSH service not responding. Waiting %d second(s) before checking again.' % seconds)
|
||
|
time.sleep(seconds)
|
||
|
|
||
6 years ago
|
# Windows is different as Ansible execution is done locally but the host is remote
|
||
|
if args.inject_httptester and not isinstance(args, WindowsIntegrationConfig):
|
||
7 years ago
|
inject_httptester(args)
|
||
|
|
||
8 years ago
|
start_at_task = args.start_at_task
|
||
|
|
||
7 years ago
|
results = {}
|
||
|
|
||
5 years ago
|
current_environment = None # type: t.Optional[EnvironmentDescription]
|
||
6 years ago
|
|
||
6 years ago
|
# common temporary directory path that will be valid on both the controller and the remote
|
||
|
# it must be common because it will be referenced in environment variables that are shared across multiple hosts
|
||
5 years ago
|
common_temp_path = '/tmp/ansible-test-%s' % ''.join(random.choice(string.ascii_letters + string.digits) for _idx in range(8))
|
||
6 years ago
|
|
||
|
setup_common_temp_dir(args, common_temp_path)
|
||
|
|
||
6 years ago
|
try:
|
||
|
for target in targets_iter:
|
||
|
if args.start_at and not found:
|
||
|
found = target.name == args.start_at
|
||
|
|
||
|
if not found:
|
||
|
continue
|
||
8 years ago
|
|
||
6 years ago
|
if args.list_targets:
|
||
|
print(target.name)
|
||
8 years ago
|
continue
|
||
|
|
||
6 years ago
|
tries = 2 if args.retry_on_error else 1
|
||
|
verbosity = args.verbosity
|
||
7 years ago
|
|
||
6 years ago
|
cloud_environment = get_cloud_environment(args, target)
|
||
8 years ago
|
|
||
6 years ago
|
original_environment = current_environment if current_environment else EnvironmentDescription(args)
|
||
|
current_environment = None
|
||
8 years ago
|
|
||
6 years ago
|
display.info('>>> Environment Description\n%s' % original_environment, verbosity=3)
|
||
8 years ago
|
|
||
6 years ago
|
try:
|
||
|
while tries:
|
||
|
tries -= 1
|
||
8 years ago
|
|
||
6 years ago
|
try:
|
||
|
if cloud_environment:
|
||
|
cloud_environment.setup_once()
|
||
8 years ago
|
|
||
6 years ago
|
run_setup_targets(args, test_dir, target.setup_once, all_targets_dict, setup_targets_executed, inventory_path, common_temp_path, False)
|
||
7 years ago
|
|
||
6 years ago
|
start_time = time.time()
|
||
7 years ago
|
|
||
6 years ago
|
run_setup_targets(args, test_dir, target.setup_always, all_targets_dict, setup_targets_executed, inventory_path, common_temp_path, True)
|
||
7 years ago
|
|
||
6 years ago
|
if not args.explain:
|
||
|
# create a fresh test directory for each test target
|
||
|
remove_tree(test_dir)
|
||
|
make_dirs(test_dir)
|
||
7 years ago
|
|
||
6 years ago
|
if pre_target:
|
||
|
pre_target(target)
|
||
7 years ago
|
|
||
6 years ago
|
try:
|
||
|
if target.script_path:
|
||
6 years ago
|
command_integration_script(args, target, test_dir, inventory_path, common_temp_path)
|
||
6 years ago
|
else:
|
||
6 years ago
|
command_integration_role(args, target, start_at_task, test_dir, inventory_path, common_temp_path)
|
||
6 years ago
|
start_at_task = None
|
||
|
finally:
|
||
|
if post_target:
|
||
|
post_target(target)
|
||
6 years ago
|
|
||
6 years ago
|
end_time = time.time()
|
||
7 years ago
|
|
||
6 years ago
|
results[target.name] = dict(
|
||
|
name=target.name,
|
||
|
type=target.type,
|
||
|
aliases=target.aliases,
|
||
|
modules=target.modules,
|
||
|
run_time_seconds=int(end_time - start_time),
|
||
|
setup_once=target.setup_once,
|
||
|
setup_always=target.setup_always,
|
||
|
coverage=args.coverage,
|
||
|
coverage_label=args.coverage_label,
|
||
|
python_version=args.python_version,
|
||
|
)
|
||
8 years ago
|
|
||
6 years ago
|
break
|
||
|
except SubprocessError:
|
||
|
if cloud_environment:
|
||
|
cloud_environment.on_failure(target, tries)
|
||
8 years ago
|
|
||
6 years ago
|
if not original_environment.validate(target.name, throw=False):
|
||
|
raise
|
||
8 years ago
|
|
||
6 years ago
|
if not tries:
|
||
|
raise
|
||
8 years ago
|
|
||
6 years ago
|
display.warning('Retrying test target "%s" with maximum verbosity.' % target.name)
|
||
|
display.verbosity = args.verbosity = 6
|
||
7 years ago
|
|
||
6 years ago
|
start_time = time.time()
|
||
|
current_environment = EnvironmentDescription(args)
|
||
|
end_time = time.time()
|
||
|
|
||
|
EnvironmentDescription.check(original_environment, current_environment, target.name, throw=True)
|
||
|
|
||
|
results[target.name]['validation_seconds'] = int(end_time - start_time)
|
||
|
|
||
|
passed.append(target)
|
||
|
except Exception as ex:
|
||
|
failed.append(target)
|
||
|
|
||
|
if args.continue_on_error:
|
||
|
display.error(ex)
|
||
|
continue
|
||
|
|
||
|
display.notice('To resume at this test target, use the option: --start-at %s' % target.name)
|
||
|
|
||
|
next_target = next(targets_iter, None)
|
||
|
|
||
|
if next_target:
|
||
|
display.notice('To resume after this test target, use the option: --start-at %s' % next_target.name)
|
||
|
|
||
|
raise
|
||
|
finally:
|
||
|
display.verbosity = args.verbosity = verbosity
|
||
|
|
||
|
finally:
|
||
|
if not args.explain:
|
||
6 years ago
|
if args.coverage:
|
||
|
coverage_temp_path = os.path.join(common_temp_path, COVERAGE_OUTPUT_PATH)
|
||
|
coverage_save_path = 'test/results/coverage'
|
||
|
|
||
|
for filename in os.listdir(coverage_temp_path):
|
||
|
shutil.copy(os.path.join(coverage_temp_path, filename), os.path.join(coverage_save_path, filename))
|
||
|
|
||
|
remove_tree(common_temp_path)
|
||
|
|
||
6 years ago
|
results_path = 'test/results/data/%s-%s.json' % (args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0))))
|
||
|
|
||
|
data = dict(
|
||
|
targets=results,
|
||
|
)
|
||
7 years ago
|
|
||
6 years ago
|
with open(results_path, 'w') as results_fd:
|
||
|
results_fd.write(json.dumps(data, sort_keys=True, indent=4))
|
||
7 years ago
|
|
||
7 years ago
|
if failed:
|
||
|
raise ApplicationError('The %d integration test(s) listed below (out of %d) failed. See error output above for details:\n%s' % (
|
||
|
len(failed), len(passed) + len(failed), '\n'.join(target.name for target in failed)))
|
||
|
|
||
8 years ago
|
|
||
7 years ago
|
def start_httptester(args):
|
||
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:rtype: str, list[str]
|
||
|
"""
|
||
|
|
||
|
# map ports from remote -> localhost -> container
|
||
|
# passing through localhost is only used when ansible-test is not already running inside a docker container
|
||
|
ports = [
|
||
|
dict(
|
||
|
remote=8080,
|
||
|
container=80,
|
||
|
),
|
||
|
dict(
|
||
|
remote=8443,
|
||
|
container=443,
|
||
|
),
|
||
|
]
|
||
|
|
||
|
container_id = get_docker_container_id()
|
||
|
|
||
|
if container_id:
|
||
|
display.info('Running in docker container: %s' % container_id, verbosity=1)
|
||
|
else:
|
||
|
for item in ports:
|
||
|
item['localhost'] = get_available_port()
|
||
|
|
||
|
docker_pull(args, args.httptester)
|
||
|
|
||
|
httptester_id = run_httptester(args, dict((port['localhost'], port['container']) for port in ports if 'localhost' in port))
|
||
|
|
||
|
if container_id:
|
||
|
container_host = get_docker_container_ip(args, httptester_id)
|
||
|
display.info('Found httptester container address: %s' % container_host, verbosity=1)
|
||
|
else:
|
||
|
container_host = 'localhost'
|
||
|
|
||
|
ssh_options = []
|
||
|
|
||
|
for port in ports:
|
||
|
ssh_options += ['-R', '%d:%s:%d' % (port['remote'], container_host, port.get('localhost', port['container']))]
|
||
|
|
||
|
return httptester_id, ssh_options
|
||
|
|
||
|
|
||
|
def run_httptester(args, ports=None):
|
||
|
"""
|
||
|
:type args: EnvironmentConfig
|
||
|
:type ports: dict[int, int] | None
|
||
|
:rtype: str
|
||
|
"""
|
||
|
options = [
|
||
|
'--detach',
|
||
|
]
|
||
|
|
||
|
if ports:
|
||
|
for localhost_port, container_port in ports.items():
|
||
|
options += ['-p', '%d:%d' % (localhost_port, container_port)]
|
||
|
|
||
5 years ago
|
httptester_id = docker_run(args, args.httptester, options=options)[0]
|
||
7 years ago
|
|
||
|
if args.explain:
|
||
|
httptester_id = 'httptester_id'
|
||
|
else:
|
||
|
httptester_id = httptester_id.strip()
|
||
|
|
||
|
return httptester_id
|
||
|
|
||
|
|
||
|
def inject_httptester(args):
|
||
|
"""
|
||
|
:type args: CommonConfig
|
||
|
"""
|
||
|
comment = ' # ansible-test httptester\n'
|
||
|
append_lines = ['127.0.0.1 %s%s' % (host, comment) for host in HTTPTESTER_HOSTS]
|
||
|
|
||
|
with open('/etc/hosts', 'r+') as hosts_fd:
|
||
|
original_lines = hosts_fd.readlines()
|
||
|
|
||
|
if not any(line.endswith(comment) for line in original_lines):
|
||
|
hosts_fd.writelines(append_lines)
|
||
|
|
||
|
# determine which forwarding mechanism to use
|
||
|
pfctl = find_executable('pfctl', required=False)
|
||
|
iptables = find_executable('iptables', required=False)
|
||
|
|
||
|
if pfctl:
|
||
|
kldload = find_executable('kldload', required=False)
|
||
|
|
||
|
if kldload:
|
||
|
try:
|
||
|
run_command(args, ['kldload', 'pf'], capture=True)
|
||
|
except SubprocessError:
|
||
|
pass # already loaded
|
||
|
|
||
|
rules = '''
|
||
|
rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
|
||
|
rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 8443
|
||
|
'''
|
||
|
cmd = ['pfctl', '-ef', '-']
|
||
|
|
||
|
try:
|
||
|
run_command(args, cmd, capture=True, data=rules)
|
||
|
except SubprocessError:
|
||
|
pass # non-zero exit status on success
|
||
|
|
||
|
elif iptables:
|
||
|
ports = [
|
||
|
(80, 8080),
|
||
|
(443, 8443),
|
||
|
]
|
||
|
|
||
|
for src, dst in ports:
|
||
|
rule = ['-o', 'lo', '-p', 'tcp', '--dport', str(src), '-j', 'REDIRECT', '--to-port', str(dst)]
|
||
|
|
||
|
try:
|
||
|
# check for existing rule
|
||
|
cmd = ['iptables', '-t', 'nat', '-C', 'OUTPUT'] + rule
|
||
|
run_command(args, cmd, capture=True)
|
||
|
except SubprocessError:
|
||
|
# append rule when it does not exist
|
||
|
cmd = ['iptables', '-t', 'nat', '-A', 'OUTPUT'] + rule
|
||
|
run_command(args, cmd, capture=True)
|
||
|
else:
|
||
|
raise ApplicationError('No supported port forwarding mechanism detected.')
|
||
|
|
||
|
|
||
6 years ago
|
def run_setup_targets(args, test_dir, target_names, targets_dict, targets_executed, inventory_path, temp_path, always):
|
||
7 years ago
|
"""
|
||
7 years ago
|
:type args: IntegrationConfig
|
||
|
:type test_dir: str
|
||
|
:type target_names: list[str]
|
||
|
:type targets_dict: dict[str, IntegrationTarget]
|
||
|
:type targets_executed: set[str]
|
||
6 years ago
|
:type inventory_path: str
|
||
6 years ago
|
:type temp_path: str
|
||
7 years ago
|
:type always: bool
|
||
7 years ago
|
"""
|
||
|
for target_name in target_names:
|
||
|
if not always and target_name in targets_executed:
|
||
|
continue
|
||
|
|
||
|
target = targets_dict[target_name]
|
||
|
|
||
|
if not args.explain:
|
||
|
# create a fresh test directory for each test target
|
||
|
remove_tree(test_dir)
|
||
|
make_dirs(test_dir)
|
||
|
|
||
|
if target.script_path:
|
||
6 years ago
|
command_integration_script(args, target, test_dir, inventory_path, temp_path)
|
||
7 years ago
|
else:
|
||
6 years ago
|
command_integration_role(args, target, None, test_dir, inventory_path, temp_path)
|
||
7 years ago
|
|
||
|
targets_executed.add(target_name)
|
||
|
|
||
|
|
||
6 years ago
|
def integration_environment(args, target, test_dir, inventory_path, ansible_config, env_config):
|
||
8 years ago
|
"""
|
||
|
:type args: IntegrationConfig
|
||
8 years ago
|
:type target: IntegrationTarget
|
||
6 years ago
|
:type test_dir: str
|
||
6 years ago
|
:type inventory_path: str
|
||
6 years ago
|
:type ansible_config: str | None
|
||
6 years ago
|
:type env_config: CloudEnvironmentConfig | None
|
||
8 years ago
|
:rtype: dict[str, str]
|
||
|
"""
|
||
6 years ago
|
env = ansible_environment(args, ansible_config=ansible_config)
|
||
8 years ago
|
|
||
7 years ago
|
if args.inject_httptester:
|
||
|
env.update(dict(
|
||
|
HTTPTESTER='1',
|
||
|
))
|
||
|
|
||
6 years ago
|
callback_plugins = ['junit'] + (env_config.callback_plugins or [] if env_config else [])
|
||
|
|
||
8 years ago
|
integration = dict(
|
||
|
JUNIT_OUTPUT_DIR=os.path.abspath('test/results/junit'),
|
||
6 years ago
|
ANSIBLE_CALLBACK_WHITELIST=','.join(sorted(set(callback_plugins))),
|
||
7 years ago
|
ANSIBLE_TEST_CI=args.metadata.ci_provider,
|
||
6 years ago
|
ANSIBLE_TEST_COVERAGE='check' if args.coverage_check else ('yes' if args.coverage else ''),
|
||
6 years ago
|
OUTPUT_DIR=test_dir,
|
||
6 years ago
|
INVENTORY_PATH=os.path.abspath(inventory_path),
|
||
8 years ago
|
)
|
||
|
|
||
7 years ago
|
if args.debug_strategy:
|
||
|
env.update(dict(ANSIBLE_STRATEGY='debug'))
|
||
|
|
||
7 years ago
|
if 'non_local/' in target.aliases:
|
||
|
if args.coverage:
|
||
6 years ago
|
display.warning('Skipping coverage reporting on Ansible modules for non-local test: %s' % target.name)
|
||
7 years ago
|
|
||
|
env.update(dict(ANSIBLE_TEST_REMOTE_INTERPRETER=''))
|
||
|
|
||
8 years ago
|
env.update(integration)
|
||
|
|
||
|
return env
|
||
|
|
||
|
|
||
6 years ago
|
def command_integration_script(args, target, test_dir, inventory_path, temp_path):
|
||
8 years ago
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type target: IntegrationTarget
|
||
6 years ago
|
:type test_dir: str
|
||
6 years ago
|
:type inventory_path: str
|
||
6 years ago
|
:type temp_path: str
|
||
8 years ago
|
"""
|
||
|
display.info('Running %s integration test script' % target.name)
|
||
|
|
||
6 years ago
|
env_config = None
|
||
|
|
||
|
if isinstance(args, PosixIntegrationConfig):
|
||
|
cloud_environment = get_cloud_environment(args, target)
|
||
|
|
||
|
if cloud_environment:
|
||
|
env_config = cloud_environment.get_environment_config()
|
||
|
|
||
6 years ago
|
with integration_test_environment(args, target, inventory_path) as test_env:
|
||
|
cmd = ['./%s' % os.path.basename(target.script_path)]
|
||
8 years ago
|
|
||
6 years ago
|
if args.verbosity:
|
||
|
cmd.append('-' + ('v' * args.verbosity))
|
||
8 years ago
|
|
||
6 years ago
|
env = integration_environment(args, target, test_dir, test_env.inventory_path, test_env.ansible_config, env_config)
|
||
6 years ago
|
cwd = os.path.join(test_env.integration_dir, 'targets', target.name)
|
||
8 years ago
|
|
||
6 years ago
|
if env_config and env_config.env_vars:
|
||
|
env.update(env_config.env_vars)
|
||
|
|
||
|
with integration_test_config_file(args, env_config, test_env.integration_dir) as config_path:
|
||
|
if config_path:
|
||
|
cmd += ['-e', '@%s' % config_path]
|
||
|
|
||
6 years ago
|
module_coverage = 'non_local/' not in target.aliases
|
||
|
intercept_command(args, cmd, target_name=target.name, env=env, cwd=cwd, temp_path=temp_path, module_coverage=module_coverage)
|
||
8 years ago
|
|
||
|
|
||
6 years ago
|
def command_integration_role(args, target, start_at_task, test_dir, inventory_path, temp_path):
|
||
8 years ago
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type target: IntegrationTarget
|
||
7 years ago
|
:type start_at_task: str | None
|
||
6 years ago
|
:type test_dir: str
|
||
6 years ago
|
:type inventory_path: str
|
||
6 years ago
|
:type temp_path: str
|
||
8 years ago
|
"""
|
||
|
display.info('Running %s integration test role' % target.name)
|
||
|
|
||
6 years ago
|
env_config = None
|
||
|
|
||
8 years ago
|
if isinstance(args, WindowsIntegrationConfig):
|
||
8 years ago
|
hosts = 'windows'
|
||
|
gather_facts = False
|
||
8 years ago
|
elif isinstance(args, NetworkIntegrationConfig):
|
||
8 years ago
|
hosts = target.name[:target.name.find('_')]
|
||
|
gather_facts = False
|
||
|
else:
|
||
|
hosts = 'testhost'
|
||
|
gather_facts = True
|
||
|
|
||
8 years ago
|
cloud_environment = get_cloud_environment(args, target)
|
||
|
|
||
|
if cloud_environment:
|
||
6 years ago
|
env_config = cloud_environment.get_environment_config()
|
||
8 years ago
|
|
||
6 years ago
|
with integration_test_environment(args, target, inventory_path) as test_env:
|
||
6 years ago
|
play = dict(
|
||
|
hosts=hosts,
|
||
|
gather_facts=gather_facts,
|
||
|
vars_files=[
|
||
6 years ago
|
os.path.relpath(test_env.vars_file, test_env.integration_dir),
|
||
6 years ago
|
],
|
||
|
roles=[
|
||
|
target.name,
|
||
|
],
|
||
|
)
|
||
|
|
||
|
if env_config:
|
||
|
play.update(dict(
|
||
|
vars=env_config.ansible_vars,
|
||
|
environment=env_config.env_vars,
|
||
|
module_defaults=env_config.module_defaults,
|
||
|
))
|
||
|
|
||
|
playbook = json.dumps([play], indent=4, sort_keys=True)
|
||
|
|
||
6 years ago
|
with named_temporary_file(args=args, directory=test_env.integration_dir, prefix='%s-' % target.name, suffix='.yml', content=playbook) as playbook_path:
|
||
|
filename = os.path.basename(playbook_path)
|
||
6 years ago
|
|
||
6 years ago
|
display.info('>>> Playbook: %s\n%s' % (filename, playbook.strip()), verbosity=3)
|
||
6 years ago
|
|
||
6 years ago
|
cmd = ['ansible-playbook', filename, '-i', os.path.relpath(test_env.inventory_path, test_env.integration_dir)]
|
||
8 years ago
|
|
||
6 years ago
|
if start_at_task:
|
||
|
cmd += ['--start-at-task', start_at_task]
|
||
8 years ago
|
|
||
6 years ago
|
if args.tags:
|
||
|
cmd += ['--tags', args.tags]
|
||
8 years ago
|
|
||
6 years ago
|
if args.skip_tags:
|
||
|
cmd += ['--skip-tags', args.skip_tags]
|
||
8 years ago
|
|
||
6 years ago
|
if args.diff:
|
||
|
cmd += ['--diff']
|
||
8 years ago
|
|
||
6 years ago
|
if isinstance(args, NetworkIntegrationConfig):
|
||
|
if args.testcase:
|
||
|
cmd += ['-e', 'testcase=%s' % args.testcase]
|
||
8 years ago
|
|
||
6 years ago
|
if args.verbosity:
|
||
|
cmd.append('-' + ('v' * args.verbosity))
|
||
8 years ago
|
|
||
6 years ago
|
env = integration_environment(args, target, test_dir, test_env.inventory_path, test_env.ansible_config, env_config)
|
||
6 years ago
|
cwd = test_env.integration_dir
|
||
7 years ago
|
|
||
6 years ago
|
env['ANSIBLE_ROLES_PATH'] = os.path.abspath(os.path.join(test_env.integration_dir, 'targets'))
|
||
7 years ago
|
|
||
6 years ago
|
module_coverage = 'non_local/' not in target.aliases
|
||
|
intercept_command(args, cmd, target_name=target.name, env=env, cwd=cwd, temp_path=temp_path, module_coverage=module_coverage)
|
||
8 years ago
|
|
||
|
|
||
|
def command_units(args):
|
||
|
"""
|
||
|
:type args: UnitsConfig
|
||
|
"""
|
||
|
changes = get_changes_filter(args)
|
||
6 years ago
|
require = args.require + changes
|
||
5 years ago
|
include = walk_internal_targets(walk_units_targets(), args.include, args.exclude, require)
|
||
8 years ago
|
|
||
5 years ago
|
paths = [target.path for target in include]
|
||
|
remote_paths = [path for path in paths
|
||
|
if is_subdir(path, data_context().content.unit_module_path)
|
||
|
or is_subdir(path, data_context().content.unit_module_utils_path)]
|
||
|
|
||
|
if not paths:
|
||
|
raise AllTargetsSkipped()
|
||
|
|
||
|
if args.python and args.python in REMOTE_ONLY_PYTHON_VERSIONS and not remote_paths:
|
||
8 years ago
|
raise AllTargetsSkipped()
|
||
|
|
||
|
if args.delegate:
|
||
6 years ago
|
raise Delegate(require=changes, exclude=args.exclude)
|
||
8 years ago
|
|
||
|
version_commands = []
|
||
|
|
||
5 years ago
|
available_versions = get_available_python_versions(list(SUPPORTED_PYTHON_VERSIONS))
|
||
5 years ago
|
|
||
8 years ago
|
for version in SUPPORTED_PYTHON_VERSIONS:
|
||
|
# run all versions unless version given, in which case run only that version
|
||
7 years ago
|
if args.python and version != args.python_version:
|
||
8 years ago
|
continue
|
||
|
|
||
5 years ago
|
if not args.python and version not in available_versions:
|
||
|
display.warning("Skipping unit tests on Python %s due to missing interpreter." % version)
|
||
|
continue
|
||
|
|
||
6 years ago
|
if args.requirements_mode != 'skip':
|
||
|
install_command_requirements(args, version)
|
||
|
|
||
8 years ago
|
env = ansible_environment(args)
|
||
|
|
||
|
cmd = [
|
||
|
'pytest',
|
||
8 years ago
|
'--boxed',
|
||
8 years ago
|
'-r', 'a',
|
||
5 years ago
|
'-n', str(args.num_workers) if args.num_workers else 'auto',
|
||
8 years ago
|
'--color',
|
||
|
'yes' if args.color else 'no',
|
||
5 years ago
|
'-p', 'no:cacheprovider',
|
||
5 years ago
|
'-c', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest.ini'),
|
||
8 years ago
|
'--junit-xml',
|
||
|
'test/results/junit/python%s-units.xml' % version,
|
||
|
]
|
||
|
|
||
5 years ago
|
plugins = []
|
||
|
|
||
|
if args.coverage:
|
||
|
plugins.append('ansible_pytest_coverage')
|
||
|
|
||
5 years ago
|
if data_context().content.collection:
|
||
|
plugins.append('ansible_pytest_collections')
|
||
|
|
||
5 years ago
|
if plugins:
|
||
5 years ago
|
env['PYTHONPATH'] += ':%s' % os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest/plugins')
|
||
5 years ago
|
|
||
|
for plugin in plugins:
|
||
|
cmd.extend(['-p', plugin])
|
||
|
|
||
8 years ago
|
if args.collect_only:
|
||
|
cmd.append('--collect-only')
|
||
|
|
||
|
if args.verbosity:
|
||
|
cmd.append('-' + ('v' * args.verbosity))
|
||
|
|
||
5 years ago
|
if version in REMOTE_ONLY_PYTHON_VERSIONS:
|
||
|
test_paths = remote_paths
|
||
|
else:
|
||
|
test_paths = paths
|
||
|
|
||
|
if not test_paths:
|
||
|
continue
|
||
|
|
||
|
cmd.extend(test_paths)
|
||
8 years ago
|
|
||
|
version_commands.append((version, cmd, env))
|
||
|
|
||
6 years ago
|
if args.requirements_mode == 'only':
|
||
|
sys.exit()
|
||
|
|
||
8 years ago
|
for version, command, env in version_commands:
|
||
6 years ago
|
check_pyyaml(args, version)
|
||
|
|
||
8 years ago
|
display.info('Unit test with Python %s' % version)
|
||
8 years ago
|
|
||
|
try:
|
||
5 years ago
|
with coverage_context(args):
|
||
|
intercept_command(args, command, target_name='units', env=env, python_version=version)
|
||
8 years ago
|
except SubprocessError as ex:
|
||
|
# pytest exits with status code 5 when all tests are skipped, which isn't an error for our use case
|
||
|
if ex.status != 5:
|
||
|
raise
|
||
8 years ago
|
|
||
|
|
||
|
def get_changes_filter(args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
paths = detect_changes(args)
|
||
|
|
||
7 years ago
|
if not args.metadata.change_description:
|
||
|
if paths:
|
||
|
changes = categorize_changes(args, paths, args.command)
|
||
|
else:
|
||
|
changes = ChangeDescription()
|
||
|
|
||
|
args.metadata.change_description = changes
|
||
|
|
||
8 years ago
|
if paths is None:
|
||
|
return [] # change detection not enabled, do not filter targets
|
||
|
|
||
|
if not paths:
|
||
|
raise NoChangesDetected()
|
||
|
|
||
7 years ago
|
if args.metadata.change_description.targets is None:
|
||
8 years ago
|
raise NoTestsForChanges()
|
||
|
|
||
7 years ago
|
return args.metadata.change_description.targets
|
||
8 years ago
|
|
||
|
|
||
|
def detect_changes(args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
:rtype: list[str] | None
|
||
|
"""
|
||
8 years ago
|
if args.changed and is_shippable():
|
||
8 years ago
|
display.info('Shippable detected, collecting parameters from environment.')
|
||
|
paths = detect_changes_shippable(args)
|
||
|
elif args.changed_from or args.changed_path:
|
||
|
paths = args.changed_path or []
|
||
|
if args.changed_from:
|
||
|
with open(args.changed_from, 'r') as changes_fd:
|
||
|
paths += changes_fd.read().splitlines()
|
||
|
elif args.changed:
|
||
|
paths = detect_changes_local(args)
|
||
|
else:
|
||
|
return None # change detection not enabled
|
||
|
|
||
7 years ago
|
if paths is None:
|
||
|
return None # act as though change detection not enabled, do not filter targets
|
||
|
|
||
8 years ago
|
display.info('Detected changes in %d file(s).' % len(paths))
|
||
|
|
||
|
for path in paths:
|
||
|
display.info(path, verbosity=1)
|
||
|
|
||
|
return paths
|
||
|
|
||
|
|
||
|
def detect_changes_shippable(args):
|
||
|
"""Initialize change detection on Shippable.
|
||
8 years ago
|
:type args: TestConfig
|
||
7 years ago
|
:rtype: list[str] | None
|
||
8 years ago
|
"""
|
||
5 years ago
|
git = Git()
|
||
8 years ago
|
result = ShippableChanges(args, git)
|
||
|
|
||
|
if result.is_pr:
|
||
|
job_type = 'pull request'
|
||
|
elif result.is_tag:
|
||
|
job_type = 'tag'
|
||
|
else:
|
||
|
job_type = 'merge commit'
|
||
|
|
||
|
display.info('Processing %s for branch %s commit %s' % (job_type, result.branch, result.commit))
|
||
|
|
||
8 years ago
|
if not args.metadata.changes:
|
||
|
args.metadata.populate_changes(result.diff)
|
||
|
|
||
8 years ago
|
return result.paths
|
||
|
|
||
|
|
||
|
def detect_changes_local(args):
|
||
|
"""
|
||
|
:type args: TestConfig
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
5 years ago
|
git = Git()
|
||
8 years ago
|
result = LocalChanges(args, git)
|
||
|
|
||
|
display.info('Detected branch %s forked from %s at commit %s' % (
|
||
|
result.current_branch, result.fork_branch, result.fork_point))
|
||
|
|
||
|
if result.untracked and not args.untracked:
|
||
|
display.warning('Ignored %s untracked file(s). Use --untracked to include them.' %
|
||
|
len(result.untracked))
|
||
|
|
||
|
if result.committed and not args.committed:
|
||
|
display.warning('Ignored %s committed change(s). Omit --ignore-committed to include them.' %
|
||
|
len(result.committed))
|
||
|
|
||
|
if result.staged and not args.staged:
|
||
|
display.warning('Ignored %s staged change(s). Omit --ignore-staged to include them.' %
|
||
|
len(result.staged))
|
||
|
|
||
|
if result.unstaged and not args.unstaged:
|
||
|
display.warning('Ignored %s unstaged change(s). Omit --ignore-unstaged to include them.' %
|
||
|
len(result.unstaged))
|
||
|
|
||
|
names = set()
|
||
|
|
||
|
if args.tracked:
|
||
|
names |= set(result.tracked)
|
||
|
if args.untracked:
|
||
|
names |= set(result.untracked)
|
||
|
if args.committed:
|
||
|
names |= set(result.committed)
|
||
|
if args.staged:
|
||
|
names |= set(result.staged)
|
||
|
if args.unstaged:
|
||
|
names |= set(result.unstaged)
|
||
|
|
||
8 years ago
|
if not args.metadata.changes:
|
||
|
args.metadata.populate_changes(result.diff)
|
||
|
|
||
|
for path in result.untracked:
|
||
|
if is_binary_file(path):
|
||
|
args.metadata.changes[path] = ((0, 0),)
|
||
|
continue
|
||
|
|
||
|
with open(path, 'r') as source_fd:
|
||
|
line_count = len(source_fd.read().splitlines())
|
||
|
|
||
|
args.metadata.changes[path] = ((1, line_count),)
|
||
|
|
||
8 years ago
|
return sorted(names)
|
||
|
|
||
|
|
||
|
def get_integration_filter(args, targets):
|
||
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
if args.tox:
|
||
|
# tox has the same exclusions as the local environment
|
||
|
return get_integration_local_filter(args, targets)
|
||
|
|
||
|
if args.docker:
|
||
|
return get_integration_docker_filter(args, targets)
|
||
|
|
||
|
if args.remote:
|
||
|
return get_integration_remote_filter(args, targets)
|
||
|
|
||
|
return get_integration_local_filter(args, targets)
|
||
|
|
||
|
|
||
7 years ago
|
def common_integration_filter(args, targets, exclude):
|
||
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:type exclude: list[str]
|
||
|
"""
|
||
|
override_disabled = set(target for target in args.include if target.startswith('disabled/'))
|
||
|
|
||
|
if not args.allow_disabled:
|
||
|
skip = 'disabled/'
|
||
|
override = [target.name for target in targets if override_disabled & set(target.aliases)]
|
||
|
skipped = [target.name for target in targets if skip in target.aliases and target.name not in override]
|
||
|
if skipped:
|
||
|
exclude.extend(skipped)
|
||
|
display.warning('Excluding tests marked "%s" which require --allow-disabled or prefixing with "disabled/": %s'
|
||
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
|
override_unsupported = set(target for target in args.include if target.startswith('unsupported/'))
|
||
|
|
||
|
if not args.allow_unsupported:
|
||
|
skip = 'unsupported/'
|
||
|
override = [target.name for target in targets if override_unsupported & set(target.aliases)]
|
||
|
skipped = [target.name for target in targets if skip in target.aliases and target.name not in override]
|
||
|
if skipped:
|
||
|
exclude.extend(skipped)
|
||
|
display.warning('Excluding tests marked "%s" which require --allow-unsupported or prefixing with "unsupported/": %s'
|
||
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
|
override_unstable = set(target for target in args.include if target.startswith('unstable/'))
|
||
|
|
||
|
if args.allow_unstable_changed:
|
||
|
override_unstable |= set(args.metadata.change_description.focused_targets or [])
|
||
|
|
||
|
if not args.allow_unstable:
|
||
|
skip = 'unstable/'
|
||
|
override = [target.name for target in targets if override_unstable & set(target.aliases)]
|
||
|
skipped = [target.name for target in targets if skip in target.aliases and target.name not in override]
|
||
|
if skipped:
|
||
|
exclude.extend(skipped)
|
||
|
display.warning('Excluding tests marked "%s" which require --allow-unstable or prefixing with "unstable/": %s'
|
||
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
6 years ago
|
# only skip a Windows test if using --windows and all the --windows versions are defined in the aliases as skip/windows/%s
|
||
|
if isinstance(args, WindowsIntegrationConfig) and args.windows:
|
||
|
all_skipped = []
|
||
|
not_skipped = []
|
||
|
|
||
|
for target in targets:
|
||
|
if "skip/windows/" not in target.aliases:
|
||
|
continue
|
||
|
|
||
|
skip_valid = []
|
||
|
skip_missing = []
|
||
|
for version in args.windows:
|
||
|
if "skip/windows/%s/" % version in target.aliases:
|
||
|
skip_valid.append(version)
|
||
|
else:
|
||
|
skip_missing.append(version)
|
||
|
|
||
|
if skip_missing and skip_valid:
|
||
|
not_skipped.append((target.name, skip_valid, skip_missing))
|
||
|
elif skip_valid:
|
||
|
all_skipped.append(target.name)
|
||
|
|
||
|
if all_skipped:
|
||
|
exclude.extend(all_skipped)
|
||
|
skip_aliases = ["skip/windows/%s/" % w for w in args.windows]
|
||
|
display.warning('Excluding tests marked "%s" which are set to skip with --windows %s: %s'
|
||
|
% ('", "'.join(skip_aliases), ', '.join(args.windows), ', '.join(all_skipped)))
|
||
|
|
||
|
if not_skipped:
|
||
|
for target, skip_valid, skip_missing in not_skipped:
|
||
|
# warn when failing to skip due to lack of support for skipping only some versions
|
||
|
display.warning('Including test "%s" which was marked to skip for --windows %s but not %s.'
|
||
|
% (target, ', '.join(skip_valid), ', '.join(skip_missing)))
|
||
|
|
||
7 years ago
|
|
||
8 years ago
|
def get_integration_local_filter(args, targets):
|
||
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
exclude = []
|
||
|
|
||
7 years ago
|
common_integration_filter(args, targets, exclude)
|
||
|
|
||
7 years ago
|
if not args.allow_root and os.getuid() != 0:
|
||
8 years ago
|
skip = 'needs/root/'
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
7 years ago
|
display.warning('Excluding tests marked "%s" which require --allow-root or running as root: %s'
|
||
8 years ago
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
7 years ago
|
override_destructive = set(target for target in args.include if target.startswith('destructive/'))
|
||
8 years ago
|
|
||
7 years ago
|
if not args.allow_destructive:
|
||
8 years ago
|
skip = 'destructive/'
|
||
7 years ago
|
override = [target.name for target in targets if override_destructive & set(target.aliases)]
|
||
|
skipped = [target.name for target in targets if skip in target.aliases and target.name not in override]
|
||
8 years ago
|
if skipped:
|
||
7 years ago
|
exclude.extend(skipped)
|
||
|
display.warning('Excluding tests marked "%s" which require --allow-destructive or prefixing with "destructive/" to run locally: %s'
|
||
8 years ago
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
6 years ago
|
exclude_targets_by_python_version(targets, args.python_version, exclude)
|
||
8 years ago
|
|
||
8 years ago
|
return exclude
|
||
|
|
||
|
|
||
|
def get_integration_docker_filter(args, targets):
|
||
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
exclude = []
|
||
|
|
||
7 years ago
|
common_integration_filter(args, targets, exclude)
|
||
|
|
||
6 years ago
|
skip = 'skip/docker/'
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
|
display.warning('Excluding tests marked "%s" which cannot run under docker: %s'
|
||
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
8 years ago
|
if not args.docker_privileged:
|
||
|
skip = 'needs/privileged/'
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
|
display.warning('Excluding tests marked "%s" which require --docker-privileged to run under docker: %s'
|
||
|
% (skip.rstrip('/'), ', '.join(skipped)))
|
||
|
|
||
6 years ago
|
python_version = get_python_version(args, get_docker_completion(), args.docker_raw)
|
||
7 years ago
|
|
||
6 years ago
|
exclude_targets_by_python_version(targets, python_version, exclude)
|
||
8 years ago
|
|
||
|
return exclude
|
||
|
|
||
|
|
||
|
def get_integration_remote_filter(args, targets):
|
||
|
"""
|
||
|
:type args: IntegrationConfig
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
parts = args.remote.split('/', 1)
|
||
|
|
||
|
platform = parts[0]
|
||
|
|
||
|
exclude = []
|
||
|
|
||
7 years ago
|
common_integration_filter(args, targets, exclude)
|
||
|
|
||
8 years ago
|
skip = 'skip/%s/' % platform
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
7 years ago
|
display.warning('Excluding tests marked "%s" which are not supported on %s: %s'
|
||
8 years ago
|
% (skip.rstrip('/'), platform, ', '.join(skipped)))
|
||
|
|
||
6 years ago
|
skip = 'skip/%s/' % args.remote.replace('/', '')
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
|
display.warning('Excluding tests marked "%s" which are not supported on %s: %s'
|
||
6 years ago
|
% (skip.rstrip('/'), args.remote.replace('/', ' '), ', '.join(skipped)))
|
||
6 years ago
|
|
||
6 years ago
|
python_version = get_python_version(args, get_remote_completion(), args.remote)
|
||
|
|
||
|
exclude_targets_by_python_version(targets, python_version, exclude)
|
||
7 years ago
|
|
||
6 years ago
|
return exclude
|
||
|
|
||
|
|
||
|
def exclude_targets_by_python_version(targets, python_version, exclude):
|
||
|
"""
|
||
|
:type targets: tuple[IntegrationTarget]
|
||
|
:type python_version: str
|
||
|
:type exclude: list[str]
|
||
|
"""
|
||
|
if not python_version:
|
||
|
display.warning('Python version unknown. Unable to skip tests based on Python version.')
|
||
|
return
|
||
|
|
||
|
python_major_version = python_version.split('.')[0]
|
||
|
|
||
|
skip = 'skip/python%s/' % python_version
|
||
7 years ago
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
6 years ago
|
display.warning('Excluding tests marked "%s" which are not supported on python %s: %s'
|
||
7 years ago
|
% (skip.rstrip('/'), python_version, ', '.join(skipped)))
|
||
|
|
||
6 years ago
|
skip = 'skip/python%s/' % python_major_version
|
||
|
skipped = [target.name for target in targets if skip in target.aliases]
|
||
|
if skipped:
|
||
|
exclude.append(skip)
|
||
|
display.warning('Excluding tests marked "%s" which are not supported on python %s: %s'
|
||
|
% (skip.rstrip('/'), python_version, ', '.join(skipped)))
|
||
|
|
||
|
|
||
|
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
|
||
8 years ago
|
|
||
|
|
||
5 years ago
|
class EnvironmentDescription:
|
||
8 years ago
|
"""Description of current running environment."""
|
||
7 years ago
|
def __init__(self, args):
|
||
|
"""Initialize snapshot of environment configuration.
|
||
|
:type args: IntegrationConfig
|
||
|
"""
|
||
|
self.args = args
|
||
|
|
||
|
if self.args.explain:
|
||
|
self.data = {}
|
||
|
return
|
||
|
|
||
6 years ago
|
warnings = []
|
||
|
|
||
8 years ago
|
versions = ['']
|
||
|
versions += SUPPORTED_PYTHON_VERSIONS
|
||
|
versions += list(set(v.split('.')[0] for v in SUPPORTED_PYTHON_VERSIONS))
|
||
|
|
||
5 years ago
|
version_check = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'versions.py')
|
||
8 years ago
|
python_paths = dict((v, find_executable('python%s' % v, required=False)) for v in sorted(versions))
|
||
|
pip_paths = dict((v, find_executable('pip%s' % v, required=False)) for v in sorted(versions))
|
||
5 years ago
|
program_versions = dict((v, self.get_version([python_paths[v], version_check], warnings)) for v in sorted(python_paths) if python_paths[v])
|
||
8 years ago
|
pip_interpreters = dict((v, self.get_shebang(pip_paths[v])) for v in sorted(pip_paths) if pip_paths[v])
|
||
7 years ago
|
known_hosts_hash = self.get_hash(os.path.expanduser('~/.ssh/known_hosts'))
|
||
8 years ago
|
|
||
6 years ago
|
for version in sorted(versions):
|
||
|
self.check_python_pip_association(version, python_paths, pip_paths, pip_interpreters, warnings)
|
||
|
|
||
|
for warning in warnings:
|
||
|
display.warning(warning, unique=True)
|
||
|
|
||
8 years ago
|
self.data = dict(
|
||
|
python_paths=python_paths,
|
||
|
pip_paths=pip_paths,
|
||
6 years ago
|
program_versions=program_versions,
|
||
8 years ago
|
pip_interpreters=pip_interpreters,
|
||
7 years ago
|
known_hosts_hash=known_hosts_hash,
|
||
6 years ago
|
warnings=warnings,
|
||
8 years ago
|
)
|
||
|
|
||
6 years ago
|
@staticmethod
|
||
|
def check_python_pip_association(version, python_paths, pip_paths, pip_interpreters, warnings):
|
||
|
"""
|
||
|
:type version: str
|
||
|
:param python_paths: dict[str, str]
|
||
|
:param pip_paths: dict[str, str]
|
||
|
:param pip_interpreters: dict[str, str]
|
||
|
:param warnings: list[str]
|
||
|
"""
|
||
|
python_label = 'Python%s' % (' %s' % version if version else '')
|
||
|
|
||
|
pip_path = pip_paths.get(version)
|
||
|
python_path = python_paths.get(version)
|
||
|
|
||
|
if not python_path and not pip_path:
|
||
|
# neither python or pip is present for this version
|
||
|
return
|
||
|
|
||
|
if not python_path:
|
||
|
warnings.append('A %s interpreter was not found, yet a matching pip was found at "%s".' % (python_label, pip_path))
|
||
|
return
|
||
|
|
||
|
if not pip_path:
|
||
|
warnings.append('A %s interpreter was found at "%s", yet a matching pip was not found.' % (python_label, python_path))
|
||
|
return
|
||
|
|
||
|
pip_shebang = pip_interpreters.get(version)
|
||
|
|
||
|
match = re.search(r'#!\s*(?P<command>[^\s]+)', pip_shebang)
|
||
|
|
||
|
if not match:
|
||
|
warnings.append('A %s pip was found at "%s", but it does not have a valid shebang: %s' % (python_label, pip_path, pip_shebang))
|
||
|
return
|
||
|
|
||
|
pip_interpreter = os.path.realpath(match.group('command'))
|
||
|
python_interpreter = os.path.realpath(python_path)
|
||
|
|
||
|
if pip_interpreter == python_interpreter:
|
||
|
return
|
||
|
|
||
|
try:
|
||
|
identical = filecmp.cmp(pip_interpreter, python_interpreter)
|
||
|
except OSError:
|
||
|
identical = False
|
||
|
|
||
|
if identical:
|
||
|
return
|
||
|
|
||
|
warnings.append('A %s pip was found at "%s", but it uses interpreter "%s" instead of "%s".' % (
|
||
|
python_label, pip_path, pip_interpreter, python_interpreter))
|
||
|
|
||
8 years ago
|
def __str__(self):
|
||
|
"""
|
||
|
:rtype: str
|
||
|
"""
|
||
|
return json.dumps(self.data, sort_keys=True, indent=4)
|
||
|
|
||
|
def validate(self, target_name, throw):
|
||
|
"""
|
||
|
:type target_name: str
|
||
|
:type throw: bool
|
||
|
:rtype: bool
|
||
|
"""
|
||
7 years ago
|
current = EnvironmentDescription(self.args)
|
||
8 years ago
|
|
||
6 years ago
|
return self.check(self, current, target_name, throw)
|
||
|
|
||
|
@staticmethod
|
||
|
def check(original, current, target_name, throw):
|
||
|
"""
|
||
|
:type original: EnvironmentDescription
|
||
|
:type current: EnvironmentDescription
|
||
|
:type target_name: str
|
||
|
:type throw: bool
|
||
|
:rtype: bool
|
||
|
"""
|
||
|
original_json = str(original)
|
||
8 years ago
|
current_json = str(current)
|
||
|
|
||
|
if original_json == current_json:
|
||
|
return True
|
||
|
|
||
6 years ago
|
unified_diff = '\n'.join(difflib.unified_diff(
|
||
|
a=original_json.splitlines(),
|
||
|
b=current_json.splitlines(),
|
||
|
fromfile='original.json',
|
||
|
tofile='current.json',
|
||
|
lineterm='',
|
||
|
))
|
||
|
|
||
8 years ago
|
message = ('Test target "%s" has changed the test environment!\n'
|
||
|
'If these changes are necessary, they must be reverted before the test finishes.\n'
|
||
|
'>>> Original Environment\n'
|
||
|
'%s\n'
|
||
|
'>>> Current Environment\n'
|
||
6 years ago
|
'%s\n'
|
||
|
'>>> Environment Diff\n'
|
||
|
'%s'
|
||
|
% (target_name, original_json, current_json, unified_diff))
|
||
8 years ago
|
|
||
|
if throw:
|
||
|
raise ApplicationError(message)
|
||
|
|
||
|
display.error(message)
|
||
|
|
||
|
return False
|
||
|
|
||
|
@staticmethod
|
||
6 years ago
|
def get_version(command, warnings):
|
||
8 years ago
|
"""
|
||
|
:type command: list[str]
|
||
5 years ago
|
:type warnings: list[text]
|
||
6 years ago
|
:rtype: list[str]
|
||
8 years ago
|
"""
|
||
7 years ago
|
try:
|
||
|
stdout, stderr = raw_command(command, capture=True, cmd_verbosity=2)
|
||
6 years ago
|
except SubprocessError as ex:
|
||
|
warnings.append(u'%s' % ex)
|
||
7 years ago
|
return None # all failures are equal, we don't care why it failed, only that it did
|
||
|
|
||
6 years ago
|
return [line.strip() for line in ((stdout or '').strip() + (stderr or '').strip()).splitlines()]
|
||
8 years ago
|
|
||
|
@staticmethod
|
||
|
def get_shebang(path):
|
||
|
"""
|
||
|
:type path: str
|
||
|
:rtype: str
|
||
|
"""
|
||
|
with open(path) as script_fd:
|
||
6 years ago
|
return script_fd.readline().strip()
|
||
8 years ago
|
|
||
7 years ago
|
@staticmethod
|
||
|
def get_hash(path):
|
||
|
"""
|
||
|
:type path: str
|
||
|
:rtype: str | None
|
||
|
"""
|
||
|
if not os.path.exists(path):
|
||
|
return None
|
||
|
|
||
|
file_hash = hashlib.md5()
|
||
|
|
||
|
with open(path, 'rb') as file_fd:
|
||
|
file_hash.update(file_fd.read())
|
||
|
|
||
|
return file_hash.hexdigest()
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
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."""
|
||
7 years ago
|
def __init__(self, exclude=None, require=None, integration_targets=None):
|
||
8 years ago
|
"""
|
||
|
:type exclude: list[str] | None
|
||
|
:type require: list[str] | None
|
||
7 years ago
|
:type integration_targets: tuple[IntegrationTarget] | None
|
||
8 years ago
|
"""
|
||
|
super(Delegate, self).__init__()
|
||
|
|
||
|
self.exclude = exclude or []
|
||
|
self.require = require or []
|
||
7 years ago
|
self.integration_targets = integration_targets or tuple()
|
||
8 years ago
|
|
||
|
|
||
|
class AllTargetsSkipped(ApplicationWarning):
|
||
|
"""All targets skipped."""
|
||
|
def __init__(self):
|
||
|
super(AllTargetsSkipped, self).__init__('All targets skipped.')
|