diff --git a/changelogs/fragments/ansible-test-docker-terminate.yml b/changelogs/fragments/ansible-test-docker-terminate.yml new file mode 100644 index 00000000000..165a02932b4 --- /dev/null +++ b/changelogs/fragments/ansible-test-docker-terminate.yml @@ -0,0 +1,2 @@ +minor_changes: + - ansible-test - --docker flag now has an associated --docker-terminate flag which controls if and when the docker container is removed following tests diff --git a/test/lib/ansible_test/_internal/cli.py b/test/lib/ansible_test/_internal/cli.py index c0aa37e5a81..942ef905cc9 100644 --- a/test/lib/ansible_test/_internal/cli.py +++ b/test/lib/ansible_test/_internal/cli.py @@ -1031,6 +1031,12 @@ def add_extra_docker_options(parser, integration=True): default=None, help='set seccomp confinement for the test container: %(choices)s') + docker.add_argument('--docker-terminate', + metavar='WHEN', + help='terminate docker container: %(choices)s (default: %(default)s)', + choices=['never', 'always', 'success'], + default='always') + if not integration: return diff --git a/test/lib/ansible_test/_internal/config.py b/test/lib/ansible_test/_internal/config.py index c73e4233a69..4e3eb7be874 100644 --- a/test/lib/ansible_test/_internal/config.py +++ b/test/lib/ansible_test/_internal/config.py @@ -59,6 +59,7 @@ class EnvironmentConfig(CommonConfig): self.docker_keep_git = args.docker_keep_git if 'docker_keep_git' in args else False # type: bool self.docker_seccomp = args.docker_seccomp if 'docker_seccomp' in args else None # type: str self.docker_memory = args.docker_memory if 'docker_memory' in args else None + self.docker_terminate = args.docker_terminate if 'docker_terminate' in args else None # type: str if self.docker_seccomp is None: self.docker_seccomp = get_docker_completion().get(self.docker_raw, {}).get('seccomp', 'default') diff --git a/test/lib/ansible_test/_internal/delegation.py b/test/lib/ansible_test/_internal/delegation.py index a139efd66d9..d66b0d10b96 100644 --- a/test/lib/ansible_test/_internal/delegation.py +++ b/test/lib/ansible_test/_internal/delegation.py @@ -230,6 +230,7 @@ def delegate_docker(args, exclude, require, integration_targets): httptester_id = None test_id = None + success = False options = { '--docker': 1, @@ -352,6 +353,9 @@ def delegate_docker(args, exclude, require, integration_targets): try: docker_exec(args, test_id, cmd, options=cmd_options) + # docker_exec will throw SubprocessError if not successful + # If we make it here, all the prep work earlier and the docker_exec line above were all successful. + success = True finally: local_test_root = os.path.dirname(os.path.join(data_context().content.root, data_context().content.results_path)) @@ -368,7 +372,8 @@ def delegate_docker(args, exclude, require, integration_targets): docker_rm(args, httptester_id) if test_id: - docker_rm(args, test_id) + if args.docker_terminate == 'always' or (args.docker_terminate == 'success' and success): + docker_rm(args, test_id) def delegate_remote(args, exclude, require, integration_targets):