config_test.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. class EnvTest(unittest.TestCase):
  36. def test_parse_environment_as_list(self):
  37. environment =[
  38. 'NORMAL=F1',
  39. 'CONTAINS_EQUALS=F=2',
  40. 'TRAILING_EQUALS=',
  41. ]
  42. self.assertEqual(
  43. config.parse_environment(environment),
  44. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  45. )
  46. def test_parse_environment_as_dict(self):
  47. environment = {
  48. 'NORMAL': 'F1',
  49. 'CONTAINS_EQUALS': 'F=2',
  50. 'TRAILING_EQUALS': None,
  51. }
  52. self.assertEqual(config.parse_environment(environment), environment)
  53. def test_parse_environment_invalid(self):
  54. with self.assertRaises(config.ConfigurationError):
  55. config.parse_environment('a=b')
  56. def test_parse_environment_empty(self):
  57. self.assertEqual(config.parse_environment(None), {})
  58. @mock.patch.dict(os.environ)
  59. def test_resolve_environment(self):
  60. os.environ['FILE_DEF'] = 'E1'
  61. os.environ['FILE_DEF_EMPTY'] = 'E2'
  62. os.environ['ENV_DEF'] = 'E3'
  63. service_dict = config.make_service_dict(
  64. 'foo',
  65. {
  66. 'environment': {
  67. 'FILE_DEF': 'F1',
  68. 'FILE_DEF_EMPTY': '',
  69. 'ENV_DEF': None,
  70. 'NO_DEF': None
  71. },
  72. },
  73. )
  74. self.assertEqual(
  75. service_dict['environment'],
  76. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  77. )
  78. def test_env_from_file(self):
  79. service_dict = config.make_service_dict(
  80. 'foo',
  81. {'env_file': 'one.env'},
  82. 'tests/fixtures/env',
  83. )
  84. self.assertEqual(
  85. service_dict['environment'],
  86. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  87. )
  88. def test_env_from_multiple_files(self):
  89. service_dict = config.make_service_dict(
  90. 'foo',
  91. {'env_file': ['one.env', 'two.env']},
  92. 'tests/fixtures/env',
  93. )
  94. self.assertEqual(
  95. service_dict['environment'],
  96. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  97. )
  98. def test_env_nonexistent_file(self):
  99. options = {'env_file': 'nonexistent.env'}
  100. self.assertRaises(
  101. config.ConfigurationError,
  102. lambda: config.make_service_dict('foo', options, 'tests/fixtures/env'),
  103. )
  104. @mock.patch.dict(os.environ)
  105. def test_resolve_environment_from_file(self):
  106. os.environ['FILE_DEF'] = 'E1'
  107. os.environ['FILE_DEF_EMPTY'] = 'E2'
  108. os.environ['ENV_DEF'] = 'E3'
  109. service_dict = config.make_service_dict(
  110. 'foo',
  111. {'env_file': 'resolve.env'},
  112. 'tests/fixtures/env',
  113. )
  114. self.assertEqual(
  115. service_dict['environment'],
  116. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  117. )
  118. class ExtendsTest(unittest.TestCase):
  119. def test_extends(self):
  120. service_dicts = config.load('tests/fixtures/extends/docker-compose.yml')
  121. service_dicts = sorted(
  122. service_dicts,
  123. key=lambda sd: sd['name'],
  124. )
  125. self.assertEqual(service_dicts, [
  126. {
  127. 'name': 'mydb',
  128. 'image': 'busybox',
  129. 'command': 'sleep 300',
  130. },
  131. {
  132. 'name': 'myweb',
  133. 'image': 'busybox',
  134. 'command': 'sleep 300',
  135. 'links': ['mydb:db'],
  136. 'environment': {
  137. "FOO": "1",
  138. "BAR": "2",
  139. "BAZ": "2",
  140. },
  141. }
  142. ])
  143. def test_nested(self):
  144. service_dicts = config.load('tests/fixtures/extends/nested.yml')
  145. self.assertEqual(service_dicts, [
  146. {
  147. 'name': 'myweb',
  148. 'image': 'busybox',
  149. 'command': '/bin/true',
  150. 'environment': {
  151. "FOO": "2",
  152. "BAR": "2",
  153. },
  154. },
  155. ])
  156. def test_circular(self):
  157. try:
  158. config.load('tests/fixtures/extends/circle-1.yml')
  159. raise Exception("Expected config.CircularReference to be raised")
  160. except config.CircularReference as e:
  161. self.assertEqual(
  162. [(os.path.basename(filename), service_name) for (filename, service_name) in e.trail],
  163. [
  164. ('circle-1.yml', 'web'),
  165. ('circle-2.yml', 'web'),
  166. ('circle-1.yml', 'web'),
  167. ],
  168. )
  169. def test_extends_validation(self):
  170. dictionary = {'extends': None}
  171. load_config = lambda: config.make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  172. self.assertRaisesRegexp(config.ConfigurationError, 'dictionary', load_config)
  173. dictionary['extends'] = {}
  174. self.assertRaises(config.ConfigurationError, load_config)
  175. dictionary['extends']['file'] = 'common.yml'
  176. self.assertRaisesRegexp(config.ConfigurationError, 'service', load_config)
  177. dictionary['extends']['service'] = 'web'
  178. self.assertIsInstance(load_config(), dict)
  179. dictionary['extends']['what'] = 'is this'
  180. self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config)
  181. def test_blacklisted_options(self):
  182. def load_config():
  183. return config.make_service_dict('myweb', {
  184. 'extends': {
  185. 'file': 'whatever',
  186. 'service': 'web',
  187. }
  188. }, '.')
  189. with self.assertRaisesRegexp(config.ConfigurationError, 'links'):
  190. other_config = {'web': {'links': ['db']}}
  191. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  192. print load_config()
  193. with self.assertRaisesRegexp(config.ConfigurationError, 'volumes_from'):
  194. other_config = {'web': {'volumes_from': ['db']}}
  195. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  196. print load_config()
  197. with self.assertRaisesRegexp(config.ConfigurationError, 'net'):
  198. other_config = {'web': {'net': 'container:db'}}
  199. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  200. print load_config()
  201. other_config = {'web': {'net': 'host'}}
  202. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  203. print load_config()