Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compose): support for setting profiles #738

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class DockerCompose:
env_file: Optional[str] = None
services: Optional[list[str]] = None
docker_command_path: Optional[str] = None
profiles: Optional[list[str]] = None

def __post_init__(self):
if isinstance(self.compose_file_name, str):
Expand Down Expand Up @@ -198,6 +199,8 @@ def compose_command_property(self) -> list[str]:
if self.compose_file_name:
for file in self.compose_file_name:
docker_compose_cmd += ["-f", file]
if self.profiles:
docker_compose_cmd += [item for profile in self.profiles for item in ["--profile", profile]]
if self.env_file:
docker_compose_cmd += ["--env-file", self.env_file]
return docker_compose_cmd
Expand Down
16 changes: 16 additions & 0 deletions core/tests/compose_fixtures/profile_support/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
runs-always: &simple-service
image: alpine:latest
init: true
command:
- sh
- -c
- 'while true; do sleep 0.1 ; date -Ins; done'
runs-profile-a:
<<: *simple-service
profiles:
- profile-a
runs-profile-b:
<<: *simple-service
profiles:
- profile-b
26 changes: 25 additions & 1 deletion core/tests/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from re import split
from time import sleep
from typing import Union
from typing import Union, Optional
from urllib.request import urlopen, Request

import pytest
Expand Down Expand Up @@ -352,3 +352,27 @@ def fetch(req: Union[Request, str]):
if 200 < res.getcode() >= 400:
raise Exception(f"HTTP Error: {res.getcode()} - {res.reason}: {body}")
return res.getcode(), body


@pytest.mark.parametrize(
argnames=["profiles", "running", "not_running"],
argvalues=[
pytest.param(None, ["runs-always"], ["runs-profile-a", "runs-profile-b"], id="default"),
pytest.param(
["profile-a"], ["runs-always", "runs-profile-a"], ["runs-profile-b"], id="one-additional-profile-via-str"
),
pytest.param(
["profile-a", "profile-b"],
["runs-always", "runs-profile-a", "runs-profile-b"],
[],
id="all-profiles-explicitly",
),
],
)
def test_compose_profile_support(profiles: Optional[list[str]], running: list[str], not_running: list[str]):
with DockerCompose(context=FIXTURES / "profile_support", profiles=profiles) as compose:
for service in running:
assert compose.get_container(service) is not None
for service in not_running:
with pytest.raises(ContainerIsNotRunning):
compose.get_container(service)