Improve handling of connection errors and error messages.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
886328640f
commit
0a091055d2
5 changed files with 153 additions and 113 deletions
|
|
@ -4,28 +4,12 @@ from __future__ import unicode_literals
|
|||
import os
|
||||
|
||||
import pytest
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from compose.cli import errors
|
||||
from compose.cli.command import friendly_error_message
|
||||
from compose.cli.command import get_config_path_from_options
|
||||
from compose.const import IS_WINDOWS_PLATFORM
|
||||
from tests import mock
|
||||
|
||||
|
||||
class TestFriendlyErrorMessage(object):
|
||||
|
||||
def test_dispatch_generic_connection_error(self):
|
||||
with pytest.raises(errors.ConnectionErrorGeneric):
|
||||
with mock.patch(
|
||||
'compose.cli.command.call_silently',
|
||||
autospec=True,
|
||||
side_effect=[0, 1]
|
||||
):
|
||||
with friendly_error_message():
|
||||
raise ConnectionError()
|
||||
|
||||
|
||||
class TestGetConfigPathFromOptions(object):
|
||||
|
||||
def test_path_from_options(self):
|
||||
|
|
|
|||
51
tests/unit/cli/errors_test.py
Normal file
51
tests/unit/cli/errors_test.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import pytest
|
||||
from docker.errors import APIError
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from compose.cli import errors
|
||||
from compose.cli.errors import handle_connection_errors
|
||||
from tests import mock
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def mock_logging():
|
||||
with mock.patch('compose.cli.errors.log', autospec=True) as mock_log:
|
||||
yield mock_log
|
||||
|
||||
|
||||
def patch_call_silently(side_effect):
|
||||
return mock.patch(
|
||||
'compose.cli.errors.call_silently',
|
||||
autospec=True,
|
||||
side_effect=side_effect)
|
||||
|
||||
|
||||
class TestHandleConnectionErrors(object):
|
||||
|
||||
def test_generic_connection_error(self, mock_logging):
|
||||
with pytest.raises(errors.ConnectionError):
|
||||
with patch_call_silently([0, 1]):
|
||||
with handle_connection_errors(mock.Mock()):
|
||||
raise ConnectionError()
|
||||
|
||||
_, args, _ = mock_logging.error.mock_calls[0]
|
||||
assert "Couldn't connect to Docker daemon at" in args[0]
|
||||
|
||||
def test_api_error_version_mismatch(self, mock_logging):
|
||||
with pytest.raises(errors.ConnectionError):
|
||||
with handle_connection_errors(mock.Mock(api_version='1.22')):
|
||||
raise APIError(None, None, "client is newer than server")
|
||||
|
||||
_, args, _ = mock_logging.error.mock_calls[0]
|
||||
assert "Docker Engine of version 1.10.0 or greater" in args[0]
|
||||
|
||||
def test_api_error_version_other(self, mock_logging):
|
||||
msg = "Something broke!"
|
||||
with pytest.raises(errors.ConnectionError):
|
||||
with handle_connection_errors(mock.Mock(api_version='1.22')):
|
||||
raise APIError(None, None, msg)
|
||||
|
||||
mock_logging.error.assert_called_once_with(msg)
|
||||
Loading…
Add table
Add a link
Reference in a new issue