2018-12-21 07:08:57 +01:00
|
|
|
"""Show information about the test environment."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-12-21 07:08:57 +01:00
|
|
|
|
|
|
|
import datetime
|
2019-03-05 20:58:13 +01:00
|
|
|
import functools
|
2018-12-21 07:08:57 +01:00
|
|
|
import os
|
|
|
|
import platform
|
2019-03-05 20:58:13 +01:00
|
|
|
import signal
|
2018-12-21 07:08:57 +01:00
|
|
|
import sys
|
2019-03-05 20:58:13 +01:00
|
|
|
import time
|
2018-12-21 07:08:57 +01:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .config import (
|
2018-12-21 07:08:57 +01:00
|
|
|
CommonConfig,
|
2019-03-05 20:58:13 +01:00
|
|
|
TestConfig,
|
2018-12-21 07:08:57 +01:00
|
|
|
)
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
from .io import (
|
|
|
|
write_json_file,
|
|
|
|
read_json_file,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2018-12-21 07:08:57 +01:00
|
|
|
display,
|
|
|
|
find_executable,
|
|
|
|
SubprocessError,
|
|
|
|
ApplicationError,
|
2019-08-26 23:02:55 +02:00
|
|
|
get_ansible_version,
|
|
|
|
get_available_python_versions,
|
2018-12-21 07:08:57 +01:00
|
|
|
)
|
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
from .util_common import (
|
2020-04-08 10:15:49 +02:00
|
|
|
data_context,
|
2019-08-28 08:40:06 +02:00
|
|
|
write_json_test_results,
|
|
|
|
ResultType,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .docker_util import (
|
2018-12-21 07:08:57 +01:00
|
|
|
docker_info,
|
|
|
|
docker_version
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .thread import (
|
2019-03-05 20:58:13 +01:00
|
|
|
WrappedThread,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .constants import (
|
2019-03-05 20:58:13 +01:00
|
|
|
TIMEOUT_PATH,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .test import (
|
2019-03-05 20:58:13 +01:00
|
|
|
TestTimeout,
|
|
|
|
)
|
|
|
|
|
2019-08-26 23:02:55 +02:00
|
|
|
from .executor import (
|
|
|
|
SUPPORTED_PYTHON_VERSIONS,
|
|
|
|
)
|
|
|
|
|
2020-04-26 03:55:39 +02:00
|
|
|
from .ci import (
|
|
|
|
get_ci_provider,
|
|
|
|
)
|
|
|
|
|
2018-12-21 07:08:57 +01:00
|
|
|
|
|
|
|
class EnvConfig(CommonConfig):
|
|
|
|
"""Configuration for the tools command."""
|
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
|
|
|
:type args: any
|
|
|
|
"""
|
|
|
|
super(EnvConfig, self).__init__(args, 'env')
|
|
|
|
|
2019-03-05 20:58:13 +01:00
|
|
|
self.show = args.show
|
2018-12-21 07:08:57 +01:00
|
|
|
self.dump = args.dump
|
2019-03-05 20:58:13 +01:00
|
|
|
self.timeout = args.timeout
|
2020-04-08 10:15:49 +02:00
|
|
|
self.list_files = args.list_files
|
2018-12-21 07:08:57 +01:00
|
|
|
|
2020-04-08 10:15:49 +02:00
|
|
|
if not self.show and not self.dump and self.timeout is None and not self.list_files:
|
2019-08-26 23:02:55 +02:00
|
|
|
# default to --show if no options were given
|
|
|
|
self.show = True
|
|
|
|
|
2018-12-21 07:08:57 +01:00
|
|
|
|
|
|
|
def command_env(args):
|
|
|
|
"""
|
|
|
|
:type args: EnvConfig
|
|
|
|
"""
|
2019-03-05 20:58:13 +01:00
|
|
|
show_dump_env(args)
|
2020-04-08 10:15:49 +02:00
|
|
|
list_files_env(args)
|
2019-03-05 20:58:13 +01:00
|
|
|
set_timeout(args)
|
|
|
|
|
|
|
|
|
|
|
|
def show_dump_env(args):
|
|
|
|
"""
|
|
|
|
:type args: EnvConfig
|
|
|
|
"""
|
|
|
|
if not args.show and not args.dump:
|
|
|
|
return
|
|
|
|
|
2018-12-21 07:08:57 +01:00
|
|
|
data = dict(
|
|
|
|
ansible=dict(
|
2019-08-09 01:14:19 +02:00
|
|
|
version=get_ansible_version(),
|
2018-12-21 07:08:57 +01:00
|
|
|
),
|
|
|
|
docker=get_docker_details(args),
|
|
|
|
environ=os.environ.copy(),
|
2019-08-26 23:02:55 +02:00
|
|
|
location=dict(
|
|
|
|
pwd=os.environ.get('PWD', None),
|
|
|
|
cwd=os.getcwd(),
|
|
|
|
),
|
2020-04-26 03:55:39 +02:00
|
|
|
git=get_ci_provider().get_git_details(args),
|
2018-12-21 07:08:57 +01:00
|
|
|
platform=dict(
|
|
|
|
datetime=datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ'),
|
|
|
|
platform=platform.platform(),
|
|
|
|
uname=platform.uname(),
|
|
|
|
),
|
|
|
|
python=dict(
|
|
|
|
executable=sys.executable,
|
|
|
|
version=platform.python_version(),
|
|
|
|
),
|
2019-08-26 23:02:55 +02:00
|
|
|
interpreters=get_available_python_versions(SUPPORTED_PYTHON_VERSIONS),
|
2018-12-21 07:08:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if args.show:
|
|
|
|
verbose = {
|
|
|
|
'docker': 3,
|
|
|
|
'docker.executable': 0,
|
|
|
|
'environ': 2,
|
|
|
|
'platform.uname': 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
show_dict(data, verbose)
|
|
|
|
|
|
|
|
if args.dump and not args.explain:
|
2019-08-28 08:40:06 +02:00
|
|
|
write_json_test_results(ResultType.BOT, 'data-environment.json', data)
|
2018-12-21 07:08:57 +01:00
|
|
|
|
|
|
|
|
2020-04-08 10:15:49 +02:00
|
|
|
def list_files_env(args): # type: (EnvConfig) -> None
|
|
|
|
"""List files on stdout."""
|
|
|
|
if not args.list_files:
|
|
|
|
return
|
|
|
|
|
|
|
|
for path in data_context().content.all_files():
|
|
|
|
display.info(path)
|
|
|
|
|
|
|
|
|
2019-03-05 20:58:13 +01:00
|
|
|
def set_timeout(args):
|
|
|
|
"""
|
|
|
|
:type args: EnvConfig
|
|
|
|
"""
|
|
|
|
if args.timeout is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if args.timeout:
|
|
|
|
deadline = (datetime.datetime.utcnow() + datetime.timedelta(minutes=args.timeout)).strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
|
|
|
|
display.info('Setting a %d minute test timeout which will end at: %s' % (args.timeout, deadline), verbosity=1)
|
|
|
|
else:
|
|
|
|
deadline = None
|
|
|
|
|
|
|
|
display.info('Clearing existing test timeout.', verbosity=1)
|
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return
|
|
|
|
|
|
|
|
if deadline:
|
|
|
|
data = dict(
|
|
|
|
duration=args.timeout,
|
|
|
|
deadline=deadline,
|
|
|
|
)
|
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
write_json_file(TIMEOUT_PATH, data)
|
2019-03-05 20:58:13 +01:00
|
|
|
elif os.path.exists(TIMEOUT_PATH):
|
|
|
|
os.remove(TIMEOUT_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
def get_timeout():
|
|
|
|
"""
|
|
|
|
:rtype: dict[str, any] | None
|
|
|
|
"""
|
|
|
|
if not os.path.exists(TIMEOUT_PATH):
|
|
|
|
return None
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
data = read_json_file(TIMEOUT_PATH)
|
2019-03-05 20:58:13 +01:00
|
|
|
data['deadline'] = datetime.datetime.strptime(data['deadline'], '%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def configure_timeout(args):
|
|
|
|
"""
|
|
|
|
:type args: CommonConfig
|
|
|
|
"""
|
|
|
|
if isinstance(args, TestConfig):
|
|
|
|
configure_test_timeout(args) # only tests are subject to the timeout
|
|
|
|
|
|
|
|
|
|
|
|
def configure_test_timeout(args):
|
|
|
|
"""
|
|
|
|
:type args: TestConfig
|
|
|
|
"""
|
|
|
|
timeout = get_timeout()
|
|
|
|
|
|
|
|
if not timeout:
|
|
|
|
return
|
|
|
|
|
|
|
|
timeout_start = datetime.datetime.utcnow()
|
|
|
|
timeout_duration = timeout['duration']
|
|
|
|
timeout_deadline = timeout['deadline']
|
|
|
|
timeout_remaining = timeout_deadline - timeout_start
|
|
|
|
|
|
|
|
test_timeout = TestTimeout(timeout_duration)
|
|
|
|
|
|
|
|
if timeout_remaining <= datetime.timedelta():
|
|
|
|
test_timeout.write(args)
|
|
|
|
|
|
|
|
raise ApplicationError('The %d minute test timeout expired %s ago at %s.' % (
|
|
|
|
timeout_duration, timeout_remaining * -1, timeout_deadline))
|
|
|
|
|
|
|
|
display.info('The %d minute test timeout expires in %s at %s.' % (
|
|
|
|
timeout_duration, timeout_remaining, timeout_deadline), verbosity=1)
|
|
|
|
|
|
|
|
def timeout_handler(_dummy1, _dummy2):
|
|
|
|
"""Runs when SIGUSR1 is received."""
|
|
|
|
test_timeout.write(args)
|
|
|
|
|
|
|
|
raise ApplicationError('Tests aborted after exceeding the %d minute time limit.' % timeout_duration)
|
|
|
|
|
|
|
|
def timeout_waiter(timeout_seconds):
|
|
|
|
"""
|
|
|
|
:type timeout_seconds: int
|
|
|
|
"""
|
|
|
|
time.sleep(timeout_seconds)
|
|
|
|
os.kill(os.getpid(), signal.SIGUSR1)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGUSR1, timeout_handler)
|
|
|
|
|
|
|
|
instance = WrappedThread(functools.partial(timeout_waiter, timeout_remaining.seconds))
|
|
|
|
instance.daemon = True
|
|
|
|
instance.start()
|
|
|
|
|
|
|
|
|
2018-12-21 07:08:57 +01:00
|
|
|
def show_dict(data, verbose, root_verbosity=0, path=None):
|
|
|
|
"""
|
|
|
|
:type data: dict[str, any]
|
|
|
|
:type verbose: dict[str, int]
|
|
|
|
:type root_verbosity: int
|
|
|
|
:type path: list[str] | None
|
|
|
|
"""
|
|
|
|
path = path if path else []
|
|
|
|
|
|
|
|
for key, value in sorted(data.items()):
|
|
|
|
indent = ' ' * len(path)
|
|
|
|
key_path = path + [key]
|
|
|
|
key_name = '.'.join(key_path)
|
|
|
|
verbosity = verbose.get(key_name, root_verbosity)
|
|
|
|
|
|
|
|
if isinstance(value, (tuple, list)):
|
|
|
|
display.info(indent + '%s:' % key, verbosity=verbosity)
|
|
|
|
for item in value:
|
|
|
|
display.info(indent + ' - %s' % item, verbosity=verbosity)
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
min_verbosity = min([verbosity] + [v for k, v in verbose.items() if k.startswith('%s.' % key)])
|
|
|
|
display.info(indent + '%s:' % key, verbosity=min_verbosity)
|
|
|
|
show_dict(value, verbose, verbosity, key_path)
|
|
|
|
else:
|
|
|
|
display.info(indent + '%s: %s' % (key, value), verbosity=verbosity)
|
|
|
|
|
|
|
|
|
|
|
|
def get_docker_details(args):
|
|
|
|
"""
|
|
|
|
:type args: CommonConfig
|
|
|
|
:rtype: dict[str, any]
|
|
|
|
"""
|
|
|
|
docker = find_executable('docker', required=False)
|
|
|
|
info = None
|
|
|
|
version = None
|
|
|
|
|
|
|
|
if docker:
|
|
|
|
try:
|
|
|
|
info = docker_info(args)
|
|
|
|
except SubprocessError as ex:
|
|
|
|
display.warning('Failed to collect docker info:\n%s' % ex)
|
|
|
|
|
|
|
|
try:
|
|
|
|
version = docker_version(args)
|
|
|
|
except SubprocessError as ex:
|
|
|
|
display.warning('Failed to collect docker version:\n%s' % ex)
|
|
|
|
|
|
|
|
docker_details = dict(
|
|
|
|
executable=docker,
|
|
|
|
info=info,
|
|
|
|
version=version,
|
|
|
|
)
|
|
|
|
|
|
|
|
return docker_details
|