docker_client_test.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import os
  4. import platform
  5. import docker
  6. import pytest
  7. import compose
  8. from compose.cli import errors
  9. from compose.cli.docker_client import docker_client
  10. from compose.cli.docker_client import tls_config_from_options
  11. from tests import mock
  12. from tests import unittest
  13. class DockerClientTestCase(unittest.TestCase):
  14. def test_docker_client_no_home(self):
  15. with mock.patch.dict(os.environ):
  16. del os.environ['HOME']
  17. docker_client(os.environ)
  18. @mock.patch.dict(os.environ)
  19. def test_docker_client_with_custom_timeout(self):
  20. os.environ['COMPOSE_HTTP_TIMEOUT'] = '123'
  21. client = docker_client(os.environ)
  22. assert client.timeout == 123
  23. @mock.patch.dict(os.environ)
  24. def test_custom_timeout_error(self):
  25. os.environ['COMPOSE_HTTP_TIMEOUT'] = '123'
  26. client = docker_client(os.environ)
  27. with mock.patch('compose.cli.errors.log') as fake_log:
  28. with pytest.raises(errors.ConnectionError):
  29. with errors.handle_connection_errors(client):
  30. raise errors.RequestsConnectionError(
  31. errors.ReadTimeoutError(None, None, None))
  32. assert fake_log.error.call_count == 1
  33. assert '123' in fake_log.error.call_args[0][0]
  34. with mock.patch('compose.cli.errors.log') as fake_log:
  35. with pytest.raises(errors.ConnectionError):
  36. with errors.handle_connection_errors(client):
  37. raise errors.ReadTimeout()
  38. assert fake_log.error.call_count == 1
  39. assert '123' in fake_log.error.call_args[0][0]
  40. def test_user_agent(self):
  41. client = docker_client(os.environ)
  42. expected = "docker-compose/{0} docker-py/{1} {2}/{3}".format(
  43. compose.__version__,
  44. docker.__version__,
  45. platform.system(),
  46. platform.release()
  47. )
  48. self.assertEqual(client.headers['User-Agent'], expected)
  49. @mock.patch.dict(os.environ)
  50. def test_docker_client_default_windows_host(self):
  51. with mock.patch('compose.cli.docker_client.IS_WINDOWS_PLATFORM', True):
  52. if 'DOCKER_HOST' in os.environ:
  53. del os.environ['DOCKER_HOST']
  54. client = docker_client(os.environ)
  55. assert client.base_url == 'http://127.0.0.1:2375'
  56. class TLSConfigTestCase(unittest.TestCase):
  57. ca_cert = 'tests/fixtures/tls/ca.pem'
  58. client_cert = 'tests/fixtures/tls/cert.pem'
  59. key = 'tests/fixtures/tls/key.key'
  60. def test_simple_tls(self):
  61. options = {'--tls': True}
  62. result = tls_config_from_options(options)
  63. assert result is True
  64. def test_tls_ca_cert(self):
  65. options = {
  66. '--tlscacert': self.ca_cert, '--tlsverify': True
  67. }
  68. result = tls_config_from_options(options)
  69. assert isinstance(result, docker.tls.TLSConfig)
  70. assert result.ca_cert == options['--tlscacert']
  71. assert result.verify is True
  72. def test_tls_ca_cert_explicit(self):
  73. options = {
  74. '--tlscacert': self.ca_cert, '--tls': True,
  75. '--tlsverify': True
  76. }
  77. result = tls_config_from_options(options)
  78. assert isinstance(result, docker.tls.TLSConfig)
  79. assert result.ca_cert == options['--tlscacert']
  80. assert result.verify is True
  81. def test_tls_client_cert(self):
  82. options = {
  83. '--tlscert': self.client_cert, '--tlskey': self.key
  84. }
  85. result = tls_config_from_options(options)
  86. assert isinstance(result, docker.tls.TLSConfig)
  87. assert result.cert == (options['--tlscert'], options['--tlskey'])
  88. def test_tls_client_cert_explicit(self):
  89. options = {
  90. '--tlscert': self.client_cert, '--tlskey': self.key,
  91. '--tls': True
  92. }
  93. result = tls_config_from_options(options)
  94. assert isinstance(result, docker.tls.TLSConfig)
  95. assert result.cert == (options['--tlscert'], options['--tlskey'])
  96. def test_tls_client_and_ca(self):
  97. options = {
  98. '--tlscert': self.client_cert, '--tlskey': self.key,
  99. '--tlsverify': True, '--tlscacert': self.ca_cert
  100. }
  101. result = tls_config_from_options(options)
  102. assert isinstance(result, docker.tls.TLSConfig)
  103. assert result.cert == (options['--tlscert'], options['--tlskey'])
  104. assert result.ca_cert == options['--tlscacert']
  105. assert result.verify is True
  106. def test_tls_client_and_ca_explicit(self):
  107. options = {
  108. '--tlscert': self.client_cert, '--tlskey': self.key,
  109. '--tlsverify': True, '--tlscacert': self.ca_cert,
  110. '--tls': True
  111. }
  112. result = tls_config_from_options(options)
  113. assert isinstance(result, docker.tls.TLSConfig)
  114. assert result.cert == (options['--tlscert'], options['--tlskey'])
  115. assert result.ca_cert == options['--tlscacert']
  116. assert result.verify is True
  117. def test_tls_client_missing_key(self):
  118. options = {'--tlscert': self.client_cert}
  119. with pytest.raises(docker.errors.TLSParameterError):
  120. tls_config_from_options(options)
  121. options = {'--tlskey': self.key}
  122. with pytest.raises(docker.errors.TLSParameterError):
  123. tls_config_from_options(options)
  124. def test_assert_hostname_explicit_skip(self):
  125. options = {'--tlscacert': self.ca_cert, '--skip-hostname-check': True}
  126. result = tls_config_from_options(options)
  127. assert isinstance(result, docker.tls.TLSConfig)
  128. assert result.assert_hostname is False
  129. def test_tls_client_and_ca_quoted_paths(self):
  130. options = {
  131. '--tlscacert': '"{0}"'.format(self.ca_cert),
  132. '--tlscert': '"{0}"'.format(self.client_cert),
  133. '--tlskey': '"{0}"'.format(self.key),
  134. '--tlsverify': True
  135. }
  136. result = tls_config_from_options(options)
  137. assert isinstance(result, docker.tls.TLSConfig)
  138. assert result.cert == (self.client_cert, self.key)
  139. assert result.ca_cert == self.ca_cert
  140. assert result.verify is True