command_test.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import os
  4. import ssl
  5. import pytest
  6. from compose.cli.command import get_config_path_from_options
  7. from compose.cli.command import get_tls_version
  8. from compose.config.environment import Environment
  9. from compose.const import IS_WINDOWS_PLATFORM
  10. from tests import mock
  11. class TestGetConfigPathFromOptions(object):
  12. def test_path_from_options(self):
  13. paths = ['one.yml', 'two.yml']
  14. opts = {'--file': paths}
  15. environment = Environment.from_env_file('.')
  16. assert get_config_path_from_options('.', opts, environment) == paths
  17. def test_single_path_from_env(self):
  18. with mock.patch.dict(os.environ):
  19. os.environ['COMPOSE_FILE'] = 'one.yml'
  20. environment = Environment.from_env_file('.')
  21. assert get_config_path_from_options('.', {}, environment) == ['one.yml']
  22. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix separator')
  23. def test_multiple_path_from_env(self):
  24. with mock.patch.dict(os.environ):
  25. os.environ['COMPOSE_FILE'] = 'one.yml:two.yml'
  26. environment = Environment.from_env_file('.')
  27. assert get_config_path_from_options(
  28. '.', {}, environment
  29. ) == ['one.yml', 'two.yml']
  30. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows separator')
  31. def test_multiple_path_from_env_windows(self):
  32. with mock.patch.dict(os.environ):
  33. os.environ['COMPOSE_FILE'] = 'one.yml;two.yml'
  34. environment = Environment.from_env_file('.')
  35. assert get_config_path_from_options(
  36. '.', {}, environment
  37. ) == ['one.yml', 'two.yml']
  38. def test_multiple_path_from_env_custom_separator(self):
  39. with mock.patch.dict(os.environ):
  40. os.environ['COMPOSE_PATH_SEPARATOR'] = '^'
  41. os.environ['COMPOSE_FILE'] = 'c:\\one.yml^.\\semi;colon.yml'
  42. environment = Environment.from_env_file('.')
  43. assert get_config_path_from_options(
  44. '.', {}, environment
  45. ) == ['c:\\one.yml', '.\\semi;colon.yml']
  46. def test_no_path(self):
  47. environment = Environment.from_env_file('.')
  48. assert not get_config_path_from_options('.', {}, environment)
  49. class TestGetTlsVersion(object):
  50. def test_get_tls_version_default(self):
  51. environment = {}
  52. assert get_tls_version(environment) is None
  53. @pytest.mark.skipif(not hasattr(ssl, 'PROTOCOL_TLSv1_2'), reason='TLS v1.2 unsupported')
  54. def test_get_tls_version_upgrade(self):
  55. environment = {'COMPOSE_TLS_VERSION': 'TLSv1_2'}
  56. assert get_tls_version(environment) == ssl.PROTOCOL_TLSv1_2
  57. def test_get_tls_version_unavailable(self):
  58. environment = {'COMPOSE_TLS_VERSION': 'TLSv5_5'}
  59. with mock.patch('compose.cli.command.log') as mock_log:
  60. tls_version = get_tls_version(environment)
  61. mock_log.warn.assert_called_once_with(mock.ANY)
  62. assert tls_version is None