| 1 |
"""Test runner for all Ansible tests.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import typing as t |
| 8 |
|
| 9 |
# This import should occur as early as possible. |
| 10 |
# It must occur before subprocess has been imported anywhere in the current process. |
| 11 |
from .init import ( |
| 12 |
CURRENT_RLIMIT_NOFILE, |
| 13 |
) |
| 14 |
|
| 15 |
from .constants import ( |
| 16 |
STATUS_HOST_CONNECTION_ERROR, |
| 17 |
) |
| 18 |
|
| 19 |
from .util import ( |
| 20 |
ApplicationError, |
| 21 |
HostConnectionError, |
| 22 |
TimeoutExpiredError, |
| 23 |
display, |
| 24 |
report_locale, |
| 25 |
) |
| 26 |
|
| 27 |
from .delegation import ( |
| 28 |
delegate, |
| 29 |
) |
| 30 |
|
| 31 |
from .executor import ( |
| 32 |
ApplicationWarning, |
| 33 |
Delegate, |
| 34 |
ListTargets, |
| 35 |
) |
| 36 |
|
| 37 |
from .timeout import ( |
| 38 |
configure_timeout, |
| 39 |
) |
| 40 |
|
| 41 |
from .data import ( |
| 42 |
data_context, |
| 43 |
) |
| 44 |
|
| 45 |
from .util_common import ( |
| 46 |
CommonConfig, |
| 47 |
ExitHandler, |
| 48 |
) |
| 49 |
|
| 50 |
from .cli import ( |
| 51 |
parse_args, |
| 52 |
) |
| 53 |
|
| 54 |
from .provisioning import ( |
| 55 |
PrimeContainers, |
| 56 |
) |
| 57 |
|
| 58 |
from .config import ( |
| 59 |
TestConfig, |
| 60 |
) |
| 61 |
|
| 62 |
from .debugging import ( |
| 63 |
initialize_debugger, |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def main(cli_args: t.Optional[list[str]] = None) -> None: |
| 68 |
"""Wrapper around the main program function to invoke cleanup functions at exit.""" |
| 69 |
with ExitHandler.context(): |
| 70 |
main_internal(cli_args) |
| 71 |
|
| 72 |
|
| 73 |
def main_internal(cli_args: t.Optional[list[str]] = None) -> None: |
| 74 |
"""Main program function.""" |
| 75 |
try: |
| 76 |
os.chdir(data_context().content.root) |
| 77 |
args = parse_args(cli_args) |
| 78 |
config: CommonConfig = args.config(args) |
| 79 |
display.verbosity = config.verbosity |
| 80 |
display.truncate = config.truncate |
| 81 |
display.redact = config.redact |
| 82 |
display.color = config.color |
| 83 |
display.fd = sys.stderr if config.display_stderr else sys.stdout |
| 84 |
initialize_debugger(config) |
| 85 |
configure_timeout(config) |
| 86 |
report_locale(isinstance(config, TestConfig) and not config.delegate) |
| 87 |
|
| 88 |
display.info('RLIMIT_NOFILE: %s' % (CURRENT_RLIMIT_NOFILE,), verbosity=2) |
| 89 |
|
| 90 |
delegate_args = None |
| 91 |
target_names = None |
| 92 |
|
| 93 |
try: |
| 94 |
if config.check_layout: |
| 95 |
data_context().check_layout() |
| 96 |
|
| 97 |
args.func(config) |
| 98 |
except PrimeContainers: |
| 99 |
pass |
| 100 |
except ListTargets as ex: |
| 101 |
# save target_names for use once we exit the exception handler |
| 102 |
target_names = ex.target_names |
| 103 |
except Delegate as ex: |
| 104 |
# save delegation args for use once we exit the exception handler |
| 105 |
delegate_args = (ex.host_state, ex.exclude, ex.require) |
| 106 |
|
| 107 |
if delegate_args: |
| 108 |
delegate(config, *delegate_args) |
| 109 |
|
| 110 |
if target_names: |
| 111 |
for target_name in target_names: |
| 112 |
print(target_name) # display goes to stderr, this should be on stdout |
| 113 |
|
| 114 |
display.review_warnings() |
| 115 |
config.success = True |
| 116 |
except HostConnectionError as ex: |
| 117 |
display.fatal(str(ex)) |
| 118 |
ex.run_callback() |
| 119 |
sys.exit(STATUS_HOST_CONNECTION_ERROR) |
| 120 |
except ApplicationWarning as ex: |
| 121 |
display.warning('%s' % ex) |
| 122 |
sys.exit(0) |
| 123 |
except ApplicationError as ex: |
| 124 |
display.fatal('%s' % ex) |
| 125 |
sys.exit(1) |
| 126 |
except TimeoutExpiredError as ex: |
| 127 |
display.fatal('%s' % ex) |
| 128 |
sys.exit(1) |
| 129 |
except KeyboardInterrupt: |
| 130 |
sys.exit(2) |
| 131 |
except BrokenPipeError: |
| 132 |
sys.exit(3) |