From 79f67ed56116be11b1c992fade04acf06d9208d1 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Fri, 9 Sep 2022 10:40:21 -0700 Subject: [PATCH] ansible-test - Fix container error handling. (#78739) --- changelogs/fragments/ansible-test-podman-create-retry.yml | 3 +++ test/lib/ansible_test/_internal/containers.py | 4 ++-- test/lib/ansible_test/_internal/docker_util.py | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/ansible-test-podman-create-retry.yml diff --git a/changelogs/fragments/ansible-test-podman-create-retry.yml b/changelogs/fragments/ansible-test-podman-create-retry.yml new file mode 100644 index 00000000000..7416e89f795 --- /dev/null +++ b/changelogs/fragments/ansible-test-podman-create-retry.yml @@ -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. diff --git a/test/lib/ansible_test/_internal/containers.py b/test/lib/ansible_test/_internal/containers.py index 70d653fabf7..5f727faacc1 100644 --- a/test/lib/ansible_test/_internal/containers.py +++ b/test/lib/ansible_test/_internal/containers.py @@ -139,7 +139,7 @@ def run_support_container( if current_container_id: publish_ports = False # publishing ports is pointless if already running in a docker container - options = (options or []) + ['--name', name] + options = (options or []) if start: options.append('-d') @@ -183,7 +183,7 @@ def run_support_container( else: display.info('Starting new "%s" container.' % name) 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 existing = False diff --git a/test/lib/ansible_test/_internal/docker_util.py b/test/lib/ansible_test/_internal/docker_util.py index b9ef4024e0b..47a3065cc20 100644 --- a/test/lib/ansible_test/_internal/docker_util.py +++ b/test/lib/ansible_test/_internal/docker_util.py @@ -287,13 +287,14 @@ def docker_cp_to(args: EnvironmentConfig, container_id: str, src: str, dst: str) def docker_run( args: EnvironmentConfig, image: str, + name: str, options: t.Optional[list[str]], cmd: t.Optional[list[str]] = None, create_only: bool = False, ) -> str: """Run a container using the given docker image.""" - if not options: - options = [] + options = list(options or []) + options.extend(['--name', name]) if not cmd: cmd = [] @@ -322,6 +323,7 @@ def docker_run( except SubprocessError as ex: display.error(ex.message) 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) raise ApplicationError('Failed to run docker image "%s".' % image)