Skip to content

Commit a9601b7

Browse files
committed
Add conditional statement for docker start/stop
1 parent 91434f4 commit a9601b7

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

buildrunner/docker/runner.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import six
2828
import timeout_decorator
2929

30+
from buildrunner.config import BuildRunnerConfig
3031
from buildrunner.docker import (
3132
new_client,
3233
force_remove_container,
@@ -132,6 +133,10 @@ def __init__(self, image_config, dockerd_url=None, log=None):
132133
if log:
133134
log.write("\nImage pulled successfully\n")
134135

136+
self.use_docker_py = (
137+
BuildRunnerConfig.get_instance().run_config.use_legacy_builder
138+
)
139+
135140
def start(
136141
self,
137142
shell="/bin/sh",
@@ -237,7 +242,10 @@ def start(
237242

238243
# start the container
239244
self.container = self.docker_client.create_container(self.image_name, **kwargs)
240-
python_on_whales.docker.start(self.container["Id"])
245+
if self.use_docker_py:
246+
self.docker_client.start(self.container["Id"])
247+
else:
248+
python_on_whales.docker.start(self.container["Id"])
241249

242250
# run any supplied provisioners
243251
if provisioners:
@@ -255,7 +263,13 @@ def stop(self):
255263
Stop the backing Docker container.
256264
"""
257265
if self.container:
258-
python_on_whales.docker.stop(self.container["Id"], time=0)
266+
if self.use_docker_py:
267+
self.docker_client.stop(
268+
self.container["Id"],
269+
timeout=0,
270+
)
271+
else:
272+
python_on_whales.docker.stop(self.container["Id"], time=0)
259273

260274
def cleanup(self):
261275
"""

tests/test_caching.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ def fixture_setup_runner():
3434
"docker.io/ubuntu:19.04",
3535
pull_image=False,
3636
)
37-
runner = DockerRunner(
38-
image_config=image_config,
39-
)
40-
runner.start(working_dir="/root")
37+
with mock.patch(
38+
"buildrunner.docker.runner.BuildRunnerConfig.get_instance"
39+
) as mock_build_runner_config:
40+
# Set to use docker-py to be used over python on whales
41+
mock_build_runner_config.run_config.use_legacy_builder.return_value = True
42+
runner = DockerRunner(
43+
image_config=image_config,
44+
)
45+
runner.start(working_dir="/root")
4146

42-
yield runner
47+
yield runner
4348

44-
runner.cleanup()
49+
runner.cleanup()
4550

4651

4752
@pytest.fixture(name="log_output")

0 commit comments

Comments
 (0)