tls_config_from_options unit tests

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-03-21 14:23:31 -07:00
parent 4727115317
commit 2cc87555cb
5 changed files with 87 additions and 4 deletions

View File

@ -22,7 +22,7 @@ def tls_config_from_options(options):
cert = options.get('--tlscert') cert = options.get('--tlscert')
key = options.get('--tlskey') key = options.get('--tlskey')
verify = options.get('--tlsverify') verify = options.get('--tlsverify')
hostname = urlparse(options.get('--host', '')).hostname hostname = urlparse(options.get('--host') or '').hostname
advanced_opts = any([ca_cert, cert, key, verify]) advanced_opts = any([ca_cert, cert, key, verify])

0
tests/fixtures/tls/ca.pem vendored Normal file
View File

0
tests/fixtures/tls/cert.pem vendored Normal file
View File

0
tests/fixtures/tls/key.key vendored Normal file
View File

View File

@ -3,7 +3,11 @@ from __future__ import unicode_literals
import os import os
from compose.cli import docker_client import docker
import pytest
from compose.cli.docker_client import docker_client
from compose.cli.docker_client import tls_config_from_options
from tests import mock from tests import mock
from tests import unittest from tests import unittest
@ -13,10 +17,89 @@ class DockerClientTestCase(unittest.TestCase):
def test_docker_client_no_home(self): def test_docker_client_no_home(self):
with mock.patch.dict(os.environ): with mock.patch.dict(os.environ):
del os.environ['HOME'] del os.environ['HOME']
docker_client.docker_client() docker_client()
def test_docker_client_with_custom_timeout(self): def test_docker_client_with_custom_timeout(self):
timeout = 300 timeout = 300
with mock.patch('compose.cli.docker_client.HTTP_TIMEOUT', 300): with mock.patch('compose.cli.docker_client.HTTP_TIMEOUT', 300):
client = docker_client.docker_client() client = docker_client()
self.assertEqual(client.timeout, int(timeout)) self.assertEqual(client.timeout, int(timeout))
class TLSConfigTestCase(unittest.TestCase):
ca_cert = 'tests/fixtures/tls/ca.pem'
client_cert = 'tests/fixtures/tls/cert.pem'
key = 'tests/fixtures/tls/key.key'
def test_simple_tls(self):
options = {'--tls': True}
result = tls_config_from_options(options)
assert result is True
def test_tls_ca_cert(self):
options = {
'--tlscacert': self.ca_cert, '--tlsverify': True
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.ca_cert == options['--tlscacert']
assert result.verify is True
def test_tls_ca_cert_explicit(self):
options = {
'--tlscacert': self.ca_cert, '--tls': True,
'--tlsverify': True
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.ca_cert == options['--tlscacert']
assert result.verify is True
def test_tls_client_cert(self):
options = {
'--tlscert': self.client_cert, '--tlskey': self.key
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.cert == (options['--tlscert'], options['--tlskey'])
def test_tls_client_cert_explicit(self):
options = {
'--tlscert': self.client_cert, '--tlskey': self.key,
'--tls': True
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.cert == (options['--tlscert'], options['--tlskey'])
def test_tls_client_and_ca(self):
options = {
'--tlscert': self.client_cert, '--tlskey': self.key,
'--tlsverify': True, '--tlscacert': self.ca_cert
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.cert == (options['--tlscert'], options['--tlskey'])
assert result.ca_cert == options['--tlscacert']
assert result.verify is True
def test_tls_client_and_ca_explicit(self):
options = {
'--tlscert': self.client_cert, '--tlskey': self.key,
'--tlsverify': True, '--tlscacert': self.ca_cert,
'--tls': True
}
result = tls_config_from_options(options)
assert isinstance(result, docker.tls.TLSConfig)
assert result.cert == (options['--tlscert'], options['--tlskey'])
assert result.ca_cert == options['--tlscacert']
assert result.verify is True
def test_tls_client_missing_key(self):
options = {'--tlscert': self.client_cert}
with pytest.raises(docker.errors.TLSParameterError):
tls_config_from_options(options)
options = {'--tlskey': self.key}
with pytest.raises(docker.errors.TLSParameterError):
tls_config_from_options(options)