From ee75a1c2e349fccab4a1bcb49142756c9a8495db Mon Sep 17 00:00:00 2001 From: grahamlyons Date: Thu, 8 Jun 2017 14:31:25 +0100 Subject: [PATCH 1/2] Ensure default timeout is used by API Client The `from_env` method on the `docker` module passed `None` as the value for the `timeout` keyword argument which overrode the default value in the initialiser, taken from `constants` module. This sets the default in the initialiser to `None` and adds logic to set that, in the same way that `version` is handled. Signed-off-by: grahamlyons --- docker/api/client.py | 9 ++++++--- tests/unit/client_test.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docker/api/client.py b/docker/api/client.py index 54ec6abb..6822f7c7 100644 --- a/docker/api/client.py +++ b/docker/api/client.py @@ -83,8 +83,7 @@ class APIClient( configuration. user_agent (str): Set a custom user agent for requests to the server. """ - def __init__(self, base_url=None, version=None, - timeout=DEFAULT_TIMEOUT_SECONDS, tls=False, + def __init__(self, base_url=None, version=None, timeout=None, tls=False, user_agent=DEFAULT_USER_AGENT, num_pools=DEFAULT_NUM_POOLS): super(APIClient, self).__init__() @@ -94,7 +93,11 @@ class APIClient( ) self.base_url = base_url - self.timeout = timeout + if timeout is not None: + self.timeout = timeout + else: + self.timeout = DEFAULT_TIMEOUT_SECONDS + self.headers['User-Agent'] = user_agent self._auth_configs = auth.load_config() diff --git a/tests/unit/client_test.py b/tests/unit/client_test.py index b79c68e1..c4996f13 100644 --- a/tests/unit/client_test.py +++ b/tests/unit/client_test.py @@ -1,6 +1,9 @@ import datetime import docker from docker.utils import kwargs_from_env +from docker.constants import ( + DEFAULT_DOCKER_API_VERSION, DEFAULT_TIMEOUT_SECONDS +) import os import unittest @@ -96,3 +99,13 @@ class FromEnvTest(unittest.TestCase): client = docker.from_env(version='2.32') self.assertEqual(client.api.base_url, "https://192.168.59.103:2376") self.assertEqual(client.api._version, '2.32') + + def test_from_env_without_version_uses_default(self): + client = docker.from_env() + + self.assertEqual(client.api._version, DEFAULT_DOCKER_API_VERSION) + + def test_from_env_without_timeout_uses_default(self): + client = docker.from_env() + + self.assertEqual(client.api.timeout, DEFAULT_TIMEOUT_SECONDS) From ff993dd858ffb3c6367013ed2c468903f0cf4fe9 Mon Sep 17 00:00:00 2001 From: grahamlyons Date: Fri, 9 Jun 2017 09:47:00 +0100 Subject: [PATCH 2/2] Move default `timeout` into `from_env` We'd like to be able to pass `None` as a value for `timeout` because it has meaning to the `requests` library (http://docs.python-requests.org/en/master/user/advanced/#timeouts) Signed-off-by: grahamlyons --- docker/api/client.py | 9 +++------ docker/client.py | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/docker/api/client.py b/docker/api/client.py index 6822f7c7..54ec6abb 100644 --- a/docker/api/client.py +++ b/docker/api/client.py @@ -83,7 +83,8 @@ class APIClient( configuration. user_agent (str): Set a custom user agent for requests to the server. """ - def __init__(self, base_url=None, version=None, timeout=None, tls=False, + def __init__(self, base_url=None, version=None, + timeout=DEFAULT_TIMEOUT_SECONDS, tls=False, user_agent=DEFAULT_USER_AGENT, num_pools=DEFAULT_NUM_POOLS): super(APIClient, self).__init__() @@ -93,11 +94,7 @@ class APIClient( ) self.base_url = base_url - if timeout is not None: - self.timeout = timeout - else: - self.timeout = DEFAULT_TIMEOUT_SECONDS - + self.timeout = timeout self.headers['User-Agent'] = user_agent self._auth_configs = auth.load_config() diff --git a/docker/client.py b/docker/client.py index 09abd633..fcfb01d8 100644 --- a/docker/client.py +++ b/docker/client.py @@ -1,4 +1,5 @@ from .api.client import APIClient +from .constants import DEFAULT_TIMEOUT_SECONDS from .models.containers import ContainerCollection from .models.images import ImageCollection from .models.networks import NetworkCollection @@ -73,7 +74,7 @@ class DockerClient(object): .. _`SSL version`: https://docs.python.org/3.5/library/ssl.html#ssl.PROTOCOL_TLSv1 """ - timeout = kwargs.pop('timeout', None) + timeout = kwargs.pop('timeout', DEFAULT_TIMEOUT_SECONDS) version = kwargs.pop('version', None) return cls(timeout=timeout, version=version, **kwargs_from_env(**kwargs))