File tree 2 files changed +18
-4
lines changed
2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change 5
5
Allows to spin up services configured via :code:`docker-compose.yml`.
6
6
"""
7
7
8
- import subprocess
9
-
10
8
import requests
9
+ import subprocess
11
10
12
11
from testcontainers .core .waiting_utils import wait_container_is_ready
13
12
from testcontainers .core .exceptions import NoSuchPortExposed
@@ -55,16 +54,19 @@ class DockerCompose(object):
55
54
expose:
56
55
- "5555"
57
56
"""
57
+
58
58
def __init__ (
59
59
self ,
60
60
filepath ,
61
61
compose_file_name = "docker-compose.yml" ,
62
- pull = False ):
62
+ pull = False ,
63
+ env_file = None ):
63
64
self .filepath = filepath
64
65
self .compose_file_names = compose_file_name if isinstance (
65
66
compose_file_name , (list , tuple )
66
67
) else [compose_file_name ]
67
68
self .pull = pull
69
+ self .env_file = env_file
68
70
69
71
def __enter__ (self ):
70
72
self .start ()
@@ -77,12 +79,15 @@ def docker_compose_command(self):
77
79
docker_compose_cmd = ['docker-compose' ]
78
80
for file in self .compose_file_names :
79
81
docker_compose_cmd += ['-f' , file ]
82
+ if self .env_file :
83
+ docker_compose_cmd += ['--env-file' , self .env_file ]
80
84
return docker_compose_cmd
81
85
82
86
def start (self ):
83
87
if self .pull :
84
88
pull_cmd = self .docker_compose_command () + ['pull' ]
85
89
subprocess .call (pull_cmd , cwd = self .filepath )
90
+
86
91
up_cmd = self .docker_compose_command () + ['up' , '-d' ]
87
92
subprocess .call (up_cmd , cwd = self .filepath )
88
93
Original file line number Diff line number Diff line change 1
1
import pytest
2
+ import subprocess
2
3
3
4
from testcontainers .compose import DockerCompose
4
5
from testcontainers .core .docker_client import DockerClient
5
6
from testcontainers .core .exceptions import NoSuchPortExposed
6
7
7
8
8
9
def test_can_spawn_service_via_compose ():
9
- with DockerCompose (" tests" ) as compose :
10
+ with DockerCompose (' tests' ) as compose :
10
11
host = compose .get_service_host ("hub" , 4444 )
11
12
port = compose .get_service_port ("hub" , 4444 )
12
13
assert host == "0.0.0.0"
@@ -53,3 +54,11 @@ def test_can_get_logs():
53
54
compose .wait_for ("http://%s:4444/wd/hub" % docker .host ())
54
55
stdout , stderr = compose .get_logs ()
55
56
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
+
You can’t perform that action at this time.
0 commit comments