Skip to content

Commit f61dcda

Browse files
committedApr 19, 2023
feat(compose): allow running specific services in compose
1 parent 35fc247 commit f61dcda

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed
 

‎compose/testcontainers/compose/__init__.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import requests
21
import subprocess
32
from typing import Iterable, List, Optional, Tuple, Union
43

5-
from testcontainers.core.waiting_utils import wait_container_is_ready
4+
import requests
5+
66
from testcontainers.core.exceptions import NoSuchPortExposed
7+
from testcontainers.core.waiting_utils import wait_container_is_ready
78

89

910
class DockerCompose:
@@ -38,19 +39,23 @@ class DockerCompose:
3839
hello-world:
3940
image: "hello-world"
4041
"""
42+
4143
def __init__(
4244
self,
4345
filepath: str,
4446
compose_file_name: Union[str, Iterable] = "docker-compose.yml",
4547
pull: bool = False,
4648
build: bool = False,
47-
env_file: Optional[str] = None) -> None:
49+
env_file: Optional[str] = None,
50+
services: Optional[List[str]] = None
51+
) -> None:
4852
self.filepath = filepath
4953
self.compose_file_names = [compose_file_name] if isinstance(compose_file_name, str) else \
5054
list(compose_file_name)
5155
self.pull = pull
5256
self.build = build
5357
self.env_file = env_file
58+
self.services = services
5459

5560
def __enter__(self) -> "DockerCompose":
5661
self.start()
@@ -84,6 +89,8 @@ def start(self) -> None:
8489
up_cmd = self.docker_compose_command() + ['up', '-d']
8590
if self.build:
8691
up_cmd.append('--build')
92+
if self.services:
93+
up_cmd.extend(self.services)
8794

8895
self._call_command(cmd=up_cmd)
8996

‎compose/tests/test_docker_compose.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from testcontainers.core.exceptions import NoSuchPortExposed
99
from testcontainers.core.waiting_utils import wait_for_logs
1010

11-
1211
ROOT = os.path.dirname(__file__)
1312

1413

@@ -40,6 +39,19 @@ def test_can_build_images_before_spawning_service_via_compose():
4039
assert "--build" in docker_compose_cmd
4140

4241

42+
def test_can_run_specific_services():
43+
with patch.object(DockerCompose, "_call_command") as call_mock:
44+
with DockerCompose(ROOT, services=["hub", "firefox"]) as compose:
45+
...
46+
47+
assert compose.services
48+
docker_compose_cmd = call_mock.call_args_list[0][1]["cmd"]
49+
services_at_the_end = docker_compose_cmd[-2:]
50+
assert "firefox" in services_at_the_end
51+
assert "hub" in services_at_the_end
52+
assert "chrome" not in docker_compose_cmd
53+
54+
4355
def test_can_throw_exception_if_no_port_exposed():
4456
with DockerCompose(ROOT) as compose:
4557
with pytest.raises(NoSuchPortExposed):

0 commit comments

Comments
 (0)
Please sign in to comment.