config_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import os
  2. import mock
  3. from .. import unittest
  4. from compose import config
  5. class ConfigTest(unittest.TestCase):
  6. def test_from_dictionary(self):
  7. service_dicts = config.from_dictionary({
  8. 'foo': {'image': 'busybox'},
  9. 'bar': {'environment': ['FOO=1']},
  10. })
  11. self.assertEqual(
  12. sorted(service_dicts, key=lambda d: d['name']),
  13. sorted([
  14. {
  15. 'name': 'bar',
  16. 'environment': {'FOO': '1'},
  17. },
  18. {
  19. 'name': 'foo',
  20. 'image': 'busybox',
  21. }
  22. ])
  23. )
  24. def test_from_dictionary_throws_error_when_not_dict(self):
  25. with self.assertRaises(config.ConfigurationError):
  26. config.from_dictionary({
  27. 'web': 'busybox:latest',
  28. })
  29. def test_config_validation(self):
  30. self.assertRaises(
  31. config.ConfigurationError,
  32. lambda: config.make_service_dict('foo', {'port': ['8000']})
  33. )
  34. config.make_service_dict('foo', {'ports': ['8000']})
  35. def test_parse_environment_as_list(self):
  36. environment =[
  37. 'NORMAL=F1',
  38. 'CONTAINS_EQUALS=F=2',
  39. 'TRAILING_EQUALS=',
  40. ]
  41. self.assertEqual(
  42. config.parse_environment(environment),
  43. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  44. )
  45. def test_parse_environment_as_dict(self):
  46. environment = {
  47. 'NORMAL': 'F1',
  48. 'CONTAINS_EQUALS': 'F=2',
  49. 'TRAILING_EQUALS': None,
  50. }
  51. self.assertEqual(config.parse_environment(environment), environment)
  52. def test_parse_environment_invalid(self):
  53. with self.assertRaises(config.ConfigurationError):
  54. config.parse_environment('a=b')
  55. def test_parse_environment_empty(self):
  56. self.assertEqual(config.parse_environment(None), {})
  57. @mock.patch.dict(os.environ)
  58. def test_resolve_environment(self):
  59. os.environ['FILE_DEF'] = 'E1'
  60. os.environ['FILE_DEF_EMPTY'] = 'E2'
  61. os.environ['ENV_DEF'] = 'E3'
  62. service_dict = config.make_service_dict(
  63. 'foo',
  64. {
  65. 'environment': {
  66. 'FILE_DEF': 'F1',
  67. 'FILE_DEF_EMPTY': '',
  68. 'ENV_DEF': None,
  69. 'NO_DEF': None
  70. },
  71. },
  72. )
  73. self.assertEqual(
  74. service_dict['environment'],
  75. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  76. )
  77. def test_env_from_file(self):
  78. service_dict = config.make_service_dict(
  79. 'foo',
  80. {'env_file': 'tests/fixtures/env/one.env'},
  81. )
  82. self.assertEqual(
  83. service_dict['environment'],
  84. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  85. )
  86. def test_env_from_multiple_files(self):
  87. service_dict = config.make_service_dict(
  88. 'foo',
  89. {
  90. 'env_file': [
  91. 'tests/fixtures/env/one.env',
  92. 'tests/fixtures/env/two.env',
  93. ],
  94. },
  95. )
  96. self.assertEqual(
  97. service_dict['environment'],
  98. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  99. )
  100. def test_env_nonexistent_file(self):
  101. options = {'env_file': 'tests/fixtures/env/nonexistent.env'}
  102. self.assertRaises(
  103. config.ConfigurationError,
  104. lambda: config.make_service_dict('foo', options),
  105. )
  106. @mock.patch.dict(os.environ)
  107. def test_resolve_environment_from_file(self):
  108. os.environ['FILE_DEF'] = 'E1'
  109. os.environ['FILE_DEF_EMPTY'] = 'E2'
  110. os.environ['ENV_DEF'] = 'E3'
  111. service_dict = config.make_service_dict(
  112. 'foo',
  113. {'env_file': 'tests/fixtures/env/resolve.env'},
  114. )
  115. self.assertEqual(
  116. service_dict['environment'],
  117. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  118. )