errors_test.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_mismatch_unicode_explanation(self, mock_logging):
  33. with pytest.raises(errors.ConnectionError):
  34. with handle_connection_errors(mock.Mock(api_version='1.22')):
  35. raise APIError(None, None, u"client is newer than server")
  36. _, args, _ = mock_logging.error.mock_calls[0]
  37. assert "Docker Engine of version 1.10.0 or greater" in args[0]
  38. def test_api_error_version_other(self, mock_logging):
  39. msg = b"Something broke!"
  40. with pytest.raises(errors.ConnectionError):
  41. with handle_connection_errors(mock.Mock(api_version='1.22')):
  42. raise APIError(None, None, msg)
  43. mock_logging.error.assert_called_once_with(msg.decode('utf-8'))
  44. def test_api_error_version_other_unicode_explanation(self, mock_logging):
  45. msg = u"Something broke!"
  46. with pytest.raises(errors.ConnectionError):
  47. with handle_connection_errors(mock.Mock(api_version='1.22')):
  48. raise APIError(None, None, msg)
  49. mock_logging.error.assert_called_once_with(msg)