Skip to content

Commit

Permalink
Merge pull request #11 from gdetrez/multiple-compose-files
Browse files Browse the repository at this point in the history
Allow fixture to return multiple compose files
  • Loading branch information
AndreLouisCaron committed Aug 13, 2017
2 parents 643211e + e898132 commit 958f037
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/pytest_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,23 @@ def wait_until_responsive(self, check, timeout, pause,
)


def str_to_list(arg):
if isinstance(arg, (list, tuple)):
return arg
return [arg]


@attr.s(frozen=True)
class DockerComposeExecutor(object):
_compose_file = attr.ib()
_compose_files = attr.ib(convert=str_to_list)
_compose_project_name = attr.ib()

def execute(self, command):
return execute(
'docker-compose -f "%s" -p "%s" %s' % (
self._compose_file, self._compose_project_name, command)
)
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)


@pytest.fixture(scope='session')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_docker_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_wait_until_responsive_timeout():

with mock.patch('time.sleep') as sleep:
docker_compose = DockerComposeExecutor(
compose_file='docker-compose.yml',
compose_files='docker-compose.yml',
compose_project_name="pytest123")
services = Services(docker_compose)
with pytest.raises(Exception) as exc:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_dockercomposeexecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import mock
import subprocess

import py

from pytest_docker import DockerComposeExecutor


Expand All @@ -15,3 +17,31 @@ def test_execute():
shell=True, stderr=subprocess.STDOUT,
),
]


def test_pypath_compose_files():
compose_file = py.path.local('/tmp/docker-compose.yml')
docker_compose = DockerComposeExecutor(compose_file, "pytest123")
with mock.patch('subprocess.check_output') as check_output:
docker_compose.execute("up")
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "/tmp/docker-compose.yml"'
' -p "pytest123" up',
shell=True, stderr=subprocess.STDOUT,
),
]


def test_multiple_compose_files():
docker_compose = DockerComposeExecutor(
["docker-compose.yml", "other-compose.yml"], "pytest123")
with mock.patch('subprocess.check_output') as check_output:
docker_compose.execute("up")
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -f "other-compose.yml"'
' -p "pytest123" up',
shell=True, stderr=subprocess.STDOUT,
),
]

0 comments on commit 958f037

Please sign in to comment.