config_test.py 25 KB

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