Skip to content

Commit 3e00e71

Browse files
authored
feat(compose): support for setting profiles (#738)
# Change Adds `profiles` support for docker compose. # Context I've had to use a workaround in a company project for compose profiles leading to multiple compose profile files. This should help by supporting profiles in compose which is a very handy feature when your service has some variety of ways to run (contextual, environmental, etc) Without this, it's still possible to run profiles buy setting the `COMPOSE_PROFILES` env variable, but this is certainly cleaner and easier on test writing. # Docker Docs https://docs.docker.com/compose/how-tos/profiles/#assigning-profiles-to-services
1 parent 24e354f commit 3e00e71

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

core/testcontainers/compose/compose.py

+3
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):
@@ -198,6 +199,8 @@ def compose_command_property(self) -> list[str]:
198199
if self.compose_file_name:
199200
for file in self.compose_file_name:
200201
docker_compose_cmd += ["-f", file]
202+
if self.profiles:
203+
docker_compose_cmd += [item for profile in self.profiles for item in ["--profile", profile]]
201204
if self.env_file:
202205
docker_compose_cmd += ["--env-file", self.env_file]
203206
return docker_compose_cmd
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

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
from re import split
44
from time import sleep
5-
from typing import Union
5+
from typing import Union, Optional
66
from urllib.request import urlopen, Request
77

88
import pytest
@@ -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: Optional[list[str]], 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)