Skip to content

Support passing custom env-file to DockerCompose #135

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

Merged
merged 4 commits into from
Apr 15, 2021
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
11 changes: 8 additions & 3 deletions testcontainers/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
Allows to spin up services configured via :code:`docker-compose.yml`.
"""

import subprocess

import requests
import subprocess

from testcontainers.core.waiting_utils import wait_container_is_ready
from testcontainers.core.exceptions import NoSuchPortExposed
Expand Down Expand Up @@ -55,16 +54,19 @@ class DockerCompose(object):
expose:
- "5555"
"""

def __init__(
self,
filepath,
compose_file_name="docker-compose.yml",
pull=False):
pull=False,
env_file=None):
self.filepath = filepath
self.compose_file_names = compose_file_name if isinstance(
compose_file_name, (list, tuple)
) else [compose_file_name]
self.pull = pull
self.env_file = env_file

def __enter__(self):
self.start()
Expand All @@ -77,12 +79,15 @@ def docker_compose_command(self):
docker_compose_cmd = ['docker-compose']
for file in self.compose_file_names:
docker_compose_cmd += ['-f', file]
if self.env_file:
docker_compose_cmd += ['--env-file', self.env_file]
return docker_compose_cmd

def start(self):
if self.pull:
pull_cmd = self.docker_compose_command() + ['pull']
subprocess.call(pull_cmd, cwd=self.filepath)

up_cmd = self.docker_compose_command() + ['up', '-d']
subprocess.call(up_cmd, cwd=self.filepath)

Expand Down
2 changes: 2 additions & 0 deletions tests/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TAG_MYSQL_ALLOW_EMPTY_PASSWORD="true"
TAG_TEST_ASSERT_KEY="test_is_passed"
7 changes: 7 additions & 0 deletions tests/docker-compose-3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mysql:
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: ${TAG_MYSQL_ALLOW_EMPTY_PASSWORD}
TEST_ASSERT_KEY: ${TAG_TEST_ASSERT_KEY}
11 changes: 10 additions & 1 deletion tests/test_docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytest
import subprocess

from testcontainers.compose import DockerCompose
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import NoSuchPortExposed


def test_can_spawn_service_via_compose():
with DockerCompose("tests") as compose:
with DockerCompose('tests') as compose:
host = compose.get_service_host("hub", 4444)
port = compose.get_service_port("hub", 4444)
assert host == "0.0.0.0"
Expand Down Expand Up @@ -53,3 +54,11 @@ def test_can_get_logs():
compose.wait_for("http://%s:4444/wd/hub" % docker.host())
stdout, stderr = compose.get_logs()
assert stdout, 'There should be something on stdout'


def test_can_pass_env_params_by_env_file():
with DockerCompose('tests', compose_file_name='docker-compose-3.yml',
env_file='.env.test') as _:
check_env_is_set_cmd = 'docker exec tests_mysql_1 printenv | grep TEST_ASSERT_KEY'.split()
out = subprocess.run(check_env_is_set_cmd, stdout=subprocess.PIPE)
assert out.stdout.decode('utf-8').splitlines()[0], 'test_is_passed'