diff --git a/repo2docker/app.py b/repo2docker/app.py index 3ca1325d..d89e430e 100644 --- a/repo2docker/app.py +++ b/repo2docker/app.py @@ -626,8 +626,12 @@ class Repo2Docker(Application): try: docker_client = docker.APIClient(version="auto", **kwargs_from_env()) except DockerException as e: - self.log.exception(e) - raise + self.log.error( + "\nDocker client initialization error: %s.\nCheck if docker is running on the host.\n", + e, + ) + self.exit(1) + # If the source to be executed is a directory, continue using the # directory. In the case of a local directory, it is used as both the # source and target. Reusing a local directory seems better than diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 55845092..d45c28a4 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -8,6 +8,7 @@ import escapism from repo2docker.app import Repo2Docker from repo2docker.__main__ import make_r2d +from repo2docker.utils import chdir def test_find_image(): @@ -124,3 +125,25 @@ def test_root_not_allowed(): builds.return_value = [] app.build() builds.assert_called_once() + + +def test_dryrun_works_without_docker(tmpdir, capsys): + with chdir(tmpdir): + with patch.object(docker, "APIClient") as client: + client.side_effect = docker.errors.DockerException("Error: no Docker") + app = Repo2Docker(dry_run=True) + app.build() + captured = capsys.readouterr() + assert "Error: no Docker" not in captured.err + + +def test_error_log_without_docker(tmpdir, capsys): + with chdir(tmpdir): + with patch.object(docker, "APIClient") as client: + client.side_effect = docker.errors.DockerException("Error: no Docker") + app = Repo2Docker() + + with pytest.raises(SystemExit): + app.build() + captured = capsys.readouterr() + assert "Error: no Docker" in captured.err diff --git a/tests/unit/test_argumentvalidation.py b/tests/unit/test_argumentvalidation.py index 1cdc203a..11647e37 100644 --- a/tests/unit/test_argumentvalidation.py +++ b/tests/unit/test_argumentvalidation.py @@ -223,7 +223,6 @@ def test_invalid_container_port_protocol_mapping_fail(temp_cwd): assert not validate_arguments(builddir, args_list, "Port specification") -@pytest.mark.xfail(reason="Regression in new arg parsing") def test_docker_handle_fail(temp_cwd): """ 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): assert not validate_arguments( builddir, args_list, - "Docker client initialization error. Check if docker is running on the host.", + "Check if docker is running on the host.", disable_dockerd=True, ) def test_docker_handle_debug_fail(temp_cwd): """ - Test to check if r2d fails with stack trace on not being able to connect to docker daemon and debug enabled + Test to check if r2d fails with helpful error message on not being able to connect to docker daemon and debug enabled """ args_list = ["--debug"] assert not validate_arguments( - builddir, args_list, "docker.errors.DockerException", disable_dockerd=True + builddir, + args_list, + "Check if docker is running on the host.", + disable_dockerd=True, ) diff --git a/tests/unit/test_subdir.py b/tests/unit/test_subdir.py index 05a77af2..43d08fce 100644 --- a/tests/unit/test_subdir.py +++ b/tests/unit/test_subdir.py @@ -33,7 +33,7 @@ def test_subdir_in_image_name(): assert escaped_dirname in app.output_image_spec -def test_subdir_invalid(caplog): +def test_subdir_invalid(): # test an error is raised when requesting a non existent subdir app = Repo2Docker(repo=TEST_REPO, subdir="invalid-sub-dir") app.initialize()