ansible-test - Fix container error handling. (#78739)

pull/76313/merge
Matt Clay 2 years ago committed by GitHub
parent 2e5e8026cc
commit 79f67ed561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- ansible-test - Always remove containers after failing to create/run them.
This avoids leaving behind created containers when using podman.

@ -139,7 +139,7 @@ def run_support_container(
if current_container_id: if current_container_id:
publish_ports = False # publishing ports is pointless if already running in a docker container publish_ports = False # publishing ports is pointless if already running in a docker container
options = (options or []) + ['--name', name] options = (options or [])
if start: if start:
options.append('-d') options.append('-d')
@ -183,7 +183,7 @@ def run_support_container(
else: else:
display.info('Starting new "%s" container.' % name) display.info('Starting new "%s" container.' % name)
docker_pull(args, image) docker_pull(args, image)
support_container_id = docker_run(args, image, options, create_only=not start, cmd=cmd) support_container_id = docker_run(args, image, name, options, create_only=not start, cmd=cmd)
running = start running = start
existing = False existing = False

@ -287,13 +287,14 @@ def docker_cp_to(args: EnvironmentConfig, container_id: str, src: str, dst: str)
def docker_run( def docker_run(
args: EnvironmentConfig, args: EnvironmentConfig,
image: str, image: str,
name: str,
options: t.Optional[list[str]], options: t.Optional[list[str]],
cmd: t.Optional[list[str]] = None, cmd: t.Optional[list[str]] = None,
create_only: bool = False, create_only: bool = False,
) -> str: ) -> str:
"""Run a container using the given docker image.""" """Run a container using the given docker image."""
if not options: options = list(options or [])
options = [] options.extend(['--name', name])
if not cmd: if not cmd:
cmd = [] cmd = []
@ -322,6 +323,7 @@ def docker_run(
except SubprocessError as ex: except SubprocessError as ex:
display.error(ex.message) display.error(ex.message)
display.warning('Failed to run docker image "%s". Waiting a few seconds before trying again.' % image) display.warning('Failed to run docker image "%s". Waiting a few seconds before trying again.' % image)
docker_rm(args, name) # podman doesn't remove containers after create if run fails
time.sleep(3) time.sleep(3)
raise ApplicationError('Failed to run docker image "%s".' % image) raise ApplicationError('Failed to run docker image "%s".' % image)

Loading…
Cancel
Save