config_test.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 MergeLabelsTest(unittest.TestCase):
  155. def test_empty(self):
  156. service_dict = config.merge_service_dicts({}, {})
  157. self.assertNotIn('labels', service_dict)
  158. def test_no_override(self):
  159. service_dict = config.merge_service_dicts(
  160. config.make_service_dict('foo', {'labels': ['foo=1', 'bar']}),
  161. config.make_service_dict('foo', {}),
  162. )
  163. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
  164. def test_no_base(self):
  165. service_dict = config.merge_service_dicts(
  166. config.make_service_dict('foo', {}),
  167. config.make_service_dict('foo', {'labels': ['foo=2']}),
  168. )
  169. self.assertEqual(service_dict['labels'], {'foo': '2'})
  170. def test_override_explicit_value(self):
  171. service_dict = config.merge_service_dicts(
  172. config.make_service_dict('foo', {'labels': ['foo=1', 'bar']}),
  173. config.make_service_dict('foo', {'labels': ['foo=2']}),
  174. )
  175. self.assertEqual(service_dict['labels'], {'foo': '2', 'bar': ''})
  176. def test_add_explicit_value(self):
  177. service_dict = config.merge_service_dicts(
  178. config.make_service_dict('foo', {'labels': ['foo=1', 'bar']}),
  179. config.make_service_dict('foo', {'labels': ['bar=2']}),
  180. )
  181. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': '2'})
  182. def test_remove_explicit_value(self):
  183. service_dict = config.merge_service_dicts(
  184. config.make_service_dict('foo', {'labels': ['foo=1', 'bar=2']}),
  185. config.make_service_dict('foo', {'labels': ['bar']}),
  186. )
  187. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
  188. class EnvTest(unittest.TestCase):
  189. def test_parse_environment_as_list(self):
  190. environment = [
  191. 'NORMAL=F1',
  192. 'CONTAINS_EQUALS=F=2',
  193. 'TRAILING_EQUALS=',
  194. ]
  195. self.assertEqual(
  196. config.parse_environment(environment),
  197. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  198. )
  199. def test_parse_environment_as_dict(self):
  200. environment = {
  201. 'NORMAL': 'F1',
  202. 'CONTAINS_EQUALS': 'F=2',
  203. 'TRAILING_EQUALS': None,
  204. }
  205. self.assertEqual(config.parse_environment(environment), environment)
  206. def test_parse_environment_invalid(self):
  207. with self.assertRaises(config.ConfigurationError):
  208. config.parse_environment('a=b')
  209. def test_parse_environment_empty(self):
  210. self.assertEqual(config.parse_environment(None), {})
  211. @mock.patch.dict(os.environ)
  212. def test_resolve_environment(self):
  213. os.environ['FILE_DEF'] = 'E1'
  214. os.environ['FILE_DEF_EMPTY'] = 'E2'
  215. os.environ['ENV_DEF'] = 'E3'
  216. service_dict = config.make_service_dict(
  217. 'foo', {
  218. 'environment': {
  219. 'FILE_DEF': 'F1',
  220. 'FILE_DEF_EMPTY': '',
  221. 'ENV_DEF': None,
  222. 'NO_DEF': None
  223. },
  224. },
  225. )
  226. self.assertEqual(
  227. service_dict['environment'],
  228. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  229. )
  230. def test_env_from_file(self):
  231. service_dict = config.make_service_dict(
  232. 'foo',
  233. {'env_file': 'one.env'},
  234. 'tests/fixtures/env',
  235. )
  236. self.assertEqual(
  237. service_dict['environment'],
  238. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  239. )
  240. def test_env_from_multiple_files(self):
  241. service_dict = config.make_service_dict(
  242. 'foo',
  243. {'env_file': ['one.env', 'two.env']},
  244. 'tests/fixtures/env',
  245. )
  246. self.assertEqual(
  247. service_dict['environment'],
  248. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  249. )
  250. def test_env_nonexistent_file(self):
  251. options = {'env_file': 'nonexistent.env'}
  252. self.assertRaises(
  253. config.ConfigurationError,
  254. lambda: config.make_service_dict('foo', options, 'tests/fixtures/env'),
  255. )
  256. @mock.patch.dict(os.environ)
  257. def test_resolve_environment_from_file(self):
  258. os.environ['FILE_DEF'] = 'E1'
  259. os.environ['FILE_DEF_EMPTY'] = 'E2'
  260. os.environ['ENV_DEF'] = 'E3'
  261. service_dict = config.make_service_dict(
  262. 'foo',
  263. {'env_file': 'resolve.env'},
  264. 'tests/fixtures/env',
  265. )
  266. self.assertEqual(
  267. service_dict['environment'],
  268. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  269. )
  270. class ExtendsTest(unittest.TestCase):
  271. def test_extends(self):
  272. service_dicts = config.load('tests/fixtures/extends/docker-compose.yml')
  273. service_dicts = sorted(
  274. service_dicts,
  275. key=lambda sd: sd['name'],
  276. )
  277. self.assertEqual(service_dicts, [
  278. {
  279. 'name': 'mydb',
  280. 'image': 'busybox',
  281. 'command': 'sleep 300',
  282. },
  283. {
  284. 'name': 'myweb',
  285. 'image': 'busybox',
  286. 'command': 'sleep 300',
  287. 'links': ['mydb:db'],
  288. 'environment': {
  289. "FOO": "1",
  290. "BAR": "2",
  291. "BAZ": "2",
  292. },
  293. }
  294. ])
  295. def test_nested(self):
  296. service_dicts = config.load('tests/fixtures/extends/nested.yml')
  297. self.assertEqual(service_dicts, [
  298. {
  299. 'name': 'myweb',
  300. 'image': 'busybox',
  301. 'command': '/bin/true',
  302. 'environment': {
  303. "FOO": "2",
  304. "BAR": "2",
  305. },
  306. },
  307. ])
  308. def test_circular(self):
  309. try:
  310. config.load('tests/fixtures/extends/circle-1.yml')
  311. raise Exception("Expected config.CircularReference to be raised")
  312. except config.CircularReference as e:
  313. self.assertEqual(
  314. [(os.path.basename(filename), service_name) for (filename, service_name) in e.trail],
  315. [
  316. ('circle-1.yml', 'web'),
  317. ('circle-2.yml', 'web'),
  318. ('circle-1.yml', 'web'),
  319. ],
  320. )
  321. def test_extends_validation(self):
  322. dictionary = {'extends': None}
  323. def load_config():
  324. return config.make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  325. self.assertRaisesRegexp(config.ConfigurationError, 'dictionary', load_config)
  326. dictionary['extends'] = {}
  327. self.assertRaises(config.ConfigurationError, load_config)
  328. dictionary['extends']['file'] = 'common.yml'
  329. self.assertRaisesRegexp(config.ConfigurationError, 'service', load_config)
  330. dictionary['extends']['service'] = 'web'
  331. self.assertIsInstance(load_config(), dict)
  332. dictionary['extends']['what'] = 'is this'
  333. self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config)
  334. def test_blacklisted_options(self):
  335. def load_config():
  336. return config.make_service_dict('myweb', {
  337. 'extends': {
  338. 'file': 'whatever',
  339. 'service': 'web',
  340. }
  341. }, '.')
  342. with self.assertRaisesRegexp(config.ConfigurationError, 'links'):
  343. other_config = {'web': {'links': ['db']}}
  344. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  345. print load_config()
  346. with self.assertRaisesRegexp(config.ConfigurationError, 'volumes_from'):
  347. other_config = {'web': {'volumes_from': ['db']}}
  348. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  349. print load_config()
  350. with self.assertRaisesRegexp(config.ConfigurationError, 'net'):
  351. other_config = {'web': {'net': 'container:db'}}
  352. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  353. print load_config()
  354. other_config = {'web': {'net': 'host'}}
  355. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  356. print load_config()
  357. def test_volume_path(self):
  358. dicts = config.load('tests/fixtures/volume-path/docker-compose.yml')
  359. paths = [
  360. '%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
  361. '%s:/bar' % os.path.abspath('tests/fixtures/volume-path/bar'),
  362. ]
  363. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  364. def test_parent_build_path_dne(self):
  365. child = config.load('tests/fixtures/extends/nonexistent-path-child.yml')
  366. self.assertEqual(child, [
  367. {
  368. 'name': 'dnechild',
  369. 'image': 'busybox',
  370. 'command': '/bin/true',
  371. 'environment': {
  372. "FOO": "1",
  373. "BAR": "2",
  374. },
  375. },
  376. ])
  377. class BuildPathTest(unittest.TestCase):
  378. def setUp(self):
  379. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  380. def test_nonexistent_path(self):
  381. options = {'build': 'nonexistent.path'}
  382. self.assertRaises(
  383. config.ConfigurationError,
  384. lambda: config.from_dictionary({
  385. 'foo': options,
  386. 'working_dir': 'tests/fixtures/build-path'
  387. })
  388. )
  389. def test_relative_path(self):
  390. relative_build_path = '../build-ctx/'
  391. service_dict = config.make_service_dict(
  392. 'relpath',
  393. {'build': relative_build_path},
  394. working_dir='tests/fixtures/build-path'
  395. )
  396. self.assertEquals(service_dict['build'], self.abs_context_path)
  397. def test_absolute_path(self):
  398. service_dict = config.make_service_dict(
  399. 'abspath',
  400. {'build': self.abs_context_path},
  401. working_dir='tests/fixtures/build-path'
  402. )
  403. self.assertEquals(service_dict['build'], self.abs_context_path)
  404. def test_from_file(self):
  405. service_dict = config.load('tests/fixtures/build-path/docker-compose.yml')
  406. self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])