config_test.py 11 KB

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