config_test.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. import mock
  2. import os
  3. import shutil
  4. import tempfile
  5. from .. import unittest
  6. from compose.config import config
  7. def make_service_dict(name, service_dict, working_dir):
  8. """
  9. Test helper function to contruct a ServiceLoader
  10. """
  11. return config.ServiceLoader(working_dir=working_dir).make_service_dict(name, service_dict)
  12. class ConfigTest(unittest.TestCase):
  13. def test_load(self):
  14. service_dicts = config.load(
  15. config.ConfigDetails(
  16. {
  17. 'foo': {'image': 'busybox'},
  18. 'bar': {'environment': ['FOO=1']},
  19. },
  20. 'working_dir',
  21. 'filename.yml'
  22. )
  23. )
  24. self.assertEqual(
  25. sorted(service_dicts, key=lambda d: d['name']),
  26. sorted([
  27. {
  28. 'name': 'bar',
  29. 'environment': {'FOO': '1'},
  30. },
  31. {
  32. 'name': 'foo',
  33. 'image': 'busybox',
  34. }
  35. ])
  36. )
  37. def test_load_throws_error_when_not_dict(self):
  38. with self.assertRaises(config.ConfigurationError):
  39. config.load(
  40. config.ConfigDetails(
  41. {'web': 'busybox:latest'},
  42. 'working_dir',
  43. 'filename.yml'
  44. )
  45. )
  46. def test_config_validation(self):
  47. self.assertRaises(
  48. config.ConfigurationError,
  49. lambda: make_service_dict('foo', {'port': ['8000']}, 'tests/')
  50. )
  51. make_service_dict('foo', {'ports': ['8000']}, 'tests/')
  52. class InterpolationTest(unittest.TestCase):
  53. @mock.patch.dict(os.environ)
  54. def test_config_file_with_environment_variable(self):
  55. os.environ.update(
  56. IMAGE="busybox",
  57. HOST_PORT="80",
  58. LABEL_VALUE="myvalue",
  59. )
  60. service_dicts = config.load(
  61. config.find('tests/fixtures/environment-interpolation', None),
  62. )
  63. self.assertEqual(service_dicts, [
  64. {
  65. 'name': 'web',
  66. 'image': 'busybox',
  67. 'ports': ['80:8000'],
  68. 'labels': {'mylabel': 'myvalue'},
  69. 'hostname': 'host-',
  70. 'command': '${ESCAPED}',
  71. }
  72. ])
  73. @mock.patch.dict(os.environ)
  74. def test_invalid_interpolation(self):
  75. with self.assertRaises(config.ConfigurationError) as cm:
  76. config.load(
  77. config.ConfigDetails(
  78. {'web': {'image': '${'}},
  79. 'working_dir',
  80. 'filename.yml'
  81. )
  82. )
  83. self.assertIn('Invalid', cm.exception.msg)
  84. self.assertIn('for "image" option', cm.exception.msg)
  85. self.assertIn('in service "web"', cm.exception.msg)
  86. self.assertIn('"${"', cm.exception.msg)
  87. @mock.patch.dict(os.environ)
  88. def test_volume_binding_with_environment_variable(self):
  89. os.environ['VOLUME_PATH'] = '/host/path'
  90. d = config.load(
  91. config.ConfigDetails(
  92. config={'foo': {'volumes': ['${VOLUME_PATH}:/container/path']}},
  93. working_dir='.',
  94. filename=None,
  95. )
  96. )[0]
  97. self.assertEqual(d['volumes'], ['/host/path:/container/path'])
  98. @mock.patch.dict(os.environ)
  99. def test_volume_binding_with_home(self):
  100. os.environ['HOME'] = '/home/user'
  101. d = make_service_dict('foo', {'volumes': ['~:/container/path']}, working_dir='.')
  102. self.assertEqual(d['volumes'], ['/home/user:/container/path'])
  103. def test_named_volume_with_driver(self):
  104. d = make_service_dict('foo', {
  105. 'volumes': ['namedvolume:/data'],
  106. 'volume_driver': 'foodriver',
  107. }, working_dir='.')
  108. self.assertEqual(d['volumes'], ['namedvolume:/data'])
  109. @mock.patch.dict(os.environ)
  110. def test_named_volume_with_special_chars(self):
  111. os.environ['NAME'] = 'surprise!'
  112. d = make_service_dict('foo', {
  113. 'volumes': ['~/${NAME}:/data'],
  114. 'volume_driver': 'foodriver',
  115. }, working_dir='.')
  116. self.assertEqual(d['volumes'], ['~/${NAME}:/data'])
  117. class MergePathMappingTest(object):
  118. def config_name(self):
  119. return ""
  120. def test_empty(self):
  121. service_dict = config.merge_service_dicts({}, {})
  122. self.assertNotIn(self.config_name(), service_dict)
  123. def test_no_override(self):
  124. service_dict = config.merge_service_dicts(
  125. {self.config_name(): ['/foo:/code', '/data']},
  126. {},
  127. )
  128. self.assertEqual(set(service_dict[self.config_name()]), set(['/foo:/code', '/data']))
  129. def test_no_base(self):
  130. service_dict = config.merge_service_dicts(
  131. {},
  132. {self.config_name(): ['/bar:/code']},
  133. )
  134. self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code']))
  135. def test_override_explicit_path(self):
  136. service_dict = config.merge_service_dicts(
  137. {self.config_name(): ['/foo:/code', '/data']},
  138. {self.config_name(): ['/bar:/code']},
  139. )
  140. self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/data']))
  141. def test_add_explicit_path(self):
  142. service_dict = config.merge_service_dicts(
  143. {self.config_name(): ['/foo:/code', '/data']},
  144. {self.config_name(): ['/bar:/code', '/quux:/data']},
  145. )
  146. self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/quux:/data']))
  147. def test_remove_explicit_path(self):
  148. service_dict = config.merge_service_dicts(
  149. {self.config_name(): ['/foo:/code', '/quux:/data']},
  150. {self.config_name(): ['/bar:/code', '/data']},
  151. )
  152. self.assertEqual(set(service_dict[self.config_name()]), set(['/bar:/code', '/data']))
  153. class MergeVolumesTest(unittest.TestCase, MergePathMappingTest):
  154. def config_name(self):
  155. return 'volumes'
  156. class MergeDevicesTest(unittest.TestCase, MergePathMappingTest):
  157. def config_name(self):
  158. return 'devices'
  159. class BuildOrImageMergeTest(unittest.TestCase):
  160. def test_merge_build_or_image_no_override(self):
  161. self.assertEqual(
  162. config.merge_service_dicts({'build': '.'}, {}),
  163. {'build': '.'},
  164. )
  165. self.assertEqual(
  166. config.merge_service_dicts({'image': 'redis'}, {}),
  167. {'image': 'redis'},
  168. )
  169. def test_merge_build_or_image_override_with_same(self):
  170. self.assertEqual(
  171. config.merge_service_dicts({'build': '.'}, {'build': './web'}),
  172. {'build': './web'},
  173. )
  174. self.assertEqual(
  175. config.merge_service_dicts({'image': 'redis'}, {'image': 'postgres'}),
  176. {'image': 'postgres'},
  177. )
  178. def test_merge_build_or_image_override_with_other(self):
  179. self.assertEqual(
  180. config.merge_service_dicts({'build': '.'}, {'image': 'redis'}),
  181. {'image': 'redis'}
  182. )
  183. self.assertEqual(
  184. config.merge_service_dicts({'image': 'redis'}, {'build': '.'}),
  185. {'build': '.'}
  186. )
  187. class MergeListsTest(unittest.TestCase):
  188. def test_empty(self):
  189. service_dict = config.merge_service_dicts({}, {})
  190. self.assertNotIn('ports', service_dict)
  191. def test_no_override(self):
  192. service_dict = config.merge_service_dicts(
  193. {'ports': ['10:8000', '9000']},
  194. {},
  195. )
  196. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
  197. def test_no_base(self):
  198. service_dict = config.merge_service_dicts(
  199. {},
  200. {'ports': ['10:8000', '9000']},
  201. )
  202. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000']))
  203. def test_add_item(self):
  204. service_dict = config.merge_service_dicts(
  205. {'ports': ['10:8000', '9000']},
  206. {'ports': ['20:8000']},
  207. )
  208. self.assertEqual(set(service_dict['ports']), set(['10:8000', '9000', '20:8000']))
  209. class MergeStringsOrListsTest(unittest.TestCase):
  210. def test_no_override(self):
  211. service_dict = config.merge_service_dicts(
  212. {'dns': '8.8.8.8'},
  213. {},
  214. )
  215. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
  216. def test_no_base(self):
  217. service_dict = config.merge_service_dicts(
  218. {},
  219. {'dns': '8.8.8.8'},
  220. )
  221. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8']))
  222. def test_add_string(self):
  223. service_dict = config.merge_service_dicts(
  224. {'dns': ['8.8.8.8']},
  225. {'dns': '9.9.9.9'},
  226. )
  227. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
  228. def test_add_list(self):
  229. service_dict = config.merge_service_dicts(
  230. {'dns': '8.8.8.8'},
  231. {'dns': ['9.9.9.9']},
  232. )
  233. self.assertEqual(set(service_dict['dns']), set(['8.8.8.8', '9.9.9.9']))
  234. class MergeLabelsTest(unittest.TestCase):
  235. def test_empty(self):
  236. service_dict = config.merge_service_dicts({}, {})
  237. self.assertNotIn('labels', service_dict)
  238. def test_no_override(self):
  239. service_dict = config.merge_service_dicts(
  240. make_service_dict('foo', {'labels': ['foo=1', 'bar']}, 'tests/'),
  241. make_service_dict('foo', {}, 'tests/'),
  242. )
  243. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
  244. def test_no_base(self):
  245. service_dict = config.merge_service_dicts(
  246. make_service_dict('foo', {}, 'tests/'),
  247. make_service_dict('foo', {'labels': ['foo=2']}, 'tests/'),
  248. )
  249. self.assertEqual(service_dict['labels'], {'foo': '2'})
  250. def test_override_explicit_value(self):
  251. service_dict = config.merge_service_dicts(
  252. make_service_dict('foo', {'labels': ['foo=1', 'bar']}, 'tests/'),
  253. make_service_dict('foo', {'labels': ['foo=2']}, 'tests/'),
  254. )
  255. self.assertEqual(service_dict['labels'], {'foo': '2', 'bar': ''})
  256. def test_add_explicit_value(self):
  257. service_dict = config.merge_service_dicts(
  258. make_service_dict('foo', {'labels': ['foo=1', 'bar']}, 'tests/'),
  259. make_service_dict('foo', {'labels': ['bar=2']}, 'tests/'),
  260. )
  261. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': '2'})
  262. def test_remove_explicit_value(self):
  263. service_dict = config.merge_service_dicts(
  264. make_service_dict('foo', {'labels': ['foo=1', 'bar=2']}, 'tests/'),
  265. make_service_dict('foo', {'labels': ['bar']}, 'tests/'),
  266. )
  267. self.assertEqual(service_dict['labels'], {'foo': '1', 'bar': ''})
  268. class MemoryOptionsTest(unittest.TestCase):
  269. def test_validation_fails_with_just_memswap_limit(self):
  270. """
  271. When you set a 'memswap_limit' it is invalid config unless you also set
  272. a mem_limit
  273. """
  274. with self.assertRaises(config.ConfigurationError):
  275. make_service_dict(
  276. 'foo', {
  277. 'memswap_limit': 2000000,
  278. },
  279. 'tests/'
  280. )
  281. def test_validation_with_correct_memswap_values(self):
  282. service_dict = make_service_dict(
  283. 'foo', {
  284. 'mem_limit': 1000000,
  285. 'memswap_limit': 2000000,
  286. },
  287. 'tests/'
  288. )
  289. self.assertEqual(service_dict['memswap_limit'], 2000000)
  290. class EnvTest(unittest.TestCase):
  291. def test_parse_environment_as_list(self):
  292. environment = [
  293. 'NORMAL=F1',
  294. 'CONTAINS_EQUALS=F=2',
  295. 'TRAILING_EQUALS=',
  296. ]
  297. self.assertEqual(
  298. config.parse_environment(environment),
  299. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  300. )
  301. def test_parse_environment_as_dict(self):
  302. environment = {
  303. 'NORMAL': 'F1',
  304. 'CONTAINS_EQUALS': 'F=2',
  305. 'TRAILING_EQUALS': None,
  306. }
  307. self.assertEqual(config.parse_environment(environment), environment)
  308. def test_parse_environment_invalid(self):
  309. with self.assertRaises(config.ConfigurationError):
  310. config.parse_environment('a=b')
  311. def test_parse_environment_empty(self):
  312. self.assertEqual(config.parse_environment(None), {})
  313. @mock.patch.dict(os.environ)
  314. def test_resolve_environment(self):
  315. os.environ['FILE_DEF'] = 'E1'
  316. os.environ['FILE_DEF_EMPTY'] = 'E2'
  317. os.environ['ENV_DEF'] = 'E3'
  318. service_dict = make_service_dict(
  319. 'foo', {
  320. 'environment': {
  321. 'FILE_DEF': 'F1',
  322. 'FILE_DEF_EMPTY': '',
  323. 'ENV_DEF': None,
  324. 'NO_DEF': None
  325. },
  326. },
  327. 'tests/'
  328. )
  329. self.assertEqual(
  330. service_dict['environment'],
  331. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  332. )
  333. def test_env_from_file(self):
  334. service_dict = make_service_dict(
  335. 'foo',
  336. {'env_file': 'one.env'},
  337. 'tests/fixtures/env',
  338. )
  339. self.assertEqual(
  340. service_dict['environment'],
  341. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  342. )
  343. def test_env_from_multiple_files(self):
  344. service_dict = make_service_dict(
  345. 'foo',
  346. {'env_file': ['one.env', 'two.env']},
  347. 'tests/fixtures/env',
  348. )
  349. self.assertEqual(
  350. service_dict['environment'],
  351. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  352. )
  353. def test_env_nonexistent_file(self):
  354. options = {'env_file': 'nonexistent.env'}
  355. self.assertRaises(
  356. config.ConfigurationError,
  357. lambda: make_service_dict('foo', options, 'tests/fixtures/env'),
  358. )
  359. @mock.patch.dict(os.environ)
  360. def test_resolve_environment_from_file(self):
  361. os.environ['FILE_DEF'] = 'E1'
  362. os.environ['FILE_DEF_EMPTY'] = 'E2'
  363. os.environ['ENV_DEF'] = 'E3'
  364. service_dict = make_service_dict(
  365. 'foo',
  366. {'env_file': 'resolve.env'},
  367. 'tests/fixtures/env',
  368. )
  369. self.assertEqual(
  370. service_dict['environment'],
  371. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  372. )
  373. @mock.patch.dict(os.environ)
  374. def test_resolve_path(self):
  375. os.environ['HOSTENV'] = '/tmp'
  376. os.environ['CONTAINERENV'] = '/host/tmp'
  377. service_dict = config.load(
  378. config.ConfigDetails(
  379. config={'foo': {'volumes': ['$HOSTENV:$CONTAINERENV']}},
  380. working_dir="tests/fixtures/env",
  381. filename=None,
  382. )
  383. )[0]
  384. self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp']))
  385. service_dict = config.load(
  386. config.ConfigDetails(
  387. config={'foo': {'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
  388. working_dir="tests/fixtures/env",
  389. filename=None,
  390. )
  391. )[0]
  392. self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp']))
  393. def load_from_filename(filename):
  394. return config.load(config.find('.', filename))
  395. class ExtendsTest(unittest.TestCase):
  396. def test_extends(self):
  397. service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
  398. service_dicts = sorted(
  399. service_dicts,
  400. key=lambda sd: sd['name'],
  401. )
  402. self.assertEqual(service_dicts, [
  403. {
  404. 'name': 'mydb',
  405. 'image': 'busybox',
  406. 'command': 'top',
  407. },
  408. {
  409. 'name': 'myweb',
  410. 'image': 'busybox',
  411. 'command': 'top',
  412. 'links': ['mydb:db'],
  413. 'environment': {
  414. "FOO": "1",
  415. "BAR": "2",
  416. "BAZ": "2",
  417. },
  418. }
  419. ])
  420. def test_nested(self):
  421. service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
  422. self.assertEqual(service_dicts, [
  423. {
  424. 'name': 'myweb',
  425. 'image': 'busybox',
  426. 'command': '/bin/true',
  427. 'environment': {
  428. "FOO": "2",
  429. "BAR": "2",
  430. },
  431. },
  432. ])
  433. def test_self_referencing_file(self):
  434. """
  435. We specify a 'file' key that is the filename we're already in.
  436. """
  437. service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
  438. self.assertEqual(service_dicts, [
  439. {
  440. 'environment':
  441. {
  442. 'YEP': '1', 'BAR': '1', 'BAZ': '3'
  443. },
  444. 'image': 'busybox',
  445. 'name': 'myweb'
  446. },
  447. {
  448. 'environment':
  449. {'YEP': '1'},
  450. 'name': 'otherweb'
  451. },
  452. {
  453. 'environment':
  454. {'YEP': '1', 'BAZ': '3'},
  455. 'image': 'busybox',
  456. 'name': 'web'
  457. }
  458. ])
  459. def test_circular(self):
  460. try:
  461. load_from_filename('tests/fixtures/extends/circle-1.yml')
  462. raise Exception("Expected config.CircularReference to be raised")
  463. except config.CircularReference as e:
  464. self.assertEqual(
  465. [(os.path.basename(filename), service_name) for (filename, service_name) in e.trail],
  466. [
  467. ('circle-1.yml', 'web'),
  468. ('circle-2.yml', 'web'),
  469. ('circle-1.yml', 'web'),
  470. ],
  471. )
  472. def test_extends_validation_empty_dictionary(self):
  473. dictionary = {'extends': None}
  474. def load_config():
  475. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  476. self.assertRaisesRegexp(config.ConfigurationError, 'dictionary', load_config)
  477. dictionary['extends'] = {}
  478. self.assertRaises(config.ConfigurationError, load_config)
  479. def test_extends_validation_missing_service_key(self):
  480. dictionary = {'extends': {'file': 'common.yml'}}
  481. def load_config():
  482. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  483. self.assertRaisesRegexp(config.ConfigurationError, 'service', load_config)
  484. def test_extends_validation_invalid_key(self):
  485. dictionary = {
  486. 'extends':
  487. {
  488. 'service': 'web', 'file': 'common.yml', 'what': 'is this'
  489. }
  490. }
  491. def load_config():
  492. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  493. self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config)
  494. def test_extends_validation_no_file_key_no_filename_set(self):
  495. dictionary = {'extends': {'service': 'web'}}
  496. def load_config():
  497. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  498. self.assertRaisesRegexp(config.ConfigurationError, 'file', load_config)
  499. def test_extends_validation_valid_config(self):
  500. dictionary = {'extends': {'service': 'web', 'file': 'common.yml'}}
  501. def load_config():
  502. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  503. self.assertIsInstance(load_config(), dict)
  504. def test_extends_file_defaults_to_self(self):
  505. """
  506. Test not specifying a file in our extends options that the
  507. config is valid and correctly extends from itself.
  508. """
  509. service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
  510. self.assertEqual(service_dicts, [
  511. {
  512. 'name': 'myweb',
  513. 'image': 'busybox',
  514. 'environment': {
  515. "BAR": "1",
  516. "BAZ": "3",
  517. }
  518. },
  519. {
  520. 'name': 'web',
  521. 'image': 'busybox',
  522. 'environment': {
  523. "BAZ": "3",
  524. }
  525. }
  526. ])
  527. def test_blacklisted_options(self):
  528. def load_config():
  529. return make_service_dict('myweb', {
  530. 'extends': {
  531. 'file': 'whatever',
  532. 'service': 'web',
  533. }
  534. }, '.')
  535. with self.assertRaisesRegexp(config.ConfigurationError, 'links'):
  536. other_config = {'web': {'links': ['db']}}
  537. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  538. print load_config()
  539. with self.assertRaisesRegexp(config.ConfigurationError, 'volumes_from'):
  540. other_config = {'web': {'volumes_from': ['db']}}
  541. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  542. print load_config()
  543. with self.assertRaisesRegexp(config.ConfigurationError, 'net'):
  544. other_config = {'web': {'net': 'container:db'}}
  545. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  546. print load_config()
  547. other_config = {'web': {'net': 'host'}}
  548. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  549. print load_config()
  550. def test_volume_path(self):
  551. dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
  552. paths = [
  553. '%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
  554. '%s:/bar' % os.path.abspath('tests/fixtures/volume-path/bar'),
  555. ]
  556. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  557. def test_parent_build_path_dne(self):
  558. child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
  559. self.assertEqual(child, [
  560. {
  561. 'name': 'dnechild',
  562. 'image': 'busybox',
  563. 'command': '/bin/true',
  564. 'environment': {
  565. "FOO": "1",
  566. "BAR": "2",
  567. },
  568. },
  569. ])
  570. class BuildPathTest(unittest.TestCase):
  571. def setUp(self):
  572. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  573. def test_nonexistent_path(self):
  574. with self.assertRaises(config.ConfigurationError):
  575. config.load(
  576. config.ConfigDetails(
  577. {
  578. 'foo': {'build': 'nonexistent.path'},
  579. },
  580. 'working_dir',
  581. 'filename.yml'
  582. )
  583. )
  584. def test_relative_path(self):
  585. relative_build_path = '../build-ctx/'
  586. service_dict = make_service_dict(
  587. 'relpath',
  588. {'build': relative_build_path},
  589. working_dir='tests/fixtures/build-path'
  590. )
  591. self.assertEquals(service_dict['build'], self.abs_context_path)
  592. def test_absolute_path(self):
  593. service_dict = make_service_dict(
  594. 'abspath',
  595. {'build': self.abs_context_path},
  596. working_dir='tests/fixtures/build-path'
  597. )
  598. self.assertEquals(service_dict['build'], self.abs_context_path)
  599. def test_from_file(self):
  600. service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
  601. self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])
  602. class GetConfigPathTestCase(unittest.TestCase):
  603. files = [
  604. 'docker-compose.yml',
  605. 'docker-compose.yaml',
  606. 'fig.yml',
  607. 'fig.yaml',
  608. ]
  609. def test_get_config_path_default_file_in_basedir(self):
  610. files = self.files
  611. self.assertEqual('docker-compose.yml', get_config_filename_for_files(files[0:]))
  612. self.assertEqual('docker-compose.yaml', get_config_filename_for_files(files[1:]))
  613. self.assertEqual('fig.yml', get_config_filename_for_files(files[2:]))
  614. self.assertEqual('fig.yaml', get_config_filename_for_files(files[3:]))
  615. with self.assertRaises(config.ComposeFileNotFound):
  616. get_config_filename_for_files([])
  617. def test_get_config_path_default_file_in_parent_dir(self):
  618. """Test with files placed in the subdir"""
  619. files = self.files
  620. def get_config_in_subdir(files):
  621. return get_config_filename_for_files(files, subdir=True)
  622. self.assertEqual('docker-compose.yml', get_config_in_subdir(files[0:]))
  623. self.assertEqual('docker-compose.yaml', get_config_in_subdir(files[1:]))
  624. self.assertEqual('fig.yml', get_config_in_subdir(files[2:]))
  625. self.assertEqual('fig.yaml', get_config_in_subdir(files[3:]))
  626. with self.assertRaises(config.ComposeFileNotFound):
  627. get_config_in_subdir([])
  628. def get_config_filename_for_files(filenames, subdir=None):
  629. def make_files(dirname, filenames):
  630. for fname in filenames:
  631. with open(os.path.join(dirname, fname), 'w') as f:
  632. f.write('')
  633. project_dir = tempfile.mkdtemp()
  634. try:
  635. make_files(project_dir, filenames)
  636. if subdir:
  637. base_dir = tempfile.mkdtemp(dir=project_dir)
  638. else:
  639. base_dir = project_dir
  640. return os.path.basename(config.get_config_path(base_dir))
  641. finally:
  642. shutil.rmtree(project_dir)