Skip to content

Add docker_compose_subprocess_kwargs fixture support #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/pytest_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import timeit


def execute(command, success_codes=(0,)):
def execute(command, success_codes=(0,), **kwargs):
"""Run a shell command."""
try:
output = subprocess.check_output(
command, stderr=subprocess.STDOUT, shell=True,
command, **kwargs,
)
status = 0
except subprocess.CalledProcessError as error:
Expand All @@ -29,6 +29,16 @@ def execute(command, success_codes=(0,)):
return output


@pytest.fixture(scope='session')
def docker_compose_subprocess_default_kwargs():
return {'stderr': subprocess.STDOUT, 'shell': True}


@pytest.fixture(scope='session')
def docker_compose_subprocess_kwargs():
return {}


@pytest.fixture(scope='session')
def docker_ip():
"""Determine IP address for TCP connections to Docker containers."""
Expand Down Expand Up @@ -106,13 +116,14 @@ def str_to_list(arg):
class DockerComposeExecutor(object):
_compose_files = attr.ib(convert=str_to_list)
_compose_project_name = attr.ib()
_compose_subprocess_kwargs = attr.ib()

def execute(self, subcommand):
command = "docker-compose"
for compose_file in self._compose_files:
command += ' -f "{}"'.format(compose_file)
command += ' -p "{}" {}'.format(self._compose_project_name, subcommand)
return execute(command)
return execute(command, **self._compose_subprocess_kwargs)


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -140,12 +151,16 @@ def docker_compose_project_name():

@pytest.fixture(scope='session')
def docker_services(
docker_compose_file, docker_compose_project_name
docker_compose_file, docker_compose_project_name, docker_compose_subprocess_default_kwargs,
docker_compose_subprocess_kwargs
):
"""Ensure all Docker-based services are up and running."""

subprocess_kwargs = {**docker_compose_subprocess_default_kwargs}
subprocess_kwargs.update(docker_compose_subprocess_kwargs)

docker_compose = DockerComposeExecutor(
docker_compose_file, docker_compose_project_name
docker_compose_file, docker_compose_project_name, subprocess_kwargs
)

# Spawn containers.
Expand Down