ci: improve docker-compose error handling

When docker_compose_up fails, the related containers may be partially
created or running. We must run docker_compose_down to ensure proper
cleansing.
This commit is contained in:
Gilles Filippini 2025-01-31 23:26:01 +01:00
parent 117b9d6824
commit 3aef5868fb

View file

@ -310,6 +310,9 @@ def get_nginx_conf_from_container(container: Container) -> bytes:
return conffile.read()
class DockerComposeException(Exception):
pass
def __prepare_and_execute_compose_cmd(compose_files: List[str], project_name: str, cmd: str):
"""
Prepare and execute the Docker Compose command with the provided compose files and project name.
@ -325,7 +328,7 @@ def __prepare_and_execute_compose_cmd(compose_files: List[str], project_name: st
try:
subprocess.check_output(shlex.split(compose_cmd.getvalue()), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
pytest.fail(f"Error while running '{compose_cmd.getvalue()}':\n{e.output}", pytrace=False)
raise DockerComposeException(f"Error while running '{compose_cmd.getvalue()}':\n{e.output}")
def docker_compose_up(compose_files: List[str], project_name: str):
@ -334,7 +337,14 @@ def docker_compose_up(compose_files: List[str], project_name: str):
"""
if compose_files is None or len(compose_files) == 0:
pytest.fail(f"No compose file passed to docker_compose_up", pytrace=False)
__prepare_and_execute_compose_cmd(compose_files, project_name, cmd="up --detach")
try:
__prepare_and_execute_compose_cmd(compose_files, project_name, cmd="up --detach")
except DockerComposeException as e:
try:
docker_compose_down(compose_files, project_name)
except:
pass
pytest.fail(str(e), pytrace=False)
def docker_compose_down(compose_files: List[str], project_name: str):