Skip to content

Commit 8ef5faa

Browse files
authored
Merge pull request #779 from nuest/docker_daemon_logmessage
add explicit log message on failing Docker connection
2 parents 484dbef + b3f82db commit 8ef5faa

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

repo2docker/app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,12 @@ def build(self):
626626
try:
627627
docker_client = docker.APIClient(version="auto", **kwargs_from_env())
628628
except DockerException as e:
629-
self.log.exception(e)
630-
raise
629+
self.log.error(
630+
"\nDocker client initialization error: %s.\nCheck if docker is running on the host.\n",
631+
e,
632+
)
633+
self.exit(1)
634+
631635
# If the source to be executed is a directory, continue using the
632636
# directory. In the case of a local directory, it is used as both the
633637
# source and target. Reusing a local directory seems better than

tests/unit/test_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from repo2docker.app import Repo2Docker
1010
from repo2docker.__main__ import make_r2d
11+
from repo2docker.utils import chdir
1112

1213

1314
def test_find_image():
@@ -124,3 +125,25 @@ def test_root_not_allowed():
124125
builds.return_value = []
125126
app.build()
126127
builds.assert_called_once()
128+
129+
130+
def test_dryrun_works_without_docker(tmpdir, capsys):
131+
with chdir(tmpdir):
132+
with patch.object(docker, "APIClient") as client:
133+
client.side_effect = docker.errors.DockerException("Error: no Docker")
134+
app = Repo2Docker(dry_run=True)
135+
app.build()
136+
captured = capsys.readouterr()
137+
assert "Error: no Docker" not in captured.err
138+
139+
140+
def test_error_log_without_docker(tmpdir, capsys):
141+
with chdir(tmpdir):
142+
with patch.object(docker, "APIClient") as client:
143+
client.side_effect = docker.errors.DockerException("Error: no Docker")
144+
app = Repo2Docker()
145+
146+
with pytest.raises(SystemExit):
147+
app.build()
148+
captured = capsys.readouterr()
149+
assert "Error: no Docker" in captured.err

tests/unit/test_argumentvalidation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ def test_invalid_container_port_protocol_mapping_fail(temp_cwd):
223223
assert not validate_arguments(builddir, args_list, "Port specification")
224224

225225

226-
@pytest.mark.xfail(reason="Regression in new arg parsing")
227226
def test_docker_handle_fail(temp_cwd):
228227
"""
229228
Test to check if r2d fails with minimal error message on not being able to connect to docker daemon
@@ -233,19 +232,22 @@ def test_docker_handle_fail(temp_cwd):
233232
assert not validate_arguments(
234233
builddir,
235234
args_list,
236-
"Docker client initialization error. Check if docker is running on the host.",
235+
"Check if docker is running on the host.",
237236
disable_dockerd=True,
238237
)
239238

240239

241240
def test_docker_handle_debug_fail(temp_cwd):
242241
"""
243-
Test to check if r2d fails with stack trace on not being able to connect to docker daemon and debug enabled
242+
Test to check if r2d fails with helpful error message on not being able to connect to docker daemon and debug enabled
244243
"""
245244
args_list = ["--debug"]
246245

247246
assert not validate_arguments(
248-
builddir, args_list, "docker.errors.DockerException", disable_dockerd=True
247+
builddir,
248+
args_list,
249+
"Check if docker is running on the host.",
250+
disable_dockerd=True,
249251
)
250252

251253

tests/unit/test_subdir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_subdir_in_image_name():
3333
assert escaped_dirname in app.output_image_spec
3434

3535

36-
def test_subdir_invalid(caplog):
36+
def test_subdir_invalid():
3737
# test an error is raised when requesting a non existent subdir
3838
app = Repo2Docker(repo=TEST_REPO, subdir="invalid-sub-dir")
3939
app.initialize()

0 commit comments

Comments
 (0)