errors_test.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import pytest
  4. from docker.errors import APIError
  5. from requests.exceptions import ConnectionError
  6. from compose.cli import errors
  7. from compose.cli.errors import handle_connection_errors
  8. from tests import mock
  9. @pytest.yield_fixture
  10. def mock_logging():
  11. with mock.patch('compose.cli.errors.log', autospec=True) as mock_log:
  12. yield mock_log
  13. def patch_find_executable(side_effect):
  14. return mock.patch(
  15. 'compose.cli.errors.find_executable',
  16. autospec=True,
  17. side_effect=side_effect)
  18. class TestHandleConnectionErrors(object):
  19. def test_generic_connection_error(self, mock_logging):
  20. with pytest.raises(errors.ConnectionError):
  21. with patch_find_executable(['/bin/docker', None]):
  22. with handle_connection_errors(mock.Mock()):
  23. raise ConnectionError()
  24. _, args, _ = mock_logging.error.mock_calls[0]
  25. assert "Couldn't connect to Docker daemon" in args[0]
  26. def test_api_error_version_mismatch(self, mock_logging):
  27. with pytest.raises(errors.ConnectionError):
  28. with handle_connection_errors(mock.Mock(api_version='1.22')):
  29. raise APIError(None, None, b"client is newer than server")
  30. _, args, _ = mock_logging.error.mock_calls[0]
  31. assert "Docker Engine of version 1.10.0 or greater" in args[0]
  32. def test_api_error_version_other(self, mock_logging):
  33. msg = b"Something broke!"
  34. with pytest.raises(errors.ConnectionError):
  35. with handle_connection_errors(mock.Mock(api_version='1.22')):
  36. raise APIError(None, None, msg)
  37. mock_logging.error.assert_called_once_with(msg)