config_test.py 23 KB

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