config_test.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 EnvTest(unittest.TestCase):
  215. def test_parse_environment_as_list(self):
  216. environment = [
  217. 'NORMAL=F1',
  218. 'CONTAINS_EQUALS=F=2',
  219. 'TRAILING_EQUALS=',
  220. ]
  221. self.assertEqual(
  222. config.parse_environment(environment),
  223. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  224. )
  225. def test_parse_environment_as_dict(self):
  226. environment = {
  227. 'NORMAL': 'F1',
  228. 'CONTAINS_EQUALS': 'F=2',
  229. 'TRAILING_EQUALS': None,
  230. }
  231. self.assertEqual(config.parse_environment(environment), environment)
  232. def test_parse_environment_invalid(self):
  233. with self.assertRaises(config.ConfigurationError):
  234. config.parse_environment('a=b')
  235. def test_parse_environment_empty(self):
  236. self.assertEqual(config.parse_environment(None), {})
  237. @mock.patch.dict(os.environ)
  238. def test_resolve_environment(self):
  239. os.environ['FILE_DEF'] = 'E1'
  240. os.environ['FILE_DEF_EMPTY'] = 'E2'
  241. os.environ['ENV_DEF'] = 'E3'
  242. service_dict = make_service_dict(
  243. 'foo', {
  244. 'environment': {
  245. 'FILE_DEF': 'F1',
  246. 'FILE_DEF_EMPTY': '',
  247. 'ENV_DEF': None,
  248. 'NO_DEF': None
  249. },
  250. },
  251. 'tests/'
  252. )
  253. self.assertEqual(
  254. service_dict['environment'],
  255. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  256. )
  257. def test_env_from_file(self):
  258. service_dict = make_service_dict(
  259. 'foo',
  260. {'env_file': 'one.env'},
  261. 'tests/fixtures/env',
  262. )
  263. self.assertEqual(
  264. service_dict['environment'],
  265. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  266. )
  267. def test_env_from_multiple_files(self):
  268. service_dict = make_service_dict(
  269. 'foo',
  270. {'env_file': ['one.env', 'two.env']},
  271. 'tests/fixtures/env',
  272. )
  273. self.assertEqual(
  274. service_dict['environment'],
  275. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  276. )
  277. def test_env_nonexistent_file(self):
  278. options = {'env_file': 'nonexistent.env'}
  279. self.assertRaises(
  280. config.ConfigurationError,
  281. lambda: make_service_dict('foo', options, 'tests/fixtures/env'),
  282. )
  283. @mock.patch.dict(os.environ)
  284. def test_resolve_environment_from_file(self):
  285. os.environ['FILE_DEF'] = 'E1'
  286. os.environ['FILE_DEF_EMPTY'] = 'E2'
  287. os.environ['ENV_DEF'] = 'E3'
  288. service_dict = make_service_dict(
  289. 'foo',
  290. {'env_file': 'resolve.env'},
  291. 'tests/fixtures/env',
  292. )
  293. self.assertEqual(
  294. service_dict['environment'],
  295. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''},
  296. )
  297. @mock.patch.dict(os.environ)
  298. def test_resolve_path(self):
  299. os.environ['HOSTENV'] = '/tmp'
  300. os.environ['CONTAINERENV'] = '/host/tmp'
  301. service_dict = make_service_dict(
  302. 'foo',
  303. {'volumes': ['$HOSTENV:$CONTAINERENV']},
  304. working_dir="tests/fixtures/env"
  305. )
  306. self.assertEqual(set(service_dict['volumes']), set(['/tmp:/host/tmp']))
  307. service_dict = make_service_dict(
  308. 'foo',
  309. {'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']},
  310. working_dir="tests/fixtures/env"
  311. )
  312. self.assertEqual(set(service_dict['volumes']), set(['/opt/tmp:/opt/host/tmp']))
  313. def load_from_filename(filename):
  314. return config.load(config.find('.', filename))
  315. class ExtendsTest(unittest.TestCase):
  316. def test_extends(self):
  317. service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
  318. service_dicts = sorted(
  319. service_dicts,
  320. key=lambda sd: sd['name'],
  321. )
  322. self.assertEqual(service_dicts, [
  323. {
  324. 'name': 'mydb',
  325. 'image': 'busybox',
  326. 'command': 'top',
  327. },
  328. {
  329. 'name': 'myweb',
  330. 'image': 'busybox',
  331. 'command': 'top',
  332. 'links': ['mydb:db'],
  333. 'environment': {
  334. "FOO": "1",
  335. "BAR": "2",
  336. "BAZ": "2",
  337. },
  338. }
  339. ])
  340. def test_nested(self):
  341. service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
  342. self.assertEqual(service_dicts, [
  343. {
  344. 'name': 'myweb',
  345. 'image': 'busybox',
  346. 'command': '/bin/true',
  347. 'environment': {
  348. "FOO": "2",
  349. "BAR": "2",
  350. },
  351. },
  352. ])
  353. def test_self_referencing_file(self):
  354. """
  355. We specify a 'file' key that is the filename we're already in.
  356. """
  357. service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
  358. self.assertEqual(service_dicts, [
  359. {
  360. 'environment':
  361. {
  362. 'YEP': '1', 'BAR': '1', 'BAZ': '3'
  363. },
  364. 'image': 'busybox',
  365. 'name': 'myweb'
  366. },
  367. {
  368. 'environment':
  369. {'YEP': '1'},
  370. 'name': 'otherweb'
  371. },
  372. {
  373. 'environment':
  374. {'YEP': '1', 'BAZ': '3'},
  375. 'image': 'busybox',
  376. 'name': 'web'
  377. }
  378. ])
  379. def test_circular(self):
  380. try:
  381. load_from_filename('tests/fixtures/extends/circle-1.yml')
  382. raise Exception("Expected config.CircularReference to be raised")
  383. except config.CircularReference as e:
  384. self.assertEqual(
  385. [(os.path.basename(filename), service_name) for (filename, service_name) in e.trail],
  386. [
  387. ('circle-1.yml', 'web'),
  388. ('circle-2.yml', 'web'),
  389. ('circle-1.yml', 'web'),
  390. ],
  391. )
  392. def test_extends_validation_empty_dictionary(self):
  393. dictionary = {'extends': None}
  394. def load_config():
  395. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  396. self.assertRaisesRegexp(config.ConfigurationError, 'dictionary', load_config)
  397. dictionary['extends'] = {}
  398. self.assertRaises(config.ConfigurationError, load_config)
  399. def test_extends_validation_missing_service_key(self):
  400. dictionary = {'extends': {'file': 'common.yml'}}
  401. def load_config():
  402. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  403. self.assertRaisesRegexp(config.ConfigurationError, 'service', load_config)
  404. def test_extends_validation_invalid_key(self):
  405. dictionary = {
  406. 'extends':
  407. {
  408. 'service': 'web', 'file': 'common.yml', 'what': 'is this'
  409. }
  410. }
  411. def load_config():
  412. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  413. self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config)
  414. def test_extends_validation_no_file_key_no_filename_set(self):
  415. dictionary = {'extends': {'service': 'web'}}
  416. def load_config():
  417. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  418. self.assertRaisesRegexp(config.ConfigurationError, 'file', load_config)
  419. def test_extends_validation_valid_config(self):
  420. dictionary = {'extends': {'service': 'web', 'file': 'common.yml'}}
  421. def load_config():
  422. return make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  423. self.assertIsInstance(load_config(), dict)
  424. def test_extends_file_defaults_to_self(self):
  425. """
  426. Test not specifying a file in our extends options that the
  427. config is valid and correctly extends from itself.
  428. """
  429. service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
  430. self.assertEqual(service_dicts, [
  431. {
  432. 'name': 'myweb',
  433. 'image': 'busybox',
  434. 'environment': {
  435. "BAR": "1",
  436. "BAZ": "3",
  437. }
  438. },
  439. {
  440. 'name': 'web',
  441. 'image': 'busybox',
  442. 'environment': {
  443. "BAZ": "3",
  444. }
  445. }
  446. ])
  447. def test_blacklisted_options(self):
  448. def load_config():
  449. return make_service_dict('myweb', {
  450. 'extends': {
  451. 'file': 'whatever',
  452. 'service': 'web',
  453. }
  454. }, '.')
  455. with self.assertRaisesRegexp(config.ConfigurationError, 'links'):
  456. other_config = {'web': {'links': ['db']}}
  457. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  458. print load_config()
  459. with self.assertRaisesRegexp(config.ConfigurationError, 'volumes_from'):
  460. other_config = {'web': {'volumes_from': ['db']}}
  461. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  462. print load_config()
  463. with self.assertRaisesRegexp(config.ConfigurationError, 'net'):
  464. other_config = {'web': {'net': 'container:db'}}
  465. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  466. print load_config()
  467. other_config = {'web': {'net': 'host'}}
  468. with mock.patch.object(config, 'load_yaml', return_value=other_config):
  469. print load_config()
  470. def test_volume_path(self):
  471. dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
  472. paths = [
  473. '%s:/foo' % os.path.abspath('tests/fixtures/volume-path/common/foo'),
  474. '%s:/bar' % os.path.abspath('tests/fixtures/volume-path/bar'),
  475. ]
  476. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  477. def test_parent_build_path_dne(self):
  478. child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
  479. self.assertEqual(child, [
  480. {
  481. 'name': 'dnechild',
  482. 'image': 'busybox',
  483. 'command': '/bin/true',
  484. 'environment': {
  485. "FOO": "1",
  486. "BAR": "2",
  487. },
  488. },
  489. ])
  490. class BuildPathTest(unittest.TestCase):
  491. def setUp(self):
  492. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  493. def test_nonexistent_path(self):
  494. with self.assertRaises(config.ConfigurationError):
  495. config.load(
  496. config.ConfigDetails(
  497. {
  498. 'foo': {'build': 'nonexistent.path'},
  499. },
  500. 'working_dir',
  501. 'filename.yml'
  502. )
  503. )
  504. def test_relative_path(self):
  505. relative_build_path = '../build-ctx/'
  506. service_dict = make_service_dict(
  507. 'relpath',
  508. {'build': relative_build_path},
  509. working_dir='tests/fixtures/build-path'
  510. )
  511. self.assertEquals(service_dict['build'], self.abs_context_path)
  512. def test_absolute_path(self):
  513. service_dict = make_service_dict(
  514. 'abspath',
  515. {'build': self.abs_context_path},
  516. working_dir='tests/fixtures/build-path'
  517. )
  518. self.assertEquals(service_dict['build'], self.abs_context_path)
  519. def test_from_file(self):
  520. service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
  521. self.assertEquals(service_dict, [{'name': 'foo', 'build': self.abs_context_path}])
  522. class GetConfigPathTestCase(unittest.TestCase):
  523. files = [
  524. 'docker-compose.yml',
  525. 'docker-compose.yaml',
  526. 'fig.yml',
  527. 'fig.yaml',
  528. ]
  529. def test_get_config_path_default_file_in_basedir(self):
  530. files = self.files
  531. self.assertEqual('docker-compose.yml', get_config_filename_for_files(files[0:]))
  532. self.assertEqual('docker-compose.yaml', get_config_filename_for_files(files[1:]))
  533. self.assertEqual('fig.yml', get_config_filename_for_files(files[2:]))
  534. self.assertEqual('fig.yaml', get_config_filename_for_files(files[3:]))
  535. with self.assertRaises(config.ComposeFileNotFound):
  536. get_config_filename_for_files([])
  537. def test_get_config_path_default_file_in_parent_dir(self):
  538. """Test with files placed in the subdir"""
  539. files = self.files
  540. def get_config_in_subdir(files):
  541. return get_config_filename_for_files(files, subdir=True)
  542. self.assertEqual('docker-compose.yml', get_config_in_subdir(files[0:]))
  543. self.assertEqual('docker-compose.yaml', get_config_in_subdir(files[1:]))
  544. self.assertEqual('fig.yml', get_config_in_subdir(files[2:]))
  545. self.assertEqual('fig.yaml', get_config_in_subdir(files[3:]))
  546. with self.assertRaises(config.ComposeFileNotFound):
  547. get_config_in_subdir([])
  548. def get_config_filename_for_files(filenames, subdir=None):
  549. def make_files(dirname, filenames):
  550. for fname in filenames:
  551. with open(os.path.join(dirname, fname), 'w') as f:
  552. f.write('')
  553. project_dir = tempfile.mkdtemp()
  554. try:
  555. make_files(project_dir, filenames)
  556. if subdir:
  557. base_dir = tempfile.mkdtemp(dir=project_dir)
  558. else:
  559. base_dir = project_dir
  560. return os.path.basename(config.get_config_path(base_dir))
  561. finally:
  562. shutil.rmtree(project_dir)