diff --git a/testcontainers/compose.py b/testcontainers/compose.py index 9e595a605..205144107 100644 --- a/testcontainers/compose.py +++ b/testcontainers/compose.py @@ -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 @@ -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() @@ -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) diff --git a/tests/.env.test b/tests/.env.test new file mode 100644 index 000000000..23e151152 --- /dev/null +++ b/tests/.env.test @@ -0,0 +1,2 @@ +TAG_MYSQL_ALLOW_EMPTY_PASSWORD="true" +TAG_TEST_ASSERT_KEY="test_is_passed" \ No newline at end of file diff --git a/tests/docker-compose-3.yml b/tests/docker-compose-3.yml new file mode 100644 index 000000000..35448cb7e --- /dev/null +++ b/tests/docker-compose-3.yml @@ -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} \ No newline at end of file diff --git a/tests/test_docker_compose.py b/tests/test_docker_compose.py index d13cbc329..747be5e16 100644 --- a/tests/test_docker_compose.py +++ b/tests/test_docker_compose.py @@ -1,4 +1,5 @@ import pytest +import subprocess from testcontainers.compose import DockerCompose from testcontainers.core.docker_client import DockerClient @@ -6,7 +7,7 @@ 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" @@ -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'