Skip to content

Commit 3b1b58b

Browse files
Support specifying multiple docker-compose.yml files (#98)
* Support specifying multiple docker-compose.yml files * Add test and example in the doc * Add missing `-d` in the `up` command * Fix code style * Fix typo * Fix typo * Update testcontainers/compose.py Co-authored-by: Till Hoffmann <[email protected]> * Update compose.py * Fix `get_logs` with multiple compose files * Remove blank line at end of file Co-authored-by: Till Hoffmann <[email protected]> Co-authored-by: Till Hoffmann <[email protected]>
1 parent 6342448 commit 3b1b58b

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

testcontainers/compose.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class DockerCompose(object):
2222
-------
2323
::
2424
25-
with DockerCompose("/home/project", pull=True) as compose:
25+
with DockerCompose("/home/project",
26+
compose_file_name=["docker-compose-1.yml", "docker-compose-2.yml"],
27+
pull=True) as compose:
2628
host = compose.get_service_host("hub", 4444)
2729
port = compose.get_service_port("hub", 4444)
2830
driver = webdriver.Remote(
@@ -60,7 +62,9 @@ def __init__(
6062
compose_file_name="docker-compose.yml",
6163
pull=False):
6264
self.filepath = filepath
63-
self.compose_file_name = compose_file_name
65+
self.compose_file_names = compose_file_name if isinstance(
66+
compose_file_name, (list, tuple)
67+
) else [compose_file_name]
6468
self.pull = pull
6569

6670
def __enter__(self):
@@ -70,23 +74,30 @@ def __enter__(self):
7074
def __exit__(self, exc_type, exc_val, exc_tb):
7175
self.stop()
7276

77+
def docker_compose_command(self):
78+
docker_compose_cmd = ['docker-compose']
79+
for file in self.compose_file_names:
80+
docker_compose_cmd += ['-f', file]
81+
return docker_compose_cmd
82+
7383
def start(self):
7484
with blindspin.spinner():
7585
if self.pull:
76-
subprocess.call(["docker-compose", "-f", self.compose_file_name, "pull"],
77-
cwd=self.filepath)
78-
subprocess.call(["docker-compose", "-f", self.compose_file_name, "up", "-d"],
79-
cwd=self.filepath)
86+
pull_cmd = self.docker_compose_command() + ['pull']
87+
subprocess.call(pull_cmd, cwd=self.filepath)
88+
up_cmd = self.docker_compose_command() + ['up', '-d']
89+
subprocess.call(up_cmd, cwd=self.filepath)
8090

8191
def stop(self):
8292
with blindspin.spinner():
83-
subprocess.call(["docker-compose", "-f", self.compose_file_name, "down", "-v"],
84-
cwd=self.filepath)
93+
down_cmd = self.docker_compose_command() + ['down', '-v']
94+
subprocess.call(down_cmd, cwd=self.filepath)
8595

8696
def get_logs(self):
97+
logs_cmd = self.docker_compose_command() + ["logs"]
8798
with blindspin.spinner():
8899
result = subprocess.run(
89-
["docker-compose", "-f", self.compose_file_name, "logs"],
100+
logs_cmd,
90101
cwd=self.filepath,
91102
stdout=subprocess.PIPE,
92103
stderr=subprocess.PIPE,
@@ -100,9 +111,8 @@ def get_service_host(self, service_name, port):
100111
return self._get_service_info(service_name, port)[0]
101112

102113
def _get_service_info(self, service, port):
103-
cmd_as_list = ["docker-compose", "-f", self.compose_file_name, "port", service, str(port)]
104-
output = subprocess.check_output(cmd_as_list,
105-
cwd=self.filepath).decode("utf-8")
114+
port_cmd = self.docker_compose_command() + ["port", service, str(port)]
115+
output = subprocess.check_output(port_cmd, cwd=self.filepath).decode("utf-8")
106116
result = str(output).rstrip().split(":")
107117
if len(result) == 1:
108118
raise NoSuchPortExposed("Port {} was not exposed for service {}"

tests/docker-compose-2.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mysql:
2+
image: mysql
3+
ports:
4+
- "3306:3306"
5+
environment:
6+
MYSQL_ALLOW_EMPTY_PASSWORD: "true"

tests/test_docker_compose.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ def test_compose_wait_for_container_ready():
3333
compose.wait_for("http://%s:4444/wd/hub" % docker.host())
3434

3535

36+
def test_can_parse_multiple_compose_files():
37+
with DockerCompose(filepath="tests",
38+
compose_file_name=["docker-compose.yml", "docker-compose-2.yml"]) as compose:
39+
host = compose.get_service_host("mysql", 3306)
40+
port = compose.get_service_port("mysql", 3306)
41+
assert host == "0.0.0.0"
42+
assert port == "3306"
43+
44+
host = compose.get_service_host("hub", 4444)
45+
port = compose.get_service_port("hub", 4444)
46+
assert host == "0.0.0.0"
47+
assert port == "4444"
48+
49+
3650
def test_can_get_logs():
3751
with DockerCompose("tests") as compose:
3852
docker = DockerClient()

0 commit comments

Comments
 (0)