From 9f6de5f764be47061cc71d29b57a305445ca554d Mon Sep 17 00:00:00 2001 From: Yatsishin Ilya <2159081+qoega@users.noreply.github.com> Date: Fri, 4 Jun 2021 18:00:59 +0300 Subject: [PATCH] opt-in for cleanup --- tests/integration/conftest.py | 26 +++++++++++++++++--------- tests/integration/runner | 14 +++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 6e0ad6fa874..f1e41bea4bc 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -9,16 +9,24 @@ def cleanup_environment(): _NetworkManager.clean_all_user_iptables_rules() try: result = subprocess.run(['docker', 'container', 'list', '-a']) - if result.returncode==0 and int(result.stdout) > 1: - logging.debug("Trying to kill unstopped containers...") - subprocess.run(['docker', 'kill', f'`docker container list -a`']) - subprocess.run(['docker', 'rm', f'`docker container list -a`']) - logging.debug("Unstopped containers killed") - r = subprocess.run(['docker-compose', 'ps', '--services', '--all']) - logging.debug(f"Docker ps before start:{r.stdout}") + if result.returncode != 0: + logging.error(f"docker ps returned error:{str(result.stderr)}") else: - logging.debug(f"No running containers") - except: + if int(result.stdout) > 1: + if env["PYTEST_CLEANUP_CONTAINERS"] != 1: + logging.warning(f"Docker containters({result.stdout}) are running before tests run. They can be left from previous pytest run and cause test failures.\n"\ + "You can set env PYTEST_CLEANUP_CONTAINERS=1 or use runner with --cleanup-containers argument to enable automatic containers cleanup.") + else: + logging.debug("Trying to kill unstopped containers...") + subprocess.run(['docker', 'kill', f'`docker container list -a`']) + subprocess.run(['docker', 'rm', f'`docker container list -a`']) + logging.debug("Unstopped containers killed") + r = subprocess.run(['docker-compose', 'ps', '--services', '--all']) + logging.debug(f"Docker ps before start:{r.stdout}") + else: + logging.debug(f"No running containers") + except Exception as e: + logging.error(f"cleanup_environment:{str(e)}") pass yield \ No newline at end of file diff --git a/tests/integration/runner b/tests/integration/runner index 3b64c30b2bc..160c4a23652 100755 --- a/tests/integration/runner +++ b/tests/integration/runner @@ -183,6 +183,13 @@ if __name__ == "__main__": dest="tmpfs", help="Use tmpfs for dockerd files") + parser.add_argument( + "--cleanup-containers", + action='store_true', + default=False, + dest="cleanup_containers", + help="Remove all running containers on test session start") + parser.add_argument( "--dockerd-volume-dir", action='store', @@ -241,6 +248,10 @@ if __name__ == "__main__": subprocess.check_call('docker volume create {name}_volume'.format(name=CONTAINER_NAME), shell=True) dockerd_internal_volume = "--volume={}_volume:/var/lib/docker".format(CONTAINER_NAME) + # If enabled we kill and remove containers before pytest session run. + env_cleanup = "" + if args.cleanup_containers: + env_cleanup = "-e PYTEST_CLEANUP_CONTAINERS=1" # enable tty mode & interactive for docker if we have real tty tty = "" if sys.stdout.isatty() and sys.stdin.isatty(): @@ -253,7 +264,7 @@ if __name__ == "__main__": --volume={base_cfg}:/clickhouse-config --volume={cases_dir}:/ClickHouse/tests/integration \ --volume={src_dir}/Server/grpc_protos:/ClickHouse/src/Server/grpc_protos \ {dockerd_internal_volume} -e DOCKER_CLIENT_TIMEOUT=300 -e COMPOSE_HTTP_TIMEOUT=600 \ - {env_tags} -e PYTEST_OPTS='{parallel} {opts} {tests_list}' {img} {command}".format( + {env_tags} {env_cleanup} -e PYTEST_OPTS='{parallel} {opts} {tests_list}' {img} {command}".format( net=net, tty=tty, bin=args.binary, @@ -263,6 +274,7 @@ if __name__ == "__main__": cases_dir=args.cases_dir, src_dir=args.src_dir, env_tags=env_tags, + env_cleanup=env_cleanup, parallel=parallel_args, opts=' '.join(args.pytest_args), tests_list=' '.join(args.tests_list),