config_test.py 9.5 KB

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