Skip to content

Commit ebaf9a6

Browse files
Anton PetrosyukAnton Petrosyuk
Anton Petrosyuk
authored and
Anton Petrosyuk
committed
adding support for passing env-file to DockerCompose (testcontainers#134)
1 parent b77372d commit ebaf9a6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

testcontainers/compose.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
Allows to spin up services configured via :code:`docker-compose.yml`.
66
"""
77

8-
import subprocess
9-
108
import requests
9+
import subprocess
1110

1211
from testcontainers.core.waiting_utils import wait_container_is_ready
1312
from testcontainers.core.exceptions import NoSuchPortExposed
@@ -55,16 +54,19 @@ class DockerCompose(object):
5554
expose:
5655
- "5555"
5756
"""
57+
5858
def __init__(
5959
self,
6060
filepath,
6161
compose_file_name="docker-compose.yml",
62-
pull=False):
62+
pull=False,
63+
env_file=None):
6364
self.filepath = filepath
6465
self.compose_file_names = compose_file_name if isinstance(
6566
compose_file_name, (list, tuple)
6667
) else [compose_file_name]
6768
self.pull = pull
69+
self.env_file = env_file
6870

6971
def __enter__(self):
7072
self.start()
@@ -77,12 +79,15 @@ def docker_compose_command(self):
7779
docker_compose_cmd = ['docker-compose']
7880
for file in self.compose_file_names:
7981
docker_compose_cmd += ['-f', file]
82+
if self.env_file:
83+
docker_compose_cmd += ['--env-file', self.env_file]
8084
return docker_compose_cmd
8185

8286
def start(self):
8387
if self.pull:
8488
pull_cmd = self.docker_compose_command() + ['pull']
8589
subprocess.call(pull_cmd, cwd=self.filepath)
90+
8691
up_cmd = self.docker_compose_command() + ['up', '-d']
8792
subprocess.call(up_cmd, cwd=self.filepath)
8893

tests/test_docker_compose.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import pytest
2+
import subprocess
23

34
from testcontainers.compose import DockerCompose
45
from testcontainers.core.docker_client import DockerClient
56
from testcontainers.core.exceptions import NoSuchPortExposed
67

78

89
def test_can_spawn_service_via_compose():
9-
with DockerCompose("tests") as compose:
10+
with DockerCompose('tests') as compose:
1011
host = compose.get_service_host("hub", 4444)
1112
port = compose.get_service_port("hub", 4444)
1213
assert host == "0.0.0.0"
@@ -53,3 +54,11 @@ def test_can_get_logs():
5354
compose.wait_for("http://%s:4444/wd/hub" % docker.host())
5455
stdout, stderr = compose.get_logs()
5556
assert stdout, 'There should be something on stdout'
57+
58+
59+
def test_can_pass_env_params_by_env_file():
60+
with DockerCompose('tests', compose_file_name='docker-compose-3.yml', env_file='.env.test') as compose:
61+
check_env_is_set_cmd = 'docker exec tests_mysql_1 printenv | grep TEST_ASSERT_KEY'.split()
62+
out = subprocess.run(check_env_is_set_cmd, stdout=subprocess.PIPE)
63+
assert out.stdout.decode('utf-8').splitlines()[0], 'test_is_passed'
64+

0 commit comments

Comments
 (0)