From 4a6f3ed64af07269191094b9f4e13ce2dda3ddbd Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Tue, 28 Feb 2023 16:54:33 +0545 Subject: [PATCH] dvc.testing: fix lockfile in docker_services fixture (#9088) dvc.testing.docker_services: rollback to previous state where we were not tearing down the containers --- dvc/testing/fixtures.py | 25 +++++++++++++++++++------ pyproject.toml | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/dvc/testing/fixtures.py b/dvc/testing/fixtures.py index ad6589193..7f58ea598 100644 --- a/dvc/testing/fixtures.py +++ b/dvc/testing/fixtures.py @@ -219,7 +219,16 @@ def docker_compose_project_name(): @pytest.fixture(scope="session") -def docker_services(request, tmp_path_factory): +def docker_services( + tmp_path_factory, + docker_compose_command, + docker_compose_file, + docker_compose_project_name, + docker_setup, +): + from filelock import FileLock + from pytest_docker.plugin import DockerComposeExecutor, Services + if os.environ.get("CI") and os.name == "nt": pytest.skip("disabled for Windows on CI") @@ -236,11 +245,15 @@ def docker_services(request, tmp_path_factory): out = (err.output or b"").decode("utf-8") pytest.skip(f"docker-compose is not installed: {out}") - from filelock import FileLock + executor = DockerComposeExecutor( + docker_compose_command, docker_compose_file, docker_compose_project_name + ) # making sure we don't accidentally launch docker-compose in parallel, # as it might result in network conflicts. Inspired by: - # https://pytest-xdist.readthedocs.io/en/stable/how-to.html#making-session-scoped-fixtures-execute-only-once - root = tmp_path_factory.getbasetemp().parent - with FileLock(os.fspath(root / "docker-compose.lock")): - yield request.getfixturevalue("docker_services") + # https://github.com/pytest-dev/pytest-xdist#making-session-scoped-fixtures-execute-only-once + lockfile = tmp_path_factory.getbasetemp().parent / "docker-compose.lock" + with FileLock(os.fspath(lockfile)): + executor.execute(docker_setup) + # note: we are not tearing down the containers here + return Services(executor) diff --git a/pyproject.toml b/pyproject.toml index f9b380a86..aa514f780 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,7 @@ tests = [ "flaky", "pytest<8,>=7", "pytest-cov", - "pytest-docker", + "pytest-docker>=1,<2", "pytest-lazy-fixture", "pytest-mock", "pytest-test-utils",