Skip to content

Commit 1f0ca14

Browse files
aloosleyAlex Loosleytillahoffmann
authored
Build option for DockerCompose (#168)
* build option for compose * if condition for adding build command only * test build causes --build arg to be added to docker-compose up call * satisfy flake8 * readability change * separate subprocess method, and fast (but weaker) test of compose with --build * hardcode name of method in object mock to satisfy review request * remove attribute test * Use more refactorable method name, remove now unused compose conext variable, make test py 3.6 compatible. * add back compose.build check Co-authored-by: Alex Loosley <[email protected]> Co-authored-by: Till Hoffmann <[email protected]>
1 parent 9100aaf commit 1f0ca14

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

testcontainers/compose.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ def __init__(
6060
filepath,
6161
compose_file_name="docker-compose.yml",
6262
pull=False,
63+
build=False,
6364
env_file=None):
6465
self.filepath = filepath
6566
self.compose_file_names = compose_file_name if isinstance(
6667
compose_file_name, (list, tuple)
6768
) else [compose_file_name]
6869
self.pull = pull
70+
self.build = build
6971
self.env_file = env_file
7072

7173
def __enter__(self):
@@ -86,14 +88,17 @@ def docker_compose_command(self):
8688
def start(self):
8789
if self.pull:
8890
pull_cmd = self.docker_compose_command() + ['pull']
89-
subprocess.call(pull_cmd, cwd=self.filepath)
91+
self._call_command(cmd=pull_cmd)
9092

9193
up_cmd = self.docker_compose_command() + ['up', '-d']
92-
subprocess.call(up_cmd, cwd=self.filepath)
94+
if self.build:
95+
up_cmd.append('--build')
96+
97+
self._call_command(cmd=up_cmd)
9398

9499
def stop(self):
95100
down_cmd = self.docker_compose_command() + ['down', '-v']
96-
subprocess.call(down_cmd, cwd=self.filepath)
101+
self._call_command(cmd=down_cmd)
97102

98103
def get_logs(self):
99104
logs_cmd = self.docker_compose_command() + ["logs"]
@@ -120,6 +125,11 @@ def _get_service_info(self, service, port):
120125
.format(port, service))
121126
return result
122127

128+
def _call_command(self, cmd, filepath=None):
129+
if filepath is None:
130+
filepath = self.filepath
131+
subprocess.call(cmd, cwd=filepath)
132+
123133
@wait_container_is_ready(requests.exceptions.ConnectionError)
124134
def wait_for(self, url):
125135
requests.get(url)

tests/test_docker_compose.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import patch
2+
13
import pytest
24
import subprocess
35

@@ -23,6 +25,18 @@ def test_can_pull_images_before_spawning_service_via_compose():
2325
assert port == "4444"
2426

2527

28+
def test_can_build_images_before_spawning_service_via_compose():
29+
with patch.object(DockerCompose, "_call_command") as call_mock:
30+
with DockerCompose("tests", build=True) as compose:
31+
...
32+
33+
assert compose.build
34+
docker_compose_cmd = call_mock.call_args_list[0][1]["cmd"]
35+
assert "docker-compose" in docker_compose_cmd
36+
assert "up" in docker_compose_cmd
37+
assert "--build" in docker_compose_cmd
38+
39+
2640
def test_can_throw_exception_if_no_port_exposed():
2741
with DockerCompose("tests") as compose:
2842
with pytest.raises(NoSuchPortExposed):

0 commit comments

Comments
 (0)