docker_client_test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. class TLSConfigTestCase(unittest.TestCase):
  50. ca_cert = 'tests/fixtures/tls/ca.pem'
  51. client_cert = 'tests/fixtures/tls/cert.pem'
  52. key = 'tests/fixtures/tls/key.key'
  53. def test_simple_tls(self):
  54. options = {'--tls': True}
  55. result = tls_config_from_options(options)
  56. assert result is True
  57. def test_tls_ca_cert(self):
  58. options = {
  59. '--tlscacert': self.ca_cert, '--tlsverify': True
  60. }
  61. result = tls_config_from_options(options)
  62. assert isinstance(result, docker.tls.TLSConfig)
  63. assert result.ca_cert == options['--tlscacert']
  64. assert result.verify is True
  65. def test_tls_ca_cert_explicit(self):
  66. options = {
  67. '--tlscacert': self.ca_cert, '--tls': True,
  68. '--tlsverify': True
  69. }
  70. result = tls_config_from_options(options)
  71. assert isinstance(result, docker.tls.TLSConfig)
  72. assert result.ca_cert == options['--tlscacert']
  73. assert result.verify is True
  74. def test_tls_client_cert(self):
  75. options = {
  76. '--tlscert': self.client_cert, '--tlskey': self.key
  77. }
  78. result = tls_config_from_options(options)
  79. assert isinstance(result, docker.tls.TLSConfig)
  80. assert result.cert == (options['--tlscert'], options['--tlskey'])
  81. def test_tls_client_cert_explicit(self):
  82. options = {
  83. '--tlscert': self.client_cert, '--tlskey': self.key,
  84. '--tls': True
  85. }
  86. result = tls_config_from_options(options)
  87. assert isinstance(result, docker.tls.TLSConfig)
  88. assert result.cert == (options['--tlscert'], options['--tlskey'])
  89. def test_tls_client_and_ca(self):
  90. options = {
  91. '--tlscert': self.client_cert, '--tlskey': self.key,
  92. '--tlsverify': True, '--tlscacert': self.ca_cert
  93. }
  94. result = tls_config_from_options(options)
  95. assert isinstance(result, docker.tls.TLSConfig)
  96. assert result.cert == (options['--tlscert'], options['--tlskey'])
  97. assert result.ca_cert == options['--tlscacert']
  98. assert result.verify is True
  99. def test_tls_client_and_ca_explicit(self):
  100. options = {
  101. '--tlscert': self.client_cert, '--tlskey': self.key,
  102. '--tlsverify': True, '--tlscacert': self.ca_cert,
  103. '--tls': True
  104. }
  105. result = tls_config_from_options(options)
  106. assert isinstance(result, docker.tls.TLSConfig)
  107. assert result.cert == (options['--tlscert'], options['--tlskey'])
  108. assert result.ca_cert == options['--tlscacert']
  109. assert result.verify is True
  110. def test_tls_client_missing_key(self):
  111. options = {'--tlscert': self.client_cert}
  112. with pytest.raises(docker.errors.TLSParameterError):
  113. tls_config_from_options(options)
  114. options = {'--tlskey': self.key}
  115. with pytest.raises(docker.errors.TLSParameterError):
  116. tls_config_from_options(options)
  117. def test_assert_hostname_explicit_skip(self):
  118. options = {'--tlscacert': self.ca_cert, '--skip-hostname-check': True}
  119. result = tls_config_from_options(options)
  120. assert isinstance(result, docker.tls.TLSConfig)
  121. assert result.assert_hostname is False
  122. def test_tls_client_and_ca_quoted_paths(self):
  123. options = {
  124. '--tlscacert': '"{0}"'.format(self.ca_cert),
  125. '--tlscert': '"{0}"'.format(self.client_cert),
  126. '--tlskey': '"{0}"'.format(self.key),
  127. '--tlsverify': True
  128. }
  129. result = tls_config_from_options(options)
  130. assert isinstance(result, docker.tls.TLSConfig)
  131. assert result.cert == (self.client_cert, self.key)
  132. assert result.ca_cert == self.ca_cert
  133. assert result.verify is True