errors_test.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 compose.const import IS_WINDOWS_PLATFORM
  9. from tests import mock
  10. @pytest.yield_fixture
  11. def mock_logging():
  12. with mock.patch('compose.cli.errors.log', autospec=True) as mock_log:
  13. yield mock_log
  14. def patch_find_executable(side_effect):
  15. return mock.patch(
  16. 'compose.cli.errors.find_executable',
  17. autospec=True,
  18. side_effect=side_effect)
  19. class TestHandleConnectionErrors(object):
  20. def test_generic_connection_error(self, mock_logging):
  21. with pytest.raises(errors.ConnectionError):
  22. with patch_find_executable(['/bin/docker', None]):
  23. with handle_connection_errors(mock.Mock()):
  24. raise ConnectionError()
  25. _, args, _ = mock_logging.error.mock_calls[0]
  26. assert "Couldn't connect to Docker daemon" in args[0]
  27. def test_api_error_version_mismatch(self, mock_logging):
  28. with pytest.raises(errors.ConnectionError):
  29. with handle_connection_errors(mock.Mock(api_version='1.22')):
  30. raise APIError(None, None, b"client is newer than server")
  31. _, args, _ = mock_logging.error.mock_calls[0]
  32. assert "Docker Engine of version 1.10.0 or greater" in args[0]
  33. def test_api_error_version_mismatch_unicode_explanation(self, mock_logging):
  34. with pytest.raises(errors.ConnectionError):
  35. with handle_connection_errors(mock.Mock(api_version='1.22')):
  36. raise APIError(None, None, u"client is newer than server")
  37. _, args, _ = mock_logging.error.mock_calls[0]
  38. assert "Docker Engine of version 1.10.0 or greater" in args[0]
  39. def test_api_error_version_other(self, mock_logging):
  40. msg = b"Something broke!"
  41. with pytest.raises(errors.ConnectionError):
  42. with handle_connection_errors(mock.Mock(api_version='1.22')):
  43. raise APIError(None, None, msg)
  44. mock_logging.error.assert_called_once_with(msg.decode('utf-8'))
  45. def test_api_error_version_other_unicode_explanation(self, mock_logging):
  46. msg = u"Something broke!"
  47. with pytest.raises(errors.ConnectionError):
  48. with handle_connection_errors(mock.Mock(api_version='1.22')):
  49. raise APIError(None, None, msg)
  50. mock_logging.error.assert_called_once_with(msg)
  51. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='Needs pywin32')
  52. def test_windows_pipe_error_no_data(self, mock_logging):
  53. import pywintypes
  54. with pytest.raises(errors.ConnectionError):
  55. with handle_connection_errors(mock.Mock(api_version='1.22')):
  56. raise pywintypes.error(232, 'WriteFile', 'The pipe is being closed.')
  57. _, args, _ = mock_logging.error.mock_calls[0]
  58. assert "The current Compose file version is not compatible with your engine version." in args[0]
  59. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='Needs pywin32')
  60. def test_windows_pipe_error_misc(self, mock_logging):
  61. import pywintypes
  62. with pytest.raises(errors.ConnectionError):
  63. with handle_connection_errors(mock.Mock(api_version='1.22')):
  64. raise pywintypes.error(231, 'WriteFile', 'The pipe is busy.')
  65. _, args, _ = mock_logging.error.mock_calls[0]
  66. assert "Windows named pipe error: The pipe is busy. (code: 231)" == args[0]