Skip to content

Commit 5dad0e7

Browse files
committed
feat(compose): support for setting profiles
1 parent f1d8d35 commit 5dad0e7

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

core/testcontainers/compose/compose.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class DockerCompose:
171171
env_file: Optional[str] = None
172172
services: Optional[list[str]] = None
173173
docker_command_path: Optional[str] = None
174+
profiles: Optional[list[str]] = None
174175

175176
def __post_init__(self):
176177
if isinstance(self.compose_file_name, str):
@@ -225,6 +226,9 @@ def start(self) -> None:
225226
# we run in detached mode instead of blocking
226227
up_cmd.append("--detach")
227228

229+
if self.profiles:
230+
up_cmd.extend([item for profile in self.profiles for item in ["--profile", profile]])
231+
228232
if self.services:
229233
up_cmd.extend(self.services)
230234

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
runs-always: &simple-service
3+
image: alpine:latest
4+
init: true
5+
command:
6+
- sh
7+
- -c
8+
- 'while true; do sleep 0.1 ; date -Ins; done'
9+
runs-profile-a:
10+
<<: *simple-service
11+
profiles:
12+
- profile-a
13+
runs-profile-b:
14+
<<: *simple-service
15+
profiles:
16+
- profile-b

core/tests/test_compose.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,27 @@ def fetch(req: Union[Request, str]):
352352
if 200 < res.getcode() >= 400:
353353
raise Exception(f"HTTP Error: {res.getcode()} - {res.reason}: {body}")
354354
return res.getcode(), body
355+
356+
357+
@pytest.mark.parametrize(
358+
argnames=["profiles", "running", "not_running"],
359+
argvalues=[
360+
pytest.param(None, ["runs-always"], ["runs-profile-a", "runs-profile-b"], id="default"),
361+
pytest.param(
362+
["profile-a"], ["runs-always", "runs-profile-a"], ["runs-profile-b"], id="one-additional-profile-via-str"
363+
),
364+
pytest.param(
365+
["profile-a", "profile-b"],
366+
["runs-always", "runs-profile-a", "runs-profile-b"],
367+
[],
368+
id="all-profiles-explicitly",
369+
),
370+
],
371+
)
372+
def test_compose_profile_support(profiles: list[str] | None, running: list[str], not_running: list[str]):
373+
with DockerCompose(context=FIXTURES / "profile_support", profiles=profiles) as compose:
374+
for service in running:
375+
assert compose.get_container(service) is not None
376+
for service in not_running:
377+
with pytest.raises(ContainerIsNotRunning):
378+
compose.get_container(service)

0 commit comments

Comments
 (0)