errors_test.py 3.9 KB

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