diff options
-rw-r--r-- | changelogs/fragments/ansible-test-probe-error-handling.yml | 3 | ||||
-rw-r--r-- | test/lib/ansible_test/_internal/docker_util.py | 10 | ||||
-rw-r--r-- | test/lib/ansible_test/_internal/util.py | 24 |
3 files changed, 28 insertions, 9 deletions
diff --git a/changelogs/fragments/ansible-test-probe-error-handling.yml b/changelogs/fragments/ansible-test-probe-error-handling.yml new file mode 100644 index 0000000000..bf4301cc48 --- /dev/null +++ b/changelogs/fragments/ansible-test-probe-error-handling.yml @@ -0,0 +1,3 @@ +minor_changes: + - ansible-test - Improve container runtime probe error handling. + When unexpected probe output is encountered, an error with more useful debugging information is provided. diff --git a/test/lib/ansible_test/_internal/docker_util.py b/test/lib/ansible_test/_internal/docker_util.py index 6bdcf927b7..0fd22fbcec 100644 --- a/test/lib/ansible_test/_internal/docker_util.py +++ b/test/lib/ansible_test/_internal/docker_util.py @@ -20,6 +20,8 @@ from .util import ( SubprocessError, cache, OutputStream, + InternalError, + format_command_output, ) from .util_common import ( @@ -300,7 +302,7 @@ def detect_host_properties(args: CommonConfig) -> ContainerHostProperties: options = ['--volume', '/sys/fs/cgroup:/probe:ro'] cmd = ['sh', '-c', ' && echo "-" && '.join(multi_line_commands)] - stdout = run_utility_container(args, 'ansible-test-probe', cmd, options)[0] + stdout, stderr = run_utility_container(args, 'ansible-test-probe', cmd, options) if args.explain: return ContainerHostProperties( @@ -313,6 +315,12 @@ def detect_host_properties(args: CommonConfig) -> ContainerHostProperties: blocks = stdout.split('\n-\n') + if len(blocks) != len(multi_line_commands): + message = f'Unexpected probe output. Expected {len(multi_line_commands)} blocks but found {len(blocks)}.\n' + message += format_command_output(stdout, stderr) + + raise InternalError(message.strip()) + values = blocks[0].split('\n') audit_parts = values[0].split(' ', 1) diff --git a/test/lib/ansible_test/_internal/util.py b/test/lib/ansible_test/_internal/util.py index 23d6a81209..b83f3d5db7 100644 --- a/test/lib/ansible_test/_internal/util.py +++ b/test/lib/ansible_test/_internal/util.py @@ -970,14 +970,7 @@ class SubprocessError(ApplicationError): error_callback: t.Optional[c.Callable[[SubprocessError], None]] = None, ) -> None: message = 'Command "%s" returned exit status %s.\n' % (shlex.join(cmd), status) - - if stderr: - message += '>>> Standard Error\n' - message += '%s%s\n' % (stderr.strip(), Display.clear) - - if stdout: - message += '>>> Standard Output\n' - message += '%s%s\n' % (stdout.strip(), Display.clear) + message += format_command_output(stdout, stderr) self.cmd = cmd self.message = message @@ -1021,6 +1014,21 @@ class HostConnectionError(ApplicationError): self._callback() +def format_command_output(stdout: str, stderr: str) -> str: + """Return a formatted string containing the given stdout and stderr (if any).""" + message = '' + + if stderr := stderr.strip(): + message += '>>> Standard Error\n' + message += f'{stderr}{Display.clear}\n' + + if stdout := stdout.strip(): + message += '>>> Standard Output\n' + message += f'{stdout}{Display.clear}\n' + + return message + + def retry(func: t.Callable[..., TValue], ex_type: t.Type[BaseException] = SubprocessError, sleep: int = 10, attempts: int = 10, warn: bool = True) -> TValue: """Retry the specified function on failure.""" for dummy in range(1, attempts): |