浏览代码

Merge pull request #3705 from aanand/fix-timeout-message

Fix timeout value in error message
Daniel Nephin 9 年之前
父节点
当前提交
619bf4c4df
共有 4 个文件被更改,包括 23 次插入14 次删除
  1. 0 4
      compose/cli/docker_client.py
  2. 3 4
      compose/cli/errors.py
  3. 1 2
      compose/const.py
  4. 19 4
      tests/unit/cli/docker_client_test.py

+ 0 - 4
compose/cli/docker_client.py

@@ -45,10 +45,6 @@ def docker_client(environment, version=None, tls_config=None, host=None,
     Returns a docker-py client configured using environment variables
     Returns a docker-py client configured using environment variables
     according to the same logic as the official Docker client.
     according to the same logic as the official Docker client.
     """
     """
-    if 'DOCKER_CLIENT_TIMEOUT' in environment:
-        log.warn("The DOCKER_CLIENT_TIMEOUT environment variable is deprecated.  "
-                 "Please use COMPOSE_HTTP_TIMEOUT instead.")
-
     try:
     try:
         kwargs = kwargs_from_env(environment=environment, ssl_version=tls_version)
         kwargs = kwargs_from_env(environment=environment, ssl_version=tls_version)
     except TLSParameterError:
     except TLSParameterError:

+ 3 - 4
compose/cli/errors.py

@@ -13,7 +13,6 @@ from requests.exceptions import SSLError
 from requests.packages.urllib3.exceptions import ReadTimeoutError
 from requests.packages.urllib3.exceptions import ReadTimeoutError
 
 
 from ..const import API_VERSION_TO_ENGINE_VERSION
 from ..const import API_VERSION_TO_ENGINE_VERSION
-from ..const import HTTP_TIMEOUT
 from .utils import call_silently
 from .utils import call_silently
 from .utils import is_docker_for_mac_installed
 from .utils import is_docker_for_mac_installed
 from .utils import is_mac
 from .utils import is_mac
@@ -47,7 +46,7 @@ def handle_connection_errors(client):
         raise ConnectionError()
         raise ConnectionError()
     except RequestsConnectionError as e:
     except RequestsConnectionError as e:
         if e.args and isinstance(e.args[0], ReadTimeoutError):
         if e.args and isinstance(e.args[0], ReadTimeoutError):
-            log_timeout_error()
+            log_timeout_error(client.timeout)
             raise ConnectionError()
             raise ConnectionError()
         exit_with_error(get_conn_error_message(client.base_url))
         exit_with_error(get_conn_error_message(client.base_url))
     except APIError as e:
     except APIError as e:
@@ -58,13 +57,13 @@ def handle_connection_errors(client):
         raise ConnectionError()
         raise ConnectionError()
 
 
 
 
-def log_timeout_error():
+def log_timeout_error(timeout):
     log.error(
     log.error(
         "An HTTP request took too long to complete. Retry with --verbose to "
         "An HTTP request took too long to complete. Retry with --verbose to "
         "obtain debug information.\n"
         "obtain debug information.\n"
         "If you encounter this issue regularly because of slow network "
         "If you encounter this issue regularly because of slow network "
         "conditions, consider setting COMPOSE_HTTP_TIMEOUT to a higher "
         "conditions, consider setting COMPOSE_HTTP_TIMEOUT to a higher "
-        "value (current value: %s)." % HTTP_TIMEOUT)
+        "value (current value: %s)." % timeout)
 
 
 
 
 def log_api_error(e, client_version):
 def log_api_error(e, client_version):

+ 1 - 2
compose/const.py

@@ -1,11 +1,10 @@
 from __future__ import absolute_import
 from __future__ import absolute_import
 from __future__ import unicode_literals
 from __future__ import unicode_literals
 
 
-import os
 import sys
 import sys
 
 
 DEFAULT_TIMEOUT = 10
 DEFAULT_TIMEOUT = 10
-HTTP_TIMEOUT = int(os.environ.get('DOCKER_CLIENT_TIMEOUT', 60))
+HTTP_TIMEOUT = 60
 IMAGE_EVENTS = ['delete', 'import', 'pull', 'push', 'tag', 'untag']
 IMAGE_EVENTS = ['delete', 'import', 'pull', 'push', 'tag', 'untag']
 IS_WINDOWS_PLATFORM = (sys.platform == "win32")
 IS_WINDOWS_PLATFORM = (sys.platform == "win32")
 LABEL_CONTAINER_NUMBER = 'com.docker.compose.container-number'
 LABEL_CONTAINER_NUMBER = 'com.docker.compose.container-number'

+ 19 - 4
tests/unit/cli/docker_client_test.py

@@ -6,6 +6,7 @@ import os
 import docker
 import docker
 import pytest
 import pytest
 
 
+from compose.cli import errors
 from compose.cli.docker_client import docker_client
 from compose.cli.docker_client import docker_client
 from compose.cli.docker_client import tls_config_from_options
 from compose.cli.docker_client import tls_config_from_options
 from tests import mock
 from tests import mock
@@ -19,11 +20,25 @@ class DockerClientTestCase(unittest.TestCase):
             del os.environ['HOME']
             del os.environ['HOME']
             docker_client(os.environ)
             docker_client(os.environ)
 
 
+    @mock.patch.dict(os.environ)
     def test_docker_client_with_custom_timeout(self):
     def test_docker_client_with_custom_timeout(self):
-        timeout = 300
-        with mock.patch('compose.cli.docker_client.HTTP_TIMEOUT', 300):
-            client = docker_client(os.environ)
-            self.assertEqual(client.timeout, int(timeout))
+        os.environ['COMPOSE_HTTP_TIMEOUT'] = '123'
+        client = docker_client(os.environ)
+        assert client.timeout == 123
+
+    @mock.patch.dict(os.environ)
+    def test_custom_timeout_error(self):
+        os.environ['COMPOSE_HTTP_TIMEOUT'] = '123'
+        client = docker_client(os.environ)
+
+        with mock.patch('compose.cli.errors.log') as fake_log:
+            with pytest.raises(errors.ConnectionError):
+                with errors.handle_connection_errors(client):
+                    raise errors.RequestsConnectionError(
+                        errors.ReadTimeoutError(None, None, None))
+
+        assert fake_log.error.call_count == 1
+        assert '123' in fake_log.error.call_args[0][0]
 
 
 
 
 class TLSConfigTestCase(unittest.TestCase):
 class TLSConfigTestCase(unittest.TestCase):