mirror of https://github.com/docker/docker-py.git
Merge branch 'default-cert-path' of https://github.com/aanand/docker-py into aanand-default-cert-path
Conflicts: tests/utils_test.py
This commit is contained in:
commit
c56c9faacd
|
@ -363,9 +363,15 @@ def kwargs_from_env(ssl_version=None, assert_hostname=None):
|
||||||
tls_verify = os.environ.get('DOCKER_TLS_VERIFY')
|
tls_verify = os.environ.get('DOCKER_TLS_VERIFY')
|
||||||
|
|
||||||
params = {}
|
params = {}
|
||||||
|
|
||||||
if host:
|
if host:
|
||||||
params['base_url'] = (host.replace('tcp://', 'https://')
|
params['base_url'] = (host.replace('tcp://', 'https://')
|
||||||
if tls_verify else host)
|
if tls_verify else host)
|
||||||
|
|
||||||
|
if tls_verify and not cert_path:
|
||||||
|
if 'HOME' in os.environ:
|
||||||
|
cert_path = os.path.join(os.environ['HOME'], '.docker')
|
||||||
|
|
||||||
if tls_verify and cert_path:
|
if tls_verify and cert_path:
|
||||||
params['tls'] = tls.TLSConfig(
|
params['tls'] = tls.TLSConfig(
|
||||||
client_cert=(os.path.join(cert_path, 'cert.pem'),
|
client_cert=(os.path.join(cert_path, 'cert.pem'),
|
||||||
|
@ -374,6 +380,7 @@ def kwargs_from_env(ssl_version=None, assert_hostname=None):
|
||||||
verify=True,
|
verify=True,
|
||||||
ssl_version=ssl_version,
|
ssl_version=ssl_version,
|
||||||
assert_hostname=assert_hostname)
|
assert_hostname=assert_hostname)
|
||||||
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,11 @@ from .helpers import make_tree
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
TEST_CERT_DIR = os.path.join(
|
||||||
|
os.path.dirname(__file__),
|
||||||
|
'testdata/certs',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class UtilsTest(base.BaseTestCase):
|
class UtilsTest(base.BaseTestCase):
|
||||||
longMessage = True
|
longMessage = True
|
||||||
|
@ -90,11 +95,18 @@ class UtilsTest(base.BaseTestCase):
|
||||||
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):
|
def test_kwargs_from_env_empty(self):
|
||||||
|
os.environ.update(DOCKER_HOST='',
|
||||||
|
DOCKER_CERT_PATH='',
|
||||||
|
DOCKER_TLS_VERIFY='')
|
||||||
|
|
||||||
|
kwargs = kwargs_from_env()
|
||||||
|
self.assertEqual(None, kwargs.get('base_url'))
|
||||||
|
self.assertEqual(None, kwargs.get('tls'))
|
||||||
|
|
||||||
|
def test_kwargs_from_env_tls(self):
|
||||||
os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
|
os.environ.update(DOCKER_HOST='tcp://192.168.59.103:2376',
|
||||||
DOCKER_CERT_PATH=os.path.join(
|
DOCKER_CERT_PATH=TEST_CERT_DIR,
|
||||||
os.path.dirname(__file__),
|
|
||||||
'testdata/certs'),
|
|
||||||
DOCKER_TLS_VERIFY='1')
|
DOCKER_TLS_VERIFY='1')
|
||||||
kwargs = kwargs_from_env(assert_hostname=False)
|
kwargs = kwargs_from_env(assert_hostname=False)
|
||||||
self.assertEqual('https://192.168.59.103:2376', kwargs['base_url'])
|
self.assertEqual('https://192.168.59.103:2376', kwargs['base_url'])
|
||||||
|
@ -110,6 +122,24 @@ class UtilsTest(base.BaseTestCase):
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
self.fail(e)
|
self.fail(e)
|
||||||
|
|
||||||
|
def test_kwargs_from_env_no_cert_path(self):
|
||||||
|
try:
|
||||||
|
temp_dir = tempfile.mkdtemp()
|
||||||
|
cert_dir = os.path.join(temp_dir, '.docker')
|
||||||
|
shutil.copytree(TEST_CERT_DIR, cert_dir)
|
||||||
|
|
||||||
|
os.environ.update(HOME=temp_dir,
|
||||||
|
DOCKER_CERT_PATH='',
|
||||||
|
DOCKER_TLS_VERIFY='1')
|
||||||
|
|
||||||
|
kwargs = kwargs_from_env()
|
||||||
|
self.assertIn(cert_dir, kwargs['tls'].verify)
|
||||||
|
self.assertIn(cert_dir, kwargs['tls'].cert[0])
|
||||||
|
self.assertIn(cert_dir, kwargs['tls'].cert[1])
|
||||||
|
finally:
|
||||||
|
if temp_dir:
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
def test_parse_env_file_proper(self):
|
def test_parse_env_file_proper(self):
|
||||||
env_file = self.generate_tempfile(
|
env_file = self.generate_tempfile(
|
||||||
file_content='USER=jdoe\nPASS=secret')
|
file_content='USER=jdoe\nPASS=secret')
|
||||||
|
|
Loading…
Reference in New Issue