mirror of https://github.com/docker/docker-py.git
Merge branch 'master' of github.com:dotcloud/docker-py
This commit is contained in:
commit
bbe80d903f
|
|
@ -2,4 +2,5 @@ include test-requirements.txt
|
||||||
include requirements.txt
|
include requirements.txt
|
||||||
include requirements3.txt
|
include requirements3.txt
|
||||||
include README.md
|
include README.md
|
||||||
include LICENSE
|
include LICENSE
|
||||||
|
recursive-include tests *.py
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,10 @@ Our latest stable is always available on PyPi.
|
||||||
Documentation
|
Documentation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Full documentation is available in the `/docs/` directory.
|
[](https://readthedocs.org/projects/docker-py/?badge=latest)
|
||||||
|
|
||||||
|
Full documentation is hosted on [ReadTheDocs](http://docker-py.readthedocs.org/en/latest/).
|
||||||
|
Sources are available in the `docs/` directory.
|
||||||
|
|
||||||
|
|
||||||
License
|
License
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import urllib3.connectionpool as connectionpool
|
import urllib3.connectionpool as connectionpool
|
||||||
|
|
||||||
|
try:
|
||||||
|
from requests.packages.urllib3._collections import RecentlyUsedContainer
|
||||||
|
except ImportError:
|
||||||
|
from urllib3._collections import RecentlyUsedContainer
|
||||||
|
|
||||||
|
|
||||||
class UnixHTTPConnection(httplib.HTTPConnection, object):
|
class UnixHTTPConnection(httplib.HTTPConnection, object):
|
||||||
def __init__(self, base_url, unix_socket, timeout=60):
|
def __init__(self, base_url, unix_socket, timeout=60):
|
||||||
|
|
@ -65,7 +70,22 @@ class UnixAdapter(requests.adapters.HTTPAdapter):
|
||||||
def __init__(self, base_url, timeout=60):
|
def __init__(self, base_url, timeout=60):
|
||||||
self.base_url = base_url
|
self.base_url = base_url
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self.pools = RecentlyUsedContainer(10,
|
||||||
|
dispose_func=lambda p: p.close())
|
||||||
super(UnixAdapter, self).__init__()
|
super(UnixAdapter, self).__init__()
|
||||||
|
|
||||||
def get_connection(self, socket_path, proxies=None):
|
def get_connection(self, socket_path, proxies=None):
|
||||||
return UnixHTTPConnectionPool(self.base_url, socket_path, self.timeout)
|
with self.pools.lock:
|
||||||
|
pool = self.pools.get(socket_path)
|
||||||
|
if pool:
|
||||||
|
return pool
|
||||||
|
|
||||||
|
pool = UnixHTTPConnectionPool(self.base_url,
|
||||||
|
socket_path,
|
||||||
|
self.timeout)
|
||||||
|
self.pools[socket_path] = pool
|
||||||
|
|
||||||
|
return pool
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.pools.clear()
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from .utils import (
|
from .utils import (
|
||||||
compare_version, convert_port_bindings, convert_volume_binds,
|
compare_version, convert_port_bindings, convert_volume_binds,
|
||||||
mkbuildcontext, ping, tar, parse_repository_tag, parse_host
|
mkbuildcontext, ping, tar, parse_repository_tag, parse_host, kwargs_from_env
|
||||||
) # flake8: noqa
|
) # flake8: noqa
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
import os.path
|
||||||
import tarfile
|
import tarfile
|
||||||
import tempfile
|
import tempfile
|
||||||
from distutils.version import StrictVersion
|
from distutils.version import StrictVersion
|
||||||
|
|
@ -23,6 +24,7 @@ import requests
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from .. import errors
|
from .. import errors
|
||||||
|
from .. import tls
|
||||||
|
|
||||||
DEFAULT_HTTP_HOST = "127.0.0.1"
|
DEFAULT_HTTP_HOST = "127.0.0.1"
|
||||||
DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock"
|
DEFAULT_UNIX_SOCKET = "http+unix://var/run/docker.sock"
|
||||||
|
|
@ -257,3 +259,23 @@ def parse_devices(devices):
|
||||||
"PathInContainer": path_in_container,
|
"PathInContainer": path_in_container,
|
||||||
"CgroupPermissions": permissions})
|
"CgroupPermissions": permissions})
|
||||||
return device_list
|
return device_list
|
||||||
|
|
||||||
|
|
||||||
|
def kwargs_from_env(ssl_version=None, assert_hostname=None):
|
||||||
|
host = os.environ.get('DOCKER_HOST')
|
||||||
|
cert_path = os.environ.get('DOCKER_CERT_PATH')
|
||||||
|
tls_verify = os.environ.get('DOCKER_TLS_VERIFY')
|
||||||
|
|
||||||
|
params = {}
|
||||||
|
if host:
|
||||||
|
params['base_url'] = (host.replace('tcp://', 'https://')
|
||||||
|
if tls_verify else host)
|
||||||
|
if tls_verify and cert_path:
|
||||||
|
params['tls'] = tls.TLSConfig(
|
||||||
|
client_cert=(os.path.join(cert_path, 'cert.pem'),
|
||||||
|
os.path.join(cert_path, 'key.pem')),
|
||||||
|
ca_cert=os.path.join(cert_path, 'ca.pem'),
|
||||||
|
verify=True,
|
||||||
|
ssl_version=ssl_version,
|
||||||
|
assert_hostname=assert_hostname)
|
||||||
|
return params
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import shutil
|
||||||
import signal
|
import signal
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import warnings
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
import six
|
import six
|
||||||
|
|
@ -1035,6 +1036,28 @@ class TestConnectionTimeout(unittest.TestCase):
|
||||||
self.assertTrue(end - start < 2 * self.timeout)
|
self.assertTrue(end - start < 2 * self.timeout)
|
||||||
|
|
||||||
|
|
||||||
|
class UnixconnTestCase(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Test UNIX socket connection adapter.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_resource_warnings(self):
|
||||||
|
"""
|
||||||
|
Test no warnings are produced when using the client.
|
||||||
|
"""
|
||||||
|
|
||||||
|
with warnings.catch_warnings(record=True) as w:
|
||||||
|
warnings.simplefilter('always')
|
||||||
|
|
||||||
|
client = docker.Client()
|
||||||
|
client.images()
|
||||||
|
client.close()
|
||||||
|
del client
|
||||||
|
|
||||||
|
assert len(w) == 0, \
|
||||||
|
"No warnings produced: {0}".format(w[0].message)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
c = docker.Client(base_url=DEFAULT_BASE_URL)
|
c = docker.Client(base_url=DEFAULT_BASE_URL)
|
||||||
c.pull('busybox')
|
c.pull('busybox')
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,9 @@ class DockerClientTest(Cleanup, unittest.TestCase):
|
||||||
# Force-clear authconfig to avoid tampering with the tests
|
# Force-clear authconfig to avoid tampering with the tests
|
||||||
self.client._cfg = {'Configs': {}}
|
self.client._cfg = {'Configs': {}}
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.client.close()
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
# INFORMATION TESTS #
|
# INFORMATION TESTS #
|
||||||
#########################
|
#########################
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,22 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from docker.errors import DockerException
|
from docker.errors import DockerException
|
||||||
from docker.utils import parse_repository_tag, parse_host
|
from docker.utils import parse_repository_tag, parse_host, kwargs_from_env
|
||||||
|
from docker.client import Client
|
||||||
|
|
||||||
|
import os
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
|
||||||
class UtilsTest(unittest.TestCase):
|
class UtilsTest(unittest.TestCase):
|
||||||
longMessage = True
|
longMessage = True
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.os_environ = os.environ.copy()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
os.environ = self.os_environ
|
||||||
|
|
||||||
def test_parse_repository_tag(self):
|
def test_parse_repository_tag(self):
|
||||||
self.assertEqual(parse_repository_tag("root"),
|
self.assertEqual(parse_repository_tag("root"),
|
||||||
("root", None))
|
("root", None))
|
||||||
|
|
@ -53,5 +63,25 @@ class UtilsTest(unittest.TestCase):
|
||||||
for host, expected in valid_hosts.items():
|
for host, expected in valid_hosts.items():
|
||||||
self.assertEqual(parse_host(host), expected, msg=host)
|
self.assertEqual(parse_host(host), expected, msg=host)
|
||||||
|
|
||||||
|
def test_kwargs_from_env(self):
|
||||||
|
os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
|
||||||
|
DOCKER_CERT_PATH=os.path.join(
|
||||||
|
os.path.dirname(__file__),
|
||||||
|
'testdata/certs'),
|
||||||
|
DOCKER_TLS_VERIFY='1')
|
||||||
|
kwargs = kwargs_from_env(assert_hostname=False)
|
||||||
|
self.assertEquals('https://192.168.59.103:2376', kwargs['base_url'])
|
||||||
|
self.assertIn('ca.pem', kwargs['tls'].verify)
|
||||||
|
self.assertIn('cert.pem', kwargs['tls'].cert[0])
|
||||||
|
self.assertIn('key.pem', kwargs['tls'].cert[1])
|
||||||
|
self.assertEquals(False, kwargs['tls'].assert_hostname)
|
||||||
|
try:
|
||||||
|
client = Client(**kwargs)
|
||||||
|
self.assertEquals(kwargs['base_url'], client.base_url)
|
||||||
|
self.assertEquals(kwargs['tls'].verify, client.verify)
|
||||||
|
self.assertEquals(kwargs['tls'].cert, client.cert)
|
||||||
|
except TypeError, e:
|
||||||
|
self.fail(e)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue