config_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 VolumePathTest(unittest.TestCase):
  36. @mock.patch.dict(os.environ)
  37. def test_volume_binding_with_environ(self):
  38. os.environ['VOLUME_PATH'] = '/host/path'
  39. d = config.make_service_dict('foo', {'volumes': ['${VOLUME_PATH}:/container/path']}, working_dir='.')
  40. self.assertEqual(d['volumes'], ['/host/path:/container/path'])
  41. @mock.patch.dict(os.environ)
  42. def test_volume_binding_with_home(self):
  43. os.environ['HOME'] = '/home/user'
  44. d = config.make_service_dict('foo', {'volumes': ['~:/container/path']}, working_dir='.')
  45. self.assertEqual(d['volumes'], ['/home/user:/container/path'])
  46. class MergeVolumesTest(unittest.TestCase):
  47. def test_empty(self):
  48. service_dict = config.merge_service_dicts({}, {})
  49. self.assertNotIn('volumes', service_dict)
  50. def test_no_override(self):
  51. service_dict = config.merge_service_dicts(
  52. {'volumes': ['/foo:/code', '/data']},
  53. {},
  54. )
  55. self.assertEqual(set(service_dict['volumes']), set(['/foo:/code', '/data']))
  56. def test_no_base(self):
  57. service_dict = config.merge_service_dicts(
  58. {},
  59. {'volumes': ['/bar:/code']},
  60. )
  61. self.assertEqual(set(service_dict['volumes']), set(['/bar:/code']))
  62. def test_override_explicit_path(self):
  63. service_dict = config.merge_service_dicts(
  64. {'volumes': ['/foo:/code', '/data']},
  65. {'volumes': ['/bar:/code']},
  66. )
  67. self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/data']))
  68. def test_add_explicit_path(self):
  69. service_dict = config.merge_service_dicts(
  70. {'volumes': ['/foo:/code', '/data']},
  71. {'volumes': ['/bar:/code', '/quux:/data']},
  72. )
  73. self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/quux:/data']))
  74. def test_remove_explicit_path(self):
  75. service_dict = config.merge_service_dicts(
  76. {'volumes': ['/foo:/code', '/quux:/data']},
  77. {'volumes': ['/bar:/code', '/data']},
  78. )
  79. self.assertEqual(set(service_dict['volumes']), set(['/bar:/code', '/data']))
  80. def test_merge_build_or_image_no_override(self):
  81. self.assertEqual(
  82. config.merge_service_dicts({'build': '.'}, {}),
  83. {'build': '.'},
  84. )
  85. self.assertEqual(
  86. config.merge_service_dicts({'image': 'redis'}, {}),
  87. {'image': 'redis'},
  88. )
  89. def test_merge_build_or_image_override_with_same(self):
  90. self.assertEqual(
  91. config.merge_service_dicts({'build': '.'}, {'build': './web'}),
  92. {'build': './web'},
  93. )
  94. self.assertEqual(
  95. config.merge_service_dicts({'image': 'redis'}, {'image': 'postgres'}),
  96. {'image': 'postgres'},
  97. )
  98. def test_merge_build_or_image_override_with_other(self):
  99. self.assertEqual(
  100. config.merge_service_dicts({'build': '.'}, {'image': 'redis'}),
  101. {'image': 'redis'}
  102. )
  103. self.assertEqual(
  104. config.merge_service_dicts({'image': 'redis'}, {'build': '.'}),
  105. {'build': '.'}
  106. )
  107. class MergeListsTest(unittest.TestCase):
  108. def test_empty(self):
  109. service_dict = config.merge_service_dicts({}, {})
  110. self.assertNotIn('ports', service_dict)
  111. def test_no_override(self):
  112. service_dict = config.merge_service_dicts(
  113. {'ports': ['10:8000', '9000']},
  114. {},
  115. )
  116. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
  117. def test_no_base(self):
  118. service_dict = config.merge_service_dicts(
  119. {},
  120. {'ports': ['10:8000', '9000']},
  121. )
  122. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
  123. def test_add_item(self):
  124. service_dict = config.merge_service_dicts(
  125. {'ports': ['10:8000', '9000']},
  126. {'ports': ['20:8000']},
  127. )
  128. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000', '20:8000']))
  129. class MergeStringsOrListsTest(unittest.TestCase):
  130. def test_no_override(self):
  131. service_dict = config.merge_service_dicts(
  132. {'dns': '8.8.8.8'},
  133. {},
  134. )
  135. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
  136. def test_no_base(self):
  137. service_dict = config.merge_service_dicts(
  138. {},
  139. {'dns': '8.8.8.8'},
  140. )
  141. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
  142. def test_add_string(self):
  143. service_dict = config.merge_service_dicts(
  144. {'dns': ['8.8.8.8']},
  145. {'dns': '9.9.9.9'},
  146. )
  147. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
  148. def test_add_list(self):
  149. service_dict = config.merge_service_dicts(
  150. {'dns': '8.8.8.8'},
  151. {'dns': ['9.9.9.9']},
  152. )
  153. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
  154. class EnvTest(unittest.TestCase):
  155. def test_parse_environment_as_list(self):
  156. environment = [
  157. 'NORMAL=F1',
  158. 'CONTAINS_EQUALS=F=2',
  159. 'TRAILING_EQUALS=',
  160. ]
  161. self.assertEqual(
  162. config.parse_environment(environment),
  163. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  164. )
  165. def test_parse_environment_as_dict(self):
  166. environment = {
  167. 'NORMAL': 'F1',
  168. 'CONTAINS_EQUALS': 'F=2',
  169. 'TRAILING_EQUALS': None,
  170. }
  171. self.assertEqual(config.parse_environment(environment), environment)
  172. def test_parse_environment_invalid(self):
  173. with self.assertRaises(config.ConfigurationError):
  174. config.parse_environment('a=b')
  175. def test_parse_environment_empty(self):
  176. self.assertEqual(config.parse_environment(None), {})
  177. @mock.patch.dict(os.environ)
  178. def test_resolve_environment(self):
  179. os.environ['FILE_DEF'] = 'E1'
  180. os.environ['FILE_DEF_EMPTY'] = 'E2'
  181. os.environ['ENV_DEF'] = 'E3'
  182. service_dict = config.make_service_dict(
  183. 'foo', {
  184. 'environment': {
  185. 'FILE_DEF': 'F1',
  186. 'FILE_DEF_EMPTY': '',
  187. 'ENV_DEF': None,
  188. 'NO_DEF': None
  189. },
  190. },
  191. )
  192. self.assertEqual(
  193. service_dict['environment'],
  194. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  195. )
  196. def test_env_from_file(self):
  197. service_dict = config.make_service_dict(
  198. 'foo',
  199. {'env_file': 'one.env'},
  200. 'tests/fixtures/env',
  201. )
  202. self.assertEqual(
  203. service_dict['environment'],
  204. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  205. )
  206. def test_env_from_multiple_files(self):
  207. service_dict = config.make_service_dict(
  208. 'foo',
  209. {'env_file': ['one.env', 'two.env']},
  210. 'tests/fixtures/env',
  211. )
  212. self.assertEqual(
  213. service_dict['environment'],
  214. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  215. )
  216. def test_env_nonexistent_file(self):
  217. options = {'env_file': 'nonexistent.env'}
  218. self.assertRaises(
  219. config.ConfigurationError,
  220. lambda: config.make_service_dict('foo', options, 'tests/fixtures/env'),
  221. )
  222. @mock.patch.dict(os.environ)
  223. def test_resolve_environment_from_file(self):
  224. os.environ['FILE_DEF'] = 'E1'
  225. os.environ['FILE_DEF_EMPTY'] = 'E2'
  226. os.environ['ENV_DEF'] = 'E3'
  227. service_dict = config.make_service_dict(
  228. 'foo',
  229. {'env_file': 'resolve.env'},
  230. 'tests/fixtures/env',
  231. )
  232. self.assertEqual(
  233. service_dict['environment'],
  234. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  235. )
  236. class ExtendsTest(unittest.TestCase):
  237. def test_extends(self):
  238. service_dicts = config.load('tests/fixtures/extends/docker-compose.yml')
  239. service_dicts = sorted(
  240. service_dicts,
  241. key=lambda sd: sd['name'],
  242. )
  243. self.assertEqual(service_dicts, [
  244. {
  245. 'name': 'mydb',
  246. 'image': 'busybox',
  247. 'command': 'sleep 300',
  248. },
  249. {
  250. 'name': 'myweb',
  251. 'image': 'busybox',
  252. 'command': 'sleep 300',
  253. 'links': ['mydb:db'],
  254. 'environment': {
  255. "FOO": "1",
  256. "BAR": "2",
  257. "BAZ": "2",
  258. },
  259. }
  260. ])
  261. def test_nested(self):
  262. service_dicts = config.load('tests/fixtures/extends/nested.yml')
  263. self.assertEqual(service_dicts, [
  264. {
  265. 'name': 'myweb',
  266. 'image': 'busybox',
  267. 'command': '/bin/true',
  268. 'environment': {
  269. "FOO": "2",
  270. "BAR": "2",
  271. },
  272. },
  273. ])
  274. def test_circular(self):
  275. try:
  276. config.load('tests/fixtures/extends/circle-1.yml')
  277. raise Exception("Expected config.CircularReference to be raised")
  278. except config.CircularReference as e:
  279. self.assertEqual(
  280. [(os.path.basename(filename), service_name) for (filename, service_name) in e.trail],
  281. [
  282. ('circle-1.yml', 'web'),
  283. ('circle-2.yml', 'web'),
  284. ('circle-1.yml', 'web'),
  285. ],
  286. )
  287. def test_extends_validation(self):
  288. dictionary = {'extends': None}
  289. def load_config():
  290. return config.make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  291. self.assertRaisesRegexp(config.ConfigurationError, 'dictionary', load_config)
  292. dictionary['extends'] = {}
  293. self.assertRaises(config.ConfigurationError, load_config)
  294. dictionary['extends']['file'] = 'common.yml'
  295. self.assertRaisesRegexp(config.ConfigurationError, 'service', load_config)
  296. dictionary['extends']['service'] = 'web'
  297. self.assertIsInstance(load_config(), dict)
  298. dictionary['extends']['what'] = 'is this'
  299. self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config)
  300. def test_blacklisted_options(self):
  301. def load_config():
  302. return config.make_service_dict('myweb', {
  303. 'extends': {
  304. 'file': 'whatever',
  305. 'service': 'web',
  306. }
  307. }, '.')
  308. with self.assertRaisesRegexp(config.ConfigurationError, 'links'):
  309. other_config = {'web': {'links': ['db']}}
  310. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  311. print load_config()
  312. with self.assertRaisesRegexp(config.ConfigurationError, 'volumes_from'):
  313. other_config = {'web': {'volumes_from': ['db']}}
  314. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  315. print load_config()
  316. with self.assertRaisesRegexp(config.ConfigurationError, 'net'):
  317. other_config = {'web': {'net': 'container:db'}}
  318. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  319. print load_config()
  320. other_config = {'web': {'net': 'host'}}
  321. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  322. print load_config()
  323. def test_volume_path(self):
  324. dicts = config.load('tests/fixtures/volume-path/docker-compose.yml')
  325. paths = [
  326. '%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
  327. '%s:/bar' % os.path.abspath('tests/fixtures/volume-path/bar'),
  328. ]
  329. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  330. class BuildPathTest(unittest.TestCase):
  331. def setUp(self):
  332. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  333. def test_nonexistent_path(self):
  334. options = {'build': 'nonexistent.path'}
  335. self.assertRaises(
  336. config.ConfigurationError,
  337. lambda: config.make_service_dict('foo', options, 'tests/fixtures/build-path'),
  338. )
  339. def test_relative_path(self):
  340. relative_build_path = '../build-ctx/'
  341. service_dict = config.make_service_dict(
  342. 'relpath',
  343. {'build': relative_build_path},
  344. working_dir='tests/fixtures/build-path'
  345. )
  346. self.assertEquals(service_dict['build'], self.abs_context_path)
  347. def test_absolute_path(self):
  348. service_dict = config.make_service_dict(
  349. 'abspath',
  350. {'build': self.abs_context_path},
  351. working_dir='tests/fixtures/build-path'
  352. )
  353. self.assertEquals(service_dict['build'], self.abs_context_path)
  354. def test_from_file(self):
  355. service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
  356. self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])