Skip to content

Commit 2b1c7eb

Browse files
authored
Merge pull request #2650 from aiordache/default_version_auto
Set default version to 'auto'
2 parents f158888 + c7c5b55 commit 2b1c7eb

File tree

7 files changed

+84
-62
lines changed

7 files changed

+84
-62
lines changed

docker/api/client.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
import six
88
import websocket
99

10+
from .. import auth
11+
from ..constants import (DEFAULT_NUM_POOLS, DEFAULT_NUM_POOLS_SSH,
12+
DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT,
13+
IS_WINDOWS_PLATFORM, MINIMUM_DOCKER_API_VERSION,
14+
STREAM_HEADER_SIZE_BYTES)
15+
from ..errors import (DockerException, InvalidVersion, TLSParameterError,
16+
create_api_error_from_http_exception)
17+
from ..tls import TLSConfig
18+
from ..transport import SSLHTTPAdapter, UnixHTTPAdapter
19+
from ..utils import check_resource, config, update_headers, utils
20+
from ..utils.json_stream import json_stream
21+
from ..utils.proxy import ProxyConfig
22+
from ..utils.socket import consume_socket_output, demux_adaptor, frames_iter
1023
from .build import BuildApiMixin
1124
from .config import ConfigApiMixin
1225
from .container import ContainerApiMixin
@@ -19,22 +32,7 @@
1932
from .service import ServiceApiMixin
2033
from .swarm import SwarmApiMixin
2134
from .volume import VolumeApiMixin
22-
from .. import auth
23-
from ..constants import (
24-
DEFAULT_TIMEOUT_SECONDS, DEFAULT_USER_AGENT, IS_WINDOWS_PLATFORM,
25-
DEFAULT_DOCKER_API_VERSION, MINIMUM_DOCKER_API_VERSION,
26-
STREAM_HEADER_SIZE_BYTES, DEFAULT_NUM_POOLS_SSH, DEFAULT_NUM_POOLS
27-
)
28-
from ..errors import (
29-
DockerException, InvalidVersion, TLSParameterError,
30-
create_api_error_from_http_exception
31-
)
32-
from ..tls import TLSConfig
33-
from ..transport import SSLHTTPAdapter, UnixHTTPAdapter
34-
from ..utils import utils, check_resource, update_headers, config
35-
from ..utils.socket import frames_iter, consume_socket_output, demux_adaptor
36-
from ..utils.json_stream import json_stream
37-
from ..utils.proxy import ProxyConfig
35+
3836
try:
3937
from ..transport import NpipeHTTPAdapter
4038
except ImportError:
@@ -183,14 +181,14 @@ def __init__(self, base_url=None, version=None,
183181
self.base_url = base_url
184182

185183
# version detection needs to be after unix adapter mounting
186-
if version is None:
187-
self._version = DEFAULT_DOCKER_API_VERSION
188-
elif isinstance(version, six.string_types):
189-
if version.lower() == 'auto':
190-
self._version = self._retrieve_server_version()
191-
else:
192-
self._version = version
184+
if version is None or (isinstance(
185+
version,
186+
six.string_types
187+
) and version.lower() == 'auto'):
188+
self._version = self._retrieve_server_version()
193189
else:
190+
self._version = version
191+
if not isinstance(self._version, six.string_types):
194192
raise DockerException(
195193
'Version parameter must be a string or None. Found {0}'.format(
196194
type(version).__name__

docker/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def from_env(cls, **kwargs):
6262
6363
Args:
6464
version (str): The version of the API to use. Set to ``auto`` to
65-
automatically detect the server's version. Default: ``1.35``
65+
automatically detect the server's version. Default: ``auto``
6666
timeout (int): Default timeout for API calls, in seconds.
6767
ssl_version (int): A valid `SSL version`_.
6868
assert_hostname (bool): Verify the hostname of the server.

tests/unit/api_test.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import datetime
2-
import json
32
import io
3+
import json
44
import os
55
import re
66
import shutil
77
import socket
8+
import struct
89
import tempfile
910
import threading
1011
import time
1112
import unittest
1213

1314
import docker
14-
from docker.api import APIClient
15+
import pytest
1516
import requests
16-
from requests.packages import urllib3
1717
import six
18-
import struct
18+
from docker.api import APIClient
19+
from docker.constants import DEFAULT_DOCKER_API_VERSION
20+
from requests.packages import urllib3
1921

2022
from . import fake_api
2123

22-
import pytest
23-
2424
try:
2525
from unittest import mock
2626
except ImportError:
@@ -105,7 +105,7 @@ def setUp(self):
105105
_read_from_socket=fake_read_from_socket
106106
)
107107
self.patcher.start()
108-
self.client = APIClient()
108+
self.client = APIClient(version=DEFAULT_DOCKER_API_VERSION)
109109

110110
def tearDown(self):
111111
self.client.close()
@@ -282,27 +282,37 @@ def _socket_path_for_client_session(self, client):
282282
return socket_adapter.socket_path
283283

284284
def test_url_compatibility_unix(self):
285-
c = APIClient(base_url="unix://socket")
285+
c = APIClient(
286+
base_url="unix://socket",
287+
version=DEFAULT_DOCKER_API_VERSION)
286288

287289
assert self._socket_path_for_client_session(c) == '/socket'
288290

289291
def test_url_compatibility_unix_triple_slash(self):
290-
c = APIClient(base_url="unix:///socket")
292+
c = APIClient(
293+
base_url="unix:///socket",
294+
version=DEFAULT_DOCKER_API_VERSION)
291295

292296
assert self._socket_path_for_client_session(c) == '/socket'
293297

294298
def test_url_compatibility_http_unix_triple_slash(self):
295-
c = APIClient(base_url="http+unix:///socket")
299+
c = APIClient(
300+
base_url="http+unix:///socket",
301+
version=DEFAULT_DOCKER_API_VERSION)
296302

297303
assert self._socket_path_for_client_session(c) == '/socket'
298304

299305
def test_url_compatibility_http(self):
300-
c = APIClient(base_url="http://hostname:1234")
306+
c = APIClient(
307+
base_url="http://hostname:1234",
308+
version=DEFAULT_DOCKER_API_VERSION)
301309

302310
assert c.base_url == "http://hostname:1234"
303311

304312
def test_url_compatibility_tcp(self):
305-
c = APIClient(base_url="tcp://hostname:1234")
313+
c = APIClient(
314+
base_url="tcp://hostname:1234",
315+
version=DEFAULT_DOCKER_API_VERSION)
306316

307317
assert c.base_url == "http://hostname:1234"
308318

@@ -447,7 +457,9 @@ def test_early_stream_response(self):
447457
b'\r\n'
448458
) + b'\r\n'.join(lines)
449459

450-
with APIClient(base_url="http+unix://" + self.socket_file) as client:
460+
with APIClient(
461+
base_url="http+unix://" + self.socket_file,
462+
version=DEFAULT_DOCKER_API_VERSION) as client:
451463
for i in range(5):
452464
try:
453465
stream = client.build(
@@ -532,7 +544,10 @@ def frame_header(stream, data):
532544

533545
def request(self, stream=None, tty=None, demux=None):
534546
assert stream is not None and tty is not None and demux is not None
535-
with APIClient(base_url=self.address) as client:
547+
with APIClient(
548+
base_url=self.address,
549+
version=DEFAULT_DOCKER_API_VERSION
550+
) as client:
536551
if tty:
537552
url = client._url('/tty')
538553
else:
@@ -597,7 +612,7 @@ def tearDown(self):
597612
self.patcher.stop()
598613

599614
def test_default_user_agent(self):
600-
client = APIClient()
615+
client = APIClient(version=DEFAULT_DOCKER_API_VERSION)
601616
client.version()
602617

603618
assert self.mock_send.call_count == 1
@@ -606,7 +621,9 @@ def test_default_user_agent(self):
606621
assert headers['User-Agent'] == expected
607622

608623
def test_custom_user_agent(self):
609-
client = APIClient(user_agent='foo/bar')
624+
client = APIClient(
625+
user_agent='foo/bar',
626+
version=DEFAULT_DOCKER_API_VERSION)
610627
client.version()
611628

612629
assert self.mock_send.call_count == 1
@@ -626,7 +643,7 @@ def gettimeout(self):
626643
return self.timeout
627644

628645
def setUp(self):
629-
self.client = APIClient()
646+
self.client = APIClient(version=DEFAULT_DOCKER_API_VERSION)
630647

631648
def test_disable_socket_timeout(self):
632649
"""Test that the timeout is disabled on a generic socket object."""

tests/unit/client_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import datetime
2-
import docker
3-
from docker.utils import kwargs_from_env
4-
from docker.constants import (
5-
DEFAULT_DOCKER_API_VERSION, DEFAULT_TIMEOUT_SECONDS
6-
)
72
import os
83
import unittest
94

10-
from . import fake_api
5+
import docker
116
import pytest
7+
from docker.constants import (
8+
DEFAULT_DOCKER_API_VERSION, DEFAULT_TIMEOUT_SECONDS)
9+
from docker.utils import kwargs_from_env
10+
11+
from . import fake_api
1212

1313
try:
1414
from unittest import mock
@@ -25,33 +25,33 @@ class ClientTest(unittest.TestCase):
2525
def test_events(self, mock_func):
2626
since = datetime.datetime(2016, 1, 1, 0, 0)
2727
mock_func.return_value = fake_api.get_fake_events()[1]
28-
client = docker.from_env()
28+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
2929
assert client.events(since=since) == mock_func.return_value
3030
mock_func.assert_called_with(since=since)
3131

3232
@mock.patch('docker.api.APIClient.info')
3333
def test_info(self, mock_func):
3434
mock_func.return_value = fake_api.get_fake_info()[1]
35-
client = docker.from_env()
35+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
3636
assert client.info() == mock_func.return_value
3737
mock_func.assert_called_with()
3838

3939
@mock.patch('docker.api.APIClient.ping')
4040
def test_ping(self, mock_func):
4141
mock_func.return_value = True
42-
client = docker.from_env()
42+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
4343
assert client.ping() is True
4444
mock_func.assert_called_with()
4545

4646
@mock.patch('docker.api.APIClient.version')
4747
def test_version(self, mock_func):
4848
mock_func.return_value = fake_api.get_fake_version()[1]
49-
client = docker.from_env()
49+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
5050
assert client.version() == mock_func.return_value
5151
mock_func.assert_called_with()
5252

5353
def test_call_api_client_method(self):
54-
client = docker.from_env()
54+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
5555
with pytest.raises(AttributeError) as cm:
5656
client.create_container()
5757
s = cm.exconly()
@@ -65,7 +65,9 @@ def test_call_api_client_method(self):
6565
assert "this method is now on the object APIClient" not in s
6666

6767
def test_call_containers(self):
68-
client = docker.DockerClient(**kwargs_from_env())
68+
client = docker.DockerClient(
69+
version=DEFAULT_DOCKER_API_VERSION,
70+
**kwargs_from_env())
6971

7072
with pytest.raises(TypeError) as cm:
7173
client.containers()
@@ -90,7 +92,7 @@ def test_from_env(self):
9092
os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
9193
DOCKER_CERT_PATH=TEST_CERT_DIR,
9294
DOCKER_TLS_VERIFY='1')
93-
client = docker.from_env()
95+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
9496
assert client.api.base_url == "https://192.168.59.103:2376"
9597

9698
def test_from_env_with_version(self):
@@ -102,11 +104,11 @@ def test_from_env_with_version(self):
102104
assert client.api._version == '2.32'
103105

104106
def test_from_env_without_version_uses_default(self):
105-
client = docker.from_env()
107+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
106108

107109
assert client.api._version == DEFAULT_DOCKER_API_VERSION
108110

109111
def test_from_env_without_timeout_uses_default(self):
110-
client = docker.from_env()
112+
client = docker.from_env(version=DEFAULT_DOCKER_API_VERSION)
111113

112114
assert client.api.timeout == DEFAULT_TIMEOUT_SECONDS

tests/unit/fake_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from . import fake_stat
21
from docker import constants
32

3+
from . import fake_stat
4+
45
CURRENT_VERSION = 'v{0}'.format(constants.DEFAULT_DOCKER_API_VERSION)
56

67
FAKE_CONTAINER_ID = '3cc2351ab11b'

tests/unit/fake_api_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
2-
import docker
32

3+
import docker
4+
from docker.constants import DEFAULT_DOCKER_API_VERSION
45
from . import fake_api
56

67
try:
@@ -30,7 +31,7 @@ def make_fake_api_client(overrides=None):
3031

3132
if overrides is None:
3233
overrides = {}
33-
api_client = docker.APIClient()
34+
api_client = docker.APIClient(version=DEFAULT_DOCKER_API_VERSION)
3435
mock_attrs = {
3536
'build.return_value': fake_api.FAKE_IMAGE_ID,
3637
'commit.return_value': fake_api.post_fake_commit()[1],
@@ -50,6 +51,7 @@ def make_fake_api_client(overrides=None):
5051
'networks.return_value': fake_api.get_fake_network_list()[1],
5152
'start.return_value': None,
5253
'wait.return_value': {'StatusCode': 0},
54+
'version.return_value': fake_api.get_fake_version()
5355
}
5456
mock_attrs.update(overrides)
5557
mock_client = CopyReturnMagicMock(**mock_attrs)
@@ -62,6 +64,6 @@ def make_fake_client(overrides=None):
6264
"""
6365
Returns a Client with a fake APIClient.
6466
"""
65-
client = docker.DockerClient()
67+
client = docker.DockerClient(version=DEFAULT_DOCKER_API_VERSION)
6668
client.api = make_fake_api_client(overrides)
6769
return client

tests/unit/utils_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212
import six
1313
from docker.api.client import APIClient
14-
from docker.constants import IS_WINDOWS_PLATFORM
14+
from docker.constants import IS_WINDOWS_PLATFORM, DEFAULT_DOCKER_API_VERSION
1515
from docker.errors import DockerException
1616
from docker.utils import (convert_filters, convert_volume_binds,
1717
decode_json_header, kwargs_from_env, parse_bytes,
@@ -35,7 +35,7 @@ def test_update_headers(self):
3535
def f(self, headers=None):
3636
return headers
3737

38-
client = APIClient()
38+
client = APIClient(version=DEFAULT_DOCKER_API_VERSION)
3939
client._general_configs = {}
4040

4141
g = update_headers(f)
@@ -86,6 +86,7 @@ def test_kwargs_from_env_tls(self):
8686
assert kwargs['tls'].verify
8787

8888
parsed_host = parse_host(kwargs['base_url'], IS_WINDOWS_PLATFORM, True)
89+
kwargs['version'] = DEFAULT_DOCKER_API_VERSION
8990
try:
9091
client = APIClient(**kwargs)
9192
assert parsed_host == client.base_url
@@ -106,6 +107,7 @@ def test_kwargs_from_env_tls_verify_false(self):
106107
assert kwargs['tls'].assert_hostname is True
107108
assert kwargs['tls'].verify is False
108109
parsed_host = parse_host(kwargs['base_url'], IS_WINDOWS_PLATFORM, True)
110+
kwargs['version'] = DEFAULT_DOCKER_API_VERSION
109111
try:
110112
client = APIClient(**kwargs)
111113
assert parsed_host == client.base_url

0 commit comments

Comments
 (0)