Skip to content

Commit

Permalink
feat(compose): allow running specific services in compose
Browse files Browse the repository at this point in the history
  • Loading branch information
balint-backmaker committed Apr 19, 2023
1 parent 35fc247 commit f61dcda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 10 additions & 3 deletions compose/testcontainers/compose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import requests
import subprocess
from typing import Iterable, List, Optional, Tuple, Union

from testcontainers.core.waiting_utils import wait_container_is_ready
import requests

from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.waiting_utils import wait_container_is_ready


class DockerCompose:
Expand Down Expand Up @@ -38,19 +39,23 @@ class DockerCompose:
hello-world:
image: "hello-world"
"""

def __init__(
self,
filepath: str,
compose_file_name: Union[str, Iterable] = "docker-compose.yml",
pull: bool = False,
build: bool = False,
env_file: Optional[str] = None) -> None:
env_file: Optional[str] = None,
services: Optional[List[str]] = None
) -> None:
self.filepath = filepath
self.compose_file_names = [compose_file_name] if isinstance(compose_file_name, str) else \
list(compose_file_name)
self.pull = pull
self.build = build
self.env_file = env_file
self.services = services

def __enter__(self) -> "DockerCompose":
self.start()
Expand Down Expand Up @@ -84,6 +89,8 @@ def start(self) -> None:
up_cmd = self.docker_compose_command() + ['up', '-d']
if self.build:
up_cmd.append('--build')
if self.services:
up_cmd.extend(self.services)

self._call_command(cmd=up_cmd)

Expand Down
14 changes: 13 additions & 1 deletion compose/tests/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.waiting_utils import wait_for_logs


ROOT = os.path.dirname(__file__)


Expand Down Expand Up @@ -40,6 +39,19 @@ def test_can_build_images_before_spawning_service_via_compose():
assert "--build" in docker_compose_cmd


def test_can_run_specific_services():
with patch.object(DockerCompose, "_call_command") as call_mock:
with DockerCompose(ROOT, services=["hub", "firefox"]) as compose:
...

assert compose.services
docker_compose_cmd = call_mock.call_args_list[0][1]["cmd"]
services_at_the_end = docker_compose_cmd[-2:]
assert "firefox" in services_at_the_end
assert "hub" in services_at_the_end
assert "chrome" not in docker_compose_cmd


def test_can_throw_exception_if_no_port_exposed():
with DockerCompose(ROOT) as compose:
with pytest.raises(NoSuchPortExposed):
Expand Down

0 comments on commit f61dcda

Please sign in to comment.