Skip to content

Commit

Permalink
make docker_compose_cm a contextmanager rather than a fixture (#7905)
Browse files Browse the repository at this point in the history
Summary:
I would like to use this in a session-scoped fixture, but i can't, because its a module-scoped fixture. Instead, make the cm a contextmanager, and docker_compose() the fixture.

Test Plan: BK
  • Loading branch information
gibsondan committed May 16, 2022
1 parent 9e786a1 commit c71b117
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from .docker_compose import docker_compose, docker_compose_cm
from .docker_compose import (
default_docker_compose_yml,
docker_compose,
docker_compose_cm,
docker_compose_cm_fixture,
)
from .utils import retrying_requests, test_directory, test_id
55 changes: 34 additions & 21 deletions python_modules/dagster-test/dagster_test/fixtures/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,48 @@
from .utils import BUILDKITE


@pytest.fixture(scope="module")
def docker_compose_cm(test_directory):
@contextmanager
def docker_compose_cm(
docker_compose_yml,
network_name=None,
docker_context=None,
service=None,
):
if not network_name:
network_name = network_name_from_yml(docker_compose_yml)
try:
docker_compose_up(docker_compose_yml, docker_context, service)
if BUILDKITE:
# When running in a container on Buildkite, we need to first connect our container
# and our network and then yield a dict of container name to the container's
# hostname.
with buildkite_hostnames_cm(network_name) as hostnames:
yield hostnames
else:
# When running locally, we don't need to jump through any special networking hoops;
# just yield a dict of container name to "localhost".
yield dict((container, "localhost") for container in list_containers())
finally:
docker_compose_down(docker_compose_yml, docker_context, service)


@pytest.fixture(scope="module", name="docker_compose_cm")
def docker_compose_cm_fixture(test_directory):
@contextmanager
def docker_compose(
def _docker_compose(
docker_compose_yml=None,
network_name=None,
docker_context=None,
service=None,
):
if not docker_compose_yml:
docker_compose_yml = default_docker_compose_yml(test_directory)
if not network_name:
network_name = network_name_from_yml(docker_compose_yml)
try:
docker_compose_up(docker_compose_yml, docker_context, service)
if BUILDKITE:
# When running in a container on Buildkite, we need to first connect our container
# and our network and then yield a dict of container name to the container's
# hostname.
with buildkite_hostnames_cm(network_name) as hostnames:
yield hostnames
else:
# When running locally, we don't need to jump through any special networking hoops;
# just yield a dict of container name to "localhost".
yield dict((container, "localhost") for container in list_containers())
finally:
docker_compose_down(docker_compose_yml, docker_context, service)

return docker_compose
with docker_compose_cm(
docker_compose_yml, network_name, docker_context, service
) as hostnames:
yield hostnames

return _docker_compose


@pytest.fixture
Expand Down

0 comments on commit c71b117

Please sign in to comment.